|
| 1 | +// Copyright (c) 2024 IoTeX Foundation |
| 2 | +// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability |
| 3 | +// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. |
| 4 | +// This source code is governed by Apache License 2.0 that can be found in the LICENSE file. |
| 5 | + |
| 6 | +package blockdao |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "sync" |
| 12 | + |
| 13 | + "github.com/iotexproject/iotex-core/v2/action" |
| 14 | + "github.com/iotexproject/iotex-core/v2/blockchain/block" |
| 15 | + "github.com/iotexproject/iotex-core/v2/db" |
| 16 | + "github.com/iotexproject/iotex-core/v2/db/batch" |
| 17 | + "github.com/iotexproject/iotex-core/v2/pkg/util/byteutil" |
| 18 | + "github.com/iotexproject/iotex-proto/golang/iotextypes" |
| 19 | + "github.com/pkg/errors" |
| 20 | + "google.golang.org/protobuf/proto" |
| 21 | +) |
| 22 | + |
| 23 | +type ( |
| 24 | + // ReceiptIndexer defines a struct to store correct receipts for poluted blocks |
| 25 | + ReceiptIndexer struct { |
| 26 | + mu sync.RWMutex |
| 27 | + height uint64 |
| 28 | + endHeight uint64 |
| 29 | + kvstore db.KVStore |
| 30 | + } |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + _receiptsNS = "receipts" |
| 35 | + _receiptMetaNS = "meta" |
| 36 | +) |
| 37 | + |
| 38 | +var _heightKey = []byte("height") |
| 39 | + |
| 40 | +// ErrIndexOutOfRange indicates the receipt does not exist in the indexer |
| 41 | +var ErrIndexOutOfRange = errors.New("index out of range") |
| 42 | + |
| 43 | +// NewReceiptIndexer creates a new receipt indexer |
| 44 | +func NewReceiptIndexer(kvstore db.KVStore, endHeight uint64) *ReceiptIndexer { |
| 45 | + return &ReceiptIndexer{ |
| 46 | + endHeight: endHeight, |
| 47 | + kvstore: kvstore, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Start starts the receipt indexer |
| 52 | +func (ri *ReceiptIndexer) Start(ctx context.Context) error { |
| 53 | + ri.mu.Lock() |
| 54 | + defer ri.mu.Unlock() |
| 55 | + if err := ri.kvstore.Start(ctx); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + value, err := ri.kvstore.Get(_receiptMetaNS, _heightKey) |
| 59 | + switch errors.Cause(err) { |
| 60 | + case nil: |
| 61 | + case db.ErrNotExist: |
| 62 | + return nil |
| 63 | + default: |
| 64 | + return err |
| 65 | + } |
| 66 | + ri.height = byteutil.BytesToUint64(value) |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +// Stop stops the receipt indexer |
| 71 | +func (ri *ReceiptIndexer) Stop(ctx context.Context) error { |
| 72 | + ri.mu.Lock() |
| 73 | + defer ri.mu.Unlock() |
| 74 | + |
| 75 | + return ri.kvstore.Stop(ctx) |
| 76 | +} |
| 77 | + |
| 78 | +// Height returns the end height of the receipt indexer |
| 79 | +func (ri *ReceiptIndexer) Height() (uint64, error) { |
| 80 | + ri.mu.RLock() |
| 81 | + defer ri.mu.RUnlock() |
| 82 | + return ri.height, nil |
| 83 | +} |
| 84 | + |
| 85 | +// PutBlock puts the receipts of the block into kvstore |
| 86 | +func (ri *ReceiptIndexer) PutBlock(ctx context.Context, blk *block.Block) error { |
| 87 | + height := blk.Height() |
| 88 | + if height > ri.endHeight { |
| 89 | + return nil |
| 90 | + } |
| 91 | + key := byteutil.Uint64ToBytes(height) |
| 92 | + if blk.Receipts == nil { |
| 93 | + return errors.Errorf("receipts of block %d is nil", height) |
| 94 | + } |
| 95 | + ri.mu.Lock() |
| 96 | + defer ri.mu.Unlock() |
| 97 | + logIndex := uint32(0) |
| 98 | + receipts := iotextypes.Receipts{} |
| 99 | + for i, receipt := range blk.Receipts { |
| 100 | + cr := receipt.FixAndClone() |
| 101 | + logIndex = cr.UpdateIndex(uint32(i), logIndex) |
| 102 | + receipts.Receipts = append(receipts.Receipts, cr.ConvertToReceiptPb()) |
| 103 | + } |
| 104 | + receiptsBytes, err := proto.Marshal(&receipts) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + b := batch.NewBatch() |
| 110 | + b.Put(_receiptsNS, key, receiptsBytes, fmt.Sprintf("failed to write receipts for block %d", height)) |
| 111 | + if height > ri.height { |
| 112 | + ri.height = height |
| 113 | + b.Put(_receiptMetaNS, _heightKey, key, "failed to write receipt indexer height") |
| 114 | + } |
| 115 | + return ri.kvstore.WriteBatch(b) |
| 116 | +} |
| 117 | + |
| 118 | +// Receipts returns the receipts of the block at the given height |
| 119 | +func (ri *ReceiptIndexer) Receipts(height uint64) ([]*action.Receipt, error) { |
| 120 | + key := byteutil.Uint64ToBytes(height) |
| 121 | + ri.mu.RLock() |
| 122 | + defer ri.mu.RUnlock() |
| 123 | + if height > ri.endHeight { |
| 124 | + return nil, errors.Wrapf(ErrIndexOutOfRange, "height %d > endHeight %d", height, ri.endHeight) |
| 125 | + } |
| 126 | + value, err := ri.kvstore.Get(_receiptsNS, key) |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + receiptsPb := &iotextypes.Receipts{} |
| 131 | + if err := proto.Unmarshal(value, receiptsPb); err != nil { |
| 132 | + return nil, errors.Wrap(err, "failed to unmarshal block receipts") |
| 133 | + } |
| 134 | + |
| 135 | + var receipts []*action.Receipt |
| 136 | + for _, receiptPb := range receiptsPb.Receipts { |
| 137 | + receipt := &action.Receipt{} |
| 138 | + receipt.ConvertFromReceiptPb(receiptPb) |
| 139 | + receipts = append(receipts, receipt) |
| 140 | + } |
| 141 | + return receipts, nil |
| 142 | +} |
0 commit comments