github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/common/ledger/blkstorage/blockstorage.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package blkstorage
    18  
    19  import (
    20  	"errors"
    21  
    22  	"github.com/inklabsfoundation/inkchain/common/ledger"
    23  	"github.com/inklabsfoundation/inkchain/protos/common"
    24  	"github.com/inklabsfoundation/inkchain/protos/peer"
    25  )
    26  
    27  // IndexableAttr represents an indexable attribute
    28  type IndexableAttr string
    29  
    30  // constants for indexable attributes
    31  const (
    32  	IndexableAttrBlockNum         = IndexableAttr("BlockNum")
    33  	IndexableAttrBlockHash        = IndexableAttr("BlockHash")
    34  	IndexableAttrTxID             = IndexableAttr("TxID")
    35  	IndexableAttrBlockNumTranNum  = IndexableAttr("BlockNumTranNum")
    36  	IndexableAttrBlockTxID        = IndexableAttr("BlockTxID")
    37  	IndexableAttrTxValidationCode = IndexableAttr("TxValidationCode")
    38  )
    39  
    40  // IndexConfig - a configuration that includes a list of attributes that should be indexed
    41  type IndexConfig struct {
    42  	AttrsToIndex []IndexableAttr
    43  }
    44  
    45  var (
    46  	// ErrNotFoundInIndex is used to indicate missing entry in the index
    47  	ErrNotFoundInIndex = errors.New("Entry not found in index")
    48  	// ErrAttrNotIndexed is used to indicate that an attribute is not indexed
    49  	ErrAttrNotIndexed = errors.New("Attribute not indexed")
    50  )
    51  
    52  // BlockStoreProvider provides an handle to a BlockStore
    53  type BlockStoreProvider interface {
    54  	CreateBlockStore(ledgerid string) (BlockStore, error)
    55  	OpenBlockStore(ledgerid string) (BlockStore, error)
    56  	Exists(ledgerid string) (bool, error)
    57  	List() ([]string, error)
    58  	Close()
    59  }
    60  
    61  // BlockStore - an interface for persisting and retrieving blocks
    62  // An implementation of this interface is expected to take an argument
    63  // of type `IndexConfig` which configures the block store on what items should be indexed
    64  type BlockStore interface {
    65  	AddBlock(block *common.Block) error
    66  	GetBlockchainInfo() (*common.BlockchainInfo, error)
    67  	RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error)
    68  	RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
    69  	RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of  math.MaxUint64 will return last block
    70  	RetrieveTxByID(txID string) (*common.Envelope, error)
    71  	RetrieveTxByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error)
    72  	RetrieveBlockByTxID(txID string) (*common.Block, error)
    73  	RetrieveTxValidationCodeByTxID(txID string) (peer.TxValidationCode, error)
    74  	Shutdown()
    75  }