github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/tests/v20_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package tests 8 9 import ( 10 "fmt" 11 "testing" 12 13 "github.com/hyperledger/fabric/common/ledger/testutil" 14 "github.com/hyperledger/fabric/core/chaincode/lifecycle" 15 "github.com/hyperledger/fabric/core/ledger/kvledger" 16 "github.com/stretchr/testify/require" 17 ) 18 19 // TestV20SampleLedger tests rebuild function with sample v2.0 ledger data generated by integration/ledger/ledger_generate_test.go 20 func TestV20SampleLedger(t *testing.T) { 21 env := newEnv(t) 22 defer env.cleanup() 23 24 dataHelper := &v20SampleDataHelper{sampleDataVersion: "v2.0", t: t} 25 env.initializer.DeployedChaincodeInfoProvider = createDeployedCCInfoProvider(dataHelper.mspIDsInChannelConfig()) 26 ledgerFSRoot := env.initializer.Config.RootFSPath 27 require.NoError(t, testutil.Unzip("testdata/v20/sample_ledgers/ledgersData.zip", ledgerFSRoot, false)) 28 29 env.initLedgerMgmt() 30 31 h1 := env.newTestHelperOpenLgr("testchannel", t) 32 dataHelper.verify(h1) 33 34 // rebuild and verify again 35 env.closeLedgerMgmt() 36 kvledger.RebuildDBs(env.getLedgerRootPath()) 37 env.initLedgerMgmt() 38 h1 = env.newTestHelperOpenLgr("testchannel", t) 39 dataHelper.verify(h1) 40 } 41 42 // The generated ledger has the following blocks: 43 // block 0: genesis 44 // block 1 to 4: network setup 45 // block 5 to 8: marblesp chaincode instantiation 46 // block 9 to 12: marbles chancode instantiation 47 // block 13: marblesp chaincode invocation (new marble1) 48 // block 14 to 17: upgrade marblesp chaincode with a new collection config 49 // block 18 to 19: marbles chaincode invocation (new marble100 and transfer) 50 type v20SampleDataHelper struct { 51 sampleDataVersion string 52 t *testing.T 53 } 54 55 func (d *v20SampleDataHelper) verify(h *testhelper) { 56 d.verifyState(h) 57 d.verifyBlockAndPvtdata(h) 58 d.verifyConfigHistory(h) 59 d.verifyHistory(h) 60 } 61 62 func (d *v20SampleDataHelper) verifyState(h *testhelper) { 63 h.verifyPubState("marbles", "marble100", d.marbleValue("marble100", "blue", "jerry", 35)) 64 h.verifyPvtState("marblesp", "collectionMarbles", "marble1", d.marbleValue("marble1", "blue", "tom", 35)) 65 h.verifyPvtState("marblesp", "collectionMarblePrivateDetails", "marble1", d.marbleDetail("marble1", 99)) 66 } 67 68 func (d *v20SampleDataHelper) verifyHistory(h *testhelper) { 69 expectedHistoryValue1 := []string{ 70 d.marbleValue("marble100", "blue", "jerry", 35), 71 d.marbleValue("marble100", "blue", "tom", 35), 72 } 73 h.verifyHistory("marbles", "marble100", expectedHistoryValue1) 74 } 75 76 func (d *v20SampleDataHelper) verifyConfigHistory(h *testhelper) { 77 // below block 10 should match integration/ledger/testdata/collection_configs/collections_config1.json 78 h.verifyMostRecentCollectionConfigBelow(10, "marblesp", 79 &expectedCollConfInfo{8, d.marbleCollConf1("marbelsp")}) 80 81 // below block 18 should match integration/ledger/testdata/collection_configs/collections_config2.json 82 h.verifyMostRecentCollectionConfigBelow(18, "marblesp", 83 &expectedCollConfInfo{17, d.marbleCollConf2("marbelsp")}) 84 } 85 86 func (d *v20SampleDataHelper) verifyBlockAndPvtdata(h *testhelper) { 87 h.verifyBlockAndPvtData(8, nil, func(r *retrievedBlockAndPvtdata) { 88 r.hasNumTx(1) 89 r.hasNoPvtdata() 90 }) 91 92 h.verifyBlockAndPvtData(13, nil, func(r *retrievedBlockAndPvtdata) { 93 r.hasNumTx(1) 94 r.pvtdataShouldContain(0, "marblesp", "collectionMarbles", "marble1", d.marbleValue("marble1", "blue", "tom", 35)) 95 r.pvtdataShouldContain(0, "marblesp", "collectionMarblePrivateDetails", "marble1", d.marbleDetail("marble1", 99)) 96 }) 97 } 98 99 func (d *v20SampleDataHelper) marbleValue(name, color, owner string, size int) string { 100 return fmt.Sprintf(`{"docType":"marble","name":"%s","color":"%s","size":%d,"owner":"%s"}`, name, color, size, owner) 101 } 102 103 func (d *v20SampleDataHelper) marbleDetail(name string, price int) string { 104 return fmt.Sprintf(`{"docType":"marblePrivateDetails","name":"%s","price":%d}`, name, price) 105 } 106 107 func (d *v20SampleDataHelper) mspIDsInChannelConfig() []string { 108 return []string{"Org1MSP", "Org2MSP", "Org2MSP"} 109 } 110 111 // match integration/ledger/testdata/collection_configs/collections_config1.json 112 func (d *v20SampleDataHelper) marbleCollConf1(ccName string) []*collConf { 113 collConfigs := make([]*collConf, 0) 114 collConfigs = append(collConfigs, &collConf{name: "collectionMarbles", btl: 1000000, members: []string{"Org1MSP", "Org2MSP"}}) 115 collConfigs = append(collConfigs, &collConf{name: "collectionMarblePrivateDetails", btl: 1000000, members: []string{"Org2MSP", "Org3MSP"}}) 116 for _, mspID := range d.mspIDsInChannelConfig() { 117 collConfigs = append(collConfigs, &collConf{name: lifecycle.ImplicitCollectionNameForOrg(mspID), btl: 0, members: []string{mspID}}) 118 } 119 return collConfigs 120 } 121 122 // match integration/ledger/testdata/collection_configs/collections_config2.json 123 func (d *v20SampleDataHelper) marbleCollConf2(ccName string) []*collConf { 124 collConfigs := make([]*collConf, 0) 125 collConfigs = append(collConfigs, &collConf{name: "collectionMarbles", btl: 1000000, members: []string{"Org2MSP", "Org3MSP"}}) 126 collConfigs = append(collConfigs, &collConf{name: "collectionMarblePrivateDetails", btl: 1000000, members: []string{"Org1MSP", "Org2MSP", "Org3MSP"}}) 127 for _, mspID := range d.mspIDsInChannelConfig() { 128 collConfigs = append(collConfigs, &collConf{name: lifecycle.ImplicitCollectionNameForOrg(mspID), btl: 0, members: []string{mspID}}) 129 } 130 return collConfigs 131 }