Commit 2a16532c authored by manish's avatar manish
Browse files

Move Blockstorage code under /fabric/common package

https://jira.hyperledger.org/browse/FAB-2022



This changes introduced by this CR
- Moves the block storage code from package
core/ledger/blkstorage to common/ledger/blkstorage

- Splits the ledger_interface.go so as to move common interfaces
and data type to common/ledger package

- Moves some of the util functions to common/ledger package

- Moves core/ledger/ordererledger package to orderer/ledger/fsledger
orderer folks can futher rename/refactor this as seems suitable to them

Change-Id: I759e16f00dc2ec9bb62196121083cf48eae76948
Signed-off-by: default avatarmanish <manish.sethi@gmail.com>
parent e67a2382
...@@ -19,10 +19,8 @@ package blkstorage ...@@ -19,10 +19,8 @@ package blkstorage
import ( import (
"errors" "errors"
"github.com/hyperledger/fabric/core/ledger" "github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
) )
// IndexableAttr represents an indexable attribute // IndexableAttr represents an indexable attribute
...@@ -62,7 +60,7 @@ type BlockStoreProvider interface { ...@@ -62,7 +60,7 @@ type BlockStoreProvider interface {
// of type `IndexConfig` which configures the block store on what items should be indexed // of type `IndexConfig` which configures the block store on what items should be indexed
type BlockStore interface { type BlockStore interface {
AddBlock(block *common.Block) error AddBlock(block *common.Block) error
GetBlockchainInfo() (*pb.BlockchainInfo, error) GetBlockchainInfo() (*common.BlockchainInfo, error)
RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error) RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error)
RetrieveBlockByHash(blockHash []byte) (*common.Block, error) RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of math.MaxUint64 will return last block RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of math.MaxUint64 will return last block
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
ledgerutil "github.com/hyperledger/fabric/core/ledger/util" ledgerutil "github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils" "github.com/hyperledger/fabric/protos/utils"
) )
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"testing" "testing"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/testutil" "github.com/hyperledger/fabric/common/ledger/testutil"
putils "github.com/hyperledger/fabric/protos/utils" putils "github.com/hyperledger/fabric/protos/utils"
) )
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"testing" "testing"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/testutil" "github.com/hyperledger/fabric/common/ledger/testutil"
) )
func TestBlockfileStream(t *testing.T) { func TestBlockfileStream(t *testing.T) {
......
...@@ -23,11 +23,10 @@ import ( ...@@ -23,11 +23,10 @@ import (
"sync/atomic" "sync/atomic"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/blkstorage" "github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/util" "github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/core/ledger/util/leveldbhelper" "github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putil "github.com/hyperledger/fabric/protos/utils" putil "github.com/hyperledger/fabric/protos/utils"
"github.com/op/go-logging" "github.com/op/go-logging"
) )
...@@ -153,7 +152,7 @@ func newBlockfileMgr(id string, conf *Conf, indexConfig *blkstorage.IndexConfig, ...@@ -153,7 +152,7 @@ func newBlockfileMgr(id string, conf *Conf, indexConfig *blkstorage.IndexConfig,
mgr.syncIndex() mgr.syncIndex()
// init BlockchainInfo for external API's // init BlockchainInfo for external API's
bcInfo := &pb.BlockchainInfo{ bcInfo := &common.BlockchainInfo{
Height: 0, Height: 0,
CurrentBlockHash: nil, CurrentBlockHash: nil,
PreviousBlockHash: nil} PreviousBlockHash: nil}
...@@ -166,7 +165,7 @@ func newBlockfileMgr(id string, conf *Conf, indexConfig *blkstorage.IndexConfig, ...@@ -166,7 +165,7 @@ func newBlockfileMgr(id string, conf *Conf, indexConfig *blkstorage.IndexConfig,
} }
lastBlockHash := lastBlockHeader.Hash() lastBlockHash := lastBlockHeader.Hash()
previousBlockHash := lastBlockHeader.PreviousHash previousBlockHash := lastBlockHeader.PreviousHash
bcInfo = &pb.BlockchainInfo{ bcInfo = &common.BlockchainInfo{
Height: cpInfo.lastBlockNumber, Height: cpInfo.lastBlockNumber,
CurrentBlockHash: lastBlockHash, CurrentBlockHash: lastBlockHash,
PreviousBlockHash: previousBlockHash} PreviousBlockHash: previousBlockHash}
...@@ -373,8 +372,8 @@ func (mgr *blockfileMgr) syncIndex() error { ...@@ -373,8 +372,8 @@ func (mgr *blockfileMgr) syncIndex() error {
return nil return nil
} }
func (mgr *blockfileMgr) getBlockchainInfo() *pb.BlockchainInfo { func (mgr *blockfileMgr) getBlockchainInfo() *common.BlockchainInfo {
return mgr.bcInfo.Load().(*pb.BlockchainInfo) return mgr.bcInfo.Load().(*common.BlockchainInfo)
} }
func (mgr *blockfileMgr) updateCheckpoint(cpInfo *checkpointInfo) { func (mgr *blockfileMgr) updateCheckpoint(cpInfo *checkpointInfo) {
...@@ -387,7 +386,7 @@ func (mgr *blockfileMgr) updateCheckpoint(cpInfo *checkpointInfo) { ...@@ -387,7 +386,7 @@ func (mgr *blockfileMgr) updateCheckpoint(cpInfo *checkpointInfo) {
func (mgr *blockfileMgr) updateBlockchainInfo(latestBlockHash []byte, latestBlock *common.Block) { func (mgr *blockfileMgr) updateBlockchainInfo(latestBlockHash []byte, latestBlock *common.Block) {
currentBCInfo := mgr.getBlockchainInfo() currentBCInfo := mgr.getBlockchainInfo()
newBCInfo := &pb.BlockchainInfo{ newBCInfo := &common.BlockchainInfo{
Height: currentBCInfo.Height + 1, Height: currentBCInfo.Height + 1,
CurrentBlockHash: latestBlockHash, CurrentBlockHash: latestBlockHash,
PreviousBlockHash: latestBlock.Header.PreviousHash} PreviousBlockHash: latestBlock.Header.PreviousHash}
......
...@@ -21,10 +21,9 @@ import ( ...@@ -21,10 +21,9 @@ import (
"testing" "testing"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/testutil" "github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putil "github.com/hyperledger/fabric/protos/utils" putil "github.com/hyperledger/fabric/protos/utils"
) )
...@@ -127,7 +126,7 @@ func TestBlockfileMgrBlockchainInfo(t *testing.T) { ...@@ -127,7 +126,7 @@ func TestBlockfileMgrBlockchainInfo(t *testing.T) {
defer blkfileMgrWrapper.close() defer blkfileMgrWrapper.close()
bcInfo := blkfileMgrWrapper.blockfileMgr.getBlockchainInfo() bcInfo := blkfileMgrWrapper.blockfileMgr.getBlockchainInfo()
testutil.AssertEquals(t, bcInfo, &pb.BlockchainInfo{Height: 0, CurrentBlockHash: nil, PreviousBlockHash: nil}) testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{Height: 0, CurrentBlockHash: nil, PreviousBlockHash: nil})
blocks := testutil.ConstructTestBlocks(t, 10) blocks := testutil.ConstructTestBlocks(t, 10)
blkfileMgrWrapper.addBlocks(blocks) blkfileMgrWrapper.addBlocks(blocks)
......
...@@ -20,8 +20,8 @@ import ( ...@@ -20,8 +20,8 @@ import (
"os" "os"
"testing" "testing"
"github.com/hyperledger/fabric/core/ledger/testutil" "github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/core/ledger/util" "github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
) )
......
...@@ -20,9 +20,9 @@ import ( ...@@ -20,9 +20,9 @@ import (
"fmt" "fmt"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/blkstorage" "github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/util" "github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/core/ledger/util/leveldbhelper" "github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
) )
const ( const (
......
...@@ -20,8 +20,8 @@ import ( ...@@ -20,8 +20,8 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/hyperledger/fabric/core/ledger/blkstorage" "github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/testutil" "github.com/hyperledger/fabric/common/ledger/testutil"
putil "github.com/hyperledger/fabric/protos/utils" putil "github.com/hyperledger/fabric/protos/utils"
) )
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"sync" "sync"
"github.com/hyperledger/fabric/core/ledger" "github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
) )
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/hyperledger/fabric/core/ledger/testutil" "github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
) )
......
...@@ -17,12 +17,11 @@ limitations under the License. ...@@ -17,12 +17,11 @@ limitations under the License.
package fsblkstorage package fsblkstorage
import ( import (
"github.com/hyperledger/fabric/core/ledger" "github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/core/ledger/blkstorage" "github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/util/leveldbhelper" "github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
) )
// fsBlockStore - filesystem based implementation for `BlockStore` // fsBlockStore - filesystem based implementation for `BlockStore`
...@@ -44,7 +43,7 @@ func (store *fsBlockStore) AddBlock(block *common.Block) error { ...@@ -44,7 +43,7 @@ func (store *fsBlockStore) AddBlock(block *common.Block) error {
} }
// GetBlockchainInfo returns the current info about blockchain // GetBlockchainInfo returns the current info about blockchain
func (store *fsBlockStore) GetBlockchainInfo() (*pb.BlockchainInfo, error) { func (store *fsBlockStore) GetBlockchainInfo() (*common.BlockchainInfo, error) {
return store.fileMgr.getBlockchainInfo(), nil return store.fileMgr.getBlockchainInfo(), nil
} }
......
...@@ -17,9 +17,9 @@ limitations under the License. ...@@ -17,9 +17,9 @@ limitations under the License.
package fsblkstorage package fsblkstorage
import ( import (
"github.com/hyperledger/fabric/core/ledger/blkstorage" "github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/util" "github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/core/ledger/util/leveldbhelper" "github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
) )
// FsBlockstoreProvider provides handle to block storage - this is not thread-safe // FsBlockstoreProvider provides handle to block storage - this is not thread-safe
......
...@@ -21,9 +21,9 @@ import ( ...@@ -21,9 +21,9 @@ import (
"fmt" "fmt"
"github.com/hyperledger/fabric/core/ledger" "github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/core/ledger/blkstorage" "github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/testutil" "github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
) )
......
...@@ -22,8 +22,8 @@ import ( ...@@ -22,8 +22,8 @@ import (
"os" "os"
"testing" "testing"
"github.com/hyperledger/fabric/core/ledger/blkstorage" "github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/testutil" "github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
) )
......
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ledger
import (
"github.com/hyperledger/fabric/protos/common"
)
// Ledger captures the methods that are common across the 'PeerLedger', 'OrdererLedger', and 'ValidatedLedger'
type Ledger interface {
// GetBlockchainInfo returns basic info about blockchain
GetBlockchainInfo() (*common.BlockchainInfo, error)
// GetBlockByNumber returns block at a given height
// blockNumber of math.MaxUint64 will return last block
GetBlockByNumber(blockNumber uint64) (*common.Block, error)
// GetBlocksIterator returns an iterator that starts from `startBlockNumber`(inclusive).
// The iterator is a blocking iterator i.e., it blocks till the next block gets available in the ledger
// ResultsIterator contains type BlockHolder
GetBlocksIterator(startBlockNumber uint64) (ResultsIterator, error)
// Close closes the ledger
Close()
// Commit adds a new block
Commit(block *common.Block) error
}
// ResultsIterator - an iterator for query result set
type ResultsIterator interface {
// Next returns the next item in the result set. The `QueryResult` is expected to be nil when
// the iterator gets exhausted
Next() (QueryResult, error)
// Close releases resources occupied by the iterator
Close()
}
// QueryResult - a general interface for supporting different types of query results. Actual types differ for different queries
type QueryResult interface{}
// BlockHolder holds block returned by the iterator in GetBlocksIterator.
// The sole purpose of this holder is to avoid desrialization if block is desired in raw bytes form (e.g., for transfer)
type BlockHolder interface {
GetBlock() *common.Block
GetBlockBytes() []byte
}
// PrunePolicy - a general interface for supporting different pruning policies
type PrunePolicy interface{}
...@@ -22,7 +22,6 @@ import ( ...@@ -22,7 +22,6 @@ import (
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/util" "github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
ptestutils "github.com/hyperledger/fabric/protos/testutils" ptestutils "github.com/hyperledger/fabric/protos/testutils"
) )
...@@ -93,14 +92,14 @@ func ConstructTestBlocks(t *testing.T, numBlocks int) []*common.Block { ...@@ -93,14 +92,14 @@ func ConstructTestBlocks(t *testing.T, numBlocks int) []*common.Block {
// ConstructTransaction constructs a transaction for testing // ConstructTransaction constructs a transaction for testing
func ConstructTransaction(t *testing.T, simulationResults []byte, sign bool) (*common.Envelope, string, error) { func ConstructTransaction(t *testing.T, simulationResults []byte, sign bool) (*common.Envelope, string, error) {
ccName := "foo" ccName := "foo"
response := &pb.Response{Status: 200} //response := &pb.Response{Status: 200}
txID := util.GenerateUUID() txID := util.GenerateUUID()
var txEnv *common.Envelope var txEnv *common.Envelope
var err error var err error
if sign { if sign {
txEnv, err = ptestutils.ConstructSingedTxEnvWithDefaultSigner(txID, util.GetTestChainID(), ccName, response, simulationResults, nil, nil) txEnv, err = ptestutils.ConstructSingedTxEnvWithDefaultSigner(txID, util.GetTestChainID(), ccName, nil, simulationResults, nil, nil)
} else { } else {
txEnv, err = ptestutils.ConstructUnsingedTxEnv(txID, util.GetTestChainID(), ccName, response, simulationResults, nil, nil) txEnv, err = ptestutils.ConstructUnsingedTxEnv(txID, util.GetTestChainID(), ccName, nil, simulationResults, nil, nil)
} }
return txEnv, txID, err return txEnv, txID, err
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment