rpc/client: support getstateroot RPC

This commit is contained in:
Evgenii Stratonikov 2020-06-25 15:29:43 +03:00
parent 45a95b5242
commit b4b0ae0f51
2 changed files with 48 additions and 0 deletions

View file

@ -13,6 +13,7 @@ import (
"github.com/gorilla/websocket"
"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/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
@ -619,6 +620,27 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
},
},
},
"getstateroot": {
{
name: "positive",
invoke: func(c *Client) (interface{}, error) {
return c.GetStateRootByHeight(205)
},
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"stateroot":{"version":0,"index":205,"prehash":"0x3959779e0a406789a96dd86277d444e66eafbaa609b3c60ce18bea202bc30eeb","stateroot":"0x2ae87960aa8e373826fd6d190cc150ceb516e7a48523b0edad58beb54210de7c"},"flag":"Unverified"}}`,
result: func(c *Client) interface{} {
return &state.MPTRootState{
MPTRoot: state.MPTRoot{
MPTRootBase: state.MPTRootBase{
Index: 205,
PrevHash: util.Uint256{0xeb, 0xe, 0xc3, 0x2b, 0x20, 0xea, 0x8b, 0xe1, 0xc, 0xc6, 0xb3, 0x9, 0xa6, 0xba, 0xaf, 0x6e, 0xe6, 0x44, 0xd4, 0x77, 0x62, 0xd8, 0x6d, 0xa9, 0x89, 0x67, 0x40, 0xa, 0x9e, 0x77, 0x59, 0x39},
Root: util.Uint256{0x7c, 0xde, 0x10, 0x42, 0xb5, 0xbe, 0x58, 0xad, 0xed, 0xb0, 0x23, 0x85, 0xa4, 0xe7, 0x16, 0xb5, 0xce, 0x50, 0xc1, 0xc, 0x19, 0x6d, 0xfd, 0x26, 0x38, 0x37, 0x8e, 0xaa, 0x60, 0x79, 0xe8, 0x2a},
},
},
Flag: state.Unverified,
}
},
},
},
"getstorage": {
{
name: "positive",

26
pkg/rpc/client/state.go Normal file
View file

@ -0,0 +1,26 @@
package client
import (
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// GetStateRootByHeight returns state root for the given height.
func (c *Client) GetStateRootByHeight(h uint32) (*state.MPTRootState, error) {
return c.getStateRoot(request.NewRawParams(h))
}
// GetStateRootByHash returns state root for the given block hash.
func (c *Client) GetStateRootByHash(h util.Uint256) (*state.MPTRootState, error) {
return c.getStateRoot(request.NewRawParams(h))
}
func (c *Client) getStateRoot(p request.RawParams) (*state.MPTRootState, error) {
var resp state.MPTRootState
err := c.performRequest("getstateroot", p, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}