Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony committed Nov 8, 2024
1 parent ae23b6d commit 7b883f1
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
26 changes: 26 additions & 0 deletions chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/test/datagen"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/tx"
)
Expand Down Expand Up @@ -49,6 +50,7 @@ func TestChain(t *testing.T) {
assert.Equal(t, M(b3.Header().ID(), nil), M(c.GetBlockID(3)))
assert.Equal(t, M(b3.Header(), nil), M(c.GetBlockHeader(3)))
assert.Equal(t, M(block.Compose(b3.Header(), b3.Transactions()), nil), M(c.GetBlock(3)))
assert.Equal(t, repo.NewBestChain().GenesisID(), repo.GenesisBlock().Header().ID())

_, err := c.GetBlockID(4)
assert.True(t, c.IsNotFound(err))
Expand Down Expand Up @@ -97,3 +99,27 @@ func TestChain(t *testing.T) {
_, err = dangleChain.Exclude(c1)
assert.Error(t, err)
}

func TestHasTransaction(t *testing.T) {
_, repo := newTestRepo()

parent := repo.GenesisBlock()
for i := 1; i <= 101; i++ {
b := newBlock(parent, uint64(i)*10)
repo.AddBlock(b, nil, 0, false)
parent = b
}

repo.SetBestBlockID(parent.Header().ID())
has, err := repo.NewBestChain().HasTransaction(datagen.RandomHash(), 0)
assert.Nil(t, err)
assert.False(t, has)

tx1 := newTx()
bx := newBlock(parent, 10020, tx1)
repo.AddBlock(bx, tx.Receipts{&tx.Receipt{}}, 0, true)

has, err = repo.NewBestChain().HasTransaction(tx1.ID(), 0)
assert.Nil(t, err)
assert.True(t, has)
}
10 changes: 10 additions & 0 deletions chain/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ func TestRepository(t *testing.T) {
}
}

func TestAddBlock(t *testing.T) {
_, repo := newTestRepo()

err := repo.AddBlock(new(block.Builder).Build(), nil, 0, false)
assert.Error(t, err, "parent missing")

b1 := newBlock(repo.GenesisBlock(), 10)
assert.Nil(t, repo.AddBlock(b1, nil, 0, false))
}

func TestConflicts(t *testing.T) {
_, repo := newTestRepo()
b0 := repo.GenesisBlock()
Expand Down
144 changes: 144 additions & 0 deletions muxdb/muxdb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright (c) 2024 The VeChainThor developers

// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>

package muxdb

import (
"context"
"math"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/trie"
)

func TestMuxdb(t *testing.T) {
var err error
db := NewMem()
db.Close()

dir := os.TempDir()

opts := Options{
TrieNodeCacheSizeMB: 128,
TrieCachedNodeTTL: 30, // 5min
TrieDedupedPartitionFactor: math.MaxUint32,
TrieWillCleanHistory: true,
OpenFilesCacheCapacity: 512,
ReadCacheMB: 256, // rely on os page cache other than huge db read cache.
WriteBufferMB: 128,
TrieHistPartitionFactor: 1000,
}
path := filepath.Join(dir, "main.db")
db, err = Open(path, &opts)
assert.Nil(t, err)

err = db.Close()
assert.Nil(t, err)

os.RemoveAll(path)
}

func TestStore(t *testing.T) {
db := NewMem()

store := db.NewStore("test")
key := []byte("key")
val := []byte("val")

store.Put(key, val)
v, err := store.Get(key)
assert.Nil(t, err)
assert.Equal(t, val, v)

store.Delete(key)
_, err = store.Get(key)
assert.True(t, db.IsNotFound(err))

db.Close()
}

func TestMuxdbTrie(t *testing.T) {
var err error
db := NewMem()

tr := db.NewTrie("test", trie.Root{})
tr.SetNoFillCache(true)
key := []byte("key")
val1 := []byte("val")
val2 := []byte("val2")

ver1 := trie.Version{Major: 1, Minor: 0}
ver2 := trie.Version{Major: 100, Minor: 0}
ver3 := trie.Version{Major: 101, Minor: 0}

err = tr.Update(key, val1, nil)
assert.Nil(t, err)
err = tr.Commit(ver1, false)
assert.Nil(t, err)

root1 := tr.Hash()
tr1 := db.NewTrie("test", trie.Root{Hash: root1, Ver: ver1})
tr1.SetNoFillCache(true)
v, _, err := tr1.Get(key)
assert.Nil(t, err)
assert.Equal(t, val1, v)

tr1.Update(key, val2, nil)
err = tr1.Commit(ver2, false)
assert.Nil(t, err)
root2 := tr1.Hash()

tr2 := db.NewTrie("test", trie.Root{Hash: root2, Ver: ver2})
tr2.SetNoFillCache(true)
v, _, err = tr2.Get(key)
assert.Nil(t, err)
assert.Equal(t, val2, v)

err = tr2.Commit(ver3, false)
assert.Nil(t, err)
root3 := tr2.Hash()

//prune trie [0, ver3)
xtr := db.NewTrie("test", trie.Root{Hash: root2, Ver: ver2})
err = xtr.Checkpoint(context.Background(), 0, nil)
assert.Nil(t, err)
err = db.DeleteTrieHistoryNodes(context.Background(), 0, ver3.Major)
assert.Nil(t, err)

//after delete history nodes,the history nodes should be deleted
path := []byte{}

histKey := xtr.back.AppendHistNodeKey(nil, "test", path, ver1)
_, err = xtr.back.Store.Get(histKey)
assert.True(t, db.IsNotFound(err))

histKey = xtr.back.AppendHistNodeKey(nil, "test", path, ver2)
_, err = xtr.back.Store.Get(histKey)
assert.True(t, db.IsNotFound(err))

histKey = xtr.back.AppendHistNodeKey(nil, "test", path, ver3)
_, err = xtr.back.Store.Get(histKey)
assert.Nil(t, err)

dedupedKey := xtr.back.AppendDedupedNodeKey(nil, "test", path, ver2)
blob, err := xtr.back.Store.Get(dedupedKey)
assert.Nil(t, err)
assert.NotNil(t, blob)

tr4 := db.NewTrie("test", trie.Root{Hash: root2, Ver: ver2})
v, _, err = tr4.Get(key)
assert.Nil(t, err)
assert.Equal(t, val2, v)

tr5 := db.NewTrie("test", trie.Root{Hash: root3, Ver: ver3})
v, _, err = tr5.Get(key)
assert.Nil(t, err)
assert.Equal(t, val2, v)

db.Close()
}

0 comments on commit 7b883f1

Please sign in to comment.