forked from TrueCloudLab/neoneo-go
rpc: adjust submitblock
RPC-call
It should return block hash instead of boolean.
This commit is contained in:
parent
c2534b1a0b
commit
889a5d7eb6
4 changed files with 22 additions and 20 deletions
|
@ -398,25 +398,22 @@ func (c *Client) SendRawTransaction(rawTX *transaction.Transaction) (util.Uint25
|
|||
}
|
||||
|
||||
// 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 (
|
||||
params request.RawParams
|
||||
resp bool
|
||||
resp = new(result.RelayResult)
|
||||
)
|
||||
buf := io.NewBufBinWriter()
|
||||
b.EncodeBinary(buf.BinWriter)
|
||||
if err := buf.Err; err != nil {
|
||||
return err
|
||||
return util.Uint256{}, err
|
||||
}
|
||||
params = request.NewRawParams(hex.EncodeToString(buf.Bytes()))
|
||||
|
||||
if err := c.performRequest("submitblock", params, &resp); err != nil {
|
||||
return err
|
||||
if err := c.performRequest("submitblock", params, resp); err != nil {
|
||||
return util.Uint256{}, err
|
||||
}
|
||||
if !resp {
|
||||
return errors.New("submitblock returned false")
|
||||
}
|
||||
return nil
|
||||
return resp.Hash, nil
|
||||
}
|
||||
|
||||
// SignAndPushInvocationTx signs and pushes given script as an invocation
|
||||
|
|
|
@ -716,16 +716,19 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
|
|||
{
|
||||
name: "positive",
|
||||
invoke: func(c *Client) (interface{}, error) {
|
||||
return nil, c.SubmitBlock(block.Block{
|
||||
return c.SubmitBlock(block.Block{
|
||||
Base: block.Base{},
|
||||
Transactions: nil,
|
||||
Trimmed: false,
|
||||
})
|
||||
},
|
||||
serverResponse: `{"jsonrpc":"2.0","id":1,"result":true}`,
|
||||
serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"hash":"0x1bdea8f80eb5bd97fade38d5e7fb93b02c9d3e01394e9f4324218132293f7ea6"}}`,
|
||||
result: func(c *Client) interface{} {
|
||||
// no error expected
|
||||
return nil
|
||||
h, err := util.Uint256DecodeStringLE("1bdea8f80eb5bd97fade38d5e7fb93b02c9d3e01394e9f4324218132293f7ea6")
|
||||
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",
|
||||
invoke: func(c *Client) (interface{}, error) {
|
||||
return nil, c.SubmitBlock(block.Block{
|
||||
return c.SubmitBlock(block.Block{
|
||||
Base: block.Base{},
|
||||
Transactions: nil,
|
||||
Trimmed: false,
|
||||
|
@ -996,7 +999,7 @@ var rpcClientErrorCases = map[string][]rpcClientErrorCase{
|
|||
{
|
||||
name: "submitblock_invalid_params_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",
|
||||
invoke: func(c *Client) (interface{}, error) {
|
||||
return nil, c.SubmitBlock(block.Block{
|
||||
return c.SubmitBlock(block.Block{
|
||||
Base: block.Base{},
|
||||
Transactions: nil,
|
||||
Trimmed: false,
|
||||
|
|
|
@ -902,7 +902,9 @@ func (s *Server) submitBlock(reqParams request.Params) (interface{}, *response.E
|
|||
return nil, response.ErrValidationFailed
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
return &result.RelayResult{
|
||||
Hash: b.Hash(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) sendrawtransaction(reqParams request.Params) (interface{}, *response.Error) {
|
||||
|
|
|
@ -760,9 +760,9 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
|
|||
b := newBlock(t, chain, 1, 0, newTx())
|
||||
body := doRPCCall(fmt.Sprintf(rpc, encodeBlock(t, b)), httpSrv.URL, t)
|
||||
data := checkErrGetResult(t, body, false)
|
||||
var res bool
|
||||
require.NoError(t, json.Unmarshal(data, &res))
|
||||
require.True(t, res)
|
||||
var res = new(result.RelayResult)
|
||||
require.NoError(t, json.Unmarshal(data, res))
|
||||
require.Equal(t, b.Hash(), res.Hash)
|
||||
})
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue