github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/tests/rebuild_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  
    13  func TestRebuildComponents(t *testing.T) {
    14  	env := newEnv(t)
    15  	defer env.cleanup()
    16  	env.initLedgerMgmt()
    17  
    18  	h1, h2 := env.newTestHelperCreateLgr("ledger1", t), env.newTestHelperCreateLgr("ledger2", t)
    19  	dataHelper := newSampleDataHelper(t)
    20  
    21  	dataHelper.populateLedger(h1)
    22  	dataHelper.populateLedger(h2)
    23  
    24  	dataHelper.verifyLedgerContent(h1)
    25  	dataHelper.verifyLedgerContent(h2)
    26  
    27  	t.Run("rebuild only statedb",
    28  		func(t *testing.T) {
    29  			env.closeAllLedgersAndDrop(rebuildableStatedb)
    30  			h1, h2 := env.newTestHelperOpenLgr("ledger1", t), env.newTestHelperOpenLgr("ledger2", t)
    31  			dataHelper.verifyLedgerContent(h1)
    32  			dataHelper.verifyLedgerContent(h2)
    33  		},
    34  	)
    35  
    36  	t.Run("rebuild statedb and config history",
    37  		func(t *testing.T) {
    38  			env.closeAllLedgersAndDrop(rebuildableStatedb | rebuildableConfigHistory)
    39  			h1, h2 := env.newTestHelperOpenLgr("ledger1", t), env.newTestHelperOpenLgr("ledger2", t)
    40  			dataHelper.verifyLedgerContent(h1)
    41  			dataHelper.verifyLedgerContent(h2)
    42  		},
    43  	)
    44  
    45  	t.Run("rebuild statedb and block index",
    46  		func(t *testing.T) {
    47  			env.closeAllLedgersAndDrop(rebuildableStatedb | rebuildableBlockIndex)
    48  			h1, h2 := env.newTestHelperOpenLgr("ledger1", t), env.newTestHelperOpenLgr("ledger2", t)
    49  			dataHelper.verifyLedgerContent(h1)
    50  			dataHelper.verifyLedgerContent(h2)
    51  		},
    52  	)
    53  
    54  	t.Run("rebuild statedb and historydb",
    55  		func(t *testing.T) {
    56  			env.closeAllLedgersAndDrop(rebuildableStatedb | rebuildableHistoryDB)
    57  			h1, h2 := env.newTestHelperOpenLgr("ledger1", t), env.newTestHelperOpenLgr("ledger2", t)
    58  			dataHelper.verifyLedgerContent(h1)
    59  			dataHelper.verifyLedgerContent(h2)
    60  		},
    61  	)
    62  }
    63  
    64  func TestRebuildComponentsWithBTL(t *testing.T) {
    65  	env := newEnv(t)
    66  	defer env.cleanup()
    67  	env.initLedgerMgmt()
    68  	h := env.newTestHelperCreateLgr("ledger1", t)
    69  	collConf := []*collConf{{name: "coll1", btl: 0}, {name: "coll2", btl: 1}}
    70  
    71  	// deploy cc1 with 'collConf'
    72  	h.simulateDeployTx("cc1", collConf)
    73  	h.cutBlockAndCommitLegacy()
    74  
    75  	// commit pvtdata writes in block 2.
    76  	h.simulateDataTx("", func(s *simulator) {
    77  		s.setPvtdata("cc1", "coll1", "key1", "value1") // (key1 would never expire)
    78  		s.setPvtdata("cc1", "coll2", "key2", "value2") // (key2 would expire at block 4)
    79  	})
    80  	blk2 := h.cutBlockAndCommitLegacy()
    81  
    82  	// After commit of block 2
    83  	h.verifyPvtState("cc1", "coll1", "key1", "value1") // key1 should still exist in the state
    84  	h.verifyPvtState("cc1", "coll2", "key2", "value2") // key2 should still exist in the state
    85  	h.verifyBlockAndPvtDataSameAs(2, blk2)             // key1 and key2 should still exist in the pvtdata storage
    86  
    87  	// commit 2 more blocks with some random key/vals
    88  
    89  	for i := 0; i < 2; i++ {
    90  		h.simulateDataTx("", func(s *simulator) {
    91  			s.setPvtdata("cc1", "coll1", "someOtherKey", "someOtherVal")
    92  			s.setPvtdata("cc1", "coll2", "someOtherKey", "someOtherVal")
    93  		})
    94  		h.cutBlockAndCommitLegacy()
    95  	}
    96  
    97  	// After commit of block 4
    98  	h.verifyPvtState("cc1", "coll1", "key1", "value1")                  // key1 should still exist in the state
    99  	h.verifyPvtState("cc1", "coll2", "key2", "")                        // key2 should have been purged from the state
   100  	h.verifyBlockAndPvtData(2, nil, func(r *retrievedBlockAndPvtdata) { // retrieve the pvtdata for block 2 from pvtdata storage
   101  		r.pvtdataShouldContain(0, "cc1", "coll1", "key1", "value1") // key1 should still exist in the pvtdata storage
   102  		r.pvtdataShouldNotContain("cc1", "coll2")                   // <cc1, coll2> shold have been purged from the pvtdata storage
   103  	})
   104  
   105  	// rebuild statedb and bookkeeper
   106  	env.closeAllLedgersAndDrop(rebuildableStatedb | rebuildableBookkeeper)
   107  
   108  	h = env.newTestHelperOpenLgr("ledger1", t)
   109  	h.verifyPvtState("cc1", "coll1", "key1", "value1")                  // key1 should still exist in the state
   110  	h.verifyPvtState("cc1", "coll2", "key2", "")                        // key2 should have been purged from the state
   111  	h.verifyBlockAndPvtData(2, nil, func(r *retrievedBlockAndPvtdata) { // retrieve the pvtdata for block 2 from pvtdata storage
   112  		r.pvtdataShouldContain(0, "cc1", "coll1", "key1", "value1") // key1 should still exist in the pvtdata storage
   113  		r.pvtdataShouldNotContain("cc1", "coll2")                   // <cc1, coll2> shold have been purged from the pvtdata storage
   114  	})
   115  
   116  	// commit pvtdata writes in block 5.
   117  	h.simulateDataTx("", func(s *simulator) {
   118  		s.setPvtdata("cc1", "coll1", "key3", "value1") // (key3 would never expire)
   119  		s.setPvtdata("cc1", "coll2", "key4", "value2") // (key4 would expire at block 7)
   120  	})
   121  	h.cutBlockAndCommitLegacy()
   122  
   123  	// commit 2 more blocks with some random key/vals
   124  	for i := 0; i < 2; i++ {
   125  		h.simulateDataTx("", func(s *simulator) {
   126  			s.setPvtdata("cc1", "coll1", "someOtherKey", "someOtherVal")
   127  			s.setPvtdata("cc1", "coll2", "someOtherKey", "someOtherVal")
   128  		})
   129  		h.cutBlockAndCommitLegacy()
   130  	}
   131  
   132  	// After commit of block 7
   133  	h.verifyPvtState("cc1", "coll1", "key3", "value1")                  // key3 should still exist in the state
   134  	h.verifyPvtState("cc1", "coll2", "key4", "")                        // key4 should have been purged from the state
   135  	h.verifyBlockAndPvtData(5, nil, func(r *retrievedBlockAndPvtdata) { // retrieve the pvtdata for block 2 from pvtdata storage
   136  		r.pvtdataShouldContain(0, "cc1", "coll1", "key3", "value1") // key3 should still exist in the pvtdata storage
   137  		r.pvtdataShouldNotContain("cc1", "coll2")                   // <cc1, coll2> shold have been purged from the pvtdata storage
   138  	})
   139  }