diff --git a/test/cosidela_test.go b/test/cosidela_test.go index d45d0928..9279638c 100644 --- a/test/cosidela_test.go +++ b/test/cosidela_test.go @@ -5,6 +5,9 @@ import ( "crypto/ecdsa" "crypto/elliptic" "crypto/x509" + ma "github.com/multiformats/go-multiaddr" + "go.dedis.ch/dela/mino/minows" + "go.dedis.ch/dela/mino/minows/key" "io" "math/rand" "net/url" @@ -46,6 +49,9 @@ import ( "golang.org/x/xerrors" ) +const minoGRPC = "grpc" +const minoWS = "ws" + const certKeyName = "cert.key" const privateKeyFile = "private.key" @@ -74,7 +80,7 @@ type cosiDelaNode struct { tree hashtree.Tree } -func newDelaNode(t require.TestingT, path string, port int) dela { +func newDelaNode(t require.TestingT, path string, port int, kind string) dela { err := os.MkdirAll(path, 0700) require.NoError(t, err) @@ -83,31 +89,43 @@ func newDelaNode(t require.TestingT, path string, port int) dela { require.NoError(t, err) // mino - router := tree.NewRouter(minogrpc.NewAddressFactory()) - addr := minogrpc.ParseAddress("127.0.0.1", uint16(port)) - - certs := certs.NewDiskStore(db, session.AddressFactory{}) - - fload := loader.NewFileLoader(filepath.Join(path, certKeyName)) - - keydata, err := fload.LoadOrCreate(newCertGenerator(rand.New(rand.NewSource(0)), - elliptic.P521())) - require.NoError(t, err) - - key, err := x509.ParseECPrivateKey(keydata) - require.NoError(t, err) - - opts := []minogrpc.Option{ - minogrpc.WithStorage(certs), - minogrpc.WithCertificateKey(key, key.Public()), + var onet mino.Mino + switch kind { + case minoGRPC: + router := tree.NewRouter(minogrpc.NewAddressFactory()) + addr := minogrpc.ParseAddress("127.0.0.1", uint16(port)) + + certs := certs.NewDiskStore(db, session.AddressFactory{}) + + fload := loader.NewFileLoader(filepath.Join(path, certKeyName)) + keydata, err := fload.LoadOrCreate(newCertGenerator(rand.New(rand.NewSource(0)), + elliptic.P521())) + require.NoError(t, err) + + key, err := x509.ParseECPrivateKey(keydata) + require.NoError(t, err) + + opts := []minogrpc.Option{ + minogrpc.WithStorage(certs), + minogrpc.WithCertificateKey(key, key.Public()), + } + + onet, err = minogrpc.NewMinogrpc(addr, nil, router, opts...) + require.NoError(t, err) + case minoWS: + listen, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0/ws") + require.NoError(t, err) + + storage := key.NewStorage(db) + key, _ := storage.LoadOrCreate() + + onet, err = minows.NewMinows(listen, nil, key) + require.NoError(t, err) } - - onet, err := minogrpc.NewMinogrpc(addr, nil, router, opts...) - require.NoError(t, err) onet.GetAddress() // ordering + validation + execution - fload = loader.NewFileLoader(filepath.Join(path, privateKeyFile)) + fload := loader.NewFileLoader(filepath.Join(path, privateKeyFile)) signerdata, err := fload.LoadOrCreate(newKeyGenerator()) require.NoError(t, err) @@ -193,25 +211,27 @@ func newDelaNode(t require.TestingT, path string, port int) dela { // Setup implements dela. It creates the roster, shares the certificate, and // create an new chain. -func (c cosiDelaNode) Setup(delas ...dela) { +func (c cosiDelaNode) Setup(kind string, delas ...dela) { // share the certificates - joinable, ok := c.onet.(minogrpc.Joinable) - require.True(c.t, ok) + if kind == minoGRPC { + joinable, ok := c.onet.(minogrpc.Joinable) + require.True(c.t, ok) - addrURL, err := url.Parse(c.onet.GetAddress().String()) - require.NoError(c.t, err, addrURL) + addrURL, err := url.Parse(c.onet.GetAddress().String()) + require.NoError(c.t, err, addrURL) - token := joinable.GenerateToken(time.Hour) + token := joinable.GenerateToken(time.Hour) - certHash, err := joinable.GetCertificateStore().Hash(joinable.GetCertificateChain()) - require.NoError(c.t, err) + certHash, err := joinable.GetCertificateStore().Hash(joinable.GetCertificateChain()) + require.NoError(c.t, err) - for _, dela := range delas { - otherJoinable, ok := dela.GetMino().(minogrpc.Joinable) - require.True(c.t, ok) + for _, dela := range delas { + otherJoinable, ok := dela.GetMino().(minogrpc.Joinable) + require.True(c.t, ok) - err = otherJoinable.Join(addrURL, token, certHash) - require.NoError(c.t, err) + err = otherJoinable.Join(addrURL, token, certHash) + require.NoError(c.t, err) + } } type extendedService interface { @@ -244,7 +264,7 @@ func (c cosiDelaNode) Setup(delas ...dela) { roster := authority.New(minoAddrs, pubKeys) // create chain - err = extended.Setup(context.Background(), roster) + err := extended.Setup(context.Background(), roster) require.NoError(c.t, err) } diff --git a/test/dela_test.go b/test/dela_test.go index 9d9c8f69..466ff8c7 100644 --- a/test/dela_test.go +++ b/test/dela_test.go @@ -9,7 +9,7 @@ import ( // dela defines the common interface for a Dela node. type dela interface { - Setup(...dela) + Setup(string, ...dela) GetMino() mino.Mino GetOrdering() ordering.Service GetTxManager() txn.Manager diff --git a/test/integration_test.go b/test/integration_test.go index da6df36b..b62932b7 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -33,14 +33,22 @@ func init() { // Use the value contract // Check the state func TestIntegration_Value_Simple(t *testing.T) { - t.Run("3 nodes", getTest[*testing.T](3, 2)) + t.Run("3 nodes: grpc", getTest[*testing.T](3, 2, minoGRPC)) + t.Run("3 nodes: ws", getTest[*testing.T](3, 2, minoWS)) } func BenchmarkValue(b *testing.B) { - getTest[*testing.B](5, b.N)(b) + testGRPC := func(b *testing.B) { + getTest[*testing.B](5, b.N, minoGRPC)(b) + } + testWS := func(b *testing.B) { + getTest[*testing.B](5, b.N, minoWS)(b) + } + b.Run("5 nodes: grpc", testGRPC) + b.Run("5 nodes: ws", testWS) } -func getTest[T require.TestingT](numNode, numTx int) func(t T) { +func getTest[T require.TestingT](numNode, numTx int, kind string) func(t T) { return func(t T) { dir, err := os.MkdirTemp(os.TempDir(), "dela-integration-test") require.NoError(t, err) @@ -52,11 +60,12 @@ func getTest[T require.TestingT](numNode, numTx int) func(t T) { nodes := make([]dela, numNode) for i := range nodes { - node := newDelaNode(t, filepath.Join(dir, "node"+strconv.Itoa(i)), 0) + node := newDelaNode(t, filepath.Join(dir, + "node"+strconv.Itoa(i)), 0, kind) nodes[i] = node } - nodes[0].Setup(nodes[1:]...) + nodes[0].Setup(kind, nodes[1:]...) l := loader.NewFileLoader(filepath.Join(dir, "private.key"))