Skip to content

Commit

Permalink
btcjson, main: Implement the getchaintips call
Browse files Browse the repository at this point in the history
getchaintips call is implemented and the behavior mimics that of Bitcoin
Core. Resolves #1912.
  • Loading branch information
kcalvinalvin committed Jul 16, 2023
1 parent 46bfb78 commit 892ae67
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
8 changes: 8 additions & 0 deletions btcjson/chainsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ type GetBlockVerboseTxResult struct {
NextHash string `json:"nextblockhash,omitempty"`
}

// GetChainTipsResult models the data from the getchaintips command.
type GetChainTipsResult struct {
Height int32 `json:"height"`
Hash string `json:"hash"`
BranchLen int32 `json:"branchlen"`
Status string `json:"status"`
}

// GetChainTxStatsResult models the data from the getchaintxstats command.
type GetChainTxStatsResult struct {
Time int64 `json:"time"`
Expand Down
25 changes: 24 additions & 1 deletion rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
"getblockhash": handleGetBlockHash,
"getblockheader": handleGetBlockHeader,
"getblocktemplate": handleGetBlockTemplate,
"getchaintips": handleGetChainTips,
"getcfilter": handleGetCFilter,
"getcfilterheader": handleGetCFilterHeader,
"getconnectioncount": handleGetConnectionCount,
Expand Down Expand Up @@ -231,7 +232,6 @@ var rpcAskWallet = map[string]struct{}{
// Commands that are currently unimplemented, but should ultimately be.
var rpcUnimplemented = map[string]struct{}{
"estimatepriority": {},
"getchaintips": {},
"getmempoolentry": {},
"getnetworkinfo": {},
"getwork": {},
Expand Down Expand Up @@ -266,6 +266,7 @@ var rpcLimited = map[string]struct{}{
"getblockcount": {},
"getblockhash": {},
"getblockheader": {},
"getchaintips": {},
"getcfilter": {},
"getcfilterheader": {},
"getcurrentnet": {},
Expand Down Expand Up @@ -2192,6 +2193,28 @@ func handleGetBlockTemplate(s *rpcServer, cmd interface{}, closeChan <-chan stru
}
}

// handleGetChainTips implements the getchaintips command.
func handleGetChainTips(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
chainTips := s.cfg.Chain.ChainTips()

ret := make([]btcjson.GetChainTipsResult, 0, len(chainTips))
for _, chainTip := range chainTips {
ret = append(ret, struct {
Height int32 "json:\"height\""
Hash string "json:\"hash\""
BranchLen int32 "json:\"branchlen\""
Status string "json:\"status\""
}{
Height: chainTip.Height,
Hash: chainTip.BlockHash.String(),
BranchLen: chainTip.BranchLen,
Status: chainTip.Status.String(),
})
}

return ret, nil
}

// handleGetCFilter implements the getcfilter command.
func handleGetCFilter(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
if s.cfg.CfIndex == nil {
Expand Down
10 changes: 10 additions & 0 deletions rpcserverhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@ var helpDescsEnUS = map[string]string{
"getblocktemplate--condition2": "mode=proposal, accepted",
"getblocktemplate--result1": "An error string which represents why the proposal was rejected or nothing if accepted",

// GetChainTipsResult help.
"getchaintipsresult-chaintips": "The chaintips that this node is aware of",
"getchaintipsresult-height": "The height of the chain tip",
"getchaintipsresult-hash": "The block hash of the chain tip",
"getchaintipsresult-branchlen": "Returns zero for main chain. Otherwise is the length of branch connecting the tip to the main chain",
"getchaintipsresult-status": "Status of the chain. Returns \"active\" for the main chain",
// GetChainTipsCmd help.
"getchaintips--synopsis": "Returns information about all known tips in the block tree, including the main chain as well as orphaned branches.",

// GetCFilterCmd help.
"getcfilter--synopsis": "Returns a block's committed filter given its hash.",
"getcfilter-filtertype": "The type of filter to return (0=regular)",
Expand Down Expand Up @@ -728,6 +737,7 @@ var rpcResultTypes = map[string][]interface{}{
"getblockheader": {(*string)(nil), (*btcjson.GetBlockHeaderVerboseResult)(nil)},
"getblocktemplate": {(*btcjson.GetBlockTemplateResult)(nil), (*string)(nil), nil},
"getblockchaininfo": {(*btcjson.GetBlockChainInfoResult)(nil)},
"getchaintips": {(*[]btcjson.GetChainTipsResult)(nil)},
"getcfilter": {(*string)(nil)},
"getcfilterheader": {(*string)(nil)},
"getconnectioncount": {(*int32)(nil)},
Expand Down

0 comments on commit 892ae67

Please sign in to comment.