@@ -8,7 +8,6 @@ package chain
88import (
99 "encoding/binary"
1010
11- lru "github.com/hashicorp/golang-lru"
1211 "github.com/pkg/errors"
1312 "github.com/vechain/thor/block"
1413 "github.com/vechain/thor/kv"
@@ -21,14 +20,13 @@ const rootCacheLimit = 2048
2120type ancestorTrie struct {
2221 kv kv.GetPutter
2322 rootsCache * cache
24- trieCache * trieCache
2523}
2624
2725func newAncestorTrie (kv kv.GetPutter ) * ancestorTrie {
2826 rootsCache := newCache (rootCacheLimit , func (key interface {}) (interface {}, error ) {
2927 return loadBlockNumberIndexTrieRoot (kv , key .(thor.Bytes32 ))
3028 })
31- return & ancestorTrie {kv , rootsCache , newTrieCache () }
29+ return & ancestorTrie {kv , rootsCache }
3230}
3331
3432func numberAsKey (num uint32 ) []byte {
@@ -48,7 +46,7 @@ func (at *ancestorTrie) Update(w kv.Putter, id, parentID thor.Bytes32) error {
4846 parentRoot = root .(thor.Bytes32 )
4947 }
5048
51- tr , err := at . trieCache . Get (parentRoot , at .kv , true )
49+ tr , err := trie . New (parentRoot , at .kv )
5250 if err != nil {
5351 return err
5452 }
@@ -64,7 +62,7 @@ func (at *ancestorTrie) Update(w kv.Putter, id, parentID thor.Bytes32) error {
6462 if err := saveBlockNumberIndexTrieRoot (w , id , root ); err != nil {
6563 return err
6664 }
67- at . trieCache . Add ( root , tr , at . kv )
65+
6866 at .rootsCache .Add (id , root )
6967 return nil
7068}
@@ -81,7 +79,7 @@ func (at *ancestorTrie) GetAncestor(descendantID thor.Bytes32, ancestorNum uint3
8179 if err != nil {
8280 return thor.Bytes32 {}, errors .WithMessage (err , "load index root" )
8381 }
84- tr , err := at . trieCache . Get (root .(thor.Bytes32 ), at .kv , false )
82+ tr , err := trie . New (root .(thor.Bytes32 ), at .kv )
8583 if err != nil {
8684 return thor.Bytes32 {}, err
8785 }
@@ -92,49 +90,3 @@ func (at *ancestorTrie) GetAncestor(descendantID thor.Bytes32, ancestorNum uint3
9290 }
9391 return thor .BytesToBytes32 (id ), nil
9492}
95-
96- ///
97- type trieCache struct {
98- cache * lru.Cache
99- }
100-
101- type trieCacheEntry struct {
102- trie * trie.Trie
103- kv kv.GetPutter
104- }
105-
106- func newTrieCache () * trieCache {
107- cache , _ := lru .New (16 )
108- return & trieCache {cache : cache }
109- }
110-
111- // to get a trie for writing, copy should be set to true
112- func (tc * trieCache ) Get (root thor.Bytes32 , kv kv.GetPutter , copy bool ) (* trie.Trie , error ) {
113-
114- if v , ok := tc .cache .Get (root ); ok {
115- entry := v .(* trieCacheEntry )
116- if entry .kv == kv {
117- if copy {
118- cpy := * entry .trie
119- return & cpy , nil
120- }
121- return entry .trie , nil
122- }
123- }
124- tr , err := trie .New (root , kv )
125- if err != nil {
126- return nil , err
127- }
128- tr .SetCacheLimit (16 )
129- tc .cache .Add (root , & trieCacheEntry {tr , kv })
130- if copy {
131- cpy := * tr
132- return & cpy , nil
133- }
134- return tr , nil
135- }
136-
137- func (tc * trieCache ) Add (root thor.Bytes32 , trie * trie.Trie , kv kv.GetPutter ) {
138- cpy := * trie
139- tc .cache .Add (root , & trieCacheEntry {& cpy , kv })
140- }
0 commit comments