forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_store.go
79 lines (68 loc) · 1.96 KB
/
block_store.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
package bcm
import (
"fmt"
"runtime/debug"
"github.com/hyperledger/burrow/txs"
"github.com/tendermint/tendermint/blockchain"
"github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
)
type BlockStore struct {
txDecoder txs.Decoder
state.BlockStoreRPC
}
func NewBlockStore(blockStore state.BlockStoreRPC) *BlockStore {
return &BlockStore{
txDecoder: txs.NewAminoCodec(),
BlockStoreRPC: blockStore,
}
}
func NewBlockExplorer(dbBackendType db.DBBackendType, dbDir string) *BlockStore {
return NewBlockStore(blockchain.NewBlockStore(db.NewDB("blockstore", dbBackendType, dbDir)))
}
func (bs *BlockStore) Block(height int64) (_ *Block, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("BlockStore.Block(): could not get block at height %v: %v", height, r)
}
}()
tmBlock := bs.LoadBlock(height)
if tmBlock == nil {
return nil, fmt.Errorf("could not pull block at height: %v", height)
}
return NewBlock(bs.txDecoder, tmBlock), nil
}
func (bs *BlockStore) BlockMeta(height int64) (_ *types.BlockMeta, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("BlockStore.BlockMeta() could not get BlockMeta at height %d: %v\n%s",
height, r, debug.Stack())
}
}()
return bs.LoadBlockMeta(height), nil
}
// Iterate over blocks between start (inclusive) and end (exclusive)
func (bs *BlockStore) Blocks(start, end int64, iter func(*Block) (stop bool)) (stopped bool, err error) {
if end > 0 && start >= end {
return false, fmt.Errorf("end height must be strictly greater than start height")
}
if start <= 0 {
// From first block
start = 1
}
if end < 0 {
// -1 means include the very last block so + 1 for offset
end = bs.Height() + end + 1
}
for height := start; height <= end; height++ {
block, err := bs.Block(height)
if err != nil {
return false, err
}
if iter(block) {
return true, nil
}
}
return false, nil
}