@@ -20,8 +20,6 @@ import (
20
20
21
21
"github.com/iotexproject/iotex-core/v2/action"
22
22
"github.com/iotexproject/iotex-core/v2/action/protocol"
23
- "github.com/iotexproject/iotex-core/v2/action/protocol/execution/evm"
24
- "github.com/iotexproject/iotex-core/v2/action/protocol/staking"
25
23
"github.com/iotexproject/iotex-core/v2/actpool"
26
24
"github.com/iotexproject/iotex-core/v2/blockchain/block"
27
25
"github.com/iotexproject/iotex-core/v2/blockchain/genesis"
40
38
atHeight (uint64 ) db.KVStore
41
39
getHeight () (uint64 , error )
42
40
putHeight (uint64 ) error
41
+ metadataNS () string
42
+ flusherOptions (bool ) []db.KVStoreFlusherOption
43
43
}
44
44
// stateDB implements StateFactory interface, tracks changes to account/contract and batch-commits to DB
45
45
stateDB struct {
53
53
protocolView protocol.View
54
54
skipBlockValidationOnPut bool
55
55
ps * patchStore
56
+ metaNS string // metadata namespace, only meaningful when archive-mode enabled
56
57
}
57
58
)
58
59
@@ -91,6 +92,14 @@ func DisableWorkingSetCacheOption() StateDBOption {
91
92
}
92
93
}
93
94
95
+ // MetadataNamespaceOption specifies the metadat namespace for versioned DB
96
+ func MetadataNamespaceOption (ns string ) StateDBOption {
97
+ return func (sdb * stateDB , cfg * Config ) error {
98
+ sdb .metaNS = ns
99
+ return nil
100
+ }
101
+ }
102
+
94
103
// NewStateDB creates a new state db
95
104
func NewStateDB (cfg Config , dao db.KVStore , opts ... StateDBOption ) (Factory , error ) {
96
105
sdb := stateDB {
@@ -106,7 +115,15 @@ func NewStateDB(cfg Config, dao db.KVStore, opts ...StateDBOption) (Factory, err
106
115
return nil , err
107
116
}
108
117
}
109
- sdb .dao = newDaoRetrofitter (dao )
118
+ if cfg .Chain .EnableArchiveMode {
119
+ daoVersioned , ok := dao .(db.KvVersioned )
120
+ if ! ok {
121
+ return nil , errors .Wrap (ErrNotSupported , "cannot enable archive mode StateDB with non-versioned DB" )
122
+ }
123
+ sdb .dao = newDaoRetrofitterArchive (daoVersioned , sdb .metaNS )
124
+ } else {
125
+ sdb .dao = newDaoRetrofitter (dao )
126
+ }
110
127
timerFactory , err := prometheustimer .New (
111
128
"iotex_statefactory_perf" ,
112
129
"Performance of state factory module" ,
@@ -181,7 +198,7 @@ func (sdb *stateDB) newWorkingSet(ctx context.Context, height uint64) (*workingS
181
198
flusher , err := db .NewKVStoreFlusher (
182
199
sdb .dao .atHeight (height ),
183
200
batch .NewCachedBatch (),
184
- sdb .flusherOptions (! g .IsEaster (height ))... ,
201
+ sdb .dao . flusherOptions (! g .IsEaster (height ))... ,
185
202
)
186
203
if err != nil {
187
204
return nil , err
@@ -193,11 +210,10 @@ func (sdb *stateDB) newWorkingSet(ctx context.Context, height uint64) (*workingS
193
210
flusher .KVStoreWithBuffer ().MustPut (p .Namespace , p .Key , p .Value )
194
211
}
195
212
}
196
- store := newStateDBWorkingSetStore (sdb .protocolView , flusher , g .IsNewfoundland (height ))
213
+ store := newStateDBWorkingSetStore (sdb .protocolView , flusher , g .IsNewfoundland (height ), sdb . dao . metadataNS () )
197
214
if err := store .Start (ctx ); err != nil {
198
215
return nil , err
199
216
}
200
-
201
217
return newWorkingSet (height , store ), nil
202
218
}
203
219
@@ -271,7 +287,6 @@ func (sdb *stateDB) WorkingSet(ctx context.Context) (protocol.StateManager, erro
271
287
}
272
288
273
289
func (sdb * stateDB ) WorkingSetAtHeight (ctx context.Context , height uint64 ) (protocol.StateManager , error ) {
274
- // TODO: implement archive mode
275
290
return sdb .newWorkingSet (ctx , height )
276
291
}
277
292
@@ -368,29 +383,9 @@ func (sdb *stateDB) ReadView(name string) (interface{}, error) {
368
383
}
369
384
370
385
//======================================
371
- // private trie constructor functions
386
+ // private statedb functions
372
387
//======================================
373
388
374
- func (sdb * stateDB ) flusherOptions (preEaster bool ) []db.KVStoreFlusherOption {
375
- opts := []db.KVStoreFlusherOption {
376
- db .SerializeOption (func (wi * batch.WriteInfo ) []byte {
377
- if preEaster {
378
- return wi .SerializeWithoutWriteType ()
379
- }
380
- return wi .Serialize ()
381
- }),
382
- }
383
- if ! preEaster {
384
- return opts
385
- }
386
- return append (
387
- opts ,
388
- db .SerializeFilterOption (func (wi * batch.WriteInfo ) bool {
389
- return wi .Namespace () == evm .CodeKVNameSpace || wi .Namespace () == staking .CandsMapNS
390
- }),
391
- )
392
- }
393
-
394
389
func (sdb * stateDB ) state (h uint64 , ns string , addr []byte , s interface {}) error {
395
390
data , err := sdb .dao .atHeight (h ).Get (ns , addr )
396
391
if err != nil {
0 commit comments