Skip to content

Commit

Permalink
comments: merge method, const string
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Jan 14, 2025
1 parent 459db5d commit fbae35c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
45 changes: 18 additions & 27 deletions core/state_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Check warning on line 49 in core/state_update.go

View check run for this annotation

Codecov / codecov/patch

core/state_update.go#L45-L49

Added lines #L45 - L49 were not covered by tests
}
}
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 {
Expand Down Expand Up @@ -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
}
12 changes: 9 additions & 3 deletions genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"github.com/NethermindEth/juno/vm"
)

// The constructor entrypoint for cairo contracts
const CONSTRUCTOR_SELECTOR = "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194"

Check failure on line 23 in genesis/genesis.go

View workflow job for this annotation

GitHub Actions / lint

ST1003: should not use ALL_CAPS in Go names; use CamelCase instead (stylecheck)

type GenesisConfig struct {
Classes []string `json:"classes"` // []path-to-class.json
Contracts map[felt.Felt]GenesisContractData `json:"contracts"` // address -> {classHash, constructorArgs}
Expand Down Expand Up @@ -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)
}

Check warning on line 131 in genesis/genesis.go

View check run for this annotation

Codecov / codecov/patch

genesis/genesis.go#L130-L131

Added lines #L130 - L131 were not covered by tests
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fbae35c

Please sign in to comment.