|
| 1 | +package headstate |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "iter" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/NethermindEth/juno/blockchain/networks" |
| 11 | + "github.com/NethermindEth/juno/core/felt" |
| 12 | + "github.com/NethermindEth/juno/db" |
| 13 | + "github.com/NethermindEth/juno/db/dbutils" |
| 14 | + "github.com/NethermindEth/juno/migration" |
| 15 | + "github.com/NethermindEth/juno/migration/pipeline" |
| 16 | + "github.com/NethermindEth/juno/migration/semaphore" |
| 17 | + "github.com/NethermindEth/juno/utils/log" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + batchByteSize = 128 * db.Megabyte |
| 22 | + targetBatchByteSize = 96 * db.Megabyte |
| 23 | + ingestorCount = 4 |
| 24 | + timeLogRate = 5 * time.Second |
| 25 | +) |
| 26 | + |
| 27 | +type task struct { |
| 28 | + batch db.Batch |
| 29 | + completedAddrs int |
| 30 | +} |
| 31 | + |
| 32 | +var ( |
| 33 | + shouldRerun = []byte{} |
| 34 | + shouldNotRerun = []byte(nil) |
| 35 | +) |
| 36 | + |
| 37 | +var _ migration.Migration = (*Migrator)(nil) |
| 38 | + |
| 39 | +// Migrator consolidates the deprecated per-field contract layout into a |
| 40 | +// single Contract record per address, written via state.WriteContract: |
| 41 | +// |
| 42 | +// ContractClassHash[addr] |
| 43 | +// ContractNonce[addr] |
| 44 | +// ContractDeploymentHeight[addr] |
| 45 | +// │ |
| 46 | +// ▼ |
| 47 | +// Contract[addr] = { ClassHash, Nonce, DeployedHeight } |
| 48 | +// |
| 49 | +// StorageRoot is left zero — the running node lazily backfills it on the |
| 50 | +// contract's first storage write. |
| 51 | +// |
| 52 | +// Each address discovered in the ContractClassHash bucket is processed by one |
| 53 | +// of ingestorCount worker goroutines that read the three old fields into a |
| 54 | +// shared db.Batch; a single committer drains batches to disk. Once every |
| 55 | +// address has been migrated, the three deprecated buckets are wiped via |
| 56 | +// DeleteRange. |
| 57 | +// |
| 58 | +// Re-run safe: an address whose Contract record already exists is skipped |
| 59 | +// (via state.HasContract), and the trailing wipe re-issues DeleteRange over |
| 60 | +// the (possibly already empty) ranges. |
| 61 | +type Migrator struct{} |
| 62 | + |
| 63 | +func (Migrator) Before([]byte) error { |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func (Migrator) Migrate( |
| 68 | + ctx context.Context, |
| 69 | + database db.KeyValueStore, |
| 70 | + _ *networks.Network, |
| 71 | + logger log.StructuredLogger, |
| 72 | +) ([]byte, error) { |
| 73 | + addressesIter, sourceErr := pendingAddresses(database) |
| 74 | + res := migrateAddresses(ctx, database, logger, addressesIter) |
| 75 | + |
| 76 | + if err := errors.Join(sourceErr(), res.Err); err != nil { |
| 77 | + return shouldRerun, err |
| 78 | + } |
| 79 | + if !res.IsDone { |
| 80 | + if ctxErr := ctx.Err(); ctxErr != nil { |
| 81 | + return shouldRerun, ctxErr |
| 82 | + } |
| 83 | + return shouldRerun, errors.New("headstate migration did not complete") |
| 84 | + } |
| 85 | + |
| 86 | + return shouldNotRerun, wipeDeprecatedBuckets(database) |
| 87 | +} |
| 88 | + |
| 89 | +func migrateAddresses( |
| 90 | + ctx context.Context, |
| 91 | + database db.KeyValueStore, |
| 92 | + logger log.StructuredLogger, |
| 93 | + addresses iter.Seq[felt.Address], |
| 94 | +) pipeline.Result { |
| 95 | + batchSemaphore := semaphore.New( |
| 96 | + ingestorCount+1, |
| 97 | + func() db.Batch { |
| 98 | + return database.NewBatchWithSize(batchByteSize) |
| 99 | + }, |
| 100 | + ) |
| 101 | + |
| 102 | + source := pipeline.Source(addresses) |
| 103 | + |
| 104 | + ingestorPipeline := pipeline.New( |
| 105 | + source, |
| 106 | + ingestorCount, |
| 107 | + newIngestor(database, batchSemaphore), |
| 108 | + ) |
| 109 | + |
| 110 | + committerPipeline := pipeline.New( |
| 111 | + ingestorPipeline, |
| 112 | + 1, |
| 113 | + newCommitter(logger, batchSemaphore), |
| 114 | + ) |
| 115 | + |
| 116 | + _, wait := committerPipeline.Run(ctx) |
| 117 | + return wait() |
| 118 | +} |
| 119 | + |
| 120 | +func pendingAddresses(r db.KeyValueReader) (iter.Seq[felt.Address], func() error) { |
| 121 | + var iterErr error |
| 122 | + seq := func(yield func(felt.Address) bool) { |
| 123 | + prefix := db.ContractClassHash.Key() |
| 124 | + it, err := r.NewIterator(prefix, true) |
| 125 | + if err != nil { |
| 126 | + iterErr = err |
| 127 | + return |
| 128 | + } |
| 129 | + defer it.Close() |
| 130 | + |
| 131 | + for valid := it.First(); valid; valid = it.Next() { |
| 132 | + key := it.Key() |
| 133 | + if len(key) != len(prefix)+felt.Bytes { |
| 134 | + iterErr = fmt.Errorf( |
| 135 | + "malformed ContractClassHash key: len %d, want %d", |
| 136 | + len(key), |
| 137 | + len(prefix)+felt.Bytes, |
| 138 | + ) |
| 139 | + return |
| 140 | + } |
| 141 | + f := felt.FromBytes[felt.Felt](key[len(prefix):]) |
| 142 | + if !yield(felt.Address(f)) { |
| 143 | + return |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + return seq, func() error { return iterErr } |
| 148 | +} |
| 149 | + |
| 150 | +func wipeDeprecatedBuckets(database db.KeyValueStore) error { |
| 151 | + for _, bucket := range []db.Bucket{ |
| 152 | + db.ContractClassHash, |
| 153 | + db.ContractNonce, |
| 154 | + db.ContractDeploymentHeight, |
| 155 | + } { |
| 156 | + start := bucket.Key() |
| 157 | + end := dbutils.UpperBound(start) |
| 158 | + if err := database.DeleteRange(start, end); err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + } |
| 162 | + return nil |
| 163 | +} |
0 commit comments