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

Conversation

rianhughes
Copy link
Contributor

@rianhughes rianhughes commented Jan 13, 2025

This PR implements the genesis pkg that builds a state diff from declaring classes, deploying contracts and invoking functions. The sequencer can then commit this state diff to state, so that upon startup, a set of account contracts have already been deployed and funded (it is not possible to achieve this from an empty state by submitting transactions, since Starknet requires accounts to be funded to perform any actions on the state - originally this wasn't a requirement in Starknet). This is particularly useful as it allows users to start the sequencer with any number of accounts pre-deployed, and pre-funded, etc, and opens up the possibility of using Juno as a devnet

In essence:

  1. we load classes, then declare them,
  2. execute the constructor functions using the vm, which writes the changes to the pending state
  3. execute any functions of interest (eg token transfers) using the vm (where similarly the vm writes the changes to the pending state)
  4. extract the state-diff and new classes from the pending state.

@rianhughes rianhughes marked this pull request as draft January 13, 2025 12:29
@rianhughes rianhughes force-pushed the feature/sequencer-genesis branch from fb11e07 to 459db5d Compare January 13, 2025 14:37
@rianhughes rianhughes marked this pull request as ready for review January 13, 2025 14:38
Copy link

codecov bot commented Jan 13, 2025

Codecov Report

Attention: Patch coverage is 64.82759% with 102 lines in your changes missing coverage. Please review.

Project coverage is 74.54%. Comparing base (d99e28b) to head (82064bd).
Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
genesis/genesis.go 64.53% 42 Missing and 19 partials ⚠️
adapters/vm2core/vm2core.go 55.00% 14 Missing and 4 partials ⚠️
sync/pending.go 68.75% 13 Missing and 2 partials ⚠️
core/state_update.go 73.91% 5 Missing and 1 partial ⚠️
node/throttled_vm.go 60.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2371      +/-   ##
==========================================
- Coverage   74.70%   74.54%   -0.16%     
==========================================
  Files         110      112       +2     
  Lines       12021    12407     +386     
==========================================
+ Hits         8980     9249     +269     
- Misses       2349     2435      +86     
- Partials      692      723      +31     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

genesis/genesis.go Outdated Show resolved Hide resolved
core/state_update.go Outdated Show resolved Hide resolved
adapters/vm2core/vm2core.go Outdated Show resolved Hide resolved
sync/pending.go Outdated Show resolved Hide resolved
sync/pending.go Outdated Show resolved Hide resolved
Copy link
Contributor

@AnkushinDaniil AnkushinDaniil left a comment

Choose a reason for hiding this comment

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

I have finished reviewing everything except vm logic. Everything is good, just small changes and questions. I'll continue reviewing vm logic.

adapters/vm2core/vm2core.go Outdated Show resolved Hide resolved
core/state_update.go Show resolved Hide resolved
genesis/genesis.go Outdated Show resolved Hide resolved
genesis/genesis.go Outdated Show resolved Hide resolved
Comment on lines +115 to +126
for classHash, class := range newClasses {
// Sets pending.newClasses, DeclaredV0Classes, (not DeclaredV1Classes)
if err = genesisState.SetContractClass(&classHash, class); err != nil {
return nil, nil, fmt.Errorf("declare v0 class: %v", err)
}

if cairo1Class, isCairo1 := class.(*core.Cairo1Class); isCairo1 {
if err = genesisState.SetCompiledClassHash(&classHash, cairo1Class.Compiled.Hash()); err != nil {
return nil, nil, fmt.Errorf("set compiled class hash: %v", err)
}
}
}
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 use descriptive variable names instead of this comment?

// Sets pending.newClasses, DeclaredV0Classes, (not DeclaredV1Classes)

I don't quite understand why only DeclaredV0Classes in newClasses.

Copy link
Contributor Author

@rianhughes rianhughes Jan 21, 2025

Choose a reason for hiding this comment

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

Could we use descriptive variable names instead of this comment?

Not sure I understand sorry. Could you give me an example?

I don't quite understand why only DeclaredV0Classes in newClasses.

Good question! Basically we inherit this structure from blockifier / starknet. To allow blockifier to make changes to the pending state we need to implement a certain interface (see impl State for JunoStateReader). We use that interface here to declare classes. Note this is how it seems to be done in blockifier / starknet, it first calls set_contract_class then if its a CairoV1 class, set_compiled_class_hash.

genesis/genesis.go Outdated Show resolved Hide resolved
genesis/genesis.go Outdated Show resolved Hide resolved
genesis/genesis.go Outdated Show resolved Hide resolved
sync/pending.go Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants