Skip to content
This repository was archived by the owner on Jun 9, 2026. It is now read-only.

Commit 8e985d2

Browse files
authored
refactor: rationalise usage of sae.SUT.runConsensusLoop() (#218)
1. Renames `SUT.runConsensusLoop()` to `runConsensusLoopOnPreference()` to allow (2). 2. Renames `runConsensusLoopFromLastAccepted()` to `runConsensusLoop()` as it's used so frequently that it should be the shorter, generic method. 3. Simplifies all usage of `runConsensusLoopOnPreference(..., sut.lastAcceptedBlock(t) / sut.genesis)` to just `runConsensusLoop()`. 4. Where appropriate, removes unnecessary transactions passed to `runConsensusLoop()`.
1 parent 21ff5ff commit 8e985d2

6 files changed

Lines changed: 42 additions & 49 deletions

File tree

sae/recovery_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestRecoverFromDatabase(t *testing.T) {
6262
}
6363

6464
vmTime.advance(850 * time.Millisecond)
65-
b := src.runConsensusLoop(t, src.lastAcceptedBlock(t))
65+
b := src.runConsensusLoop(t)
6666
if !quick {
6767
require.Len(t, b.Transactions(), 1, "transactions in block")
6868
}
@@ -113,7 +113,7 @@ func TestRecoverFromDatabase(t *testing.T) {
113113
} {
114114
t.Run(sys.name, func(t *testing.T) {
115115
sys.mustSendTx(t, tx)
116-
b := sys.runConsensusLoop(t, sys.lastAcceptedBlock(t))
116+
b := sys.runConsensusLoop(t)
117117
require.Len(t, b.Transactions(), 1)
118118
require.NoError(t, b.WaitUntilExecuted(sys.ctx))
119119
})

sae/rpc_stateful_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestEthCall(t *testing.T) {
4343
}
4444

4545
sign := sut.wallet.SetNonceAndSign
46-
b := sut.runConsensusLoopFromLastAccepted(t, sign(t, 0, deploy), sign(t, 0, deposit))
46+
b := sut.runConsensusLoop(t, sign(t, 0, deploy), sign(t, 0, deposit))
4747
require.Len(t, b.Transactions(), 2, "tx count")
4848
require.NoErrorf(t, b.WaitUntilExecuted(ctx), "%T.WaitUntilExecuted()", b)
4949
for _, r := range b.Receipts() {
@@ -52,7 +52,7 @@ func TestEthCall(t *testing.T) {
5252

5353
vmTime.advanceToSettle(ctx, t, b)
5454
for range 2 {
55-
bb := sut.runConsensusLoop(t, sut.lastAcceptedBlock(t))
55+
bb := sut.runConsensusLoop(t)
5656
vmTime.advanceToSettle(ctx, t, bb)
5757
}
5858
_, ok := sut.rawVM.blocks.Load(b.Hash())

sae/rpc_test.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestSubscriptions(t *testing.T) {
166166
runConsensusLoop := func(wantLogs ...types.Log) {
167167
t.Helper()
168168

169-
b := sut.runConsensusLoop(t, sut.lastAcceptedBlock(t))
169+
b := sut.runConsensusLoop(t)
170170
require.Equal(t, b.Hash(), (<-newHeads).Hash(), "header hash from newHeads subscription")
171171

172172
for _, want := range wantLogs {
@@ -423,28 +423,23 @@ func TestEthGetters(t *testing.T) {
423423
blockingPrecompile := common.Address{'b', 'l', 'o', 'c', 'k'}
424424
registerBlockingPrecompile(t, blockingPrecompile)
425425

426-
createTx := func(t *testing.T, to common.Address) *types.Transaction {
427-
t.Helper()
428-
return sut.wallet.SetNonceAndSign(t, 0, &types.DynamicFeeTx{
429-
To: &to,
430-
Gas: params.TxGas,
431-
GasFeeCap: big.NewInt(1),
432-
})
433-
}
434-
435426
genesis := sut.lastAcceptedBlock(t)
436427

437428
// Once a block is settled, its ancestors are only accessible from the
438429
// database.
439-
onDisk := sut.runConsensusLoopFromLastAccepted(t, createTx(t, zeroAddr))
430+
onDisk := sut.runConsensusLoop(t)
440431

441-
settled := sut.runConsensusLoopFromLastAccepted(t, createTx(t, zeroAddr))
432+
settled := sut.runConsensusLoop(t)
442433
vmTime.advanceToSettle(ctx, t, settled)
443434

444-
executed := sut.runConsensusLoopFromLastAccepted(t, createTx(t, zeroAddr))
435+
executed := sut.runConsensusLoop(t)
445436
require.NoErrorf(t, executed.WaitUntilExecuted(ctx), "%T.WaitUntilExecuted()", executed)
446437

447-
pending := sut.runConsensusLoopFromLastAccepted(t, createTx(t, blockingPrecompile))
438+
pending := sut.runConsensusLoop(t, sut.wallet.SetNonceAndSign(t, 0, &types.DynamicFeeTx{
439+
To: &blockingPrecompile,
440+
Gas: params.TxGas,
441+
GasFeeCap: big.NewInt(1),
442+
}))
448443

449444
for _, b := range []*blocks.Block{genesis, onDisk, settled, executed, pending} {
450445
t.Run(fmt.Sprintf("block_num_%d", b.Height()), func(t *testing.T) {
@@ -532,15 +527,15 @@ func TestGetLogs(t *testing.T) {
532527
// and therefore moved to disk.
533528
indexed := make([]*blocks.Block, bloomSectionSize)
534529
for i := range indexed {
535-
indexed[i] = sut.runConsensusLoopFromLastAccepted(t, txWithLog(t))
530+
indexed[i] = sut.runConsensusLoop(t, txWithLog(t))
536531
}
537532

538-
settled := sut.runConsensusLoopFromLastAccepted(t, txWithLog(t))
533+
settled := sut.runConsensusLoop(t, txWithLog(t))
539534
vmTime.advanceToSettle(ctx, t, settled)
540535

541-
noLogs := sut.runConsensusLoopFromLastAccepted(t, txWithoutLog(t))
536+
noLogs := sut.runConsensusLoop(t, txWithoutLog(t))
542537

543-
executed := sut.runConsensusLoopFromLastAccepted(t, txWithLog(t))
538+
executed := sut.runConsensusLoop(t, txWithLog(t))
544539
require.NoErrorf(t, executed.WaitUntilExecuted(ctx), "%T.WaitUntilExecuted()", executed)
545540

546541
// Although the FiltersAPI will work without any blocks indexed, such a
@@ -698,7 +693,7 @@ func TestGetReceipts(t *testing.T) {
698693

699694
slice := func(t *testing.T, from, to int) (*blocks.Block, []*types.Receipt) {
700695
t.Helper()
701-
b := sut.runConsensusLoopFromLastAccepted(t, txs[from:to]...)
696+
b := sut.runConsensusLoop(t, txs[from:to]...)
702697
rs := want[from:to]
703698

704699
var totalGas uint64
@@ -725,7 +720,7 @@ func TestGetReceipts(t *testing.T) {
725720
Gas: params.TxGas,
726721
GasPrice: big.NewInt(1),
727722
})
728-
pending := sut.runConsensusLoopFromLastAccepted(t, pendingTx)
723+
pending := sut.runConsensusLoop(t, pendingTx)
729724

730725
var tests []rpcTest
731726
for _, tc := range []struct {
@@ -913,7 +908,7 @@ func TestDebugGetRawTransaction(t *testing.T) {
913908
Gas: params.TxGas,
914909
GasFeeCap: big.NewInt(1),
915910
})
916-
b := sut.runConsensusLoopFromLastAccepted(t, tx)
911+
b := sut.runConsensusLoop(t, tx)
917912
require.NoErrorf(t, b.WaitUntilExecuted(ctx), "%T.WaitUntilExecuted()", b)
918913

919914
marshaled, err := tx.MarshalBinary()
@@ -1179,17 +1174,17 @@ func TestResolveBlockNumberOrHash(t *testing.T) {
11791174
opt, vmTime := withVMTime(t, time.Unix(saeparams.TauSeconds, 0))
11801175
ctx, sut := newSUT(t, 0, opt)
11811176

1182-
settled := sut.runConsensusLoop(t, sut.lastAcceptedBlock(t))
1177+
settled := sut.runConsensusLoop(t)
11831178
vmTime.advanceToSettle(ctx, t, settled)
11841179

11851180
for range 2 {
1186-
b := sut.runConsensusLoop(t, sut.lastAcceptedBlock(t))
1181+
b := sut.runConsensusLoop(t)
11871182
vmTime.advanceToSettle(ctx, t, b)
11881183
}
11891184
_, ok := sut.rawVM.blocks.Load(settled.Hash())
11901185
require.False(t, ok, "settled block still in VM memory")
11911186

1192-
accepted := sut.runConsensusLoop(t, sut.lastAcceptedBlock(t))
1187+
accepted := sut.runConsensusLoop(t)
11931188
require.NoError(t, sut.SetPreference(ctx, accepted.ID()), "SetPreference()")
11941189

11951190
b, err := sut.BuildBlock(ctx)

sae/tx_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestTxTypeSupport(t *testing.T) {
4444
t.FailNow()
4545
}
4646
}
47-
b := sut.runConsensusLoop(t, sut.genesis)
47+
b := sut.runConsensusLoop(t)
4848
require.NoErrorf(t, b.WaitUntilExecuted(ctx), "%T.WaitUntilExecuted()", b)
4949

5050
sdb := sut.stateAt(t, b.PostExecutionStateRoot())

sae/vm_test.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -376,24 +376,26 @@ func (s *SUT) createAndVerifyBlock(tb testing.TB, preference *blocks.Block, txs
376376
return b
377377
}
378378

379-
// createAndAcceptBlock is equivalent to [SUT.createAndVerifyBlock] except that
380-
// it also accepts the block with [VM.AcceptBlock]. It does NOT wait for it to
381-
// be executed; to do this automatically, set the [VM] to [snow.Bootstrapping].
379+
// runConsensusLoopOnPreference is equivalent to [SUT.createAndVerifyBlock]
380+
// except that it also accepts the block with [VM.AcceptBlock]. It does NOT wait
381+
// for it to be executed; to do this automatically, set the [VM] to
382+
// [snow.Bootstrapping].
382383
//
383384
// There is no longer any need to wrap the block as an [adaptor.Block] so it is
384385
// returned in its raw form, unlike earlier steps in the consenus loop.
385-
func (s *SUT) runConsensusLoop(tb testing.TB, preference *blocks.Block, txs ...*types.Transaction) *blocks.Block {
386+
func (s *SUT) runConsensusLoopOnPreference(tb testing.TB, preference *blocks.Block, txs ...*types.Transaction) *blocks.Block {
386387
tb.Helper()
387388
b := s.createAndVerifyBlock(tb, preference, txs...)
388389
require.NoErrorf(tb, b.Accept(s.context(tb)), "%T.Accept()", b)
389390
return unwrap(tb, b)
390391
}
391392

392-
// runConsensusLoopFromLastAccepted is a convenience wrapper for
393-
// [SUT.runConsensusLoop], using [SUT.lastAcceptedBlock] as the preference.
394-
func (s *SUT) runConsensusLoopFromLastAccepted(tb testing.TB, txs ...*types.Transaction) *blocks.Block {
393+
// runConsensusLoop is a convenience wrapper for
394+
// [SUT.runConsensusLoopOnPreference], using [SUT.lastAcceptedBlock] as the
395+
// preference.
396+
func (s *SUT) runConsensusLoop(tb testing.TB, txs ...*types.Transaction) *blocks.Block {
395397
tb.Helper()
396-
return s.runConsensusLoop(tb, s.lastAcceptedBlock(tb), txs...)
398+
return s.runConsensusLoopOnPreference(tb, s.lastAcceptedBlock(tb), txs...)
397399
}
398400

399401
// waitUntilExecuted blocks until an external indicator shows that `b` has been
@@ -545,7 +547,7 @@ func TestIntegration(t *testing.T) {
545547
sut.syncMempool(t) // technically we've only proven 1 tx added, as unlikely as a race is
546548
require.Equal(t, numTxs, sut.rawVM.numPendingTxs(), "number of pending txs")
547549

548-
b := sut.runConsensusLoop(t, sut.genesis)
550+
b := sut.runConsensusLoopOnPreference(t, sut.genesis)
549551
assert.Equal(t, sut.genesis.ID(), b.Parent(), "BuildBlock() builds on preference")
550552
require.Lenf(t, b.Transactions(), numTxs, "%T.Transactions()", b)
551553

@@ -559,7 +561,7 @@ func TestIntegration(t *testing.T) {
559561
// If the tx-inclusion logic were broken then this would include the
560562
// transactions again, resulting in a FATAL in the execution loop due to
561563
// non-increasing nonce.
562-
b := sut.runConsensusLoop(t, b)
564+
b := sut.runConsensusLoopOnPreference(t, b)
563565
assert.Emptyf(t, b.Transactions(), "%T.Transactions()", b)
564566
require.NoErrorf(t, b.WaitUntilExecuted(ctx), "%T.WaitUntilExecuted()", b)
565567
})
@@ -580,9 +582,8 @@ func TestEmptyChainConfig(t *testing.T) {
580582
}
581583
}))
582584
for range 5 {
583-
sut.runConsensusLoop(t, sut.lastAcceptedBlock(t))
585+
sut.runConsensusLoop(t)
584586
}
585-
sut.waitUntilExecuted(t, sut.lastAcceptedBlock(t))
586587
}
587588

588589
func TestSyntacticBlockChecks(t *testing.T) {
@@ -639,17 +640,14 @@ func TestAcceptBlock(t *testing.T) {
639640
require.NoError(t, sut.SetState(ctx, snow.Bootstrapping), "SetState(Bootstrapping)")
640641

641642
unsettled := []*blocks.Block{sut.genesis}
642-
last := func() *blocks.Block {
643-
return unsettled[len(unsettled)-1]
644-
}
645643
sut.genesis = nil // allow it to be GCd when appropriate
646644

647645
rng := rand.New(rand.NewPCG(0, 0)) //nolint:gosec // Reproducibility is useful for tests
648646
for range 100 {
649647
ffMillis := 100 + rng.IntN(1000*(1+saeparams.TauSeconds))
650648
vmTime.advance(time.Millisecond * time.Duration(ffMillis))
651649

652-
b := sut.runConsensusLoop(t, last())
650+
b := sut.runConsensusLoop(t)
653651
unsettled = append(unsettled, b)
654652
sut.assertBlockHashInvariants(ctx, t)
655653

@@ -808,10 +806,10 @@ func TestGetBlock(t *testing.T) {
808806
genesis := sut.lastAcceptedBlock(t)
809807
// Once a block is settled, its ancestors are only accessible from the
810808
// database.
811-
onDisk := sut.runConsensusLoopFromLastAccepted(t)
812-
settled := sut.runConsensusLoopFromLastAccepted(t)
809+
onDisk := sut.runConsensusLoop(t)
810+
settled := sut.runConsensusLoop(t)
813811
vmTime.advanceToSettle(ctx, t, settled)
814-
unsettled := sut.runConsensusLoopFromLastAccepted(t)
812+
unsettled := sut.runConsensusLoop(t)
815813

816814
verified := sut.createAndVerifyBlock(t, unsettled)
817815
unverified := sut.buildAndParseBlock(

sae/worstcase_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestWorstCase(t *testing.T) {
175175
}))
176176
}
177177

178-
b := sut.runConsensusLoop(t, sut.genesis)
178+
b := sut.runConsensusLoop(t)
179179
require.NoError(t, b.WaitUntilExecuted(ctx), "%T.WaitUntilExecuted()", b)
180180
require.Lenf(t, b.Receipts(), len(precompileTests), "%T.Receipts()", b)
181181
for i, r := range b.Receipts() {

0 commit comments

Comments
 (0)