Skip to content

Commit 0aa80ea

Browse files
kcalvinalvinCrypt-iQ
authored andcommitted
main: Add invalidateblock and reconsiderblock rpc commands
The rpc calls and the rpchelp is added for the invalidateblock and reconsiderblock methods on BlockChain.
1 parent 1cb4d3a commit 0aa80ea

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

rpcclient/chain.go

+35
Original file line numberDiff line numberDiff line change
@@ -1419,3 +1419,38 @@ func (c *Client) GetDescriptorInfoAsync(descriptor string) FutureGetDescriptorIn
14191419
func (c *Client) GetDescriptorInfo(descriptor string) (*btcjson.GetDescriptorInfoResult, error) {
14201420
return c.GetDescriptorInfoAsync(descriptor).Receive()
14211421
}
1422+
1423+
// FutureReconsiderBlockResult is a future promise to deliver the result of a
1424+
// ReconsiderBlockAsync RPC invocation (or an applicable error).
1425+
type FutureReconsiderBlockResult chan *Response
1426+
1427+
// Receive waits for the Response promised by the future and returns the raw
1428+
// block requested from the server given its hash.
1429+
func (r FutureReconsiderBlockResult) Receive() error {
1430+
_, err := ReceiveFuture(r)
1431+
return err
1432+
}
1433+
1434+
// ReconsiderBlockAsync returns an instance of a type that can be used to get the
1435+
// result of the RPC at some future time by invoking the Receive function on the
1436+
// returned instance.
1437+
//
1438+
// See ReconsiderBlock for the blocking version and more details.
1439+
func (c *Client) ReconsiderBlockAsync(
1440+
blockHash *chainhash.Hash) FutureReconsiderBlockResult {
1441+
1442+
hash := ""
1443+
if blockHash != nil {
1444+
hash = blockHash.String()
1445+
}
1446+
1447+
cmd := btcjson.NewReconsiderBlockCmd(hash)
1448+
return c.SendCmd(cmd)
1449+
}
1450+
1451+
// ReconsiderBlock reconsiders an verifies a specific block and the branch that
1452+
// the block is included in. If the block is valid on reconsideration, the chain
1453+
// will reorg to that block if it has more PoW than the current tip.
1454+
func (c *Client) ReconsiderBlock(blockHash *chainhash.Hash) error {
1455+
return c.ReconsiderBlockAsync(blockHash).Receive()
1456+
}

rpcserver.go

+38-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
170170
"getrawtransaction": handleGetRawTransaction,
171171
"gettxout": handleGetTxOut,
172172
"help": handleHelp,
173+
"invalidateblock": handleInvalidateBlock,
173174
"node": handleNode,
174175
"ping": handlePing,
176+
"reconsiderblock": handleReconsiderBlock,
175177
"searchrawtransactions": handleSearchRawTransactions,
176178
"sendrawtransaction": handleSendRawTransaction,
177179
"setgenerate": handleSetGenerate,
@@ -241,9 +243,7 @@ var rpcUnimplemented = map[string]struct{}{
241243
"getmempoolentry": {},
242244
"getnetworkinfo": {},
243245
"getwork": {},
244-
"invalidateblock": {},
245246
"preciousblock": {},
246-
"reconsiderblock": {},
247247
}
248248

