Skip to content

Commit

Permalink
Add more tests for new transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Mar 22, 2024
1 parent 6e0a7fa commit ebbee81
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 120 deletions.
9 changes: 5 additions & 4 deletions lib/runtime/storage/storagediff.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (cs *storageDiff) delete(key string) {
func (cs *storageDiff) deleteChildLimit(keyToChild string,
currentChildKeys []string, limit int) (
deleted uint32, allDeleted bool) {

childChanges := cs.childChangeSet[keyToChild]
if childChanges == nil {
childChanges = newStorageDiff()
Expand Down Expand Up @@ -160,18 +161,18 @@ func (cs *storageDiff) clearPrefix(prefix []byte, trieKeys []string, limit int)
// getFromChild attempts to retrieve a value associated with a specific key
// from a child trie's change set identified by keyToChild.
// It returns the value and a boolean indicating if it was marked for deletion.
func (cs *storageDiff) getFromChild(keyToChild, key string) ([]byte, bool, error) {
func (cs *storageDiff) getFromChild(keyToChild, key string) ([]byte, bool) {
if cs == nil {
return nil, false, nil
return nil, false
}

childTrieChanges := cs.childChangeSet[keyToChild]
if childTrieChanges != nil {
value, deleted := childTrieChanges.get(key)
return value, deleted, nil
return value, deleted
}

return nil, false, trie.ErrChildTrieDoesNotExist
return nil, false
}

// upsertChild inserts or updates a value associated with a key within a
Expand Down
18 changes: 6 additions & 12 deletions lib/runtime/storage/storagediff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,8 @@ func Test_ChildTrie(t *testing.T) {
t.Parallel()

changes := newStorageDiff()
val, deleted, err := changes.getFromChild("notFound", "testChildKey")
val, deleted := changes.getFromChild("notFound", "testChildKey")

require.ErrorIs(t, err, trie.ErrChildTrieDoesNotExist)
require.False(t, deleted)
require.Nil(t, val)
})
Expand All @@ -214,9 +213,8 @@ func Test_ChildTrie(t *testing.T) {

changes := newStorageDiff()
changes.upsertChild("testChild", "testChildKey", []byte("test"))
val, deleted, err := changes.getFromChild("notFound", "testChildKey")
val, deleted := changes.getFromChild("notFound", "testChildKey")

require.ErrorIs(t, err, trie.ErrChildTrieDoesNotExist)
require.False(t, deleted)
require.Nil(t, val)
})
Expand All @@ -226,9 +224,8 @@ func Test_ChildTrie(t *testing.T) {

changes := newStorageDiff()
changes.upsertChild("testChild", "testChildKey", []byte("test"))
val, deleted, err := changes.getFromChild("testChild", "notFound")
val, deleted := changes.getFromChild("testChild", "notFound")

require.Nil(t, err)
require.False(t, deleted)
require.Nil(t, val)
})
Expand All @@ -238,9 +235,8 @@ func Test_ChildTrie(t *testing.T) {

changes := newStorageDiff()
changes.upsertChild("testChild", "testChildKey", []byte("test"))
val, deleted, err := changes.getFromChild("testChild", "testChildKey")
val, deleted := changes.getFromChild("testChild", "testChildKey")

require.Nil(t, err)
require.False(t, deleted)
require.Equal(t, []byte("test"), val)
})
Expand All @@ -254,9 +250,8 @@ func Test_ChildTrie(t *testing.T) {
childkey := "testChild"
changes.upsertChild(childkey, testKey, testValue)

val, deleted, err := changes.getFromChild(childkey, testKey)
val, deleted := changes.getFromChild(childkey, testKey)

require.Nil(t, err)
require.False(t, deleted)
require.Equal(t, testValue, val)
})
Expand All @@ -270,9 +265,8 @@ func Test_ChildTrie(t *testing.T) {
changes.upsertChild(childkey, testKey, testValue)
changes.deleteFromChild(childkey, testKey)

val, deleted, err := changes.getFromChild(childkey, testKey)
val, deleted := changes.getFromChild(childkey, testKey)

require.Nil(t, err)
require.True(t, deleted)
require.Nil(t, val)
})
Expand Down
45 changes: 26 additions & 19 deletions lib/runtime/storage/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package storage
import (
"container/list"
"encoding/binary"
"errors"
"fmt"
"sort"
"sync"
Expand Down Expand Up @@ -249,7 +248,7 @@ func (t *TrieState) SetChildStorage(keyToChild, key, value []byte) error {
if currentTx := t.getCurrentTransaction(); currentTx != nil {
keyToChildStr := string(keyToChild)
keyString := string(key)
t.getCurrentTransaction().upsertChild(keyToChildStr, keyString, value)
currentTx.upsertChild(keyToChildStr, keyString, value)
return nil
}

Expand All @@ -274,13 +273,7 @@ func (t *TrieState) GetChildStorage(keyToChild, key []byte) ([]byte, error) {
defer t.mtx.RUnlock()

if currentTx := t.getCurrentTransaction(); currentTx != nil {
val, deleted, err := currentTx.getFromChild(string(keyToChild), string(key))
if err != nil {
// If child trie is not present in current change lookup in state
if errors.Is(err, trie.ErrChildTrieDoesNotExist) {
return t.state.GetFromChild(keyToChild, key)
}
}
val, deleted := currentTx.getFromChild(string(keyToChild), string(key))
if val != nil || deleted {
return val, nil
}
Expand Down Expand Up @@ -309,22 +302,35 @@ func (t *TrieState) DeleteChildLimit(key []byte, limit *[]byte) (
t.mtx.Lock()
defer t.mtx.Unlock()

child, err := t.state.GetChild(key)
if err != nil {
return 0, false, err
}

if currentTx := t.getCurrentTransaction(); currentTx != nil {
deleteLimit := -1
if limit != nil {
deleteLimit = int(binary.LittleEndian.Uint32(*limit))
}

childEntriesKeys := maps.Keys(child.Entries())
deleted, allDeleted = currentTx.deleteChildLimit(string(key), childEntriesKeys, deleteLimit)
childKey := string(key)

child, err := t.state.GetChild(key)

childEntriesKeys := make([]string, 0)
if err != nil {
// If child trie does not exists and won't be created return err
if currentTx.childChangeSet[childKey] == nil {
return 0, false, err
}
} else {
childEntriesKeys = maps.Keys(child.Entries())
}

deleted, allDeleted = currentTx.deleteChildLimit(childKey, childEntriesKeys, deleteLimit)
return deleted, allDeleted, nil
}

child, err := t.state.GetChild(key)
if err != nil {
return 0, false, err
}

childTrieEntries := child.Entries()
qtyEntries := uint32(len(childTrieEntries))
if limit == nil {
Expand Down Expand Up @@ -382,12 +388,13 @@ func (t *TrieState) ClearPrefixInChild(keyToChild, prefix []byte) error {

if currentTx := t.getCurrentTransaction(); currentTx != nil {
child, err := t.state.GetChild(keyToChild)
if err != nil {
return err
childKeys := make([]string, 0)
if err == nil {
childKeys = maps.Keys(child.Entries())
}

childKeys := maps.Keys(child.Entries())
currentTx.clearPrefixInChild(string(keyToChild), prefix, childKeys, -1)
return nil
}

child, err := t.state.GetChild(keyToChild)
Expand Down
Loading

0 comments on commit ebbee81

Please sign in to comment.