github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/tests/rebuild_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 13 func TestRebuildComponents(t *testing.T) { 14 env := newEnv(t) 15 defer env.cleanup() 16 env.initLedgerMgmt() 17 18 l1, l2 := env.createTestLedgerFromGenesisBlk("ledger1"), env.createTestLedgerFromGenesisBlk("ledger2") 19 dataHelper := newSampleDataHelper(t) 20 21 dataHelper.populateLedger(l1) 22 dataHelper.populateLedger(l2) 23 24 dataHelper.verifyLedgerContent(l1) 25 dataHelper.verifyLedgerContent(l2) 26 27 t.Run("rebuild only statedb", 28 func(t *testing.T) { 29 env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb) 30 l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2") 31 dataHelper.verifyLedgerContent(l1) 32 dataHelper.verifyLedgerContent(l2) 33 }, 34 ) 35 36 t.Run("rebuild statedb and config history", 37 func(t *testing.T) { 38 env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableConfigHistory) 39 l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2") 40 dataHelper.verifyLedgerContent(l1) 41 dataHelper.verifyLedgerContent(l2) 42 }, 43 ) 44 45 t.Run("rebuild statedb and block index", 46 func(t *testing.T) { 47 env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableBlockIndex) 48 l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2") 49 dataHelper.verifyLedgerContent(l1) 50 dataHelper.verifyLedgerContent(l2) 51 }, 52 ) 53 54 t.Run("rebuild statedb and historydb", 55 func(t *testing.T) { 56 env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableHistoryDB) 57 l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2") 58 dataHelper.verifyLedgerContent(l1) 59 dataHelper.verifyLedgerContent(l2) 60 }, 61 ) 62 } 63 64 func TestRebuildComponentsWithBTL(t *testing.T) { 65 env := newEnv(t) 66 defer env.cleanup() 67 env.initLedgerMgmt() 68 l := env.createTestLedgerFromGenesisBlk("ledger1") 69 collConf := []*collConf{{name: "coll1", btl: 0}, {name: "coll2", btl: 1}} 70 71 // deploy cc1 with 'collConf' 72 l.simulateDeployTx("cc1", collConf) 73 l.cutBlockAndCommitLegacy() 74 75 // commit pvtdata writes in block 2. 76 l.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 := l.cutBlockAndCommitLegacy() 81 82 // After commit of block 2 83 l.verifyPvtState("cc1", "coll1", "key1", "value1") // key1 should still exist in the state 84 l.verifyPvtState("cc1", "coll2", "key2", "value2") // key2 should still exist in the state 85 l.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 l.simulateDataTx("", func(s *simulator) { 91 s.setPvtdata("cc1", "coll1", "someOtherKey", "someOtherVal") 92 s.setPvtdata("cc1", "coll2", "someOtherKey", "someOtherVal") 93 }) 94 l.cutBlockAndCommitLegacy() 95 } 96 97 // After commit of block 4 98 l.verifyPvtState("cc1", "coll1", "key1", "value1") // key1 should still exist in the state 99 l.verifyPvtState("cc1", "coll2", "key2", "") // key2 should have been purged from the state 100 l.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.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableBookkeeper) 107 108 l = env.openTestLedger("ledger1") 109 l.verifyPvtState("cc1", "coll1", "key1", "value1") // key1 should still exist in the state 110 l.verifyPvtState("cc1", "coll2", "key2", "") // key2 should have been purged from the state 111 l.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 l.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 l.cutBlockAndCommitLegacy() 122 123 // commit 2 more blocks with some random key/vals 124 for i := 0; i < 2; i++ { 125 l.simulateDataTx("", func(s *simulator) { 126 s.setPvtdata("cc1", "coll1", "someOtherKey", "someOtherVal") 127 s.setPvtdata("cc1", "coll2", "someOtherKey", "someOtherVal") 128 }) 129 l.cutBlockAndCommitLegacy() 130 } 131 132 // After commit of block 7 133 l.verifyPvtState("cc1", "coll1", "key3", "value1") // key3 should still exist in the state 134 l.verifyPvtState("cc1", "coll2", "key4", "") // key4 should have been purged from the state 135 l.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 }