Skip to content

Commit 4723e75

Browse files
authored
Merge pull request #38 from renproject/fn/tcp-options
TCP opts in builder | content type in Hash
2 parents 50b815f + 51ef017 commit 4723e75

File tree

12 files changed

+117
-32
lines changed

12 files changed

+117
-32
lines changed

Diff for: aw.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/renproject/aw/gossip"
1010
"github.com/renproject/aw/handshake"
1111
"github.com/renproject/aw/peer"
12+
"github.com/renproject/aw/tcp"
1213
"github.com/renproject/aw/transport"
1314
"github.com/renproject/aw/wire"
1415
"github.com/renproject/id"
@@ -51,6 +52,19 @@ func New() *Builder {
5152
return builder
5253
}
5354

55+
// WithLogger consumes a logger instance and updates the Airwave Builder to
56+
// use this logger for all its components
57+
func (builder *Builder) WithLogger(logger *zap.Logger) *Builder {
58+
builder.opts = builder.opts.WithLogger(logger)
59+
builder.handshaker = builder.handshaker.WithLogger(logger)
60+
builder.trans = builder.trans.WithLogger(logger)
61+
builder.trans.TCPClientOpts = builder.trans.TCPClientOpts.WithLogger(logger)
62+
builder.trans.TCPServerOpts = builder.trans.TCPServerOpts.WithLogger(logger)
63+
builder.peer = builder.peer.WithLogger(logger)
64+
builder.gossiper = builder.gossiper.WithLogger(logger)
65+
return builder
66+
}
67+
5468
func (builder *Builder) WithPrivKey(privKey *id.PrivKey) *Builder {
5569
builder.handshaker.PrivKey = privKey
5670
builder.dht = dht.New(
@@ -90,6 +104,16 @@ func (builder *Builder) WithPort(port uint16) *Builder {
90104
return builder
91105
}
92106

107+
func (builder *Builder) WithTCPClientOptions(opts tcp.ClientOptions) *Builder {
108+
builder.trans.TCPClientOpts = opts
109+
return builder
110+
}
111+
112+
func (builder *Builder) WithTCPServerOptions(opts tcp.ServerOptions) *Builder {
113+
builder.trans.TCPServerOpts = opts
114+
return builder
115+
}
116+
93117
func (builder *Builder) Build() *Node {
94118
handshaker := handshake.NewECDSA(builder.handshaker)
95119
trans := transport.New(builder.trans, handshaker)
@@ -133,13 +157,11 @@ func (node *Node) Run(ctx context.Context) {
133157
wg.Wait()
134158
}
135159

136-
func (node *Node) Send(ctx context.Context, signatory id.Signatory, dataType uint8, data []byte) {
137-
hash := sha256.Sum256(data)
160+
func (node *Node) Send(ctx context.Context, signatory id.Signatory, hash id.Hash, dataType uint8, data []byte) {
138161
node.gossiper.Gossip(id.Hash(signatory), hash, dataType)
139162
}
140163

141-
func (node *Node) Broadcast(ctx context.Context, subnet id.Hash, dataType uint8, data []byte) {
142-
hash := sha256.Sum256(data)
164+
func (node *Node) Broadcast(ctx context.Context, subnet id.Hash, hash id.Hash, dataType uint8, data []byte) {
143165
node.gossiper.Gossip(subnet, hash, dataType)
144166
}
145167

@@ -170,3 +192,8 @@ func (node *Node) Identity() id.Signatory {
170192
func (node *Node) Addr() wire.Address {
171193
return node.peer.Addr()
172194
}
195+
196+
func Hash(dataType uint8, data []byte) id.Hash {
197+
data = append(data, byte(dataType))
198+
return sha256.Sum256(data)
199+
}

Diff for: aw_test.go

+56-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package aw_test
33
import (
44
"bytes"
55
"context"
6-
"crypto/sha256"
76
"fmt"
87
"math/rand"
98
"sync/atomic"
@@ -12,8 +11,11 @@ import (
1211
"github.com/renproject/aw"
1312
"github.com/renproject/aw/dht"
1413
"github.com/renproject/aw/gossip"
14+
"github.com/renproject/aw/tcp"
1515
"github.com/renproject/aw/wire"
1616
"github.com/renproject/id"
17+
"go.uber.org/zap"
18+
"go.uber.org/zap/zapcore"
1719

1820
. "github.com/onsi/ginkgo"
1921
. "github.com/onsi/gomega"
@@ -38,21 +40,37 @@ var _ = Describe("Airwave", func() {
3840
addr1 := wire.NewUnsignedAddress(wire.TCP, fmt.Sprintf("0.0.0.0:%v", port1), uint64(time.Now().UnixNano()))
3941
privKey1 := id.NewPrivKey()
4042
Expect(addr1.Sign(privKey1)).To(Succeed())
43+
44+
tcpClientOpts := tcp.DefaultClientOptions().
45+
WithTimeToDial(1 * time.Second)
46+
tcpServerOpts := tcp.DefaultServerOptions().
47+
WithHost("0.0.0.0").
48+
WithPreventDuplicateConns(false)
49+
50+
logger, _ := zap.Config{
51+
Encoding: "json",
52+
Level: zap.NewAtomicLevelAt(zapcore.ErrorLevel),
53+
}.Build()
54+
4155
node1 := aw.New().
4256
WithPrivKey(privKey1).
4357
WithAddr(addr1).
44-
WithHost("0.0.0.0").
58+
WithTCPClientOptions(tcpClientOpts).
59+
WithTCPServerOptions(tcpServerOpts).
4560
WithPort(port1).
61+
WithLogger(logger).
4662
Build()
4763

4864
port2 := uint16(3000 + r.Int()%3000)
4965
addr2 := wire.NewUnsignedAddress(wire.TCP, fmt.Sprintf("0.0.0.0:%v", port2), uint64(time.Now().UnixNano()))
5066
privKey2 := id.NewPrivKey()
5167
Expect(addr2.Sign(privKey2)).To(Succeed())
68+
5269
node2 := aw.New().
5370
WithPrivKey(privKey2).
5471
WithAddr(addr2).
55-
WithHost("0.0.0.0").
72+
WithTCPClientOptions(tcpClientOpts).
73+
WithTCPServerOptions(tcpServerOpts).
5674
WithPort(port2).
5775
WithContentResolver(
5876
dht.NewDoubleCacheContentResolver(dht.DefaultDoubleCacheContentResolverOptions(), dht.CallbackContentResolver{
@@ -77,6 +95,7 @@ var _ = Describe("Airwave", func() {
7795
},
7896
}),
7997
).
98+
WithLogger(logger).
8099
Build()
81100

82101
node1.DHT().InsertAddr(node2.Addr())
@@ -89,12 +108,21 @@ var _ = Describe("Airwave", func() {
89108
time.Sleep(100 * time.Millisecond)
90109

91110
subnet := node1.DHT().AddSubnet([]id.Signatory{node2.Identity()})
92-
fmt.Printf("%v\n", subnet)
93111
for i := uint64(0); i < willSendN; i++ {
94-
node1.Broadcast(ctx, subnet, 0, []byte("once"))
95-
node1.Broadcast(ctx, subnet, 0, []byte(fmt.Sprintf("message #%v", i)))
112+
data1 := []byte("once")
113+
data2 := []byte(fmt.Sprintf("message #%v", i))
114+
hash1 := aw.Hash(0, data1)
115+
hash2 := aw.Hash(0, data2)
116+
117+
node1.DHT().InsertContent(hash1, 0, data1)
118+
node1.Broadcast(ctx, subnet, hash1, 0, data1)
119+
node1.DHT().InsertContent(hash2, 0, data2)
120+
node1.Broadcast(ctx, subnet, hash2, 0, data2)
96121
}
97-
node1.Broadcast(ctx, subnet, 0, []byte("done"))
122+
data := []byte("done")
123+
hash := aw.Hash(0, data)
124+
node1.DHT().InsertContent(hash, 0, data)
125+
node1.Broadcast(ctx, subnet, hash, 0, data)
98126

99127
<-ctx.Done()
100128

@@ -117,17 +145,33 @@ var _ = Describe("Airwave", func() {
117145
n := 3
118146
nodes := make([]*aw.Node, n)
119147
addrs := make([]wire.Address, n)
148+
149+
tcpClientOpts := tcp.DefaultClientOptions().
150+
WithTimeToDial(1 * time.Second)
151+
tcpServerOpts := tcp.DefaultServerOptions().
152+
WithHost("0.0.0.0").
153+
WithPreventDuplicateConns(false)
154+
155+
logger, _ := zap.Config{
156+
Encoding: "json",
157+
Level: zap.NewAtomicLevelAt(zapcore.ErrorLevel),
158+
}.Build()
159+
120160
for i := range nodes {
121161
port := uint16(3000 + i)
122162
addrs[i] = wire.NewUnsignedAddress(wire.TCP, fmt.Sprintf("0.0.0.0:%v", port), uint64(time.Now().UnixNano()))
123163
privKey := id.NewPrivKey()
124164
Expect(addrs[i].Sign(privKey)).To(Succeed())
165+
125166
node := aw.New().
126167
WithPrivKey(privKey).
127168
WithAddr(addrs[i]).
128-
WithHost("0.0.0.0").
169+
WithTCPClientOptions(tcpClientOpts).
170+
WithTCPServerOptions(tcpServerOpts).
129171
WithPort(port).
172+
WithLogger(logger).
130173
Build()
174+
131175
nodes[i] = node
132176
}
133177

@@ -150,10 +194,11 @@ var _ = Describe("Airwave", func() {
150194
// each other.
151195
time.Sleep(100 * time.Millisecond)
152196

153-
contentHash := sha256.Sum256([]byte("hello!"))
154-
contentType := uint8(1)
155197
content := []byte("hello!")
156-
nodes[0].Broadcast(ctx, gossip.DefaultSubnet, contentType, content)
198+
contentType := uint8(1)
199+
contentHash := aw.Hash(contentType, content)
200+
nodes[0].DHT().InsertContent(contentHash, contentType, content)
201+
nodes[0].Broadcast(ctx, gossip.DefaultSubnet, contentHash, contentType, content)
157202

158203
found := map[id.Signatory]struct{}{}
159204
for {

Diff for: gossip/gossip_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/renproject/aw/transport"
1717
"github.com/renproject/aw/wire"
1818
"github.com/renproject/id"
19+
"go.uber.org/zap"
20+
"go.uber.org/zap/zapcore"
1921

2022
. "github.com/onsi/ginkgo"
2123
. "github.com/onsi/gomega"
@@ -321,6 +323,10 @@ func initNodes(ctx context.Context, n uint, alpha int) []node {
321323
signatory := id.NewSignatory((*id.PubKey)(&privKey.PublicKey))
322324
host := "0.0.0.0"
323325
port := uint16(3000 + rand.Int()%3000)
326+
logger, _ := zap.Config{
327+
Encoding: "json",
328+
Level: zap.NewAtomicLevelAt(zapcore.ErrorLevel),
329+
}.Build()
324330

325331
dht := dht.New(
326332
signatory,
@@ -333,6 +339,7 @@ func initNodes(ctx context.Context, n uint, alpha int) []node {
333339
transport.DefaultOptions().
334340
WithTCPServerOptions(
335341
tcp.DefaultServerOptions().
342+
WithLogger(logger).
336343
WithHost(host).
337344
WithPort(port).
338345
WithPreventDuplicateConns(false),
@@ -344,6 +351,7 @@ func initNodes(ctx context.Context, n uint, alpha int) []node {
344351
)
345352
gossiper := gossip.New(
346353
gossip.DefaultOptions().
354+
WithLogger(logger).
347355
WithAlpha(int(n)),
348356
signatory,
349357
dht,

Diff for: handshake/opt_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package handshake_test
1+
package handshake_test

Diff for: opt_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package aw_test
1+
package aw_test

Diff for: peer/opt.go

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func DefaultOptions() Options {
4242
}
4343
}
4444

45+
func (opts Options) WithLogger(logger *zap.Logger) Options {
46+
opts.Logger = logger
47+
return opts
48+
}
49+
4550
func (opts Options) WithAddr(addr wire.Address) Options {
4651
opts.Addr = addr
4752
return opts

Diff for: tcp/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package tcp_test
1+
package tcp_test

Diff for: wire/msg_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
var _ = Describe("Message", func() {
1717
Context("when marshaling and unmarshaling", func() {
18-
It("shoudl equal itself", func() {
18+
It("should equal itself", func() {
1919
f := func() bool {
2020
r := rand.New(rand.NewSource(time.Now().UnixNano()))
2121
msg := wireutil.NewMessageBuilder(r).Build()
@@ -27,7 +27,7 @@ var _ = Describe("Message", func() {
2727
Expect(msg.Equal(&unmarshaledMsg)).To(BeTrue())
2828
return true
2929
}
30-
Expect(quick.Check(f, nil)).To(Succeed())
30+
Expect(quick.Check(f, &quick.Config{MaxCount: 10})).To(Succeed())
3131
})
3232
})
3333
})

Diff for: wire/wire_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package wire_test
1+
package wire_test

Diff for: wire/wireutil/addr_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
var _ = Describe("Address builder", func() {
1515
Context("when building messages", func() {
16-
Context("when settting the protocol", func() {
16+
Context("when setting the protocol", func() {
1717
It("should build a message with that protocol", func() {
1818
f := func() bool {
1919
r := rand.New(rand.NewSource(time.Now().UnixNano()))
@@ -26,7 +26,7 @@ var _ = Describe("Address builder", func() {
2626
})
2727
})
2828

29-
Context("when settting the value", func() {
29+
Context("when setting the value", func() {
3030
It("should build a message with that value", func() {
3131
f := func() bool {
3232
r := rand.New(rand.NewSource(time.Now().UnixNano()))
@@ -39,7 +39,7 @@ var _ = Describe("Address builder", func() {
3939
})
4040
})
4141

42-
Context("when settting the nonce", func() {
42+
Context("when setting the nonce", func() {
4343
It("should build a message with that nonce", func() {
4444
f := func() bool {
4545
r := rand.New(rand.NewSource(time.Now().UnixNano()))
@@ -52,7 +52,7 @@ var _ = Describe("Address builder", func() {
5252
})
5353
})
5454

55-
Context("when settting the signature", func() {
55+
Context("when setting the signature", func() {
5656
It("should build a message with that signature", func() {
5757
f := func() bool {
5858
r := rand.New(rand.NewSource(time.Now().UnixNano()))

Diff for: wire/wireutil/msg_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
var _ = Describe("Message builder", func() {
1616
Context("when building messages", func() {
17-
Context("when settting the version", func() {
17+
Context("when setting the version", func() {
1818
It("should build a message with that version", func() {
1919
r := rand.New(rand.NewSource(time.Now().UnixNano()))
2020
f := func() bool {
@@ -23,11 +23,11 @@ var _ = Describe("Message builder", func() {
2323
Expect(msg.Version).To(Equal(version))
2424
return true
2525
}
26-
Expect(quick.Check(f, nil)).To(Succeed())
26+
Expect(quick.Check(f, &quick.Config{MaxCount: 10})).To(Succeed())
2727
})
2828
})
2929

30-
Context("when settting the type", func() {
30+
Context("when setting the type", func() {
3131
It("should build a message with that type", func() {
3232
r := rand.New(rand.NewSource(time.Now().UnixNano()))
3333
f := func() bool {
@@ -36,11 +36,11 @@ var _ = Describe("Message builder", func() {
3636
Expect(msg.Type).To(Equal(ty))
3737
return true
3838
}
39-
Expect(quick.Check(f, nil)).To(Succeed())
39+
Expect(quick.Check(f, &quick.Config{MaxCount: 10})).To(Succeed())
4040
})
4141
})
4242

43-
Context("when settting the data", func() {
43+
Context("when setting the data", func() {
4444
It("should build a message with that data", func() {
4545
r := rand.New(rand.NewSource(time.Now().UnixNano()))
4646
f := func() bool {
@@ -49,7 +49,7 @@ var _ = Describe("Message builder", func() {
4949
Expect(bytes.Equal(msg.Data, data)).To(BeTrue())
5050
return true
5151
}
52-
Expect(quick.Check(f, nil)).To(Succeed())
52+
Expect(quick.Check(f, &quick.Config{MaxCount: 10})).To(Succeed())
5353
})
5454
})
5555
})

Diff for: wire/wireutil/wireutil.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package wireutil
1+
package wireutil

0 commit comments

Comments
 (0)