Skip to content

Commit

Permalink
Integration tests for MinoWS
Browse files Browse the repository at this point in the history
Co-authored-by: Zach Xion [email protected]
  • Loading branch information
ineiti committed Jul 5, 2024
1 parent e9eaa22 commit 0d1740a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 42 deletions.
92 changes: 56 additions & 36 deletions test/cosidela_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -46,6 +49,9 @@ import (
"golang.org/x/xerrors"
)

const minoGRPC = "grpc"
const minoWS = "ws"

const certKeyName = "cert.key"
const privateKeyFile = "private.key"

Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion test/dela_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"))

Expand Down

0 comments on commit 0d1740a

Please sign in to comment.