@@ -13,19 +13,32 @@ import (
1313
1414var _ database.TrieDB = (* Database )(nil )
1515
16- type Config struct {}
16+ type Config struct {
17+ CleanCacheSize uint64 // Maximum size (in bytes) for caching clean nodes
18+ }
1719
1820type Database struct {
1921 disk db.KeyValueStore
2022
2123 lock sync.RWMutex
2224 log utils.StructuredLogger
25+
26+ config Config
27+ cleanCache * cleanCache
2328}
2429
25- func New (disk db.KeyValueStore ) * Database {
30+ func New (disk db.KeyValueStore , config * Config ) * Database {
31+ if config == nil {
32+ config = & Config {
33+ CleanCacheSize : 16 * utils .Megabyte ,
34+ }
35+ }
36+ cleanCache := newCleanCache (config .CleanCacheSize )
2637 return & Database {
27- disk : disk ,
28- log : utils .NewNopZapLogger (),
38+ disk : disk ,
39+ config : * config ,
40+ cleanCache : & cleanCache ,
41+ log : utils .NewNopZapLogger (),
2942 }
3043}
3144
@@ -37,11 +50,19 @@ func (d *Database) readNode(
3750) ([]byte , error ) {
3851 d .lock .RLock ()
3952 defer d .lock .RUnlock ()
53+
54+ isClass := id .Type () == trieutils .Class
55+ blob := d .cleanCache .getNode (owner , path , isClass )
56+ if blob != nil {
57+ return blob , nil
58+ }
59+
4060 blob , err := trieutils .GetNodeByPath (d .disk , id .Bucket (), owner , path , isLeaf )
4161 if err != nil {
4262 return nil , err
4363 }
4464
65+ d .cleanCache .putNode (owner , path , isClass , blob )
4566 return blob , nil
4667}
4768
@@ -79,29 +100,26 @@ func (d *Database) Update(
79100 }
80101
81102 for path , n := range classNodes {
82- err := d .updateNode (batch , db .ClassTrie , & felt.Address {}, & path , n )
83- if err != nil {
103+ if err := d .updateNode (batch , db .ClassTrie , & felt .Zero , & path , n , true ); err != nil {
84104 return err
85105 }
86106 }
87107
88108 for path , n := range contractNodes {
89- err := d .updateNode (batch , db .ContractTrieContract , & felt.Address {}, & path , n )
90- if err != nil {
109+ if err := d .updateNode (batch , db .ContractTrieContract , & felt .Zero , & path , n , false ); err != nil {
91110 return err
92111 }
93112 }
94113
95114 for owner , nodes := range contractStorageNodes {
96115 for path , n := range nodes {
97- err := d .updateNode (batch , db .ContractTrieStorage , & owner , & path , n )
98- if err != nil {
116+ if err := d .updateNode (batch , db .ContractTrieStorage , & owner , & path , n , false ); err != nil {
99117 return err
100118 }
101119 }
102120 }
103121
104- return batch . Write ()
122+ return nil
105123}
106124
107125func (d * Database ) updateNode (
@@ -110,9 +128,25 @@ func (d *Database) updateNode(
110128 owner * felt.Address ,
111129 path * trieutils.Path ,
112130 n trienode.TrieNode ,
131+ isClass bool ,
113132) error {
114133 if _ , deleted := n .(* trienode.DeletedNode ); deleted {
115- return trieutils .DeleteNodeByPath (batch , bucket , owner , path , n .IsLeaf ())
134+ if err := trieutils .DeleteNodeByPath (batch , bucket , owner , path , n .IsLeaf ()); err != nil {
135+ return err
136+ }
137+ d .cleanCache .deleteNode (owner , path , isClass )
138+ } else {
139+ if err := trieutils .WriteNodeByPath (
140+ batch ,
141+ bucket ,
142+ owner ,
143+ path ,
144+ n .IsLeaf (),
145+ n .Blob (),
146+ ); err != nil {
147+ return err
148+ }
149+ d .cleanCache .putNode (owner , path , isClass , n .Blob ())
116150 }
117151 return trieutils .WriteNodeByPath (
118152 batch ,
0 commit comments