From 8b215edaa28536b8fc8db39f36cf5c9569b486ee Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Tue, 30 Jun 2020 14:06:36 +0200 Subject: [PATCH] multi: create init result structs --- client.go | 6 ++++-- loopin.go | 12 ++++++++++-- loopin_test.go | 6 ++++-- loopout.go | 11 +++++++++-- loopout_test.go | 12 ++++++++---- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/client.go b/client.go index 470b701..c33c695 100644 --- a/client.go +++ b/client.go @@ -366,12 +366,13 @@ func (s *Client) LoopOut(globalCtx context.Context, // Create a new swap object for this swap. initiationHeight := s.executor.height() swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server) - swap, err := newLoopOutSwap( + initResult, err := newLoopOutSwap( globalCtx, swapCfg, initiationHeight, request, ) if err != nil { return nil, err } + swap := initResult.swap // Post swap to the main loop. s.executor.initiateSwap(globalCtx, swap) @@ -482,12 +483,13 @@ func (s *Client) LoopIn(globalCtx context.Context, // Create a new swap object for this swap. initiationHeight := s.executor.height() swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server) - swap, err := newLoopInSwap( + initResult, err := newLoopInSwap( globalCtx, swapCfg, initiationHeight, request, ) if err != nil { return nil, err } + swap := initResult.swap // Post swap to the main loop. s.executor.initiateSwap(globalCtx, swap) diff --git a/loopin.go b/loopin.go index fb7e6bd..8645d3b 100644 --- a/loopin.go +++ b/loopin.go @@ -63,9 +63,15 @@ type loopInSwap struct { timeoutAddr btcutil.Address } +// loopInInitResult contains information about a just-initiated loop in swap. +type loopInInitResult struct { + swap *loopInSwap +} + // newLoopInSwap initiates a new loop in swap. func newLoopInSwap(globalCtx context.Context, cfg *swapConfig, - currentHeight int32, request *LoopInRequest) (*loopInSwap, error) { + currentHeight int32, request *LoopInRequest) (*loopInInitResult, + error) { // Request current server loop in terms and use these to calculate the // swap fee that we should subtract from the swap amount in the payment @@ -184,7 +190,9 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig, swap.log.Infof("Server message: %v", swapResp.serverMessage) } - return swap, nil + return &loopInInitResult{ + swap: swap, + }, nil } // resumeLoopInSwap returns a swap object representing a pending swap that has diff --git a/loopin_test.go b/loopin_test.go index ceda3e1..9848236 100644 --- a/loopin_test.go +++ b/loopin_test.go @@ -35,13 +35,14 @@ func TestLoopInSuccess(t *testing.T) { cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server) - swap, err := newLoopInSwap( + initResult, err := newLoopInSwap( context.Background(), cfg, height, &testLoopInRequest, ) if err != nil { t.Fatal(err) } + swap := initResult.swap ctx.store.assertLoopInStored() @@ -159,13 +160,14 @@ func testLoopInTimeout(t *testing.T, req.ExternalHtlc = true } - s, err := newLoopInSwap( + initResult, err := newLoopInSwap( context.Background(), cfg, height, &req, ) if err != nil { t.Fatal(err) } + s := initResult.swap ctx.store.assertLoopInStored() diff --git a/loopout.go b/loopout.go index e1aa270..40b423e 100644 --- a/loopout.go +++ b/loopout.go @@ -73,10 +73,15 @@ type executeConfig struct { loopOutMaxParts uint32 } +// loopOutInitResult contains information about a just-initiated loop out swap. +type loopOutInitResult struct { + swap *loopOutSwap +} + // newLoopOutSwap initiates a new swap with the server and returns a // corresponding swap object. func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig, - currentHeight int32, request *OutRequest) (*loopOutSwap, error) { + currentHeight int32, request *OutRequest) (*loopOutInitResult, error) { // Generate random preimage. var swapPreimage [32]byte @@ -180,7 +185,9 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig, swap.log.Infof("Server message: %v", swapResp.serverMessage) } - return swap, nil + return &loopOutInitResult{ + swap: swap, + }, nil } // resumeLoopOutSwap returns a swap object representing a pending swap that has diff --git a/loopout_test.go b/loopout_test.go index 2263ff0..0c7fa2d 100644 --- a/loopout_test.go +++ b/loopout_test.go @@ -54,12 +54,13 @@ func TestLoopOutPaymentParameters(t *testing.T) { req := *testRequest req.OutgoingChanSet = loopdb.ChannelSet{2, 3} - swap, err := newLoopOutSwap( + initResult, err := newLoopOutSwap( context.Background(), cfg, height, &req, ) if err != nil { t.Fatal(err) } + swap := initResult.swap // Execute the swap in its own goroutine. errChan := make(chan error) @@ -150,12 +151,13 @@ func TestLateHtlcPublish(t *testing.T) { cfg := newSwapConfig(&lnd.LndServices, store, server) - swap, err := newLoopOutSwap( + initResult, err := newLoopOutSwap( context.Background(), cfg, height, testRequest, ) if err != nil { t.Fatal(err) } + swap := initResult.swap sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices} @@ -235,12 +237,13 @@ func TestCustomSweepConfTarget(t *testing.T) { &lnd.LndServices, newStoreMock(t), server, ) - swap, err := newLoopOutSwap( + initResult, err := newLoopOutSwap( context.Background(), cfg, ctx.Lnd.Height, testRequest, ) if err != nil { t.Fatal(err) } + swap := initResult.swap // Set up the required dependencies to execute the swap. // @@ -442,10 +445,11 @@ func TestPreimagePush(t *testing.T) { &lnd.LndServices, newStoreMock(t), server, ) - swap, err := newLoopOutSwap( + initResult, err := newLoopOutSwap( context.Background(), cfg, ctx.Lnd.Height, testRequest, ) require.NoError(t, err) + swap := initResult.swap // Set up the required dependencies to execute the swap. sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}