github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/tests/pvtdata_test.go (about)

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