From f15a1edbf274fa90986cfffc2305a6e9c6bfb3cc Mon Sep 17 00:00:00 2001 From: "Masih H. Derkani" Date: Tue, 15 Feb 2022 11:32:02 +0000 Subject: [PATCH] Protect index provider peer ID before connecting to full node 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: - https://github.com/filecoin-project/index-provider/issues/177 --- markets/idxprov/mesh.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/markets/idxprov/mesh.go b/markets/idxprov/mesh.go index 5b580c7fd..097513d42 100644 --- a/markets/idxprov/mesh.go +++ b/markets/idxprov/mesh.go @@ -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 }