Skip to content

Commit 9fd197b

Browse files
committed
state migration phases under one migration
1 parent 958d81f commit 9fd197b

2 files changed

Lines changed: 96 additions & 5 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package newstate
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/NethermindEth/juno/blockchain/networks"
8+
"github.com/NethermindEth/juno/db"
9+
"github.com/NethermindEth/juno/migration"
10+
"github.com/NethermindEth/juno/migration/state/headstate"
11+
"github.com/NethermindEth/juno/migration/state/history"
12+
"github.com/NethermindEth/juno/utils/log"
13+
"go.uber.org/zap"
14+
)
15+
16+
var _ migration.Migration = (*Migrator)(nil)
17+
18+
type phase struct {
19+
name string
20+
m migration.Migration
21+
}
22+
23+
// Migrator applies new state migrations as one unit.
24+
//
25+
// Intermediate state is [phase index][that phase's own state].
26+
type Migrator struct {
27+
phases []phase
28+
phase uint8
29+
subState []byte
30+
}
31+
32+
// New returns a migrator over the phases in the only order they may run
33+
func New() *Migrator {
34+
return &Migrator{phases: []phase{
35+
{name: "headstate", m: &headstate.Migrator{}},
36+
{name: "history", m: &history.Migrator{}},
37+
}}
38+
}
39+
40+
// Before restores the phase to resume from and that phase's own state.
41+
func (m *Migrator) Before(state []byte) error {
42+
if len(state) == 0 {
43+
m.phase, m.subState = 0, nil
44+
return nil
45+
}
46+
if int(state[0]) >= len(m.phases) {
47+
return fmt.Errorf(
48+
"newstate: intermediate state names phase %d, but only %d phases are registered",
49+
state[0], len(m.phases),
50+
)
51+
}
52+
m.phase, m.subState = state[0], state[1:]
53+
return nil
54+
}
55+
56+
// Migrate runs the phases in order, starting from the one Before restored.
57+
func (m *Migrator) Migrate(
58+
ctx context.Context,
59+
database db.KeyValueStore,
60+
network *networks.Network,
61+
logger log.StructuredLogger,
62+
) ([]byte, error) {
63+
for i := int(m.phase); i < len(m.phases); i++ {
64+
p := m.phases[i]
65+
66+
var sub []byte
67+
if i == int(m.phase) {
68+
sub = m.subState
69+
}
70+
if err := p.m.Before(sub); err != nil {
71+
return checkpoint(i, sub), fmt.Errorf("newstate: %s: restoring state: %w", p.name, err)
72+
}
73+
74+
logger.Info("Applying new state migration phase",
75+
zap.String("phase", p.name),
76+
zap.String("progress", fmt.Sprintf("%d/%d", i+1, len(m.phases))),
77+
)
78+
79+
next, err := p.m.Migrate(ctx, database, network, logger)
80+
if err != nil {
81+
return checkpoint(i, next), fmt.Errorf("newstate: %s: %w", p.name, err)
82+
}
83+
if next != nil {
84+
return checkpoint(i, next), nil
85+
}
86+
}
87+
88+
return nil, nil
89+
}
90+
91+
func checkpoint(i int, sub []byte) []byte {
92+
return append([]byte{uint8(i)}, sub...)
93+
}

node/migration.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import (
1313
"github.com/NethermindEth/juno/migration/blocktransactions"
1414
"github.com/NethermindEth/juno/migration/deprecated" //nolint:staticcheck,nolintlint,lll // ignore statick check package will be removed in future, nolinlint because main config does not check
1515
"github.com/NethermindEth/juno/migration/historyprunner"
16-
"github.com/NethermindEth/juno/migration/state/headstate"
17-
"github.com/NethermindEth/juno/migration/state/history"
16+
"github.com/NethermindEth/juno/migration/state/newstate"
1817
"github.com/NethermindEth/juno/migration/statedifflength"
1918
"github.com/NethermindEth/juno/utils/log"
2019
)
@@ -30,9 +29,8 @@ func registerMigrations(cfg *Config) *migration.Registry {
3029
cfg.Prune,
3130
PruneModeFlag,
3231
).
33-
WithOptional(&headstate.Migrator{}, cfg.NewState, "new-state").
34-
With(&statedifflength.Migrator{}).
35-
WithOptional(&history.Migrator{}, cfg.NewState, "new-state")
32+
WithOptional(newstate.New(), cfg.NewState, "new-state").
33+
With(&statedifflength.Migrator{})
3634

3735
return registry
3836
}

0 commit comments

Comments
 (0)