-
Notifications
You must be signed in to change notification settings - Fork 37
/
statedb.go
475 lines (397 loc) · 11.8 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
474
475
// Copyright 2018 The Fractal Team Authors
// This file is part of the fractal project.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package state
import (
"errors"
"fmt"
"sort"
"sync"
"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/rawdb"
"github.com/fractalplatform/fractal/types"
"github.com/fractalplatform/fractal/utils/fdb"
)
type revision struct {
id int
journalIndex int
}
var (
statePrefix = "ST"
acctDataPrefix = "AD"
linkSymbol = "*"
)
// StateDB store block operate info
type StateDB struct {
db Database
trie Trie
readSet map[string][]byte // save old/unmodified data
writeSet map[string][]byte // last modify data
dirtySet map[string]struct{} // writeSet which key is modified
dbErr error
refund uint64 // unuse gas
thash, bhash common.Hash // current transaction hash and current block hash
txIndex int // transaction index in block
logs map[common.Hash][]*types.Log
logSize uint
preimages map[common.Hash][]byte
journal *journal
validRevisions []revision
nextRevisionID int
stateTrace bool // replay transaction, true is replayed , false is not replayed
lock sync.Mutex
}
//New func generate a statedb object
//parentHash: block's parent hash, db: cachedb
func New(root common.Hash, db Database) (*StateDB, error) {
tr, err := db.OpenTrie(root)
if err != nil {
return nil, err
}
return &StateDB{
db: db,
trie: tr,
readSet: make(map[string][]byte),
writeSet: make(map[string][]byte),
dirtySet: make(map[string]struct{}),
logs: make(map[common.Hash][]*types.Log),
preimages: make(map[common.Hash][]byte),
journal: newJournal(),
stateTrace: false}, nil
}
//setError only save first err
func (s *StateDB) setError(err error) {
if s.dbErr == nil {
s.dbErr = err
}
}
// Error return error info
func (s *StateDB) Error() error {
return s.dbErr
}
// Reset clear StateDB
func (s *StateDB) Reset(root common.Hash) error {
tr, err := s.db.OpenTrie(root)
if err != nil {
return err
}
s.trie = tr
s.readSet = make(map[string][]byte)
s.writeSet = make(map[string][]byte)
s.dirtySet = make(map[string]struct{})
s.thash = common.Hash{}
s.bhash = common.Hash{}
s.txIndex = 0
s.logs = make(map[common.Hash][]*types.Log)
s.logSize = 0
s.preimages = make(map[common.Hash][]byte)
s.dbErr = nil
s.clearJournalAndRefund()
return nil
}
// AddLog save transaction log
func (s *StateDB) AddLog(log *types.Log) {
s.journal.append(addLogChange{txhash: s.thash})
log.TxHash = s.thash
log.BlockHash = s.bhash
log.TxIndex = uint(s.txIndex)
log.Index = s.logSize
s.logs[s.thash] = append(s.logs[s.thash], log)
s.logSize++
}
// GetLogs get a strip of transaction log
func (s *StateDB) GetLogs(hash common.Hash) []*types.Log {
return s.logs[hash]
}
// Logs get all transaction log
func (s *StateDB) Logs() []*types.Log {
var logs []*types.Log
for _, lgs := range s.logs {
logs = append(logs, lgs...)
}
return logs
}
// AddPreimage hash is preimageHash
func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
if _, ok := s.preimages[hash]; !ok {
s.journal.append(addPreimageChange{hash: hash})
pi := make([]byte, len(preimage))
copy(pi, preimage)
s.preimages[hash] = pi
}
}
// Preimages return preimages
func (s *StateDB) Preimages() map[common.Hash][]byte {
return s.preimages
}
// save unuse gas
func (s *StateDB) AddRefund(gas uint64) {
s.journal.append(refundChange{prev: s.refund})
s.refund += gas
}
func (s *StateDB) GetRefund() uint64 {
return s.refund
}
func (s *StateDB) GetState(account string, key common.Hash) common.Hash {
optKey := statePrefix + linkSymbol + account + linkSymbol + key.String()
value, _ := s.get(optKey)
if (value == nil) || (len(value) != common.HashLength) {
return common.Hash{}
}
return common.BytesToHash(value)
}
// set contract variable key value
func (s *StateDB) SetState(account string, key, value common.Hash) {
optKey := statePrefix + linkSymbol + account + linkSymbol + key.String()
s.put(optKey, value[:])
}
// set writeSet
func (s *StateDB) set(key string, value []byte) {
if value == nil {
s.writeSet[key] = nil
} else {
valueCopy := make([]byte, len(value))
copy(valueCopy, value)
s.writeSet[key] = valueCopy
}
}
func (s *StateDB) put(key string, value []byte) {
oldValue, _ := s.get(key)
s.journal.append(stateChange{key: &key,
prevalue: oldValue})
s.set(key, value)
}
//get return nil when key not exsit
func (s *StateDB) get(key string) ([]byte, error) {
if value, exsit := s.writeSet[key]; exsit {
return common.CopyBytes(value), nil
}
// replay transaction
if s.stateTrace {
errInfo := fmt.Sprintf("No value when trace.")
err := errors.New(errInfo)
s.setError(err)
return nil, err
}
value, err := s.trie.TryGet([]byte(key))
if len(value) == 0 {
s.setError(err)
return nil, err
}
s.readSet[key] = common.CopyBytes(value)
s.writeSet[key] = common.CopyBytes(value)
return common.CopyBytes(value), nil
}
//RpcGetState provide get value of the key to rpc
//when called please RLock cachedb
func (s *StateDB) RpcGetState(account string, key common.Hash) common.Hash {
optKey := statePrefix + linkSymbol + account + linkSymbol + key.String()
value, err := s.trie.TryGet([]byte(optKey))
if len(value) == 0 {
s.setError(err)
return common.Hash{}
}
return common.BytesToHash(value)
}
//RpcGet provide get value of the key to rpc
//when called please RLock cachedb
func (s *StateDB) RpcGet(account string, key string) ([]byte, error) {
optKey := acctDataPrefix + linkSymbol + account + linkSymbol + key
value, err := s.trie.TryGet([]byte(optKey))
if len(value) == 0 {
s.setError(err)
return nil, err
}
return common.CopyBytes(value), nil
}
func (s *StateDB) Database() Database {
return s.db
}
func (s *StateDB) Copy() *StateDB {
s.lock.Lock()
defer s.lock.Unlock()
state := &StateDB{
db: s.db,
trie: s.trie,
readSet: make(map[string][]byte, len(s.writeSet)),
writeSet: make(map[string][]byte, len(s.writeSet)),
dirtySet: make(map[string]struct{}, len(s.dirtySet)),
refund: s.refund,
logs: make(map[common.Hash][]*types.Log, len(s.logs)),
logSize: s.logSize,
preimages: make(map[common.Hash][]byte),
journal: newJournal()}
for key := range s.journal.dirties {
value := s.writeSet[key]
state.readSet[key] = common.CopyBytes(value)
state.writeSet[key] = common.CopyBytes(value)
}
for hash, logs := range s.logs {
state.logs[hash] = make([]*types.Log, len(logs))
copy(state.logs[hash], logs)
}
for hash, preimage := range s.preimages {
state.preimages[hash] = preimage
}
return state
}
func (s *StateDB) Snapshot() int {
id := s.nextRevisionID
s.nextRevisionID++
s.validRevisions = append(s.validRevisions, revision{id, s.journal.length()})
return id
}
func (s *StateDB) RevertToSnapshot(revid int) {
idx := sort.Search(len(s.validRevisions), func(i int) bool {
return s.validRevisions[i].id >= revid
})
if idx == len(s.validRevisions) || s.validRevisions[idx].id != revid {
panic(fmt.Errorf("revision id %v cannot be reverted", revid))
}
snapshot := s.validRevisions[idx].journalIndex
s.journal.revert(s, snapshot)
s.validRevisions = s.validRevisions[:idx]
}
//Put account's data to db
func (s *StateDB) Put(account string, key string, value []byte) {
optKey := acctDataPrefix + linkSymbol + account + linkSymbol + key
s.put(optKey, value)
}
//Get account's data from db
func (s *StateDB) Get(account string, key string) ([]byte, error) {
optKey := acctDataPrefix + linkSymbol + account + linkSymbol + key
return s.get(optKey)
}
//Delete account's data from db
func (s *StateDB) Delete(account string, key string) {
optKey := acctDataPrefix + linkSymbol + account + linkSymbol + key
s.put(optKey, nil)
}
// ReceiptRoot compute one tx‘ receipt hash
func (s *StateDB) ReceiptRoot() common.Hash {
s.Finalise()
return s.trie.Hash()
}
func (s *StateDB) IntermediateRoot() common.Hash {
s.Finalise()
return s.trie.Hash()
}
// execute transaction called
func (s *StateDB) Prepare(thash, bhash common.Hash, ti int) {
s.thash = thash
s.bhash = bhash
s.txIndex = ti
}
func (s *StateDB) clearJournalAndRefund() {
s.journal = newJournal()
s.validRevisions = s.validRevisions[:0]
s.refund = 0
}
func (s *StateDB) Finalise() {
for key := range s.journal.dirties {
s.dirtySet[key] = struct{}{}
}
for key := range s.dirtySet {
value, exsit := s.writeSet[key]
if !exsit {
panic("WriteSet is invalid when commit")
}
//update the value to trie
if value != nil {
s.trie.TryUpdate([]byte(key), value)
} else {
s.trie.TryDelete([]byte(key))
}
delete(s.dirtySet, key)
}
s.clearJournalAndRefund()
}
// commit call, save state change record
func (s *StateDB) genBlockStateOut(parentHash, blockHash common.Hash, blockNum uint64) *types.StateOut {
stateOut := &types.StateOut{
ParentHash: parentHash,
Number: blockNum,
Hash: blockHash,
ReadSet: make([]*types.KvNode, 0, len(s.readSet)),
}
// replay
for key, value := range s.readSet {
stateOut.ReadSet = append(stateOut.ReadSet,
&types.KvNode{Key: key, Value: common.CopyBytes(value)})
}
return stateOut
}
//Commit the block state to db. after success please call commitcache
//batch: batch to db
//blockHash: the hash of commit block
func (s *StateDB) Commit(batch fdb.Batch, blockHash common.Hash, blockNum uint64) (common.Hash, error) {
defer s.clearJournalAndRefund()
var parentHash common.Hash
s.Finalise()
if s.Error() != nil {
return common.Hash{}, errors.New("DB error when commit")
}
db := s.db.GetDB()
if blockNum == 0 {
parentHash = common.Hash{}
} else {
parentHash = rawdb.ReadCanonicalHash(db, blockNum-1)
}
//generate revert and write to db
stateOut := s.genBlockStateOut(parentHash, blockHash, blockNum)
rawdb.WriteBlockStateOut(batch, blockHash, stateOut)
rawdb.WriteOptBlockHash(batch, blockHash)
root, err := s.trie.Commit(nil)
return root, err
}
//TransToSpecBlock change block state (from->to)
func TransToSpecBlock(db fdb.Database, cache Database, from common.Hash, to common.Hash) error {
batch := db.NewBatch()
rollState := rawdb.ReadBlockStateOut(db, from)
fwdState := rawdb.ReadBlockStateOut(db, to)
if rollState == nil || fwdState == nil {
err := fmt.Errorf("from or to's stateout not exsit, from:%x to:%x", from, to)
return err
}
for rollState.Number > fwdState.Number {
rawdb.DeleteBlockStateOut(batch, rollState.Hash)
rollState = rawdb.ReadBlockStateOut(db, rollState.ParentHash)
}
err := batch.Write()
return err
}
//TraceNew get state of special block hash for trace
//blockHash: the hash of block
func TraceNew(blockHash common.Hash, cache Database) (*StateDB, error) {
db := cache.GetDB()
stateOut := rawdb.ReadBlockStateOut(db, blockHash)
if stateOut == nil {
err := fmt.Errorf("TraceNew blockHash error, hash:%x", blockHash)
return nil, err
}
stateDb := &StateDB{
db: cache,
readSet: make(map[string][]byte),
writeSet: make(map[string][]byte),
dirtySet: make(map[string]struct{}),
logs: make(map[common.Hash][]*types.Log),
preimages: make(map[common.Hash][]byte),
journal: newJournal(),
stateTrace: true}
for _, node := range stateOut.ReadSet {
stateDb.writeSet[node.Key] = common.CopyBytes(node.Value)
}
return stateDb, nil
}