diff --git a/proxy/grpc/client.go b/proxy/grpc/client.go index 7b14f73..6f29f4b 100644 --- a/proxy/grpc/client.go +++ b/proxy/grpc/client.go @@ -61,8 +61,7 @@ func (c *Client) InitChain(ctx context.Context, genesisTime time.Time, initialHe return types.Hash{}, 0, err } - var stateRoot types.Hash - copy(stateRoot[:], resp.StateRoot) + stateRoot := copyHash(resp.StateRoot) return stateRoot, resp.MaxBytes, nil } @@ -99,8 +98,7 @@ func (c *Client) ExecuteTxs(ctx context.Context, txs []types.Tx, blockHeight uin return types.Hash{}, 0, err } - var updatedStateRoot types.Hash - copy(updatedStateRoot[:], resp.UpdatedStateRoot) + updatedStateRoot := copyHash(resp.UpdatedStateRoot) return updatedStateRoot, resp.MaxBytes, nil } diff --git a/proxy/grpc/client_server_test.go b/proxy/grpc/client_server_test.go index af9bc34..3b8a3dc 100644 --- a/proxy/grpc/client_server_test.go +++ b/proxy/grpc/client_server_test.go @@ -56,10 +56,8 @@ func TestClientServer(t *testing.T) { chainID := "test-chain" // initialize a new Hash with a fixed size - expectedStateRoot := make([]byte, 32) + expectedStateRoot := types.Hash(make([]byte, 32)) copy(expectedStateRoot, []byte{1, 2, 3}) - var stateRootHash types.Hash - copy(stateRootHash[:], expectedStateRoot) expectedMaxBytes := uint64(1000000) @@ -68,12 +66,12 @@ func TestClientServer(t *testing.T) { expectedTime := time.Unix(unixTime, 0).UTC() mockExec.On("InitChain", mock.Anything, expectedTime, initialHeight, chainID). - Return(stateRootHash, expectedMaxBytes, nil).Once() + Return(expectedStateRoot, expectedMaxBytes, nil).Once() stateRoot, maxBytes, err := client.InitChain(context.TODO(), genesisTime, initialHeight, chainID) require.NoError(t, err) - assert.Equal(t, stateRootHash, stateRoot) + assert.Equal(t, expectedStateRoot, stateRoot) assert.Equal(t, expectedMaxBytes, maxBytes) mockExec.AssertExpectations(t) }) diff --git a/proxy/grpc/server.go b/proxy/grpc/server.go index 629360e..f924d19 100644 --- a/proxy/grpc/server.go +++ b/proxy/grpc/server.go @@ -83,8 +83,7 @@ func (s *Server) ExecuteTxs(ctx context.Context, req *pb.ExecuteTxsRequest) (*pb txs[i] = tx } - var prevStateRoot types.Hash - copy(prevStateRoot[:], req.PrevStateRoot) + prevStateRoot := copyHash(req.PrevStateRoot) updatedStateRoot, maxBytes, err := s.exec.ExecuteTxs( ctx, diff --git a/proxy/grpc/util.go b/proxy/grpc/util.go new file mode 100644 index 0000000..2e3cf67 --- /dev/null +++ b/proxy/grpc/util.go @@ -0,0 +1,9 @@ +package grpc + +import "github.com/rollkit/go-execution/types" + +func copyHash(src []byte) types.Hash { + dst := make([]byte, len(src)) + copy(dst, src) + return dst +} diff --git a/test/suite.go b/test/suite.go index 81d1014..52342e4 100644 --- a/test/suite.go +++ b/test/suite.go @@ -62,9 +62,9 @@ func (s *ExecutorSuite) TestExecuteTxs() { stateRoot, maxBytes, err := s.Exec.ExecuteTxs(context.TODO(), txs, initialHeight+1, genesisTime.Add(time.Second), genesisStateRoot) s.Require().NoError(err) - s.NotEqual(types.Hash{}, stateRoot) - s.NotEqual(genesisStateRoot, stateRoot) - s.Greater(maxBytes, uint64(0)) + s.Require().NotEmpty(stateRoot) + s.Require().NotEqualValues(genesisStateRoot, stateRoot) + s.Require().Greater(maxBytes, uint64(0)) } // TestSetFinal tests SetFinal method. @@ -73,8 +73,8 @@ func (s *ExecutorSuite) TestSetFinal() { err := s.Exec.SetFinal(context.TODO(), 1) s.Require().Error(err) - _, _, _, _ = s.initChain(context.TODO()) - _, _, err = s.Exec.ExecuteTxs(context.TODO(), nil, 2, time.Now(), types.Hash("test state")) + _, height, stateRoot, _ := s.initChain(context.TODO()) + _, _, err = s.Exec.ExecuteTxs(context.TODO(), nil, height+1, time.Now(), stateRoot) s.Require().NoError(err) err = s.Exec.SetFinal(context.TODO(), 2) s.Require().NoError(err) @@ -107,5 +107,6 @@ func (s *ExecutorSuite) initChain(ctx context.Context) (time.Time, uint64, types stateRoot, maxBytes, err := s.Exec.InitChain(ctx, genesisTime, initialHeight, chainID) s.Require().NoError(err) + s.Require().NotEmpty(stateRoot) return genesisTime, initialHeight, stateRoot, maxBytes }