forked from OffchainLabs/go-ethereum
/
recordingdb.go
194 lines (170 loc) · 5.6 KB
/
recordingdb.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
package arbitrum
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"github.com/MinseokOh/arbitrum/common"
"github.com/MinseokOh/arbitrum/consensus"
"github.com/MinseokOh/arbitrum/core"
"github.com/MinseokOh/arbitrum/core/rawdb"
"github.com/MinseokOh/arbitrum/core/state"
"github.com/MinseokOh/arbitrum/core/types"
"github.com/MinseokOh/arbitrum/crypto"
"github.com/MinseokOh/arbitrum/ethdb"
"github.com/MinseokOh/arbitrum/log"
"github.com/MinseokOh/arbitrum/rlp"
"github.com/MinseokOh/arbitrum/trie"
)
type RecordingKV struct {
inner *trie.Database
readDbEntries map[common.Hash][]byte
enableBypass bool
}
func NewRecordingKV(inner *trie.Database) *RecordingKV {
return &RecordingKV{inner, make(map[common.Hash][]byte), false}
}
func (db *RecordingKV) Has(key []byte) (bool, error) {
return false, errors.New("recording KV doesn't support Has")
}
func (db *RecordingKV) Get(key []byte) ([]byte, error) {
var hash common.Hash
var res []byte
var err error
if len(key) == 32 {
copy(hash[:], key)
res, err = db.inner.Node(hash)
} else if len(key) == len(rawdb.CodePrefix)+32 && bytes.HasPrefix(key, rawdb.CodePrefix) {
// Retrieving code
copy(hash[:], key[len(rawdb.CodePrefix):])
res, err = db.inner.DiskDB().Get(key)
} else {
err = fmt.Errorf("recording KV attempted to access non-hash key %v", hex.EncodeToString(key))
}
if err != nil {
return nil, err
}
if db.enableBypass {
return res, nil
}
if crypto.Keccak256Hash(res) != hash {
return nil, fmt.Errorf("recording KV attempted to access non-hash key %v", hash)
}
db.readDbEntries[hash] = res
return res, nil
}
func (db *RecordingKV) Put(key []byte, value []byte) error {
return errors.New("recording KV doesn't support Put")
}
func (db *RecordingKV) Delete(key []byte) error {
return errors.New("recording KV doesn't support Delete")
}
func (db *RecordingKV) NewBatch() ethdb.Batch {
if db.enableBypass {
return db.inner.DiskDB().NewBatch()
}
log.Error("recording KV: attempted to create batch when bypass not enabled")
return nil
}
func (db *RecordingKV) NewBatchWithSize(size int) ethdb.Batch {
if db.enableBypass {
return db.inner.DiskDB().NewBatchWithSize(size)
}
log.Error("recording KV: attempted to create batch when bypass not enabled")
return nil
}
func (db *RecordingKV) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
if db.enableBypass {
return db.inner.DiskDB().NewIterator(prefix, start)
}
log.Error("recording KV: attempted to create iterator when bypass not enabled")
return nil
}
func (db *RecordingKV) NewSnapshot() (ethdb.Snapshot, error) {
// This is fine as RecordingKV doesn't support mutation
return db, nil
}
func (db *RecordingKV) Stat(property string) (string, error) {
return "", errors.New("recording KV doesn't support Stat")
}
func (db *RecordingKV) Compact(start []byte, limit []byte) error {
return nil
}
func (db *RecordingKV) Close() error {
return nil
}
func (db *RecordingKV) Release() {
return
}
func (db *RecordingKV) GetRecordedEntries() map[common.Hash][]byte {
return db.readDbEntries
}
func (db *RecordingKV) EnableBypass() {
db.enableBypass = true
}
type RecordingChainContext struct {
bc core.ChainContext
minBlockNumberAccessed uint64
initialBlockNumber uint64
}
func NewRecordingChainContext(inner core.ChainContext, blocknumber uint64) *RecordingChainContext {
return &RecordingChainContext{
bc: inner,
minBlockNumberAccessed: blocknumber,
initialBlockNumber: blocknumber,
}
}
func (r *RecordingChainContext) Engine() consensus.Engine {
return r.bc.Engine()
}
func (r *RecordingChainContext) GetHeader(hash common.Hash, num uint64) *types.Header {
if num < r.minBlockNumberAccessed {
r.minBlockNumberAccessed = num
}
return r.bc.GetHeader(hash, num)
}
func (r *RecordingChainContext) GetMinBlockNumberAccessed() uint64 {
return r.minBlockNumberAccessed
}
func PrepareRecording(blockchain *core.BlockChain, lastBlockHeader *types.Header) (*state.StateDB, core.ChainContext, *RecordingKV, error) {
rawTrie := blockchain.StateCache().TrieDB()
recordingKeyValue := NewRecordingKV(rawTrie)
recordingStateDatabase := state.NewDatabase(rawdb.NewDatabase(recordingKeyValue))
var prevRoot common.Hash
if lastBlockHeader != nil {
prevRoot = lastBlockHeader.Root
}
recordingStateDb, err := state.New(prevRoot, recordingStateDatabase, nil)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create recordingStateDb: %w", err)
}
var recordingChainContext *RecordingChainContext
if lastBlockHeader != nil {
if !lastBlockHeader.Number.IsUint64() {
return nil, nil, nil, errors.New("block number not uint64")
}
recordingChainContext = NewRecordingChainContext(blockchain, lastBlockHeader.Number.Uint64())
}
return recordingStateDb, recordingChainContext, recordingKeyValue, nil
}
func PreimagesFromRecording(chainContextIf core.ChainContext, recordingDb *RecordingKV) (map[common.Hash][]byte, error) {
entries := recordingDb.GetRecordedEntries()
recordingChainContext, ok := chainContextIf.(*RecordingChainContext)
if (recordingChainContext == nil) || (!ok) {
return nil, errors.New("recordingChainContext invalid")
}
blockchain, ok := recordingChainContext.bc.(*core.BlockChain)
if (blockchain == nil) || (!ok) {
return nil, errors.New("blockchain invalid")
}
for i := recordingChainContext.GetMinBlockNumberAccessed(); i <= recordingChainContext.initialBlockNumber; i++ {
header := blockchain.GetHeaderByNumber(i)
hash := header.Hash()
bytes, err := rlp.EncodeToBytes(header)
if err != nil {
panic(fmt.Sprintf("Error RLP encoding header: %v\n", err))
}
entries[hash] = bytes
}
return entries, nil
}