From 5fe08773f781a42b7b70f658dfaa09dadb62dce9 Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Mon, 19 Feb 2024 15:05:03 +0100 Subject: [PATCH] insert htlc used for channel open --- history/store.go | 11 ++++++++ interceptor/intercept_handler.go | 23 +++++++++++++++- lsps2/intercept_handler.go | 16 +++++++++++- lsps2/mocks.go | 3 +++ postgresql/history_store.go | 26 +++++++++++++++++++ .../000016_open_channel_htlc.down.sql | 1 + .../000016_open_channel_htlc.up.sql | 9 +++++++ 7 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 postgresql/migrations/000016_open_channel_htlc.down.sql create mode 100644 postgresql/migrations/000016_open_channel_htlc.up.sql diff --git a/history/store.go b/history/store.go index 2238e62..4cdd2d1 100644 --- a/history/store.go +++ b/history/store.go @@ -26,6 +26,16 @@ type Forward struct { ResolvedTime time.Time } +type OpenChannelHtlc struct { + NodeId []byte + PeerId []byte + ChannelPoint *wire.OutPoint + OriginalAmountMsat uint64 + ForwardAmountMsat uint64 + IncomingAmountMsat uint64 + ForwardTime time.Time +} + type Store interface { UpdateChannels(ctx context.Context, updates []*ChannelUpdate) error InsertForwards(ctx context.Context, forwards []*Forward, nodeId []byte) error @@ -33,4 +43,5 @@ type Store interface { FetchClnForwardOffsets(ctx context.Context, nodeId []byte) (uint64, uint64, error) SetClnForwardOffsets(ctx context.Context, nodeId []byte, created uint64, updated uint64) error FetchLndForwardOffset(ctx context.Context, nodeId []byte) (*time.Time, error) + AddOpenChannelHtlc(ctx context.Context, htlc *OpenChannelHtlc) error } diff --git a/interceptor/intercept_handler.go b/interceptor/intercept_handler.go index c1fe639..df53851 100644 --- a/interceptor/intercept_handler.go +++ b/interceptor/intercept_handler.go @@ -255,7 +255,28 @@ func (i *Interceptor) Intercept(req common.InterceptRequest) common.InterceptRes }}) if err != nil { // Don't break here, this is not critical. - log.Printf("paymentHash: %s, failed to insert newly opened channel in history store. %v", reqPaymentHashStr, channelPoint.String()) + log.Printf("paymentHash: %s, failed to insert newly opened channel in history store. %v: %v", reqPaymentHashStr, channelPoint.String(), err) + } + + err = i.historyStore.AddOpenChannelHtlc(context.TODO(), &history.OpenChannelHtlc{ + NodeId: i.nodeid, + PeerId: destination, + ChannelPoint: channelPoint, + OriginalAmountMsat: req.OutgoingAmountMsat, + ForwardAmountMsat: uint64(amt), + IncomingAmountMsat: req.IncomingAmountMsat, + ForwardTime: time.Now(), + }) + if err != nil { + // Don't break here, this is not critical. + log.Printf( + "paymentHash: %s, failed to insert htlc used for channel open in history store: channel: %v,"+ + " original amount: %v, forward amount: %v", + reqPaymentHashStr, + channelPoint.String(), + req.OutgoingAmountMsat, + amt, + ) } useLegacyOnionBlob := slices.Contains(i.config.LegacyOnionTokens, token) diff --git a/lsps2/intercept_handler.go b/lsps2/intercept_handler.go index 4ae89fb..14aa4c1 100644 --- a/lsps2/intercept_handler.go +++ b/lsps2/intercept_handler.go @@ -625,8 +625,22 @@ func (i *Interceptor) handlePaymentChanOpened(event *paymentChanOpenedEvent) { resolution.part.resolution <- resolution.resolution } + now := time.Now() payment.registration.IsComplete = true - go i.store.SetCompleted(context.TODO(), payment.registration.Id) + go func() { + i.store.SetCompleted(context.TODO(), payment.registration.Id) + for _, resolution := range resolutions { + i.historyStore.AddOpenChannelHtlc(context.TODO(), &history.OpenChannelHtlc{ + NodeId: i.config.NodeId, + PeerId: destination, + ChannelPoint: event.channelPoint, + OriginalAmountMsat: resolution.part.req.OutgoingAmountMsat, + ForwardAmountMsat: resolution.resolution.AmountMsat, + IncomingAmountMsat: resolution.part.req.IncomingAmountMsat, + ForwardTime: now, + }) + } + }() delete(i.inflightPayments, event.paymentId) } diff --git a/lsps2/mocks.go b/lsps2/mocks.go index 53b79ce..0736a32 100644 --- a/lsps2/mocks.go +++ b/lsps2/mocks.go @@ -117,6 +117,9 @@ func (s *mockHistoryStore) SetClnForwardOffsets(ctx context.Context, nodeId []by func (s *mockHistoryStore) FetchLndForwardOffset(ctx context.Context, nodeId []byte) (*time.Time, error) { return nil, ErrNotImplemented } +func (s *mockHistoryStore) AddOpenChannelHtlc(ctx context.Context, htlc *history.OpenChannelHtlc) error { + return nil +} type mockLightningClient struct { openResponses []*wire.OutPoint diff --git a/postgresql/history_store.go b/postgresql/history_store.go index 675656d..b39cbfe 100644 --- a/postgresql/history_store.go +++ b/postgresql/history_store.go @@ -345,3 +345,29 @@ func (s *HistoryStore) SetClnForwardOffsets( ) return err } + +func (s *HistoryStore) AddOpenChannelHtlc(ctx context.Context, htlc *history.OpenChannelHtlc) error { + // TODO: Find an identifier equal to the forwarding_history identifier. + _, err := s.pool.Exec(ctx, ` + INSERT INTO open_channel_htlcs ( + nodeid + , peerid + , funding_tx_id + , funding_tx_outnum + , forward_amt_msat + , original_amt_msat + , incoming_amt_msat + , forward_time + ) VALUES ($1, $2, $3, $4, $5, $6, $7) + `, + htlc.NodeId, + htlc.PeerId, + htlc.ChannelPoint.Hash[:], + htlc.ChannelPoint.Index, + int64(htlc.ForwardAmountMsat), + int64(htlc.OriginalAmountMsat), + int64(htlc.IncomingAmountMsat), + htlc.ForwardTime.UnixNano(), + ) + return err +} diff --git a/postgresql/migrations/000016_open_channel_htlc.down.sql b/postgresql/migrations/000016_open_channel_htlc.down.sql new file mode 100644 index 0000000..51886b6 --- /dev/null +++ b/postgresql/migrations/000016_open_channel_htlc.down.sql @@ -0,0 +1 @@ +DROP TABLE public.open_channel_htlcs; \ No newline at end of file diff --git a/postgresql/migrations/000016_open_channel_htlc.up.sql b/postgresql/migrations/000016_open_channel_htlc.up.sql new file mode 100644 index 0000000..a5ba4aa --- /dev/null +++ b/postgresql/migrations/000016_open_channel_htlc.up.sql @@ -0,0 +1,9 @@ +CREATE TABLE public.open_channel_htlcs ( + nodeid bytea NOT NULL, + peerid bytea NOT NULL, + funding_tx_id bytea NOT NULL, + funding_tx_outnum bigint NOT NULL, + forward_amt_msat bigint NOT NULL, + original_amt_msat bigint NOT NULL, + forward_time bigint NOT NULL +); \ No newline at end of file