Skip to content

Commit a47089b

Browse files
authored
Integrate undo log (#4498)
* This PR integrates the new historical state store (based on undo logs) in giga.
1 parent e089531 commit a47089b

19 files changed

Lines changed: 919 additions & 190 deletions

‎giga/config/config.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type StorageConfig struct {
4242
RollbackWindow uint64 `mapstructure:"rollback_window"`
4343
// LookbackWindow is how many queryable blocks are kept below the rollback window; -1 keeps all.
4444
LookbackWindow int64 `mapstructure:"lookback_window"`
45+
// StateStoreBackend is how the state store keeps its history: "pebbledb" stores every version of
46+
// every key, "undolog" the value each block replaced, over the current state.
47+
StateStoreBackend string `mapstructure:"state_store_backend"`
4548
// PruneInterval is how often the storage garbage collector runs.
4649
PruneInterval time.Duration `mapstructure:"prune_interval"`
4750
// CheckpointTimeInterval is the wall-clock gap between state-commit checkpoints.
@@ -90,6 +93,7 @@ func defaultStorageConfig() StorageConfig {
9093
Receipts: true,
9194
RollbackWindow: gc.RollbackWindow,
9295
LookbackWindow: gc.LookbackWindow,
96+
StateStoreBackend: seidbconfig.DefaultSSBackend,
9397
PruneInterval: gc.PruneInterval,
9498
CheckpointTimeInterval: cp.TimeInterval,
9599
CheckpointBlockInterval: cp.BlockInterval,
@@ -102,6 +106,7 @@ const (
102106
FlagStorageReceipts = "giga.storage.receipts"
103107
FlagStorageRollbackWindow = "giga.storage.rollback_window"
104108
FlagStorageLookbackWindow = "giga.storage.lookback_window"
109+
FlagStorageStateStoreBackend = "giga.storage.state_store_backend"
105110
FlagStoragePruneInterval = "giga.storage.prune_interval"
106111
FlagStorageCheckpointTimeInterval = "giga.storage.checkpoint_time_interval"
107112
FlagStorageCheckpointBlockInterval = "giga.storage.checkpoint_block_interval"
@@ -137,6 +142,11 @@ func ReadConfig(opts AppOptions) (Config, error) {
137142
return cfg, fmt.Errorf("%s: %w", FlagStorageLookbackWindow, err)
138143
}
139144
}
145+
if v := opts.Get(FlagStorageStateStoreBackend); v != nil {
146+
if cfg.Storage.StateStoreBackend, err = cast.ToStringE(v); err != nil {
147+
return cfg, fmt.Errorf("%s: %w", FlagStorageStateStoreBackend, err)
148+
}
149+
}
140150
if v := opts.Get(FlagStoragePruneInterval); v != nil {
141151
if cfg.Storage.PruneInterval, err = cast.ToDurationE(v); err != nil {
142152
return cfg, fmt.Errorf("%s: %w", FlagStoragePruneInterval, err)
@@ -197,6 +207,12 @@ func (c Config) Validate() error {
197207
return fmt.Errorf("%s: must be >= 0, or -1 to keep all history, got %d", FlagStorageLookbackWindow,
198208
c.Storage.LookbackWindow)
199209
}
210+
switch c.Storage.StateStoreBackend {
211+
case seidbconfig.PebbleDBBackend, seidbconfig.PebbleDBUndoBackend:
212+
default:
213+
return fmt.Errorf("%s: %q is not %q or %q", FlagStorageStateStoreBackend, c.Storage.StateStoreBackend,
214+
seidbconfig.PebbleDBBackend, seidbconfig.PebbleDBUndoBackend)
215+
}
200216
if c.Storage.PruneInterval <= 0 {
201217
return fmt.Errorf("%s: must be positive, got %s", FlagStoragePruneInterval, c.Storage.PruneInterval)
202218
}
@@ -257,6 +273,11 @@ rollback_window = {{ .Giga.Storage.RollbackWindow }}
257273
# -1 keeps all history.
258274
lookback_window = {{ .Giga.Storage.LookbackWindow }}
259275
276+
# state_store_backend is how the state store keeps its history. "pebbledb" stores every version
277+
# of every key; "undolog" stores the value each block replaced, over the current state, and
278+
# drops expired history a bucket at a time.
279+
state_store_backend = "{{ .Giga.Storage.StateStoreBackend }}"
280+
260281
# prune_interval is how often the storage garbage collector runs.
261282
prune_interval = "{{ .Giga.Storage.PruneInterval }}"
262283

‎giga/config/config_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestDefaultsMatchTheStorageDefaults(t *testing.T) {
2727
require.True(t, s.Receipts)
2828
require.Equal(t, gc.RollbackWindow, s.RollbackWindow)
2929
require.Equal(t, gc.LookbackWindow, s.LookbackWindow)
30+
require.Equal(t, seidbconfig.DefaultStateStoreConfig().Backend, s.StateStoreBackend)
3031
require.Equal(t, gc.PruneInterval, s.PruneInterval)
3132
require.Equal(t, cp.TimeInterval, s.CheckpointTimeInterval)
3233
require.Equal(t, cp.BlockInterval, s.CheckpointBlockInterval)
@@ -38,6 +39,7 @@ func TestReadConfigReadsEveryKey(t *testing.T) {
3839
"giga.storage.receipts": "false",
3940
"giga.storage.rollback_window": "250",
4041
"giga.storage.lookback_window": "-1",
42+
"giga.storage.state_store_backend": "undolog",
4143
"giga.storage.prune_interval": "90s",
4244
"giga.storage.checkpoint_time_interval": "1h",
4345
"giga.storage.checkpoint_block_interval": "5000",
@@ -55,6 +57,7 @@ func TestReadConfigReadsEveryKey(t *testing.T) {
5557
Receipts: false,
5658
RollbackWindow: 250,
5759
LookbackWindow: -1,
60+
StateStoreBackend: seidbconfig.PebbleDBUndoBackend,
5861
PruneInterval: 90 * time.Second,
5962
CheckpointTimeInterval: time.Hour,
6063
CheckpointBlockInterval: 5000,
@@ -76,6 +79,7 @@ func TestReadConfigRejectsUnusableValues(t *testing.T) {
7679
"unknown mode": {"giga.storage.mode": "archive"},
7780
"non-boolean receipts": {"giga.storage.receipts": "maybe"},
7881
"lookback below -1": {"giga.storage.lookback_window": "-2"},
82+
"unknown backend": {"giga.storage.state_store_backend": "rocksdb"},
7983
"zero prune interval": {"giga.storage.prune_interval": "0s"},
8084
"negative occ workers": {"giga.execution.occ_workers": "-1"},
8185
"empty result pool": {"giga.execution.block_result_pool_size": "0"},

‎giga/config/register_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestTheDeclaredKeysAreTheKeysThisReaderResolves(t *testing.T) {
2121
FlagStorageReceipts,
2222
FlagStorageRollbackWindow,
2323
FlagStorageLookbackWindow,
24+
FlagStorageStateStoreBackend,
2425
FlagStoragePruneInterval,
2526
FlagStorageCheckpointTimeInterval,
2627
FlagStorageCheckpointBlockInterval,

‎giga/evmonly/giga_store.go‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ var (
2424

2525
var _ StateReader = gigaSnapshotStateReader{}
2626

27+
// historyStore is a giga store that takes a block's change set alongside its changesets, to keep the
28+
// values the block replaced.
29+
type historyStore interface {
30+
CommitStateChangesWithPrior(blockNum int64, changeset []*proto.NamedChangeSet, changes *gigatypes.StateChangeSet) error
31+
}
32+
2733
// NamedChangeSetEncoder converts an executor-native state result into the on-disk changesets
2834
// understood by a giga store. It must treat the input as immutable, and its output must not alias
2935
// the input, since the commit outlives the pooled block result. It may read the store only to
@@ -240,15 +246,16 @@ func (e *Executor) awaitPipelineCommit() error {
240246
// Commits stay ordered because only one is ever in flight: awaitPipelineCommit lands the previous
241247
// one before this is called.
242248
func (e *Executor) startPipelineCommit(blockNumber int64, changesets []*proto.NamedChangeSet, changes *StateChangeSet) error {
249+
// The commit outlives the block result, which returns to a pool and is reset.
250+
pending := changes.clone()
243251
// Close sets closed before taking storeMu, which the caller holds, so a block that sees it
244252
// unset is one Close waits for.
245253
if e.closed.Load() {
246254
if err := e.awaitPipelineCommit(); err != nil {
247255
return err
248256
}
249-
return e.stateStore.CommitStateChanges(blockNumber, changesets)
257+
return e.commitState(blockNumber, changesets, pending)
250258
}
251-
pending := changes.clone()
252259
done := make(chan struct{})
253260
e.pipelineMu.Lock()
254261
if failure := e.pipelineFailure; failure != nil {
@@ -261,7 +268,7 @@ func (e *Executor) startPipelineCommit(blockNumber int64, changesets []*proto.Na
261268
e.pipelineMu.Unlock()
262269

263270
go func() {
264-
err := e.stateStore.CommitStateChanges(blockNumber, changesets)
271+
err := e.commitState(blockNumber, changesets, pending)
265272
e.pipelineMu.Lock()
266273
e.pipelineErr = err
267274
e.pipelineMu.Unlock()
@@ -270,6 +277,37 @@ func (e *Executor) startPipelineCommit(blockNumber int64, changesets []*proto.Na
270277
return nil
271278
}
272279

280+
// commitState writes the block to the store, handing over changes as well when the store keeps the
281+
// values the block replaced.
282+
func (e *Executor) commitState(blockNumber int64, changesets []*proto.NamedChangeSet, changes *StateChangeSet) error {
283+
if store, ok := e.stateStore.(historyStore); ok {
284+
return store.CommitStateChangesWithPrior(blockNumber, changesets, gigaChangeSet(changes))
285+
}
286+
return e.stateStore.CommitStateChanges(blockNumber, changesets)
287+
}
288+
289+
// TODO: Temporal. At some point both struct will be reused to avoid extra copies and allocs.
290+
func gigaChangeSet(changes *StateChangeSet) *gigatypes.StateChangeSet {
291+
if changes == nil {
292+
return nil
293+
}
294+
return &gigatypes.StateChangeSet{
295+
Balances: convertAll(changes.Balances, func(c BalanceChange) gigatypes.BalanceChange { return gigatypes.BalanceChange(c) }),
296+
Nonces: convertAll(changes.Nonces, func(c NonceChange) gigatypes.NonceChange { return gigatypes.NonceChange(c) }),
297+
Code: convertAll(changes.Code, func(c CodeChange) gigatypes.CodeChange { return gigatypes.CodeChange(c) }),
298+
StorageClears: changes.StorageClears,
299+
Storage: convertAll(changes.Storage, func(c StorageChange) gigatypes.StorageChange { return gigatypes.StorageChange(c) }),
300+
}
301+
}
302+
303+
func convertAll[S, D any](values []S, convert func(S) D) []D {
304+
out := make([]D, len(values))
305+
for i, value := range values {
306+
out[i] = convert(value)
307+
}
308+
return out
309+
}
310+
273311
type gigaSnapshotStateReader struct {
274312
snapshot gigatypes.EVMStateView
275313
missingState StateReader

‎giga/evmonly/giga_store_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ import (
1212
"github.com/stretchr/testify/require"
1313

1414
"github.com/sei-protocol/sei-chain/sei-db/proto"
15+
"github.com/sei-protocol/sei-chain/sei-db/state_db/giga"
1516
gigatypes "github.com/sei-protocol/sei-chain/sei-db/state_db/giga/types"
1617
"github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/lthash"
1718
)
1819

20+
// The giga state DB must take each block's change set, or its undo log would silently go without it.
21+
var _ historyStore = (*giga.StateDB)(nil)
22+
1923
type recordingGigaStore struct {
2024
snapshot gigatypes.StateView
2125
openCount int

‎giga/evmonly/pipeline_overlay.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ func (c *StateChangeSet) clone() *StateChangeSet {
159159
Storage: append([]StorageChange(nil), c.Storage...),
160160
}
161161
for i, change := range c.Balances {
162-
cp.Balances[i] = BalanceChange{Address: change.Address, Balance: cloneBig(change.Balance)}
162+
change.Balance = cloneBig(change.Balance)
163+
change.PriorBalance = cloneBig(change.PriorBalance)
164+
cp.Balances[i] = change
163165
}
164166
for i, change := range c.Code {
165-
cp.Code[i] = CodeChange{
166-
Address: change.Address,
167-
Code: append([]byte(nil), change.Code...),
168-
Delete: change.Delete,
169-
}
167+
change.Code = append([]byte(nil), change.Code...)
168+
change.PriorCode = append([]byte(nil), change.PriorCode...)
169+
cp.Code[i] = change
170170
}
171171
return cp
172172
}

‎giga/evmonly/pipeline_overlay_test.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,19 @@ func TestOverlayReportsDeletedCodeAsAbsent(t *testing.T) {
152152
func TestCloneSurvivesTheBlockResultBeingReused(t *testing.T) {
153153
addr := common.HexToAddress("0x1")
154154
original := &StateChangeSet{
155-
Balances: []BalanceChange{{Address: addr, Balance: big.NewInt(5)}},
156-
Code: []CodeChange{{Address: addr, Code: []byte{0x01}}},
155+
Balances: []BalanceChange{{Address: addr, Balance: big.NewInt(5), PriorBalance: big.NewInt(3)}},
156+
Code: []CodeChange{{Address: addr, Code: []byte{0x01}, PriorCode: []byte{0x02}}},
157157
}
158158
clone := original.clone()
159159

160160
original.Balances[0].Balance.SetInt64(999)
161+
original.Balances[0].PriorBalance.SetInt64(999)
161162
original.Code[0].Code[0] = 0xff
163+
original.Code[0].PriorCode[0] = 0xff
162164
original.Balances = original.Balances[:0]
163165

164166
require.Equal(t, big.NewInt(5), clone.Balances[0].Balance)
167+
require.Equal(t, big.NewInt(3), clone.Balances[0].PriorBalance)
165168
require.Equal(t, []byte{0x01}, clone.Code[0].Code)
169+
require.Equal(t, []byte{0x02}, clone.Code[0].PriorCode)
166170
}

‎sei-db/config/giga_config.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ func AutobahnStorageConfig(homePath string) (*GigaStorageConfig, error) {
8686
return storageConfig, nil
8787
}
8888

89+
// WithStateStoreBackend has the state store keep its history as backend, in that backend's
90+
// directory under the home path.
91+
func (c *GigaStorageConfig) WithStateStoreBackend(backend string) *GigaStorageConfig {
92+
c.SSConfig.Backend = backend
93+
c.SSConfig.EVMDBDirectory = utils.GetEVMStateStorePath(c.HomePath, backend)
94+
return c
95+
}
96+
8997
func (c *GigaStorageConfig) WithAccountDBCacheSize(sizeInBytes uint64) *GigaStorageConfig {
9098
c.FlatKVConfig.AccountStoreConfig.MaxSize = sizeInBytes
9199
return c

0 commit comments

Comments
 (0)