Skip to content

Commit

Permalink
Protect index provider peer ID before connecting to full node
Browse files Browse the repository at this point in the history
Add the peer ID of index provider host to the list of protected peers
before connecting to full node. Otherwise, it is possible for the
connection to be reset by full node before we reach the line that adds
the ID to list of protected peers via JsonRPC API.

Relates to:
 - ipni/index-provider#177
  • Loading branch information
masih committed Feb 15, 2022
1 parent 1bf7e6a commit f15a1ed
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions markets/idxprov/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ type Libp2pMeshCreator struct {
}

func (mc Libp2pMeshCreator) Connect(ctx context.Context) error {

// Add the index provider's host ID to list of protected peers first, before any attempt to
// connect to full node.
idxProvID := mc.idxProvHost.ID()
if err := mc.fullnodeApi.NetProtectAdd(ctx, []peer.ID{idxProvID}); err != nil {
return fmt.Errorf("failed to call NetProtectAdd on the full node, err: %w", err)
}

faddrs, err := mc.fullnodeApi.NetAddrsListen(ctx)
if err != nil {
return fmt.Errorf("failed to fetch full node listen addrs, err: %w", err)
}

// otherwise, connect to the full node, ask it to protect the connection and protect the connection on our end too
// Connect to the full node, ask it to protect the connection and protect the connection on
// markets end too.
if err := mc.idxProvHost.Connect(ctx, faddrs); err != nil {
return fmt.Errorf("failed to connect index provider host with the full node: %w", err)
}
mc.idxProvHost.ConnManager().Protect(faddrs.ID, "index-provider-gossipsub")
if err := mc.fullnodeApi.NetProtectAdd(ctx, []peer.ID{mc.idxProvHost.ID()}); err != nil {
return fmt.Errorf("failed to call NetProtectAdd on the full node, err: %w", err)
}

log.Debugw("successfully connected to full node and asked it protect indexer provider peer conn", "fullNodeInfo", faddrs.String(),
"idxProviderPeerId", mc.idxProvHost.ID())
"idxProviderPeerId", idxProvID)

return nil
}
Expand Down

0 comments on commit f15a1ed

Please sign in to comment.