Skip to content

Commit

Permalink
fix stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Jan 13, 2025
1 parent a0ee00e commit fb11e07
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
66 changes: 40 additions & 26 deletions adapters/vm2core/vm2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 0 additions & 20 deletions genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
6 changes: 6 additions & 0 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fb11e07

Please sign in to comment.