Skip to content

Instantly share code, notes, and snippets.

@magik6k
Created March 14, 2023 09:34
Show Gist options
  • Save magik6k/20cb25b7df056fdc3a7f649d8fd696a7 to your computer and use it in GitHub Desktop.
Save magik6k/20cb25b7df056fdc3a7f649d8fd696a7 to your computer and use it in GitHub Desktop.
diff --git a/go.mod b/go.mod
index 405284f49..b39292b02 100644
--- a/go.mod
+++ b/go.mod
@@ -135,6 +135,7 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
+ github.com/filecoin-project/go-fil-commp-hashhash v0.1.0 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-kit/log v0.2.0 // indirect
diff --git a/go.sum b/go.sum
index ef0441f6c..067c048c6 100644
--- a/go.sum
+++ b/go.sum
@@ -230,6 +230,8 @@ github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL
github.com/felixge/httpsnoop v1.0.2/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
+github.com/filecoin-project/go-fil-commp-hashhash v0.1.0 h1:imrrpZWEHRnNqqv0tN7LXep5bFEVOVmQWHJvl2mgsGo=
+github.com/filecoin-project/go-fil-commp-hashhash v0.1.0/go.mod h1:73S8WSEWh9vr0fDJVnKADhfIv/d6dCbAGaAGWbdJEI8=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/flynn/noise v0.0.0-20180327030543-2492fe189ae6/go.mod h1:1i71OnUq3iUe1ma7Lr6yG6/rjvM3emb6yoL7xLFzcVQ=
github.com/flynn/noise v1.0.0 h1:DlTHqmzmvcEiKj+4RYo/imoswx/4r6iBlCMfVtrMXpQ=
diff --git a/plugin/loader/preload.go b/plugin/loader/preload.go
index 430486211..70928dc0e 100644
--- a/plugin/loader/preload.go
+++ b/plugin/loader/preload.go
@@ -8,6 +8,7 @@ import (
pluginipldgit "github.com/ipfs/kubo/plugin/plugins/git"
pluginlevelds "github.com/ipfs/kubo/plugin/plugins/levelds"
pluginpeerlog "github.com/ipfs/kubo/plugin/plugins/peerlog"
+ pluginribsbs "github.com/ipfs/kubo/plugin/plugins/ribsbs"
)
// DO NOT EDIT THIS FILE
@@ -22,4 +23,5 @@ func init() {
Preload(pluginlevelds.Plugins...)
Preload(pluginpeerlog.Plugins...)
Preload(pluginfxtest.Plugins...)
+ Preload(pluginribsbs.Plugins...)
}
diff --git a/plugin/plugins/ribsbs/kuboribs.go b/plugin/plugins/ribsbs/kuboribs.go
new file mode 100644
index 000000000..08a209515
--- /dev/null
+++ b/plugin/plugins/ribsbs/kuboribs.go
@@ -0,0 +1,137 @@
+package kuboribs
+
+import (
+ "context"
+ "fmt"
+ blockstore "github.com/ipfs/go-ipfs-blockstore"
+ logging "github.com/ipfs/go-log"
+ "github.com/ipfs/kubo/core"
+ "github.com/ipfs/kubo/core/node"
+ "github.com/ipfs/kubo/plugin"
+ "github.com/libp2p/go-libp2p"
+ "github.com/libp2p/go-libp2p/core/host"
+ "github.com/lotus-web3/ribs"
+ "github.com/lotus-web3/ribs/impl"
+ ribsbstore "github.com/lotus-web3/ribs/integrations/blockstore"
+ "github.com/lotus-web3/ribs/integrations/web"
+ "github.com/mitchellh/go-homedir"
+ "go.uber.org/fx"
+ "golang.org/x/xerrors"
+ "os"
+)
+
+var log = logging.Logger("ribsplugin")
+
+var Plugins = []plugin.Plugin{
+ &ribsPlugin{},
+}
+
+// ribsPlugin is used for testing the fx plugin.
+// It merely adds an fx option that logs a debug statement, so we can verify that it works in tests.
+type ribsPlugin struct{}
+
+var _ plugin.PluginFx = (*ribsPlugin)(nil)
+
+func (p *ribsPlugin) Name() string {
+ return "ribs-bs"
+}
+
+func (p *ribsPlugin) Version() string {
+ return "0.0.0"
+}
+
+func (p *ribsPlugin) Init(env *plugin.Environment) error {
+ return nil
+}
+
+func (p *ribsPlugin) Options(info core.FXNodeInfo) ([]fx.Option, error) {
+ fmt.Println("RIBS OPTIONS PLUGIN")
+
+ opts := info.FXOptions
+ opts = append(opts,
+ fx.Provide(makeRibs),
+ fx.Provide(ribsBlockstores),
+
+ fx.Decorate(func(rbs *ribsbstore.Blockstore) node.BaseBlocks {
+ return rbs
+ }),
+ )
+ return opts, nil
+}
+
+// node.BaseBlocks, blockstore.Blockstore, blockstore.GCLocker, blockstore.GCBlockstore
+
+type ribsIn struct {
+ fx.In
+
+ H host.Host `optional:"true"`
+}
+
+var (
+ defaultDataDir = "~/.ribsdata"
+ dataEnv = "RIBS_DATA"
+)
+
+func makeRibs(ri ribsIn) (ribs.RIBS, error) {
+ var opts []impl.OpenOption
+ if ri.H != nil {
+ opts = append(opts, impl.WithHostGetter(func(...libp2p.Option) (host.Host, error) {
+ return ri.H, nil
+ }))
+ }
+
+ dataDir := os.Getenv(dataEnv)
+ if dataDir == "" {
+ dataDir = defaultDataDir
+ }
+ dataDir, err := homedir.Expand(dataDir)
+ if err != nil {
+ return nil, xerrors.Errorf("expand data dir: %w", err)
+ }
+
+ r, err := impl.Open(dataDir, opts...)
+ if err != nil {
+ return nil, xerrors.Errorf("open ribs: %w", err)
+ }
+ go func() {
+ if err := web.Serve(":9010", r); err != nil {
+ panic("ribsweb serve failed")
+ }
+ }()
+
+ _, _ = fmt.Fprintf(os.Stderr, "RIBSWeb at http://%s\n", "127.0.0.1:9010")
+
+ return r, nil
+}
+
+func ribsBlockstores(r ribs.RIBS, lc fx.Lifecycle) *ribsbstore.Blockstore {
+ rbs := ribsbstore.New(context.TODO(), r)
+
+ lc.Append(fx.Hook{
+ OnStop: func(ctx context.Context) error {
+ return rbs.Close()
+ },
+ })
+
+ return rbs
+}
+
+type dummyGCLocker struct{}
+
+func (d *dummyGCLocker) Unlock(ctx context.Context) {
+ return
+}
+
+func (d *dummyGCLocker) GCLock(ctx context.Context) blockstore.Unlocker {
+ panic("no gc")
+}
+
+func (d *dummyGCLocker) PinLock(ctx context.Context) blockstore.Unlocker {
+ return d
+}
+
+func (d *dummyGCLocker) GCRequested(ctx context.Context) bool {
+ return false
+}
+
+var _ blockstore.GCLocker = (*dummyGCLocker)(nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment