From 9515c30aabe096685199014eed6a5fee11460430 Mon Sep 17 00:00:00 2001 From: Louis Thibault Date: Sat, 25 May 2024 16:54:06 -0400 Subject: [PATCH] Minor refactor to vat-to-system interface. --- system/system.go | 6 ++-- system/system_test.go | 5 +-- vat/vat.go | 6 ++-- vat/vat_test.go | 78 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 85 insertions(+), 10 deletions(-) diff --git a/system/system.go b/system/system.go index ee11704..9ac9008 100644 --- a/system/system.go +++ b/system/system.go @@ -8,6 +8,7 @@ import ( "io" "log/slog" + "capnproto.org/go/capnp/v3" "capnproto.org/go/capnp/v3/exp/bufferpool" "github.com/tetratelabs/wazero" "github.com/tetratelabs/wazero/api" @@ -35,9 +36,10 @@ func (m *Module) SocketConfig(mod api.Module) SocketConfig { } } -func (m *Module) Bind(mod api.Module) Proc { +func (m *Module) Bind(mod api.Module) capnp.Client { socket := m.SocketConfig(mod).Build() - return Proc_ServerToClient(socket) + server := Proc_NewServer(socket) + return capnp.NewClient(server) } type Builder struct { diff --git a/system/system_test.go b/system/system_test.go index 22ea8c9..3649455 100644 --- a/system/system_test.go +++ b/system/system_test.go @@ -43,9 +43,10 @@ func TestSystemSocket(t *testing.T) { require.NoError(t, err) defer mod.Close(ctx) - proc := sys.Bind(mod) // bind the guest module to the system socket + client := sys.Bind(mod) // bind the guest module to the system socket + defer client.Release() - f, release := proc.Handle(ctx, func(h system.Proc_handle_Params) error { + f, release := system.Proc(client).Handle(ctx, func(h system.Proc_handle_Params) error { return h.SetEvent([]byte("Hello, Wetware!")) }) defer release() diff --git a/vat/vat.go b/vat/vat.go index eaa84d2..3335c12 100644 --- a/vat/vat.go +++ b/vat/vat.go @@ -66,10 +66,10 @@ func (n Network) BootstrapClient() capnp.Client { } func (n Network) Serve(ctx context.Context) error { - proc := n.System.Bind(n.Guest) - defer proc.Release() + c := n.BootstrapClient() + defer c.Release() - for c := capnp.Client(proc); ; { + for { if conn, err := n.Accept(ctx, &rpc.Options{ BootstrapClient: c.AddRef(), Network: n, diff --git a/vat/vat_test.go b/vat/vat_test.go index f11b37d..07a902a 100644 --- a/vat/vat_test.go +++ b/vat/vat_test.go @@ -1,8 +1,80 @@ package vat_test -import "testing" +import ( + "context" + "crypto/rand" + "os" + "testing" -func TestNetwork(t *testing.T) { + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" + "github.com/tetratelabs/wazero" + wasi "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" + "github.com/wetware/go/system" + test_libp2p "github.com/wetware/go/test/libp2p" + "github.com/wetware/go/vat" +) + +func TestNetConfig(t *testing.T) { t.Parallel() - t.Skip("TODO") + + t.Skip("XXX") + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig(). + WithCloseOnContextDone(true). + WithDebugInfoEnabled(false)) + defer r.Close(ctx) + + cl, err := wasi.Instantiate(ctx, r) + require.NoError(t, err) + defer cl.Close(ctx) + + sys, err := system.Builder{ + // Host: c.Host, + // IPFS: c.IPFS, + }.Instantiate(ctx, r) + require.NoError(t, err) + defer sys.Close(ctx) + + b, err := os.ReadFile("testdata/socket/main.wasm") + require.NoError(t, err) + + cm, err := r.CompileModule(ctx, b) + require.NoError(t, err) + defer cm.Close(ctx) + + ch := make(chan []byte, 1) + defer close(ch) + ctx = system.WithMailbox(ctx, ch) + + mod, err := r.InstantiateModule(ctx, cm, wazero.NewModuleConfig(). + // WithName(). + // WithArgs(). + // WithEnv(). + WithRandSource(rand.Reader). + // WithFS(). + // WithFSConfig(). + // WithStartFunctions(). // remove _start so that we can call it later + WithStdin(sys.Stdin()). + WithStdout(os.Stdout). // FIXME + WithStderr(os.Stderr). // FIXME + WithSysNanotime()) + require.NoError(t, err) + defer mod.Close(ctx) + + h := test_libp2p.NewMockHost(ctrl) + + net := vat.NetConfig{ + Host: h, + }.Build(ctx) + + require.NotZero(t, net.DialTimeout, + "should initialize dial_timeout parameter") + }