Skip to content

Commit

Permalink
Move Blockstorage code under /fabric/common package
Browse files Browse the repository at this point in the history
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: manish <manish.sethi@gmail.com>
  • Loading branch information
manish-sethi committed Feb 3, 2017
1 parent e67a238 commit 2a16532
Show file tree
Hide file tree
Showing 82 changed files with 626 additions and 490 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ package blkstorage
import (
"errors"

"github.com/hyperledger/fabric/core/ledger"

"github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
)

// IndexableAttr represents an indexable attribute
Expand Down Expand Up @@ -62,7 +60,7 @@ type BlockStoreProvider interface {
// of type `IndexConfig` which configures the block store on what items should be indexed
type BlockStore interface {
AddBlock(block *common.Block) error
GetBlockchainInfo() (*pb.BlockchainInfo, error)
GetBlockchainInfo() (*common.BlockchainInfo, error)
RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error)
RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of math.MaxUint64 will return last block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"

"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/utils"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"testing"

"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"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"testing"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/common/ledger/testutil"
)

func TestBlockfileStream(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ import (
"sync/atomic"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/core/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putil "github.com/hyperledger/fabric/protos/utils"
"github.com/op/go-logging"
)
Expand Down Expand Up @@ -153,7 +152,7 @@ func newBlockfileMgr(id string, conf *Conf, indexConfig *blkstorage.IndexConfig,
mgr.syncIndex()

// init BlockchainInfo for external API's
bcInfo := &pb.BlockchainInfo{
bcInfo := &common.BlockchainInfo{
Height: 0,
CurrentBlockHash: nil,
PreviousBlockHash: nil}
Expand All @@ -166,7 +165,7 @@ func newBlockfileMgr(id string, conf *Conf, indexConfig *blkstorage.IndexConfig,
}
lastBlockHash := lastBlockHeader.Hash()
previousBlockHash := lastBlockHeader.PreviousHash
bcInfo = &pb.BlockchainInfo{
bcInfo = &common.BlockchainInfo{
Height: cpInfo.lastBlockNumber,
CurrentBlockHash: lastBlockHash,
PreviousBlockHash: previousBlockHash}
Expand Down Expand Up @@ -373,8 +372,8 @@ func (mgr *blockfileMgr) syncIndex() error {
return nil
}

func (mgr *blockfileMgr) getBlockchainInfo() *pb.BlockchainInfo {
return mgr.bcInfo.Load().(*pb.BlockchainInfo)
func (mgr *blockfileMgr) getBlockchainInfo() *common.BlockchainInfo {
return mgr.bcInfo.Load().(*common.BlockchainInfo)
}

func (mgr *blockfileMgr) updateCheckpoint(cpInfo *checkpointInfo) {
Expand All @@ -387,7 +386,7 @@ func (mgr *blockfileMgr) updateCheckpoint(cpInfo *checkpointInfo) {

func (mgr *blockfileMgr) updateBlockchainInfo(latestBlockHash []byte, latestBlock *common.Block) {
currentBCInfo := mgr.getBlockchainInfo()
newBCInfo := &pb.BlockchainInfo{
newBCInfo := &common.BlockchainInfo{
Height: currentBCInfo.Height + 1,
CurrentBlockHash: latestBlockHash,
PreviousBlockHash: latestBlock.Header.PreviousHash}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import (
"testing"

"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"
pb "github.com/hyperledger/fabric/protos/peer"
putil "github.com/hyperledger/fabric/protos/utils"
)

Expand Down Expand Up @@ -127,7 +126,7 @@ func TestBlockfileMgrBlockchainInfo(t *testing.T) {
defer blkfileMgrWrapper.close()

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)
blkfileMgrWrapper.addBlocks(blocks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"os"
"testing"

"github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/common/ledger/util"

"github.com/hyperledger/fabric/protos/common"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"fmt"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/core/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"fmt"
"testing"

"github.com/hyperledger/fabric/core/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/common/ledger/testutil"
putil "github.com/hyperledger/fabric/protos/utils"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"
"sync"

"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/common/ledger"

"github.com/hyperledger/fabric/protos/common"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"testing"
"time"

"github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/protos/common"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ limitations under the License.
package fsblkstorage

import (
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"

"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
)

// fsBlockStore - filesystem based implementation for `BlockStore`
Expand All @@ -44,7 +43,7 @@ func (store *fsBlockStore) AddBlock(block *common.Block) error {
}

// 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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package fsblkstorage

import (
"github.com/hyperledger/fabric/core/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/core/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
)

// FsBlockstoreProvider provides handle to block storage - this is not thread-safe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (

"fmt"

"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/protos/common"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"os"
"testing"

"github.com/hyperledger/fabric/core/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/common/ledger/testutil"

"github.com/hyperledger/fabric/protos/common"
)
Expand Down
60 changes: 60 additions & 0 deletions common/ledger/ledger_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
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{}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
ptestutils "github.com/hyperledger/fabric/protos/testutils"
)

Expand Down Expand Up @@ -93,14 +92,14 @@ func ConstructTestBlocks(t *testing.T, numBlocks int) []*common.Block {
// ConstructTransaction constructs a transaction for testing
func ConstructTransaction(t *testing.T, simulationResults []byte, sign bool) (*common.Envelope, string, error) {
ccName := "foo"
response := &pb.Response{Status: 200}
//response := &pb.Response{Status: 200}
txID := util.GenerateUUID()
var txEnv *common.Envelope
var err error
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 {
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
}
Expand Down
Loading

0 comments on commit 2a16532

Please sign in to comment.