github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/tests/v20_test.go (about) 1 /* 2 Copyright hechain. 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/hechain20/hechain/common/ledger/testutil" 14 "github.com/hechain20/hechain/core/chaincode/implicitcollection" 15 "github.com/hechain20/hechain/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 l1 := env.openTestLedger("testchannel") 31 dataHelper.verify(l1) 32 33 // rebuild and verify again 34 env.closeLedgerMgmt() 35 require.NoError(t, kvledger.RebuildDBs(env.initializer.Config)) 36 env.initLedgerMgmt() 37 l1 = env.openTestLedger("testchannel") 38 dataHelper.verify(l1) 39 } 40 41 // The generated ledger has the following blocks: 42 // block 0: genesis 43 // block 1 to 4: network setup 44 // block 5 to 8: marblesp chaincode instantiation 45 // block 9 to 12: marbles chancode instantiation 46 // block 13: marblesp chaincode invocation (new marble1) 47 // block 14 to 17: upgrade marblesp chaincode with a new collection config 48 // block 18 to 19: marbles chaincode invocation (new marble100 and transfer) 49 type v20SampleDataHelper struct { 50 sampleDataVersion string 51 t *testing.T 52 } 53 54 func (d *v20SampleDataHelper) verify(l *testLedger) { 55 d.verifyState(l) 56 d.verifyBlockAndPvtdata(l) 57 d.verifyConfigHistory(l) 58 d.verifyHistory(l) 59 } 60 61 func (d *v20SampleDataHelper) verifyState(l *testLedger) { 62 l.verifyPubState("marbles", "marble100", d.marbleValue("marble100", "blue", "jerry", 35)) 63 l.verifyPvtState("marblesp", "collectionMarbles", "marble1", d.marbleValue("marble1", "blue", "tom", 35)) 64 l.verifyPvtState("marblesp", "collectionMarblePrivateDetails", "marble1", d.marbleDetail("marble1", 99)) 65 } 66 67 func (d *v20SampleDataHelper) verifyHistory(l *testLedger) { 68 expectedHistoryValue1 := []string{ 69 d.marbleValue("marble100", "blue", "jerry", 35), 70 d.marbleValue("marble100", "blue", "tom", 35), 71 } 72 l.verifyHistory("marbles", "marble100", expectedHistoryValue1) 73 } 74 75 func (d *v20SampleDataHelper) verifyConfigHistory(l *testLedger) { 76 // below block 10 should match integration/ledger/testdata/collection_configs/collections_config1.json 77 l.verifyMostRecentCollectionConfigBelow(10, "marblesp", 78 &expectedCollConfInfo{8, d.marbleCollConf1("marbelsp")}) 79 80 // below block 18 should match integration/ledger/testdata/collection_configs/collections_config2.json 81 l.verifyMostRecentCollectionConfigBelow(18, "marblesp", 82 &expectedCollConfInfo{17, d.marbleCollConf2("marbelsp")}) 83 } 84 85 func (d *v20SampleDataHelper) verifyBlockAndPvtdata(l *testLedger) { 86 l.verifyBlockAndPvtData(8, nil, func(r *retrievedBlockAndPvtdata) { 87 r.hasNumTx(1) 88 r.hasNoPvtdata() 89 }) 90 91 l.verifyBlockAndPvtData(13, nil, func(r *retrievedBlockAndPvtdata) { 92 r.hasNumTx(1) 93 r.pvtdataShouldContain(0, "marblesp", "collectionMarbles", "marble1", d.marbleValue("marble1", "blue", "tom", 35)) 94 r.pvtdataShouldContain(0, "marblesp", "collectionMarblePrivateDetails", "marble1", d.marbleDetail("marble1", 99)) 95 }) 96 } 97 98 func (d *v20SampleDataHelper) marbleValue(name, color, owner string, size int) string { 99 return fmt.Sprintf(`{"docType":"marble","name":"%s","color":"%s","size":%d,"owner":"%s"}`, name, color, size, owner) 100 } 101 102 func (d *v20SampleDataHelper) marbleDetail(name string, price int) string { 103 return fmt.Sprintf(`{"docType":"marblePrivateDetails","name":"%s","price":%d}`, name, price) 104 } 105 106 func (d *v20SampleDataHelper) mspIDsInChannelConfig() []string { 107 return []string{"Org1MSP", "Org2MSP", "Org2MSP"} 108 } 109 110 // match integration/ledger/testdata/collection_configs/collections_config1.json 111 func (d *v20SampleDataHelper) marbleCollConf1(ccName string) []*collConf { 112 collConfigs := make([]*collConf, 0) 113 collConfigs = append(collConfigs, &collConf{name: "collectionMarbles", btl: 1000000, members: []string{"Org1MSP", "Org2MSP"}}) 114 collConfigs = append(collConfigs, &collConf{name: "collectionMarblePrivateDetails", btl: 1000000, members: []string{"Org2MSP", "Org3MSP"}}) 115 for _, mspID := range d.mspIDsInChannelConfig() { 116 collConfigs = append(collConfigs, &collConf{name: implicitcollection.NameForOrg(mspID), btl: 0, members: []string{mspID}}) 117 } 118 return collConfigs 119 } 120 121 // match integration/ledger/testdata/collection_configs/collections_config2.json 122 func (d *v20SampleDataHelper) marbleCollConf2(ccName string) []*collConf { 123 collConfigs := make([]*collConf, 0) 124 collConfigs = append(collConfigs, &collConf{name: "collectionMarbles", btl: 1000000, members: []string{"Org2MSP", "Org3MSP"}}) 125 collConfigs = append(collConfigs, &collConf{name: "collectionMarblePrivateDetails", btl: 1000000, members: []string{"Org1MSP", "Org2MSP", "Org3MSP"}}) 126 for _, mspID := range d.mspIDsInChannelConfig() { 127 collConfigs = append(collConfigs, &collConf{name: implicitcollection.NameForOrg(mspID), btl: 0, members: []string{mspID}}) 128 } 129 return collConfigs 130 }