Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Oct 17, 2024
1 parent 1d45e0e commit 7670cbc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ var (

type OnValueChanged = func(location, oldValue *felt.Felt) error

// StateContract represents a contract instance.
// The usage of a `StateContract` is as follows:
// 1. Create or obtain `StateContract` instance from the database.
// 2. Update the contract fields
// 3. Commit the contract to the database
type StateContract struct {
// ClassHash is the hash of the contract's class
ClassHash *felt.Felt
Expand All @@ -33,6 +38,7 @@ type StateContract struct {
dirtyStorage map[felt.Felt]*felt.Felt `cbor:"-"`
}

// NewStateContract creates a new contract instance.
func NewStateContract(
addr *felt.Felt,
classHash *felt.Felt,
Expand All @@ -50,6 +56,7 @@ func NewStateContract(
return sc
}

// StorageRoot returns the root of the contract's storage trie.
func (c *StateContract) StorageRoot(txn db.Transaction) (*felt.Felt, error) {
storageTrie, err := storage(c.Address, txn)
if err != nil {
Expand All @@ -59,6 +66,8 @@ func (c *StateContract) StorageRoot(txn db.Transaction) (*felt.Felt, error) {
return storageTrie.Root()
}

// UpdateStorage updates the storage of a contract.
// Note that this does not modify the storage trie, which must be committed separately.
func (c *StateContract) UpdateStorage(key, value *felt.Felt) {
if c.dirtyStorage == nil {
c.dirtyStorage = make(map[felt.Felt]*felt.Felt)

Check warning on line 73 in core/contract.go

View check run for this annotation

Codecov / codecov/patch

core/contract.go#L73

Added line #L73 was not covered by tests
Expand All @@ -67,6 +76,7 @@ func (c *StateContract) UpdateStorage(key, value *felt.Felt) {
c.dirtyStorage[*key] = value
}

// GetStorage retrieves the value of a storage location from the contract's storage
func (c *StateContract) GetStorage(key *felt.Felt, txn db.Transaction) (*felt.Felt, error) {
if c.dirtyStorage != nil {
if val, ok := c.dirtyStorage[*key]; ok {
Expand All @@ -83,25 +93,30 @@ func (c *StateContract) GetStorage(key *felt.Felt, txn db.Transaction) (*felt.Fe
return storage.Get(key)
}

// logOldValue is a helper function to record the history of a contract's value
func (c *StateContract) logOldValue(key []byte, oldValue *felt.Felt, height uint64, txn db.Transaction) error {
return txn.Set(logDBKey(key, height), oldValue.Marshal())
}

// LogStorage records the history of the contract's storage
func (c *StateContract) LogStorage(location, oldVal *felt.Felt, height uint64, txn db.Transaction) error {
key := storageLogKey(c.Address, location)
return c.logOldValue(key, oldVal, height, txn)
}

// LogNonce records the history of the contract's nonce
func (c *StateContract) LogNonce(height uint64, txn db.Transaction) error {
key := nonceLogKey(c.Address)
return c.logOldValue(key, c.Nonce, height, txn)
}

// LogClassHash records the history of the contract's class hash
func (c *StateContract) LogClassHash(height uint64, txn db.Transaction) error {
key := classHashLogKey(c.Address)
return c.logOldValue(key, c.ClassHash, height, txn)
}

// BufferedCommit creates a buffered transaction and commits the contract to the database
func (c *StateContract) BufferedCommit(txn db.Transaction, logChanges bool, blockNum uint64) (*db.BufferedTransaction, error) {
bufferedTxn := db.NewBufferedTransaction(txn)

Expand Down
1 change: 1 addition & 0 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ var (
}
)

// Commit updates the state by committing the dirty contracts to the database.
func (s *State) Commit(
stateTrie *trie.Trie,
contracts map[felt.Felt]*StateContract,
Expand Down

0 comments on commit 7670cbc

Please sign in to comment.