Skip to content

Commit

Permalink
revise NewWrappedTrieBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Nov 6, 2024
1 parent f571741 commit 0f918fa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 53 deletions.
4 changes: 3 additions & 1 deletion internal/primitives/state-machine/trie_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ func NewTrieBackend[H runtime.Hash, Hasher runtime.Hasher[H]](
// The backend storage and the cache will be taken from other.
func NewWrappedTrieBackend[H runtime.Hash, Hasher runtime.Hasher[H]](
other *TrieBackend[H, Hasher],
cache TrieCacheProvider[H, *cache.TrieCache[H]],
recorder *recorder.Recorder[H],
) *TrieBackend[H, Hasher] {
return NewTrieBackend[H, Hasher](
other.essence.BackendStorage(),
other.essence.root,
other.essence.trieNodeCache,
nil,
recorder,
)
}

Expand Down
85 changes: 37 additions & 48 deletions internal/primitives/state-machine/trie_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ func TestTrieBackend(t *testing.T) {
cache = &local
}
tb := testTrie(t, param.StateVersion, cache, param.Recorder)
tb1 := NewWrappedTrieBackend[hash.H256, runtime.BlakeTwo256](tb)
tb1.essence.recorder = recorder.NewRecorder[hash.H256]()
tb1 := NewWrappedTrieBackend(tb, nil, recorder.NewRecorder[hash.H256]())
proof := tb1.ExtractProof()
require.NotNil(t, proof)
require.True(t, proof.Empty())
Expand All @@ -377,8 +376,7 @@ func TestTrieBackend(t *testing.T) {
cache = &local
}
tb := testTrie(t, param.StateVersion, cache, param.Recorder)
tb1 := NewWrappedTrieBackend[hash.H256, runtime.BlakeTwo256](tb)
tb1.essence.recorder = recorder.NewRecorder[hash.H256]()
tb1 := NewWrappedTrieBackend(tb, nil, recorder.NewRecorder[hash.H256]())

sv, err := tb1.Storage([]byte("key"))
require.NoError(t, err)
Expand All @@ -403,8 +401,7 @@ func TestTrieBackend(t *testing.T) {
cache = &local
}
tb := testTrie(t, param.StateVersion, cache, param.Recorder)
provingBackend := NewWrappedTrieBackend[hash.H256, runtime.BlakeTwo256](tb)
provingBackend.essence.recorder = recorder.NewRecorder[hash.H256]()
provingBackend := NewWrappedTrieBackend(tb, nil, recorder.NewRecorder[hash.H256]())

sv, err := provingBackend.Storage([]byte("key"))
require.NoError(t, err)
Expand Down Expand Up @@ -472,8 +469,7 @@ func TestTrieBackend(t *testing.T) {
}
}

