Skip to content

Commit

Permalink
Rename TransactionStorage to Storage (#1803)
Browse files Browse the repository at this point in the history
  • Loading branch information
KananGiovanni authored Apr 10, 2024
1 parent 7d43e6a commit 1815864
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion core/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,6 @@ func (c *ContractUpdater) Replace(classHash *felt.Felt) error {
// storage of the contract.
func storage(addr *felt.Felt, txn db.Transaction) (*trie.Trie, error) {
addrBytes := addr.Marshal()
trieTxn := trie.NewTransactionStorage(txn, db.ContractStorage.Key(addrBytes))
trieTxn := trie.NewStorage(txn, db.ContractStorage.Key(addrBytes))
return trie.NewTriePedersen(trieTxn, contractStorageTrieHeight)
}
2 changes: 1 addition & 1 deletion core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (s *State) classesTrie() (*trie.Trie, func() error, error) {

func (s *State) globalTrie(bucket db.Bucket, newTrie trie.NewTrieFunc) (*trie.Trie, func() error, error) {
dbPrefix := bucket.Key()
tTxn := trie.NewTransactionStorage(s.txn, dbPrefix)
tTxn := trie.NewStorage(s.txn, dbPrefix)

// fetch root key
rootKeyDBKey := dbPrefix
Expand Down
28 changes: 14 additions & 14 deletions core/trie/transaction_storage.go → core/trie/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ func getBuffer() *bytes.Buffer {
}

// TransactionStorage is a database transaction on a trie.
type TransactionStorage struct {
type Storage struct {
txn db.Transaction
prefix []byte
}

func NewTransactionStorage(txn db.Transaction, prefix []byte) *TransactionStorage {
return &TransactionStorage{
func NewStorage(txn db.Transaction, prefix []byte) *Storage {
return &Storage{
txn: txn,
prefix: prefix,
}
}

// dbKey creates a byte array to be used as a key to our KV store
// it simply appends the given key to the configured prefix
func (t *TransactionStorage) dbKey(key *Key, buffer *bytes.Buffer) (int64, error) {
func (t *Storage) dbKey(key *Key, buffer *bytes.Buffer) (int64, error) {
_, err := buffer.Write(t.prefix)
if err != nil {
return 0, err
Expand All @@ -52,7 +52,7 @@ func (t *TransactionStorage) dbKey(key *Key, buffer *bytes.Buffer) (int64, error
return int64(len(t.prefix)) + keyLen, err
}

func (t *TransactionStorage) Put(key *Key, value *Node) error {
func (t *Storage) Put(key *Key, value *Node) error {
buffer := getBuffer()
defer bufferPool.Put(buffer)
keyLen, err := t.dbKey(key, buffer)
Expand All @@ -69,7 +69,7 @@ func (t *TransactionStorage) Put(key *Key, value *Node) error {
return t.txn.Set(encodedBytes[:keyLen], encodedBytes[keyLen:])
}

func (t *TransactionStorage) Get(key *Key) (*Node, error) {
func (t *Storage) Get(key *Key) (*Node, error) {
buffer := getBuffer()
defer bufferPool.Put(buffer)
_, err := t.dbKey(key, buffer)
Expand All @@ -87,7 +87,7 @@ func (t *TransactionStorage) Get(key *Key) (*Node, error) {
return node, err
}

func (t *TransactionStorage) Delete(key *Key) error {
func (t *Storage) Delete(key *Key) error {
buffer := getBuffer()
defer bufferPool.Put(buffer)
_, err := t.dbKey(key, buffer)
Expand All @@ -97,7 +97,7 @@ func (t *TransactionStorage) Delete(key *Key) error {
return t.txn.Delete(buffer.Bytes())
}

func (t *TransactionStorage) RootKey() (*Key, error) {
func (t *Storage) RootKey() (*Key, error) {
var rootKey *Key
if err := t.txn.Get(t.prefix, func(val []byte) error {
rootKey = new(Key)
Expand All @@ -108,7 +108,7 @@ func (t *TransactionStorage) RootKey() (*Key, error) {
return rootKey, nil
}

func (t *TransactionStorage) PutRootKey(newRootKey *Key) error {
func (t *Storage) PutRootKey(newRootKey *Key) error {
buffer := getBuffer()
defer bufferPool.Put(buffer)
_, err := newRootKey.WriteTo(buffer)
Expand All @@ -118,17 +118,17 @@ func (t *TransactionStorage) PutRootKey(newRootKey *Key) error {
return t.txn.Set(t.prefix, buffer.Bytes())
}

func (t *TransactionStorage) DeleteRootKey() error {
func (t *Storage) DeleteRootKey() error {
return t.txn.Delete(t.prefix)
}

func (t *TransactionStorage) SyncedStorage() *TransactionStorage {
return &TransactionStorage{
func (t *Storage) SyncedStorage() *Storage {
return &Storage{
txn: db.NewSyncTransaction(t.txn),
prefix: t.prefix,
}
}

func newMemStorage() *TransactionStorage {
return NewTransactionStorage(db.NewMemTransaction(), nil)
func newMemStorage() *Storage {
return NewStorage(db.NewMemTransaction(), nil)
}
20 changes: 10 additions & 10 deletions core/trie/transaction_storage_test.go → core/trie/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestTransactionStorage(t *testing.T) {
func TestStorage(t *testing.T) {
testDB := pebble.NewMemTest(t)
prefix := []byte{37, 44}
key := trie.NewKey(44, nil)
Expand All @@ -26,14 +26,14 @@ func TestTransactionStorage(t *testing.T) {

t.Run("put a node", func(t *testing.T) {
require.NoError(t, testDB.Update(func(txn db.Transaction) error {
tTxn := trie.NewTransactionStorage(txn, prefix)
tTxn := trie.NewStorage(txn, prefix)
return tTxn.Put(&key, node)
}))
})

t.Run("get a node", func(t *testing.T) {
require.NoError(t, testDB.View(func(txn db.Transaction) error {
tTxn := trie.NewTransactionStorage(txn, prefix)
tTxn := trie.NewStorage(txn, prefix)
var got *trie.Node
got, err = tTxn.Get(&key)
require.NoError(t, err)
Expand All @@ -45,7 +45,7 @@ func TestTransactionStorage(t *testing.T) {
t.Run("roll back on error", func(t *testing.T) {
// Successfully delete a node and return an error to force a roll back.
require.Error(t, testDB.Update(func(txn db.Transaction) error {
tTxn := trie.NewTransactionStorage(txn, prefix)
tTxn := trie.NewStorage(txn, prefix)
err = tTxn.Delete(&key)
require.NoError(t, err)
return errors.New("should rollback")
Expand All @@ -54,7 +54,7 @@ func TestTransactionStorage(t *testing.T) {
// If the transaction was properly rolled back, the node that we
// "deleted" should still exist in the db.
require.NoError(t, testDB.View(func(txn db.Transaction) error {
tTxn := trie.NewTransactionStorage(txn, prefix)
tTxn := trie.NewStorage(txn, prefix)
var got *trie.Node
got, err = tTxn.Get(&key)
assert.Equal(t, node, got)
Expand All @@ -65,13 +65,13 @@ func TestTransactionStorage(t *testing.T) {
t.Run("delete a node", func(t *testing.T) {
// Delete a node.
require.NoError(t, testDB.Update(func(txn db.Transaction) error {
tTxn := trie.NewTransactionStorage(txn, prefix)
tTxn := trie.NewStorage(txn, prefix)
return tTxn.Delete(&key)
}))

// Node should no longer exist in the database.
require.EqualError(t, testDB.View(func(txn db.Transaction) error {
tTxn := trie.NewTransactionStorage(txn, prefix)
tTxn := trie.NewStorage(txn, prefix)
_, err = tTxn.Get(&key)
return err
}), db.ErrKeyNotFound.Error())
Expand All @@ -81,14 +81,14 @@ func TestTransactionStorage(t *testing.T) {

t.Run("put root key", func(t *testing.T) {
require.NoError(t, testDB.Update(func(txn db.Transaction) error {
tTxn := trie.NewTransactionStorage(txn, prefix)
tTxn := trie.NewStorage(txn, prefix)
return tTxn.PutRootKey(&rootKey)
}))
})

t.Run("read root key", func(t *testing.T) {
require.NoError(t, testDB.View(func(txn db.Transaction) error {
tTxn := trie.NewTransactionStorage(txn, prefix)
tTxn := trie.NewStorage(txn, prefix)
gotRootKey, err := tTxn.RootKey()
require.NoError(t, err)
assert.Equal(t, rootKey, *gotRootKey)
Expand All @@ -98,7 +98,7 @@ func TestTransactionStorage(t *testing.T) {

t.Run("delete root key", func(t *testing.T) {
require.NoError(t, testDB.Update(func(txn db.Transaction) error {
tTxn := trie.NewTransactionStorage(txn, prefix)
tTxn := trie.NewStorage(txn, prefix)
require.NoError(t, tTxn.DeleteRootKey())
_, err := tTxn.RootKey()
require.ErrorIs(t, err, db.ErrKeyNotFound)
Expand Down
10 changes: 5 additions & 5 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ type Trie struct {
height uint8
rootKey *Key
maxKey *felt.Felt
storage *TransactionStorage
storage *Storage
hash hashFunc

dirtyNodes []*Key
rootKeyIsDirty bool
}

type NewTrieFunc func(*TransactionStorage, uint8) (*Trie, error)
type NewTrieFunc func(*Storage, uint8) (*Trie, error)

func NewTriePedersen(storage *TransactionStorage, height uint8) (*Trie, error) {
func NewTriePedersen(storage *Storage, height uint8) (*Trie, error) {
return newTrie(storage, height, crypto.Pedersen)
}

func NewTriePoseidon(storage *TransactionStorage, height uint8) (*Trie, error) {
func NewTriePoseidon(storage *Storage, height uint8) (*Trie, error) {
return newTrie(storage, height, crypto.Poseidon)
}

func newTrie(storage *TransactionStorage, height uint8, hash hashFunc) (*Trie, error) {
func newTrie(storage *Storage, height uint8, hash hashFunc) (*Trie, error) {
if height > felt.Bits {
return nil, fmt.Errorf("max trie height is %d, got: %d", felt.Bits, height)
}
Expand Down
4 changes: 2 additions & 2 deletions core/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func TestRootKeyAlwaysUpdatedOnCommit(t *testing.T) {

// The database transaction we will use to create both tries.
txn := db.NewMemTransaction()
tTxn := trie.NewTransactionStorage(txn, []byte{1, 2, 3})
tTxn := trie.NewStorage(txn, []byte{1, 2, 3})

// Step 1: Create first trie
tempTrie, err := trie.NewTriePedersen(tTxn, height)
Expand All @@ -346,7 +346,7 @@ func TestRootKeyAlwaysUpdatedOnCommit(t *testing.T) {
assert.Equal(t, want, got)

// Step 2: Different trie created with the same db transaction and calls [trie.Root].
tTxn = trie.NewTransactionStorage(txn, []byte{1, 2, 3})
tTxn = trie.NewStorage(txn, []byte{1, 2, 3})
secondTrie, err := trie.NewTriePedersen(tTxn, height)
require.NoError(t, err)
got, err = secondTrie.Root()
Expand Down

0 comments on commit 1815864

Please sign in to comment.