Skip to content

Commit ce968e2

Browse files
authored
fix: allow alternative timestamp syncs for L1 and L2 (#298)
<!-- 🚨 ATTENTION! 🚨 This PR template is REQUIRED. PRs not following this format will be closed without review. Requirements: - PR title must follow commit conventions: https://www.conventionalcommits.org/en/v1.0.0/ - Label your PR with the correct type (e.g., 🐛 Bug, ✨ Enhancement, 🧪 Test, etc.) - Provide clear and specific details in each section --> **Motivation:** <!-- Explain here the context, and why you're making that change. What is the problem you're trying to solve. --> As a devkit user forking from a chain with out of sync block timestamps, I want the syncing process to move to the required timestamp directly, falling back to syncing one block at a time when required. **Modifications:** <!-- Describe the modifications you've done from a high level. What are the key technical decisions and why were they made? --> - Adds `evm_setNextBlockTimestamp`, `evm_mine` and `evm_increaseTime` RPC calls (attempted in that order) to move to a timestamp directly without depending on `anvil_mine` (1 block at a time) syncing (which is used as a fallback if all others fail). - Adds a ts buffer (12s) so that timestamps don't have to match exactly. **Result:** <!-- *After your change, what will change. --> - Faster syncing of the L1 and L2 chains **Testing:** <!-- *List testing procedures taken and useful artifacts. --> - Tested locally (on anvil)
1 parent 906bcb6 commit ce968e2

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

pkg/common/devnet/utils.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,34 @@ func AdvanceBlocks(ctx *cli.Context, l1RpcUrl string, numBlocks uint64) error {
236236
return nil
237237
}
238238

239+
func IntToHex(u uint64) string { return fmt.Sprintf("0x%x", u) }
240+
239241
func AdvanceBlocksToTS(client *rpc.Client, name string, fromTS, toTS uint64) error {
242+
// Skip if already at or past target
243+
if toTS <= fromTS {
244+
return nil
245+
}
246+
delta := toTS - fromTS
247+
248+
// Set next block timestamp exactly, then mine once - supported by Hardhat, Ganache, and Anvil
249+
if err := client.Call(nil, "evm_setNextBlockTimestamp", IntToHex(toTS)); err == nil {
250+
if err := client.Call(nil, "evm_mine"); err == nil {
251+
return nil
252+
}
253+
}
254+
255+
// Evm_mine with explicit timestamp - Hardhat supports evm_mine with { timestamp }
256+
if err := client.Call(nil, "evm_mine", map[string]any{"timestamp": IntToHex(toTS)}); err == nil {
257+
return nil
258+
}
259+
260+
// Increase time by delta then mine once - supported by Hardhat, Ganache, Anvil
261+
if err := client.Call(nil, "evm_increaseTime", IntToHex(delta)); err == nil {
262+
if err := client.Call(nil, "evm_mine"); err == nil {
263+
return nil
264+
}
265+
}
266+
240267
// Set number of blocks to move each iteration
241268
const blocksPerBatch = 1
242269

@@ -268,6 +295,9 @@ func GetTimestamp(client *rpc.Client, name string) (uint64, error) {
268295
}
269296

270297
func SyncL1L2Timestamps(ctx *cli.Context, l1RpcUrl string, l2RpcUrl string) error {
298+
// Define how much buffer to allow for timestamp proximity
299+
const tsBuffer = 12
300+
271301
// Connect to l1
272302
l1Client, err := rpc.Dial(l1RpcUrl)
273303
if err != nil {
@@ -293,9 +323,9 @@ func SyncL1L2Timestamps(ctx *cli.Context, l1RpcUrl string, l2RpcUrl string) erro
293323
}
294324

295325
// Advance one or the other until we reach sync
296-
if l1TS > l2TS {
326+
if l1TS > l2TS+tsBuffer {
297327
return AdvanceBlocksToTS(l2Client, "L2", l2TS, l1TS)
298-
} else if l2TS > l1TS {
328+
} else if l2TS > l1TS+tsBuffer {
299329
return AdvanceBlocksToTS(l1Client, "L1", l1TS, l2TS)
300330
}
301331

0 commit comments

Comments
 (0)