github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/pvtdatastorage/v11_V12_test.go (about)

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