multi: inject clocks into swap lifecycle and update tests

Inject clock.Clock into swapConfig so that swap initiation times
are deterministic and testable. Update all swap tests to use
TestClock with a fixed time, and assert that InitiationTime
matches the clock value.
This commit is contained in:
Slyghtning 2026-03-13 08:58:56 +01:00
parent 66a17f47e6
commit b702aebbf4
No known key found for this signature in database
GPG key ID: F82D456EA023C9BF
8 changed files with 63 additions and 19 deletions

View file

@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"sync"
"time"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
@ -264,7 +263,7 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
// Instantiate a struct that contains all required data to start the
// swap.
initiationTime := time.Now()
initiationTime := cfg.clock.Now()
contract := loopdb.LoopInContract{
HtlcConfTarget: request.HtlcConfTarget,
@ -837,7 +836,7 @@ func (s *loopInSwap) publishOnChainHtlc(ctx context.Context) (bool, error) {
// the fee for publishing the htlc.
s.cost.Onchain = fee
s.lastUpdateTime = time.Now()
s.lastUpdateTime = s.clock.Now()
if err := s.persistState(ctx); err != nil {
return false, fmt.Errorf("persist htlc tx: %v", err)
}
@ -1210,7 +1209,7 @@ func (s *loopInSwap) persistState(ctx context.Context) error {
// setState updates the swap state and last update timestamp.
func (s *loopInSwap) setState(state loopdb.SwapState) {
s.lastUpdateTime = time.Now()
s.lastUpdateTime = s.clock.Now()
s.state = state
}