/
filedao_v2.go
315 lines (275 loc) · 8.21 KB
/
filedao_v2.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
// 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 filedao
import (
"context"
"sync/atomic"
"unsafe"
"github.com/pkg/errors"
"github.com/iotexproject/go-pkgs/cache"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/db/batch"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)
// namespace for hash, block, and header storage
const (
_hashDataNS = "hsh"
_blockDataNS = "bdn"
_headerDataNs = "hdr"
)
var (
_fileHeaderKey = []byte("fh")
)
type (
// fileDAOv2 handles chain db file after file split activation at v1.1.2
fileDAOv2 struct {
filename string
header *FileHeader
tip *FileTip
blkBuffer *stagingBuffer
blkCache cache.LRUCache
kvStore db.KVStore
batch batch.KVStoreBatch
hashStore db.CountingIndex // store block hash
blkStore db.CountingIndex // store raw blocks
sysStore db.CountingIndex // store transaction log
deser *block.Deserializer
}
)
// newFileDAOv2 creates a new v2 file
func newFileDAOv2(bottom uint64, cfg db.Config, deser *block.Deserializer) (*fileDAOv2, error) {
if bottom == 0 {
return nil, ErrNotSupported
}
fd := fileDAOv2{
filename: cfg.DbPath,
header: &FileHeader{
Version: FileV2,
Compressor: cfg.Compressor,
BlockStoreSize: uint64(cfg.BlockStoreBatchSize),
Start: bottom,
},
tip: &FileTip{
Height: bottom - 1,
},
blkCache: cache.NewThreadSafeLruCache(16),
kvStore: db.NewBoltDB(cfg),
batch: batch.NewBatch(),
deser: deser,
}
return &fd, nil
}
// openFileDAOv2 opens an existing v2 file
func openFileDAOv2(cfg db.Config, deser *block.Deserializer) *fileDAOv2 {
return &fileDAOv2{
filename: cfg.DbPath,
blkCache: cache.NewThreadSafeLruCache(16),
kvStore: db.NewBoltDB(cfg),
batch: batch.NewBatch(),
deser: deser,
}
}
func (fd *fileDAOv2) Start(ctx context.Context) error {
if err := fd.kvStore.Start(ctx); err != nil {
return err
}
// check file header
header, err := ReadHeaderV2(fd.kvStore)
if err != nil {
if errors.Cause(err) != db.ErrBucketNotExist && errors.Cause(err) != db.ErrNotExist {
return errors.Wrap(err, "failed to get file header")
}
// write file header and tip
if err = WriteHeaderV2(fd.kvStore, fd.header); err != nil {
return err
}
if err = WriteTip(fd.kvStore, _headerDataNs, _topHeightKey, fd.tip); err != nil {
return err
}
} else {
fd.header = header
// read file tip
if fd.tip, err = ReadTip(fd.kvStore, _headerDataNs, _topHeightKey); err != nil {
return err
}
}
// create counting index for hash, blk, and transaction log
if fd.hashStore, err = db.NewCountingIndexNX(fd.kvStore, []byte(_hashDataNS)); err != nil {
return err
}
if fd.blkStore, err = db.NewCountingIndexNX(fd.kvStore, []byte(_blockDataNS)); err != nil {
return err
}
if fd.sysStore, err = db.NewCountingIndexNX(fd.kvStore, []byte(_systemLogNS)); err != nil {
return err
}
// populate staging buffer
if fd.blkBuffer, err = fd.populateStagingBuffer(); err != nil {
return err
}
return nil
}
func (fd *fileDAOv2) Stop(ctx context.Context) error {
return fd.kvStore.Stop(ctx)
}
func (fd *fileDAOv2) Height() (uint64, error) {
tip := fd.loadTip()
return tip.Height, nil
}
func (fd *fileDAOv2) Bottom() (uint64, error) {
return fd.header.Start, nil
}
func (fd *fileDAOv2) ContainsHeight(height uint64) bool {
return fd.header.Start <= height && height <= fd.loadTip().Height
}
func (fd *fileDAOv2) GetBlockHash(height uint64) (hash.Hash256, error) {
if height == 0 {
return block.GenesisHash(), nil
}
if !fd.ContainsHeight(height) {
return hash.ZeroHash256, db.ErrNotExist
}
h, err := fd.hashStore.Get(height - fd.header.Start)
if err != nil {
return hash.ZeroHash256, errors.Wrap(err, "failed to get block hash")
}
return hash.BytesToHash256(h), nil
}
func (fd *fileDAOv2) GetBlockHeight(h hash.Hash256) (uint64, error) {
if h == block.GenesisHash() {
return 0, nil
}
value, err := getValueMustBe8Bytes(fd.kvStore, _blockHashHeightMappingNS, hashKey(h))
if err != nil {
return 0, errors.Wrap(err, "failed to get block height")
}
return byteutil.BytesToUint64BigEndian(value), nil
}
func (fd *fileDAOv2) GetBlock(h hash.Hash256) (*block.Block, error) {
height, err := fd.GetBlockHeight(h)
if err != nil {
return nil, errors.Wrap(err, "failed to get block")
}
return fd.GetBlockByHeight(height)
}
func (fd *fileDAOv2) GetBlockByHeight(height uint64) (*block.Block, error) {
if height == 0 {
return block.GenesisBlock(), nil
}
blkInfo, err := fd.getBlockStore(height)
if err != nil {
return nil, errors.Wrapf(err, "failed to get block at height %d", height)
}
return blkInfo.Block, nil
}
func (fd *fileDAOv2) GetReceipts(height uint64) ([]*action.Receipt, error) {
blkInfo, err := fd.getBlockStore(height)
if err != nil {
return nil, errors.Wrapf(err, "failed to get receipts at height %d", height)
}
return blkInfo.Receipts, nil
}
func (fd *fileDAOv2) ContainsTransactionLog() bool {
return true
}
func (fd *fileDAOv2) TransactionLogs(height uint64) (*iotextypes.TransactionLogs, error) {
if !fd.ContainsHeight(height) {
return nil, ErrNotSupported
}
value, err := fd.sysStore.Get(height - fd.header.Start)
if err != nil {
return nil, errors.Wrapf(err, "failed to get transaction log at height %d", height)
}
value, err = decompBytes(value, fd.header.Compressor)
if err != nil {
return nil, errors.Wrapf(err, "failed to get transaction log at height %d", height)
}
return block.DeserializeSystemLogPb(value)
}
func (fd *fileDAOv2) PutBlock(_ context.Context, blk *block.Block) error {
tip := fd.loadTip()
if blk.Height() != tip.Height+1 {
return ErrInvalidTipHeight
}
// write tip hash and hash-height mapping
if err := fd.putTipHashHeightMapping(blk); err != nil {
return errors.Wrap(err, "failed to write hash-height mapping")
}
// write block data
if err := fd.putBlock(blk); err != nil {
return errors.Wrap(err, "failed to write block")
}
// write receipt and transaction log
if err := fd.putTransactionLog(blk); err != nil {
return errors.Wrap(err, "failed to write receipt")
}
if err := fd.kvStore.WriteBatch(fd.batch); err != nil {
return errors.Wrapf(err, "failed to put block at height %d", blk.Height())
}
fd.batch.Clear()
// update file tip
tip = &FileTip{Height: blk.Height(), Hash: blk.HashBlock()}
fd.storeTip(tip)
return nil
}
func (fd *fileDAOv2) DeleteTipBlock() error {
tip := fd.loadTip()
height := tip.Height
if !fd.ContainsHeight(height) {
// cannot delete block that does not exist
return ErrNotSupported
}
// delete hash
if err := fd.hashStore.Revert(1); err != nil {
return err
}
// delete tip of block storage, if new tip height < lowest block stored in it
if height-1 < fd.lowestBlockOfStoreTip() {
if err := fd.blkStore.Revert(1); err != nil {
return err
}
}
// delete transaction log
if err := fd.sysStore.Revert(1); err != nil {
return err
}
// delete hash -> height mapping
fd.batch.Delete(_blockHashHeightMappingNS, hashKey(tip.Hash), "failed to delete hash -> height mapping")
// update file tip
var (
h = hash.ZeroHash256
err error
)
if height > fd.header.Start {
h, err = fd.GetBlockHash(height - 1)
if err != nil {
return err
}
}
tip = &FileTip{Height: height - 1, Hash: h}
ser, err := tip.Serialize()
if err != nil {
return err
}
fd.batch.Put(_headerDataNs, _topHeightKey, ser, "failed to put file tip")
if err := fd.kvStore.WriteBatch(fd.batch); err != nil {
return err
}
fd.batch.Clear()
fd.storeTip(tip)
return nil
}
func (fd *fileDAOv2) loadTip() *FileTip {
p := (*unsafe.Pointer)(unsafe.Pointer(&fd.tip))
return (*FileTip)(atomic.LoadPointer(p))
}
func (fd *fileDAOv2) storeTip(tip *FileTip) {
p := (*unsafe.Pointer)(unsafe.Pointer(&fd.tip))
atomic.StorePointer(p, unsafe.Pointer(tip))
}