Skip to content

Commit

Permalink
Log class hash history
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Apr 24, 2023
1 parent cbe4447 commit 3d0fa25
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
22 changes: 22 additions & 0 deletions core/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,25 @@ func (h *History) ContractNonceAt(contractAddress *felt.Felt, height uint64) (*f

return new(felt.Felt).SetBytes(value), nil
}

func classHashLogKey(contractAddress *felt.Felt) []byte {
return db.ContractClassHashHistory.Key(contractAddress.Marshal())
}

func (h *History) LogContractClassHash(contractAddress, oldValue *felt.Felt, height uint64) error {
return h.logOldValue(classHashLogKey(contractAddress), oldValue.Marshal(), height)
}

func (h *History) DeleteContractClassHashLog(contractAddress *felt.Felt, height uint64) error {
return h.deleteLog(classHashLogKey(contractAddress), height)
}

func (h *History) ContractClassHashAt(contractAddress *felt.Felt, height uint64) (*felt.Felt, error) {
key := classHashLogKey(contractAddress)
value, err := h.valueAt(key, height)
if err != nil {
return nil, err
}

return new(felt.Felt).SetBytes(value), nil
}
26 changes: 20 additions & 6 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type StateHistoryReader interface {

ContractStorageAt(addr, key *felt.Felt, blockNumber uint64) (*felt.Felt, error)
ContractNonceAt(addr *felt.Felt, blockNumber uint64) (*felt.Felt, error)
// todo: extend
ContractClassHashAt(addr *felt.Felt, blockNumber uint64) (*felt.Felt, error)
}

type StateReader interface {
Expand Down Expand Up @@ -222,7 +222,12 @@ func (s *State) updateContracts(blockNumber uint64, diff *StateDiff) error {

// replace contract instances
for _, replace := range diff.ReplacedClasses {
if err := s.replaceContract(replace.Address, replace.ClassHash); err != nil {
oldClassHash, err := s.replaceContract(replace.Address, replace.ClassHash)
if err != nil {
return err
}

if err = s.LogContractClassHash(replace.Address, oldClassHash, blockNumber); err != nil {
return err
}
}
Expand Down Expand Up @@ -254,17 +259,26 @@ func (s *State) updateContracts(blockNumber uint64, diff *StateDiff) error {
}

// replaceContract replaces the class that a contract at a given address instantiates
func (s *State) replaceContract(addr, classHash *felt.Felt) error {
func (s *State) replaceContract(addr, classHash *felt.Felt) (*felt.Felt, error) {
contract, err := NewContract(addr, s.txn)
if err != nil {
return err
return nil, err
}

oldClassHash, err := contract.ClassHash()
if err != nil {
return nil, err
}

if err = contract.Replace(classHash); err != nil {
return err
return nil, err
}

return s.updateContractCommitment(contract)
if err = s.updateContractCommitment(contract); err != nil {
return nil, err
}

return oldClassHash, nil
}

func (s *State) putClass(classHash *felt.Felt, class Class) error {
Expand Down
1 change: 1 addition & 0 deletions db/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
ClassesTrie
ContractStorageHistory
ContractNonceHistory
ContractClassHashHistory
)

// Key flattens a prefix and series of byte arrays into a single []byte.
Expand Down

0 comments on commit 3d0fa25

Please sign in to comment.