From d98e041e9433adfd4fe4d22d7633af965db9a74a Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Mon, 4 Sep 2023 09:29:01 +0200 Subject: [PATCH] move basetypes into lightning --- cln/cln_client.go | 11 +++++------ cln/cln_interceptor.go | 4 ++-- interceptor/intercept.go | 3 +-- lightning/client.go | 7 +++---- {basetypes => lightning}/outpoint.go | 2 +- {basetypes => lightning}/short_channel_id.go | 2 +- lnd/client.go | 9 ++++----- lnd/interceptor.go | 4 ++-- lsps2/mocks.go | 5 ++--- lsps2/scid.go | 6 +++--- lsps2/store.go | 8 ++++---- postgresql/intercept_store.go | 4 ++-- postgresql/lsps2_store.go | 8 ++++---- 13 files changed, 34 insertions(+), 39 deletions(-) rename {basetypes => lightning}/outpoint.go (95%) rename {basetypes => lightning}/short_channel_id.go (98%) diff --git a/cln/cln_client.go b/cln/cln_client.go index c2c8b83..2909806 100644 --- a/cln/cln_client.go +++ b/cln/cln_client.go @@ -8,7 +8,6 @@ import ( "strings" "time" - "github.com/breez/lspd/basetypes" "github.com/breez/lspd/lightning" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" @@ -136,7 +135,7 @@ func (c *ClnClient) OpenChannel(req *lightning.OpenChannelRequest) (*wire.OutPoi return nil, err } - channelPoint, err := basetypes.NewOutPoint(fundingTxId[:], uint32(fundResult.FundingTxOutputNum)) + channelPoint, err := lightning.NewOutPoint(fundingTxId[:], uint32(fundResult.FundingTxOutputNum)) if err != nil { log.Printf("CLN: NewOutPoint(%s, %d) error: %v", fundingTxId.String(), fundResult.FundingTxOutputNum, err) return nil, err @@ -157,12 +156,12 @@ func (c *ClnClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*ligh for _, c := range peer.Channels { log.Printf("getChannel destination: %s, Short channel id: %v, local alias: %v , FundingTxID:%v, State:%v ", pubkey, c.ShortChannelId, c.Alias.Local, c.FundingTxId, c.State) if slices.Contains(OPEN_STATUSES, c.State) && c.FundingTxId == fundingTxID { - confirmedChanID, err := basetypes.NewShortChannelIDFromString(c.ShortChannelId) + confirmedChanID, err := lightning.NewShortChannelIDFromString(c.ShortChannelId) if err != nil { fmt.Printf("NewShortChannelIDFromString %v error: %v", c.ShortChannelId, err) return nil, err } - initialChanID, err := basetypes.NewShortChannelIDFromString(c.Alias.Local) + initialChanID, err := lightning.NewShortChannelIDFromString(c.Alias.Local) if err != nil { fmt.Printf("NewShortChannelIDFromString %v error: %v", c.Alias.Local, err) return nil, err @@ -213,7 +212,7 @@ func (c *ClnClient) GetClosedChannels(nodeID string, channelPoints map[string]ui lookup := make(map[string]uint64) for _, c := range peer.Channels { if slices.Contains(CLOSING_STATUSES, c.State) { - cid, err := basetypes.NewShortChannelIDFromString(c.ShortChannelId) + cid, err := lightning.NewShortChannelIDFromString(c.ShortChannelId) if err != nil { log.Printf("CLN: GetClosedChannels NewShortChannelIDFromString(%v) error: %v", c.ShortChannelId, err) continue @@ -234,7 +233,7 @@ func (c *ClnClient) GetClosedChannels(nodeID string, channelPoints map[string]ui return r, nil } -func (c *ClnClient) GetPeerId(scid *basetypes.ShortChannelID) ([]byte, error) { +func (c *ClnClient) GetPeerId(scid *lightning.ShortChannelID) ([]byte, error) { scidStr := scid.ToString() peers, err := c.client.ListPeers() if err != nil { diff --git a/cln/cln_interceptor.go b/cln/cln_interceptor.go index 3a57987..f853b96 100644 --- a/cln/cln_interceptor.go +++ b/cln/cln_interceptor.go @@ -10,10 +10,10 @@ import ( "sync" "time" - "github.com/breez/lspd/basetypes" "github.com/breez/lspd/cln_plugin/proto" "github.com/breez/lspd/config" "github.com/breez/lspd/interceptor" + "github.com/breez/lspd/lightning" sphinx "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/record" @@ -140,7 +140,7 @@ func (i *ClnHtlcInterceptor) intercept() error { return } - scid, err := basetypes.NewShortChannelIDFromString(request.Onion.ShortChannelId) + scid, err := lightning.NewShortChannelIDFromString(request.Onion.ShortChannelId) if err != nil { interceptorClient.Send(i.defaultResolution(request)) i.doneWg.Done() diff --git a/interceptor/intercept.go b/interceptor/intercept.go index c54cb28..1ff0a7f 100644 --- a/interceptor/intercept.go +++ b/interceptor/intercept.go @@ -9,7 +9,6 @@ import ( "math/big" "time" - "github.com/breez/lspd/basetypes" "github.com/breez/lspd/chain" "github.com/breez/lspd/config" "github.com/breez/lspd/lightning" @@ -80,7 +79,7 @@ func NewInterceptor( } } -func (i *Interceptor) Intercept(scid *basetypes.ShortChannelID, reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32, reqIncomingExpiry uint32) InterceptResult { +func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32, reqIncomingExpiry uint32) InterceptResult { reqPaymentHashStr := hex.EncodeToString(reqPaymentHash) log.Printf("Intercept: scid: %s, paymentHash: %s, outgoindAmount: %v, outgoingExpiry: %v, incomingExpiry: %v", scid.ToString(), reqPaymentHashStr, reqOutgoingAmountMsat, reqOutgoingExpiry, reqIncomingExpiry) resp, _, _ := i.payHashGroup.Do(reqPaymentHashStr, func() (interface{}, error) { diff --git a/lightning/client.go b/lightning/client.go index 7aba6ea..236f0cd 100644 --- a/lightning/client.go +++ b/lightning/client.go @@ -3,7 +3,6 @@ package lightning import ( "time" - "github.com/breez/lspd/basetypes" "github.com/btcsuite/btcd/wire" ) @@ -13,8 +12,8 @@ type GetInfoResult struct { } type GetChannelResult struct { - InitialChannelID basetypes.ShortChannelID - ConfirmedChannelID basetypes.ShortChannelID + InitialChannelID ShortChannelID + ConfirmedChannelID ShortChannelID HtlcMinimumMsat uint64 } @@ -34,7 +33,7 @@ type Client interface { IsConnected(destination []byte) (bool, error) OpenChannel(req *OpenChannelRequest) (*wire.OutPoint, error) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*GetChannelResult, error) - GetPeerId(scid *basetypes.ShortChannelID) ([]byte, error) + GetPeerId(scid *ShortChannelID) ([]byte, error) GetNodeChannelCount(nodeID []byte) (int, error) GetClosedChannels(nodeID string, channelPoints map[string]uint64) (map[string]uint64, error) WaitOnline(peerID []byte, deadline time.Time) error diff --git a/basetypes/outpoint.go b/lightning/outpoint.go similarity index 95% rename from basetypes/outpoint.go rename to lightning/outpoint.go index cdfa979..43f2715 100644 --- a/basetypes/outpoint.go +++ b/lightning/outpoint.go @@ -1,4 +1,4 @@ -package basetypes +package lightning import ( "log" diff --git a/basetypes/short_channel_id.go b/lightning/short_channel_id.go similarity index 98% rename from basetypes/short_channel_id.go rename to lightning/short_channel_id.go index 4b88c7a..45ca07f 100644 --- a/basetypes/short_channel_id.go +++ b/lightning/short_channel_id.go @@ -1,4 +1,4 @@ -package basetypes +package lightning import ( "fmt" diff --git a/lnd/client.go b/lnd/client.go index eacaad3..0006da3 100644 --- a/lnd/client.go +++ b/lnd/client.go @@ -9,7 +9,6 @@ import ( "sync" "time" - "github.com/breez/lspd/basetypes" "github.com/breez/lspd/config" "github.com/breez/lspd/lightning" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -275,7 +274,7 @@ func (c *LndClient) OpenChannel(req *lightning.OpenChannelRequest) (*wire.OutPoi return nil, fmt.Errorf("LND: OpenChannel() error: %w", err) } - result, err := basetypes.NewOutPoint(channelPoint.GetFundingTxidBytes(), channelPoint.OutputIndex) + result, err := lightning.NewOutPoint(channelPoint.GetFundingTxidBytes(), channelPoint.OutputIndex) if err != nil { log.Printf("LND: OpenChannel returned invalid outpoint. error: %v", err) return nil, err @@ -307,8 +306,8 @@ func (c *LndClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*ligh } } return &lightning.GetChannelResult{ - InitialChannelID: basetypes.ShortChannelID(c.ChanId), - ConfirmedChannelID: basetypes.ShortChannelID(confirmedChanId), + InitialChannelID: lightning.ShortChannelID(c.ChanId), + ConfirmedChannelID: lightning.ShortChannelID(confirmedChanId), HtlcMinimumMsat: c.LocalConstraints.MinHtlcMsat, }, nil } @@ -380,7 +379,7 @@ func (c *LndClient) getWaitingCloseChannels(nodeID string) ([]*lnrpc.PendingChan return waitingCloseChannels, nil } -func (c *LndClient) GetPeerId(scid *basetypes.ShortChannelID) ([]byte, error) { +func (c *LndClient) GetPeerId(scid *lightning.ShortChannelID) ([]byte, error) { scidu64 := uint64(*scid) peer, err := c.client.GetPeerIdByScid(context.Background(), &lnrpc.GetPeerIdByScidRequest{ Scid: scidu64, diff --git a/lnd/interceptor.go b/lnd/interceptor.go index 2d4ecb1..0f6c6ed 100644 --- a/lnd/interceptor.go +++ b/lnd/interceptor.go @@ -7,9 +7,9 @@ import ( "sync" "time" - "github.com/breez/lspd/basetypes" "github.com/breez/lspd/config" "github.com/breez/lspd/interceptor" + "github.com/breez/lspd/lightning" "github.com/btcsuite/btcd/btcec/v2" sphinx "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/lnrpc" @@ -135,7 +135,7 @@ func (i *LndHtlcInterceptor) intercept() error { i.doneWg.Add(1) go func() { - scid := basetypes.ShortChannelID(request.OutgoingRequestedChanId) + scid := lightning.ShortChannelID(request.OutgoingRequestedChanId) interceptResult := i.interceptor.Intercept(&scid, request.PaymentHash, request.OutgoingAmountMsat, request.OutgoingExpiry, request.IncomingExpiry) switch interceptResult.Action { case interceptor.INTERCEPT_RESUME_WITH_ONION: diff --git a/lsps2/mocks.go b/lsps2/mocks.go index ff87387..1db2f91 100644 --- a/lsps2/mocks.go +++ b/lsps2/mocks.go @@ -6,7 +6,6 @@ import ( "fmt" "time" - "github.com/breez/lspd/basetypes" "github.com/breez/lspd/chain" "github.com/breez/lspd/lightning" "github.com/breez/lspd/shared" @@ -61,7 +60,7 @@ func (s *mockLsps2Store) RegisterBuy(ctx context.Context, req *RegisterBuy) erro return s.err } -func (s *mockLsps2Store) GetBuyRegistration(ctx context.Context, scid basetypes.ShortChannelID) (*BuyRegistration, error) { +func (s *mockLsps2Store) GetBuyRegistration(ctx context.Context, scid lightning.ShortChannelID) (*BuyRegistration, error) { if s.delay.Nanoseconds() != 0 { <-time.After(s.delay) } @@ -119,7 +118,7 @@ func (c *mockLightningClient) GetChannel(peerID []byte, channelPoint wire.OutPoi return res, nil } -func (c *mockLightningClient) GetPeerId(scid *basetypes.ShortChannelID) ([]byte, error) { +func (c *mockLightningClient) GetPeerId(scid *lightning.ShortChannelID) ([]byte, error) { return nil, ErrNotImplemented } func (c *mockLightningClient) GetNodeChannelCount(nodeID []byte) (int, error) { diff --git a/lsps2/scid.go b/lsps2/scid.go index 31ad9f8..733cd77 100644 --- a/lsps2/scid.go +++ b/lsps2/scid.go @@ -4,7 +4,7 @@ import ( "crypto/rand" "math/big" - "github.com/breez/lspd/basetypes" + "github.com/breez/lspd/lightning" ) var one = big.NewInt(1) @@ -13,12 +13,12 @@ var sixtyfour = big.NewInt(64) var twoPowSixtyfour = two.Exp(two, sixtyfour, nil) var maxUint64 = twoPowSixtyfour.Sub(twoPowSixtyfour, one) -func newScid() (*basetypes.ShortChannelID, error) { +func newScid() (*lightning.ShortChannelID, error) { s, err := rand.Int(rand.Reader, maxUint64) if err != nil { return nil, err } - scid := basetypes.ShortChannelID(s.Uint64()) + scid := lightning.ShortChannelID(s.Uint64()) return &scid, nil } diff --git a/lsps2/store.go b/lsps2/store.go index 8bdb7ec..7dcc886 100644 --- a/lsps2/store.go +++ b/lsps2/store.go @@ -6,7 +6,7 @@ import ( "log" "time" - "github.com/breez/lspd/basetypes" + "github.com/breez/lspd/lightning" "github.com/breez/lspd/lsps0" "github.com/breez/lspd/shared" "github.com/btcsuite/btcd/wire" @@ -15,7 +15,7 @@ import ( type RegisterBuy struct { LspId string PeerId string - Scid basetypes.ShortChannelID + Scid lightning.ShortChannelID OpeningFeeParams shared.OpeningFeeParams PaymentSizeMsat *uint64 Mode OpeningMode @@ -25,7 +25,7 @@ type BuyRegistration struct { Id uint64 LspId string PeerId string // TODO: Make peerId in the registration a byte array. - Scid basetypes.ShortChannelID + Scid lightning.ShortChannelID OpeningFeeParams shared.OpeningFeeParams PaymentSizeMsat *uint64 Mode OpeningMode @@ -59,7 +59,7 @@ var ErrNotFound = errors.New("not found") type Lsps2Store interface { RegisterBuy(ctx context.Context, req *RegisterBuy) error - GetBuyRegistration(ctx context.Context, scid basetypes.ShortChannelID) (*BuyRegistration, error) + GetBuyRegistration(ctx context.Context, scid lightning.ShortChannelID) (*BuyRegistration, error) SetChannelOpened(ctx context.Context, channelOpened *ChannelOpened) error SetCompleted(ctx context.Context, registrationId uint64) error } diff --git a/postgresql/intercept_store.go b/postgresql/intercept_store.go index 79b3a30..9ba0d6f 100644 --- a/postgresql/intercept_store.go +++ b/postgresql/intercept_store.go @@ -7,7 +7,7 @@ import ( "log" "time" - "github.com/breez/lspd/basetypes" + "github.com/breez/lspd/lightning" "github.com/breez/lspd/shared" "github.com/btcsuite/btcd/wire" "github.com/jackc/pgtype" @@ -45,7 +45,7 @@ func (s *PostgresInterceptStore) PaymentInfo(htlcPaymentHash []byte) (string, *s var cp *wire.OutPoint if fundingTxID != nil { - cp, err = basetypes.NewOutPoint(fundingTxID, uint32(fundingTxOutnum.Int)) + cp, err = lightning.NewOutPoint(fundingTxID, uint32(fundingTxOutnum.Int)) if err != nil { log.Printf("invalid funding txid in database %x", fundingTxID) } diff --git a/postgresql/lsps2_store.go b/postgresql/lsps2_store.go index 36ad35c..9536b40 100644 --- a/postgresql/lsps2_store.go +++ b/postgresql/lsps2_store.go @@ -5,7 +5,7 @@ import ( "fmt" "strings" - "github.com/breez/lspd/basetypes" + "github.com/breez/lspd/lightning" "github.com/breez/lspd/lsps2" "github.com/breez/lspd/shared" "github.com/btcsuite/btcd/wire" @@ -64,7 +64,7 @@ func (s *Lsps2Store) RegisterBuy( return nil } -func (s *Lsps2Store) GetBuyRegistration(ctx context.Context, scid basetypes.ShortChannelID) (*lsps2.BuyRegistration, error) { +func (s *Lsps2Store) GetBuyRegistration(ctx context.Context, scid lightning.ShortChannelID) (*lsps2.BuyRegistration, error) { row := s.pool.QueryRow( ctx, `SELECT r.id @@ -133,7 +133,7 @@ func (s *Lsps2Store) GetBuyRegistration(ctx context.Context, scid basetypes.Shor var cp *wire.OutPoint if db_funding_tx_id != nil { - cp, err = basetypes.NewOutPoint(db_funding_tx_id, db_funding_tx_outnum) + cp, err = lightning.NewOutPoint(db_funding_tx_id, db_funding_tx_outnum) if err != nil { return nil, fmt.Errorf("invalid funding tx id in db: %x", db_funding_tx_id) } @@ -143,7 +143,7 @@ func (s *Lsps2Store) GetBuyRegistration(ctx context.Context, scid basetypes.Shor Id: db_id, LspId: db_lsp_id, PeerId: db_peer_id, - Scid: basetypes.ShortChannelID(uint64(db_scid)), + Scid: lightning.ShortChannelID(uint64(db_scid)), OpeningFeeParams: shared.OpeningFeeParams{ MinFeeMsat: uint64(db_params_min_fee_msat), Proportional: db_params_proportional,