|
| 1 | +package deprecatedmigration |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/NethermindEth/juno/core" |
| 8 | + "github.com/NethermindEth/juno/core/felt" |
| 9 | + "github.com/NethermindEth/juno/db" |
| 10 | + "github.com/NethermindEth/juno/migration/blocktransactions/txlayout" |
| 11 | + "github.com/NethermindEth/juno/utils" |
| 12 | +) |
| 13 | + |
| 14 | +// testStateBackend is a copy of the [statebackend.deprecatedStateBackend] type |
| 15 | +// that stores the state using the old layout. |
| 16 | +// |
| 17 | +// Juno now uses a new layout to store transactions and receipts on the database, |
| 18 | +// but there are some old migrations that use the old layout. These migrations |
| 19 | +// can not be tested using the current state code, since it uses the new layout. |
| 20 | +// [testStateBackend] was created specifically to solve this problem. |
| 21 | +type testStateBackend struct { |
| 22 | + database db.KeyValueStore |
| 23 | + network *utils.Network |
| 24 | +} |
| 25 | + |
| 26 | +// NewTestState creates a new test state that stores the data using the old layout. |
| 27 | +func NewTestState( |
| 28 | + database db.KeyValueStore, |
| 29 | + network *utils.Network, |
| 30 | +) *testStateBackend { |
| 31 | + return &testStateBackend{ |
| 32 | + database: database, |
| 33 | + network: network, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (b *testStateBackend) Store( |
| 38 | + block *core.Block, |
| 39 | + blockCommitments *core.BlockCommitments, |
| 40 | + stateUpdate *core.StateUpdate, |
| 41 | + newClasses map[felt.Felt]core.ClassDefinition, |
| 42 | +) error { |
| 43 | + //nolint:staticcheck,nolintlint // used by old state |
| 44 | + err := b.database.Update(func(txn db.IndexedBatch) error { |
| 45 | + if err := verifyBlockSuccession(txn, block); err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + err := core.NewDeprecatedState(txn).Update(block.Header, stateUpdate, newClasses, false) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + return writeBlockContent( |
| 54 | + txn, |
| 55 | + block, |
| 56 | + stateUpdate, |
| 57 | + blockCommitments, |
| 58 | + newClasses, |
| 59 | + ) |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +func verifyBlockSuccession(reader db.KeyValueReader, block *core.Block) error { |
| 69 | + ErrParentDoesNotMatchHead := errors.New("block's parent hash does not match head block hash") |
| 70 | + if err := core.CheckBlockVersion(block.ProtocolVersion); err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + expectedBlockNumber := uint64(0) |
| 75 | + expectedParentHash := &felt.Zero |
| 76 | + |
| 77 | + h, err := headsHeader(reader) |
| 78 | + if err == nil { |
| 79 | + expectedBlockNumber = h.Number + 1 |
| 80 | + expectedParentHash = h.Hash |
| 81 | + } else if !errors.Is(err, db.ErrKeyNotFound) { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + if expectedBlockNumber != block.Number { |
| 86 | + return fmt.Errorf("expected block #%d, got block #%d", expectedBlockNumber, block.Number) |
| 87 | + } |
| 88 | + if !block.ParentHash.Equal(expectedParentHash) { |
| 89 | + return ErrParentDoesNotMatchHead |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func headsHeader(reader db.KeyValueReader) (*core.Header, error) { |
| 96 | + height, err := core.GetChainHeight(reader) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + return core.GetBlockHeaderByNumber(reader, height) |
| 101 | +} |
| 102 | + |
| 103 | +func writeBlockContent( |
| 104 | + writer db.Batch, |
| 105 | + block *core.Block, |
| 106 | + stateUpdate *core.StateUpdate, |
| 107 | + commitments *core.BlockCommitments, |
| 108 | + newClasses map[felt.Felt]core.ClassDefinition, |
| 109 | +) error { |
| 110 | + if err := core.WriteBlockHeader(writer, block.Header); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + if err := txlayout.TransactionLayoutPerTx.WriteTransactionsAndReceipts( |
| 115 | + writer, |
| 116 | + block.Number, |
| 117 | + block.Transactions, |
| 118 | + block.Receipts, |
| 119 | + ); err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + if err := core.WriteStateUpdateByBlockNum(writer, block.Number, stateUpdate); err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + if err := core.WriteBlockCommitment(writer, block.Number, commitments); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + if err := core.WriteL1HandlerMsgHashes(writer, block.Transactions); err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + // we use v1 directly since the tests that use this function are using |
| 136 | + // protocol < 0.14.1 blocks |
| 137 | + if err := storeCasmHashMetadataV1( |
| 138 | + writer, |
| 139 | + block.Number, |
| 140 | + stateUpdate, |
| 141 | + newClasses, |
| 142 | + ); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + return core.WriteChainHeight(writer, block.Number) |
| 147 | +} |
| 148 | + |
| 149 | +func storeCasmHashMetadataV1( |
| 150 | + writer db.KeyValueWriter, |
| 151 | + blockNumber uint64, |
| 152 | + stateUpdate *core.StateUpdate, |
| 153 | + newClasses map[felt.Felt]core.ClassDefinition, |
| 154 | +) error { |
| 155 | + for sierraClassHash, casmHash := range stateUpdate.StateDiff.DeclaredV1Classes { |
| 156 | + casmHashV1 := (*felt.CasmClassHash)(casmHash) |
| 157 | + |
| 158 | + classDef, ok := newClasses[sierraClassHash] |
| 159 | + if !ok { |
| 160 | + return fmt.Errorf("class %s not available in newClasses at block %d", |
| 161 | + sierraClassHash.String(), |
| 162 | + blockNumber, |
| 163 | + ) |
| 164 | + } |
| 165 | + |
| 166 | + sierraClass, ok := classDef.(*core.SierraClass) |
| 167 | + if !ok { |
| 168 | + return fmt.Errorf("class %s must be a SierraClass at block %d", |
| 169 | + sierraClassHash.String(), |
| 170 | + blockNumber, |
| 171 | + ) |
| 172 | + } |
| 173 | + |
| 174 | + v2Hash := sierraClass.Compiled.Hash(core.HashVersionV2) |
| 175 | + casmHashV2 := felt.CasmClassHash(v2Hash) |
| 176 | + |
| 177 | + metadata := core.NewCasmHashMetadataDeclaredV1(blockNumber, casmHashV1, &casmHashV2) |
| 178 | + err := core.WriteClassCasmHashMetadata( |
| 179 | + writer, |
| 180 | + (*felt.SierraClassHash)(&sierraClassHash), |
| 181 | + &metadata, |
| 182 | + ) |
| 183 | + if err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + } |
| 187 | + return nil |
| 188 | +} |
0 commit comments