在libp2p中,是否可以在重新启动后保持相同的ID?

huangapple go评论97阅读模式
英文:

Can I keep the same ID in libp2p across restarts?

问题

我正在编写一个测试应用程序。为此,如果我的libp2p节点在重新启动后能保持它们的ID不变,那将是理想的情况 - 否则,我需要考虑一些中间服务,用于在测试期间跟踪节点的NodeID以进行连接。

我可以做到这一点吗?

目前,如果我只是运行https://docs.libp2p.io/guides/getting-started/go/教程,每次重新启动后,它都会打印出不同的节点ID。

❯ go run ./main.go
Listen addresses: /ip4/127.0.0.1/tcp/9436/p2p/12D3KooWNK774CCm9b48H84qZu3mp2Jq4wvDBh9813omB712qqLW
^CReceived signal, shutting down...
❯ go run ./main.go
Listen addresses: /ip4/127.0.0.1/tcp/9436/p2p/12D3KooWGLgDLUaNiuruX5umaExyFQJQBsf3yyXgC9MPy7cGS99F
^CReceived signal, shutting down...
英文:

I am writing a test app. For this, it would be ideal if my libp2p nodes would keep their IDs across restarts - otherwise I need to think of some intermediate service which keeps track of NodeIDs for the nodes to connect during a test.

Can I do this?

Currently, if I just run the https://docs.libp2p.io/guides/getting-started/go/ tutorial, after each restart, it prints a different Node ID.

❯ go run ./main.go
Listen addresses: /ip4/127.0.0.1/tcp/9436/p2p/12D3KooWNK774CCm9b48H84qZu3mp2Jq4wvDBh9813omB712qqLW
^CReceived signal, shutting down...
❯ go run ./main.go
Listen addresses: /ip4/127.0.0.1/tcp/9436/p2p/12D3KooWGLgDLUaNiuruX5umaExyFQJQBsf3yyXgC9MPy7cGS99F
^CReceived signal, shutting down...

答案1

得分: -2

这条评论似乎提供了答案:https://github.com/libp2p/go-libp2p/issues/1040#issuecomment-767695679

引用其中的内容:

一个 libp2p 节点无法连接到没有其 peerId 的远程节点。

你可以通过使用 Identity 选项,配置一个 libp2p 节点使用持久化的密钥来派生与之前相同的 peerId。

libp2p.New(context.Background(), libp2p.Identity(//在这里填入密钥))

要获取主机的密钥并将其编组,可以使用:

sk := h.Peerstore().PrivKey(h.ID()) 	
bz, _ := crypto.MarshalPrivateKey(sk)
英文:

This comment seems to provide the answer: https://github.com/libp2p/go-libp2p/issues/1040#issuecomment-767695679

Quoting from there:

> A libp2p peer can't connect to a remote peer without it's peerId.
>
> You can configure a libp2p node to use a persisted secret key to
> derive the same peerId as before by using the Identity option.

libp2p.New(context.Background(), libp2p.Identity(//secret key here))

>
> To get a hosts's secret key and then marshal it, you can use:
>
>

sk := h.Peerstore().PrivKey(h.ID()) 	
bz, _ := crypto.MarshalPrivateKey(sk)

huangapple
  • 本文由 发表于 2023年7月25日 03:20:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76757457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定