Implement submitblock.

Closes #61.
This commit is contained in:
David Hill 2014-01-15 10:01:12 -05:00 committed by Dave Collins
parent e3122c1b1d
commit 871481ce1b
2 changed files with 42 additions and 1 deletions

View file

@ -108,7 +108,7 @@ var rpcHandlers = map[string]commandHandler{
"signmessage": handleAskWallet, "signmessage": handleAskWallet,
"signrawtransaction": handleAskWallet, "signrawtransaction": handleAskWallet,
"stop": handleStop, "stop": handleStop,
"submitblock": handleUnimplemented, "submitblock": handleSubmitBlock,
"validateaddress": handleAskWallet, "validateaddress": handleAskWallet,
"verifychain": handleVerifyChain, "verifychain": handleVerifyChain,
"verifymessage": handleAskWallet, "verifymessage": handleAskWallet,
@ -1062,6 +1062,36 @@ func handleStop(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
return "btcd stopping.", nil return "btcd stopping.", nil
} }
// handleSubmitBlock implements the submitblock command.
func handleSubmitBlock(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
c := cmd.(*btcjson.SubmitBlockCmd)
// Deserialize and send off to block processor.
serializedBlock, err := hex.DecodeString(c.HexBlock)
if err != nil {
err := btcjson.Error{
Code: btcjson.ErrDeserialization.Code,
Message: "Block decode failed",
}
return nil, err
}
block, err := btcutil.NewBlockFromBytes(serializedBlock)
if err != nil {
err := btcjson.Error{
Code: btcjson.ErrDeserialization.Code,
Message: "Block decode failed",
}
return nil, err
}
err = s.server.blockManager.blockChain.ProcessBlock(block, false)
if err != nil {
return fmt.Sprintf("rejected: %s", err.Error()), nil
}
return nil, nil
}
func verifyChain(db btcdb.Db, level, depth int32) error { func verifyChain(db btcdb.Db, level, depth int32) error {
_, curheight64, err := db.NewestSha() _, curheight64, err := db.NewestSha()
if err != nil { if err != nil {

View file

@ -69,6 +69,7 @@ var commandHandlers = map[string]*handlerData{
"verifychain": &handlerData{0, 2, displayJSONDump, []conversionHandler{toInt, toInt}, makeVerifyChain, "[level] [numblocks]"}, "verifychain": &handlerData{0, 2, displayJSONDump, []conversionHandler{toInt, toInt}, makeVerifyChain, "[level] [numblocks]"},
"sendrawtransaction": &handlerData{1, 0, displayGeneric, nil, makeSendRawTransaction, "<hextx>"}, "sendrawtransaction": &handlerData{1, 0, displayGeneric, nil, makeSendRawTransaction, "<hextx>"},
"stop": &handlerData{0, 0, displayGeneric, nil, makeStop, ""}, "stop": &handlerData{0, 0, displayGeneric, nil, makeStop, ""},
"submitblock": &handlerData{1, 1, displayGeneric, nil, makeSubmitBlock, "<hexdata> [jsonparametersobject]"},
} }
// toInt attempts to convert the passed string to an integer. It returns the // toInt attempts to convert the passed string to an integer. It returns the
@ -354,6 +355,16 @@ func makeStop(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewStopCmd("btcctl") return btcjson.NewStopCmd("btcctl")
} }
// makeSubmitBlock generates the cmd structure for submitblock commands.
func makeSubmitBlock(args []interface{}) (btcjson.Cmd, error) {
opts := &btcjson.SubmitBlockOptions{}
if len(args) == 2 {
opts.WorkId = args[1].(string)
}
return btcjson.NewSubmitBlockCmd("btcctl", args[0].(string), opts)
}
// makeVerifyChain generates the cmd structure for verifychain comands. // makeVerifyChain generates the cmd structure for verifychain comands.
func makeVerifyChain(args []interface{}) (btcjson.Cmd, error) { func makeVerifyChain(args []interface{}) (btcjson.Cmd, error) {
iargs := make([]int32, 0, 2) iargs := make([]int32, 0, 2)