Skip to content

Commit 3f74024

Browse files
committed
add comm tests
1 parent 5de26b1 commit 3f74024

File tree

5 files changed

+222
-139
lines changed

5 files changed

+222
-139
lines changed

message/simplex_outbound_msg_builder.go

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ type SimplexOutboundMessageBuilder interface {
5353
) (OutboundMessage, error)
5454
}
5555

56-
5756
func (b *outMsgBuilder) BlockProposal(
5857
chainID ids.ID,
5958
block []byte,
@@ -63,31 +62,31 @@ func (b *outMsgBuilder) BlockProposal(
6362
&p2p.Message{
6463
Message: &p2p.Message_Simplex{
6564
Simplex: &p2p.Simplex{
66-
ChainId: chainID[:],
67-
Message: &p2p.Simplex_BlockProposal{
68-
BlockProposal: &p2p.BlockProposal{
69-
Block: block,
70-
Vote: &p2p.Vote{
71-
Vote: &p2p.BlockHeader{
72-
Metadata: &p2p.ProtocolMetadata{
73-
Version: uint32(vote.Vote.Version),
74-
Epoch: vote.Vote.Epoch,
75-
Round: vote.Vote.Round,
76-
Seq: vote.Vote.Seq,
77-
Prev: vote.Vote.Prev[:],
65+
ChainId: chainID[:],
66+
Message: &p2p.Simplex_BlockProposal{
67+
BlockProposal: &p2p.BlockProposal{
68+
Block: block,
69+
Vote: &p2p.Vote{
70+
Vote: &p2p.BlockHeader{
71+
Metadata: &p2p.ProtocolMetadata{
72+
Version: uint32(vote.Vote.Version),
73+
Epoch: vote.Vote.Epoch,
74+
Round: vote.Vote.Round,
75+
Seq: vote.Vote.Seq,
76+
Prev: vote.Vote.Prev[:],
77+
},
78+
Digest: vote.Vote.Digest[:],
79+
},
80+
Signature: &p2p.Signature{
81+
Signer: vote.Signature.Signer[:],
82+
Value: vote.Signature.Value[:],
7883
},
79-
Digest: vote.Vote.Digest[:],
80-
},
81-
Signature: &p2p.Signature{
82-
Signer: vote.Signature.Signer[:],
83-
Value: vote.Signature.Value[:],
8484
},
8585
},
8686
},
8787
},
8888
},
8989
},
90-
},
9190
b.compressionType,
9291
false,
9392
)
@@ -108,16 +107,16 @@ func (b *outMsgBuilder) Vote(
108107
Vote: &p2p.BlockHeader{
109108
Metadata: &p2p.ProtocolMetadata{
110109
Version: uint32(blockHeader.Version),
111-
Epoch: blockHeader.Epoch,
112-
Round: blockHeader.Round,
113-
Seq: blockHeader.Seq,
114-
Prev: blockHeader.Prev[:],
110+
Epoch: blockHeader.Epoch,
111+
Round: blockHeader.Round,
112+
Seq: blockHeader.Seq,
113+
Prev: blockHeader.Prev[:],
115114
},
116115
Digest: blockHeader.Digest[:],
117116
},
118117
Signature: &p2p.Signature{
119118
Signer: signature.Signer[:],
120-
Value: signature.Value[:],
119+
Value: signature.Value[:],
121120
},
122121
},
123122
},
@@ -143,14 +142,14 @@ func (b *outMsgBuilder) EmptyVote(
143142
EmptyVote: &p2p.EmptyVote{
144143
Vote: &p2p.ProtocolMetadata{
145144
Version: uint32(protocolMetadata.Version),
146-
Epoch: protocolMetadata.Epoch,
147-
Round: protocolMetadata.Round,
148-
Seq: protocolMetadata.Seq,
149-
Prev: protocolMetadata.Prev[:],
145+
Epoch: protocolMetadata.Epoch,
146+
Round: protocolMetadata.Round,
147+
Seq: protocolMetadata.Seq,
148+
Prev: protocolMetadata.Prev[:],
150149
},
151150
Signature: &p2p.Signature{
152151
Signer: signature.Signer[:],
153-
Value: signature.Value[:],
152+
Value: signature.Value[:],
154153
},
155154
},
156155
},
@@ -177,16 +176,16 @@ func (b *outMsgBuilder) FinalizeVote(
177176
Vote: &p2p.BlockHeader{
178177
Metadata: &p2p.ProtocolMetadata{
179178
Version: uint32(blockHeader.Version),
180-
Epoch: blockHeader.Epoch,
181-
Round: blockHeader.Round,
182-
Seq: blockHeader.Seq,
183-
Prev: blockHeader.Prev[:],
179+
Epoch: blockHeader.Epoch,
180+
Round: blockHeader.Round,
181+
Seq: blockHeader.Seq,
182+
Prev: blockHeader.Prev[:],
184183
},
185184
Digest: blockHeader.Digest[:],
186185
},
187186
Signature: &p2p.Signature{
188187
Signer: signature.Signer[:],
189-
Value: signature.Value[:],
188+
Value: signature.Value[:],
190189
},
191190
},
192191
},
@@ -213,10 +212,10 @@ func (b *outMsgBuilder) Notarization(
213212
Finalization: &p2p.BlockHeader{
214213
Metadata: &p2p.ProtocolMetadata{
215214
Version: uint32(blockHeader.Version),
216-
Epoch: blockHeader.Epoch,
217-
Round: blockHeader.Round,
218-
Seq: blockHeader.Seq,
219-
Prev: blockHeader.Prev[:],
215+
Epoch: blockHeader.Epoch,
216+
Round: blockHeader.Round,
217+
Seq: blockHeader.Seq,
218+
Prev: blockHeader.Prev[:],
220219
},
221220
Digest: blockHeader.Digest[:],
222221
},
@@ -245,10 +244,10 @@ func (b *outMsgBuilder) EmptyNotarization(
245244
EmptyNotarization: &p2p.EmptyNotarization{
246245
EmptyVote: &p2p.ProtocolMetadata{
247246
Version: uint32(protocolMetadata.Version),
248-
Epoch: protocolMetadata.Epoch,
249-
Round: protocolMetadata.Round,
250-
Seq: protocolMetadata.Seq,
251-
Prev: protocolMetadata.Prev[:],
247+
Epoch: protocolMetadata.Epoch,
248+
Round: protocolMetadata.Round,
249+
Seq: protocolMetadata.Seq,
250+
Prev: protocolMetadata.Prev[:],
252251
},
253252
QuorumCertificate: qc,
254253
},
@@ -276,10 +275,10 @@ func (b *outMsgBuilder) Finalization(
276275
Finalization: &p2p.BlockHeader{
277276
Metadata: &p2p.ProtocolMetadata{
278277
Version: uint32(blockHeader.Version),
279-
Epoch: blockHeader.Epoch,
280-
Round: blockHeader.Round,
281-
Seq: blockHeader.Seq,
282-
Prev: blockHeader.Prev[:],
278+
Epoch: blockHeader.Epoch,
279+
Round: blockHeader.Round,
280+
Seq: blockHeader.Seq,
281+
Prev: blockHeader.Prev[:],
283282
},
284283
Digest: blockHeader.Digest[:],
285284
},
@@ -292,4 +291,4 @@ func (b *outMsgBuilder) Finalization(
292291
b.compressionType,
293292
false,
294293
)
295-
}
294+
}

simplex/bls_test.go

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,83 +9,11 @@ import (
99
"github.com/stretchr/testify/require"
1010

1111
"github.com/ava-labs/avalanchego/ids"
12-
"github.com/ava-labs/avalanchego/snow/validators"
1312
"github.com/ava-labs/avalanchego/utils/crypto/bls"
14-
"github.com/ava-labs/avalanchego/utils/crypto/bls/signer/localsigner"
1513
)
1614

17-
var _ ValidatorInfo = (*testValidatorInfo)(nil)
18-
19-
// testValidatorInfo is a mock implementation of ValidatorInfo for testing purposes.
20-
// it assumes all validators are in the same subnet and returns all of them for any subnetID.
21-
type testValidatorInfo struct {
22-
validators map[ids.NodeID]validators.Validator
23-
}
24-
25-
func (v *testValidatorInfo) GetValidatorIDs(_ ids.ID) []ids.NodeID {
26-
if v.validators == nil {
27-
return nil
28-
}
29-
30-
ids := make([]ids.NodeID, 0, len(v.validators))
31-
for id := range v.validators {
32-
ids = append(ids, id)
33-
}
34-
return ids
35-
}
36-
37-
func (v *testValidatorInfo) GetValidator(_ ids.ID, nodeID ids.NodeID) (*validators.Validator, bool) {
38-
if v.validators == nil {
39-
return nil, false
40-
}
41-
42-
val, exists := v.validators[nodeID]
43-
if !exists {
44-
return nil, false
45-
}
46-
return &val, true
47-
}
48-
49-
func newTestValidatorInfo(nodeIds []ids.NodeID, pks []*bls.PublicKey) *testValidatorInfo {
50-
if len(nodeIds) != len(pks) {
51-
panic("nodeIds and pks must have the same length")
52-
}
53-
54-
vds := make(map[ids.NodeID]validators.Validator, len(pks))
55-
for i, pk := range pks {
56-
validator := validators.Validator{
57-
PublicKey: pk,
58-
NodeID: nodeIds[i],
59-
}
60-
vds[nodeIds[i]] = validator
61-
}
62-
// all we need is to generate the public keys for the validators
63-
return &testValidatorInfo{
64-
validators: vds,
65-
}
66-
}
67-
68-
func newEngineConfig(ls *localsigner.LocalSigner) *Config {
69-
nodeID := ids.GenerateTestNodeID()
70-
71-
simplexChainContext := SimplexChainContext{
72-
NodeID: nodeID,
73-
ChainID: ids.GenerateTestID(),
74-
SubnetID: ids.GenerateTestID(),
75-
}
76-
77-
return &Config{
78-
Ctx: simplexChainContext,
79-
Validators: newTestValidatorInfo([]ids.NodeID{nodeID}, []*bls.PublicKey{ls.PublicKey()}),
80-
SignBLS: ls.Sign,
81-
}
82-
}
83-
8415
func TestBLSSignVerify(t *testing.T) {
85-
ls, err := localsigner.New()
86-
require.NoError(t, err)
87-
88-
config := newEngineConfig(ls)
16+
config, _ := newTestEngineConfig(t, 0)
8917

9018
signer, verifier := NewBLSAuth(config)
9119

@@ -99,10 +27,7 @@ func TestBLSSignVerify(t *testing.T) {
9927
}
10028

10129
func TestSignerNotInMemberSet(t *testing.T) {
102-
ls, err := localsigner.New()
103-
require.NoError(t, err)
104-
105-
config := newEngineConfig(ls)
30+
config, _ := newTestEngineConfig(t, 0)
10631
signer, verifier := NewBLSAuth(config)
10732

10833
msg := "Begin at the beginning, and go on till you come to the end: then stop"
@@ -116,10 +41,7 @@ func TestSignerNotInMemberSet(t *testing.T) {
11641
}
11742

11843
func TestSignerInvalidMessageEncoding(t *testing.T) {
119-
ls, err := localsigner.New()
120-
require.NoError(t, err)
121-
122-
config := newEngineConfig(ls)
44+
config, ls := newTestEngineConfig(t, 0)
12345

12446
// sign a message with invalid encoding
12547
dummyMsg := []byte("dummy message")

simplex/comm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func (c *Comm) ListNodes() []simplex.NodeID {
8181
}
8282

8383
func (c *Comm) SendMessage(msg *simplex.Message, destination simplex.NodeID) {
84+
// TODO: do we want to check if the destination is in the subnet?
85+
8486
var outboundMessage message.OutboundMessage
8587
var err error
8688
switch {
@@ -98,6 +100,7 @@ func (c *Comm) SendMessage(msg *simplex.Message, destination simplex.NodeID) {
98100
outboundMessage, err = c.msgBuilder.EmptyNotarization(c.chainID, msg.EmptyNotarization.Vote.ProtocolMetadata, msg.EmptyNotarization.QC.Bytes())
99101
case msg.Finalization != nil:
100102
outboundMessage, err = c.msgBuilder.Finalization(c.chainID, msg.Finalization.Finalization.BlockHeader, msg.Finalization.QC.Bytes())
103+
// TODO: create replication messages
101104
}
102105

103106
if err != nil {

0 commit comments

Comments
 (0)