|
5 | 5 | "slices" |
6 | 6 |
|
7 | 7 | "github.com/NethermindEth/juno/core" |
| 8 | + "github.com/NethermindEth/juno/core/felt" |
8 | 9 | "github.com/NethermindEth/juno/utils" |
9 | 10 | "github.com/NethermindEth/juno/vm" |
10 | 11 | "github.com/ethereum/go-ethereum/common" |
@@ -39,3 +40,50 @@ func AdaptOrderedEvents(events []vm.OrderedEvent) []*core.Event { |
39 | 40 | }) |
40 | 41 | return utils.Map(events, AdaptOrderedEvent) |
41 | 42 | } |
| 43 | + |
| 44 | +func AdaptStateDiff(fromStateDiff *vm.StateDiff) core.StateDiff { |
| 45 | + var toStateDiff core.StateDiff |
| 46 | + if fromStateDiff == nil { |
| 47 | + return toStateDiff |
| 48 | + } |
| 49 | + |
| 50 | + // Preallocate all maps with known sizes from fromStateDiff |
| 51 | + toStateDiff = core.StateDiff{ |
| 52 | + StorageDiffs: make(map[felt.Felt]map[felt.Felt]*felt.Felt, len(fromStateDiff.StorageDiffs)), |
| 53 | + Nonces: make(map[felt.Felt]*felt.Felt, len(fromStateDiff.Nonces)), |
| 54 | + DeployedContracts: make(map[felt.Felt]*felt.Felt, len(fromStateDiff.DeployedContracts)), |
| 55 | + DeclaredV0Classes: make([]*felt.Felt, len(fromStateDiff.DeprecatedDeclaredClasses)), |
| 56 | + DeclaredV1Classes: make(map[felt.Felt]*felt.Felt, len(fromStateDiff.DeclaredClasses)), |
| 57 | + ReplacedClasses: make(map[felt.Felt]*felt.Felt, len(fromStateDiff.ReplacedClasses)), |
| 58 | + } |
| 59 | + |
| 60 | + for _, sd := range fromStateDiff.StorageDiffs { |
| 61 | + entries := make(map[felt.Felt]*felt.Felt, len(sd.StorageEntries)) |
| 62 | + for _, entry := range sd.StorageEntries { |
| 63 | + val := entry.Value |
| 64 | + entries[entry.Key] = &val |
| 65 | + } |
| 66 | + toStateDiff.StorageDiffs[sd.Address] = entries |
| 67 | + } |
| 68 | + |
| 69 | + for _, nonce := range fromStateDiff.Nonces { |
| 70 | + newNonce := nonce.Nonce |
| 71 | + toStateDiff.Nonces[nonce.ContractAddress] = &newNonce |
| 72 | + } |
| 73 | + |
| 74 | + for _, dc := range fromStateDiff.DeployedContracts { |
| 75 | + ch := dc.ClassHash |
| 76 | + toStateDiff.DeployedContracts[dc.Address] = &ch |
| 77 | + } |
| 78 | + |
| 79 | + for _, dc := range fromStateDiff.DeclaredClasses { |
| 80 | + cch := dc.CompiledClassHash |
| 81 | + toStateDiff.DeclaredV1Classes[dc.ClassHash] = &cch |
| 82 | + } |
| 83 | + |
| 84 | + for _, rc := range fromStateDiff.ReplacedClasses { |
| 85 | + ch := rc.ClassHash |
| 86 | + toStateDiff.ReplacedClasses[rc.ContractAddress] = &ch |
| 87 | + } |
| 88 | + return toStateDiff |
| 89 | +} |
0 commit comments