Skip to content

Commit

Permalink
Merge pull request #242 from joostjager/user-message
Browse files Browse the repository at this point in the history
multi: expose server message to clients
  • Loading branch information
joostjager committed Jun 30, 2020
2 parents d76959d + 1869ad6 commit dcb3b50
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 193 deletions.
20 changes: 13 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/lightninglabs/loop/lsat"
"github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/sweep"
"github.com/lightningnetwork/lnd/lntypes"
)

var (
Expand Down Expand Up @@ -354,32 +353,37 @@ func (s *Client) resumeSwaps(ctx context.Context,
//
// The return value is a hash that uniquely identifies the new swap.
func (s *Client) LoopOut(globalCtx context.Context,
request *OutRequest) (*lntypes.Hash, btcutil.Address, error) {
request *OutRequest) (*LoopOutSwapInfo, error) {

log.Infof("LoopOut %v to %v (channels: %v)",
request.Amount, request.DestAddr, request.OutgoingChanSet,
)

if err := s.waitForInitialized(globalCtx); err != nil {
return nil, nil, err
return nil, err
}

// 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, nil, err
return nil, err
}
swap := initResult.swap

// Post swap to the main loop.
s.executor.initiateSwap(globalCtx, swap)

// Return hash so that the caller can identify this swap in the updates
// stream.
return &swap.hash, swap.htlc.Address, nil
return &LoopOutSwapInfo{
SwapHash: swap.hash,
HtlcAddressP2WSH: swap.htlc.Address,
ServerMessage: initResult.serverMessage,
}, nil
}

// LoopOutQuote takes a LoopOut amount and returns a break down of estimated
Expand Down Expand Up @@ -480,12 +484,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)
Expand All @@ -496,6 +501,7 @@ func (s *Client) LoopIn(globalCtx context.Context,
SwapHash: swap.hash,
HtlcAddressP2WSH: swap.htlcP2WSH.Address,
HtlcAddressNP2WSH: swap.htlcNP2WSH.Address,
ServerMessage: initResult.serverMessage,
}
return swapInfo, nil
}
Expand Down
8 changes: 4 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestSuccess(t *testing.T) {
ctx := createClientTestContext(t, nil)

// Initiate loop out.
hash, _, err := ctx.swapClient.LoopOut(context.Background(), testRequest)
info, err := ctx.swapClient.LoopOut(context.Background(), testRequest)
if err != nil {
t.Fatal(err)
}
Expand All @@ -59,7 +59,7 @@ func TestSuccess(t *testing.T) {
// Expect client to register for conf.
confIntent := ctx.AssertRegisterConf(false)

testSuccess(ctx, testRequest.Amount, *hash,
testSuccess(ctx, testRequest.Amount, info.SwapHash,
signalPrepaymentResult, signalSwapPaymentResult, false,
confIntent,
)
Expand All @@ -72,7 +72,7 @@ func TestFailOffchain(t *testing.T) {

ctx := createClientTestContext(t, nil)

_, _, err := ctx.swapClient.LoopOut(context.Background(), testRequest)
_, err := ctx.swapClient.LoopOut(context.Background(), testRequest)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestFailWrongAmount(t *testing.T) {
// Modify mock for this subtest.
modifier(ctx.serverMock)

_, _, err := ctx.swapClient.LoopOut(
_, err := ctx.swapClient.LoopOut(
context.Background(), testRequest,
)
if err != expectedErr {
Expand Down
3 changes: 3 additions & 0 deletions cmd/loop/loopin.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ func loopIn(ctx *cli.Context) error {
fmt.Printf("HTLC address (NP2WSH): %v\n", resp.HtlcAddressNp2Wsh)
}
fmt.Printf("HTLC address (P2WSH): %v\n", resp.HtlcAddressP2Wsh)
if resp.ServerMessage != "" {
fmt.Printf("Server message: %v\n", resp.ServerMessage)
}
fmt.Println()
fmt.Printf("Run `loop monitor` to monitor progress.\n")

Expand Down
7 changes: 5 additions & 2 deletions cmd/loop/loopout.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,11 @@ func loopOut(ctx *cli.Context) error {
}

fmt.Printf("Swap initiated\n")
fmt.Printf("ID: %v\n", resp.Id)
fmt.Printf("HTLC address: %v\n", resp.HtlcAddress)
fmt.Printf("ID: %v\n", resp.Id)
fmt.Printf("HTLC address: %v\n", resp.HtlcAddress)
if resp.ServerMessage != "" {
fmt.Printf("Server message: %v\n", resp.ServerMessage)
}
fmt.Println()
fmt.Printf("Run `loop monitor` to monitor progress.\n")

Expand Down
19 changes: 19 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,25 @@ type LoopInSwapInfo struct { // nolint
// HtlcAddressNP2WSH contains the nested segwit swap htlc address,
// where the loop-in funds may be paid.
HtlcAddressNP2WSH btcutil.Address

// ServerMessages is the human-readable message received from the loop
// server.
ServerMessage string
}

// LoopOutSwapInfo contains essential information of a loop-out swap after the
// swap is initiated.
type LoopOutSwapInfo struct { // nolint:golint
// SwapHash contains the sha256 hash of the swap preimage.
SwapHash lntypes.Hash

// HtlcAddressP2WSH contains the native segwit swap htlc address that
// the server will publish to.
HtlcAddressP2WSH btcutil.Address

// ServerMessages is the human-readable message received from the loop
// server.
ServerMessage string
}

// SwapInfoKit contains common swap info fields.
Expand Down
12 changes: 7 additions & 5 deletions loopd/swapclient_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,18 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
req.OutgoingChanSet = in.OutgoingChanSet
}

hash, htlc, err := s.impl.LoopOut(ctx, req)
info, err := s.impl.LoopOut(ctx, req)
if err != nil {
log.Errorf("LoopOut: %v", err)
return nil, err
}

return &looprpc.SwapResponse{
Id: hash.String(),
IdBytes: hash[:],
HtlcAddress: htlc.String(),
HtlcAddressP2Wsh: htlc.String(),
Id: info.SwapHash.String(),
IdBytes: info.SwapHash[:],
HtlcAddress: info.HtlcAddressP2WSH.String(),
HtlcAddressP2Wsh: info.HtlcAddressP2WSH.String(),
ServerMessage: info.ServerMessage,
}, nil
}

Expand Down Expand Up @@ -456,6 +457,7 @@ func (s *swapClientServer) LoopIn(ctx context.Context,
Id: swapInfo.SwapHash.String(),
IdBytes: swapInfo.SwapHash[:],
HtlcAddressP2Wsh: swapInfo.HtlcAddressP2WSH.String(),
ServerMessage: swapInfo.ServerMessage,
}

if req.ExternalHtlc {
Expand Down
18 changes: 16 additions & 2 deletions loopin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ type loopInSwap struct {
timeoutAddr btcutil.Address
}

// loopInInitResult contains information about a just-initiated loop in swap.
type loopInInitResult struct {
swap *loopInSwap
serverMessage string
}

// 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
Expand Down Expand Up @@ -180,7 +187,14 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
return nil, fmt.Errorf("cannot store swap: %v", err)
}

return swap, nil
if swapResp.serverMessage != "" {
swap.log.Infof("Server message: %v", swapResp.serverMessage)
}

return &loopInInitResult{
swap: swap,
serverMessage: swapResp.serverMessage,
}, nil
}

// resumeLoopInSwap returns a swap object representing a pending swap that has
Expand Down
6 changes: 4 additions & 2 deletions loopin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
17 changes: 15 additions & 2 deletions loopout.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ type executeConfig struct {
loopOutMaxParts uint32
}

// loopOutInitResult contains information about a just-initiated loop out swap.
type loopOutInitResult struct {
swap *loopOutSwap
serverMessage string
}

// 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
Expand Down Expand Up @@ -176,7 +182,14 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
return nil, fmt.Errorf("cannot store swap: %v", err)
}

return swap, nil
if swapResp.serverMessage != "" {
swap.log.Infof("Server message: %v", swapResp.serverMessage)
}

return &loopOutInitResult{
swap: swap,
serverMessage: swapResp.serverMessage,
}, nil
}

// resumeLoopOutSwap returns a swap object representing a pending swap that has
Expand Down
12 changes: 8 additions & 4 deletions loopout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}

Expand Down Expand Up @@ -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.
//
Expand Down Expand Up @@ -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}
Expand Down
Loading

0 comments on commit dcb3b50

Please sign in to comment.