github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/tests/testledger.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 "time" 12 13 "github.com/hechain20/hechain/core/ledger" 14 "github.com/hechain20/hechain/core/ledger/kvledger" 15 "github.com/stretchr/testify/require" 16 ) 17 18 // testLedger embeds (1) a client, (2) a committer and (3) a verifier, all three operate on 19 // a ledger instance and add helping/reusable functionality on top of ledger apis that helps 20 // in avoiding the repeation in the actual tests code. 21 // the 'client' adds value to the simulation relation apis, the 'committer' helps in cutting the 22 // next block and committing the block, and finally, the verifier helps in veryfying that the 23 // ledger apis returns correct values based on the blocks submitted 24 type testLedger struct { 25 *client 26 *committer 27 *verifier 28 29 lgrid string 30 config *ledger.Config 31 lgr ledger.PeerLedger 32 33 t *testing.T 34 } 35 36 // createTestLedgerFromGenesisBlk creates a new ledger and retruns a 'testhelper' for the ledger 37 func (env *env) createTestLedgerFromGenesisBlk(id string) *testLedger { 38 t := env.t 39 genesisBlk, err := constructTestGenesisBlock(id) 40 require.NoError(t, err) 41 lgr, err := env.ledgerMgr.CreateLedger(id, genesisBlk) 42 require.NoError(t, err) 43 return &testLedger{ 44 client: newClient(lgr, id, t), 45 committer: newCommitter(lgr, t), 46 verifier: newVerifier(lgr, t), 47 lgrid: id, 48 lgr: lgr, 49 config: env.initializer.Config, 50 t: t, 51 } 52 } 53 54 func (env *env) createTestLedgerFromSnapshot(snapshotDir string) *testLedger { 55 t := env.t 56 var lgr ledger.PeerLedger 57 var lgrID string 58 require.NoError( 59 env.t, 60 env.ledgerMgr.CreateLedgerFromSnapshot( 61 snapshotDir, 62 func(l ledger.PeerLedger, id string) { 63 lgr = l 64 lgrID = id 65 }, 66 ), 67 ) 68 require.Eventually( 69 env.t, 70 func() bool { 71 status := env.ledgerMgr.JoinBySnapshotStatus() 72 return !status.InProgress && status.BootstrappingSnapshotDir == "" 73 }, 74 time.Minute, 75 100*time.Microsecond, 76 ) 77 require.NotNil(env.t, lgr) 78 79 return &testLedger{ 80 client: newClient(lgr, lgrID, t), 81 committer: newCommitter(lgr, t), 82 verifier: newVerifier(lgr, t), 83 lgrid: lgrID, 84 lgr: lgr, 85 config: env.initializer.Config, 86 t: t, 87 } 88 } 89 90 // openTestLedger opens an existing ledger and retruns a 'testhelper' for the ledger 91 func (env *env) openTestLedger(id string) *testLedger { 92 t := env.t 93 lgr, err := env.ledgerMgr.OpenLedger(id) 94 require.NoError(t, err) 95 return &testLedger{ 96 client: newClient(lgr, id, t), 97 committer: newCommitter(lgr, t), 98 verifier: newVerifier(lgr, t), 99 lgrid: id, 100 lgr: lgr, 101 config: env.initializer.Config, 102 t: t, 103 } 104 } 105 106 // cutBlockAndCommitLegacy gathers all the transactions simulated by the test code (by calling 107 // the functions available in the 'client') and cuts the next block and commits to the ledger 108 func (l *testLedger) cutBlockAndCommitLegacy() *ledger.BlockAndPvtData { 109 defer func() { 110 l.simulatedTrans = nil 111 l.missingPvtData = make(ledger.TxMissingPvtData) 112 }() 113 return l.committer.cutBlockAndCommitLegacy(l.simulatedTrans, l.missingPvtData) 114 } 115 116 func (l *testLedger) cutBlockAndCommitExpectError() *ledger.BlockAndPvtData { 117 defer func() { 118 l.simulatedTrans = nil 119 l.missingPvtData = make(ledger.TxMissingPvtData) 120 }() 121 return l.committer.cutBlockAndCommitExpectError(l.simulatedTrans, l.missingPvtData) 122 } 123 124 func (l *testLedger) commitPvtDataOfOldBlocks(blocksPvtData []*ledger.ReconciledPvtdata, unreconciled ledger.MissingPvtDataInfo) ([]*ledger.PvtdataHashMismatch, error) { 125 return l.lgr.CommitPvtDataOfOldBlocks(blocksPvtData, unreconciled) 126 } 127 128 func (l *testLedger) generateSnapshot() string { 129 bcInfo, err := l.lgr.GetBlockchainInfo() 130 require.NoError(l.t, err) 131 blockNum := bcInfo.Height - 1 132 require.NoError(l.t, l.lgr.SubmitSnapshotRequest(blockNum)) 133 require.Eventually(l.t, 134 func() bool { 135 requests, err := l.lgr.PendingSnapshotRequests() 136 require.NoError(l.t, err) 137 return len(requests) == 0 138 }, 139 time.Minute, 140 100*time.Millisecond, 141 ) 142 return kvledger.SnapshotDirForLedgerBlockNum(l.config.SnapshotsConfig.RootDir, l.lgrID, blockNum) 143 }