Skip to content

Commit

Permalink
Simplify, simplify!
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Jan 10, 2024
1 parent d55d605 commit 0f88fae
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 81 deletions.
7 changes: 6 additions & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"math/big"
"os"
"runtime"
"sync"

Expand Down Expand Up @@ -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()
Expand Down
51 changes: 0 additions & 51 deletions suave/datastore/ipfs_test.go

This file was deleted.

14 changes: 7 additions & 7 deletions suave/datastore/ipfs.go → suave/offchain/blockstore.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package datastore
package offchain

import (
"context"
Expand All @@ -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 {
Expand Down
46 changes: 46 additions & 0 deletions suave/offchain/blockstore_test.go
Original file line number Diff line number Diff line change
@@ -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")
}()

}
31 changes: 9 additions & 22 deletions suave/offchain/offchain.go
Original file line number Diff line number Diff line change
@@ -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(),
}
}

0 comments on commit 0f88fae

Please sign in to comment.