Skip to content

Commit

Permalink
Merge pull request #173 from kcalvinalvin/2024-05-01-add-getbeststate…
Browse files Browse the repository at this point in the history
…-rpc

btcjson, main, utreexoctl: add getbeststate command
  • Loading branch information
kcalvinalvin committed May 2, 2024
2 parents 7010aed + 7de71f5 commit ea4136e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
10 changes: 10 additions & 0 deletions btcjson/chainsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ func NewGetBlockCountCmd() *GetBlockCountCmd {
return &GetBlockCountCmd{}
}

// GetBestStateCmd defines the getbeststate JSON-RPC command.
type GetBestStateCmd struct{}

// NewGetBestState returns a new instance which can be used to issue a
// getbeststate JSON-RPC command.
func NewGetBestState() *GetBestStateCmd {
return &GetBestStateCmd{}
}

// FilterTypeName defines the type used in the getblockfilter JSON-RPC command for the
// filter type field.
type FilterTypeName string
Expand Down Expand Up @@ -1273,6 +1282,7 @@ func init() {
MustRegisterCmd("fundrawtransaction", (*FundRawTransactionCmd)(nil), flags)
MustRegisterCmd("getaddednodeinfo", (*GetAddedNodeInfoCmd)(nil), flags)
MustRegisterCmd("getbestblockhash", (*GetBestBlockHashCmd)(nil), flags)
MustRegisterCmd("getbeststate", (*GetBestStateCmd)(nil), flags)
MustRegisterCmd("getblock", (*GetBlockCmd)(nil), flags)
MustRegisterCmd("getblockchaininfo", (*GetBlockChainInfoCmd)(nil), flags)
MustRegisterCmd("getblockcount", (*GetBlockCountCmd)(nil), flags)
Expand Down
12 changes: 12 additions & 0 deletions btcjson/chainsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ type GetBlockVerboseTxResult struct {
NextHash string `json:"nextblockhash,omitempty"`
}

// GetBestStateResult models the data from the getbeststate command.
type GetBestStateResult struct {
Hash string `json:"hash"` // The hash of the block.
Height int32 `json:"height"` // The height of the block.
Bits uint32 `json:"bits"` // The difficulty bits of the block.
BlockSize uint64 `json:"blocksize"` // The size of the block.
BlockWeight uint64 `json:"blockweight"` // The weight of the block.
NumTxns uint64 `json:"numtxns"` // The number of txns in the block.
TotalTxns uint64 `json:"totaltxns"` // The total number of txns in the chain.
MedianTime int64 `json:"mediantime"` // Median time as per CalcPastMedianTime.
}

// GetChainTipsResult models the data from the getchaintips command.
type GetChainTipsResult struct {
Height int32 `json:"height"`
Expand Down
2 changes: 1 addition & 1 deletion cmd/utreexoctl/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr
// versioning 2.0.0 spec (http://semver.org/).
const (
appMajor uint = 0
appMinor uint = 1
appMinor uint = 2
appPatch uint = 0

// appPreRelease MUST only contain characters from semanticAlphabet
Expand Down
19 changes: 19 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
"getaddednodeinfo": handleGetAddedNodeInfo,
"getbestblock": handleGetBestBlock,
"getbestblockhash": handleGetBestBlockHash,
"getbeststate": handleGetBestState,
"getblock": handleGetBlock,
"getblockchaininfo": handleGetBlockChainInfo,
"getblockcount": handleGetBlockCount,
Expand Down Expand Up @@ -285,6 +286,7 @@ var rpcLimited = map[string]struct{}{
"estimatefee": {},
"getbestblock": {},
"getbestblockhash": {},
"getbeststate": {},
"getblock": {},
"getblockcount": {},
"getblockhash": {},
Expand Down Expand Up @@ -1191,6 +1193,23 @@ func handleGetBestBlockHash(s *rpcServer, cmd interface{}, closeChan <-chan stru
return best.Hash.String(), nil
}

// handleGetBestState implements the getbeststate command.
func handleGetBestState(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
best := s.cfg.Chain.BestSnapshot()
reply := btcjson.GetBestStateResult{
Hash: best.Hash.String(),
Height: best.Height,
Bits: best.Bits,
BlockSize: best.BlockSize,
BlockWeight: best.BlockWeight,
NumTxns: best.NumTxns,
TotalTxns: best.TotalTxns,
MedianTime: best.MedianTime.Unix(),
}

return reply, nil
}

// getDifficultyRatio returns the proof-of-work difficulty as a multiple of the
// minimum difficulty using the passed bits field from the header of a block.
func getDifficultyRatio(bits uint32, params *chaincfg.Params) float64 {
Expand Down
14 changes: 14 additions & 0 deletions rpcserverhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ var helpDescsEnUS = map[string]string{
"getbestblockhash--synopsis": "Returns the hash of the of the best (most recent) block in the longest block chain.",
"getbestblockhash--result0": "The hex-encoded block hash",

// GetBestStateCmd help.
"getbeststate--synopsis": "Returns the current best state and the information associated with it in the longest block chain.",

// GetBestStateResult help.
"getbeststateresult-hash": "The hash of the block",
"getbeststateresult-height": "The height of the block",
"getbeststateresult-bits": "The difficultly bits of the block",
"getbeststateresult-blocksize": "The blocksize of the block",
"getbeststateresult-blockweight": "The blockweight of the block",
"getbeststateresult-numtxns": "The number of transactions in the block",
"getbeststateresult-totaltxns": "The total number of transactions in the longest chain",
"getbeststateresult-mediantime": "The median time of the block in unix time",

// GetBlockCmd help.
"getblock--synopsis": "Returns information about a block given its hash.",
"getblock-hash": "The hash of the block",
Expand Down Expand Up @@ -894,6 +907,7 @@ var rpcResultTypes = map[string][]interface{}{
"getaddednodeinfo": {(*[]string)(nil), (*[]btcjson.GetAddedNodeInfoResult)(nil)},
"getbestblock": {(*btcjson.GetBestBlockResult)(nil)},
"getbestblockhash": {(*string)(nil)},
"getbeststate": {(*btcjson.GetBestStateResult)(nil)},
"getblock": {(*string)(nil), (*btcjson.GetBlockVerboseResult)(nil)},
"getblockcount": {(*int64)(nil)},
"getblockhash": {(*string)(nil)},
Expand Down

0 comments on commit ea4136e

Please sign in to comment.