Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for race conditions in merkledb #1256

Merged
merged 6 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions x/merkledb/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"context"
"math/rand"
"strconv"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -1178,3 +1180,179 @@ func TestTrieViewInvalidChildrenExcept(t *testing.T) {
require.True(view3.invalidated)
require.Empty(view1.childViews)
}

func Test_Trie_ConcurrentReadWrite(t *testing.T) {
require := require.New(t)

trie, err := getBasicDB()
require.NoError(err)
require.NotNil(trie)
newTrie, err := trie.NewView()
require.NoError(err)

var wg sync.WaitGroup
defer wg.Wait()

wg.Add(1)
go func() {
defer wg.Done()
err := newTrie.Insert(context.Background(), []byte("key"), []byte("value"))
require.NoError(err)
}()

require.Eventually(
func() bool {
value, err := newTrie.GetValue(context.Background(), []byte("key"))

if err == database.ErrNotFound {
return false
}

require.NoError(err)
require.Equal([]byte("value"), value)
return true
},
time.Second,
time.Millisecond,
)
}

func Test_Trie_ConcurrentNewViewAndCommit(t *testing.T) {
require := require.New(t)

trie, err := getBasicDB()
require.NoError(err)
require.NotNil(trie)

newTrie, err := trie.NewView()
require.NoError(err)
err = newTrie.Insert(context.Background(), []byte("key"), []byte("value0"))
require.NoError(err)

var wg sync.WaitGroup
defer wg.Wait()

wg.Add(1)
go func() {
defer wg.Done()
err := newTrie.CommitToDB(context.Background())
require.NoError(err)
}()

newView, err := newTrie.NewView()
require.NoError(err)
require.NotNil(newView)
}

func Test_Trie_ConcurrentDeleteAndMerkleRoot(t *testing.T) {
require := require.New(t)

trie, err := getBasicDB()
require.NoError(err)
require.NotNil(trie)

newTrie, err := trie.NewView()
require.NoError(err)
err = newTrie.Insert(context.Background(), []byte("key"), []byte("value0"))
require.NoError(err)

var wg sync.WaitGroup
defer wg.Wait()

wg.Add(1)
go func() {
defer wg.Done()
err := newTrie.Remove(context.Background(), []byte("key"))
require.NoError(err)
}()

rootID, err := newTrie.GetMerkleRoot(context.Background())
require.NoError(err)
require.NotZero(rootID)
}

func Test_Trie_ConcurrentInsertProveCommit(t *testing.T) {
require := require.New(t)

trie, err := getBasicDB()
require.NoError(err)
require.NotNil(trie)

newTrie, err := trie.NewView()
require.NoError(err)

var wg sync.WaitGroup
defer wg.Wait()

wg.Add(1)
go func() {
defer wg.Done()
err := newTrie.Insert(context.Background(), []byte("key2"), []byte("value2"))
require.NoError(err)
}()

require.Eventually(
func() bool {
proof, err := newTrie.GetProof(context.Background(), []byte("key2"))
require.NoError(err)
require.NotNil(proof)

if proof.Value.value == nil {
// this is an exclusion proof since the value is nil
// return false to keep waiting for Insert to complete.
return false
}
require.Equal([]byte("value2"), proof.Value.value)

err = newTrie.CommitToDB(context.Background())
require.NoError(err)
return true
},
time.Second,
time.Millisecond,
)
}

func Test_Trie_ConcurrentInsertAndRangeProof(t *testing.T) {
require := require.New(t)

trie, err := getBasicDB()
require.NoError(err)
require.NotNil(trie)

newTrie, err := trie.NewView()
require.NoError(err)
err = newTrie.Insert(context.Background(), []byte("key1"), []byte("value1"))
require.NoError(err)

var wg sync.WaitGroup
defer wg.Wait()

wg.Add(1)
go func() {
defer wg.Done()
err := newTrie.Insert(context.Background(), []byte("key2"), []byte("value2"))
require.NoError(err)
err = newTrie.Insert(context.Background(), []byte("key3"), []byte("value3"))
require.NoError(err)
}()

require.Eventually(
func() bool {
rangeProof, err := newTrie.GetRangeProof(context.Background(), []byte("key1"), []byte("key3"), 3)
require.NoError(err)
require.NotNil(rangeProof)

if len(rangeProof.KeyValues) < 3 {
// Wait for the other goroutine to finish inserting
return false
}

// Make sure we have exactly 3 KeyValues
require.Len(rangeProof.KeyValues, 3)
return true
},
time.Second,
time.Millisecond,
)
}
2 changes: 1 addition & 1 deletion x/merkledb/trieview.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ func (t *trieView) GetRangeProof(

// CommitToDB commits changes from this trie to the underlying DB.
func (t *trieView) CommitToDB(ctx context.Context) error {
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.Commit")
ctx, span := t.db.tracer.Start(ctx, "MerkleDB.trieview.CommitToDB")
defer span.End()

t.db.commitLock.Lock()
Expand Down