From fbae35cd77cbbf8469fda2d19528cac166b915fe Mon Sep 17 00:00:00 2001 From: rian Date: Tue, 14 Jan 2025 13:43:25 +0200 Subject: [PATCH] comments: merge method, const string --- core/state_update.go | 45 ++++++++++++++++++-------------------------- genesis/genesis.go | 12 +++++++++--- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/core/state_update.go b/core/state_update.go index 41c344a9b..fd8deaf84 100644 --- a/core/state_update.go +++ b/core/state_update.go @@ -39,6 +39,24 @@ func (d *StateDiff) Length() uint64 { return uint64(length) } +func (d *StateDiff) Merge(incoming *StateDiff) { + mergeStorageDiffs := func(oldMap, newMap map[felt.Felt]map[felt.Felt]*felt.Felt) { + for addr, newAddrStorage := range newMap { + if oldAddrStorage, exists := oldMap[addr]; exists { + maps.Copy(oldAddrStorage, newAddrStorage) + } else { + oldMap[addr] = newAddrStorage + } + } + } + maps.Copy(d.Nonces, incoming.Nonces) + maps.Copy(d.DeployedContracts, incoming.DeployedContracts) + maps.Copy(d.DeclaredV1Classes, incoming.DeclaredV1Classes) + maps.Copy(d.ReplacedClasses, incoming.ReplacedClasses) + mergeStorageDiffs(d.StorageDiffs, incoming.StorageDiffs) + d.DeclaredV0Classes = append(d.DeclaredV0Classes, incoming.DeclaredV0Classes...) +} + var starknetStateDiff0 = new(felt.Felt).SetBytes([]byte("STARKNET_STATE_DIFF0")) func (d *StateDiff) Hash() *felt.Felt { @@ -210,30 +228,3 @@ func EmptyStateDiff() *StateDiff { ReplacedClasses: make(map[felt.Felt]*felt.Felt), } } - -func MergeStateDiffs(oldStateDiff, newStateDiff *StateDiff) *StateDiff { - mergeMaps := func(oldMap, newMap map[felt.Felt]*felt.Felt) { - for key, value := range newMap { - oldMap[key] = value - } - } - - mergeStorageDiffs := func(oldMap, newMap map[felt.Felt]map[felt.Felt]*felt.Felt) { - for addr, newAddrStorage := range newMap { - if oldAddrStorage, exists := oldMap[addr]; exists { - mergeMaps(oldAddrStorage, newAddrStorage) - } else { - oldMap[addr] = newAddrStorage - } - } - } - - mergeStorageDiffs(oldStateDiff.StorageDiffs, newStateDiff.StorageDiffs) - mergeMaps(oldStateDiff.Nonces, newStateDiff.Nonces) - mergeMaps(oldStateDiff.DeployedContracts, newStateDiff.DeployedContracts) - mergeMaps(oldStateDiff.DeclaredV1Classes, newStateDiff.DeclaredV1Classes) - mergeMaps(oldStateDiff.ReplacedClasses, newStateDiff.ReplacedClasses) - oldStateDiff.DeclaredV0Classes = append(oldStateDiff.DeclaredV0Classes, newStateDiff.DeclaredV0Classes...) - - return oldStateDiff -} diff --git a/genesis/genesis.go b/genesis/genesis.go index 95ae567ac..2f535fca2 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -19,6 +19,9 @@ import ( "github.com/NethermindEth/juno/vm" ) +// The constructor entrypoint for cairo contracts +const CONSTRUCTOR_SELECTOR = "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194" + type GenesisConfig struct { Classes []string `json:"classes"` // []path-to-class.json Contracts map[felt.Felt]GenesisContractData `json:"contracts"` // address -> {classHash, constructorArgs} @@ -122,7 +125,7 @@ func GenesisStateDiff( //nolint:funlen,gocyclo } } - constructorSelector, err := new(felt.Felt).SetString("0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194") + constructorSelector, err := new(felt.Felt).SetString(CONSTRUCTOR_SELECTOR) if err != nil { return nil, nil, fmt.Errorf("convert string to felt: %v", err) } @@ -210,8 +213,11 @@ func GenesisStateDiff( //nolint:funlen,gocyclo traceSD := vm2core.AdaptStateDiff(&trace[0]) genesisSD, _ := genesisState.StateDiffAndClasses() - mergedSD := core.MergeStateDiffs(genesisSD, traceSD) - genesisState.SetStateDiff(mergedSD) + fmt.Println("==") + fmt.Println(genesisSD) + fmt.Println(traceSD) + genesisSD.Merge(traceSD) + genesisState.SetStateDiff(genesisSD) } genesisStateDiff, genesisClasses := genesisState.StateDiffAndClasses() return genesisStateDiff, genesisClasses, nil