From 00a5f9b5f477f46df6b8ba9bf197bb9de354f912 Mon Sep 17 00:00:00 2001 From: Diego Date: Tue, 19 Mar 2024 13:21:08 -0300 Subject: [PATCH] Change constructor name --- lib/runtime/storage/storagediff.go | 10 ++--- lib/runtime/storage/storagediff_test.go | 49 +++++++++++++------------ lib/runtime/storage/trie.go | 2 +- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/lib/runtime/storage/storagediff.go b/lib/runtime/storage/storagediff.go index 0dee97e889..94eb0729ee 100644 --- a/lib/runtime/storage/storagediff.go +++ b/lib/runtime/storage/storagediff.go @@ -29,7 +29,7 @@ type storageDiff struct { } // newChangeSet initialises and returns a new storageDiff instance -func newChangeSet() *storageDiff { +func newStorageDiff() *storageDiff { return &storageDiff{ upserts: make(map[string][]byte), deletes: make(map[string]bool), @@ -100,7 +100,7 @@ func (cs *storageDiff) deleteChildLimit(keyToChild string, deleted uint32, allDeleted bool) { childChanges := cs.childChangeSet[keyToChild] if childChanges == nil { - childChanges = newChangeSet() + childChanges = newStorageDiff() } if limit == -1 { @@ -133,7 +133,7 @@ func (cs *storageDiff) deleteChildLimit(keyToChild string, func (cs *storageDiff) clearPrefixInChild(keyToChild string, prefix []byte, childKeys []string) { childChanges := cs.childChangeSet[keyToChild] if childChanges == nil { - childChanges = newChangeSet() + childChanges = newStorageDiff() } childChanges.clearPrefix(prefix, childKeys, -1) cs.childChangeSet[keyToChild] = childChanges @@ -201,7 +201,7 @@ func (cs *storageDiff) upsertChild(keyToChild, key string, value []byte) { childChanges := cs.childChangeSet[keyToChild] if childChanges == nil { - childChanges = newChangeSet() + childChanges = newStorageDiff() } childChanges.upserts[key] = value @@ -219,7 +219,7 @@ func (cs *storageDiff) deleteFromChild(keyToChild, key string) { childChanges := cs.childChangeSet[keyToChild] if childChanges == nil { - childChanges = newChangeSet() + childChanges = newStorageDiff() } else { delete(cs.childChangeSet, keyToChild) } diff --git a/lib/runtime/storage/storagediff_test.go b/lib/runtime/storage/storagediff_test.go index 655659764f..37e6b38e7e 100644 --- a/lib/runtime/storage/storagediff_test.go +++ b/lib/runtime/storage/storagediff_test.go @@ -9,28 +9,29 @@ import ( "github.com/stretchr/testify/require" ) -func TestChangeSet_InsertGet(t *testing.T) { - changes := newChangeSet() - - key := "key" - value := []byte("value") - - changes.upsert(key, value) - val, deleted := changes.get(key) - require.False(t, deleted) - require.Equal(t, value, val) -} - -func TestChangeSet_InsertDeleteGet(t *testing.T) { - changes := newChangeSet() - - key := "key" - value := []byte("value") - - changes.upsert(key, value) - changes.delete(key) - - val, deleted := changes.get(key) - require.True(t, deleted) - require.Nil(t, val) +func TestStorageDiff_Get(t *testing.T) { + t.Run("Upsert", func(t *testing.T) { + changes := newStorageDiff() + + key := "key" + value := []byte("value") + changes.upsert(key, value) + + val, deleted := changes.get(key) + require.False(t, deleted) + require.Equal(t, value, val) + }) + + t.Run("Upsert then delete", func(t *testing.T) { + changes := newStorageDiff() + + key := "key" + value := []byte("value") + changes.upsert(key, value) + changes.delete(key) + + val, deleted := changes.get(key) + require.True(t, deleted) + require.Nil(t, val) + }) } diff --git a/lib/runtime/storage/trie.go b/lib/runtime/storage/trie.go index 83bacca2a2..0c64d64de2 100644 --- a/lib/runtime/storage/trie.go +++ b/lib/runtime/storage/trie.go @@ -56,7 +56,7 @@ func (t *TrieState) StartTransaction() { nextChangeSet := t.getCurrentTransaction() if nextChangeSet == nil { - nextChangeSet = newChangeSet() + nextChangeSet = newStorageDiff() } t.transactions.PushBack(nextChangeSet.snapshot())