blockstorage.go 2.33 KB
Newer Older
manish's avatar
manish committed
1
/*
2
Copyright IBM Corp. All Rights Reserved.
manish's avatar
manish committed
3

4
SPDX-License-Identifier: Apache-2.0
manish's avatar
manish committed
5
6
7
8
9
*/

package blkstorage

import (
10
	"github.com/hyperledger/fabric/common/ledger"
11
	l "github.com/hyperledger/fabric/core/ledger"
12
	"github.com/hyperledger/fabric/protos/common"
13
	"github.com/hyperledger/fabric/protos/peer"
14
	"github.com/pkg/errors"
manish's avatar
manish committed
15
16
)

manish's avatar
manish committed
17
18
19
20
21
// IndexableAttr represents an indexable attribute
type IndexableAttr string

// constants for indexable attributes
const (
22
23
24
25
26
27
	IndexableAttrBlockNum         = IndexableAttr("BlockNum")
	IndexableAttrBlockHash        = IndexableAttr("BlockHash")
	IndexableAttrTxID             = IndexableAttr("TxID")
	IndexableAttrBlockNumTranNum  = IndexableAttr("BlockNumTranNum")
	IndexableAttrBlockTxID        = IndexableAttr("BlockTxID")
	IndexableAttrTxValidationCode = IndexableAttr("TxValidationCode")
manish's avatar
manish committed
28
29
30
31
32
33
34
35
36
)

// IndexConfig - a configuration that includes a list of attributes that should be indexed
type IndexConfig struct {
	AttrsToIndex []IndexableAttr
}

var (
	// ErrNotFoundInIndex is used to indicate missing entry in the index
37
38
	ErrNotFoundInIndex = l.NotFoundInIndexErr("")

manish's avatar
manish committed
39
	// ErrAttrNotIndexed is used to indicate that an attribute is not indexed
40
	ErrAttrNotIndexed = errors.New("attribute not indexed")
manish's avatar
manish committed
41
42
)

manish's avatar
manish committed
43
44
45
46
47
48
49
50
51
// BlockStoreProvider provides an handle to a BlockStore
type BlockStoreProvider interface {
	CreateBlockStore(ledgerid string) (BlockStore, error)
	OpenBlockStore(ledgerid string) (BlockStore, error)
	Exists(ledgerid string) (bool, error)
	List() ([]string, error)
	Close()
}

manish's avatar
manish committed
52
// BlockStore - an interface for persisting and retrieving blocks
manish's avatar
manish committed
53
54
// An implementation of this interface is expected to take an argument
// of type `IndexConfig` which configures the block store on what items should be indexed
manish's avatar
manish committed
55
type BlockStore interface {
56
	AddBlock(block *common.Block) error
57
	GetBlockchainInfo() (*common.BlockchainInfo, error)
manish's avatar
manish committed
58
	RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error)
59
	RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
denyeart's avatar
denyeart committed
60
	RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of  math.MaxUint64 will return last block
61
	RetrieveTxByID(txID string) (*common.Envelope, error)
62
	RetrieveTxByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error)
63
	RetrieveBlockByTxID(txID string) (*common.Block, error)
64
	RetrieveTxValidationCodeByTxID(txID string) (peer.TxValidationCode, error)
manish's avatar
manish committed
65
66
	Shutdown()
}