Skip to content

Commit ab5961a

Browse files
authored
Automatically commit reference block after fork mode initialization (#898)
When fork mode is initialized, the emulator now automatically commits an initial empty block to provide a valid reference block ID for transactions. This eliminates the need for users to manually call ExecuteAndCommitBlock() before submitting transactions. Previously, fork mode would initialize but leave the emulator in a state where it couldn't accept transactions until a reference block was manually committed. Tests had to work around this by explicitly committing a block. Changes: - Auto-commit initial reference block in NewEmulatorServer when ForkHost is set - Remove manual workaround from fork integration tests - Add logging for the committed reference block Fixes the issue where forked emulators were not immediately ready to accept transactions.
1 parent 5acc152 commit ab5961a

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

server/fork_integration_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ func TestForkingAgainstTestnet(t *testing.T) {
6969
t.Fatalf("expected ChainID to be Testnet after fork detection, got %q", cfg.ChainID)
7070
}
7171

72-
// Create an initial local block so we have a valid reference block ID in the forked store
73-
if _, _, err := srv.Emulator().ExecuteAndCommitBlock(); err != nil {
74-
t.Fatalf("prime local block: %v", err)
75-
}
76-
7772
// Submit a minimal transaction against the forked emulator to ensure tx processing works
7873
latest, err := srv.Emulator().GetLatestBlock()
7974
if err != nil {
@@ -205,11 +200,6 @@ func TestForkingAgainstMainnet(t *testing.T) {
205200
t.Fatalf("expected ChainID to be Mainnet after fork detection, got %q", cfg.ChainID)
206201
}
207202

208-
// Create an initial local block so we have a valid reference block ID in the forked store
209-
if _, _, err := srv.Emulator().ExecuteAndCommitBlock(); err != nil {
210-
t.Fatalf("prime local block: %v", err)
211-
}
212-
213203
// Test account key retrieval for a known mainnet account with multiple keys
214204
// This tests the account key deduplication shim by executing a script that accesses
215205
// keys and ensures no errors occur (successful execution proves the shim works)

server/server.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,19 @@ func NewEmulatorServer(logger *zerolog.Logger, conf *Config) *EmulatorServer {
193193
return nil
194194
}
195195

196+
// In fork mode, commit an initial reference block so transactions can be submitted immediately
197+
if conf.ForkHost != "" {
198+
block, _, err := emulatedBlockchain.ExecuteAndCommitBlock()
199+
if err != nil {
200+
logger.Error().Err(err).Msg("❗ Failed to commit initial reference block for fork mode")
201+
return nil
202+
}
203+
logger.Info().
204+
Uint64("blockHeight", block.Height).
205+
Str("blockID", block.ID().String()).
206+
Msg("📦 Committed initial reference block for fork mode")
207+
}
208+
196209
chain := emulatedBlockchain.GetChain()
197210

198211
sc := systemcontracts.SystemContractsForChain(chain.ChainID())

0 commit comments

Comments
 (0)