Skip to content

Commit 28d672a

Browse files
Derive Autobahn AppHash from the storage hash builder (CON-444) (#4306)
## Summary - Take Autobahn's AppHash from `GigaStorageManager.AppHashBuilder()`. Execution reports the block hash before the block runs. The state store reports the state hash and BUD hash, and a zero receipt hash until execution reports a real one. `GigaActivationHeight` is genesis `InitialHeight`. - Let execution run ahead of app-hash proposal. `FinalizeBlock` and `Commit` hand each committed block to a second loop that waits for the builder's listener and calls `PushAppHash`. Both handoffs hold 128 blocks. The storage-tail timer stays on the execute loop. - On restart, backfill proposes every committed road through the app tip, including the road already proposed, through the same `finishExecuteBlock` path. The listener does not replay; its newest published hash must be the app tip, and the next callback is the block after it. Backfill uses one `GetValidators()` snapshot at the tip. Closing-road stake is a follow-up. - An execute stop cancels the hash wait without canceling an in-progress proposal. The listener's context ends when execution returns, so a later callback cannot block the builder. - The router no longer opens the equivocation vault or prunes from `RetainHeight`, and `hash-vault-disabled-unsafe` is removed. Mock and EVM-only both feed the builder. A committee already voting on ABCI `ResponseFinalizeBlock.AppHash` cannot roll onto this binary. Autobahn has not launched, so there is nothing to migrate. The rollout is a coordinated start of this binary. ## Test plan - [x] `TestExecuteBuffersPendingAppHashes` — a run of commits finishes before the hash loop receives them - [x] `TestBackfillAppHashesReadsTheBuilder` — the already-proposed road is replayed, and a missing hash fails the restart - [x] `TestListenerContinuesAtTheAppTip` and `TestRunExecuteRejectsAListenerOffTheAppTip` — a restart refuses a listener that is not at the app tip - [x] `TestRunAppHashesStopsWaitingWhenExecuteStops` — an execute stop cancels the hash wait and not the proposal - [x] `TestRunExecuteStopsTheListenerWhenItReturns` — hangs if the listener keeps the parent context after execution returns - [x] `go test -race` on `./sei-tendermint/internal/p2p` - [x] CI --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 222ffe9 commit 28d672a

30 files changed

Lines changed: 1173 additions & 439 deletions

‎cmd/seid/cmd/legacy_config_fuzz_test.go‎

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"reflect"
1010
"slices"
11-
"strings"
1211
"testing"
1312

1413
"go.opentelemetry.io/otel/sdk/trace"
@@ -172,68 +171,6 @@ var tmKeys = []tmKey{
172171
},
173172
}
174173

