/
rawdb_delta.go
73 lines (63 loc) · 1.44 KB
/
rawdb_delta.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
package mpt
import (
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/trie"
"sync"
)
var (
rawdbDeltaInstance *rawdbDelta
rawdbOnce sync.Once
)
func GetRawDBDeltaInstance() *rawdbDelta {
rawdbOnce.Do(func() {
rawdbDeltaInstance = newRawdbDelta()
})
return rawdbDeltaInstance
}
type rawdbDelta struct {
sync.RWMutex
Codes map[ethcmn.Hash][]byte
}
func newRawdbDelta() *rawdbDelta {
return &rawdbDelta{
Codes: make(map[ethcmn.Hash][]byte),
}
}
func (delta *rawdbDelta) SetCode(hash ethcmn.Hash, code []byte) {
if !produceDelta {
return
}
delta.Lock()
defer delta.Unlock()
delta.Codes[hash] = code
}
func (delta *rawdbDelta) reset() {
delta.Codes = make(map[ethcmn.Hash][]byte)
}
func (delta *rawdbDelta) fillCodeDelta(mptDelta *trie.MptDelta) {
if !produceDelta {
return
}
delta.RLock()
defer delta.RUnlock()
mptDelta.CodeKV = make([]*trie.DeltaKV, 0, len(delta.Codes))
for k, v := range delta.Codes {
mptDelta.CodeKV = append(mptDelta.CodeKV, &trie.DeltaKV{k.Bytes(), v})
}
}
func (ms *MptStore) applyRawDBDelta(delta []*trie.DeltaKV) {
if !applyDelta {
return
}
codeWriter := ms.db.TrieDB().DiskDB().NewBatch()
defer codeWriter.Reset()
for _, code := range delta {
rawdb.WriteCode(codeWriter, ethcmn.BytesToHash(code.Key), code.Val)
}
if codeWriter.ValueSize() > 0 {
if err := codeWriter.Write(); err != nil {
panic(err)
}
}
}