lntest+itest: add new method CurrentHeight

This commit is contained in:
yyforyongyu authored and yyforyongyu committed 2024-07-23 21:30:08 +08:00
1 parent 14e7b134d9
commit f1f341095e
14 files changed
+66 -41

No files matched your search

+5 -1
View File
@@ -105,6 +105,9 @@ type HarnessTest struct {
// cleaned specifies whether the cleanup has been applied for the
// current HarnessTest.
cleaned bool
// currentHeight is the current height of the chain backend.
currentHeight uint32
}
// harnessOpts contains functional option to modify the behavior of the various
@@ -433,7 +436,8 @@ func (h *HarnessTest) Subtest(t *testing.T) *HarnessTest {
st.feeService.Reset()
// Record block height.
_, startHeight := h.GetBestBlock()
h.updateCurrentHeight()
startHeight := int32(h.CurrentHeight())
st.Cleanup(func() {
_, endHeight := h.GetBestBlock()
+21
View File
@@ -31,6 +31,9 @@ func (h *HarnessTest) Miner() *miner.HarnessMiner {
func (h *HarnessTest) MineBlocks(num int) {
require.Less(h, num, maxBlocksAllowed, "too many blocks to mine")
// Update the harness's current height.
defer h.updateCurrentHeight()
// Mine num of blocks.
for i := 0; i < num; i++ {
block := h.miner.MineBlocks(1)[0]
@@ -73,6 +76,9 @@ func (h *HarnessTest) MineBlocks(num int) {
func (h *HarnessTest) MineEmptyBlocks(num int) []*wire.MsgBlock {
require.Less(h, num, maxBlocksAllowed, "too many blocks to mine")
// Update the harness's current height.
defer h.updateCurrentHeight()
blocks := h.miner.MineEmptyBlocks(num)
// Finally, make sure all the active nodes are synced.
@@ -90,6 +96,9 @@ func (h *HarnessTest) MineEmptyBlocks(num int) []*wire.MsgBlock {
func (h *HarnessTest) MineBlocksAndAssertNumTxes(num uint32,
numTxs int) []*wire.MsgBlock {
// Update the harness's current height.
defer h.updateCurrentHeight()
// If we expect transactions to be included in the blocks we'll mine,
// we wait here until they are seen in the miner's mempool.
txids := h.AssertNumTxsInMempool(numTxs)
@@ -309,3 +318,15 @@ func (h *HarnessTest) SendRawTransaction(tx *wire.MsgTx,
return *txid, nil
}
// CurrentHeight returns the current block height.
func (h *HarnessTest) CurrentHeight() uint32 {
return h.currentHeight
}
// updateCurrentHeight set the harness's current height to the best known
// height.
func (h *HarnessTest) updateCurrentHeight() {
_, height := h.GetBestBlock()
h.currentHeight = uint32(height)
}