github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/core/ledger/kvledger/rwset_backward_compatibility_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package kvledger 8 9 import ( 10 "fmt" 11 "io/ioutil" 12 "testing" 13 14 "github.com/hyperledger/fabric-protos-go/common" 15 "github.com/osdi23p228/fabric/common/ledger/testutil" 16 "github.com/osdi23p228/fabric/common/util" 17 lgr "github.com/osdi23p228/fabric/core/ledger" 18 "github.com/osdi23p228/fabric/core/ledger/mock" 19 "github.com/osdi23p228/fabric/protoutil" 20 "github.com/stretchr/testify/require" 21 ) 22 23 // TestBackwardCompatibilityRWSetV21 is added to protect against any changes in the hash function 24 // that is used in preparing the rwset that includes a merkle tree for the range query 25 func TestBackwardCompatibilityRWSetV21(t *testing.T) { 26 rwsetBytes, err := ioutil.ReadFile("testdata/rwsetbytes_v21") 27 require.NoError(t, err) 28 b := testGenerateSampleRWSet(t) 29 require.Equal(t, rwsetBytes, b) 30 } 31 32 // TestGenerateSampleRWSet generates the rwset that includes a merkle tree for a range-query read-set as well 33 // The data present in the file testdata/rwsetbytes_v21 is generated by running the below test code on version 2.1 34 // To regenrate the data, (if needed in order to add some more data to the generated rwset), checkout release-2.1, 35 // and uncomment and run the following test 36 // func TestGenerateSampleRWSet(t *testing.T) { 37 // b := testGenerateSampleRWSet(t) 38 // require.NoError(t, ioutil.WriteFile("testdata/rwsetbytes_v21", b, 0644)) 39 // } 40 41 func testGenerateSampleRWSet(t *testing.T) []byte { 42 conf, cleanup := testConfig(t) 43 defer cleanup() 44 provider := testutilNewProvider(conf, t, &mock.DeployedChaincodeInfoProvider{}) 45 defer provider.Close() 46 47 bg, gb := testutil.NewBlockGenerator(t, "testLedger", false) 48 gbHash := protoutil.BlockHeaderHash(gb.Header) 49 ledger, err := provider.Create(gb) 50 require.NoError(t, err) 51 defer ledger.Close() 52 53 bcInfo, err := ledger.GetBlockchainInfo() 54 require.NoError(t, err) 55 require.Equal(t, &common.BlockchainInfo{ 56 Height: 1, CurrentBlockHash: gbHash, PreviousBlockHash: nil, 57 }, bcInfo) 58 59 txid := util.GenerateUUID() 60 61 // perform a range query for significant larger scan so that the merkle tree building kicks in 62 // each level contains max 50 nodes per the current configuration 63 simulator, err := ledger.NewTxSimulator(txid) 64 require.NoError(t, err) 65 for i := 0; i < 10011; i++ { 66 simulator.SetState("ns1", fmt.Sprintf("key-%000d", i), []byte(fmt.Sprintf("value-%000d", i))) 67 } 68 simulator.Done() 69 simRes, err := simulator.GetTxSimulationResults() 70 require.NoError(t, err) 71 pubSimBytes, err := simRes.GetPubSimulationBytes() 72 require.NoError(t, err) 73 block1 := bg.NextBlock([][]byte{pubSimBytes}) 74 ledger.CommitLegacy(&lgr.BlockAndPvtData{Block: block1}, &lgr.CommitOptions{}) 75 76 simulator, err = ledger.NewTxSimulator(txid) 77 require.NoError(t, err) 78 simulator.GetState("ns1", fmt.Sprintf("key-%000d", 5)) 79 simulator.SetState("ns1", fmt.Sprintf("key-%000d", 6), []byte(fmt.Sprintf("value-%000d-new", 6))) 80 itr, err := simulator.GetStateRangeScanIterator("ns1", "", "") 81 require.NoError(t, err) 82 numKVs := 0 83 for { 84 kv, err := itr.Next() 85 require.NoError(t, err) 86 if kv == nil { 87 break 88 } 89 numKVs++ 90 } 91 require.Equal(t, 10011, numKVs) 92 simulator.Done() 93 simRes, err = simulator.GetTxSimulationResults() 94 require.NoError(t, err) 95 pubSimBytes, err = simRes.GetPubSimulationBytes() 96 require.NoError(t, err) 97 return pubSimBytes 98 }