Skip to content

Commit

Permalink
FFISelect improvements (#70)
Browse files Browse the repository at this point in the history
* fixed it

* simpler
  • Loading branch information
snadrus authored Jun 26, 2024
1 parent 10b1884 commit 2b4ed70
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
16 changes: 16 additions & 0 deletions lib/ffiselect/ffi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ffiselect

import (
"context"
"testing"

"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"
)

func TestSelf(t *testing.T) {
testCID := cid.NewCidV1(1, []byte("test"))
cid, err := FFISelect.SelfTest(context.Background(), 12345678, testCID)
require.NoError(t, err)
require.Equal(t, testCID, cid)
}
42 changes: 18 additions & 24 deletions lib/ffiselect/ffiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"os/exec"
"reflect"
"strconv"
"strings"

Expand All @@ -19,6 +20,7 @@ import (
"github.com/filecoin-project/go-state-types/proof"

"github.com/filecoin-project/curio/build"
"github.com/filecoin-project/curio/lib/ffiselect/ffidirect"

"github.com/filecoin-project/lotus/storage/sealer/storiface"
)
Expand All @@ -42,28 +44,29 @@ func init() {
if err != nil {
panic(err)
}
if len(devices) == 0 {
ch = make(chan string, 1)
ch <- "0"
} else {
if len(devices) > 1 {
_, err := jsonrpc.NewCustomClient("FFI", []interface{}{&FFISelect}, call)
if err != nil {
panic(err)
}
ch = make(chan string, len(devices))
for i := 0; i < len(devices); i++ {
ch <- strconv.Itoa(i)
}
} else { // direct call for single device case.
ffiSelect := reflect.ValueOf(&FFISelect).Elem()
ffiDirectValue := reflect.ValueOf(ffidirect.FFI{})
ffiDirectType := ffiDirectValue.Type()

for i := 0; i < ffiDirectType.NumMethod(); i++ {
f := ffiSelect.FieldByName(ffiDirectType.Method(i).Name)
f.Set(reflect.MakeFunc(f.Type(), func(args []reflect.Value) []reflect.Value {
return ffiDirectValue.Method(i).Call(args[1:]) // avoid sending ctx
}))
}
}
}

type ValErr struct {
Val []interface{}
Err string
}

// This is not the one you're looking for.
type FFICall struct {
Fn string
Args []interface{}
}

func subStrInSet(set []string, sub string) bool {
return lo.Reduce(set, func(agg bool, item string, _ int) bool { return agg || strings.Contains(item, sub) }, false)
}
Expand Down Expand Up @@ -174,12 +177,3 @@ var FFISelect struct {

SelfTest func(ctx context.Context, val1 int, val2 cid.Cid) (cid.Cid, error)
}

// //////////////////////////

func init() {
_, err := jsonrpc.NewCustomClient("FFI", []interface{}{&FFISelect}, call)
if err != nil {
panic(err)
}
}

0 comments on commit 2b4ed70

Please sign in to comment.