/
block.go
271 lines (242 loc) · 7.47 KB
/
block.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
package store
import (
"bytes"
"encoding/binary"
"errors"
"math/big"
"path/filepath"
cmtdb "github.com/cometbft/cometbft-db"
"github.com/ethereum/go-ethereum/rlp"
"github.com/spf13/viper"
"github.com/DOIDFoundation/node/flags"
"github.com/DOIDFoundation/node/types"
"github.com/cometbft/cometbft/libs/log"
_ "github.com/mattn/go-sqlite3"
)
var (
// ErrNotFound is returned when record not found.
ErrNotFound = errors.New("not found")
)
type BlockStore struct {
log.Logger
db cmtdb.DB
MinerDb MinerStore
}
func NewBlockStore(logger log.Logger) (*BlockStore, error) {
homeDir := viper.GetString(flags.Home)
db, err := cmtdb.NewDB("chaindata", cmtdb.BackendType(viper.GetString(flags.DB_Engine)), filepath.Join(homeDir, "data"))
if err != nil {
return nil, err
}
minerDb := newMinerStore(logger)
return &BlockStore{
Logger: logger.With("module", "blockStore"),
db: db,
MinerDb: minerDb,
}, nil
}
func (bs *BlockStore) ReadBlock(height uint64, hash types.Hash) *types.Block {
header := bs.ReadHeader(height, hash)
if header == nil {
return nil
}
uncles := new(types.Headers)
if err := bs.ReadData(unclesKey(header.UncleHash), uncles); err != nil {
bs.Logger.Error("invalid block uncles", "err", err, "hash", header.UncleHash)
return nil
}
txs := new(types.Txs)
if err := bs.ReadData(txsKey(header.TxHash), txs); err != nil {
bs.Logger.Error("invalid block txs", "err", err, "hash", header.TxHash)
return nil
}
receipts := new(types.Receipts)
if err := bs.ReadData(receiptsKey(header.ReceiptHash), receipts); err != nil {
bs.Logger.Error("invalid block receipts", "err", err, "hash", header.ReceiptHash)
return nil
}
block := types.NewBlockWithHeader(header)
block.Txs = *txs
block.Receipts = *receipts
block.Uncles = *uncles
return block
}
func (bs *BlockStore) WriteBlock(block *types.Block) {
bs.WriteData(txsKey(block.Txs.Hash()), block.Txs)
for _, t := range block.Txs {
bs.WriteData(transactionKey(t.Hash()), t)
}
bs.WriteData(unclesKey(block.Uncles.Hash()), block.Uncles)
bs.WriteData(receiptsKey(block.Receipts.Hash()), block.Receipts)
for i, r := range block.Receipts {
bs.WriteData(
receiptKey(r.TxHash),
types.StoredReceipt{
Receipt: *r,
BlockHash: block.Hash(),
BlockNumber: block.Header.Height,
TransactionIndex: uint(i),
},
)
}
block.Hash() // call hash to fill header if needed
bs.WriteHeader(block.Header)
}
func (bs *BlockStore) DeleteBlock(height uint64, hash types.Hash) {
header := bs.ReadHeader(height, hash)
if header == nil {
return
}
bs.DeleteData(unclesKey(header.UncleHash))
bs.DeleteData(txsKey(header.TxHash))
bs.DeleteData(receiptsKey(header.ReceiptHash))
bs.DeleteHeader(height, hash)
}
// ReadHeader retrieves the block header corresponding to the hash.
func (bs *BlockStore) ReadHeader(height uint64, hash types.Hash) *types.Header {
header := new(types.Header)
if err := bs.ReadData(headerKey(height, hash), header); err != nil {
if errors.Is(err, ErrNotFound) {
bs.Logger.Debug("block header not found", "height", height, "hash", hash)
} else {
bs.Logger.Error("invalid block header", "err", err, "height", height, "hash", hash)
}
return nil
}
return header
}
// WriteHeader writes the block header corresponding to the hash.
func (bs *BlockStore) WriteHeader(header *types.Header) {
var (
hash = header.Hash()
height = header.Height.Uint64()
)
bs.WriteData(headerKey(height, hash), header)
bs.WriteHeightByHash(hash, height)
}
func (bs *BlockStore) DeleteHeader(height uint64, hash types.Hash) {
bs.DeleteData(headerKey(height, hash))
}
// ReadDataRLP retrieves the data in RLP encoding.
func (bs *BlockStore) ReadDataRLP(hash types.Hash) rlp.RawValue {
bz, err := bs.db.Get(hash)
if err != nil {
bs.Logger.Error("failed to read data by hash", "err", err, "hash", hash)
return nil
}
if len(bz) == 0 {
return nil
}
return bz
}
// ReadData retrieves the data corresponding to the hash and decode into result.
func (bs *BlockStore) ReadData(hash types.Hash, result interface{}) error {
data := bs.ReadDataRLP(hash)
if len(data) == 0 {
return ErrNotFound
}
return rlp.DecodeBytes(data, result)
}
func (bs *BlockStore) WriteData(hash types.Hash, data interface{}) {
// Write the encoded data
bz, err := rlp.EncodeToBytes(data)
if err != nil {
bs.Logger.Error("failed to RLP encode data", "err", err)
panic(err)
}
if err := bs.db.Set(hash, bz); err != nil {
bs.Logger.Error("failed to store data", "err", err)
panic(err)
}
}
func (bs *BlockStore) DeleteData(hash types.Hash) {
if err := bs.db.Delete(hash); err != nil {
bs.Logger.Error("failed to delete data by hash", "err", err, "hash", hash)
panic(err)
}
}
// ReadHeightByHash returns the header height assigned to a hash.
func (bs *BlockStore) ReadHeightByHash(hash types.Hash) *uint64 {
data, err := bs.db.Get(headerHeightKey(hash))
if err != nil {
bs.Logger.Error("failed to read height by hash", "err", err, "hash", hash)
panic(err)
}
if len(data) != 8 {
return nil
}
height := binary.BigEndian.Uint64(data)
return &height
}
// WriteHeightByHash stores the hash->height mapping.
func (bs *BlockStore) WriteHeightByHash(hash types.Hash, height uint64) {
key := headerHeightKey(hash)
enc := encodeBlockHeight(height)
if err := bs.db.Set(key, enc); err != nil {
bs.Logger.Error("Failed to store hash to height mapping", "err", err)
panic(err)
}
}
// DeleteHeaderHeight removes hash->height mapping.
func (bs *BlockStore) DeleteHeaderHeight(hash types.Hash) {
if err := bs.db.Delete(headerHeightKey(hash)); err != nil {
bs.Logger.Error("Failed to delete hash to height mapping", "err", err)
panic(err)
}
}
// ReadTd retrieves a block's total difficulty corresponding to the hash.
func (bs *BlockStore) ReadTd(height uint64, hash types.Hash) *big.Int {
data, err := bs.db.Get(headerTDKey(height, hash))
if err != nil {
bs.Logger.Error("Failed to read block total difficulty", "err", err)
return nil
}
if len(data) == 0 {
return nil
}
td := new(big.Int)
if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
bs.Logger.Error("Invalid block total difficulty RLP", "hash", hash, "err", err)
return nil
}
return td
}
// WriteTd stores the total difficulty of a block into the database.
func (bs *BlockStore) WriteTd(height uint64, hash types.Hash, td *big.Int) {
data, err := rlp.EncodeToBytes(td)
if err != nil {
bs.Logger.Error("Failed to RLP encode block total difficulty", "err", err)
panic(err)
}
if err := bs.db.Set(headerTDKey(height, hash), data); err != nil {
bs.Logger.Error("Failed to store block total difficulty", "err", err)
panic(err)
}
}
// DeleteTd removes all block total difficulty data associated with a hash.
func (bs *BlockStore) DeleteTd(height uint64, hash types.Hash) {
if err := bs.db.Delete(headerTDKey(height, hash)); err != nil {
bs.Logger.Error("Failed to delete block total difficulty", "err", err)
panic(err)
}
}
func (bs *BlockStore) ReadTx(hash types.Hash) (result types.Tx) {
if err := bs.ReadData(transactionKey(hash), &result); err != nil {
bs.Logger.Error("Failed to read tx", "err", err, "hash", hash)
return nil
}
return
}
func (bs *BlockStore) ReadReceipt(hash types.Hash) (result *types.StoredReceipt) {
result = new(types.StoredReceipt)
if err := bs.ReadData(receiptKey(hash), result); err != nil {
bs.Logger.Error("Failed to read receipt", "err", err, "hash", hash)
return nil
}
return
}
func (bs *BlockStore) Close() error {
bs.Logger.Debug("closing block store")
bs.MinerDb.Close()
return bs.db.Close()
}