Skip to content

Commit

Permalink
Merge pull request #121 from ConsenSys/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
nkeywal authored Feb 13, 2019
2 parents 783e656 + 16a3a63 commit 64de81f
Show file tree
Hide file tree
Showing 110 changed files with 4,512 additions and 560 deletions.
4 changes: 3 additions & 1 deletion bn256/cf/bn256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bn256

import (
"crypto/rand"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -44,7 +45,8 @@ func TestSign(t *testing.T) {

sig, err := sk.Sign(msg, nil)
require.NoError(t, err)

buff, _ := sig.MarshalBinary()
fmt.Println(len(buff))
err = pk.VerifySignature(msg, sig)
require.NoError(t, err)
}
Expand Down
6 changes: 6 additions & 0 deletions bn256/go/bn256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bn256

import (
"crypto/rand"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -45,6 +46,11 @@ func TestSign(t *testing.T) {
sig, err := sk.Sign(msg, nil)
require.NoError(t, err)

buff, _ := sig.MarshalBinary()
fmt.Println(len(buff))
buff, _ = pk.MarshalBinary()
fmt.Println(len(buff))

err = pk.VerifySignature(msg, sig)
require.NoError(t, err)
}
Expand Down
63 changes: 63 additions & 0 deletions network/counter_encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package network

import (
"bytes"
"io"
"sync"

"github.com/ConsenSys/handel"
)

// CounterEncoding is a wrapper around an Encoding that can report how many
// bytes sent and received and implements the monitor.Counter interface
type CounterEncoding struct {
*sync.RWMutex
Encoding
sent int // bytes sent
rcvd int // bytes received
}

// NewCounterEncoding returns an Encoding that implements the monitor.Counter
// interface
func NewCounterEncoding(e Encoding) *CounterEncoding {
return &CounterEncoding{Encoding: e, RWMutex: new(sync.RWMutex)}
}

// Encode implements the Encoding interface
func (c *CounterEncoding) Encode(p *handel.Packet, w io.Writer) error {
var b bytes.Buffer
combined := io.MultiWriter(w, &b)
if err := c.Encoding.Encode(p, combined); err != nil {
return err
}

c.Lock()
c.sent += b.Len()
c.Unlock()
return nil
}

// Decode implements the Encoding interface
func (c *CounterEncoding) Decode(r io.Reader) (*handel.Packet, error) {
var b bytes.Buffer
var tee = io.TeeReader(r, &b)
p, err := c.Encoding.Decode(tee)
if err != nil {
return nil, err
}

c.Lock()
c.rcvd += b.Len()
c.Unlock()
return p, nil
}

// Values implements the monitor.Counter interface
func (c *CounterEncoding) Values() map[string]float64 {
c.RLock()
defer c.RUnlock()
return map[string]float64{
"sentBytes": float64(c.sent),
"rcvdBytes": float64(c.rcvd),
}
}
34 changes: 34 additions & 0 deletions network/counter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package network

import (
"bytes"
"testing"

"github.com/ConsenSys/handel"
"github.com/stretchr/testify/require"
)

func TestCounterEncoding(t *testing.T) {
var medium bytes.Buffer

actual := NewGOBEncoding()
counter := NewCounterEncoding(actual)

require.True(t, counter.Values()["sentBytes"] == 0.0)
require.True(t, counter.Values()["rcvdBytes"] == 0.0)

toSend := &handel.Packet{
Origin: 156,
Level: 8,
MultiSig: []byte("History repeats itself, first as tragedy, second as farce."),
}

require.NoError(t, counter.Encode(toSend, &medium))
require.True(t, counter.Values()["sentBytes"] > 0.0)

read, err := counter.Decode(&medium)
require.NoError(t, err)

require.Equal(t, toSend, read)
require.True(t, counter.Values()["rcvdBytes"] > 0.0)
}
24 changes: 24 additions & 0 deletions network/udp/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Network struct {
ready chan bool
done chan bool
buff []*handel.Packet
sent int
rcvd int
}

// NewNetwork creates Network baked by udp protocol
Expand Down Expand Up @@ -69,6 +71,7 @@ func (udpNet *Network) Stop() {
if udpNet.quit {
return
}
udpNet.udpSock.Close()
udpNet.quit = true
close(udpNet.done)
}
Expand All @@ -82,6 +85,9 @@ func (udpNet *Network) RegisterListener(listener h.Listener) {

//Send sends a packet to supplied identities
func (udpNet *Network) Send(identities []h.Identity, packet *h.Packet) {
udpNet.Lock()
udpNet.sent += len(identities)
udpNet.Unlock()
for _, id := range identities {
udpNet.send(id, packet)
}
Expand Down Expand Up @@ -176,6 +182,7 @@ func (udpNet *Network) loop() {
func (udpNet *Network) getListeners() []handel.Listener {
udpNet.RLock()
defer udpNet.RUnlock()
udpNet.rcvd++
return udpNet.listeners
}

Expand All @@ -200,3 +207,20 @@ func (udpNet *Network) dispatchLoop() {
}
}
}

// Values implements the monitor.CounterMeasure interface
func (udpNet *Network) Values() map[string]float64 {
udpNet.RLock()
defer udpNet.RUnlock()
toSend := map[string]float64{
"sent": float64(udpNet.sent),
"rcvd": float64(udpNet.rcvd),
}
counter, ok := udpNet.enc.(*network.CounterEncoding)
if ok {
for k, v := range counter.Values() {
toSend[k] = v
}
}
return toSend
}
69 changes: 6 additions & 63 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ type ReportHandel struct {
*Handel
}

// Reporter is a generic interface that can report different data about its
// internal state
// Reporter is simply a copy of monitor.Counter interface to avoid importint
// monitor in handel.
type Reporter interface {
Values() map[string]float64
}

// NewReportHandel returns a Handel that can report some statistis about its
// internals
func NewReportHandel(h *Handel) *ReportHandel {
h.net = NewReportNetwork(h.net)
h.store = newReportStore(h.store)
return &ReportHandel{h}
}

// Values returns the values of ALL internal components of Handel merged together.
func (r *ReportHandel) Values() map[string]float64 {
net := r.Handel.net.(*ReportNetwork)
net := r.Network()
netValues := net.Values()
store := r.Handel.store.(*ReportStore)
storeValues := store.Values()
Expand All @@ -38,7 +37,7 @@ func (r *ReportHandel) Values() map[string]float64 {

// Network returns the Network reporter interface
func (r *ReportHandel) Network() Reporter {
return r.Handel.net.(*ReportNetwork)
return r.Handel.net.(Reporter)
}

// Store returns the Store reporter interface
Expand All @@ -51,62 +50,6 @@ func (r *ReportHandel) Processing() Reporter {
return r.Handel.proc.(Reporter)
}

// ReportNetwork is a struct that implements the Network interface by augmenting
// the Network's method with accountability. How many packets received and send
// can be logged.
type ReportNetwork struct {
Network
sentPackets uint32
rcvdPackets uint32
lis []Listener
}

// NewReportNetwork returns a Network with reporting capabilities.
func NewReportNetwork(n Network) Network {
r := &ReportNetwork{
Network: n,
}
n.RegisterListener(r)
return r
}

// Send implements the Network interface
func (r *ReportNetwork) Send(ids []Identity, p *Packet) {
r.sentPackets += uint32(len(ids))
r.Network.Send(ids, p)
}

// RegisterListener implements the Network interface
func (r *ReportNetwork) RegisterListener(l Listener) {
r.lis = append(r.lis, l)
}

// NewPacket implements the Listener interface
func (r *ReportNetwork) NewPacket(p *Packet) {
r.rcvdPackets++
for _, l := range r.lis {
l.NewPacket(p)
}
}

// Sent returns the number of sent packets
func (r *ReportNetwork) Sent() uint32 {
return r.sentPackets
}

// Received returns the number of received packets
func (r *ReportNetwork) Received() uint32 {
return r.rcvdPackets
}

// Values implements the simul/monitor/CounterIO interface
func (r *ReportNetwork) Values() map[string]float64 {
return map[string]float64{
"sent": float64(r.Sent()),
"rcvd": float64(r.Received()),
}
}

// ReportStore is a Store that can report some statistics about the storage
type ReportStore struct {
signatureStore
Expand All @@ -123,7 +66,7 @@ func newReportStore(s signatureStore) signatureStore {
}

// Store implements the signatureStore interface
func (r *ReportStore) Store(sp *incomingSig) *MultiSignature{
func (r *ReportStore) Store(sp *incomingSig) *MultiSignature {
ms := r.signatureStore.Store(sp)
if ms != nil {
r.sucessReplaced++
Expand All @@ -137,6 +80,6 @@ func (r *ReportStore) Store(sp *incomingSig) *MultiSignature{
func (r *ReportStore) Values() map[string]float64 {
return map[string]float64{
"successReplace": float64(r.sucessReplaced),
"replaceTrial": float64(r.replacedTrial),
"replaceTrial": float64(r.replacedTrial),
}
}
4 changes: 2 additions & 2 deletions simul/aws_config_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ MasterTimeOut = 10
SSHUser = "ubuntu"
TargetSystem = "linux"
TargetArch = "amd64"
ConfTimeout = 10
CopyBinFiles = true
ConfTimeout = 5
CopyBinFiles = false
Loading

0 comments on commit 64de81f

Please sign in to comment.