249249
// Commands that are available to a limited user
@@ -284,6 +284,8 @@ var rpcLimited = map[string]struct{}{
284284
"getrawmempool": {},
285285
"getrawtransaction": {},
286286
"gettxout": {},
287+
"invalidateblock": {},
288+
"reconsiderblock": {},
287289
"searchrawtransactions": {},
288290
"sendrawtransaction": {},
289291
"submitblock": {},
@@ -2850,6 +2852,23 @@ func handleGetTxOut(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
28502852
return txOutReply, nil
28512853
}
28522854

2855+
// handleInvalidateBlock implements the invalidateblock command.
2856+
func handleInvalidateBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
2857+
c := cmd.(*btcjson.InvalidateBlockCmd)
2858+
2859+
invalidateHash, err := chainhash.NewHashFromStr(c.BlockHash)
2860+
if err != nil {
2861+
return nil, &btcjson.RPCError{
2862+
Code: btcjson.ErrRPCDeserialization,
2863+
Message: fmt.Sprintf("Failed to deserialize blockhash from string of %s",
2864+
invalidateHash),
2865+
}
2866+
}
2867+
2868+
err = s.cfg.Chain.InvalidateBlock(invalidateHash)
2869+
return nil, err
2870+
}
2871+
28532872
// handleHelp implements the help command.
28542873
func handleHelp(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
28552874
c := cmd.(*btcjson.HelpCmd)
@@ -3123,6 +3142,23 @@ func fetchMempoolTxnsForAddress(s *rpcServer, addr btcutil.Address, numToSkip, n
31233142
return mpTxns[numToSkip:rangeEnd], numToSkip
31243143
}
31253144

3145+
// handleReconsiderBlock implements the reconsiderblock command.
3146+
func handleReconsiderBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
3147+
c := cmd.(*btcjson.ReconsiderBlockCmd)
3148+
3149+
reconsiderHash, err := chainhash.NewHashFromStr(c.BlockHash)
3150+
if err != nil {
3151+
return nil, &btcjson.RPCError{
3152+
Code: btcjson.ErrRPCDeserialization,
3153+
Message: fmt.Sprintf("Failed to deserialize blockhash from string of %s",
3154+
reconsiderHash),
3155+
}
3156+
}
3157+
3158+
err = s.cfg.Chain.ReconsiderBlock(reconsiderHash)
3159+
return nil, err
3160+
}
3161+
31263162
// handleSearchRawTransactions implements the searchrawtransactions command.
31273163
func handleSearchRawTransactions(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
31283164
// Respond with an error if the address index is not enabled.

rpcserverhelp.go

+10
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,10 @@ var helpDescsEnUS = map[string]string{
544544
"gettxout-vout": "The index of the output",
545545
"gettxout-includemempool": "Include the mempool when true",
546546

547+
// InvalidateBlockCmd help.
548+
"invalidateblock--synopsis": "Invalidates the block of the given block hash. To re-validate the invalidated block, use the reconsiderblock rpc",
549+
"invalidateblock-blockhash": "The block hash of the block to invalidate",
550+
547551
// HelpCmd help.
548552
"help--synopsis": "Returns a list of all commands or help for a specified command.",
549553
"help-command": "The command to retrieve help for",
@@ -681,6 +685,10 @@ var helpDescsEnUS = map[string]string{
681685
"loadtxfilter-addresses": "Array of addresses to add to the transaction filter",
682686
"loadtxfilter-outpoints": "Array of outpoints to add to the transaction filter",
683687

688+
// ReconsiderBlockCmd help.
689+
"reconsiderblock--synopsis": "Reconsiders the block of the given block hash. Can be used to re-validate blocks invalidated with invalidateblock",
690+
"reconsiderblock-blockhash": "The block hash of the block to reconsider",
691+
684692
// Rescan help.
685693
"rescan--synopsis": "Rescan block chain for transactions to addresses.\n" +
686694
"When the endblock parameter is omitted, the rescan continues through the best block in the main chain.\n" +
@@ -788,7 +796,9 @@ var rpcResultTypes = map[string][]interface{}{
788796
"gettxout": {(*btcjson.GetTxOutResult)(nil)},
789797
"node": nil,
790798
"help": {(*string)(nil), (*string)(nil)},
799+
"invalidateblock": nil,
791800
"ping": nil,
801+
"reconsiderblock": nil,
792802
"searchrawtransactions": {(*string)(nil), (*[]btcjson.SearchRawTransactionsResult)(nil)},
793803
"sendrawtransaction": {(*string)(nil)},
794804
"setgenerate": nil,

0 commit comments

Comments
 (0)