Skip to content

Commit 65234e4

Browse files
committed
fix: only upgrade last sync block if no later chain head exists
1 parent e940d2b commit 65234e4

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

blocks/execution.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ func (e *executionResults) Equal(f *executionResults) bool {
4848
}
4949

5050
// MarkExecuted marks the block as having being executed at the specified
51-
// time(s) and with the specified results. This function MUST NOT be called more
52-
// than once. The wall-clock [time.Time] is for metrics only.
51+
// time(s) and with the specified results. It also sets the chain's head block
52+
// to b.
5353
//
5454
// MarkExecuted guarantees that state is persisted to the database before
5555
// in-memory indicators of execution are updated. [Block.Executed] returning
5656
// true is therefore indicative of a successful database write by MarkExecuted.
57-
func (b *Block) MarkExecuted(db ethdb.Database, isLastSyncBlock bool, byGas *gastime.Time, byWall time.Time, receipts types.Receipts, stateRootPost common.Hash) error {
57+
//
58+
// This function MUST NOT be called more than once. The wall-clock [time.Time]
59+
// is for metrics only.
60+
func (b *Block) MarkExecuted(db ethdb.Database, byGas *gastime.Time, byWall time.Time, receipts types.Receipts, stateRootPost common.Hash) error {
5861
var used gas.Gas
5962
for _, r := range receipts {
6063
used += gas.Gas(r.GasUsed)
@@ -70,14 +73,8 @@ func (b *Block) MarkExecuted(db ethdb.Database, isLastSyncBlock bool, byGas *gas
7073
}
7174

7275
batch := db.NewBatch()
73-
if !isLastSyncBlock {
74-
// Although the last synchronous block is required to be the head block
75-
// at the time of upgrade, not setting the head here allows this method
76-
// to be called idempotently for that block. This is useful when
77-
// converting it to an SAE block.
78-
rawdb.WriteHeadBlockHash(batch, b.Hash())
79-
rawdb.WriteHeadHeaderHash(batch, b.Hash())
80-
}
76+
rawdb.WriteHeadBlockHash(batch, b.Hash())
77+
rawdb.WriteHeadHeaderHash(batch, b.Hash())
8178
rawdb.WriteReceipts(batch, b.Hash(), b.NumberU64(), receipts)
8279
if err := b.writePostExecutionState(batch, e); err != nil {
8380
return err

db.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"math"
7+
"math/big"
78

89
"github.com/ava-labs/avalanchego/database"
910
"github.com/ava-labs/libevm/common"
@@ -19,6 +20,11 @@ func (vm *VM) upgradeLastSynchronousBlock(hash common.Hash) error {
1920
if lastSyncNum == nil {
2021
return fmt.Errorf("read number of last synchronous block (%#x): %w", hash, database.ErrNotFound)
2122
}
23+
lastSyncNumBig := new(big.Int).SetUint64(*lastSyncNum)
24+
if rawdb.ReadHeadHeader(vm.db).Number.Cmp(lastSyncNumBig) > 0 {
25+
return nil
26+
}
27+
2228
ethBlock := rawdb.ReadBlock(vm.db, hash, *lastSyncNum)
2329
if ethBlock == nil {
2430
return fmt.Errorf("read last synchronous block (%#x): %w", hash, database.ErrNotFound)
@@ -45,7 +51,7 @@ func (vm *VM) upgradeLastSynchronousBlock(hash common.Hash) error {
4551
1e6, 0,
4652
)
4753
receipts := rawdb.ReadRawReceipts(vm.db, hash, block.Height())
48-
if err := block.MarkExecuted(vm.db, true, clock, block.Timestamp(), receipts, block.Root()); err != nil {
54+
if err := block.MarkExecuted(vm.db, clock, block.Timestamp(), receipts, block.Root()); err != nil {
4955
return err
5056
}
5157
if err := block.WriteLastSettledNumber(vm.db); err != nil {

saexec/execution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (e *Executor) execute(ctx context.Context, b *blocks.Block) error {
135135
// 1. [blocks.Block.MarkExecuted] guarantees disk then in-memory changes.
136136
// 2. Internal indicator of last executed MUST follow in-memory change.
137137
// 3. External indicator of last executed MUST follow internal indicator.
138-
if err := b.MarkExecuted(e.db, false, e.gasClock.Clone(), endTime, receipts, root); err != nil {
138+
if err := b.MarkExecuted(e.db, e.gasClock.Clone(), endTime, receipts, root); err != nil {
139139
return err
140140
}
141141
e.lastExecuted.Store(b) // (2)

0 commit comments

Comments
 (0)