Skip to content

Commit

Permalink
handle new system contract 0x2 (#2385)
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes authored Jan 17, 2025
1 parent 0c6508c commit dc1ead2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
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

0 comments on commit dc1ead2

Please sign in to comment.