Skip to content

Commit

Permalink
insert htlc used for channel open
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed Feb 26, 2024
1 parent 04f2ee0 commit 5fe0877
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 2 deletions.
11 changes: 11 additions & 0 deletions history/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ 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
UpdateForwards(ctx context.Context, forwards []*Forward, nodeId []byte) error
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
}
23 changes: 22 additions & 1 deletion interceptor/intercept_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion lsps2/intercept_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions lsps2/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions postgresql/history_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions postgresql/migrations/000016_open_channel_htlc.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE public.open_channel_htlcs;
9 changes: 9 additions & 0 deletions postgresql/migrations/000016_open_channel_htlc.up.sql
Original file line number Diff line number Diff line change
@@ -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
);

0 comments on commit 5fe0877

Please sign in to comment.