From fb11e075b24ceaf1878bed3ffd7f61acb46dd6b1 Mon Sep 17 00:00:00 2001 From: rian Date: Mon, 13 Jan 2025 16:32:21 +0200 Subject: [PATCH] fix stuff --- adapters/vm2core/vm2core.go | 66 ++++++++++++++++++++++--------------- genesis/genesis.go | 2 +- genesis/genesis_test.go | 20 ----------- vm/vm_test.go | 6 ++++ 4 files changed, 47 insertions(+), 47 deletions(-) diff --git a/adapters/vm2core/vm2core.go b/adapters/vm2core/vm2core.go index 66f9625d00..94fa6e1a90 100644 --- a/adapters/vm2core/vm2core.go +++ b/adapters/vm2core/vm2core.go @@ -41,37 +41,51 @@ func AdaptOrderedEvents(events []vm.OrderedEvent) []*core.Event { return utils.Map(events, AdaptOrderedEvent) } -func AdaptStateDiff(sd *vm.StateDiff) *core.StateDiff { - result := core.StateDiff{ - StorageDiffs: make(map[felt.Felt]map[felt.Felt]*felt.Felt), - Nonces: make(map[felt.Felt]*felt.Felt), - DeployedContracts: make(map[felt.Felt]*felt.Felt), - DeclaredV0Classes: []*felt.Felt{}, - DeclaredV1Classes: make(map[felt.Felt]*felt.Felt), - ReplacedClasses: make(map[felt.Felt]*felt.Felt), +func AdaptStateDiff(trace *vm.TransactionTrace) *core.StateDiff { + if trace.StateDiff == nil { + return nil } - if sd == nil { - return &result - } - for _, entries := range sd.StorageDiffs { - KeyVals := map[felt.Felt]*felt.Felt{} - for _, entry := range entries.StorageEntries { - KeyVals[entry.Key] = &entry.Value + stateDiff := trace.StateDiff + newStorageDiffs := make(map[felt.Felt]map[felt.Felt]*felt.Felt) + for _, sd := range stateDiff.StorageDiffs { + entries := make(map[felt.Felt]*felt.Felt) + for _, entry := range sd.StorageEntries { + val := entry.Value + entries[entry.Key] = &val } - result.StorageDiffs[entries.Address] = KeyVals + newStorageDiffs[sd.Address] = entries } - for _, addrNonce := range sd.Nonces { - result.Nonces[addrNonce.ContractAddress] = &addrNonce.Nonce + + newNonces := make(map[felt.Felt]*felt.Felt) + for _, nonce := range stateDiff.Nonces { + nonc := nonce.Nonce + newNonces[nonce.ContractAddress] = &nonc } - for _, addrClassHash := range sd.DeployedContracts { - result.Nonces[addrClassHash.Address] = &addrClassHash.ClassHash + + newDeployedContracts := make(map[felt.Felt]*felt.Felt) + for _, dc := range stateDiff.DeployedContracts { + ch := dc.ClassHash + newDeployedContracts[dc.Address] = &ch } - for _, hashes := range sd.DeclaredClasses { - result.DeclaredV1Classes[hashes.ClassHash] = &hashes.CompiledClassHash + + newDeclaredV1Classes := make(map[felt.Felt]*felt.Felt) + for _, dc := range stateDiff.DeclaredClasses { + cch := dc.CompiledClassHash + newDeclaredV1Classes[dc.ClassHash] = &cch } - for _, addrClassHash := range sd.ReplacedClasses { - result.ReplacedClasses[addrClassHash.ClassHash] = &addrClassHash.ClassHash + + newReplacedClasses := make(map[felt.Felt]*felt.Felt) + for _, rc := range stateDiff.ReplacedClasses { + ch := rc.ClassHash + newReplacedClasses[rc.ContractAddress] = &ch + } + + return &core.StateDiff{ + StorageDiffs: newStorageDiffs, + Nonces: newNonces, + DeployedContracts: newDeployedContracts, + DeclaredV0Classes: stateDiff.DeprecatedDeclaredClasses, + DeclaredV1Classes: newDeclaredV1Classes, + ReplacedClasses: newReplacedClasses, } - result.DeclaredV0Classes = append(result.DeclaredV0Classes, sd.DeprecatedDeclaredClasses...) - return &result } diff --git a/genesis/genesis.go b/genesis/genesis.go index 6cfe149193..95ae567acc 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -208,7 +208,7 @@ func GenesisStateDiff( //nolint:funlen,gocyclo return nil, nil, fmt.Errorf("execute function call: %v", err) } - traceSD := vm2core.AdaptStateDiff(trace[0].StateDiff) + traceSD := vm2core.AdaptStateDiff(&trace[0]) genesisSD, _ := genesisState.StateDiffAndClasses() mergedSD := core.MergeStateDiffs(genesisSD, traceSD) genesisState.SetStateDiff(mergedSD) diff --git a/genesis/genesis_test.go b/genesis/genesis_test.go index c30d908739..4ca1a7b1c7 100644 --- a/genesis/genesis_test.go +++ b/genesis/genesis_test.go @@ -10,28 +10,8 @@ import ( ) func TestGenesisStateDiff(t *testing.T) { - // testDB := pebble.NewMemTest(t) - // mockCtrl := gomock.NewController(t) network := &utils.Mainnet - // client := feeder.NewTestClient(t, network) - // gw := adaptfeeder.New(client) log := utils.NewNopZapLogger() - // chain := blockchain.New(pebble.NewMemTest(t), network, nil) - // mockVM := mocks.NewMockVM(mockCtrl) - // privKey, err := ecdsa.GenerateKey(rand.Reader) - // require.NoError(t, err) - // p := mempool.New(pebble.NewMemTest(t)) - // testBuilder := builder.New(privKey, new(felt.Felt).SetUint64(1), chain, mockVM, time.Millisecond, p, utils.NewNopZapLogger(), false, testDB) - // // Need to store pending block create NewPendingState - // block, err := gw.BlockByNumber(context.Background(), 0) - // require.NoError(t, err) - // su, err := gw.StateUpdate(context.Background(), 0) - // require.NoError(t, err) - // pendingGenesis := sync.Pending{ - // Block: block, - // StateUpdate: su, - // } - // require.NoError(t, testBuilder.StorePending(&pendingGenesis)) t.Run("empty genesis config", func(t *testing.T) { genesisConfig := genesis.GenesisConfig{} diff --git a/vm/vm_test.go b/vm/vm_test.go index 2f54ec2e25..100da65d0d 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -93,6 +93,8 @@ func TestV1Call(t *testing.T) { testState := core.NewState(txn) require.NoError(t, testState.Update(0, &core.StateUpdate{ + OldRoot: &felt.Zero, + NewRoot: utils.HexToFelt(t, "0x2650cef46c190ec6bb7dc21a5a36781132e7c883b27175e625031149d4f1a84"), StateDiff: &core.StateDiff{ DeployedContracts: map[felt.Felt]*felt.Felt{ *contractAddr: classHash, @@ -119,6 +121,8 @@ func TestV1Call(t *testing.T) { assert.Equal(t, []*felt.Felt{&felt.Zero}, ret) require.NoError(t, testState.Update(1, &core.StateUpdate{ + OldRoot: utils.HexToFelt(t, "0x2650cef46c190ec6bb7dc21a5a36781132e7c883b27175e625031149d4f1a84"), + NewRoot: utils.HexToFelt(t, "0x7a9da0a7471a8d5118d3eefb8c26a6acbe204eb1eaa934606f4757a595fe552"), StateDiff: &core.StateDiff{ StorageDiffs: map[felt.Felt]map[felt.Felt]*felt.Felt{ *contractAddr: { @@ -157,6 +161,8 @@ func TestCall_MaxSteps(t *testing.T) { testState := core.NewState(txn) require.NoError(t, testState.Update(0, &core.StateUpdate{ + OldRoot: &felt.Zero, + NewRoot: utils.HexToFelt(t, "0x3d452fbb3c3a32fe85b1a3fbbcdec316d5fc940cefc028ee808ad25a15991c8"), StateDiff: &core.StateDiff{ DeployedContracts: map[felt.Felt]*felt.Felt{ *contractAddr: classHash,