Skip to content

Commit

Permalink
Merge pull request #38 from dedis/topology-passing
Browse files Browse the repository at this point in the history
Implements a simple tree networking topology
  • Loading branch information
Gaylor Bosson committed May 7, 2020
2 parents 12b1c9f + 23b23ec commit 5d4e608
Show file tree
Hide file tree
Showing 14 changed files with 1,334 additions and 570 deletions.
23 changes: 16 additions & 7 deletions internal/testing/fake/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,24 @@ type AddressIterator struct {
index int
}

// NewAddressIterator returns a new address iterator
func NewAddressIterator(addrs []mino.Address) AddressIterator {
return AddressIterator{
addrs: addrs,
}
}

// HasNext implements mino.AddressIterator.
func (i *AddressIterator) HasNext() bool {
return i.index+1 < len(i.addrs)
return i.index < len(i.addrs)
}

// GetNext implements mino.AddressIterator.
func (i *AddressIterator) GetNext() mino.Address {
if i.HasNext() {
res := i.addrs[i.index]
i.index++
return i.addrs[i.index]
return res
}
return nil
}
Expand All @@ -120,14 +128,15 @@ type PublicKeyIterator struct {

// HasNext implements crypto.PublicKeyIterator.
func (i *PublicKeyIterator) HasNext() bool {
return i.index+1 < len(i.signers)
return i.index < len(i.signers)
}

// GetNext implements crypto.PublicKeyIterator.
func (i *PublicKeyIterator) GetNext() crypto.PublicKey {
if i.HasNext() {
res := i.signers[i.index]
i.index++
return i.signers[i.index].GetPublicKey()
return res.GetPublicKey()
}
return nil
}
Expand Down Expand Up @@ -272,12 +281,12 @@ func (ca CollectiveAuthority) Len() int {

// AddressIterator implements mino.Players.
func (ca CollectiveAuthority) AddressIterator() mino.AddressIterator {
return &AddressIterator{addrs: ca.addrs, index: -1}
return &AddressIterator{addrs: ca.addrs}
}

// PublicKeyIterator implements cosi.CollectiveAuthority.
func (ca CollectiveAuthority) PublicKeyIterator() crypto.PublicKeyIterator {
return &PublicKeyIterator{signers: ca.signers, index: -1}
return &PublicKeyIterator{signers: ca.signers}
}

// PublicKeyFactory is a fake implementation of a public key factory.
Expand Down Expand Up @@ -597,7 +606,7 @@ type AddressFactory struct {

// FromText implements mino.AddressFactory.
func (f AddressFactory) FromText(text []byte) mino.Address {
if len(text) > 4 {
if len(text) >= 4 {
index := binary.LittleEndian.Uint32(text)
return Address{index: int(index)}
}
Expand Down
5 changes: 5 additions & 0 deletions mino/minogrpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TODO

- [ ] Merge OverlayMsg and Envelope
- [ ] Check the `<-ctx.Done()` in overlay.go
- [ ] Test traffic.go
26 changes: 15 additions & 11 deletions mino/minogrpc/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (

"go.dedis.ch/fabric/encoding"
"go.dedis.ch/fabric/mino"
"go.dedis.ch/fabric/mino/minogrpc/routing"
"golang.org/x/xerrors"
)

//go:generate protoc -I ./ --go_out=plugins=grpc:./ ./overlay.proto

var namespaceMatch = regexp.MustCompile("^[a-zA-Z0-9]+$")
var (
namespaceMatch = regexp.MustCompile("^[a-zA-Z0-9]+$")
defaultAddressFactory = addressFactory{}
)

// Minogrpc represents a grpc service restricted to a namespace
type Minogrpc struct {
Expand Down Expand Up @@ -52,12 +56,11 @@ func (f addressFactory) FromText(text []byte) mino.Address {
return address{id: string(text)}
}

// NewMinogrpc sets up the grpc and http servers. It does not start the
// server. Identifier must be an address with a port, something like
// 127.0.0.1:3333
// NewMinogrpc sets up the grpc and http servers. Identifier must be an address
// with a port, something like 127.0.0.1:3333
//
// TODO: use a different type of argument for identifier, maybe net/url ?
func NewMinogrpc(identifier string) (Minogrpc, error) {
func NewMinogrpc(identifier string, rf routing.Factory) (Minogrpc, error) {

minoGrpc := Minogrpc{}

Expand All @@ -69,7 +72,7 @@ func NewMinogrpc(identifier string) (Minogrpc, error) {
id: identifier,
}

server, err := CreateServer(addr)
server, err := NewServer(addr, rf)
if err != nil {
return minoGrpc, xerrors.Errorf("failed to create server: %v", err)
}
Expand All @@ -91,7 +94,7 @@ func NewMinogrpc(identifier string) (Minogrpc, error) {
// GetAddressFactory implements Mino. It returns the address
// factory.
func (m Minogrpc) GetAddressFactory() mino.AddressFactory {
return addressFactory{}
return defaultAddressFactory
}

// GetAddress implements Mino. It returns the address of the server
Expand Down Expand Up @@ -129,10 +132,11 @@ func (m Minogrpc) MakeNamespace(namespace string) (mino.Mino, error) {
func (m Minogrpc) MakeRPC(name string, h mino.Handler) (mino.RPC, error) {
URI := fmt.Sprintf("%s/%s", m.namespace, name)
rpc := &RPC{
encoder: encoding.NewProtoEncoder(),
handler: h,
srv: m.server,
uri: URI,
encoder: encoding.NewProtoEncoder(),
handler: h,
srv: m.server,
uri: URI,
routingFactory: m.server.routingFactory,
}

m.server.handlers[URI] = h
Expand Down
16 changes: 8 additions & 8 deletions mino/minogrpc/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_NewMinogrpc(t *testing.T) {
// The happy path
id := "127.0.0.1:3333"

minoRPC, err := NewMinogrpc(id)
minoRPC, err := NewMinogrpc(id, nil)
require.NoError(t, err)

require.Equal(t, id, minoRPC.GetAddress().String())
Expand All @@ -41,7 +41,7 @@ func Test_NewMinogrpc(t *testing.T) {
require.Equal(t, peer, minoRPC.server.neighbours[id])

// Giving an empty address
minoRPC, err = NewMinogrpc("")
minoRPC, err = NewMinogrpc("", nil)
require.EqualError(t, err, "identifier can't be empty")
}

Expand Down Expand Up @@ -164,7 +164,7 @@ func TestMinogrpc_GetAddressFactory(t *testing.T) {
}

func TestPlayers_AddressIterator(t *testing.T) {
players := fakePlayers{players: []address{{"test"}}}
players := fakePlayers{players: []mino.Address{address{"test"}}}
it := players.AddressIterator()
it2, ok := it.(*fakeAddressIterator)
require.True(t, ok)
Expand All @@ -175,9 +175,9 @@ func TestPlayers_AddressIterator(t *testing.T) {
}

func TestAddressIterator(t *testing.T) {
a := address{"test"}
a := &address{"test"}
it := fakeAddressIterator{
players: []address{a},
players: []mino.Address{a},
}

require.True(t, it.HasNext())
Expand All @@ -187,13 +187,13 @@ func TestAddressIterator(t *testing.T) {
require.False(t, it.HasNext())
}

// -----------------
// -----------------------------------------------------------------------------
// Utility functions

// fakePlayers implements mino.Players{}
type fakePlayers struct {
mino.Players
players []address
players []mino.Address
iterator *fakeAddressIterator
}

Expand All @@ -212,7 +212,7 @@ func (p *fakePlayers) Len() int {

// fakeAddressIterator implements mino.addressIterator{}
type fakeAddressIterator struct {
players []address
players []mino.Address
cursor int
}

Expand Down
Loading

0 comments on commit 5d4e608

Please sign in to comment.