From 9ae422f12f3cc0a3ba536678cd6ec7743f98f174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Zdyba=C5=82?= Date: Wed, 20 Nov 2024 09:51:57 +0100 Subject: [PATCH] chore: fix linter issues --- .golangci.yml | 3 --- .yamllint.yml | 9 +++++++++ README.md | 8 ++++---- docker/docker-compose.yml | 10 ++++----- execution.go | 25 +++++++++++------------ execution_test.go | 36 +++++++++++++++++---------------- integration_test.go | 11 +++++----- mocks/mocks.go => mocks_test.go | 15 +++++++------- 8 files changed, 63 insertions(+), 54 deletions(-) create mode 100644 .yamllint.yml rename mocks/mocks.go => mocks_test.go (94%) diff --git a/.golangci.yml b/.golangci.yml index 690483b..ecbd405 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,7 +4,6 @@ run: linters: enable: - - deadcode - errcheck - gofmt - goimports @@ -15,10 +14,8 @@ linters: - misspell - revive - staticcheck - - structcheck - typecheck - unused - - varcheck issues: exclude-use-default: false diff --git a/.yamllint.yml b/.yamllint.yml new file mode 100644 index 0000000..cd2a9e8 --- /dev/null +++ b/.yamllint.yml @@ -0,0 +1,9 @@ +--- +# Built from docs https://yamllint.readthedocs.io/en/stable/configuration.html +extends: default + +rules: + # 120 chars should be enough, but don't fail if a line is longer + line-length: + max: 120 + level: warning diff --git a/README.md b/README.md index b520a50..9b8ac6e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## Architecture +# Architecture ```mermaid graph LR @@ -64,7 +64,7 @@ The architecture consists of several key components: ## Development ```bash -$ cd docker -$ docker compose up -d -$ docker compose down +cd docker +docker compose up -d +docker compose down ``` diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 70ae7a2..dc0966a 100755 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -12,10 +12,10 @@ services: timeout: 5s retries: 3 command: > - /bin/sh -c "mkdir -p /jwt && - if [ ! -f /jwt/jwt.hex ]; then + /bin/sh -c "mkdir -p /jwt && + if [ ! -f /jwt/jwt.hex ]; then apk add --no-cache openssl && - openssl rand -hex 32 | tr -d '\n' > /jwt/jwt.hex; + openssl rand -hex 32 | tr -d '\n' > /jwt/jwt.hex; fi" reth: @@ -38,8 +38,8 @@ services: - ./jwttoken:/root/jwt:ro - ./chain:/root/chain:ro pid: host - entrypoint: /bin/sh -c - command: + entrypoint: /bin/sh -c + command: - | reth init --chain /root/chain/genesis.json reth node \ diff --git a/execution.go b/execution.go index 48662d9..c13b151 100644 --- a/execution.go +++ b/execution.go @@ -10,20 +10,23 @@ import ( "net/http" "time" + "github.com/golang-jwt/jwt/v5" + "github.com/ethereum/go-ethereum/beacon/engine" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rpc" - "github.com/golang-jwt/jwt/v5" - execution "github.com/rollkit/go-execution" + "github.com/rollkit/go-execution" proxy_json_rpc "github.com/rollkit/go-execution/proxy/jsonrpc" execution_types "github.com/rollkit/go-execution/types" ) var ( - ErrNilPayloadStatus = errors.New("nil payload status") + // ErrNilPayloadStatus indicates that PayloadID returned by EVM was nil + ErrNilPayloadStatus = errors.New("nil payload status") + // ErrInvalidPayloadStatus indicates that EVM returned status != VALID ErrInvalidPayloadStatus = errors.New("invalid payload status") ) @@ -32,7 +35,6 @@ var _ execution.Executor = (*EngineAPIExecutionClient)(nil) // EngineAPIExecutionClient implements the execution.Execute interface type EngineAPIExecutionClient struct { - proxyClient *proxy_json_rpc.Client engineClient *rpc.Client // engine api ethClient *ethclient.Client genesisHash common.Hash @@ -88,7 +90,6 @@ func NewEngineAPIExecutionClient( } return &EngineAPIExecutionClient{ - proxyClient: proxyClient, engineClient: engineClient, ethClient: ethClient, genesisHash: genesisHash, @@ -97,14 +98,12 @@ func NewEngineAPIExecutionClient( } // Start starts the execution client -func (c *EngineAPIExecutionClient) Start(url string) error { - return c.proxyClient.Start(url) +func (c *EngineAPIExecutionClient) Start() error { + return nil } // Stop stops the execution client and closes all connections func (c *EngineAPIExecutionClient) Stop() { - c.proxyClient.Stop() - if c.engineClient != nil { c.engineClient.Close() } @@ -124,7 +123,7 @@ func (c *EngineAPIExecutionClient) InitChain(ctx context.Context, genesisTime ti FinalizedBlockHash: c.genesisHash, }, engine.PayloadAttributes{ - Timestamp: uint64(genesisTime.Unix()), + Timestamp: uint64(genesisTime.Unix()), //nolint:gosec // disable G115 Random: common.Hash{}, SuggestedFeeRecipient: c.feeRecipient, BeaconRoot: &c.genesisHash, @@ -222,7 +221,7 @@ func (c *EngineAPIExecutionClient) ExecuteTxs(ctx context.Context, txs []executi FinalizedBlockHash: c.genesisHash, }, &engine.PayloadAttributes{ - Timestamp: uint64(timestamp.Unix()), + Timestamp: uint64(timestamp.Unix()), //nolint:gosec // disable G115 Random: c.derivePrevRandao(height), SuggestedFeeRecipient: c.feeRecipient, Withdrawals: []*types.Withdrawal{}, @@ -264,7 +263,7 @@ func (c *EngineAPIExecutionClient) ExecuteTxs(ctx context.Context, txs []executi // SetFinal marks a block at the given height as final func (c *EngineAPIExecutionClient) SetFinal(ctx context.Context, height uint64) error { - block, err := c.ethClient.BlockByNumber(ctx, big.NewInt(int64(height))) + block, err := c.ethClient.BlockByNumber(ctx, big.NewInt(int64(height))) //nolint:gosec // disable G115 if err != nil { return fmt.Errorf("failed to get block at height %d: %w", height, err) } @@ -293,5 +292,5 @@ func (c *EngineAPIExecutionClient) SetFinal(ctx context.Context, height uint64) // derivePrevRandao generates a deterministic prevRandao value based on block height func (c *EngineAPIExecutionClient) derivePrevRandao(blockHeight uint64) common.Hash { - return common.BigToHash(big.NewInt(int64(blockHeight))) + return common.BigToHash(big.NewInt(int64(blockHeight))) //nolint:gosec // disable G115 } diff --git a/execution_test.go b/execution_test.go index c8d9155..ed63830 100644 --- a/execution_test.go +++ b/execution_test.go @@ -1,7 +1,8 @@ -package execution +package execution_test import ( "context" + "encoding/hex" "encoding/json" "io" "math/big" @@ -10,14 +11,14 @@ import ( "testing" "time" - "encoding/hex" + "github.com/stretchr/testify/require" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/rollkit/go-execution-evm/mocks" + + "github.com/rollkit/go-execution-evm" proxy_json_rpc "github.com/rollkit/go-execution/proxy/jsonrpc" execution_types "github.com/rollkit/go-execution/types" - "github.com/stretchr/testify/require" ) // Helper function to generate a test JWT secret @@ -31,14 +32,14 @@ func generateTestJWTSecret() string { } func TestEngineAPIExecutionClient_InitChain(t *testing.T) { - mockEngine := mocks.NewMockEngineAPI(t) + mockEngine := NewMockEngineAPI(t) defer mockEngine.Close() - mockEth := mocks.NewMockEthAPI(t) + mockEth := NewMockEthAPI(t) defer mockEth.Close() jwtSecret := generateTestJWTSecret() - client, err := NewEngineAPIExecutionClient( + client, err := execution.NewEngineAPIExecutionClient( &proxy_json_rpc.Config{}, mockEth.URL, mockEngine.URL, @@ -70,16 +71,16 @@ func TestEngineAPIExecutionClient_InitChain(t *testing.T) { } func TestEngineAPIExecutionClient_ExecuteTxs(t *testing.T) { - mockEngine := mocks.NewMockEngineAPI(t) + mockEngine := NewMockEngineAPI(t) defer mockEngine.Close() - mockEth := mocks.NewMockEthAPI(t) + mockEth := NewMockEthAPI(t) defer mockEth.Close() jwtSecret := generateTestJWTSecret() prevStateRoot := execution_types.Hash(common.Hex2Bytes("111122223333444455556666777788889999aaaabbbbccccddddeeeeffff0000")) - client, err := NewEngineAPIExecutionClient( + client, err := execution.NewEngineAPIExecutionClient( &proxy_json_rpc.Config{}, mockEth.URL, mockEngine.URL, @@ -119,14 +120,14 @@ func TestEngineAPIExecutionClient_ExecuteTxs(t *testing.T) { } func TestEngineAPIExecutionClient_GetTxs(t *testing.T) { - mockEngine := mocks.NewMockEngineAPI(t) + mockEngine := NewMockEngineAPI(t) defer mockEngine.Close() - mockEth := mocks.NewMockEthAPI(t) + mockEth := NewMockEthAPI(t) defer mockEth.Close() jwtSecret := generateTestJWTSecret() - client, err := NewEngineAPIExecutionClient( + client, err := execution.NewEngineAPIExecutionClient( &proxy_json_rpc.Config{}, mockEth.URL, mockEngine.URL, @@ -172,11 +173,12 @@ func TestEngineAPIExecutionClient_GetTxs(t *testing.T) { } } - json.NewEncoder(w).Encode(map[string]interface{}{ + err = json.NewEncoder(w).Encode(map[string]interface{}{ "jsonrpc": "2.0", "id": req["id"], "result": resp, }) + require.NoError(t, err) })) ctx := context.Background() @@ -187,14 +189,14 @@ func TestEngineAPIExecutionClient_GetTxs(t *testing.T) { } func TestEngineAPIExecutionClient_SetFinal(t *testing.T) { - mockEngine := mocks.NewMockEngineAPI(t) + mockEngine := NewMockEngineAPI(t) defer mockEngine.Close() - mockEth := mocks.NewMockEthAPI(t) + mockEth := NewMockEthAPI(t) defer mockEth.Close() jwtSecret := generateTestJWTSecret() - client, err := NewEngineAPIExecutionClient( + client, err := execution.NewEngineAPIExecutionClient( &proxy_json_rpc.Config{}, mockEth.URL, mockEngine.URL, diff --git a/integration_test.go b/integration_test.go index 0157e87..273bd63 100644 --- a/integration_test.go +++ b/integration_test.go @@ -8,6 +8,9 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" "github.com/docker/go-connections/nat" @@ -15,8 +18,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" proxy_json_rpc "github.com/rollkit/go-execution/proxy/jsonrpc" rollkit_types "github.com/rollkit/go-execution/types" @@ -25,7 +26,7 @@ import ( const ( TEST_ETH_URL = "http://localhost:8545" TEST_ENGINE_URL = "http://localhost:8551" - JWT_SECRET = "09a23c010d96caaebb21c193b85d30bbb62a9bac5bd0a684e9e91c77c811ca65" + JWT_SECRET = "09a23c010d96caaebb21c193b85d30bbb62a9bac5bd0a684e9e91c77c811ca65" //nolint:gosec CHAIN_ID = "1234" GENESIS_HASH = "0x8bf225d50da44f60dee1c4ee6f810fe5b44723c76ac765654b6692d50459f216" @@ -34,7 +35,7 @@ const ( TEST_TO_ADDRESS = "0x944fDcD1c868E3cC566C78023CcB38A32cDA836E" DOCKER_CHAIN_PATH = "./docker/chain" // path relative to the test file - DOCKER_JWTSECRET_PATH = "./docker/jwttoken/" // path relative to the test file + DOCKER_JWTSECRET_PATH = "./docker/jwttoken/" //nolint:gosec // path relative to the test file DOCKER_JWT_SECRET_FILE = "testsecret.hex" ) @@ -47,7 +48,7 @@ func setupTestRethEngine(t *testing.T) { jwtSecretPath, err := filepath.Abs(DOCKER_JWTSECRET_PATH) require.NoError(t, err) - err = os.WriteFile(DOCKER_JWTSECRET_PATH+DOCKER_JWT_SECRET_FILE, []byte(JWT_SECRET), 0644) + err = os.WriteFile(DOCKER_JWTSECRET_PATH+DOCKER_JWT_SECRET_FILE, []byte(JWT_SECRET), 0600) require.NoError(t, err) cli, err := client.NewClientWithOpts() diff --git a/mocks/mocks.go b/mocks_test.go similarity index 94% rename from mocks/mocks.go rename to mocks_test.go index f0a4aba..cf6a200 100644 --- a/mocks/mocks.go +++ b/mocks_test.go @@ -1,4 +1,4 @@ -package mocks +package execution_test import ( "encoding/json" @@ -16,13 +16,13 @@ type MockEngineAPI struct { *httptest.Server } -type ForkchoiceState struct { +type forkChoiceState struct { HeadBlockHash string SafeBlockHash string FinalizedBlockHash string } -var lastForkchoiceUpdate *ForkchoiceState +var lastForkchoiceUpdate *forkChoiceState func NewMockEngineAPI(t *testing.T) *MockEngineAPI { t.Helper() @@ -50,7 +50,7 @@ func NewMockEngineAPI(t *testing.T) *MockEngineAPI { params := req["params"].([]interface{}) forkchoiceState := params[0].(map[string]interface{}) - lastForkchoiceUpdate = &ForkchoiceState{ + lastForkchoiceUpdate = &forkChoiceState{ HeadBlockHash: forkchoiceState["headBlockHash"].(string), SafeBlockHash: forkchoiceState["safeBlockHash"].(string), FinalizedBlockHash: forkchoiceState["finalizedBlockHash"].(string), @@ -92,11 +92,12 @@ func NewMockEngineAPI(t *testing.T) *MockEngineAPI { } } - json.NewEncoder(w).Encode(map[string]interface{}{ + err = json.NewEncoder(w).Encode(map[string]interface{}{ "jsonrpc": "2.0", "id": req["id"], "result": resp, }) + require.NoError(t, err) })) return mock @@ -191,7 +192,7 @@ func NewMockEthAPI(t *testing.T) *MockEthAPI { } } - json.NewEncoder(w).Encode(map[string]interface{}{ + _ = json.NewEncoder(w).Encode(map[string]interface{}{ "jsonrpc": "2.0", "id": req["id"], "result": resp, @@ -201,6 +202,6 @@ func NewMockEthAPI(t *testing.T) *MockEthAPI { return mock } -func (m *MockEngineAPI) GetLastForkchoiceUpdated() *ForkchoiceState { +func (m *MockEngineAPI) GetLastForkchoiceUpdated() *forkChoiceState { return lastForkchoiceUpdate }