proving := NewWrappedTrieBackend(trie)
proving.essence.recorder = recorder.NewRecorder[hash.H256]()
proving := NewWrappedTrieBackend(trie, nil, recorder.NewRecorder[hash.H256]())
if cache != nil {
local := cache.LocalTrieCache()
proving.essence.trieNodeCache = &local
Expand All @@ -500,14 +496,14 @@ func TestTrieBackend(t *testing.T) {

t.Run("proof_record_works_with_iter", func(t *testing.T) {
for _, stateVersion := range []storage.StateVersion{storage.StateVersionV0, storage.StateVersionV1} {
for _, cache := range []*cache.SharedTrieCache[hash.H256]{cache.NewSharedTrieCache[hash.H256](1024 * 10), nil} {
for _, sharedCache := range []*cache.SharedTrieCache[hash.H256]{cache.NewSharedTrieCache[hash.H256](1024 * 10), nil} {
// Run multiple times to have a different cache conditions.
for i := 0; i < 5; i++ {
if cache != nil {
if sharedCache != nil {
if i == 2 {
cache.ResetNodeCache()
sharedCache.ResetNodeCache()
} else if i == 3 {
cache.ResetValueCache()
sharedCache.ResetValueCache()
}
}

Expand Down Expand Up @@ -536,12 +532,13 @@ func TestTrieBackend(t *testing.T) {
require.Equal(t, []byte{i}, []byte(val))
}

proving := NewWrappedTrieBackend(trie)
proving.essence.recorder = recorder.NewRecorder[hash.H256]()
if cache != nil {
local := cache.LocalTrieCache()
proving.essence.trieNodeCache = &local
var local TrieCacheProvider[hash.H256, *cache.TrieCache[hash.H256]]
if sharedCache != nil {
ltc := sharedCache.LocalTrieCache()
local = &ltc
}
proving := NewWrappedTrieBackend(trie, local, recorder.NewRecorder[hash.H256]())

for i := uint8(0); i < 63; i++ {
sk, err := proving.NextStorageKey([]byte{i})
require.NoError(t, err)
Expand Down Expand Up @@ -610,14 +607,14 @@ func TestTrieBackend(t *testing.T) {
require.Equal(t, []byte{i}, []byte(val))
}

for _, cache := range []*cache.SharedTrieCache[hash.H256]{cache.NewSharedTrieCache[hash.H256](1024 * 10), nil} {
for _, sharedCache := range []*cache.SharedTrieCache[hash.H256]{cache.NewSharedTrieCache[hash.H256](1024 * 10), nil} {
// Run multiple times to have a different cache conditions.
for i := 0; i < 5; i++ {
if cache != nil {
if sharedCache != nil {
if i == 2 {
cache.ResetNodeCache()
sharedCache.ResetNodeCache()
} else if i == 3 {
cache.ResetValueCache()
sharedCache.ResetValueCache()
}
}

Expand All @@ -630,12 +627,12 @@ func TestTrieBackend(t *testing.T) {
require.Equal(t, []byte{i}, []byte(val))
}

proving := NewWrappedTrieBackend(trie)
proving.essence.recorder = recorder.NewRecorder[hash.H256]()
if cache != nil {
local := cache.LocalTrieCache()
proving.essence.trieNodeCache = &local
var local TrieCacheProvider[hash.H256, *cache.TrieCache[hash.H256]]
if sharedCache != nil {
ltc := sharedCache.LocalTrieCache()
local = &ltc
}
proving := NewWrappedTrieBackend(trie, local, recorder.NewRecorder[hash.H256]())
val, err := proving.Storage([]byte{42})
require.NoError(t, err)
require.Equal(t, []byte{42}, []byte(val))
Expand All @@ -658,12 +655,7 @@ func TestTrieBackend(t *testing.T) {
require.NoError(t, err)
require.Nil(t, val)

proving = NewWrappedTrieBackend(trie)
proving.essence.recorder = recorder.NewRecorder[hash.H256]()
if cache != nil {
local := cache.LocalTrieCache()
proving.essence.trieNodeCache = &local
}
proving = NewWrappedTrieBackend(trie, nil, recorder.NewRecorder[hash.H256]())
val, err = proving.ChildStorage(childInfo1, []byte{64})
require.NoError(t, err)
require.Equal(t, []byte{64}, []byte(val))
Expand Down Expand Up @@ -741,8 +733,7 @@ func TestTrieBackend(t *testing.T) {
}
var nodes []hashNode
{
backend := NewWrappedTrieBackend(trie)
backend.essence.recorder = recorder.NewRecorder[hash.H256]()
backend := NewWrappedTrieBackend(trie, nil, recorder.NewRecorder[hash.H256]())
val, err := backend.ChildStorage(childInfo1, []byte{65})
require.NoError(t, err)
require.NotNil(t, val)
Expand Down Expand Up @@ -793,8 +784,7 @@ func TestTrieBackend(t *testing.T) {

{
// Record the access
proving := NewWrappedTrieBackend(trie)
proving.essence.recorder = recorder.NewRecorder[hash.H256]()
proving := NewWrappedTrieBackend(trie, nil, recorder.NewRecorder[hash.H256]())
if cache != nil {
local := cache.LocalTrieCache()
proving.essence.trieNodeCache = &local
Expand Down Expand Up @@ -851,9 +841,7 @@ func TestTrieBackend(t *testing.T) {
}

for n := 0; n < len(keys); n++ {
backend := NewWrappedTrieBackend(tb)
backend.essence.recorder = recorder.NewRecorder[hash.H256]()

backend := NewWrappedTrieBackend(tb, nil, recorder.NewRecorder[hash.H256]())
// Read n keys
for i := 0; i < n; i++ {
_, err := backend.Storage(keys[i])
Expand Down Expand Up @@ -911,27 +899,28 @@ func TestTrieBackend(t *testing.T) {
require.NoError(t, err)
require.Equal(t, childTrie2Val, []byte(val))

for _, cache := range []*cache.SharedTrieCache[hash.H256]{cache.NewSharedTrieCache[hash.H256](1024 * 10), nil} {
for _, sharedCache := range []*cache.SharedTrieCache[hash.H256]{cache.NewSharedTrieCache[hash.H256](1024 * 10), nil} {
// Run multiple times to have a different cache conditions.
for i := 0; i < 5; i++ {
if cache != nil {
if sharedCache != nil {
if i == 2 {
cache.ResetNodeCache()
sharedCache.ResetNodeCache()
} else if i == 3 {
cache.ResetValueCache()
sharedCache.ResetValueCache()
}
}

trie := inMemory.TrieBackend
trieRoot, _ := trie.StorageRoot(nil, stateVersion)
require.Equal(t, inMemoryRoot, trieRoot)

proving := NewWrappedTrieBackend(trie)
proving.essence.recorder = recorder.NewRecorder[hash.H256]()
if cache != nil {
local := cache.LocalTrieCache()
proving.essence.trieNodeCache = &local
var local TrieCacheProvider[hash.H256, *cache.TrieCache[hash.H256]]
if sharedCache != nil {
ltc := sharedCache.LocalTrieCache()
local = &ltc
}
proving := NewWrappedTrieBackend(trie, local, recorder.NewRecorder[hash.H256]())

val, err := proving.Storage(key)
require.NoError(t, err)
require.Equal(t, topTrieVal, []byte(val))
Expand Down
5 changes: 2 additions & 3 deletions internal/primitives/trie/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_SharedTrieCache(t *testing.T) {

{
cache, unlock := localCache.TrieCache(root)
trie := triedb.NewTrieDB[hash.H256, runtime.BlakeTwo256](
trie := triedb.NewTrieDB(
root, db, triedb.WithCache[hash.H256, runtime.BlakeTwo256](cache))
trie.SetVersion(pkgtrie.V1)

Expand Down Expand Up @@ -135,10 +135,9 @@ func Test_SharedTrieCache(t *testing.T) {
cache.MergeInto(&localCache, newRoot)
unlock()
localCache.Commit()

}

// After the local cache is dropped, all changes should have been merged back to the shared
// After the local cache is committed, all changes should have been merged back to the shared
// cache.
cachedVal, ok := sharedCache.inner.valueCache.lru.Peek(ValueCacheKey[hash.H256]{
StorageRoot: newRoot,
Expand Down
2 changes: 1 addition & 1 deletion internal/primitives/trie/recorder/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestRecorder_TransactionAccessedKeys(t *testing.T) {
trieDB := triedb.NewTrieDB(root, db, triedb.WithRecorder[hash.H256, runtime.BlakeTwo256](trieRecorder))
trieDB.SetVersion(trie.V1)

hash, err := trieDB.GetHash(testData[0].Key)
hash, err := trieDB.GetHash(key)
require.NoError(t, err)
require.NotNil(t, hash)
require.Equal(t, runtime.BlakeTwo256{}.Hash(testData[0].Value), *hash)
Expand Down

0 comments on commit 0f918fa

Please sign in to comment.