rpc: implement submitblock RPC

closes #344
This commit is contained in:
Anna Shaleva 2020-03-02 20:01:32 +03:00
parent d3063c26e1
commit a746d8e6e6
6 changed files with 108 additions and 1 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/nspcc-dev/neo-go/config"
"github.com/nspcc-dev/neo-go/pkg/core"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
@ -302,6 +303,10 @@ Methods:
case "invokescript":
results, resultsErr = s.invokescript(reqParams)
case "submitblock":
submitblockCalled.Inc()
results, resultsErr = s.submitBlock(reqParams)
case "sendrawtransaction":
sendrawtransactionCalled.Inc()
results, resultsErr = s.sendrawtransaction(reqParams)
@ -808,6 +813,34 @@ func (s *Server) runScriptInVM(script []byte) *result.Invoke {
return result
}
// submitBlock broadcasts a raw block over the NEO network.
func (s *Server) submitBlock(reqParams request.Params) (interface{}, error) {
param, ok := reqParams.ValueWithType(0, request.StringT)
if !ok {
return nil, response.ErrInvalidParams
}
blockBytes, err := param.GetBytesHex()
if err != nil {
return nil, response.ErrInvalidParams
}
b := block.Block{}
r := io.NewBinReaderFromBuf(blockBytes)
b.DecodeBinary(r)
if r.Err != nil {
return nil, response.ErrInvalidParams
}
err = s.chain.AddBlock(&b)
if err != nil {
switch err {
case core.ErrInvalidBlockIndex, core.ErrAlreadyExists:
return nil, response.ErrAlreadyExists
default:
return nil, response.ErrValidationFailed
}
}
return true, nil
}
func (s *Server) sendrawtransaction(reqParams request.Params) (interface{}, error) {
var resultsErr error
var results interface{}