From 64adca980b8b38cfdbb43f7e690e58a22e7ab189 Mon Sep 17 00:00:00 2001 From: Ishan Date: Thu, 18 Jan 2024 15:11:50 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Simplify=20chain=20utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 5 -- go.sum | 11 ----- pkg/chain/chain.go | 78 +++++++++--------------------- pkg/chain/chain_mocks_test.go | 49 ------------------- pkg/chain/chain_test.go | 91 ----------------------------------- 5 files changed, 23 insertions(+), 211 deletions(-) delete mode 100644 pkg/chain/chain_mocks_test.go delete mode 100644 pkg/chain/chain_test.go diff --git a/go.mod b/go.mod index c9c98d0..6f45dfe 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.21.5 require ( github.com/ethereum/go-ethereum v1.13.10 - github.com/stretchr/testify v1.8.4 go.uber.org/zap v1.26.0 ) @@ -16,7 +15,6 @@ require ( github.com/consensys/bavard v0.1.13 // indirect github.com/consensys/gnark-crypto v0.12.1 // indirect github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set/v2 v2.1.0 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/ethereum/c-kzg-4844 v0.4.0 // indirect @@ -24,9 +22,7 @@ require ( github.com/gorilla/websocket v1.4.2 // indirect github.com/holiman/uint256 v1.2.4 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect - github.com/stretchr/objx v0.5.0 // indirect github.com/supranational/blst v0.3.11 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect @@ -37,6 +33,5 @@ require ( golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/tools v0.15.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect rsc.io/tmplfunc v0.0.3 // indirect ) diff --git a/go.sum b/go.sum index 6bd8bfe..baa8c8b 100644 --- a/go.sum +++ b/go.sum @@ -38,7 +38,6 @@ github.com/crate-crypto/go-ipa v0.0.0-20231025140028-3c0104f4b233 h1:d28BXYi+wUp github.com/crate-crypto/go-ipa v0.0.0-20231025140028-3c0104f4b233/go.mod h1:geZJZH3SzKCqnz5VT0q/DyIG/tvu/dZk+VIfXicupJs= github.com/crate-crypto/go-kzg-4844 v0.7.0 h1:C0vgZRk4q4EZ/JgPfzuSoxdCq3C3mOZMBShovmncxvA= github.com/crate-crypto/go-kzg-4844 v0.7.0/go.mod h1:1kMhvPgI0Ky3yIa+9lFySEBUBXkYxeOi8ZF1sYioxhc= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI= @@ -139,12 +138,6 @@ github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1 github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/status-im/keycard-go v0.2.0 h1:QDLFswOQu1r5jsycloeQh3bVU8n/NatHHaZobtDnDzA= github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9+mHxBEeo3Hbg= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbeoEm4= @@ -188,14 +181,10 @@ golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8= golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk= google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU= diff --git a/pkg/chain/chain.go b/pkg/chain/chain.go index 19c0e36..fee8ff3 100644 --- a/pkg/chain/chain.go +++ b/pkg/chain/chain.go @@ -1,4 +1,3 @@ -// Package chain implements everything related to interaction with smart contracts, rpcprovider, etc package chain import ( @@ -10,92 +9,61 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" - "github.com/ethereum/go-ethereum/rpc" ) const ( - RPCEndpointGetProof = "eth_getProof" + rpcEndpointGetProof = "eth_getProof" ) -type APIMethods interface { - ChainID(ctx context.Context) (*big.Int, error) - BlockNumber(ctx context.Context) (uint64, error) - BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) - Client() *rpc.Client +// ChainAPIClient connects and encapsulates all the methods to interact with the a chain. +type ChainAPIClient struct { + eth *ethclient.Client + log log.Logger } -type RPCClient interface { - Call(result interface{}, method string, args ...interface{}) error +type proofResponse struct { + Address common.Address `json:"address"` + AccountProof []hexutil.Bytes `json:"accountProof"` + Balance *hexutil.Big `json:"balance"` + CodeHash common.Hash `json:"codeHash"` + Nonce hexutil.Uint64 `json:"nonce"` + StorageHash common.Hash `json:"storageHash"` + StorageProof []common.Hash `json:"storageProof"` } // GetAPIClient return [ChainAPIClient] with client attached. -func GetAPIClient(url string, log log.Logger) (*ChainAPIClient, error) { - client, err := ethclient.Dial(url) +func GetAPIClient(ctx context.Context, url string, log log.Logger) (*ChainAPIClient, error) { + client, err := ethclient.DialContext(ctx, url) if err != nil { return nil, err } - return NewChainAPIClient(client, log) -} - -type ChainAPIClient struct { - apiClient APIMethods - log log.Logger -} - -// NewChainAPIClient returns a [ChainAPIClient], wrapping all RPC endpoints to access chain related data. -func NewChainAPIClient(apiClient APIMethods, log log.Logger) (*ChainAPIClient, error) { return &ChainAPIClient{ - apiClient: apiClient, - log: log, + eth: client, + log: log, }, nil } // Returns chainID of the connected node. func (c *ChainAPIClient) GetChainID(ctx context.Context) (*big.Int, error) { - chainID, err := c.apiClient.ChainID(ctx) - if err != nil { - return nil, err - } - - return chainID, nil + return c.eth.ChainID(ctx) } // Returns latest block number from the connected node. func (c *ChainAPIClient) GetLatestBlockNumber(ctx context.Context) (uint64, error) { - blockNumber, err := c.apiClient.BlockNumber(ctx) - if err != nil { - return 0, err - } - - return blockNumber, nil + return c.eth.BlockNumber(ctx) } // Returns block for a given block number from the connected node. func (c *ChainAPIClient) GetBlockByNumber(ctx context.Context, blockNumber *big.Int) (*types.Block, error) { - block, err := c.apiClient.BlockByNumber(ctx, blockNumber) - if err != nil { - return nil, err - } - - return block, nil -} - -type ProofResponse struct { - Address common.Address `json:"address"` - AccountProof []hexutil.Bytes `json:"accountProof"` - Balance *hexutil.Big `json:"balance"` - CodeHash common.Hash `json:"codeHash"` - Nonce hexutil.Uint64 `json:"nonce"` - StorageHash common.Hash `json:"storageHash"` - StorageProof []common.Hash `json:"storageProof"` + return c.eth.BlockByNumber(ctx, blockNumber) } // Returns the account and storage values, including the Merkle proof, of the specified account/address. -func (c *ChainAPIClient) GetProof(client RPCClient, blockNumber *big.Int, address string) (*ProofResponse, error) { - var result ProofResponse +func (c *ChainAPIClient) GetProof(ctx context.Context, blockNumber *big.Int, address common.Address) (*proofResponse, error) { + var result proofResponse - if err := client.Call(&result, RPCEndpointGetProof, address, []string{}, blockNumber); err != nil { + if err := c.eth.Client().CallContext(ctx, &result, rpcEndpointGetProof, address, []string{}, hexutil.Big(*blockNumber)); err != nil { return nil, err } diff --git a/pkg/chain/chain_mocks_test.go b/pkg/chain/chain_mocks_test.go deleted file mode 100644 index ede01f8..0000000 --- a/pkg/chain/chain_mocks_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package chain - -import ( - "context" - "math/big" - - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/rpc" - "github.com/stretchr/testify/mock" -) - -type MockAPIClient struct { - mock.Mock -} - -func (c *MockAPIClient) ChainID(ctx context.Context) (*big.Int, error) { - ret := c.Called(ctx) - - return ret.Get(0).(*big.Int), ret.Error(1) -} -func (c *MockAPIClient) BlockNumber(ctx context.Context) (uint64, error) { - ret := c.Called(ctx) - - return ret.Get(0).(uint64), ret.Error(1) -} -func (c *MockAPIClient) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { - ret := c.Called(ctx, number) - - return ret.Get(0).(*types.Block), ret.Error(1) -} -func (c *MockAPIClient) Client() *rpc.Client { - ret := c.Called() - - return ret.Get(0).(*rpc.Client) -} - -type MockRPCClient struct { - mock.Mock -} - -func (c *MockRPCClient) Call(result interface{}, method string, args ...interface{}) error { - allArgs := []interface{}{result, method} - allArgs = append(allArgs, args...) - ret := c.Called(allArgs...) - - ptr := &result - *ptr = ret.Get(0) - return ret.Error(1) -} diff --git a/pkg/chain/chain_test.go b/pkg/chain/chain_test.go deleted file mode 100644 index 7080877..0000000 --- a/pkg/chain/chain_test.go +++ /dev/null @@ -1,91 +0,0 @@ -package chain - -import ( - "context" - "fmt" - "math/big" - "testing" - "time" - - "github.com/LiskHQ/op-fault-detector/pkg/log" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" -) - -func TestNewChainAPIClient(t *testing.T) { - log := log.DefaultLogger - - apiClientMock := &MockAPIClient{} - chainClient, _ := NewChainAPIClient(apiClientMock, log) - assert.Equal(t, chainClient.log, log) -} - -func TestGetChainID(t *testing.T) { - ctx := context.Background() - log := log.DefaultLogger - apiClientMock := &MockAPIClient{} - chainClient, _ := NewChainAPIClient(apiClientMock, log) - - assert.Equal(t, chainClient.log, log) - - expectedChainID := big.NewInt(2) - apiClientMock.On("ChainID", context.Background()).Return(expectedChainID, nil) - receivedChainID, _ := chainClient.GetChainID(ctx) - assert.Equal(t, expectedChainID, receivedChainID) -} - -func TestGetLatestBlockNumber(t *testing.T) { - ctx := context.Background() - log := log.DefaultLogger - apiClientMock := &MockAPIClient{} - chainClient, _ := NewChainAPIClient(apiClientMock, log) - - assert.Equal(t, chainClient.log, log) - - expectedLatestBlockNumber := uint64(12345) - apiClientMock.On("BlockNumber", context.Background()).Return(expectedLatestBlockNumber, nil) - receivedLatestBlockNumber, _ := chainClient.GetLatestBlockNumber(ctx) - assert.Equal(t, expectedLatestBlockNumber, receivedLatestBlockNumber) -} - -func TestGetBlockByNumber(t *testing.T) { - ctx := context.Background() - log := log.DefaultLogger - apiClientMock := &MockAPIClient{} - chainClient, _ := NewChainAPIClient(apiClientMock, log) - - assert.Equal(t, chainClient.log, log) - - blockNumber := big.NewInt(800) - expectedBlock := &types.Block{ - ReceivedAt: time.Now(), - } - apiClientMock.On("BlockByNumber", context.Background(), blockNumber).Return(expectedBlock, nil) - receivedBlock, _ := chainClient.GetBlockByNumber(ctx, blockNumber) - assert.Equal(t, expectedBlock, receivedBlock) -} - -func TestGetProof(t *testing.T) { - // t.Skipf("Skipping GetProof test") - log := log.DefaultLogger - apiClientMock := &MockAPIClient{} - chainClient, _ := NewChainAPIClient(apiClientMock, log) - - assert.Equal(t, chainClient.log, log) - - rpcClientMock := &MockRPCClient{} - // Args for Call() method - address := common.Address{} - blockNumber := big.NewInt(800) - var proofResponseExpected ProofResponse - - apiClientMock.On("Client").Return(rpcClientMock, nil) - rpcClientMock.On("Call", mock.AnythingOfType("*chain.ProofResponse"), RPCEndpointGetProof, address.String(), []string{}, blockNumber).Return(fmt.Println("Error")) - - proofResponseRecieved, err := chainClient.GetProof(rpcClientMock, blockNumber, address.Hex()) - - assert.NoError(t, err) - assert.Equal(t, &proofResponseExpected, proofResponseRecieved) -}