Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

circuitRelayTransport repeatedly reconnecting to relay #1635

Closed
ckousik opened this issue Mar 21, 2023 · 0 comments · Fixed by #1636
Closed

circuitRelayTransport repeatedly reconnecting to relay #1635

ckousik opened this issue Mar 21, 2023 · 0 comments · Fixed by #1636
Labels
need/triage Needs initial labeling and prioritization

Comments

@ckousik
Copy link
Contributor

ckousik commented Mar 21, 2023

  • Version: 0.43.0
  • Platform: Linux archlinux 5.15.59-2-lts
  • Subsystem: Relay

Severity:

Description:

  • Connected a libp2p node to a Go libp2p node serving circuit v2 relay.
  • A relay slot was repeatedly being reserved on the Go relay (looked like an infinite loop).
  • It should only reserve the slot once and wait for a timeout to trigger.

Steps to reproduce the error:

Instantiate the go relay with GOLOG_LOG_LEVEL=debug go run main.go. It should print out a websocket multiaddr.

package main

import (
	"crypto/rand"
	"fmt"

	"github.com/libp2p/go-libp2p"
	"github.com/libp2p/go-libp2p/core/crypto"
	"github.com/libp2p/go-libp2p/core/host"

	// "github.com/libp2p/go-libp2p/core/peer"
	"github.com/libp2p/go-libp2p/p2p/muxer/mplex"
	relay "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
	ma "github.com/multiformats/go-multiaddr"
)

func main() {
	makeRelayV2()
	select {}
}

func makeRelayV2() host.Host {
	r := rand.Reader
	// Generate a key pair for this host. We will use it at least
	// to obtain a valid host ID.
	priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
	if err != nil {
		panic(err)
	}

	opts := []libp2p.Option{
		libp2p.DefaultTransports,
		libp2p.ListenAddrStrings(
			"/ip4/0.0.0.0/tcp/4003/ws",
		),
		libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
		libp2p.Identity(priv),
		libp2p.EnableRelay(),
	}

	host, err := libp2p.New(opts...)
	if err != nil {
		panic(err)
	}

	_, err = relay.New(host)
	if err != nil {
		panic(err)
	}

	// fmt.Println(host.Mux().Protocols())

	for _, addr := range host.Addrs() {
		a, err := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", host.ID().Pretty()))
		if err != nil {
			panic(err)
		}
		fmt.Println("p2p addr: ", addr.Encapsulate(a))
	}
	return host
}

Instantiate the node process with DEBUG=libp2p:circuit-relay:* node index.js

import { noise } from "@chainsafe/libp2p-noise";
import { mplex } from "@libp2p/mplex";
import { webSockets } from "@libp2p/websockets";
import * as filters from  "@libp2p/websockets/filters";
import { createLibp2p } from "libp2p";
import { circuitRelayTransport } from "libp2p/circuit-relay";
import { multiaddr } from "@multiformats/multiaddr";

const node = await createLibp2p({
    transports: [
        webSockets({
            filters: filters.all,
        }),
        circuitRelayTransport({
            discoverRelays: 1,
        }),
    ],
    connectionEncryption: [noise()],
    streamMuxers: [mplex()],
});


await node.dial(multiaddr(process.argv[2]))

The node and go processes should keep logging a new circuit relay reservation.

@ckousik ckousik added the need/triage Needs initial labeling and prioritization label Mar 21, 2023
ckousik added a commit that referenced this issue Mar 21, 2023
This fixes #1635 which causes the circuit relay to repeatedly connect
to a discovered relay. If the relay returns connection expiration in
seconds, we get a negative ttl when calculating
`expiration - new Date().getTime()`.
This caused the `addRelay` function to set the timeout at 0ms. This timeout
instantly triggers and calls `addRelay` recursively.
achingbrain added a commit that referenced this issue Mar 21, 2023
Spec update: libp2p/specs#531

Fixes #1635 which causes the circuit relay to repeatedly connect
to a discovered relay. If the relay returns connection expiration in
seconds, we get a negative ttl when calculating 
`expiration - new Date().getTime()`.

This caused the `addRelay` function to set the timeout at 0ms which
triggers instantly.

---------

Co-authored-by: achingbrain <alex@achingbrain.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need/triage Needs initial labeling and prioritization
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant