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

handle new system contract 0x2 #2385

Merged
merged 1 commit into from
Jan 17, 2025
Merged
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
23 changes: 12 additions & 11 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,11 @@ func (s *State) Update(blockNumber uint64, update *StateUpdate, declaredClasses
}

var (
noClassContractsClassHash = new(felt.Felt).SetUint64(0)
systemContractsClassHash = new(felt.Felt).SetUint64(0)

noClassContracts = map[felt.Felt]struct{}{
systemContracts = map[felt.Felt]struct{}{
*new(felt.Felt).SetUint64(1): {},
*new(felt.Felt).SetUint64(2): {},
}
)

Expand Down Expand Up @@ -377,9 +378,9 @@ func (s *State) updateContractStorages(stateTrie *trie.Trie, diffs map[felt.Felt
addr *felt.Felt
}

// make sure all noClassContracts are deployed
// make sure all systemContracts are deployed
for addr := range diffs {
if _, ok := noClassContracts[addr]; !ok {
if _, ok := systemContracts[addr]; !ok {
continue
}

Expand All @@ -388,8 +389,8 @@ func (s *State) updateContractStorages(stateTrie *trie.Trie, diffs map[felt.Felt
if !errors.Is(err, ErrContractNotDeployed) {
return err
}
// Deploy noClassContract
err = s.putNewContract(stateTrie, &addr, noClassContractsClassHash, blockNumber)
// Deploy systemContract
err = s.putNewContract(stateTrie, &addr, systemContractsClassHash, blockNumber)
if err != nil {
return err
}
Expand Down Expand Up @@ -570,18 +571,18 @@ func (s *State) Revert(blockNumber uint64, update *StateUpdate) error {
}
}

if err = s.purgeNoClassContracts(); err != nil {
if err = s.purgesystemContracts(); err != nil {
return err
}

return s.verifyStateUpdateRoot(update.OldRoot)
}

func (s *State) purgeNoClassContracts() error {
// As noClassContracts are not in StateDiff.DeployedContracts we can only purge them if their storage no longer exists.
func (s *State) purgesystemContracts() error {
// As systemContracts are not in StateDiff.DeployedContracts we can only purge them if their storage no longer exists.
// Updating contracts with reverse diff will eventually lead to the deletion of noClassContract's storage key from db. Thus,
// we can use the lack of key's existence as reason for purging noClassContracts.
for addr := range noClassContracts {
// we can use the lack of key's existence as reason for purging systemContracts.
for addr := range systemContracts {
noClassC, err := NewContractUpdater(&addr, s.txn)
if err != nil {
if !errors.Is(err, ErrContractNotDeployed) {
Expand Down
4 changes: 2 additions & 2 deletions core/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestUpdate(t *testing.T) {
},
}

t.Run("update noClassContracts storage", func(t *testing.T) {
t.Run("update systemContracts storage", func(t *testing.T) {
require.NoError(t, state.Update(4, su4, nil))

gotValue, err := state.ContractStorage(scAddr, scKey)
Expand Down Expand Up @@ -571,7 +571,7 @@ func TestRevertGenesisStateDiff(t *testing.T) {
require.NoError(t, state.Revert(0, su))
}

func TestRevertNoClassContracts(t *testing.T) {
func TestRevertSystemContracts(t *testing.T) {
client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)

Expand Down
Loading