Skip to content

Commit 722e790

Browse files
committed
[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: manish <[email protected]>
1 parent 7f114bb commit 722e790

28 files changed

+254
-242
lines changed

common/ledger/blkstorage/fsblkstorage/block_serialization.go

+3
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ func extractHeader(buf *ledgerutil.Buffer) (*common.BlockHeader, error) {
160160
if header.PreviousHash, err = buf.DecodeRawBytes(false); err != nil {
161161
return nil, err
162162
}
163+
if len(header.PreviousHash) == 0 {
164+
header.PreviousHash = nil
165+
}
163166
return header, nil
164167
}
165168

common/ledger/blkstorage/fsblkstorage/block_serialization_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
func TestBlockSerialization(t *testing.T) {
28-
block := testutil.ConstructTestBlock(t, 10, 100)
28+
block := testutil.ConstructTestBlock(t, 1, 10, 100)
2929
bb, _, err := serializeBlock(block)
3030
testutil.AssertNoError(t, err, "")
3131
deserializedBlock, err := deserializeBlock(bb)
@@ -42,7 +42,7 @@ func TestExtractTxid(t *testing.T) {
4242
}
4343

4444
func TestSerializedBlockInfo(t *testing.T) {
45-
block := testutil.ConstructTestBlock(t, 10, 100)
45+
block := testutil.ConstructTestBlock(t, 1, 10, 100)
4646
bb, info, err := serializeBlock(block)
4747
testutil.AssertNoError(t, err, "")
4848
infoFromBB, err := extractSerializedBlockInfo(bb)

common/ledger/blkstorage/fsblkstorage/block_stream_test.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/golang/protobuf/proto"
2323
"github.com/hyperledger/fabric/common/ledger/testutil"
24+
"github.com/hyperledger/fabric/protos/common"
2425
)
2526

2627
func TestBlockfileStream(t *testing.T) {
@@ -100,16 +101,23 @@ func TestBlockStream(t *testing.T) {
100101
}
101102

102103
func testBlockStream(t *testing.T, numFiles int) {
104+
ledgerID := "testLedger"
103105
env := newTestEnv(t, NewConf(testPath(), 0))
104106
defer env.Cleanup()
105-
w := newTestBlockfileWrapper(env, "testLedger")
107+
w := newTestBlockfileWrapper(env, ledgerID)
106108
defer w.close()
107109
blockfileMgr := w.blockfileMgr
108110

109111
numBlocksInEachFile := 10
110-
bg := testutil.NewBlockGenerator(t)
112+
bg, gb := testutil.NewBlockGenerator(t, ledgerID, false)
113+
w.addBlocks([]*common.Block{gb})
111114
for i := 0; i < numFiles; i++ {
112-
blocks := bg.NextTestBlocks(numBlocksInEachFile)
115+
numBlocks := numBlocksInEachFile
116+
if i == 0 {
117+
// genesis block already added so adding one less block
118+
numBlocks = numBlocksInEachFile - 1
119+
}
120+
blocks := bg.NextTestBlocks(numBlocks)
113121
w.addBlocks(blocks)
114122
blockfileMgr.moveToNextFile()
115123
}

common/ledger/blkstorage/fsblkstorage/blockfile_mgr_test.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ func testBlockfileMgrCrashDuringWriting(t *testing.T, numBlocksBeforeCheckpoint
5151
defer env.Cleanup()
5252
ledgerid := "testLedger"
5353
blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
54-
bg := testutil.NewBlockGenerator(t)
55-
blocksBeforeCP := bg.NextTestBlocks(numBlocksBeforeCheckpoint)
54+
bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
55+
blocksBeforeCP := []*common.Block{}
56+
if numBlocksBeforeCheckpoint > 0 {
57+
blocksBeforeCP = append(blocksBeforeCP, gb)
58+
}
59+
blocksBeforeCP = append(blocksBeforeCP, bg.NextTestBlocks(numBlocksBeforeCheckpoint-1)...)
5660
blkfileMgrWrapper.addBlocks(blocksBeforeCP)
5761
currentCPInfo := blkfileMgrWrapper.blockfileMgr.cpInfo
5862
cpInfo1 := &checkpointInfo{
@@ -61,7 +65,14 @@ func testBlockfileMgrCrashDuringWriting(t *testing.T, numBlocksBeforeCheckpoint
6165
currentCPInfo.isChainEmpty,
6266
currentCPInfo.lastBlockNumber}
6367

64-
blocksAfterCP := bg.NextTestBlocks(numBlocksAfterCheckpoint)
68+
blocksAfterCP := []*common.Block{}
69+
if numBlocksBeforeCheckpoint == 0 {
70+
blocksAfterCP = append(blocksAfterCP, gb)
71+
blocksAfterCP = append(blocksAfterCP, bg.NextTestBlocks(numBlocksAfterCheckpoint-1)...)
72+
} else {
73+
blocksAfterCP = bg.NextTestBlocks(numBlocksAfterCheckpoint)
74+
}
75+
6576
blkfileMgrWrapper.addBlocks(blocksAfterCP)
6677
cpInfo2 := blkfileMgrWrapper.blockfileMgr.cpInfo
6778

@@ -140,11 +151,11 @@ func TestBlockfileMgrGetTxById(t *testing.T) {
140151
defer env.Cleanup()
141152
blkfileMgrWrapper := newTestBlockfileWrapper(env, "testLedger")
142153
defer blkfileMgrWrapper.close()
143-
blocks := testutil.ConstructTestBlocks(t, 10)
154+
blocks := testutil.ConstructTestBlocks(t, 2)
144155
blkfileMgrWrapper.addBlocks(blocks)
145156
for _, blk := range blocks {
146157
for j, txEnvelopeBytes := range blk.Data.Data {
147-
// blockNum starts with 1
158+
// blockNum starts with 0
148159
txID, err := extractTxID(blk.Data.Data[j])
149160
testutil.AssertNoError(t, err, "")
150161
txEnvelopeFromFileMgr, err := blkfileMgrWrapper.blockfileMgr.retrieveTransactionByID(txID)

common/ledger/blkstorage/fsblkstorage/blockfile_scan_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func TestBlockFileScanSmallTxOnly(t *testing.T) {
3131
defer env.Cleanup()
3232
ledgerid := "testLedger"
3333
blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
34-
bg := testutil.NewBlockGenerator(t)
35-
blocks := []*common.Block{}
34+
bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
35+
blocks := []*common.Block{gb}
3636
blocks = append(blocks, bg.NextTestBlock(0, 0))
3737
blocks = append(blocks, bg.NextTestBlock(0, 0))
3838
blocks = append(blocks, bg.NextTestBlock(0, 0))
@@ -54,8 +54,8 @@ func TestBlockFileScanSmallTxLastTxIncomplete(t *testing.T) {
5454
defer env.Cleanup()
5555
ledgerid := "testLedger"
5656
blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
57-
bg := testutil.NewBlockGenerator(t)
58-
blocks := []*common.Block{}
57+
bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
58+
blocks := []*common.Block{gb}
5959
blocks = append(blocks, bg.NextTestBlock(0, 0))
6060
blocks = append(blocks, bg.NextTestBlock(0, 0))
6161
blocks = append(blocks, bg.NextTestBlock(0, 0))

common/ledger/blkstorage/fsblkstorage/pkg_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (w *testBlockfileMgrWrapper) testGetBlockByNumber(blocks []*common.Block, s
100100
for i := 0; i < len(blocks); i++ {
101101
b, err := w.blockfileMgr.retrieveBlockByNumber(startingNum + uint64(i))
102102
testutil.AssertNoError(w.t, err, fmt.Sprintf("Error while retrieving [%d]th block from blockfileMgr", i))
103-
testutil.AssertEquals(w.t, b, blocks[i])
103+
testutil.AssertEquals(w.t, b.Header, blocks[i].Header)
104104
}
105105
// test getting the last block
106106
b, err := w.blockfileMgr.retrieveBlockByNumber(math.MaxUint64)

common/ledger/testutil/test_helper.go

+52-31
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,36 @@ import (
2020
"testing"
2121

2222
"github.com/golang/protobuf/proto"
23+
"github.com/hyperledger/fabric/common/configtx/test"
24+
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
2325
"github.com/hyperledger/fabric/common/util"
2426
lutils "github.com/hyperledger/fabric/core/ledger/util"
2527
"github.com/hyperledger/fabric/protos/common"
2628
ptestutils "github.com/hyperledger/fabric/protos/testutils"
2729
"github.com/hyperledger/fabric/protos/utils"
30+
31+
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
2832
)
2933

3034
//BlockGenerator generates a series of blocks for testing
3135
type BlockGenerator struct {
3236
blockNum uint64
3337
previousHash []byte
38+
signTxs bool
3439
t *testing.T
3540
}
3641

3742
// NewBlockGenerator instantiates new BlockGenerator for testing
38-
func NewBlockGenerator(t *testing.T) *BlockGenerator {
39-
return &BlockGenerator{0, []byte{}, t}
43+
func NewBlockGenerator(t *testing.T, ledgerID string, signTxs bool) (*BlockGenerator, *common.Block) {
44+
gb, err := test.MakeGenesisBlock(ledgerID)
45+
AssertNoError(t, err, "")
46+
gb.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = lutils.NewTxValidationFlags(len(gb.Data.Data))
47+
return &BlockGenerator{1, gb.GetHeader().Hash(), signTxs, t}, gb
4048
}
4149

4250
// NextBlock constructs next block in sequence that includes a number of transactions - one per simulationResults
43-
func (bg *BlockGenerator) NextBlock(simulationResults [][]byte, sign bool) *common.Block {
44-
envs := []*common.Envelope{}
45-
for i := 0; i < len(simulationResults); i++ {
46-
env, _, err := ConstructTransaction(bg.t, simulationResults[i], sign)
47-
if err != nil {
48-
bg.t.Fatalf("ConstructTestTransaction failed, err %s", err)
49-
}
50-
envs = append(envs, env)
51-
}
52-
block := newBlock(envs, bg.blockNum, bg.previousHash)
51+
func (bg *BlockGenerator) NextBlock(simulationResults [][]byte) *common.Block {
52+
block := ConstructBlock(bg.t, bg.blockNum, bg.previousHash, simulationResults, bg.signTxs)
5353
bg.blockNum++
5454
bg.previousHash = block.Header.Hash()
5555
return block
@@ -61,7 +61,7 @@ func (bg *BlockGenerator) NextTestBlock(numTx int, txSize int) *common.Block {
6161
for i := 0; i < numTx; i++ {
6262
simulationResults = append(simulationResults, ConstructRandomBytes(bg.t, txSize))
6363
}
64-
return bg.NextBlock(simulationResults, false)
64+
return bg.NextBlock(simulationResults)
6565
}
6666

6767
// NextTestBlocks constructs 'numBlocks' number of blocks for testing
@@ -73,24 +73,6 @@ func (bg *BlockGenerator) NextTestBlocks(numBlocks int) []*common.Block {
7373
return blocks
7474
}
7575

76-
// ConstructBlock constructs a single block with blockNum=1
77-
func ConstructBlock(t *testing.T, simulationResults [][]byte, sign bool) *common.Block {
78-
bg := NewBlockGenerator(t)
79-
return bg.NextBlock(simulationResults, sign)
80-
}
81-
82-
// ConstructTestBlock constructs a single block with blocknum=1
83-
func ConstructTestBlock(t *testing.T, numTx int, txSize int) *common.Block {
84-
bg := NewBlockGenerator(t)
85-
return bg.NextTestBlock(numTx, txSize)
86-
}
87-
88-
// ConstructTestBlocks returns a series of blocks starting with blockNum=1
89-
func ConstructTestBlocks(t *testing.T, numBlocks int) []*common.Block {
90-
bg := NewBlockGenerator(t)
91-
return bg.NextTestBlocks(numBlocks)
92-
}
93-
9476
// ConstructTransaction constructs a transaction for testing
9577
func ConstructTransaction(_ *testing.T, simulationResults []byte, sign bool) (*common.Envelope, string, error) {
9678
ccName := "foo"
@@ -106,6 +88,39 @@ func ConstructTransaction(_ *testing.T, simulationResults []byte, sign bool) (*c
10688
return txEnv, txID, err
10789
}
10890

91+
// ConstructBlock constructs a single block
92+
func ConstructBlock(t *testing.T, blockNum uint64, previousHash []byte, simulationResults [][]byte, sign bool) *common.Block {
93+
envs := []*common.Envelope{}
94+
for i := 0; i < len(simulationResults); i++ {
95+
env, _, err := ConstructTransaction(t, simulationResults[i], sign)
96+
if err != nil {
97+
t.Fatalf("ConstructTestTransaction failed, err %s", err)
98+
}
99+
envs = append(envs, env)
100+
}
101+
return newBlock(envs, blockNum, previousHash)
102+
}
103+
104+
//ConstructTestBlock constructs a single block with random contents
105+
func ConstructTestBlock(t *testing.T, blockNum uint64, numTx int, txSize int) *common.Block {
106+
simulationResults := [][]byte{}
107+
for i := 0; i < numTx; i++ {
108+
simulationResults = append(simulationResults, ConstructRandomBytes(t, txSize))
109+
}
110+
return ConstructBlock(t, blockNum, ConstructRandomBytes(t, 32), simulationResults, false)
111+
}
112+
113+
// ConstructTestBlocks returns a series of blocks starting with blockNum=0.
114+
// The first block in the returned array is a config tx block that represents a genesis block
115+
func ConstructTestBlocks(t *testing.T, numBlocks int) []*common.Block {
116+
bg, gb := NewBlockGenerator(t, util.GetTestChainID(), false)
117+
blocks := []*common.Block{}
118+
if numBlocks != 0 {
119+
blocks = append(blocks, gb)
120+
}
121+
return append(blocks, bg.NextTestBlocks(numBlocks-1)...)
122+
}
123+
109124
func newBlock(env []*common.Envelope, blockNum uint64, previousHash []byte) *common.Block {
110125
block := common.NewBlock(blockNum, previousHash)
111126
for i := 0; i < len(env); i++ {
@@ -119,3 +134,9 @@ func newBlock(env []*common.Envelope, blockNum uint64, previousHash []byte) *com
119134

120135
return block
121136
}
137+
138+
func MakeGenesisBlock() *common.Block {
139+
genConf := genesisconfig.Load(genesisconfig.SampleInsecureProfile)
140+
gb := provisional.New(genConf).GenesisBlock()
141+
return gb
142+
}

core/chaincode/exectransaction_test.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,8 @@ func checkFinalState(cccid *ccprovider.CCContext) error {
571571

572572
// Invoke chaincode_example02
573573
func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContext, cID *pb.ChaincodeID, chaincodeType pb.ChaincodeSpec_Type, args []string, destroyImage bool) error {
574-
575-
var nextBlockNumber uint64
576-
574+
// the ledger is created with genesis block. Start block number 1 onwards
575+
var nextBlockNumber uint64 = 1
577576
f := "init"
578577
argsDeploy := util.ToChaincodeArgs(f, "a", "100", "b", "200")
579578
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,
645644
sProp, prop := putils.MockSignedEndorserProposalOrPanic(chainID1, spec1, []byte([]byte("Alice")), nil)
646645
cccid1 := ccprovider.NewCCContext(chainID1, "example02", "0", "", false, sProp, prop)
647646

648-
var nextBlockNumber uint64
647+
var nextBlockNumber uint64 = 1
649648

650649
_, err = deploy(ctxt, cccid1, spec1, nextBlockNumber)
651650
nextBlockNumber++
@@ -741,7 +740,7 @@ func runChaincodeInvokeChaincode(t *testing.T, chainID1 string, chainID2 string,
741740
sProp, prop = putils.MockSignedEndorserProposalOrPanic(chainID2, spec3, []byte([]byte("Alice")), nil)
742741
cccid3 := ccprovider.NewCCContext(chainID2, "example04", "0", "", false, sProp, prop)
743742

744-
_, err = deploy(ctxt, cccid3, spec3, 0)
743+
_, err = deploy(ctxt, cccid3, spec3, 1)
745744
chaincodeID2 := spec2.ChaincodeId.Name
746745
if err != nil {
747746
t.Fail()
@@ -764,7 +763,7 @@ func runChaincodeInvokeChaincode(t *testing.T, chainID1 string, chainID2 string,
764763
//var uuid string
765764

766765
// Bob should not be able to call
767-
_, _, _, err = invoke(ctxt, chainID2, spec2, 1, []byte("Bob"))
766+
_, _, _, err = invoke(ctxt, chainID2, spec2, 2, []byte("Bob"))
768767
if err == nil {
769768
t.Fail()
770769
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,
775774
}
776775

777776
// Alice should be able to call
778-
_, _, _, err = invoke(ctxt, chainID2, spec2, 1, []byte("Alice"))
777+
_, _, _, err = invoke(ctxt, chainID2, spec2, 2, []byte("Alice"))
779778
if err != nil {
780779
t.Fail()
781780
t.Logf("Alice invoking <%s> should not fail. It did happen instead: %s", chaincodeID2, err)
@@ -953,7 +952,7 @@ func TestChaincodeInvokeChaincodeErrorCase(t *testing.T) {
953952
sProp, prop := putils.MockSignedEndorserProposalOrPanic(util.GetTestChainID(), spec1, []byte([]byte("Alice")), nil)
954953
cccid1 := ccprovider.NewCCContext(chainID, "example02", "0", "", false, sProp, prop)
955954

956-
var nextBlockNumber uint64
955+
var nextBlockNumber uint64 = 1
957956

958957
_, err = deploy(ctxt, cccid1, spec1, nextBlockNumber)
959958
nextBlockNumber++
@@ -1044,7 +1043,7 @@ func TestQueries(t *testing.T) {
10441043

10451044
cccid := ccprovider.NewCCContext(chainID, "tmap", "0", "", false, nil, nil)
10461045

1047-
var nextBlockNumber uint64
1046+
var nextBlockNumber uint64 = 1
10481047
_, err = deploy(ctxt, cccid, spec, nextBlockNumber)
10491048
nextBlockNumber++
10501049
ccID := spec.ChaincodeId.Name
@@ -1398,7 +1397,7 @@ func TestGetEvent(t *testing.T) {
13981397
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: cID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(f)}}
13991398

14001399
cccid := ccprovider.NewCCContext(chainID, "esender", "0", "", false, nil, nil)
1401-
var nextBlockNumber uint64
1400+
var nextBlockNumber uint64 = 1
14021401
_, err = deploy(ctxt, cccid, spec, nextBlockNumber)
14031402
nextBlockNumber++
14041403
ccID := spec.ChaincodeId.Name
@@ -1580,7 +1579,7 @@ func TestChaincodeInvokesForbiddenSystemChaincode(t *testing.T) {
15801579

15811580
var ctxt = context.Background()
15821581

1583-
var nextBlockNumber uint64
1582+
var nextBlockNumber uint64 = 1
15841583

15851584
// Deploy second chaincode
15861585
url := "github.com/hyperledger/fabric/examples/chaincode/go/passthru"
@@ -1635,7 +1634,7 @@ func TestChaincodeInvokesSystemChaincode(t *testing.T) {
16351634

16361635
var ctxt = context.Background()
16371636

1638-
var nextBlockNumber uint64
1637+
var nextBlockNumber uint64 = 1
16391638

16401639
// Deploy second chaincode
16411640
url := "github.com/hyperledger/fabric/examples/chaincode/go/passthru"

core/chaincode/systemchaincode_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ func deploySampleSysCC(t *testing.T, ctxt context.Context, chainID string) error
104104
args := util.ToChaincodeArgs(f, "greeting", "hey there")
105105

106106
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "sample_syscc", Path: url, Version: sysCCVers}, Input: &pb.ChaincodeInput{Args: args}}
107-
var nextBlockNumber uint64
107+
// the ledger is created with genesis block. Start block number 1 onwards
108+
var nextBlockNumber uint64 = 1
108109
_, _, _, err := invokeWithVersion(ctxt, chainID, sysCCVers, spec, nextBlockNumber, nil)
109110
nextBlockNumber++
110111

core/chaincode/upgrade_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func TestUpgradeCC(t *testing.T) {
146146
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: args}}
147147

148148
cccid := ccprovider.NewCCContext(chainID, ccName, "0", "", false, nil, nil)
149-
var nextBlockNumber uint64
149+
var nextBlockNumber uint64 = 1
150150
_, err = deploy(ctxt, cccid, spec, nextBlockNumber)
151151

152152
if err != nil {

0 commit comments

Comments
 (0)