github.com/yimialmonte/fabric@v2.1.1+incompatible/core/ledger/pvtdatastorage/v11_V12_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package pvtdatastorage 8 9 import ( 10 "io/ioutil" 11 "path/filepath" 12 13 "os" 14 "testing" 15 16 "github.com/davecgh/go-spew/spew" 17 "github.com/hyperledger/fabric/common/ledger/testutil" 18 "github.com/hyperledger/fabric/core/ledger" 19 btltestutil "github.com/hyperledger/fabric/core/ledger/pvtdatapolicy/testutil" 20 "github.com/stretchr/testify/assert" 21 ) 22 23 // TestV11v12 test that we are able to read the mixed format data (for v11 and v12) 24 // from pvtdata store. This test used a pvt data store that is produced in one of the 25 // upgrade tests. The store contains total 15 blocks. Block number one to nine has not 26 // pvt data because, that time peer code was v1.0 and hence no pvt data. Block 10 contains 27 // a pvtdata from peer v1.1. Block 11 - 13 has not pvt data. Block 14 has pvt data from peer v1.2 28 func TestV11v12(t *testing.T) { 29 testWorkingDir, err := ioutil.TempDir("", "pdstore") 30 if err != nil { 31 t.Fatalf("Failed to create private data storage directory: %s", err) 32 } 33 defer os.RemoveAll(testWorkingDir) 34 testutil.CopyDir("testdata/v11_v12/ledgersData/pvtdataStore", testWorkingDir, false) 35 36 ledgerid := "ch1" 37 btlPolicy := btltestutil.SampleBTLPolicy( 38 map[[2]string]uint64{ 39 {"marbles_private", "collectionMarbles"}: 0, 40 {"marbles_private", "collectionMarblePrivateDetails"}: 0, 41 }, 42 ) 43 conf := &PrivateDataConfig{ 44 PrivateDataConfig: &ledger.PrivateDataConfig{ 45 BatchesInterval: 1000, 46 MaxBatchSize: 5000, 47 PurgeInterval: 100, 48 }, 49 StorePath: filepath.Join(testWorkingDir, "pvtdataStore"), 50 } 51 p, err := NewProvider(conf) 52 assert.NoError(t, err) 53 defer p.Close() 54 s, err := p.OpenStore(ledgerid) 55 assert.NoError(t, err) 56 s.Init(btlPolicy) 57 58 for blk := 0; blk < 10; blk++ { 59 checkDataNotExists(t, s, blk) 60 } 61 checkDataExists(t, s, 10) 62 for blk := 11; blk < 14; blk++ { 63 checkDataNotExists(t, s, blk) 64 } 65 checkDataExists(t, s, 14) 66 67 _, err = s.GetPvtDataByBlockNum(uint64(15), nil) 68 _, ok := err.(*ErrOutOfRange) 69 assert.True(t, ok) 70 } 71 72 func checkDataNotExists(t *testing.T, s Store, blkNum int) { 73 data, err := s.GetPvtDataByBlockNum(uint64(blkNum), nil) 74 assert.NoError(t, err) 75 assert.Nil(t, data) 76 } 77 78 func checkDataExists(t *testing.T, s Store, blkNum int) { 79 data, err := s.GetPvtDataByBlockNum(uint64(blkNum), nil) 80 assert.NoError(t, err) 81 assert.NotNil(t, data) 82 t.Logf("pvtdata = %s\n", spew.Sdump(data)) 83 }