|
| 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 | +} |
0 commit comments