diff --git a/chainntnfs/test_utils.go b/chainntnfs/test_utils.go index cfdafd85b..44c844d09 100644 --- a/chainntnfs/test_utils.go +++ b/chainntnfs/test_utils.go @@ -26,6 +26,13 @@ import ( "github.com/lightninglabs/neutrino" ) +var ( + // trickleInterval is the interval at which the miner should trickle + // transactions to its peers. We'll set it small to ensure the miner + // propagates transactions quickly in the tests. + trickleInterval = 10 * time.Millisecond +) + var ( NetParams = &chaincfg.RegressionNetParams @@ -62,6 +69,7 @@ func GetTestTxidAndScript(h *rpctest.Harness) (*chainhash.Hash, []byte, error) { // WaitForMempoolTx waits for the txid to be seen in the miner's mempool. func WaitForMempoolTx(miner *rpctest.Harness, txid *chainhash.Hash) error { timeout := time.After(10 * time.Second) + trickle := time.After(2 * trickleInterval) for { // Check for the harness' knowledge of the txid. tx, err := miner.Node.GetRawTransaction(txid) @@ -84,6 +92,16 @@ func WaitForMempoolTx(miner *rpctest.Harness, txid *chainhash.Hash) error { } } + // To ensure any transactions propagate from the miner to the peers + // before returning, ensure we have waited for at least + // 2*trickleInterval before returning. + select { + case <-trickle: + case <-timeout: + return errors.New("timeout waiting for trickle interval. " + + "Trickle interval to large?") + } + return nil } @@ -141,6 +159,10 @@ func NewMiner(t *testing.T, extraArgs []string, createChain bool, t.Helper() + // Add the trickle interval argument to the extra args. + trickle := fmt.Sprintf("--trickleinterval=%v", trickleInterval) + extraArgs = append(extraArgs, trickle) + node, err := rpctest.New(NetParams, nil, extraArgs) if err != nil { t.Fatalf("unable to create backend node: %v", err)