Skip to content

Commit

Permalink
Merge pull request #141 from libsv/fix/more-host-pc
Browse files Browse the repository at this point in the history
Fix: Query subscription peer channel on notification, not own.
  • Loading branch information
roppa authored Mar 24, 2022
2 parents 5e255a7 + ce5c062 commit 9d0eb00
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions peerchannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@ type PeerChannelAPITokenStoreArgs struct {
// PeerChannelMessageArgs for quering a peer channel message.
type PeerChannelMessageArgs struct {
ChannelID string
Host string
Path string
Token string
}
2 changes: 2 additions & 0 deletions peerchannels_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type PeerChannelsMessageHandler interface {

// PeerChannelSubscription for subscribing to channel notifications.
type PeerChannelSubscription struct {
Host string
Path string
ChannelID string
Token string
ChannelType PeerChannelHandlerType
Expand Down
2 changes: 1 addition & 1 deletion service/pay.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (p *pay) Pay(ctx context.Context, req payd.PayRequest) (*dpp.PaymentACK, er
Path: ack.PeerChannel.Path,
Type: payd.PeerChannelHandlerTypeProof,
}); err != nil {
log.Err(err)
log.Error().Err(err)
}
if err := p.storeTx.Commit(ctx); err != nil {
return nil, errors.Wrap(err, "failed to commit tx")
Expand Down
4 changes: 2 additions & 2 deletions service/peerchannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func (p *peerChannelsSvc) PeerChannelsMessage(ctx context.Context, args *payd.Pe
spvchannels.WithToken(args.Token),
spvchannels.WithChannelID(args.ChannelID),
spvchannels.WithVersion("v1"),
spvchannels.WithBaseURL(p.cfg.Host),
spvchannels.WithPath(p.cfg.Path),
spvchannels.WithBaseURL(args.Host),
spvchannels.WithPath(args.Path),
spvchannels.WithNoTLS(),
)
msgs, err := c.Messages(ctx, spvchannels.MessagesRequest{
Expand Down
9 changes: 9 additions & 0 deletions service/peerchannels_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func (p *peerChannelsNotifySvc) Subscribe(ctx context.Context, channel *payd.Pee
channel.CreatedAt = time.Now()
}

log.Debug().Msgf("subscribing to channel %s with token %s at %s%s", channel.ID, channel.Token, channel.Host, channel.Path)

u := url.URL{
Scheme: "ws",
Host: channel.Host,
Expand Down Expand Up @@ -76,6 +78,8 @@ func (p *peerChannelsNotifySvc) Subscribe(ctx context.Context, channel *payd.Pee
}()

sub := payd.PeerChannelSubscription{
Host: channel.Host,
Path: channel.Path,
ChannelID: channel.ID,
ChannelType: channel.Type,
Token: channel.Token,
Expand Down Expand Up @@ -107,6 +111,7 @@ func (p *peerChannelsNotifySvc) listen(ctx context.Context, sub *payd.PeerChanne
log.Error().Err(errors.WithStack(err))
}

log.Debug().Msgf("message received on channel %s", sub.ChannelID)
in <- true
}()

Expand All @@ -124,13 +129,17 @@ func (p *peerChannelsNotifySvc) listen(ctx context.Context, sub *payd.PeerChanne
func (p *peerChannelsNotifySvc) handleNotification(ctx context.Context, sub *payd.PeerChannelSubscription, cancel context.CancelFunc) error {
msgs, err := p.pcSvc.PeerChannelsMessage(ctx, &payd.PeerChannelMessageArgs{
ChannelID: sub.ChannelID,
Host: sub.Host,
Path: sub.Path,
Token: sub.Token,
})
if err != nil {
log.Error().Err(errors.WithStack(err))
return err
}

log.Debug().Msgf("channel %s fetched messages: %#v", sub.ChannelID, msgs)

hdlr := p.handlers[sub.ChannelType]
finished, err := hdlr.HandlePeerChannelsMessage(ctx, msgs)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion service/proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (p *proofs) HandlePeerChannelsMessage(ctx context.Context, msgs spvchannels
}

if mm["callbackReason"].(string) != "merkleProof" {
log.Info().Msg("skipping msg")
log.Debug().Msgf("skipping msg %#v", msg)
continue
}

Expand Down

0 comments on commit 9d0eb00

Please sign in to comment.