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