175-
// FuzzHashVaultDisabledUnsafeResolution pins the root-scope kill switch for the
176-
// app-hash equivocation guard.
177-
//
178-
// Two things make it worth its own target. It is a bool whose safe value is the
179-
// default, so an absent key must resolve false — setting it true removes
180-
// equivocation protection with only a log banner. And it lives at TOML root scope,
181-
// before any [section] header: nested under a section it parses as a different key
182-
// and is silently ignored, which reads as "I disabled the guard" while the guard
183-
// stays on, and would read the other way round if the scope were ever mishandled.
184-
// The document is built from the fuzzer's choices rather than taken as free text,
185-
// so the expected outcome follows from construction instead of being a second
186-
// input the fuzzer can mutate out of agreement with the first.
187-
func FuzzHashVaultDisabledUnsafeResolution(f *testing.F) {
188-
f.Add(false, false, false)
189-
f.Add(true, true, false) // root scope, true: the guard is off
190-
f.Add(true, false, false) // root scope, false
191-
f.Add(true, true, true) // nested under a section: silently ignored
192-
f.Add(true, false, true)
193-
194-
f.Fuzz(func(t *testing.T, present, value, underSection bool) {
195-
configtest.Isolate(t)
196-
home := configtest.NewHome(t)
197-
198-
var doc strings.Builder
199-
if present {
200-
if underSection {
201-
doc.WriteString("[p2p]\n")
202-
}
203-
fmt.Fprintf(&doc, "hash-vault-disabled-unsafe = %t\n", value)
204-
}
205-
if doc.Len() > 0 {
206-
home.WriteConfigTOML(t, []byte(doc.String()))
207-
}
208-
209-
// Root scope is the only placement that resolves. Nested under a section the
210-
// key becomes p2p.hash-vault-disabled-unsafe, which nothing reads.
211-
wantDisabled := present && value && !underSection
212-
213-
got := applyLegacy(t, home, nil)
214-
if got.err != nil {
215-
t.Fatalf("Apply must succeed on a well-formed config.toml, got %v", got.err)
216-
}
217-
if got.ctx.Config.HashVaultDisabledUnsafe != wantDisabled {
218-
t.Fatalf("hash-vault-disabled-unsafe resolved to %v, want %v, from:\n%s",
219-
got.ctx.Config.HashVaultDisabledUnsafe, wantDisabled, doc.String())
220-
}
221-
})
222-
}
223-
224-
// TestHashVaultDisabledUnsafeDefaultsToEnabledGuard states the default on its own,
225-
// so the guard's safe value is pinned even if every seed above were removed.
226-
func TestHashVaultDisabledUnsafeDefaultsToEnabledGuard(t *testing.T) {
227-
configtest.Isolate(t)
228-
got := applyLegacy(t, configtest.NewHome(t), nil)
229-
if got.err != nil {
230-
t.Fatalf("Apply: %v", got.err)
231-
}
232-
if got.ctx.Config.HashVaultDisabledUnsafe {
233-
t.Fatal("an empty home must leave the app-hash equivocation guard enabled")
234-
}
235-
}
236-
237174
// FuzzApplyPrecedenceTendermint pins the resolution order for Tendermint config:
238175
// flag beats environment beats config.toml beats the in-code default.
239176
//

