Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement logic to build genesis state for the sequencer #2371

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions adapters/vm2core/vm2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"slices"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/vm"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -39,3 +40,52 @@
})
return utils.Map(events, AdaptOrderedEvent)
}

func AdaptStateDiff(trace *vm.TransactionTrace) *core.StateDiff {
rianhughes marked this conversation as resolved.
Show resolved Hide resolved
if trace.StateDiff == nil {
return nil
}

Check warning on line 47 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L46-L47

Added lines #L46 - L47 were not covered by tests
stateDiff := trace.StateDiff
newStorageDiffs := make(map[felt.Felt]map[felt.Felt]*felt.Felt)
rianhughes marked this conversation as resolved.
Show resolved Hide resolved
for _, sd := range stateDiff.StorageDiffs {
entries := make(map[felt.Felt]*felt.Felt)
for _, entry := range sd.StorageEntries {
val := entry.Value
entries[entry.Key] = &val
}
newStorageDiffs[sd.Address] = entries

Check warning on line 56 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L51-L56

Added lines #L51 - L56 were not covered by tests
}

newNonces := make(map[felt.Felt]*felt.Felt)
for _, nonce := range stateDiff.Nonces {
nonc := nonce.Nonce
newNonces[nonce.ContractAddress] = &nonc
}

newDeployedContracts := make(map[felt.Felt]*felt.Felt)
for _, dc := range stateDiff.DeployedContracts {
ch := dc.ClassHash
newDeployedContracts[dc.Address] = &ch
}

newDeclaredV1Classes := make(map[felt.Felt]*felt.Felt)
for _, dc := range stateDiff.DeclaredClasses {
cch := dc.CompiledClassHash
newDeclaredV1Classes[dc.ClassHash] = &cch
}

Check warning on line 75 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L73-L75

Added lines #L73 - L75 were not covered by tests

newReplacedClasses := make(map[felt.Felt]*felt.Felt)
for _, rc := range stateDiff.ReplacedClasses {
ch := rc.ClassHash
newReplacedClasses[rc.ContractAddress] = &ch
}

Check warning on line 81 in adapters/vm2core/vm2core.go

View check run for this annotation

Codecov / codecov/patch

adapters/vm2core/vm2core.go#L79-L81

Added lines #L79 - L81 were not covered by tests

return &core.StateDiff{
StorageDiffs: newStorageDiffs,
Nonces: newNonces,
DeployedContracts: newDeployedContracts,
DeclaredV0Classes: stateDiff.DeprecatedDeclaredClasses,
DeclaredV1Classes: newDeclaredV1Classes,
ReplacedClasses: newReplacedClasses,
}
}
38 changes: 38 additions & 0 deletions core/state_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,41 @@
digest.Update(&addr, nonces[addr])
}
}

func EmptyStateDiff() *StateDiff {
return &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: make([]*felt.Felt, 0),
DeclaredV1Classes: make(map[felt.Felt]*felt.Felt),
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
}

Check warning on line 227 in core/state_update.go

View check run for this annotation

Codecov / codecov/patch

core/state_update.go#L223-L227

Added lines #L223 - L227 were not covered by tests
}
}

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
}
rianhughes marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading