/
statedb.go
473 lines (432 loc) · 14.3 KB
/
statedb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright (c) 2020 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package factory
import (
"context"
"fmt"
"strconv"
"sync"
"time"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/iotexproject/go-pkgs/cache"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
"github.com/iotexproject/iotex-core/action/protocol/staking"
"github.com/iotexproject/iotex-core/actpool"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/db/batch"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/prometheustimer"
"github.com/iotexproject/iotex-core/pkg/tracer"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/state"
)
// stateDB implements StateFactory interface, tracks changes to account/contract and batch-commits to DB
type stateDB struct {
mutex sync.RWMutex
currentChainHeight uint64
cfg Config
registry *protocol.Registry
dao db.KVStore // the underlying DB for account/contract storage
timerFactory *prometheustimer.TimerFactory
workingsets cache.LRUCache // lru cache for workingsets
protocolView protocol.View
skipBlockValidationOnPut bool
ps *patchStore
}
// StateDBOption sets stateDB construction parameter
type StateDBOption func(*stateDB, *Config) error
// DefaultPatchOption loads patchs
func DefaultPatchOption() StateDBOption {
return func(sdb *stateDB, cfg *Config) (err error) {
sdb.ps, err = newPatchStore(cfg.Chain.TrieDBPatchFile)
return
}
}
// RegistryStateDBOption sets the registry in state db
func RegistryStateDBOption(reg *protocol.Registry) StateDBOption {
return func(sdb *stateDB, cfg *Config) error {
sdb.registry = reg
return nil
}
}
// SkipBlockValidationStateDBOption skips block validation on PutBlock
func SkipBlockValidationStateDBOption() StateDBOption {
return func(sdb *stateDB, cfg *Config) error {
sdb.skipBlockValidationOnPut = true
return nil
}
}
// DisableWorkingSetCacheOption disable workingset cache
func DisableWorkingSetCacheOption() StateDBOption {
return func(sdb *stateDB, cfg *Config) error {
sdb.workingsets = cache.NewDummyLruCache()
return nil
}
}
// NewStateDB creates a new state db
func NewStateDB(cfg Config, dao db.KVStore, opts ...StateDBOption) (Factory, error) {
sdb := stateDB{
cfg: cfg,
currentChainHeight: 0,
registry: protocol.NewRegistry(),
protocolView: protocol.View{},
workingsets: cache.NewThreadSafeLruCache(int(cfg.Chain.WorkingSetCacheSize)),
dao: dao,
}
for _, opt := range opts {
if err := opt(&sdb, &cfg); err != nil {
log.S().Errorf("Failed to execute state factory creation option %p: %v", opt, err)
return nil, err
}
}
timerFactory, err := prometheustimer.New(
"iotex_statefactory_perf",
"Performance of state factory module",
[]string{"topic", "chainID"},
[]string{"default", strconv.FormatUint(uint64(cfg.Chain.ID), 10)},
)
if err != nil {
log.L().Error("Failed to generate prometheus timer factory.", zap.Error(err))
}
sdb.timerFactory = timerFactory
return &sdb, nil
}
func (sdb *stateDB) Start(ctx context.Context) error {
ctx = protocol.WithRegistry(ctx, sdb.registry)
if err := sdb.dao.Start(ctx); err != nil {
return err
}
// check factory height
h, err := sdb.dao.Get(AccountKVNamespace, []byte(CurrentHeightKey))
switch errors.Cause(err) {
case nil:
sdb.currentChainHeight = byteutil.BytesToUint64(h)
// start all protocols
if sdb.protocolView, err = sdb.registry.StartAll(ctx, sdb); err != nil {
return err
}
case db.ErrNotExist:
sdb.currentChainHeight = 0
if err = sdb.dao.Put(AccountKVNamespace, []byte(CurrentHeightKey), byteutil.Uint64ToBytes(0)); err != nil {
return errors.Wrap(err, "failed to init statedb's height")
}
// start all protocols
if sdb.protocolView, err = sdb.registry.StartAll(ctx, sdb); err != nil {
return err
}
ctx = protocol.WithBlockCtx(
ctx,
protocol.BlockCtx{
BlockHeight: 0,
BlockTimeStamp: time.Unix(sdb.cfg.Genesis.Timestamp, 0),
Producer: sdb.cfg.Chain.ProducerAddress(),
GasLimit: sdb.cfg.Genesis.BlockGasLimitByHeight(0),
})
ctx = protocol.WithFeatureCtx(ctx)
// init the state factory
if err = sdb.createGenesisStates(ctx); err != nil {
return errors.Wrap(err, "failed to create genesis states")
}
default:
return err
}
return nil
}
func (sdb *stateDB) Stop(ctx context.Context) error {
sdb.mutex.Lock()
defer sdb.mutex.Unlock()
sdb.workingsets.Clear()
return sdb.dao.Stop(ctx)
}
// Height returns factory's height
func (sdb *stateDB) Height() (uint64, error) {
sdb.mutex.RLock()
defer sdb.mutex.RUnlock()
height, err := sdb.dao.Get(AccountKVNamespace, []byte(CurrentHeightKey))
if err != nil {
return 0, errors.Wrap(err, "failed to get factory's height from underlying DB")
}
return byteutil.BytesToUint64(height), nil
}
func (sdb *stateDB) newWorkingSet(ctx context.Context, height uint64) (*workingSet, error) {
g := genesis.MustExtractGenesisContext(ctx)
flusher, err := db.NewKVStoreFlusher(
sdb.dao,
batch.NewCachedBatch(),
sdb.flusherOptions(!g.IsEaster(height))...,
)
if err != nil {
return nil, err
}
for _, p := range sdb.ps.Get(height) {
if p.Type == _Delete {
flusher.KVStoreWithBuffer().MustDelete(p.Namespace, p.Key)
} else {
flusher.KVStoreWithBuffer().MustPut(p.Namespace, p.Key, p.Value)
}
}
store := newStateDBWorkingSetStore(sdb.protocolView, flusher, g.IsNewfoundland(height))
if err := store.Start(ctx); err != nil {
return nil, err
}
return newWorkingSet(height, store), nil
}
func (sdb *stateDB) Register(p protocol.Protocol) error {
return p.Register(sdb.registry)
}
func (sdb *stateDB) Validate(ctx context.Context, blk *block.Block) error {
ctx = protocol.WithRegistry(ctx, sdb.registry)
key := generateWorkingSetCacheKey(blk.Header, blk.Header.ProducerAddress())
ws, isExist, err := sdb.getFromWorkingSets(ctx, key)
if err != nil {
return err
}
if !isExist {
if err = ws.ValidateBlock(ctx, blk); err != nil {
return errors.Wrap(err, "failed to validate block with workingset in statedb")
}
sdb.workingsets.Add(key, ws)
}
receipts, err := ws.Receipts()
if err != nil {
return err
}
blk.Receipts = receipts
return nil
}
// NewBlockBuilder returns block builder which hasn't been signed yet
func (sdb *stateDB) NewBlockBuilder(
ctx context.Context,
ap actpool.ActPool,
sign func(action.Envelope) (*action.SealedEnvelope, error),
) (*block.Builder, error) {
ctx = protocol.WithRegistry(ctx, sdb.registry)
sdb.mutex.RLock()
currHeight := sdb.currentChainHeight
sdb.mutex.RUnlock()
ws, err := sdb.newWorkingSet(ctx, currHeight+1)
if err != nil {
return nil, err
}
postSystemActions := make([]*action.SealedEnvelope, 0)
unsignedSystemActions, err := ws.generateSystemActions(ctx)
if err != nil {
return nil, err
}
for _, elp := range unsignedSystemActions {
se, err := sign(elp)
if err != nil {
return nil, err
}
postSystemActions = append(postSystemActions, se)
}
blkBuilder, err := ws.CreateBuilder(ctx, ap, postSystemActions, sdb.cfg.Chain.AllowedBlockGasResidue)
if err != nil {
return nil, err
}
blkCtx := protocol.MustGetBlockCtx(ctx)
key := generateWorkingSetCacheKey(blkBuilder.GetCurrentBlockHeader(), blkCtx.Producer.String())
sdb.workingsets.Add(key, ws)
return blkBuilder, nil
}
// SimulateExecution simulates a running of smart contract operation, this is done off the network since it does not
// cause any state change
func (sdb *stateDB) SimulateExecution(
ctx context.Context,
caller address.Address,
ex *action.Execution,
) ([]byte, *action.Receipt, error) {
ctx, span := tracer.NewSpan(ctx, "stateDB.SimulateExecution")
defer span.End()
sdb.mutex.RLock()
currHeight := sdb.currentChainHeight
sdb.mutex.RUnlock()
ws, err := sdb.newWorkingSet(ctx, currHeight+1)
if err != nil {
return nil, nil, err
}
return evm.SimulateExecution(ctx, ws, caller, ex)
}
// ReadContractStorage reads contract's storage
func (sdb *stateDB) ReadContractStorage(ctx context.Context, contract address.Address, key []byte) ([]byte, error) {
sdb.mutex.RLock()
currHeight := sdb.currentChainHeight
sdb.mutex.RUnlock()
ws, err := sdb.newWorkingSet(ctx, currHeight+1)
if err != nil {
return nil, errors.Wrap(err, "failed to generate working set from state db")
}
return evm.ReadContractStorage(ctx, ws, contract, key)
}
// PutBlock persists all changes in RunActions() into the DB
func (sdb *stateDB) PutBlock(ctx context.Context, blk *block.Block) error {
sdb.mutex.Lock()
timer := sdb.timerFactory.NewTimer("Commit")
sdb.mutex.Unlock()
defer timer.End()
producer := blk.PublicKey().Address()
if producer == nil {
return errors.New("failed to get address")
}
g := genesis.MustExtractGenesisContext(ctx)
ctx = protocol.WithBlockCtx(
protocol.WithRegistry(ctx, sdb.registry),
protocol.BlockCtx{
BlockHeight: blk.Height(),
BlockTimeStamp: blk.Timestamp(),
GasLimit: g.BlockGasLimitByHeight(blk.Height()),
Producer: producer,
},
)
ctx = protocol.WithFeatureCtx(ctx)
key := generateWorkingSetCacheKey(blk.Header, blk.Header.ProducerAddress())
ws, isExist, err := sdb.getFromWorkingSets(ctx, key)
if err != nil {
return err
}
if !isExist {
if !sdb.skipBlockValidationOnPut {
err = ws.ValidateBlock(ctx, blk)
} else {
err = ws.Process(ctx, blk.RunnableActions().Actions())
}
if err != nil {
log.L().Error("Failed to update state.", zap.Error(err))
return err
}
}
sdb.mutex.Lock()
defer sdb.mutex.Unlock()
receipts, err := ws.Receipts()
if err != nil {
return err
}
blk.Receipts = receipts
h, _ := ws.Height()
if sdb.currentChainHeight+1 != h {
// another working set with correct version already committed, do nothing
return fmt.Errorf(
"current state height %d + 1 doesn't match working set height %d",
sdb.currentChainHeight, h,
)
}
if err := ws.Commit(ctx); err != nil {
return err
}
sdb.currentChainHeight = h
return nil
}
func (sdb *stateDB) DeleteTipBlock(_ context.Context, _ *block.Block) error {
return errors.Wrap(ErrNotSupported, "cannot delete tip block from state db")
}
// State returns a confirmed state in the state factory
func (sdb *stateDB) State(s interface{}, opts ...protocol.StateOption) (uint64, error) {
cfg, err := processOptions(opts...)
if err != nil {
return 0, err
}
sdb.mutex.RLock()
defer sdb.mutex.RUnlock()
if cfg.Keys != nil {
return 0, errors.Wrap(ErrNotSupported, "Read state with keys option has not been implemented yet")
}
return sdb.currentChainHeight, sdb.state(cfg.Namespace, cfg.Key, s)
}
// State returns a set of states in the state factory
func (sdb *stateDB) States(opts ...protocol.StateOption) (uint64, state.Iterator, error) {
cfg, err := processOptions(opts...)
if err != nil {
return 0, nil, err
}
sdb.mutex.RLock()
defer sdb.mutex.RUnlock()
if cfg.Key != nil {
return sdb.currentChainHeight, nil, errors.Wrap(ErrNotSupported, "Read states with key option has not been implemented yet")
}
values, err := readStates(sdb.dao, cfg.Namespace, cfg.Keys)
if err != nil {
return 0, nil, err
}
return sdb.currentChainHeight, state.NewIterator(values), nil
}
// StateAtHeight returns a confirmed state at height -- archive mode
func (sdb *stateDB) StateAtHeight(height uint64, s interface{}, opts ...protocol.StateOption) error {
return ErrNotSupported
}
// StatesAtHeight returns a set states in the state factory at height -- archive mode
func (sdb *stateDB) StatesAtHeight(height uint64, opts ...protocol.StateOption) (state.Iterator, error) {
return nil, errors.Wrap(ErrNotSupported, "state db does not support archive mode")
}
// ReadView reads the view
func (sdb *stateDB) ReadView(name string) (interface{}, error) {
return sdb.protocolView.Read(name)
}
//======================================
// private trie constructor functions
//======================================
func (sdb *stateDB) flusherOptions(preEaster bool) []db.KVStoreFlusherOption {
opts := []db.KVStoreFlusherOption{
db.SerializeOption(func(wi *batch.WriteInfo) []byte {
if preEaster {
return wi.SerializeWithoutWriteType()
}
return wi.Serialize()
}),
}
if !preEaster {
return opts
}
return append(
opts,
db.SerializeFilterOption(func(wi *batch.WriteInfo) bool {
return wi.Namespace() == evm.CodeKVNameSpace || wi.Namespace() == staking.CandsMapNS
}),
)
}
func (sdb *stateDB) state(ns string, addr []byte, s interface{}) error {
data, err := sdb.dao.Get(ns, addr)
if err != nil {
if errors.Cause(err) == db.ErrNotExist {
return errors.Wrapf(state.ErrStateNotExist, "state of %x doesn't exist", addr)
}
return errors.Wrapf(err, "error when getting the state of %x", addr)
}
if err := state.Deserialize(s, data); err != nil {
return errors.Wrapf(err, "error when deserializing state data into %T", s)
}
return nil
}
func (sdb *stateDB) createGenesisStates(ctx context.Context) error {
ws, err := sdb.newWorkingSet(ctx, 0)
if err != nil {
return err
}
if err := ws.CreateGenesisStates(ctx); err != nil {
return err
}
return ws.Commit(ctx)
}
// getFromWorkingSets returns (workingset, true) if it exists in a cache, otherwise generates new workingset and return (ws, false)
func (sdb *stateDB) getFromWorkingSets(ctx context.Context, key hash.Hash256) (*workingSet, bool, error) {
if data, ok := sdb.workingsets.Get(key); ok {
if ws, ok := data.(*workingSet); ok {
// if it is already validated, return workingset
return ws, true, nil
}
return nil, false, errors.New("type assertion failed to be WorkingSet")
}
sdb.mutex.RLock()
currHeight := sdb.currentChainHeight
sdb.mutex.RUnlock()
tx, err := sdb.newWorkingSet(ctx, currHeight+1)
return tx, false, err
}