rpc: adjust submitblock RPC-call

It should return block hash instead of boolean.
This commit is contained in:
Anna Shaleva 2020-07-21 10:41:18 +03:00
parent c2534b1a0b
commit 889a5d7eb6
4 changed files with 22 additions and 20 deletions

View file

@ -398,25 +398,22 @@ func (c *Client) SendRawTransaction(rawTX *transaction.Transaction) (util.Uint25
} }
// SubmitBlock broadcasts a raw block over the NEO network. // SubmitBlock broadcasts a raw block over the NEO network.
func (c *Client) SubmitBlock(b block.Block) error { func (c *Client) SubmitBlock(b block.Block) (util.Uint256, error) {
var ( var (
params request.RawParams params request.RawParams
resp bool resp = new(result.RelayResult)
) )
buf := io.NewBufBinWriter() buf := io.NewBufBinWriter()
b.EncodeBinary(buf.BinWriter) b.EncodeBinary(buf.BinWriter)
if err := buf.Err; err != nil { if err := buf.Err; err != nil {
return err return util.Uint256{}, err
} }
params = request.NewRawParams(hex.EncodeToString(buf.Bytes())) params = request.NewRawParams(hex.EncodeToString(buf.Bytes()))
if err := c.performRequest("submitblock", params, &resp); err != nil { if err := c.performRequest("submitblock", params, resp); err != nil {
return err return util.Uint256{}, err
} }
if !resp { return resp.Hash, nil
return errors.New("submitblock returned false")
}
return nil
} }
// SignAndPushInvocationTx signs and pushes given script as an invocation // SignAndPushInvocationTx signs and pushes given script as an invocation

View file

@ -716,16 +716,19 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
{ {
name: "positive", name: "positive",
invoke: func(c *Client) (interface{}, error) { invoke: func(c *Client) (interface{}, error) {
return nil, c.SubmitBlock(block.Block{ return c.SubmitBlock(block.Block{
Base: block.Base{}, Base: block.Base{},
Transactions: nil, Transactions: nil,
Trimmed: false, Trimmed: false,
}) })
}, },
serverResponse: `{"jsonrpc":"2.0","id":1,"result":true}`, serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"hash":"0x1bdea8f80eb5bd97fade38d5e7fb93b02c9d3e01394e9f4324218132293f7ea6"}}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
// no error expected h, err := util.Uint256DecodeStringLE("1bdea8f80eb5bd97fade38d5e7fb93b02c9d3e01394e9f4324218132293f7ea6")
return nil if err != nil {
panic(fmt.Errorf("can't decode `submitblock` result hash: %v", err))
}
return h
}, },
}, },
}, },
@ -840,7 +843,7 @@ var rpcClientErrorCases = map[string][]rpcClientErrorCase{
{ {
name: "submitblock_bad_server_answer", name: "submitblock_bad_server_answer",
invoke: func(c *Client) (interface{}, error) { invoke: func(c *Client) (interface{}, error) {
return nil, c.SubmitBlock(block.Block{ return c.SubmitBlock(block.Block{
Base: block.Base{}, Base: block.Base{},
Transactions: nil, Transactions: nil,
Trimmed: false, Trimmed: false,
@ -996,7 +999,7 @@ var rpcClientErrorCases = map[string][]rpcClientErrorCase{
{ {
name: "submitblock_invalid_params_error", name: "submitblock_invalid_params_error",
invoke: func(c *Client) (interface{}, error) { invoke: func(c *Client) (interface{}, error) {
return nil, c.SubmitBlock(block.Block{}) return c.SubmitBlock(block.Block{})
}, },
}, },
{ {
@ -1178,7 +1181,7 @@ var rpcClientErrorCases = map[string][]rpcClientErrorCase{
{ {
name: "submitblock_unmarshalling_error", name: "submitblock_unmarshalling_error",
invoke: func(c *Client) (interface{}, error) { invoke: func(c *Client) (interface{}, error) {
return nil, c.SubmitBlock(block.Block{ return c.SubmitBlock(block.Block{
Base: block.Base{}, Base: block.Base{},
Transactions: nil, Transactions: nil,
Trimmed: false, Trimmed: false,

View file

@ -902,7 +902,9 @@ func (s *Server) submitBlock(reqParams request.Params) (interface{}, *response.E
return nil, response.ErrValidationFailed return nil, response.ErrValidationFailed
} }
} }
return true, nil return &result.RelayResult{
Hash: b.Hash(),
}, nil
} }
func (s *Server) sendrawtransaction(reqParams request.Params) (interface{}, *response.Error) { func (s *Server) sendrawtransaction(reqParams request.Params) (interface{}, *response.Error) {

View file

@ -760,9 +760,9 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
b := newBlock(t, chain, 1, 0, newTx()) b := newBlock(t, chain, 1, 0, newTx())
body := doRPCCall(fmt.Sprintf(rpc, encodeBlock(t, b)), httpSrv.URL, t) body := doRPCCall(fmt.Sprintf(rpc, encodeBlock(t, b)), httpSrv.URL, t)
data := checkErrGetResult(t, body, false) data := checkErrGetResult(t, body, false)
var res bool var res = new(result.RelayResult)
require.NoError(t, json.Unmarshal(data, &res)) require.NoError(t, json.Unmarshal(data, res))
require.True(t, res) require.Equal(t, b.Hash(), res.Hash)
}) })
}) })