Browse Source

loop: extract code from client package into new loop primary package

pull/9/head
Olaoluwa Osuntokun 1 year ago
parent
commit
908d82acdb
No known key found for this signature in database GPG Key ID: CE58F7F8E20FD9A2
21 changed files with 126 additions and 147 deletions
  1. +12
    -13
      client.go
  2. +0
    -24
      client/log.go
  3. +0
    -20
      client/utils.go
  4. +1
    -1
      client_test.go
  5. +1
    -1
      config.go
  6. +5
    -1
      executor.go
  7. +8
    -61
      interface.go
  8. +3
    -3
      lndclient/chainnotifier_client.go
  9. +2
    -2
      lndclient/lnd_services.go
  10. +2
    -2
      lndclient/signer_client.go
  11. +3
    -3
      lndclient/walletkit_client.go
  12. +70
    -0
      log.go
  13. +1
    -1
      server_mock_test.go
  14. +1
    -1
      store_mock_test.go
  15. +1
    -1
      swap.go
  16. +1
    -1
      swap_server_client.go
  17. +2
    -2
      test/lightning_client_mock.go
  18. +7
    -4
      testcontext_test.go
  19. +4
    -5
      uncharge.go
  20. +1
    -0
      uncharge_state.go
  21. +1
    -1
      uncharge_test.go

