Skip to content

Commit

Permalink
chore: include tests for nested transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Jan 11, 2024
1 parent 02ed484 commit 5948c66
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 6 deletions.
7 changes: 1 addition & 6 deletions lib/runtime/storage/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (t *TrieState) Rollback() {
defer t.mtx.Unlock()

if len(t.transactions) < 1 {
panic("no transactions to commit")
panic("no transactions to rollback")
}

t.transactions = t.transactions[:len(t.transactions)-1]
Expand Down Expand Up @@ -112,11 +112,6 @@ func (t *TrieState) Put(key, value []byte) (err error) {
func (t *TrieState) Get(key []byte) []byte {
t.mtx.RLock()
defer t.mtx.RUnlock()

if t.getCurrentTrie() == nil {
panic("trie cannot be nil at trie state")
}

return t.getCurrentTrie().Get(key)
}

Expand Down
103 changes: 103 additions & 0 deletions lib/runtime/storage/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,109 @@ func TestTrieState_RollbackStorageTransaction(t *testing.T) {
require.Equal(t, []byte(testCases[0]), val)
}

func TestTrieState_NestedTransactions(t *testing.T) {
cases := map[string]struct {
createTrieState func() *TrieState
assert func(*testing.T, *TrieState)
}{
"commiting_and_rollback_on_nested_transactions": {

Check failure on line 231 in lib/runtime/storage/trie_test.go

View workflow job for this annotation

GitHub Actions / linting

`commiting` is a misspelling of `committing` (misspell)
createTrieState: func() *TrieState {
ts := NewTrieState(trie.NewEmptyTrie())

ts.Put([]byte("key-1"), []byte("value-1"))
ts.Put([]byte("key-2"), []byte("value-2"))
ts.Put([]byte("key-3"), []byte("value-3"))

{
ts.StartTransaction()
ts.Put([]byte("key-4"), []byte("value-4"))

{
ts.StartTransaction()
ts.Delete([]byte("key-3"))
ts.Commit() // commit the most nested transaction
}

// rollback this transaction will discard the modifications
// made by the most nested transactions so this original trie
// should not be affected
ts.Rollback()
}
return ts
},
assert: func(t *testing.T, ts *TrieState) {
require.NotNil(t, ts.Get([]byte("key-1")))
require.NotNil(t, ts.Get([]byte("key-2")))
require.NotNil(t, ts.Get([]byte("key-3")))

require.Nil(t, ts.Get([]byte("key-4")))
require.Zero(t, len(ts.transactions))
},
},
"commiting_all_nested_transactions": {

Check failure on line 265 in lib/runtime/storage/trie_test.go

View workflow job for this annotation

GitHub Actions / linting

`commiting` is a misspelling of `committing` (misspell)
createTrieState: func() *TrieState {
ts := NewTrieState(trie.NewEmptyTrie())
{
ts.StartTransaction()
ts.Put([]byte("key-1"), []byte("value-1"))
{
ts.StartTransaction()
ts.Put([]byte("key-2"), []byte("value-2"))
{
ts.StartTransaction()
ts.Put([]byte("key-3"), []byte("value-3"))
{
ts.StartTransaction()
ts.Put([]byte("key-4"), []byte("value-4"))
{
ts.StartTransaction()
ts.Delete([]byte("key-3"))
ts.Commit()
}
ts.Commit()
}
ts.Commit()
}
ts.Commit()
}
ts.Commit()
}
return ts
},
assert: func(t *testing.T, ts *TrieState) {
require.NotNil(t, ts.Get([]byte("key-1")))
require.NotNil(t, ts.Get([]byte("key-2")))
require.NotNil(t, ts.Get([]byte("key-4")))
require.Zero(t, len(ts.transactions))
},
},
"rollback_without_transaction_should_panic": {
createTrieState: func() *TrieState {
return NewTrieState(trie.NewEmptyTrie())
},
assert: func(t *testing.T, ts *TrieState) {
require.PanicsWithValue(t, "no transactions to rollback", func() { ts.Rollback() })
},
},
"commit_without_transaction_should_panic": {
createTrieState: func() *TrieState {
return NewTrieState(trie.NewEmptyTrie())
},
assert: func(t *testing.T, ts *TrieState) {
require.PanicsWithValue(t, "no transactions to commit", func() { ts.Commit() })
},
},
}

for tname, tt := range cases {
tt := tt
t.Run(tname, func(t *testing.T) {
ts := tt.createTrieState()
tt.assert(t, ts)
})
}
}

func TestTrieState_DeleteChildLimit(t *testing.T) {
ts := &TrieState{currentTrie: trie.NewEmptyTrie()}
child := trie.NewEmptyTrie()
Expand Down

0 comments on commit 5948c66

Please sign in to comment.