From 78b3fd4d789dcfd6c16e24b9c52c68721a549bc5 Mon Sep 17 00:00:00 2001 From: Pablo Date: Tue, 12 Aug 2025 13:38:56 -0600 Subject: [PATCH 1/3] feat: add time advance call to anvil client --- framework/rpc/rpc.go | 15 +++++++++++++++ framework/rpc/rpc_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/framework/rpc/rpc.go b/framework/rpc/rpc.go index 6a7ed52f2..ee217193c 100644 --- a/framework/rpc/rpc.go +++ b/framework/rpc/rpc.go @@ -51,6 +51,21 @@ func New(url string, headers http.Header) *RPCClient { } } +// EVMIncreaseTime jumps forward in time by `seconds`. +// The parameter is a JSON number (in seconds) +func (m *RPCClient) EVMIncreaseTime(seconds uint64) error { + payload := map[string]interface{}{ + "jsonrpc": "2.0", + "method": "evm_increaseTime", + "params": []interface{}{seconds}, + "id": rand.Int(), + } + if _, err := m.client.R().SetBody(payload).Post(m.URL); err != nil { + return errors.Wrap(err, "evm_increaseTime") + } + return nil +} + // AnvilAutoImpersonate sets auto impersonification to true or false func (m *RPCClient) AnvilAutoImpersonate(b bool) error { //nolint:gosec diff --git a/framework/rpc/rpc_test.go b/framework/rpc/rpc_test.go index 0ee83b873..f09639f78 100644 --- a/framework/rpc/rpc_test.go +++ b/framework/rpc/rpc_test.go @@ -243,4 +243,38 @@ func TestRPCAPI(t *testing.T) { require.GreaterOrEqual(t, int64(block.Transactions().Len()), txnInBlock) } }) + + t.Run("(anvil) evm_increaseTime advances timestamp", func(t *testing.T) { + ac, err := StartAnvil([]string{"--no-mine"}) + require.NoError(t, err) + client, err := ethclient.Dial(ac.URL) + require.NoError(t, err) + + anvil := New(ac.URL, nil) + + // Mine an initial block to establish a baseline timestamp + require.NoError(t, anvil.AnvilMine([]interface{}{1})) + + // Read latest header/time + h1, err := client.HeaderByNumber(context.Background(), nil) + require.NoError(t, err) + t1 := h1.Time // uint64 (seconds) + + // Jump forward in time, then mine exactly one block so the new timestamp is materialized + advance := uint64(60) // 1 minute + require.NoError(t, anvil.EVMIncreaseTime(advance)) + require.NoError(t, anvil.AnvilMine([]interface{}{1})) + + // Read new header/time + h2, err := client.HeaderByNumber(context.Background(), nil) + require.NoError(t, err) + t2 := h2.Time + + // Assert the delta is at least the requested advance + // (Anvil should add exactly `advance`, but we allow >= to be safe.) + require.GreaterOrEqual(t, int64(t2-t1), int64(advance), "timestamp did not advance by expected seconds") + + t.Logf("advanced time by %d seconds: %d -> %d (Δ=%d)", advance, t1, t2, t2-t1) + }) + } From 0669dd4711b518e473c5bf9806a9ca4a87ef8474 Mon Sep 17 00:00:00 2001 From: Pablo Date: Tue, 12 Aug 2025 13:48:43 -0600 Subject: [PATCH 2/3] fix: linting --- framework/rpc/rpc.go | 2 +- framework/rpc/rpc_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/rpc/rpc.go b/framework/rpc/rpc.go index ee217193c..df95acc37 100644 --- a/framework/rpc/rpc.go +++ b/framework/rpc/rpc.go @@ -6,7 +6,7 @@ import ( "encoding/json" "fmt" "math/big" - "math/rand" + "math/rand/v2" "net/http" "os" "strconv" diff --git a/framework/rpc/rpc_test.go b/framework/rpc/rpc_test.go index f09639f78..f10932630 100644 --- a/framework/rpc/rpc_test.go +++ b/framework/rpc/rpc_test.go @@ -272,7 +272,7 @@ func TestRPCAPI(t *testing.T) { // Assert the delta is at least the requested advance // (Anvil should add exactly `advance`, but we allow >= to be safe.) - require.GreaterOrEqual(t, int64(t2-t1), int64(advance), "timestamp did not advance by expected seconds") + require.GreaterOrEqual(t, t2-t1, advance, "timestamp did not advance by expected seconds") t.Logf("advanced time by %d seconds: %d -> %d (Δ=%d)", advance, t1, t2, t2-t1) }) From a20dd636ad3d12e519a068ef40a5aa677708a69a Mon Sep 17 00:00:00 2001 From: Pablo Date: Tue, 12 Aug 2025 13:53:06 -0600 Subject: [PATCH 3/3] fix: linting --- framework/rpc/rpc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/rpc/rpc.go b/framework/rpc/rpc.go index df95acc37..96f58c9e3 100644 --- a/framework/rpc/rpc.go +++ b/framework/rpc/rpc.go @@ -58,7 +58,7 @@ func (m *RPCClient) EVMIncreaseTime(seconds uint64) error { "jsonrpc": "2.0", "method": "evm_increaseTime", "params": []interface{}{seconds}, - "id": rand.Int(), + "id": rand.Int(), //nolint:gosec } if _, err := m.client.R().SetBody(payload).Post(m.URL); err != nil { return errors.Wrap(err, "evm_increaseTime")