client/client.go → client.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"context"
@ -46,8 +46,8 @@ var (
republishDelay = 10 * time.Second
)
// Client performs the client side part of swaps. This interface exists to
// be able to implement a stub.
// Client performs the client side part of swaps. This interface exists to be
// able to implement a stub.
type Client struct {
started uint32 // To be used atomically.
errChan chan error
@ -118,9 +118,9 @@ func (s *Client) GetUnchargeSwaps() ([]*PersistentUncharge, error) {
}
// Run is a blocking call that executes all swaps. Any pending swaps are
// restored from persistent storage and resumed. Subsequent updates
// will be sent through the passed in statusChan. The function can be
// terminated by cancelling the context.
// restored from persistent storage and resumed. Subsequent updates will be
// sent through the passed in statusChan. The function can be terminated by
// cancelling the context.
func (s *Client) Run(ctx context.Context,
statusChan chan<- SwapInfo) error {
@ -213,11 +213,10 @@ func (s *Client) resumeSwaps(ctx context.Context,
}
}
// Uncharge initiates a uncharge swap. It blocks until the swap is
// initiation with the swap server is completed (typically this takes
// only a short amount of time). From there on further status
// information can be acquired through the status channel returned from
// the Run call.
// Uncharge initiates a uncharge swap. It blocks until the swap is initiation
// with the swap server is completed (typically this takes only a short amount
// of time). From there on further status information can be acquired through
// the status channel returned from the Run call.
//
// When the call returns, the swap has been persisted and will be
// resumed automatically after restarts.
@ -258,8 +257,8 @@ func (s *Client) Uncharge(globalCtx context.Context,
}
// UnchargeQuote takes a Uncharge amount and returns a break down of estimated
// costs for the client. Both the swap server and the on-chain fee estimator are
// queried to get to build the quote response.
// costs for the client. Both the swap server and the on-chain fee estimator
// are queried to get to build the quote response.
func (s *Client) UnchargeQuote(ctx context.Context,
request *UnchargeQuoteRequest) (*UnchargeQuote, error) {

+ 0
- 24
client/log.go View File

@ -1,24 +0,0 @@
package client
import (
"github.com/btcsuite/btclog"
"os"
)
// log is a logger that is initialized with no output filters. This
// means the package will not perform any logging by default until the caller
// requests it.
var (
backendLog = btclog.NewBackend(logWriter{})
logger = backendLog.Logger("CLIENT")
servicesLogger = backendLog.Logger("SERVICES")
)
// logWriter implements an io.Writer that outputs to both standard output and
// the write-end pipe of an initialized log rotator.
type logWriter struct{}
func (logWriter) Write(p []byte) (n int, err error) {
os.Stdout.Write(p)
return len(p), nil
}

+ 0
- 20
client/utils.go View File

@ -1,20 +0,0 @@
package client
import (
"errors"
"github.com/btcsuite/btcd/wire"
)
// getTxInputByOutpoint returns a tx input based on a given input outpoint.
func getTxInputByOutpoint(tx *wire.MsgTx, input *wire.OutPoint) (
*wire.TxIn, error) {
for _, in := range tx.TxIn {
if in.PreviousOutPoint == *input {
return in, nil
}
}
return nil, errors.New("input not found")
}

client/client_test.go → client_test.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"bytes"

client/config.go → config.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"time"

client/executor.go → executor.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"context"
@ -21,6 +21,8 @@ type executorConfig struct {
}
// executor is responsible for executing swaps.
//
// TODO(roasbeef): rename to SubSwapper
type executor struct {
wg sync.WaitGroup
newSwaps chan genericSwap
@ -91,6 +93,7 @@ func (s *executor) run(mainCtx context.Context,
nextSwapID := 0
for {
select {
case newSwap := <-s.newSwaps:
queue := queue.NewConcurrentQueue(10)
queue.Start()
@ -115,6 +118,7 @@ func (s *executor) run(mainCtx context.Context,
}()
nextSwapID++
case doneID := <-swapDoneChan:
queue, ok := blockEpochQueues[doneID]
if !ok {

client/interface.go → interface.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"time"
@ -29,9 +29,9 @@ type UnchargeRequest struct {
MaxPrepayRoutingFee btcutil.Amount
// MaxSwapFee is the maximum we are willing to pay the server for the
// swap. This value is not disclosed in the swap initiation call, but if
// the server asks for a higher fee, we abort the swap. Typically this
// value is taken from the response of the UnchargeQuote call. It
// swap. This value is not disclosed in the swap initiation call, but
// if the server asks for a higher fee, we abort the swap. Typically
// this value is taken from the response of the UnchargeQuote call. It
// includes the prepay amount.
MaxSwapFee btcutil.Amount
@ -66,28 +66,6 @@ type UnchargeRequest struct {
UnchargeChannel *uint64
}
// UnchargeContract contains the data that is serialized to persistent storage for
// pending swaps.
type UnchargeContract struct {
SwapContract
DestAddr btcutil.Address
SwapInvoice string
// MaxSwapRoutingFee is the maximum off-chain fee in msat that may be
// paid for the swap payment to the server.
MaxSwapRoutingFee btcutil.Amount
// SweepConfTarget specifies the targeted confirmation target for the
// client sweep tx.
SweepConfTarget int32
// UnchargeChannel is the channel to uncharge. If zero, any channel may
// be used.
UnchargeChannel *uint64
}
// UnchargeSwapInfo contains status information for a uncharge swap.
type UnchargeSwapInfo struct {
UnchargeContract
@ -152,7 +130,8 @@ type UnchargeTerms struct {
// SwapFeeRate is the variable fee in parts per million.
SwapFeeRate int64
// PrepayAmt is the fixed part of the swap fee that needs to be prepaid.
// PrepayAmt is the fixed part of the swap fee that needs to be
// prepaid.
PrepayAmt btcutil.Amount
// MinSwapAmount is the minimum amount that the server requires for a
@ -172,42 +151,10 @@ type UnchargeTerms struct {
SwapPaymentDest [33]byte
}
// SwapContract contains the base data that is serialized to persistent storage
// for pending swaps.
type SwapContract struct {
Preimage lntypes.Preimage
AmountRequested btcutil.Amount
PrepayInvoice string
SenderKey [33]byte
ReceiverKey [33]byte
CltvExpiry int32
// MaxPrepayRoutingFee is the maximum off-chain fee in msat that may be
// paid for the prepayment to the server.
MaxPrepayRoutingFee btcutil.Amount
// MaxSwapFee is the maximum we are willing to pay the server for the
// swap.
MaxSwapFee btcutil.Amount
// MaxMinerFee is the maximum in on-chain fees that we are willing to
// spend.
MaxMinerFee btcutil.Amount
// InitiationHeight is the block height at which the swap was initiated.
InitiationHeight int32
// InitiationTime is the time at which the swap was initiated.
InitiationTime time.Time
}
// SwapInfoKit contains common swap info fields.
type SwapInfoKit struct {
// Hash is the sha256 hash of the preimage that unlocks the htlcs. It is
// used to uniquely identify this swap.
// Hash is the sha256 hash of the preimage that unlocks the htlcs. It
// is used to uniquely identify this swap.
Hash lntypes.Hash
// LastUpdateTime is the time of the last update of this swap.

+ 3
- 3
lndclient/chainnotifier_client.go View File

@ -7,7 +7,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/loop/utils"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
"google.golang.org/grpc"
@ -77,7 +77,7 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
if err != nil {
return err
}
tx, err := utils.DecodeTx(d.RawSpendingTx)
tx, err := swap.DecodeTx(d.RawSpendingTx)
if err != nil {
return err
}
@ -165,7 +165,7 @@ func (s *chainNotifierClient) RegisterConfirmationsNtfn(ctx context.Context,
// Script confirmed
case *chainrpc.ConfEvent_Conf:
tx, err := utils.DecodeTx(c.Conf.RawTx)
tx, err := swap.DecodeTx(c.Conf.RawTx)
if err != nil {
errChan <- err
return

+ 2
- 2
lndclient/lnd_services.go View File

@ -4,13 +4,13 @@ import (
"context"
"errors"
"fmt"
"github.com/lightninglabs/loop/utils"
"io/ioutil"
"path/filepath"
"time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/macaroons"
"google.golang.org/grpc"
@ -53,7 +53,7 @@ func NewLndServices(lndAddress string, application string,
logger.Infof("Connected to lnd")
chainParams, err := utils.ChainParamsFromNetwork(network)
chainParams, err := swap.ChainParamsFromNetwork(network)
if err != nil {
return nil, err
}

+ 2
- 2
lndclient/signer_client.go View File

@ -4,7 +4,7 @@ import (
"context"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/loop/utils"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
"google.golang.org/grpc"
@ -29,7 +29,7 @@ func newSignerClient(conn *grpc.ClientConn) *signerClient {
func (s *signerClient) SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
signDescriptors []*input.SignDescriptor) ([][]byte, error) {
txRaw, err := utils.EncodeTx(tx)
txRaw, err := swap.EncodeTx(tx)
if err != nil {
return nil, err
}

+ 3
- 3
lndclient/walletkit_client.go View File

@ -6,7 +6,7 @@ import (
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/lightninglabs/loop/utils"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
@ -117,7 +117,7 @@ func (m *walletKitClient) NextAddr(ctx context.Context) (
func (m *walletKitClient) PublishTransaction(ctx context.Context,
tx *wire.MsgTx) error {
txHex, err := utils.EncodeTx(tx)
txHex, err := swap.EncodeTx(tx)
if err != nil {
return err
}
@ -155,7 +155,7 @@ func (m *walletKitClient) SendOutputs(ctx context.Context,
return nil, err
}
tx, err := utils.DecodeTx(resp.RawTx)
tx, err := swap.DecodeTx(resp.RawTx)
if err != nil {
return nil, err
}

+ 70
- 0
log.go View File

@ -0,0 +1,70 @@
package loop
import (
"fmt"
"os"
"github.com/btcsuite/btclog"
"github.com/lightningnetwork/lnd/lntypes"
)
// log is a logger that is initialized with no output filters. This
// means the package will not perform any logging by default until the caller
// requests it.
var (
backendLog = btclog.NewBackend(logWriter{})
logger = backendLog.Logger("CLIENT")
servicesLogger = backendLog.Logger("SERVICES")
)
// logWriter implements an io.Writer that outputs to both standard output and
// the write-end pipe of an initialized log rotator.
type logWriter struct{}
func (logWriter) Write(p []byte) (n int, err error) {
os.Stdout.Write(p)
return len(p), nil
}
// SwapLog logs with a short swap hash prefix.
type SwapLog struct {
// Logger is the underlying based logger.
Logger btclog.Logger
// Hash is the hash the identifies the target swap.
Hash lntypes.Hash
}
// Infof formats message according to format specifier and writes to
// log with LevelInfo.
func (s *SwapLog) Infof(format string, params ...interface{}) {
s.Logger.Infof(
fmt.Sprintf("%v %s", ShortHash(&s.Hash), format),
params...,
)
}
// Warnf formats message according to format specifier and writes to
// to log with LevelError.
func (s *SwapLog) Warnf(format string, params ...interface{}) {
s.Logger.Warnf(
fmt.Sprintf("%v %s", ShortHash(&s.Hash), format),
params...,
)
}
// Errorf formats message according to format specifier and writes to
// to log with LevelError.
func (s *SwapLog) Errorf(format string, params ...interface{}) {
s.Logger.Errorf(
fmt.Sprintf("%v %s", ShortHash(&s.Hash), format),
params...,
)
}
// ShortHash returns a shortened version of the hash suitable for use in
// logging.
func ShortHash(hash *lntypes.Hash) string {
return hash.String()[:6]
}

client/server_mock_test.go → server_mock_test.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"context"

client/store_mock_test.go → store_mock_test.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"errors"

client/swap.go → swap.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"context"

client/swap_server_client.go → swap_server_client.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"context"

+ 2
- 2
test/lightning_client_mock.go View File

@ -9,7 +9,7 @@ import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/lightninglabs/loop/lndclient"
"github.com/lightninglabs/loop/utils"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
@ -34,7 +34,7 @@ func (h *mockLightningClient) PayInvoice(ctx context.Context, invoice string,
go func() {
defer h.wg.Done()
amt, err := utils.GetInvoiceAmt(&chaincfg.TestNet3Params, invoice)
amt, err := swap.GetInvoiceAmt(&chaincfg.TestNet3Params, invoice)
if err != nil {
select {
case done <- lndclient.PaymentResult{

client/testcontext_test.go → testcontext_test.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"context"
@ -139,8 +139,9 @@ func (ctx *testContext) finish() {
ctx.assertIsDone()
}
// notifyHeight notifies swap client of the arrival of a new block and waits for
// the notification to be processed by selecting on a dedicated test channel.
// notifyHeight notifies swap client of the arrival of a new block and
// waits for the notification to be processed by selecting on a dedicated
// test channel.
func (ctx *testContext) notifyHeight(height int32) {
ctx.T.Helper()
@ -205,7 +206,9 @@ func (ctx *testContext) assertStatus(expectedState SwapState) {
}
}
func (ctx *testContext) publishHtlc(script []byte, amt btcutil.Amount) wire.OutPoint {
func (ctx *testContext) publishHtlc(script []byte,
amt btcutil.Amount) wire.OutPoint {
// Create the htlc tx.
htlcTx := wire.MsgTx{}
htlcTx.AddTxIn(&wire.TxIn{

client/uncharge.go → uncharge.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"context"
@ -305,10 +305,9 @@ func (s *unchargeSwap) executeSwap(globalCtx context.Context) error {
return err
}
// Inspect witness stack to see if it is a success transaction. We don't
// just try to match with the hash of our sweep tx, because it may be
// swept by a different (fee) sweep tx from a previous run.
// Inspect witness stack to see if it is a success transaction. We
// don't just try to match with the hash of our sweep tx, because it
// may be swept by a different (fee) sweep tx from a previous run.
htlcInput, err := getTxInputByOutpoint(
spendDetails.SpendingTx, htlcOutpoint,
)

+ 1
- 0
uncharge_state.go View File

@ -0,0 +1 @@
package loop

client/uncharge_test.go → uncharge_test.go View File

@ -1,4 +1,4 @@
package client
package loop
import (
"context"

Loading…
Cancel
Save