Skip to content

Commit

Permalink
Minor refactor to vat-to-system interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed May 25, 2024
1 parent 444171d commit 9515c30
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
6 changes: 4 additions & 2 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions vat/vat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
78 changes: 75 additions & 3 deletions vat/vat_test.go
Original file line number Diff line number Diff line change
@@ -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")

}

0 comments on commit 9515c30

Please sign in to comment.