github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/tests/pvtdata_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  	"testing"
    11  
    12  	"github.com/hechain20/hechain/core/ledger"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestMissingCollConfig(t *testing.T) {
    17  	env := newEnv(t)
    18  	defer env.cleanup()
    19  	env.initLedgerMgmt()
    20  	l := env.createTestLedgerFromGenesisBlk("ledger1")
    21  
    22  	collConf := []*collConf{{name: "coll1", btl: 5}}
    23  
    24  	// deploy cc1 with no coll config
    25  	l.simulateDeployTx("cc1", nil)
    26  	l.cutBlockAndCommitLegacy()
    27  
    28  	// pvt data operations should give error as no collection config defined
    29  	l.simulateDataTx("", func(s *simulator) {
    30  		expectedErr := "collection config not defined for chaincode [cc1], pass the collection configuration upon chaincode definition/instantiation"
    31  		_, err := s.GetPrivateData("cc1", "coll1", "key")
    32  		require.EqualError(t, err, expectedErr)
    33  
    34  		err = s.SetPrivateData("cc1", "coll1", "key", []byte("value"))
    35  		require.EqualError(t, err, expectedErr)
    36  
    37  		err = s.DeletePrivateData("cc1", "coll1", "key")
    38  		require.EqualError(t, err, expectedErr)
    39  	})
    40  
    41  	// upgrade cc1 (add collConf)
    42  	l.simulateUpgradeTx("cc1", collConf)
    43  	l.cutBlockAndCommitLegacy()
    44  
    45  	// operations on coll1 should not give error
    46  	// operations on coll2 should give error (because, only coll1 is defined in collConf)
    47  	l.simulateDataTx("", func(s *simulator) {
    48  		_, err := s.GetPrivateData("cc1", "coll1", "key1")
    49  		require.NoError(t, err)
    50  
    51  		err = s.SetPrivateData("cc1", "coll1", "key2", []byte("value"))
    52  		require.NoError(t, err)
    53  
    54  		err = s.DeletePrivateData("cc1", "coll1", "key3")
    55  		require.NoError(t, err)
    56  
    57  		expectedErr := "collection [coll2] not defined in the collection config for chaincode [cc1]"
    58  		_, err = s.GetPrivateData("cc1", "coll2", "key")
    59  		require.EqualError(t, err, expectedErr)
    60  
    61  		err = s.SetPrivateData("cc1", "coll2", "key", []byte("value"))
    62  		require.EqualError(t, err, expectedErr)
    63  
    64  		err = s.DeletePrivateData("cc1", "coll2", "key")
    65  		require.EqualError(t, err, expectedErr)
    66  	})
    67  }
    68  
    69  func TestTxWithMissingPvtdata(t *testing.T) {
    70  	env := newEnv(t)
    71  	defer env.cleanup()
    72  	env.initLedgerMgmt()
    73  	l := env.createTestLedgerFromGenesisBlk("ledger1")
    74  
    75  	collConf := []*collConf{{name: "coll1", btl: 5}}
    76  
    77  	// deploy cc1 with 'collConf'
    78  	l.simulateDeployTx("cc1", collConf)
    79  	l.cutBlockAndCommitLegacy()
    80  
    81  	// pvtdata simulation
    82  	l.simulateDataTx("", func(s *simulator) {
    83  		s.setPvtdata("cc1", "coll1", "key1", "value1")
    84  	})
    85  	// another pvtdata simulation
    86  	l.simulateDataTx("", func(s *simulator) {
    87  		s.setPvtdata("cc1", "coll1", "key2", "value2")
    88  	})
    89  
    90  	l.causeMissingPvtData(0)
    91  	blk2 := l.cutBlockAndCommitLegacy()
    92  
    93  	l.verifyPvtState("cc1", "coll1", "key2", "value2") // key2 should have been committed
    94  	l.simulateDataTx("", func(s *simulator) {
    95  		_, err := s.GetPrivateData("cc1", "coll1", "key1") // key1 would be stale with respect to hashed version
    96  		require.EqualError(t, err, "private data matching public hash version is not available. Public hash version = {BlockNum: 2, TxNum: 0}, Private data version = <nil>")
    97  	})
    98  
    99  	// verify missing pvtdata info
   100  	l.verifyBlockAndPvtDataSameAs(2, blk2)
   101  	expectedMissingPvtDataInfo := make(ledger.MissingPvtDataInfo)
   102  	expectedMissingPvtDataInfo.Add(2, 0, "cc1", "coll1")
   103  	l.verifyMissingPvtDataSameAs(2, expectedMissingPvtDataInfo)
   104  
   105  	// another data tx overwritting key1
   106  	l.simulateDataTx("", func(s *simulator) {
   107  		s.setPvtdata("cc1", "coll1", "key1", "newvalue1")
   108  	})
   109  	blk3 := l.cutBlockAndCommitLegacy()
   110  	l.verifyPvtState("cc1", "coll1", "key1", "newvalue1") // key1 should have been committed with new value
   111  	l.verifyBlockAndPvtDataSameAs(2, blk2)
   112  	l.verifyBlockAndPvtDataSameAs(3, blk3)
   113  }
   114  
   115  func TestTxWithWrongPvtdata(t *testing.T) {
   116  	env := newEnv(t)
   117  	defer env.cleanup()
   118  	env.initLedgerMgmt()
   119  	l := env.createTestLedgerFromGenesisBlk("ledger1")
   120  
   121  	collConf := []*collConf{{name: "coll1", btl: 5}}
   122  
   123  	// deploy cc1 with 'collConf'
   124  	l.simulateDeployTx("cc1", collConf)
   125  	l.cutBlockAndCommitLegacy()
   126  
   127  	// pvtdata simulation
   128  	l.simulateDataTx("", func(s *simulator) {
   129  		s.setPvtdata("cc1", "coll1", "key1", "value1")
   130  	})
   131  	// another pvtdata simulation
   132  	l.simulateDataTx("", func(s *simulator) {
   133  		s.setPvtdata("cc1", "coll1", "key2", "value2")
   134  	})
   135  	l.simulatedTrans[0].Pvtws = l.simulatedTrans[1].Pvtws // put wrong pvt writeset in first simulation
   136  	// the commit of block is rejected if the hash of collection present in the block does not match with the pvtdata
   137  	l.cutBlockAndCommitExpectError()
   138  	l.verifyPvtState("cc1", "coll1", "key2", "")
   139  }
   140  
   141  func TestBTL(t *testing.T) {
   142  	env := newEnv(t)
   143  	defer env.cleanup()
   144  	env.initLedgerMgmt()
   145  	l := env.createTestLedgerFromGenesisBlk("ledger1")
   146  	collConf := []*collConf{{name: "coll1", btl: 0}, {name: "coll2", btl: 5}}
   147  
   148  	// deploy cc1 with 'collConf'
   149  	l.simulateDeployTx("cc1", collConf)
   150  	l.cutBlockAndCommitLegacy()
   151  
   152  	// commit pvtdata writes in block 2.
   153  	l.simulateDataTx("", func(s *simulator) {
   154  		s.setPvtdata("cc1", "coll1", "key1", "value1") // (key1 would never expire)
   155  		s.setPvtdata("cc1", "coll2", "key2", "value2") // (key2 would expire at block 8)
   156  	})
   157  	blk2 := l.cutBlockAndCommitLegacy()
   158  
   159  	// commit 5 more blocks with some random key/vals
   160  	for i := 0; i < 5; i++ {
   161  		l.simulateDataTx("", func(s *simulator) {
   162  			s.setPvtdata("cc1", "coll1", "someOtherKey", "someOtherVal")
   163  			s.setPvtdata("cc1", "coll2", "someOtherKey", "someOtherVal")
   164  		})
   165  		l.cutBlockAndCommitLegacy()
   166  	}
   167  
   168  	// After commit of block 7
   169  	l.verifyPvtState("cc1", "coll1", "key1", "value1") // key1 should still exist in the state
   170  	l.verifyPvtState("cc1", "coll2", "key2", "value2") // key2 should still exist in the state
   171  	l.verifyBlockAndPvtDataSameAs(2, blk2)             // key1 and key2 should still exist in the pvtdata storage
   172  
   173  	// commit block 8 with some random key/vals
   174  	l.simulateDataTx("", func(s *simulator) {
   175  		s.setPvtdata("cc1", "coll1", "someOtherKey", "someOtherVal")
   176  		s.setPvtdata("cc1", "coll2", "someOtherKey", "someOtherVal")
   177  	})
   178  	l.cutBlockAndCommitLegacy()
   179  
   180  	// After commit of block 8
   181  	l.verifyPvtState("cc1", "coll1", "key1", "value1")                  // key1 should still exist in the state
   182  	l.verifyPvtState("cc1", "coll2", "key2", "")                        // key2 should have been purged from the state
   183  	l.verifyBlockAndPvtData(2, nil, func(r *retrievedBlockAndPvtdata) { // retrieve the pvtdata for block 2 from pvtdata storage
   184  		r.pvtdataShouldContain(0, "cc1", "coll1", "key1", "value1") // key1 should still exist in the pvtdata storage
   185  		r.pvtdataShouldNotContain("cc1", "coll2")                   // <cc1, coll2> shold have been purged from the pvtdata storage
   186  	})
   187  }