From b57f6cb7053ab80797c4a4d69884439e503497ca Mon Sep 17 00:00:00 2001 From: Timothy Wu Date: Wed, 6 Nov 2024 11:26:31 -0500 Subject: [PATCH] revise NewWrappedTrieBackend --- .../primitives/state-machine/trie_backend.go | 4 +- .../state-machine/trie_backend_test.go | 85 ++++++++----------- internal/primitives/trie/cache/cache_test.go | 5 +- .../primitives/trie/recorder/recorder_test.go | 2 +- 4 files changed, 43 insertions(+), 53 deletions(-) diff --git a/internal/primitives/state-machine/trie_backend.go b/internal/primitives/state-machine/trie_backend.go index 57961fd85f..fb0b20d1ea 100644 --- a/internal/primitives/state-machine/trie_backend.go +++ b/internal/primitives/state-machine/trie_backend.go @@ -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, ) } diff --git a/internal/primitives/state-machine/trie_backend_test.go b/internal/primitives/state-machine/trie_backend_test.go index 2d4dfeac9a..16a5251dae 100644 --- a/internal/primitives/state-machine/trie_backend_test.go +++ b/internal/primitives/state-machine/trie_backend_test.go @@ -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()) @@ -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) @@ -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) @@ -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 @@ -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() } } @@ -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 = <c } + 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) @@ -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() } } @@ -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 = <c } + 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)) @@ -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)) @@ -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) @@ -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 @@ -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]) @@ -911,14 +899,14 @@ 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() } } @@ -926,12 +914,13 @@ func TestTrieBackend(t *testing.T) { 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 = <c } + proving := NewWrappedTrieBackend(trie, local, recorder.NewRecorder[hash.H256]()) + val, err := proving.Storage(key) require.NoError(t, err) require.Equal(t, topTrieVal, []byte(val)) diff --git a/internal/primitives/trie/cache/cache_test.go b/internal/primitives/trie/cache/cache_test.go index 5b9314e679..59c4ab1151 100644 --- a/internal/primitives/trie/cache/cache_test.go +++ b/internal/primitives/trie/cache/cache_test.go @@ -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) @@ -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, diff --git a/internal/primitives/trie/recorder/recorder_test.go b/internal/primitives/trie/recorder/recorder_test.go index 010e3aaff8..7c66a7b92a 100644 --- a/internal/primitives/trie/recorder/recorder_test.go +++ b/internal/primitives/trie/recorder/recorder_test.go @@ -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)