Skip to content

Commit

Permalink
Refactors fromAny and fromIterator to make the code DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
nkcr committed May 7, 2020
1 parent 0384ba8 commit 23b23ec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 67 deletions.
3 changes: 1 addition & 2 deletions mino/minogrpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

- [ ] Merge OverlayMsg and Envelope
- [ ] Check the `<-ctx.Done()` in overlay.go
- [ ] Test traffic.go
- [ ] Refactor the two function fromAny and fromItarator in routing
- [ ] Test traffic.go
1 change: 1 addition & 0 deletions mino/minogrpc/routing/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ package routing;
import "google/protobuf/any.proto";

message TreeRoutingProto {
// Should be the MarshalText representation of each addr
repeated bytes addrs = 1;
}
77 changes: 12 additions & 65 deletions mino/minogrpc/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,56 +111,12 @@ func (t TreeRoutingFactory) FromIterator(iterator mino.AddressIterator) (Routing
"the iterator")
}

sort.Stable(&addrsBuf)

// We will use the hash of the addresses to set the random seed.
hash := sha256.New()
for _, addr := range addrsBuf {
_, err := hash.Write(addr)
if err != nil {
fabric.Logger.Fatal().Msgf("failed to write hash: %v", err)
}
}

seed := binary.LittleEndian.Uint64(hash.Sum(nil))

// We shuffle the list of addresses, which will then be used to create the
// network tree.
rand.Seed(int64(seed))
rand.Shuffle(len(addrsBuf), func(i, j int) {
addrsBuf[i], addrsBuf[j] = addrsBuf[j], addrsBuf[i]
})

addrs := make([]mino.Address, len(addrsBuf))
for i, addrBuf := range addrsBuf {
addrs[i] = t.addrFactory.FromText(addrBuf)
routing, err := t.fromAddrBuf(addrsBuf)
if err != nil {
return nil, xerrors.Errorf("failed to build from addrsBuf: %v", err)
}

// maximum number of direct connections each node can have. It is comupted
// from the treeHeight and the total number of nodes. There are the
// following relations:
//
// N: total number of nodes
// D: number of direct connections wanted for each node
// H: height of the network tree
//
// N = D^H
// D = sqrt[H](N)
// H = log_D N
d := int(math.Ceil(math.Pow(float64(len(addrs)), 1.0/float64(t.height))))

tree := buildTree(t.rootAddr, addrs, d, -1)

routingNodes := make(map[string]*treeNode)
routingNodes[t.rootAddr.String()] = tree
tree.ForEach(func(n *treeNode) {
routingNodes[n.Addr.String()] = n
})

return &TreeRouting{
routingNodes: routingNodes,
root: tree,
}, nil
return routing, nil
}

// FromAny creates the network tree in a deterministic manner based on
Expand All @@ -173,25 +129,15 @@ func (t TreeRoutingFactory) FromAny(m *any.Any) (Routing, error) {
return nil, xerrors.Errorf("failed to unmarshal routing message: %v", err)
}

if len(msg.Addrs) == 0 {
return nil, xerrors.Errorf("there should be at least one address")
}

addrs := make([]mino.Address, len(msg.Addrs))
for i, addrBuf := range msg.Addrs {
addr := t.addrFactory.FromText(addrBuf)
addrs[i] = addr
routing, err := t.fromAddrBuf(msg.Addrs)
if err != nil {
return nil, xerrors.Errorf("failed to build from addrsBuf: %v", err)
}

addrsBuf := make(addrsBuf, len(addrs))
for i, addr := range addrs {
addrBuf, err := addr.MarshalText()
if err != nil {
return nil, xerrors.Errorf("failed to marshal addr '%s': %v", addr, err)
}
return routing, nil
}

addrsBuf[i] = addrBuf
}
func (t TreeRoutingFactory) fromAddrBuf(addrsBuf addrsBuf) (Routing, error) {

sort.Stable(&addrsBuf)

Expand All @@ -213,11 +159,12 @@ func (t TreeRoutingFactory) FromAny(m *any.Any) (Routing, error) {
addrsBuf[i], addrsBuf[j] = addrsBuf[j], addrsBuf[i]
})

addrs := make([]mino.Address, len(addrsBuf))
for i, addrBuf := range addrsBuf {
addrs[i] = t.addrFactory.FromText(addrBuf)
}

// maximum number of direct connections each node can have. It is computed
// maximum number of direct connections each node can have. It is comupted
// from the treeHeight and the total number of nodes. There are the
// following relations:
//
Expand Down

0 comments on commit 23b23ec

Please sign in to comment.