From 45a95b5242e2cd10190369137b6a137552a1a328 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 25 Jun 2020 16:19:35 +0300 Subject: [PATCH 1/5] rpc/server: remove debug `fmt.Println()` from tests --- pkg/rpc/server/server_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index 509867121..222a74853 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -984,9 +984,7 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getproof", "params": ["%s", "%s", "%x"]}`, r.Root.StringLE(), testContractHash, []byte("testkey")) - fmt.Println(rpc) body := doRPCCall(rpc, httpSrv.URL, t) - fmt.Println(string(body)) rawRes := checkErrGetResult(t, body, false) res := new(result.GetProof) require.NoError(t, json.Unmarshal(rawRes, res)) @@ -1008,7 +1006,6 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] t.Run("getstateroot", func(t *testing.T) { testRoot := func(t *testing.T, p string) { rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getstateroot", "params": [%s]}`, p) - fmt.Println(rpc) body := doRPCCall(rpc, httpSrv.URL, t) rawRes := checkErrGetResult(t, body, false) From b4b0ae0f51a1794540a19f420276fe54fa663697 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 25 Jun 2020 15:29:43 +0300 Subject: [PATCH 2/5] rpc/client: support `getstateroot` RPC --- pkg/rpc/client/rpc_test.go | 22 ++++++++++++++++++++++ pkg/rpc/client/state.go | 26 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 pkg/rpc/client/state.go diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index f21b5b2ad..501fd15d1 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -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", diff --git a/pkg/rpc/client/state.go b/pkg/rpc/client/state.go new file mode 100644 index 000000000..499ef9060 --- /dev/null +++ b/pkg/rpc/client/state.go @@ -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 +} From d8dddabc86f82a7db00a97e831d333b7e03cece1 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 25 Jun 2020 15:35:32 +0300 Subject: [PATCH 3/5] rpc/client: support `getstateheight` RPC --- pkg/rpc/client/rpc_test.go | 15 +++++++++++++++ pkg/rpc/client/state.go | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index 501fd15d1..636c0ffbd 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -620,6 +620,21 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{ }, }, }, + "getstateheight": { + { + name: "positive", + invoke: func(c *Client) (interface{}, error) { + return c.GetStateHeight() + }, + serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"blockHeight":208,"stateHeight":200}}`, + result: func(c *Client) interface{} { + return &result.StateHeight{ + BlockHeight: 208, + StateHeight: 200, + } + }, + }, + }, "getstateroot": { { name: "positive", diff --git a/pkg/rpc/client/state.go b/pkg/rpc/client/state.go index 499ef9060..6dbc324cf 100644 --- a/pkg/rpc/client/state.go +++ b/pkg/rpc/client/state.go @@ -3,6 +3,7 @@ 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/rpc/response/result" "github.com/nspcc-dev/neo-go/pkg/util" ) @@ -24,3 +25,13 @@ func (c *Client) getStateRoot(p request.RawParams) (*state.MPTRootState, error) } return &resp, nil } + +// GetStateHeight returns current block and state height. +func (c *Client) GetStateHeight() (*result.StateHeight, error) { + resp := new(result.StateHeight) + err := c.performRequest("getstateheight", request.NewRawParams(), resp) + if err != nil { + return nil, err + } + return resp, nil +} From 654877fb1bd1cbec2aff10b0364cf57b56e51dff Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 25 Jun 2020 15:52:51 +0300 Subject: [PATCH 4/5] rpc/client: support `getproof` RPC --- pkg/rpc/client/rpc_test.go | 20 ++++++++++++++++++++ pkg/rpc/client/state.go | 13 +++++++++++++ 2 files changed, 33 insertions(+) diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index 636c0ffbd..cf89eeae9 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -635,6 +635,26 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{ }, }, }, + "getproof": { + { + name: "positive", + serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"proof":"256f20ccfbd5f01d5b9633387428b8bab95a9e78c2746573746b657900000000000000000009026d014a060f02000c0c0f0b0d050f00010d050b090603030308070402080b080b0a0b09050a090e07080c020704060507030704060b0605070900000000000000000000000000000000000000092068ae9a1a2c6b6b64216df47b4c44086141569d5d20dc9bd65fdfabed54d2c3250e030c00097465737476616c756500","success":true}}`, + invoke: func(c *Client) (interface{}, error) { + return c.GetProof(util.Uint256{}, util.Uint160{}, []byte{}) + }, + result: func(c *Client) interface{} { + var p result.ProofWithKey + err := p.FromString("256f20ccfbd5f01d5b9633387428b8bab95a9e78c2746573746b657900000000000000000009026d014a060f02000c0c0f0b0d050f00010d050b090603030308070402080b080b0a0b09050a090e07080c020704060507030704060b0605070900000000000000000000000000000000000000092068ae9a1a2c6b6b64216df47b4c44086141569d5d20dc9bd65fdfabed54d2c3250e030c00097465737476616c756500") + if err != nil { + panic(err) + } + return &result.GetProof{ + Result: p, + Success: true, + } + }, + }, + }, "getstateroot": { { name: "positive", diff --git a/pkg/rpc/client/state.go b/pkg/rpc/client/state.go index 6dbc324cf..b3259279d 100644 --- a/pkg/rpc/client/state.go +++ b/pkg/rpc/client/state.go @@ -1,6 +1,8 @@ package client import ( + "encoding/hex" + "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/rpc/response/result" @@ -35,3 +37,14 @@ func (c *Client) GetStateHeight() (*result.StateHeight, error) { } return resp, nil } + +// GetProof returns proof that key belongs to a contract sc state rooted at root. +func (c *Client) GetProof(root util.Uint256, sc util.Uint160, key []byte) (*result.GetProof, error) { + var resp result.GetProof + ps := request.NewRawParams(root, sc, hex.EncodeToString(key)) + err := c.performRequest("getproof", ps, &resp) + if err != nil { + return nil, err + } + return &resp, nil +} From eb37f92881273b3ba3d55e56f8e7192428ff6f35 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 25 Jun 2020 15:56:05 +0300 Subject: [PATCH 5/5] rpc/client: support `verifyproof` RPC --- pkg/rpc/client/rpc_test.go | 14 ++++++++++++++ pkg/rpc/client/state.go | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index cf89eeae9..d7e1437f1 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -913,6 +913,20 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{ }, }, }, + "verifyproof": { + { + name: "positive", + invoke: func(c *Client) (interface{}, error) { + return c.VerifyProof(util.Uint256{}, new(result.ProofWithKey)) + }, + serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"value":"7465737476616c7565"}}`, + result: func(c *Client) interface{} { + return &result.VerifyProof{ + Value: []byte("testvalue"), + } + }, + }, + }, } type rpcClientErrorCase struct { diff --git a/pkg/rpc/client/state.go b/pkg/rpc/client/state.go index b3259279d..bfd78439b 100644 --- a/pkg/rpc/client/state.go +++ b/pkg/rpc/client/state.go @@ -48,3 +48,14 @@ func (c *Client) GetProof(root util.Uint256, sc util.Uint160, key []byte) (*resu } return &resp, nil } + +// VerifyProof verifies keyed proof for the state with the specified root. +func (c *Client) VerifyProof(root util.Uint256, proof *result.ProofWithKey) (*result.VerifyProof, error) { + resp := new(result.VerifyProof) + ps := request.NewRawParams(root, proof.String()) + err := c.performRequest("verifyproof", ps, resp) + if err != nil { + return nil, err + } + return resp, nil +}