‎giga/evmonly/cmd/evmonly-loadtest/pipeline.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ func runPrebuilt(ctx context.Context, cfg config, state *generatedState, workloa
160160
if err != nil {
161161
return fmt.Errorf("encode generated genesis state: %w", err)
162162
}
163+
genesisHash := scenarios.BlockContext(scenarioConfig(cfg), 1).BlockHash
164+
if err := storage.AppHashBuilder().ReportBlockHash(ctx, 1, [32]byte(genesisHash)); err != nil {
165+
return fmt.Errorf("report the block hash of block 1: %w", err)
166+
}
163167
if err := storage.StateDB().CommitStateChanges(1, genesisChanges); err != nil {
164168
return fmt.Errorf("commit generated genesis state: %w", err)
165169
}
@@ -206,7 +210,7 @@ func runPrebuilt(ctx context.Context, cfg config, state *generatedState, workloa
206210
for workerID := 0; workerID < cfg.workers; workerID++ {
207211
workerID := workerID
208212
group.Go(func() error {
209-
return executeBlocks(groupCtx, workerID, executor, preparedBlocks, metrics)
213+
return executeBlocks(groupCtx, workerID, storage, executor, preparedBlocks, metrics)
210214
})
211215
}
212216

@@ -570,6 +574,7 @@ func durationFromUint64Nanos(nanos uint64) time.Duration {
570574
func executeBlocks(
571575
ctx context.Context,
572576
workerID int,
577+
storage *bootstrap.GigaStorageManager,
573578
executor *evmonly.Executor,
574579
blocks <-chan preparedBlockEnvelope,
575580
metrics *loadMetrics,
@@ -583,6 +588,10 @@ func executeBlocks(
583588
if !ok {
584589
return nil
585590
}
591+
blockHash := [32]byte(block.block.Context.BlockHash)
592+
if err := storage.AppHashBuilder().ReportBlockHash(ctx, block.block.Context.Number, blockHash); err != nil {
593+
return fmt.Errorf("report the block hash of block %d: %w", block.block.Context.Number, err)
594+
}
586595
result, err := executor.ExecutePreparedBlock(ctx, block.block)
587596
if err != nil {
588597
if ctx.Err() != nil {

‎giga/metrics/autobahn_loop.go‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,11 @@ const (
2323
// PhaseStorage is time spent persisting receipts, state, and the app commit.
2424
PhaseStorage = "storage"
2525

26-
// StoragePhaseVaultCommit is the app hash's durable write to the hash vault.
27-
StoragePhaseVaultCommit = "vault_commit"
2826
// StoragePhaseAppCommit is the app's Commit call.
2927
StoragePhaseAppCommit = "app_commit"
30-
// StoragePhasePushAppHash is publishing the app hash to the data layer.
31-
StoragePhasePushAppHash = "push_app_hash"
32-
// StoragePhaseBookkeeping is the loop's own work between those calls: committee weights, gas
33-
// accounting and the executed-block window.
28+
// StoragePhaseBookkeeping is the loop's own work after Commit: committee weights and the
29+
// executed-block window.
3430
StoragePhaseBookkeeping = "bookkeeping"
35-
// StoragePhasePruneData is pruning the data layer below the app's retain height.
36-
StoragePhasePruneData = "prune_data"
37-
// StoragePhasePruneVault is pruning the hash vault to the same boundary.
38-
StoragePhasePruneVault = "prune_vault"
3931
)
4032

4133
var (

‎sei-db/bench/gigasim/app_hashes.go‎

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
package gigasim
22

33
import (
4+
"context"
5+
"encoding/binary"
46
"fmt"
5-
"time"
67

7-
"github.com/sei-protocol/sei-chain/giga/apphash/vault"
8+
"github.com/sei-protocol/sei-chain/sei-db/bootstrap"
89
)
910

10-
// How often awaitAppHash checks the hash vault's head.
11-
const appHashPollInterval = 10 * time.Millisecond
12-
13-
// awaitAppHash waits until hashVault holds the app hash of block height.
14-
func awaitAppHash(hashVault vault.HashVault, height uint64) error {
15-
deadline := time.Now().Add(hashWaitTimeout)
16-
for {
17-
head, err := hashVault.GetLatestBlock()
18-
if err != nil {
19-
return fmt.Errorf("failed to read the hash vault's head: %w", err)
20-
}
21-
if head >= height {
22-
return nil
23-
}
24-
if time.Now().After(deadline) {
25-
return fmt.Errorf("the app hash of block %d was not stored within %s", height, hashWaitTimeout)
26-
}
27-
time.Sleep(appHashPollInterval)
11+
// reportExecutedBlockHash reports height's block hash before that block is committed.
12+
func reportExecutedBlockHash(manager *bootstrap.GigaStorageManager, height uint64) error {
13+
var hash [32]byte
14+
binary.BigEndian.PutUint64(hash[:8], height)
15+
// TODO: pass a context that outlives the run. The benchmark cancels its run
16+
// context to stop, and the block then in flight still reports.
17+
if err := manager.AppHashBuilder().ReportBlockHash(context.Background(), height, hash); err != nil {
18+
return fmt.Errorf("report the block hash of block %d: %w", height, err)
2819
}
20+
return nil
2921
}

‎sei-db/bench/gigasim/gigasim.go‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package gigasim
44

55
import (
66
"context"
7-
"errors"
87
"fmt"
98
"os"
109
"path/filepath"
@@ -19,6 +18,7 @@ import (
1918
"github.com/sei-protocol/sei-chain/sei-db/common/utils"
2019
dbconfig "github.com/sei-protocol/sei-chain/sei-db/config"
2120
"github.com/sei-protocol/sei-chain/sei-db/ledger_db/receipt"
21+
tmutils "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils"
2222
)
2323

2424
// GigaSim runs the benchmark, driving generated blocks through the block store, the state DB and the
@@ -533,6 +533,13 @@ func (g *GigaSim) persistExecutionResults(
533533
receiptBytes int64,
534534
writes blockWrites,
535535
) error {
536+
height, ok := tmutils.SafeCast[uint64](number)
537+
if !ok {
538+
return fmt.Errorf("block number %d does not fit in a uint64", number)
539+
}
540+
if err := reportExecutedBlockHash(g.storage, height); err != nil {
541+
return err
542+
}
536543
if g.receipts != nil {
537544
g.lifecycle.SetPhase("write_receipts")
538545
if err := g.receipts.writeBlock(number, records, receiptBytes); err != nil {
@@ -545,8 +552,6 @@ func (g *GigaSim) persistExecutionResults(
545552
// The benchmark runs no EVM, so its blocks have no transactions to hash receipts for. Without a receipt
546553
// hash the block's app hash is never built, so the submission is not abandoned when the run is cancelled:
547554
// the state is already committed, and closing waits for that block's app hash.
548-
//nolint:gosec // block numbers are positive
549-
height := uint64(number)
550555
if err := g.storage.ReceiptHasher().Submit(context.Background(), height, nil); err != nil {
551556
return fmt.Errorf("failed to submit the receipts of block %d: %w", number, err)
552557
}
@@ -628,19 +633,11 @@ func (g *GigaSim) teardown() {
628633
// closePipeline stops the executor pool and releases every database, in the order the stores require:
629634
// the executors stop reading first, then the state read view closes, because the commit store cannot
630635
// release the reference it holds while a view is open, and the storage manager closes last.
631-
//
632-
// The app hash of the last block committed is waited for before storage closes. A restart rolls every
633-
// store back to the newest app hash, and agreedNextBlockNumber refuses a block store left ahead of state.
634636
func (g *GigaSim) closePipeline() error {
635637
g.cancel()
636638
g.stopExecutors()
637639
g.state.Close()
638-
var err error
639-
if highest := g.HighestBlock(); highest > 0 {
640-
//nolint:gosec // block numbers are positive
641-
err = awaitAppHash(g.storage.AppHashVault(), uint64(highest))
642-
}
643-
err = errors.Join(err, g.storage.Close())
640+
err := g.storage.Close()
644641
g.stopStorage()
645642
return err
646643
}

‎sei-db/bootstrap/app_hash.go‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,11 @@ func (m *GigaStorageManager) reportStateHashesToAppHashBuilder(
8585
return nil
8686
}
8787

88-
// reportStateHash reports a committed block's state hash to the app hash builder, along with a zero block hash. Its
89-
// signature is gigatypes.HashListener.
88+
// reportStateHash reports a committed block's state hash to the app hash builder.
89+
// Its signature is gigatypes.HashListener.
9090
func (m *GigaStorageManager) reportStateHash(ctx context.Context, blockNumber int64, hash *lthash.BlockHash) error {
9191
//nolint:gosec // committed block numbers are non-negative
9292
height := uint64(blockNumber)
93-
// TODO: report the real block hash once execution reports it.
94-
if err := m.appHashBuilder.ReportBlockHash(ctx, height, [32]byte{}); err != nil {
95-
return fmt.Errorf("report the block hash of block %d: %w", height, err)
96-
}
9793
stateHash := hash.Global.Checksum()
9894
componentHashes := hashlog.FlatKVComponentHashes(hash)
9995
if err := m.appHashBuilder.ReportStateHash(ctx, height, stateHash, componentHashes); err != nil {

‎sei-db/bootstrap/app_hash_test.go‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ func restartAt(t *testing.T, manager *GigaStorageManager, target int64) {
102102
require.NoError(t, manager.completeAppHashBuilderSetup(t.Context()))
103103
}
104104

105-
// Storage reports the state and BUD hashes of each committed block, with a zero block hash.
105+
// Storage reports the state hash and BUD hash of each committed block, execution reports its block
106+
// hash, and the receipt hasher reports the receipt hash.
106107
func TestAppHashesBuiltFromCommittedBlocks(t *testing.T) {
107108
manager, _ := openManager(t, func(cfg *config.GigaStorageConfig) {
108109
cfg.AppHashBuilderConfig.ChainID = testAppHashChainID
@@ -117,9 +118,12 @@ func TestAppHashesBuiltFromCommittedBlocks(t *testing.T) {
117118
height := uint64(i) + 1 //nolint:gosec // small test heights
118119
require.Equal(t, height, appHash.BlockHeight())
119120
require.Equal(t, testAppHashChainID, appHash.ChainID())
120-
require.Equal(t, [32]byte{}, appHash.BlockHash())
121+
require.Equal(t, testBlockHash(height), appHash.BlockHash())
121122
require.NotEqual(t, [32]byte{}, appHash.StateHash())
122123
require.NotEqual(t, apphash.BUDHash{}, appHash.BUDHash())
124+
emptyReceipts, err := receipthash.ComputeReceiptHash(nil)
125+
require.NoError(t, err)
126+
require.Equal(t, emptyReceipts, appHash.ReceiptHash())
123127
}
124128
head, err := manager.AppHashVault().GetLatestBlock()
125129
require.NoError(t, err)
@@ -140,7 +144,9 @@ func TestRestartWithStateAheadOfTheAppHashVault(t *testing.T) {
140144
savedVaultDir := filepath.Join(t.TempDir(), "vault")
141145
require.NoError(t, os.CopyFS(savedVaultDir, os.DirFS(vaultDir)))
142146
restartAt(t, manager, 3)
147+
reportTestBlockHash(t, manager, 4)
143148
commitBlock(t, manager, 4, evmBlock(4, 4))
149+
reportTestBlockHash(t, manager, 5)
144150
commitBlock(t, manager, 5, evmBlock(5, 5))
145151
awaitAppHashes(t, manager, 5)
146152
stopForRestart(t, manager)
@@ -160,7 +166,9 @@ func TestRestartWithStateAheadOfTheAppHashVault(t *testing.T) {
160166
newest, published := listenForAppHashes(t, manager)
161167
require.Equal(t, stored[2].AppHash(), newest.AppHash())
162168

169+
reportTestBlockHash(t, manager, 4)
163170
commitBlock(t, manager, 4, evmBlock(4, 4))
171+
reportTestBlockHash(t, manager, 5)
164172
commitBlock(t, manager, 5, evmBlock(5, 5))
165173
rebuilt := receiveAppHashes(t, published, 2)
166174
require.Equal(t, uint64(4), rebuilt[0].BlockHeight())
@@ -183,7 +191,9 @@ func TestRestartWithTheAppHashVaultAheadOfState(t *testing.T) {
183191
require.NoError(t, err)
184192
require.Equal(t, uint64(5), head, "the app hashes above the recovered height are kept")
185193

194+
reportTestBlockHash(t, manager, 4)
186195
commitBlock(t, manager, 4, evmBlock(4, 4))
196+
reportTestBlockHash(t, manager, 5)
187197
commitBlock(t, manager, 5, evmBlock(5, 5))
188198
rebuilt := receiveAppHashes(t, published, 2)
189199
require.Equal(t, stored[3].AppHash(), rebuilt[0].AppHash())
@@ -220,6 +230,7 @@ func TestAppHashCommitsToTheSubmittedReceipts(t *testing.T) {
220230
GasUsed: 21000,
221231
}}
222232

233+
reportTestBlockHash(t, manager, 1)
223234
require.NoError(t, manager.StateDB().CommitStateChanges(1, evmBlock(1, 1)))
224235
require.NoError(t, manager.ReceiptHasher().Submit(t.Context(), 1, receipts))
225236

‎sei-db/bootstrap/recovery_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bootstrap
22

33
import (
4+
"encoding/binary"
45
"fmt"
56
"math/big"
67
"os"
@@ -58,10 +59,26 @@ func evmBlock(addr, nonce byte) []*proto.NamedChangeSet {
5859
}}
5960
}
6061

62+
// testBlockHash is the block hash execution would report for height. It is a function of the height
63+
// alone, so a block re-executed after a restart reports the same hash the vault already stored.
64+
func testBlockHash(height uint64) [32]byte {
65+
var blockHash [32]byte
66+
binary.BigEndian.PutUint64(blockHash[:8], height)
67+
return blockHash
68+
}
69+
70+
// reportTestBlockHash reports height's block hash to the app hash builder, as execution does before
71+
// running the block.
72+
func reportTestBlockHash(t *testing.T, manager *GigaStorageManager, height uint64) {
73+
t.Helper()
74+
require.NoError(t, manager.AppHashBuilder().ReportBlockHash(t.Context(), height, testBlockHash(height)))
75+
}
76+
6177
// commitBlocks commits blocks 1 through through, and waits until their app hashes are stored.
6278
func commitBlocks(t *testing.T, manager *GigaStorageManager, through byte) {
6379
t.Helper()
6480
for block := byte(1); block <= through; block++ {
81+
reportTestBlockHash(t, manager, uint64(block))
6582
commitBlock(t, manager, int64(block), evmBlock(block, block))
6683
}
6784
awaitAppHashes(t, manager, uint64(through))

0 commit comments

Comments
 (0)