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 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I can see we only use trace.StateDiff in this function, why do we pass trace as a parameter?

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could reduce memory allocation this way

Suggested change
newStorageDiffs := make(map[felt.Felt]map[felt.Felt]*felt.Felt)
newStorageDiffs := make(map[felt.Felt]map[felt.Felt]*felt.Felt, len(stateDiff.StorageDiffs))

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,
}
}
29 changes: 29 additions & 0 deletions core/state_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
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
}
}
Comment on lines +43 to +51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just inline this function?

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 @@ -199,3 +217,14 @@
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),
}
}
Loading
Loading