From 0f88fae5bb639c975575423eadee309f8dd454ee Mon Sep 17 00:00:00 2001 From: Louis Thibault Date: Wed, 10 Jan 2024 16:37:33 -0500 Subject: [PATCH] Simplify, simplify! --- eth/backend.go | 7 ++- suave/datastore/ipfs_test.go | 51 ------------------- .../ipfs.go => offchain/blockstore.go} | 14 ++--- suave/offchain/blockstore_test.go | 46 +++++++++++++++++ suave/offchain/offchain.go | 31 ++++------- 5 files changed, 68 insertions(+), 81 deletions(-) delete mode 100644 suave/datastore/ipfs_test.go rename suave/{datastore/ipfs.go => offchain/blockstore.go} (60%) create mode 100644 suave/offchain/blockstore_test.go diff --git a/eth/backend.go b/eth/backend.go index 39ff10b17..d76c8cd08 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "math/big" + "os" "runtime" "sync" @@ -319,7 +320,11 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { stack.RegisterProtocols(eth.Protocols()) stack.RegisterLifecycle(eth) stack.RegisterLifecycle(confidentialStoreEngine) - stack.RegisterLifecycle(&offchain.Env{}) + + // TODO(lthibault): replace this with a proper CLI flag. + if os.Getenv("SUAVE_EXPERIMENTAL_IPFS") != "" { + stack.RegisterLifecycle(&offchain.Env{}) + } // Successful startup; push a marker and check previous unclean shutdowns. eth.shutdownTracker.MarkStartup() diff --git a/suave/datastore/ipfs_test.go b/suave/datastore/ipfs_test.go deleted file mode 100644 index 49674b219..000000000 --- a/suave/datastore/ipfs_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package datastore_test - -import ( - "testing" -) - -func TestIPFS(t *testing.T) { - // t.Parallel() - - // ipfs := datastore.IPFS{} - // data := []byte("hello world") - - // require.Equal(t, "http://localhost:5001/api/v0", ipfs.String()) - - // t.Run("Put", func(t *testing.T) { - // req, err := ipfs.Put(context.TODO(), bytes.NewReader(data)) - // require.NoError(t, err) - // require.NotNil(t, req) - - // t.Log(req.URL.String()) - - // res, err := http.DefaultClient.Do(req) - // bb, _ := io.ReadAll(res.Body) - // t.Log(string(bb)) - - // require.NoError(t, err) - // require.Equal(t, http.StatusOK, res.StatusCode) - - // b, err := io.ReadAll(res.Body) - // require.NoError(t, err) - // _, cid, err := cid.CidFromBytes(b) - // require.NoError(t, err) - // require.Equal(t, "QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o", cid.String()) - // }) - - // t.Run("Get", func(t *testing.T) { - // cid := cid.MustParse("QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o") - - // req, err := ipfs.Get(context.TODO(), cid) - // require.NoError(t, err) - // require.NotNil(t, req) - - // res, err := http.DefaultClient.Do(req) - // require.NoError(t, err) - - // b, err := io.ReadAll(res.Body) - // require.NoError(t, err) - // t.Log(string(b)) - // t.Fail() - // }) -} diff --git a/suave/datastore/ipfs.go b/suave/offchain/blockstore.go similarity index 60% rename from suave/datastore/ipfs.go rename to suave/offchain/blockstore.go index 12a501996..a035db556 100644 --- a/suave/datastore/ipfs.go +++ b/suave/offchain/blockstore.go @@ -1,4 +1,4 @@ -package datastore +package offchain import ( "context" @@ -11,17 +11,17 @@ import ( "github.com/multiformats/go-multihash" ) -type IPFS struct { - API iface.CoreAPI +type Blockstore struct { + API iface.BlockAPI } -func (c *IPFS) Get(ctx context.Context, cid cid.Cid) (io.Reader, error) { +func (b Blockstore) Get(ctx context.Context, cid cid.Cid) (io.Reader, error) { p := path.FromCid(cid) - return c.API.Block().Get(ctx, p) + return b.API.Get(ctx, p) } -func (c *IPFS) Put(ctx context.Context, r io.Reader) (cid.Cid, error) { - bs, err := c.API.Block().Put(ctx, r, +func (b Blockstore) Put(ctx context.Context, r io.Reader) (cid.Cid, error) { + bs, err := b.API.Put(ctx, r, // options.Block.Pin(false), // TODO: refcounting options.Block.Hash(multihash.BLAKE3, 512)) if err != nil { diff --git a/suave/offchain/blockstore_test.go b/suave/offchain/blockstore_test.go new file mode 100644 index 000000000..b3c6e2d35 --- /dev/null +++ b/suave/offchain/blockstore_test.go @@ -0,0 +1,46 @@ +package offchain_test + +import ( + "log" + "os" + "os/exec" + "testing" + + "github.com/ethereum/go-ethereum/suave/offchain" + iface "github.com/ipfs/kubo/core/coreiface" + "github.com/stretchr/testify/require" +) + +var api iface.CoreAPI + +func TestMain(m *testing.M) { + // Check if IPFS is available in the environment before attempting + // to run the integration tests. + cmd := exec.Command("which", "ipfs") + switch err := cmd.Run().(type) { + case *exec.ExitError: + if status := err.ExitCode(); status > 0 { + log.Println("ipfs not found in $PATH. Skipping...") + } else { + os.Exit(status) // abort; we still don't know if IPFS is available + } + + case error: + log.Fatal(err) + } + + os.Exit(m.Run()) +} + +func TestBlockstore(t *testing.T) { + t.Parallel() + + env := offchain.Env{ + IPFS: api, + } + require.NoError(t, env.Start(), "failed to bind offchain environment") + defer func() { + require.NoError(t, env.Stop(), "failed to release offchain environment") + }() + +} diff --git a/suave/offchain/offchain.go b/suave/offchain/offchain.go index ff0a5d365..443a5b710 100644 --- a/suave/offchain/offchain.go +++ b/suave/offchain/offchain.go @@ -1,39 +1,26 @@ package offchain import ( - "context" - "io" - - "github.com/ethereum/go-ethereum/suave/datastore" - "github.com/ipfs/go-cid" "github.com/ipfs/kubo/client/rpc" iface "github.com/ipfs/kubo/core/coreiface" ) -type Datastore interface { - Get(context.Context, cid.Cid) (io.Reader, error) - Put(context.Context, io.Reader) (cid.Cid, error) -} - type Env struct { - Core iface.CoreAPI - Store Datastore + IPFS iface.CoreAPI } func (env *Env) Start() (err error) { - if env.Store == nil { - // Import IPFS into the off-chain environment - if env.Core, err = rpc.NewLocalApi(); err == nil { - env.Store = &datastore.IPFS{ - API: env.Core, - } - } - - } - + // Bind IPFS into the off-chain environment + env.IPFS, err = rpc.NewLocalApi() return } func (env *Env) Stop() error { return nil } + +func (env *Env) Blockstore() Blockstore { + return Blockstore{ + API: env.IPFS.Block(), + } +}