Kubo,来自IpfsNode的无效内存地址或空指针解引用。

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

Kubo, invalid memory address or nil pointer dereference from IpfsNode.Bootstrap

问题

我从IpfsNode.Bootstrap中得到了一个无效的内存地址或空指针解引用错误。我猜测在引导循环中发生了一些恐慌,但是我在这里做错了什么吗?还是有一个错误?我正在使用kubo v0.20.0版本。以下是我的代码:

package main

import (
	"context"
	"fmt"

	"github.com/ipfs/kubo/config"
	"github.com/ipfs/kubo/core"
	"github.com/ipfs/kubo/core/bootstrap"
)

func main() {
	ctx := context.Background()
	conf := core.BuildCfg{}
	node, err := core.NewNode(ctx, &conf)
	if err != nil {
		panic(err)
	}
	peers, err := config.DefaultBootstrapPeers()
	if err != nil {
		panic(err)
	}
	bs_conf := bootstrap.BootstrapConfigWithPeers(peers)
	err = node.Bootstrap(bs_conf) // 这里出现了空指针解引用
	fmt.Println("这行不会打印")
	if err != nil {
		panic(err)
	}
}

完整的错误信息如下:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0xee49eb]

goroutine 69 [running]:
github.com/ipfs/kubo/core/bootstrap.bootstrapRound({0x18bf1d8?, 0xc0000b05f0?}, {0x0, 0x0}, {0x0?, 0x0?, 0x0?, 0xc0006240c0?})
        /home/axel/go/pkg/mod/github.com/ipfs/kubo@v0.20.0/core/bootstrap/bootstrap.go:118 +0xab
github.com/ipfs/kubo/core/bootstrap.Bootstrap.func1({0x18cbd58?, 0xc0006226c0?})
        /home/axel/go/pkg/mod/github.com/ipfs/kubo@v0.20.0/core/bootstrap/bootstrap.go:89 +0xdc
github.com/jbenet/goprocess.(*process).Go.func1()
        /home/axel/go/pkg/mod/github.com/jbenet/goprocess@v0.1.4/impl-mutex.go:134 +0x36
created by github.com/jbenet/goprocess.(*process).Go
        /home/axel/go/pkg/mod/github.com/jbenet/goprocess@v0.1.4/impl-mutex.go:133 +0x238
exit status 2
英文:

I’m getting an invalid memory address or nil pointer dereference from IpfsNode.Bootstrap
I assume that there is some panic in bootstrap Round, but am I doing something wrong here?
Or is there a bug?
I’m using kubo v0.20.0
here is my code:

package main

import (
	"context"
	"fmt"

	"github.com/ipfs/kubo/config"
	"github.com/ipfs/kubo/core"
	"github.com/ipfs/kubo/core/bootstrap"
)

func main() {
	ctx := context.Background()
	conf := core.BuildCfg{}
	node, err := core.NewNode(ctx, &conf)
	if err != nil {
		panic(err)
	}
	peers, err := config.DefaultBootstrapPeers()
	if err != nil {
		panic(err)
	}
	bs_conf := bootstrap.BootstrapConfigWithPeers(peers)
	err = node.Bootstrap(bs_conf) // this gets nil pointer dereference
	fmt.Println("THIS LINE WILL NOT PRINT")
	if err != nil {
		panic(err)
	}
}

The full error is:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0xee49eb]

goroutine 69 [running]:
github.com/ipfs/kubo/core/bootstrap.bootstrapRound({0x18bf1d8?, 0xc0000b05f0?}, {0x0, 0x0}, {0x0?, 0x0?, 0x0?, 0xc0006240c0?})
        /home/axel/go/pkg/mod/github.com/ipfs/kubo@v0.20.0/core/bootstrap/bootstrap.go:118 +0xab
github.com/ipfs/kubo/core/bootstrap.Bootstrap.func1({0x18cbd58?, 0xc0006226c0?})
        /home/axel/go/pkg/mod/github.com/ipfs/kubo@v0.20.0/core/bootstrap/bootstrap.go:89 +0xdc
github.com/jbenet/goprocess.(*process).Go.func1()
        /home/axel/go/pkg/mod/github.com/jbenet/goprocess@v0.1.4/impl-mutex.go:134 +0x36
created by github.com/jbenet/goprocess.(*process).Go
        /home/axel/go/pkg/mod/github.com/jbenet/goprocess@v0.1.4/impl-mutex.go:133 +0x238
exit status 2

答案1

得分: 0

我能够通过GitHub上的一个贡献者获得一些帮助,以下是他的回答:

你的问题似乎是你使用了conf := core.BuildCfg{},这将将BuildCfg.Offline设置为false,这意味着你将没有一个实例化的libp2p节点来引导,这会导致空引用异常,因为你试图使用一个不存在的libp2p网络主机进行引导。

如果你将Offline设置为true,它就不会再出现panic,因为主机会启动。

话虽如此,我不确定你具体想做什么,也许使用kubo作为一个库已经足够完成你的工作了。然而,如果你计划在Go中构建一个自定义的IPFS应用程序,你可能更适合使用类似于GitHub - ipfs/boxo的东西:一个用于构建IPFS应用程序和实现的参考库。

最近有一篇文档描述了开发人员在决定如何从Kubo转移到类似但更可配置的东西时可能做出的一些权衡,但是你可以在kubo/customizing.md at master · ipfs/kubo · GitHub中找到更多信息。

来自https://github.com/aschmahmann

英文:

I was able to get some help by a contributor on GitHub, here is his answer:

Your issue seems to be that you use conf := core.BuildCfg{} which will set BulidCfg.Offline to false which means that you will not have a libp2p node instantiated with which to bootstrap which then gives you a null reference exception since you’re trying to bootstrap using a non-existent libp2p networking host.

If you set Offline to true it won’t panic anymore since the host will start.

That being said I’m not sure exactly what you’re trying to do and maybe using kubo as a library does enough of the job for you. However, you might be better suited to using something like GitHub - ipfs/boxo: A reference library for building IPFS applications and implementations. if you’re planning on building a custom IPFS application in Go.

There’s a relatively recent doc describing some of the tradeoffs a developer might make in deciding how to move from Kubo to something similar, but more configurable in kubo/customizing.md at master · ipfs/kubo · GitHub.

by https://github.com/aschmahmann

huangapple
  • 本文由 发表于 2023年5月10日 21:10:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218845.html
匿名

发表评论

匿名网友

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

确定