-
Notifications
You must be signed in to change notification settings - Fork 319
/
blockdao.go
356 lines (311 loc) · 9.6 KB
/
blockdao.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
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. 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 blockdao
import (
"context"
"sync/atomic"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"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/blockchain/filedao"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/pkg/lifecycle"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/prometheustimer"
)
// vars
var (
_cacheMtc = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "iotex_blockdao_cache",
Help: "IoTeX blockdao cache counter.",
},
[]string{"result"},
)
)
type (
// BlockDAO represents the block data access object
BlockDAO interface {
filedao.FileDAO
GetActionByActionHash(hash.Hash256, uint64) (action.SealedEnvelope, uint32, error)
GetReceiptByActionHash(hash.Hash256, uint64) (*action.Receipt, error)
DeleteBlockToTarget(uint64) error
}
blockDAO struct {
blockStore filedao.FileDAO
indexers []BlockIndexer
timerFactory *prometheustimer.TimerFactory
lifecycle lifecycle.Lifecycle
headerCache cache.LRUCache
bodyCache cache.LRUCache
footerCache cache.LRUCache
tipHeight uint64
}
)
// NewBlockDAO instantiates a block DAO
func NewBlockDAO(indexers []BlockIndexer, cfg db.Config, deser *block.Deserializer) BlockDAO {
blkStore, err := filedao.NewFileDAO(cfg, deser)
if err != nil {
log.L().Fatal(err.Error(), zap.Any("cfg", cfg))
return nil
}
return createBlockDAO(blkStore, indexers, cfg)
}
// NewBlockDAOInMemForTest creates a in-memory block DAO for testing
func NewBlockDAOInMemForTest(indexers []BlockIndexer) BlockDAO {
blkStore, err := filedao.NewFileDAOInMemForTest()
if err != nil {
return nil
}
return createBlockDAO(blkStore, indexers, db.Config{MaxCacheSize: 16})
}
// Start starts block DAO and initiates the top height if it doesn't exist
func (dao *blockDAO) Start(ctx context.Context) error {
err := dao.lifecycle.OnStart(ctx)
if err != nil {
return errors.Wrap(err, "failed to start child services")
}
tipHeight, err := dao.blockStore.Height()
if err != nil {
return err
}
atomic.StoreUint64(&dao.tipHeight, tipHeight)
return dao.checkIndexers(ctx)
}
func (dao *blockDAO) checkIndexers(ctx context.Context) error {
checker := NewBlockIndexerChecker(dao)
for i, indexer := range dao.indexers {
if err := checker.CheckIndexer(ctx, indexer, 0, func(height uint64) {
if height%5000 == 0 {
log.L().Info(
"indexer is catching up.",
zap.Int("indexer", i),
zap.Uint64("height", height),
)
}
}); err != nil {
return err
}
log.L().Info(
"indexer is up to date.",
zap.Int("indexer", i),
)
}
return nil
}
func (dao *blockDAO) Stop(ctx context.Context) error { return dao.lifecycle.OnStop(ctx) }
func (dao *blockDAO) GetBlockHash(height uint64) (hash.Hash256, error) {
timer := dao.timerFactory.NewTimer("get_block_hash")
defer timer.End()
return dao.blockStore.GetBlockHash(height)
}
func (dao *blockDAO) GetBlockHeight(hash hash.Hash256) (uint64, error) {
timer := dao.timerFactory.NewTimer("get_block_height")
defer timer.End()
return dao.blockStore.GetBlockHeight(hash)
}
func (dao *blockDAO) GetBlock(hash hash.Hash256) (*block.Block, error) {
timer := dao.timerFactory.NewTimer("get_block")
defer timer.End()
return dao.blockStore.GetBlock(hash)
}
func (dao *blockDAO) GetBlockByHeight(height uint64) (*block.Block, error) {
timer := dao.timerFactory.NewTimer("get_block_byheight")
defer timer.End()
return dao.blockStore.GetBlockByHeight(height)
}
func (dao *blockDAO) HeaderByHeight(height uint64) (*block.Header, error) {
if v, ok := lruCacheGet(dao.headerCache, height); ok {
_cacheMtc.WithLabelValues("hit_header").Inc()
return v.(*block.Header), nil
}
_cacheMtc.WithLabelValues("miss_header").Inc()
header, err := dao.blockStore.HeaderByHeight(height)
if err != nil {
return nil, err
}
lruCachePut(dao.headerCache, height, header)
return header, nil
}
func (dao *blockDAO) FooterByHeight(height uint64) (*block.Footer, error) {
if v, ok := lruCacheGet(dao.footerCache, height); ok {
_cacheMtc.WithLabelValues("hit_footer").Inc()
return v.(*block.Footer), nil
}
_cacheMtc.WithLabelValues("miss_footer").Inc()
footer, err := dao.blockStore.FooterByHeight(height)
if err != nil {
return nil, err
}
lruCachePut(dao.footerCache, height, footer)
return footer, nil
}
func (dao *blockDAO) Height() (uint64, error) {
return dao.blockStore.Height()
}
func (dao *blockDAO) Header(h hash.Hash256) (*block.Header, error) {
if header, ok := lruCacheGet(dao.headerCache, h); ok {
_cacheMtc.WithLabelValues("hit_header").Inc()
return header.(*block.Header), nil
}
_cacheMtc.WithLabelValues("miss_header").Inc()
header, err := dao.blockStore.Header(h)
if err != nil {
return nil, err
}
lruCachePut(dao.headerCache, h, header)
return header, nil
}
func (dao *blockDAO) GetActionByActionHash(h hash.Hash256, height uint64) (action.SealedEnvelope, uint32, error) {
blk, err := dao.blockStore.GetBlockByHeight(height)
if err != nil {
return action.SealedEnvelope{}, 0, err
}
for i, act := range blk.Actions {
actHash, err := act.Hash()
if err != nil {
return action.SealedEnvelope{}, 0, errors.Errorf("hash failed for block %d", height)
}
if actHash == h {
return act, uint32(i), nil
}
}
return action.SealedEnvelope{}, 0, errors.Errorf("block %d does not have action %x", height, h)
}
func (dao *blockDAO) GetReceiptByActionHash(h hash.Hash256, height uint64) (*action.Receipt, error) {
receipts, err := dao.blockStore.GetReceipts(height)
if err != nil {
return nil, err
}
for _, r := range receipts {
if r.ActionHash == h {
return r, nil
}
}
return nil, errors.Errorf("receipt of action %x isn't found", h)
}
func (dao *blockDAO) GetReceipts(height uint64) ([]*action.Receipt, error) {
timer := dao.timerFactory.NewTimer("get_receipt")
defer timer.End()
return dao.blockStore.GetReceipts(height)
}
func (dao *blockDAO) ContainsTransactionLog() bool {
return dao.blockStore.ContainsTransactionLog()
}
func (dao *blockDAO) TransactionLogs(height uint64) (*iotextypes.TransactionLogs, error) {
timer := dao.timerFactory.NewTimer("get_transactionlog")
defer timer.End()
return dao.blockStore.TransactionLogs(height)
}
func (dao *blockDAO) PutBlock(ctx context.Context, blk *block.Block) error {
timer := dao.timerFactory.NewTimer("put_block")
if err := dao.blockStore.PutBlock(ctx, blk); err != nil {
timer.End()
return err
}
atomic.StoreUint64(&dao.tipHeight, blk.Height())
header := blk.Header
lruCachePut(dao.headerCache, blk.Height(), &header)
lruCachePut(dao.headerCache, header.HashHeader(), &header)
timer.End()
// index the block if there's indexer
timer = dao.timerFactory.NewTimer("index_block")
defer timer.End()
for _, indexer := range dao.indexers {
if err := indexer.PutBlock(ctx, blk); err != nil {
return err
}
}
return nil
}
func (dao *blockDAO) DeleteTipBlock() error {
timer := dao.timerFactory.NewTimer("del_block")
defer timer.End()
return dao.blockStore.DeleteTipBlock()
}
func (dao *blockDAO) DeleteBlockToTarget(targetHeight uint64) error {
tipHeight, err := dao.blockStore.Height()
if err != nil {
return err
}
ctx := context.Background()
for tipHeight > targetHeight {
blk, err := dao.blockStore.GetBlockByHeight(tipHeight)
if err != nil {
return errors.Wrap(err, "failed to get tip block")
}
// delete block index if there's indexer
for _, indexer := range dao.indexers {
if err := indexer.DeleteTipBlock(ctx, blk); err != nil {
return err
}
}
if err := dao.blockStore.DeleteTipBlock(); err != nil {
return err
}
// purge from cache
h := blk.HashBlock()
lruCacheDel(dao.headerCache, tipHeight)
lruCacheDel(dao.headerCache, h)
lruCacheDel(dao.bodyCache, tipHeight)
lruCacheDel(dao.bodyCache, h)
lruCacheDel(dao.footerCache, tipHeight)
lruCacheDel(dao.footerCache, h)
tipHeight--
atomic.StoreUint64(&dao.tipHeight, tipHeight)
}
return nil
}
func createBlockDAO(blkStore filedao.FileDAO, indexers []BlockIndexer, cfg db.Config) BlockDAO {
if blkStore == nil {
return nil
}
blockDAO := &blockDAO{
blockStore: blkStore,
indexers: indexers,
}
blockDAO.lifecycle.Add(blkStore)
for _, indexer := range indexers {
blockDAO.lifecycle.Add(indexer)
}
if cfg.MaxCacheSize > 0 {
blockDAO.headerCache = cache.NewThreadSafeLruCache(cfg.MaxCacheSize)
blockDAO.bodyCache = cache.NewThreadSafeLruCache(cfg.MaxCacheSize)
blockDAO.footerCache = cache.NewThreadSafeLruCache(cfg.MaxCacheSize)
}
timerFactory, err := prometheustimer.New(
"iotex_block_dao_perf",
"Performance of block DAO",
[]string{"type"},
[]string{"default"},
)
if err != nil {
return nil
}
blockDAO.timerFactory = timerFactory
return blockDAO
}
func lruCacheGet(c cache.LRUCache, key interface{}) (interface{}, bool) {
if c != nil {
return c.Get(key)
}
return nil, false
}
func lruCachePut(c cache.LRUCache, k, v interface{}) {
if c != nil {
c.Add(k, v)
}
}
func lruCacheDel(c cache.LRUCache, k interface{}) {
if c != nil {
c.Remove(k)
}
}