-
Notifications
You must be signed in to change notification settings - Fork 185
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
rianhughes
wants to merge
3
commits into
main
Choose a base branch
from
feature/sequencer-genesis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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" | ||||||
|
@@ -39,3 +40,52 @@ | |||||
}) | ||||||
return utils.Map(events, AdaptOrderedEvent) | ||||||
} | ||||||
|
||||||
func AdaptStateDiff(trace *vm.TransactionTrace) *core.StateDiff { | ||||||
if trace.StateDiff == nil { | ||||||
return nil | ||||||
} | ||||||
stateDiff := trace.StateDiff | ||||||
newStorageDiffs := make(map[felt.Felt]map[felt.Felt]*felt.Felt) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could reduce memory allocation this way
Suggested change
|
||||||
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 | ||||||
} | ||||||
|
||||||
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 | ||||||
} | ||||||
|
||||||
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, | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
} | ||
} | ||
Comment on lines
+43
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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), | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 passtrace
as a parameter?