Commit 722e790d authored by manish's avatar manish
Browse files

[FAB-2676] Allow create-ledger with genesis block only

This CR removes the option of peer ledger creation
without genesis block

  - Changed the APIs in PeerLedgerProvider interface
    and ledgermgmt.CreateLedger function

  - Fixed the usage of the ledger creation API in peer code and tests

  - Marked one test (core/scc/qscc:TestQueryGetBlockByTxID) to skip
    which should be made to normal once
    https://jira.hyperledger.org/browse/FAB-3091

 is resolved

Change-Id: Ife926c20226f45c5586b75b37881f374d42075d6
Signed-off-by: default avatarmanish <manish.sethi@gmail.com>
parent 7f114bba
......@@ -160,6 +160,9 @@ func extractHeader(buf *ledgerutil.Buffer) (*common.BlockHeader, error) {
if header.PreviousHash, err = buf.DecodeRawBytes(false); err != nil {
return nil, err
}
if len(header.PreviousHash) == 0 {
header.PreviousHash = nil
}
return header, nil
}
......
......@@ -25,7 +25,7 @@ import (
)
func TestBlockSerialization(t *testing.T) {
block := testutil.ConstructTestBlock(t, 10, 100)
block := testutil.ConstructTestBlock(t, 1, 10, 100)
bb, _, err := serializeBlock(block)
testutil.AssertNoError(t, err, "")
deserializedBlock, err := deserializeBlock(bb)
......@@ -42,7 +42,7 @@ func TestExtractTxid(t *testing.T) {
}
func TestSerializedBlockInfo(t *testing.T) {
block := testutil.ConstructTestBlock(t, 10, 100)
block := testutil.ConstructTestBlock(t, 1, 10, 100)
bb, info, err := serializeBlock(block)
testutil.AssertNoError(t, err, "")
infoFromBB, err := extractSerializedBlockInfo(bb)
......
......@@ -21,6 +21,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/protos/common"
)
func TestBlockfileStream(t *testing.T) {
......@@ -100,16 +101,23 @@ func TestBlockStream(t *testing.T) {
}
func testBlockStream(t *testing.T, numFiles int) {
ledgerID := "testLedger"
env := newTestEnv(t, NewConf(testPath(), 0))
defer env.Cleanup()
w := newTestBlockfileWrapper(env, "testLedger")
w := newTestBlockfileWrapper(env, ledgerID)
defer w.close()
blockfileMgr := w.blockfileMgr
numBlocksInEachFile := 10
bg := testutil.NewBlockGenerator(t)
bg, gb := testutil.NewBlockGenerator(t, ledgerID, false)
w.addBlocks([]*common.Block{gb})
for i := 0; i < numFiles; i++ {
blocks := bg.NextTestBlocks(numBlocksInEachFile)
numBlocks := numBlocksInEachFile
if i == 0 {
// genesis block already added so adding one less block
numBlocks = numBlocksInEachFile - 1
}
blocks := bg.NextTestBlocks(numBlocks)
w.addBlocks(blocks)
blockfileMgr.moveToNextFile()
}
......
......@@ -51,8 +51,12 @@ func testBlockfileMgrCrashDuringWriting(t *testing.T, numBlocksBeforeCheckpoint
defer env.Cleanup()
ledgerid := "testLedger"
blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
bg := testutil.NewBlockGenerator(t)
blocksBeforeCP := bg.NextTestBlocks(numBlocksBeforeCheckpoint)
bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
blocksBeforeCP := []*common.Block{}
if numBlocksBeforeCheckpoint > 0 {
blocksBeforeCP = append(blocksBeforeCP, gb)
}
blocksBeforeCP = append(blocksBeforeCP, bg.NextTestBlocks(numBlocksBeforeCheckpoint-1)...)
blkfileMgrWrapper.addBlocks(blocksBeforeCP)
currentCPInfo := blkfileMgrWrapper.blockfileMgr.cpInfo
cpInfo1 := &checkpointInfo{
......@@ -61,7 +65,14 @@ func testBlockfileMgrCrashDuringWriting(t *testing.T, numBlocksBeforeCheckpoint
currentCPInfo.isChainEmpty,
currentCPInfo.lastBlockNumber}
blocksAfterCP := bg.NextTestBlocks(numBlocksAfterCheckpoint)
blocksAfterCP := []*common.Block{}
if numBlocksBeforeCheckpoint == 0 {
blocksAfterCP = append(blocksAfterCP, gb)
blocksAfterCP = append(blocksAfterCP, bg.NextTestBlocks(numBlocksAfterCheckpoint-1)...)
} else {
blocksAfterCP = bg.NextTestBlocks(numBlocksAfterCheckpoint)
}
blkfileMgrWrapper.addBlocks(blocksAfterCP)
cpInfo2 := blkfileMgrWrapper.blockfileMgr.cpInfo
......@@ -140,11 +151,11 @@ func TestBlockfileMgrGetTxById(t *testing.T) {
defer env.Cleanup()
blkfileMgrWrapper := newTestBlockfileWrapper(env, "testLedger")
defer blkfileMgrWrapper.close()
blocks := testutil.ConstructTestBlocks(t, 10)
blocks := testutil.ConstructTestBlocks(t, 2)
blkfileMgrWrapper.addBlocks(blocks)
for _, blk := range blocks {
for j, txEnvelopeBytes := range blk.Data.Data {
// blockNum starts with 1
// blockNum starts with 0
txID, err := extractTxID(blk.Data.Data[j])
testutil.AssertNoError(t, err, "")
txEnvelopeFromFileMgr, err := blkfileMgrWrapper.blockfileMgr.retrieveTransactionByID(txID)
......
......@@ -31,8 +31,8 @@ func TestBlockFileScanSmallTxOnly(t *testing.T) {
defer env.Cleanup()
ledgerid := "testLedger"
blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
bg := testutil.NewBlockGenerator(t)
blocks := []*common.Block{}
bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
blocks := []*common.Block{gb}
blocks = append(blocks, bg.NextTestBlock(0, 0))
blocks = append(blocks, bg.NextTestBlock(0, 0))
blocks = append(blocks, bg.NextTestBlock(0, 0))
......@@ -54,8 +54,8 @@ func TestBlockFileScanSmallTxLastTxIncomplete(t *testing.T) {
defer env.Cleanup()
ledgerid := "testLedger"
blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
bg := testutil.NewBlockGenerator(t)
blocks := []*common.Block{}
bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
blocks := []*common.Block{gb}
blocks = append(blocks, bg.NextTestBlock(0, 0))
blocks = append(blocks, bg.NextTestBlock(0, 0))
blocks = append(blocks, bg.NextTestBlock(0, 0))
......
......@@ -100,7 +100,7 @@ func (w *testBlockfileMgrWrapper) testGetBlockByNumber(blocks []*common.Block, s
for i := 0; i < len(blocks); i++ {
b, err := w.blockfileMgr.retrieveBlockByNumber(startingNum + uint64(i))
testutil.AssertNoError(w.t, err, fmt.Sprintf("Error while retrieving [%d]th block from blockfileMgr", i))
testutil.AssertEquals(w.t, b, blocks[i])
testutil.AssertEquals(w.t, b.Header, blocks[i].Header)
}
// test getting the last block
b, err := w.blockfileMgr.retrieveBlockByNumber(math.MaxUint64)
......
......@@ -20,36 +20,36 @@ import (
"testing"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
"github.com/hyperledger/fabric/common/util"
lutils "github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/protos/common"
ptestutils "github.com/hyperledger/fabric/protos/testutils"
"github.com/hyperledger/fabric/protos/utils"
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
)
//BlockGenerator generates a series of blocks for testing
type BlockGenerator struct {
blockNum uint64
previousHash []byte
signTxs bool
t *testing.T
}
// NewBlockGenerator instantiates new BlockGenerator for testing
func NewBlockGenerator(t *testing.T) *BlockGenerator {
return &BlockGenerator{0, []byte{}, t}
func NewBlockGenerator(t *testing.T, ledgerID string, signTxs bool) (*BlockGenerator, *common.Block) {
gb, err := test.MakeGenesisBlock(ledgerID)
AssertNoError(t, err, "")
gb.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = lutils.NewTxValidationFlags(len(gb.Data.Data))
return &BlockGenerator{1, gb.GetHeader().Hash(), signTxs, t}, gb
}
// NextBlock constructs next block in sequence that includes a number of transactions - one per simulationResults
func (bg *BlockGenerator) NextBlock(simulationResults [][]byte, sign bool) *common.Block {
envs := []*common.Envelope{}
for i := 0; i < len(simulationResults); i++ {
env, _, err := ConstructTransaction(bg.t, simulationResults[i], sign)
if err != nil {
bg.t.Fatalf("ConstructTestTransaction failed, err %s", err)
}
envs = append(envs, env)
}
block := newBlock(envs, bg.blockNum, bg.previousHash)
func (bg *BlockGenerator) NextBlock(simulationResults [][]byte) *common.Block {
block := ConstructBlock(bg.t, bg.blockNum, bg.previousHash, simulationResults, bg.signTxs)
bg.blockNum++
bg.previousHash = block.Header.Hash()
return block
......@@ -61,7 +61,7 @@ func (bg *BlockGenerator) NextTestBlock(numTx int, txSize int) *common.Block {
for i := 0; i < numTx; i++ {
simulationResults = append(simulationResults, ConstructRandomBytes(bg.t, txSize))
}
return bg.NextBlock(simulationResults, false)
return bg.NextBlock(simulationResults)
}
// NextTestBlocks constructs 'numBlocks' number of blocks for testing
......@@ -73,24 +73,6 @@ func (bg *BlockGenerator) NextTestBlocks(numBlocks int) []*common.Block {
return blocks
}
// ConstructBlock constructs a single block with blockNum=1
func ConstructBlock(t *testing.T, simulationResults [][]byte, sign bool) *common.Block {
bg := NewBlockGenerator(t)
return bg.NextBlock(simulationResults, sign)
}
// ConstructTestBlock constructs a single block with blocknum=1
func ConstructTestBlock(t *testing.T, numTx int, txSize int) *common.Block {
bg := NewBlockGenerator(t)
return bg.NextTestBlock(numTx, txSize)
}
// ConstructTestBlocks returns a series of blocks starting with blockNum=1
func ConstructTestBlocks(t *testing.T, numBlocks int) []*common.Block {
bg := NewBlockGenerator(t)
return bg.NextTestBlocks(numBlocks)
}
// ConstructTransaction constructs a transaction for testing
func ConstructTransaction(_ *testing.T, simulationResults []byte, sign bool) (*common.Envelope, string, error) {
ccName := "foo"
......@@ -106,6 +88,39 @@ func ConstructTransaction(_ *testing.T, simulationResults []byte, sign bool) (*c
return txEnv, txID, err
}
// ConstructBlock constructs a single block
func ConstructBlock(t *testing.T, blockNum uint64, previousHash []byte, simulationResults [][]byte, sign bool) *common.Block {
envs := []*common.Envelope{}
for i := 0; i < len(simulationResults); i++ {
env, _, err := ConstructTransaction(t, simulationResults[i], sign)
if err != nil {
t.Fatalf("ConstructTestTransaction failed, err %s", err)
}
envs = append(envs, env)
}
return newBlock(envs, blockNum, previousHash)
}
//ConstructTestBlock constructs a single block with random contents
func ConstructTestBlock(t *testing.T, blockNum uint64, numTx int, txSize int) *common.Block {
simulationResults := [][]byte{}
for i := 0; i < numTx; i++ {
simulationResults = append(simulationResults, ConstructRandomBytes(t, txSize))
}
return ConstructBlock(t, blockNum, ConstructRandomBytes(t, 32), simulationResults, false)
}
// ConstructTestBlocks returns a series of blocks starting with blockNum=0.
// The first block in the returned array is a config tx block that represents a genesis block
func ConstructTestBlocks(t *testing.T, numBlocks int) []*common.Block {
bg, gb := NewBlockGenerator(t, util.GetTestChainID(), false)
blocks := []*common.Block{}
if numBlocks != 0 {
blocks = append(blocks, gb)
}
return append(blocks, bg.NextTestBlocks(numBlocks-1)...)
}
func newBlock(env []*common.Envelope, blockNum uint64, previousHash []byte) *common.Block {
block := common.NewBlock(blockNum, previousHash)
for i := 0; i < len(env); i++ {
......@@ -119,3 +134,9 @@ func newBlock(env []*common.Envelope, blockNum uint64, previousHash []byte) *com
return block
}
func MakeGenesisBlock() *common.Block {
genConf := genesisconfig.Load(genesisconfig.SampleInsecureProfile)
gb := provisional.New(genConf).GenesisBlock()
return gb
}
......@@ -571,9 +571,8 @@ func checkFinalState(cccid *ccprovider.CCContext) error {
// Invoke chaincode_example02
func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContext, cID *pb.ChaincodeID, chaincodeType pb.ChaincodeSpec_Type, args []string, destroyImage bool) error {
var nextBlockNumber uint64
// the ledger is created with genesis block. Start block number 1 onwards
var nextBlockNumber uint64 = 1
f := "init"
argsDeploy := util.ToChaincodeArgs(f, "a", "100", "b", "200")
spec := &pb.ChaincodeSpec{Type: chaincodeType, ChaincodeId: cID, Input: &pb.ChaincodeInput{Args: argsDeploy}}
......@@ -645,7 +644,7 @@ func runChaincodeInvokeChaincode(t *testing.T, chainID1 string, chainID2 string,
sProp, prop := putils.MockSignedEndorserProposalOrPanic(chainID1, spec1, []byte([]byte("Alice")), nil)
cccid1 := ccprovider.NewCCContext(chainID1, "example02", "0", "", false, sProp, prop)
var nextBlockNumber uint64
var nextBlockNumber uint64 = 1
_, err = deploy(ctxt, cccid1, spec1, nextBlockNumber)
nextBlockNumber++
......@@ -741,7 +740,7 @@ func runChaincodeInvokeChaincode(t *testing.T, chainID1 string, chainID2 string,
sProp, prop = putils.MockSignedEndorserProposalOrPanic(chainID2, spec3, []byte([]byte("Alice")), nil)
cccid3 := ccprovider.NewCCContext(chainID2, "example04", "0", "", false, sProp, prop)
_, err = deploy(ctxt, cccid3, spec3, 0)
_, err = deploy(ctxt, cccid3, spec3, 1)
chaincodeID2 := spec2.ChaincodeId.Name
if err != nil {
t.Fail()
......@@ -764,7 +763,7 @@ func runChaincodeInvokeChaincode(t *testing.T, chainID1 string, chainID2 string,
//var uuid string
// Bob should not be able to call
_, _, _, err = invoke(ctxt, chainID2, spec2, 1, []byte("Bob"))
_, _, _, err = invoke(ctxt, chainID2, spec2, 2, []byte("Bob"))
if err == nil {
t.Fail()
t.Logf("Bob invoking <%s> should fail. It did not happen instead: %s", chaincodeID2, err)
......@@ -775,7 +774,7 @@ func runChaincodeInvokeChaincode(t *testing.T, chainID1 string, chainID2 string,
}
// Alice should be able to call
_, _, _, err = invoke(ctxt, chainID2, spec2, 1, []byte("Alice"))
_, _, _, err = invoke(ctxt, chainID2, spec2, 2, []byte("Alice"))
if err != nil {
t.Fail()
t.Logf("Alice invoking <%s> should not fail. It did happen instead: %s", chaincodeID2, err)
......@@ -953,7 +952,7 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) {
sProp, prop := putils.MockSignedEndorserProposalOrPanic(util.GetTestChainID(), spec1, []byte([]byte("Alice")), nil)
cccid1 := ccprovider.NewCCContext(chainID, "example02", "0", "", false, sProp, prop)
var nextBlockNumber uint64
var nextBlockNumber uint64 = 1
_, err = deploy(ctxt, cccid1, spec1, nextBlockNumber)
nextBlockNumber++
......@@ -1044,7 +1043,7 @@ func TestQueries(t *testing.T) {
cccid := ccprovider.NewCCContext(chainID, "tmap", "0", "", false, nil, nil)
var nextBlockNumber uint64
var nextBlockNumber uint64 = 1
_, err = deploy(ctxt, cccid, spec, nextBlockNumber)
nextBlockNumber++
ccID := spec.ChaincodeId.Name
......@@ -1398,7 +1397,7 @@ func TestGetEvent(t *testing.T) {
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: cID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(f)}}
cccid := ccprovider.NewCCContext(chainID, "esender", "0", "", false, nil, nil)
var nextBlockNumber uint64
var nextBlockNumber uint64 = 1
_, err = deploy(ctxt, cccid, spec, nextBlockNumber)
nextBlockNumber++
ccID := spec.ChaincodeId.Name
......@@ -1580,7 +1579,7 @@ func TestChaincodeInvokesForbiddenSystemChaincode(t *testing.T) {
var ctxt = context.Background()
var nextBlockNumber uint64
var nextBlockNumber uint64 = 1
// Deploy second chaincode
url := "github.com/hyperledger/fabric/examples/chaincode/go/passthru"
......@@ -1635,7 +1634,7 @@ func TestChaincodeInvokesSystemChaincode(t *testing.T) {
var ctxt = context.Background()
var nextBlockNumber uint64
var nextBlockNumber uint64 = 1
// Deploy second chaincode
url := "github.com/hyperledger/fabric/examples/chaincode/go/passthru"
......
......@@ -104,7 +104,8 @@ func deploySampleSysCC(t *testing.T, ctxt context.Context, chainID string) error
args := util.ToChaincodeArgs(f, "greeting", "hey there")
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "sample_syscc", Path: url, Version: sysCCVers}, Input: &pb.ChaincodeInput{Args: args}}
var nextBlockNumber uint64
// the ledger is created with genesis block. Start block number 1 onwards
var nextBlockNumber uint64 = 1
_, _, _, err := invokeWithVersion(ctxt, chainID, sysCCVers, spec, nextBlockNumber, nil)
nextBlockNumber++
......
......@@ -146,7 +146,7 @@ func TestUpgradeCC(t *testing.T) {
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: args}}
cccid := ccprovider.NewCCContext(chainID, ccName, "0", "", false, nil, nil)
var nextBlockNumber uint64
var nextBlockNumber uint64 = 1
_, err = deploy(ctxt, cccid, spec, nextBlockNumber)
if err != nil {
......
......@@ -19,6 +19,7 @@ package committer
import (
"testing"
"github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
......@@ -32,18 +33,20 @@ func TestKVLedgerBlockStorage(t *testing.T) {
viper.Set("peer.fileSystemPath", "/tmp/fabric/committertest")
ledgermgmt.InitializeTestEnv()
defer ledgermgmt.CleanupTestEnv()
ledger, err := ledgermgmt.CreateLedger("TestLedger")
gb, _ := test.MakeGenesisBlock("TestLedger")
gbHash := gb.Header.Hash()
ledger, err := ledgermgmt.CreateLedger(gb)
assert.NoError(t, err, "Error while creating ledger: %s", err)
defer ledger.Close()
committer := NewLedgerCommitter(ledger, &validator.MockValidator{})
height, err := committer.LedgerHeight()
assert.Equal(t, uint64(0), height)
assert.Equal(t, uint64(1), height)
assert.NoError(t, err)
bcInfo, _ := ledger.GetBlockchainInfo()
testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
Height: 0, CurrentBlockHash: nil, PreviousBlockHash: nil})
Height: 1, CurrentBlockHash: gbHash, PreviousBlockHash: nil})
simulator, _ := ledger.NewTxSimulator()
simulator.SetState("ns1", "key1", []byte("value1"))
......@@ -52,13 +55,13 @@ func TestKVLedgerBlockStorage(t *testing.T) {
simulator.Done()
simRes, _ := simulator.GetTxSimulationResults()
block0 := testutil.ConstructBlock(t, [][]byte{simRes}, true)
block1 := testutil.ConstructBlock(t, 1, gbHash, [][]byte{simRes}, true)
err = committer.Commit(block0)
err = committer.Commit(block1)
assert.NoError(t, err)
height, err = committer.LedgerHeight()
assert.Equal(t, uint64(1), height)
assert.Equal(t, uint64(2), height)
assert.NoError(t, err)
blocks := committer.GetBlocks([]uint64{0})
......@@ -66,7 +69,7 @@ func TestKVLedgerBlockStorage(t *testing.T) {
assert.NoError(t, err)
bcInfo, _ = ledger.GetBlockchainInfo()
block1Hash := block0.Header.Hash()
block1Hash := block1.Header.Hash()
testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
Height: 1, CurrentBlockHash: block1Hash, PreviousBlockHash: []byte{}})
Height: 2, CurrentBlockHash: block1Hash, PreviousBlockHash: gbHash})
}
......@@ -20,6 +20,7 @@ import (
"testing"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/ledger/testutil"
util2 "github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
......@@ -41,14 +42,17 @@ func TestFirstBlockValidation(t *testing.T) {
viper.Set("peer.fileSystemPath", "/tmp/fabric/txvalidatortest")
ledgermgmt.InitializeTestEnv()
defer ledgermgmt.CleanupTestEnv()
ledger, _ := ledgermgmt.CreateLedger("TestLedger")
gb, _ := test.MakeGenesisBlock("TestLedger")
gbHash := gb.Header.Hash()
ledger, _ := ledgermgmt.CreateLedger(gb)
defer ledger.Close()
tValidator := &txValidator{&mocktxvalidator.Support{LedgerVal: ledger}, &validator.MockVsccValidator{}}
bcInfo, _ := ledger.GetBlockchainInfo()
testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
Height: 0, CurrentBlockHash: nil, PreviousBlockHash: nil})
Height: 1, CurrentBlockHash: gbHash, PreviousBlockHash: nil})
simulator, _ := ledger.NewTxSimulator()
simulator.SetState("ns1", "key1", []byte("value1"))
......@@ -57,7 +61,7 @@ func TestFirstBlockValidation(t *testing.T) {
simulator.Done()
simRes, _ := simulator.GetTxSimulationResults()
block := testutil.ConstructBlock(t, [][]byte{simRes}, true)
block := testutil.ConstructBlock(t, 1, gbHash, [][]byte{simRes}, true)
tValidator.Validate(block)
......@@ -69,7 +73,10 @@ func TestNewTxValidator_DuplicateTransactions(t *testing.T) {
viper.Set("peer.fileSystemPath", "/tmp/fabric/txvalidatortest")
ledgermgmt.InitializeTestEnv()
defer ledgermgmt.CleanupTestEnv()
ledger, _ := ledgermgmt.CreateLedger("TestLedger")
gb, _ := test.MakeGenesisBlock("TestLedger")
ledger, _ := ledgermgmt.CreateLedger(gb)
defer ledger.Close()
tValidator := &txValidator{&mocktxvalidator.Support{LedgerVal: ledger}, &validator.MockVsccValidator{}}
......
......@@ -395,7 +395,7 @@ func TestDeployAndInvoke(t *testing.T) {
chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}})
return
}
var nextBlockNumber uint64 // first block
var nextBlockNumber uint64 = 1 // first block needs to be block number = 1. Genesis block is block 0
err = endorserServer.(*Endorser).commitTxSimulation(prop, chainID, signer, resp, nextBlockNumber)
if err != nil {
t.Fail()
......@@ -485,7 +485,7 @@ func TestDeployAndUpgrade(t *testing.T) {
return
}
var nextBlockNumber uint64 = 2 // something above created block 0
var nextBlockNumber uint64 = 3 // something above created block 0
err = endorserServer.(*Endorser).commitTxSimulation(prop, chainID, signer, resp, nextBlockNumber)
if err != nil {
t.Fail()
......
......@@ -20,6 +20,7 @@ import (
"fmt"
"os"
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/kvledger/example"
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
......@@ -54,7 +55,9 @@ func init() {
cleanup()
ledgermgmt.Initialize()
var err error
peerLedger, err = ledgermgmt.CreateLedger(ledgerID)
gb, _ := configtxtest.MakeGenesisBlock(ledgerID)
peerLedger, err = ledgermgmt.CreateLedger(gb)
if err != nil {
panic(fmt.Errorf("Error in NewKVLedger(): %s", err))
}
......
......@@ -46,20 +46,23 @@ func TestSavepoint(t *testing.T) {
testutil.AssertNoError(t, err, "Error upon historyDatabase.GetLastSavepoint()")
testutil.AssertNil(t, savepoint)
// create the first block (block 0)
bg, gb := testutil.NewBlockGenerator(t, "testLedger", false)
testutil.AssertNoError(t, env.testHistoryDB.Commit(gb), "")
// read the savepoint, it should now exist and return a Height object with BlockNum 0
savepoint, err = env.testHistoryDB.GetLastSavepoint()
testutil.AssertNoError(t, err, "Error upon historyDatabase.GetLastSavepoint()")
testutil.AssertEquals(t, savepoint.BlockNum, uint64(0))
// create the next block (block 1)
simulator, _ := env.txmgr.NewTxSimulator()
simulator.SetState("ns1", "key1", []byte("value1"))
simulator.Done()
simRes, _ := simulator.GetTxSimulationResults()
bg := testutil.NewBlockGenerator(t)
block1 := bg.NextBlock([][]byte{simRes}, false)
err = env.testHistoryDB.Commit(block1)
testutil.AssertNoError(t, err, "")
// read the savepoint, it should now exist and return a Height object with BlockNum 0
block1 := bg.NextBlock([][]byte{simRes})
testutil.AssertNoError(t, env.testHistoryDB.Commit(block1), "")
savepoint, err = env.testHistoryDB.GetLastSavepoint()
testutil.AssertNoError(t, err, "Error upon historyDatabase.GetLastSavepoint()")
testutil.AssertEquals(t, savepoint.BlockNum, uint64(0))
testutil.AssertEquals(t, savepoint.BlockNum, uint64(1))
}
func TestHistory(t *testing.T) {
......@@ -67,18 +70,22 @@ func TestHistory(t *testing.T) {
env := NewTestHistoryEnv(t)
defer env.cleanup()
provider := env.testBlockStorageEnv.provider
store1, err := provider.OpenBlockStore("ledger1")
ledger1id := "ledger1"
store1, err := provider.OpenBlockStore(ledger1id)
testutil.AssertNoError(t, err, "Error upon provider.OpenBlockStore()")
defer store1.Shutdown()
bg, gb := testutil.NewBlockGenerator(t, ledger1id, false)
testutil.AssertNoError(t, store1.AddBlock(gb), "")
testutil.AssertNoError(t, env.testHistoryDB.Commit(gb), "")
//block1
simulator, _ := env.txmgr.NewTxSimulator()
value1 := []byte("value1")
simulator.SetState("ns1", "key7", value1)
simulator.Done()
simRes, _ := simulator.GetTxSimulationResults()