|
| 1 | +//go:build evm |
| 2 | +// +build evm |
| 3 | + |
| 4 | +package evm |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "crypto/rand" |
| 10 | + "encoding/hex" |
| 11 | + "encoding/json" |
| 12 | + "fmt" |
| 13 | + "math/big" |
| 14 | + "net/http" |
| 15 | + "os" |
| 16 | + "path/filepath" |
| 17 | + "strings" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/ethereum/go-ethereum/common" |
| 22 | + "github.com/ethereum/go-ethereum/core/types" |
| 23 | + "github.com/ethereum/go-ethereum/crypto" |
| 24 | + "github.com/ethereum/go-ethereum/ethclient" |
| 25 | + "github.com/golang-jwt/jwt/v5" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + tc "github.com/testcontainers/testcontainers-go/modules/compose" |
| 28 | +) |
| 29 | + |
| 30 | +// JWT Helpers |
| 31 | +func GenerateJWTSecret() (string, error) { |
| 32 | + jwtSecret := make([]byte, 32) |
| 33 | + _, err := rand.Read(jwtSecret) |
| 34 | + if err != nil { |
| 35 | + return "", fmt.Errorf("failed to generate random bytes: %w", err) |
| 36 | + } |
| 37 | + return hex.EncodeToString(jwtSecret), nil |
| 38 | +} |
| 39 | + |
| 40 | +func DecodeSecret(jwtSecret string) ([]byte, error) { |
| 41 | + secret, err := hex.DecodeString(strings.TrimPrefix(jwtSecret, "0x")) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("failed to decode JWT secret: %w", err) |
| 44 | + } |
| 45 | + return secret, nil |
| 46 | +} |
| 47 | + |
| 48 | +func GetAuthToken(jwtSecret []byte) (string, error) { |
| 49 | + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ |
| 50 | + "exp": time.Now().Add(time.Hour * 1).Unix(), |
| 51 | + "iat": time.Now().Unix(), |
| 52 | + }) |
| 53 | + authToken, err := token.SignedString(jwtSecret) |
| 54 | + if err != nil { |
| 55 | + return "", fmt.Errorf("failed to sign JWT token: %w", err) |
| 56 | + } |
| 57 | + return authToken, nil |
| 58 | +} |
| 59 | + |
| 60 | +// Docker Compose Setup for Reth Engine |
| 61 | +func SetupTestRethEngine(t *testing.T, dockerPath, jwtFilename string) string { |
| 62 | + dockerAbsPath, err := filepath.Abs(dockerPath) |
| 63 | + require.NoError(t, err) |
| 64 | + jwtPath := filepath.Join(dockerAbsPath, "jwttoken") |
| 65 | + err = os.MkdirAll(jwtPath, 0750) |
| 66 | + require.NoError(t, err) |
| 67 | + jwtSecret, err := GenerateJWTSecret() |
| 68 | + require.NoError(t, err) |
| 69 | + jwtFile := filepath.Join(jwtPath, jwtFilename) |
| 70 | + err = os.WriteFile(jwtFile, []byte(jwtSecret), 0600) |
| 71 | + require.NoError(t, err) |
| 72 | + t.Cleanup(func() { _ = os.Remove(jwtFile) }) |
| 73 | + composeFilePath := filepath.Join(dockerAbsPath, "docker-compose.yml") |
| 74 | + identifier := tc.StackIdentifier(strings.ToLower(t.Name())) |
| 75 | + identifier = tc.StackIdentifier(strings.ReplaceAll(string(identifier), "/", "_")) |
| 76 | + identifier = tc.StackIdentifier(strings.ReplaceAll(string(identifier), " ", "_")) |
| 77 | + compose, err := tc.NewDockerComposeWith(tc.WithStackFiles(composeFilePath), identifier) |
| 78 | + require.NoError(t, err, "Failed to create docker compose") |
| 79 | + t.Cleanup(func() { |
| 80 | + ctx := context.Background() |
| 81 | + err := compose.Down(ctx, tc.RemoveOrphans(true), tc.RemoveVolumes(true)) |
| 82 | + if err != nil { |
| 83 | + t.Logf("Warning: Failed to tear down docker-compose environment: %v", err) |
| 84 | + } |
| 85 | + }) |
| 86 | + ctx := context.Background() |
| 87 | + err = compose.Up(ctx, tc.Wait(true)) |
| 88 | + require.NoError(t, err, "Failed to start docker compose") |
| 89 | + err = WaitForRethContainer(t, jwtSecret, "http://localhost:8545", "http://localhost:8551") |
| 90 | + require.NoError(t, err) |
| 91 | + return jwtSecret |
| 92 | +} |
| 93 | + |
| 94 | +// Wait for Reth container to be ready |
| 95 | +func WaitForRethContainer(t *testing.T, jwtSecret, ethURL, engineURL string) error { |
| 96 | + t.Helper() |
| 97 | + client := &http.Client{Timeout: 100 * time.Millisecond} |
| 98 | + timer := time.NewTimer(30 * time.Second) |
| 99 | + defer timer.Stop() |
| 100 | + for { |
| 101 | + select { |
| 102 | + case <-timer.C: |
| 103 | + return fmt.Errorf("timeout waiting for reth container to be ready") |
| 104 | + default: |
| 105 | + rpcReq := strings.NewReader(`{"jsonrpc":"2.0","method":"net_version","params":[],"id":1}`) |
| 106 | + resp, err := client.Post(ethURL, "application/json", rpcReq) |
| 107 | + if err == nil { |
| 108 | + if err := resp.Body.Close(); err != nil { |
| 109 | + return fmt.Errorf("failed to close response body: %w", err) |
| 110 | + } |
| 111 | + if resp.StatusCode == http.StatusOK { |
| 112 | + req, err := http.NewRequest("POST", engineURL, strings.NewReader(`{"jsonrpc":"2.0","method":"engine_getClientVersionV1","params":[],"id":1}`)) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + req.Header.Set("Content-Type", "application/json") |
| 117 | + secret, err := DecodeSecret(jwtSecret) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + authToken, err := GetAuthToken(secret) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authToken)) |
| 126 | + resp, err := client.Do(req) |
| 127 | + if err == nil { |
| 128 | + if err := resp.Body.Close(); err != nil { |
| 129 | + return fmt.Errorf("failed to close response body: %w", err) |
| 130 | + } |
| 131 | + if resp.StatusCode == http.StatusOK { |
| 132 | + return nil |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + time.Sleep(100 * time.Millisecond) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// Transaction Helpers |
| 143 | +func GetRandomTransaction(t *testing.T, privateKeyHex, toAddressHex, chainID string, gasLimit uint64, lastNonce *uint64) *types.Transaction { |
| 144 | + privateKey, err := crypto.HexToECDSA(privateKeyHex) |
| 145 | + require.NoError(t, err) |
| 146 | + chainId, _ := new(big.Int).SetString(chainID, 10) |
| 147 | + txValue := big.NewInt(1000000000000000000) |
| 148 | + gasPrice := big.NewInt(30000000000) |
| 149 | + toAddress := common.HexToAddress(toAddressHex) |
| 150 | + data := make([]byte, 16) |
| 151 | + _, err = rand.Read(data) |
| 152 | + require.NoError(t, err) |
| 153 | + tx := types.NewTx(&types.LegacyTx{ |
| 154 | + Nonce: *lastNonce, |
| 155 | + To: &toAddress, |
| 156 | + Value: txValue, |
| 157 | + Gas: gasLimit, |
| 158 | + GasPrice: gasPrice, |
| 159 | + Data: data, |
| 160 | + }) |
| 161 | + *lastNonce++ |
| 162 | + signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainId), privateKey) |
| 163 | + require.NoError(t, err) |
| 164 | + return signedTx |
| 165 | +} |
| 166 | + |
| 167 | +func SubmitTransaction(t *testing.T, tx *types.Transaction) { |
| 168 | + rpcClient, err := ethclient.Dial("http://localhost:8545") |
| 169 | + require.NoError(t, err) |
| 170 | + err = rpcClient.SendTransaction(context.Background(), tx) |
| 171 | + require.NoError(t, err) |
| 172 | +} |
| 173 | + |
| 174 | +func CheckTxIncluded(t *testing.T, txHash common.Hash) bool { |
| 175 | + rpcClient, err := ethclient.Dial("http://localhost:8545") |
| 176 | + if err != nil { |
| 177 | + return false |
| 178 | + } |
| 179 | + receipt, err := rpcClient.TransactionReceipt(context.Background(), txHash) |
| 180 | + return err == nil && receipt != nil && receipt.Status == 1 |
| 181 | +} |
| 182 | + |
| 183 | +// Genesis Hash Helper |
| 184 | +func GetGenesisHash(t *testing.T) string { |
| 185 | + client := &http.Client{Timeout: 2 * time.Second} |
| 186 | + data := []byte(`{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x0", false],"id":1}`) |
| 187 | + resp, err := client.Post("http://localhost:8545", "application/json", bytes.NewReader(data)) |
| 188 | + require.NoError(t, err) |
| 189 | + defer resp.Body.Close() |
| 190 | + var result struct { |
| 191 | + Result struct { |
| 192 | + Hash string `json:"hash"` |
| 193 | + } `json:"result"` |
| 194 | + } |
| 195 | + require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) |
| 196 | + require.NotEmpty(t, result.Result.Hash) |
| 197 | + return result.Result.Hash |
| 198 | +} |
0 commit comments