-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil_test.go
137 lines (129 loc) · 3.72 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex_test
import (
"errors"
"simplex"
. "simplex"
"simplex/testutil"
"testing"
"github.com/stretchr/testify/require"
)
func TestRetrieveFromStorage(t *testing.T) {
brokenStorage := newInMemStorage()
brokenStorage.data[41] = struct {
VerifiedBlock
FinalizationCertificate
}{VerifiedBlock: newTestBlock(ProtocolMetadata{Seq: 41})}
block := newTestBlock(ProtocolMetadata{Seq: 0})
fCert := FinalizationCertificate{
Finalization: ToBeSignedFinalization{
BlockHeader: block.BlockHeader(),
},
}
normalStorage := newInMemStorage()
normalStorage.data[0] = struct {
VerifiedBlock
FinalizationCertificate
}{VerifiedBlock: block, FinalizationCertificate: fCert}
for _, testCase := range []struct {
description string
storage Storage
expectedErr error
expectedBlock VerifiedBlock
expectedFCert *FinalizationCertificate
}{
{
description: "no blocks in storage",
storage: newInMemStorage(),
},
{
description: "broken storage",
storage: brokenStorage,
expectedErr: errors.New("failed retrieving last block from storage with seq 0"),
},
{
description: "normal storage",
storage: normalStorage,
expectedBlock: block,
expectedFCert: &fCert,
},
} {
t.Run(testCase.description, func(t *testing.T) {
block, fCert, err := RetrieveLastIndexFromStorage(testCase.storage)
require.Equal(t, testCase.expectedErr, err)
require.Equal(t, testCase.expectedBlock, block)
require.Equal(t, testCase.expectedFCert, fCert)
})
}
}
func TestFinalizationCertificateValidation(t *testing.T) {
l := testutil.MakeLogger(t, 0)
nodes := []NodeID{{1}, {2}, {3}, {4}, {5}}
eligibleSigners := make(map[string]struct{})
for _, n := range nodes {
eligibleSigners[string(n)] = struct{}{}
}
quorumSize := Quorum(len(nodes))
signatureAggregator := &testSignatureAggregator{}
// Test
tests := []struct {
name string
fCert FinalizationCertificate
quorumSize int
valid bool
}{
{
name: "valid finalization certificate",
fCert: func() FinalizationCertificate {
block := newTestBlock(ProtocolMetadata{})
fCert, _ := newFinalizationRecord(t, l, signatureAggregator, block, nodes[:quorumSize])
return fCert
}(),
quorumSize: quorumSize,
valid: true,
}, {
name: "not enough signers",
fCert: func() FinalizationCertificate {
block := newTestBlock(ProtocolMetadata{})
fCert, _ := newFinalizationRecord(t, l, signatureAggregator, block, nodes[:quorumSize-1])
return fCert
}(),
quorumSize: quorumSize,
valid: false,
},
{
name: "signer signed twice",
fCert: func() FinalizationCertificate {
block := newTestBlock(ProtocolMetadata{})
doubleNodes := []NodeID{{1}, {2}, {3}, {4}, {4}}
fCert, _ := newFinalizationRecord(t, l, signatureAggregator, block, doubleNodes)
return fCert
}(),
quorumSize: quorumSize,
valid: false,
},
{
name: "quorum certificate not in finalization certificate",
fCert: FinalizationCertificate{Finalization: ToBeSignedFinalization{}},
quorumSize: quorumSize,
valid: false,
},
{
name: "nodes are not eligible signers",
fCert: func() FinalizationCertificate {
block := newTestBlock(ProtocolMetadata{})
signers := []NodeID{{1}, {2}, {3}, {4}, {6}}
fCert, _ := newFinalizationRecord(t, l, signatureAggregator, block, signers)
return fCert
}(), quorumSize: quorumSize,
valid: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
valid := simplex.IsFinalizationCertificateValid(eligibleSigners, &tt.fCert, tt.quorumSize, l)
require.Equal(t, tt.valid, valid)
})
}
}