-
Notifications
You must be signed in to change notification settings - Fork 12
/
committee_election_test.go
194 lines (159 loc) · 8.13 KB
/
committee_election_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2019 the orbs-network-go authors
// This file is part of the orbs-network-go library in the Orbs project.
//
// This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
// The above notice should be included in all copies or substantial portions of the software.
package acceptance
import (
"context"
"github.com/orbs-network/crypto-lib-go/crypto/digest"
"github.com/orbs-network/orbs-network-go/test/acceptance/callcontract"
testKeys "github.com/orbs-network/orbs-network-go/test/crypto/keys"
"github.com/orbs-network/orbs-spec/types/go/primitives"
"github.com/orbs-network/orbs-spec/types/go/protocol/consensus"
"github.com/orbs-network/scribe/log"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func TestCommitteeElections_OneElectionAndCheckReputationChanges(t *testing.T) {
NewHarness().
WithNumNodes(6).
WithManagementPollingInterval(20*time.Millisecond).
WithLogFilters(log.DiscardAll()).
WithConsensusAlgos(consensus.CONSENSUS_ALGO_TYPE_LEAN_HELIX).
Start(t, func(t testing.TB, ctx context.Context, network *Network) {
contract := callcontract.NewContractClient(network)
network.WaitForBlock(ctx, 2)
reputationCommitteeTerm0, _ := getCommitteeMisses(t, contract.GetAllCommitteeMisses(ctx, 0))
require.Len(t, reputationCommitteeTerm0, 6, "number of addresses should equal original committee of 6")
t.Log("elect 0,1,2,3")
newRefTime := GenerateNewManagementReferenceTime(0)
blockOfChange := setElectCommitteeAtAndWait(t, ctx, network, 0, newRefTime, 0, 1, 2, 3)
network.WaitForBlock(ctx, blockOfChange+1) // need to be able to run query on block closed AFTER change
reputationCommitteeTerm1, _ := getCommitteeMisses(t, contract.GetAllCommitteeMisses(ctx, 0))
require.Len(t, reputationCommitteeTerm1, 4, "number of addresses should equal new partail committee of 4")
t.Log("test done, shutting down")
})
}
func TestCommitteeElections_VerifyCommitteeSigns(t *testing.T) {
NewHarness().
WithNumNodes(6).
WithManagementPollingInterval(20*time.Millisecond).
WithLogFilters(log.DiscardAll()).
WithConsensusAlgos(consensus.CONSENSUS_ALGO_TYPE_LEAN_HELIX).
Start(t, func(t testing.TB, ctx context.Context, network *Network) {
contract := callcontract.NewContractClient(network)
token := network.DeployBenchmarkTokenContract(ctx, 5)
_, txHash := token.Transfer(ctx, 0, 10, 5, 6)
network.WaitForTransactionInNodeState(ctx, txHash, 0)
require.EqualValues(t, 10, token.GetBalance(ctx, 0, 6))
verifyTxSignersAreFromGroup(t, ctx, contract.API, txHash, 0, []int{0, 1, 2, 3, 4, 5})
t.Log("elect 0,1,2,3")
newRefTime := GenerateNewManagementReferenceTime(0)
setElectCommitteeAtAndWait(t, ctx, network, 0, newRefTime, 0, 1, 2, 3)
_, txHash = token.Transfer(ctx, 4, 10, 5, 6)
network.WaitForTransactionInNodeState(ctx, txHash, 4)
require.EqualValues(t, 20, token.GetBalance(ctx, 4, 6))
verifyTxSignersAreFromGroup(t, ctx, contract.API, txHash, 4, []int{0, 1, 2, 3})
t.Log("test done, shutting down")
})
}
func TestCommitteeElections_MultipleReElections(t *testing.T) {
NewHarness().
WithNumNodes(6).
WithManagementPollingInterval(20*time.Millisecond).
WithLogFilters(log.DiscardAll()).
WithConsensusAlgos(consensus.CONSENSUS_ALGO_TYPE_LEAN_HELIX).
Start(t, func(t testing.TB, ctx context.Context, network *Network) {
contract := callcontract.NewContractClient(network)
token := network.DeployBenchmarkTokenContract(ctx, 5)
t.Log("elect 0,1,2,3")
newRefTime := GenerateNewManagementReferenceTime(0)
setElectCommitteeAtAndWait(t, ctx, network, 0, newRefTime, 0, 1, 2, 3)
t.Log("elect 1,2,3,4")
newRefTime = GenerateNewManagementReferenceTime(newRefTime)
setElectCommitteeAtAndWait(t, ctx, network, 0, newRefTime, 1, 2, 3, 4)
t.Log("elect 2,3,4,5")
newRefTime = GenerateNewManagementReferenceTime(newRefTime)
setElectCommitteeAtAndWait(t, ctx, network, 1, newRefTime, 2, 3, 4, 5)
_, txHash := token.Transfer(ctx, 3, 10, 5, 6)
network.WaitForTransactionInNodeState(ctx, txHash, 3)
require.EqualValues(t, 10, token.GetBalance(ctx, 3, 6))
verifyTxSignersAreFromGroup(t, ctx, contract.API, txHash, 3, []int{2, 3, 4, 5})
t.Log("test done, shutting down")
})
}
func TestCommitteeElections_AllNodesLoseElectionButReturn(t *testing.T) {
NewHarness().
WithNumNodes(8).
WithManagementPollingInterval(20*time.Millisecond).
WithLogFilters(log.DiscardAll()).
WithConsensusAlgos(consensus.CONSENSUS_ALGO_TYPE_LEAN_HELIX).
Start(t, func(t testing.TB, ctx context.Context, network *Network) {
contract := callcontract.NewContractClient(network)
token := network.DeployBenchmarkTokenContract(ctx, 5)
t.Log("elect 0,1,2,3")
newRefTime := GenerateNewManagementReferenceTime(0)
setElectCommitteeAtAndWait(t, ctx, network, 0, newRefTime, 0, 1, 2, 3)
t.Log("elect 4,5,6,7 - entire first group loses")
newRefTime = GenerateNewManagementReferenceTime(newRefTime)
setElectCommitteeAtAndWait(t, ctx, network, 0, newRefTime, 4, 5, 6, 7)
_, txHash := token.Transfer(ctx, 4, 10, 5, 6)
network.WaitForTransactionInNodeState(ctx, txHash, 0)
require.EqualValues(t, 10, token.GetBalance(ctx, 4, 6))
verifyTxSignersAreFromGroup(t, ctx, contract.API, txHash, 4, []int{4, 5, 6, 7})
t.Log("elect 0,1,2,3 - first group returns")
newRefTime = GenerateNewManagementReferenceTime(newRefTime)
setElectCommitteeAtAndWait(t, ctx, network, 4, newRefTime, 0, 1, 2, 3)
_, txHash = token.Transfer(ctx, 3, 10, 5, 6)
network.WaitForTransactionInNodeState(ctx, txHash, 3)
require.EqualValues(t, 20, token.GetBalance(ctx, 3, 6))
verifyTxSignersAreFromGroup(t, ctx, contract.API, txHash, 3, []int{0, 1, 2, 3})
t.Log("test done, shutting down")
})
}
func TestCommitteeElections_GrowingNumberOfElected(t *testing.T) {
NewHarness().
WithNumNodes(7).
WithManagementPollingInterval(50*time.Millisecond).
WithLogFilters(log.DiscardAll()).
WithConsensusAlgos(consensus.CONSENSUS_ALGO_TYPE_LEAN_HELIX).
Start(t, func(t testing.TB, ctx context.Context, network *Network) {
contract := callcontract.NewContractClient(network)
token := network.DeployBenchmarkTokenContract(ctx, 5)
t.Log("elect 0,1,2,3")
newRefTime := GenerateNewManagementReferenceTime(0)
setElectCommitteeAtAndWait(t, ctx, network, 0, newRefTime, 0, 1, 2, 3)
_, txHash := token.Transfer(ctx, 0, 10, 5, 6)
network.WaitForTransactionInNodeState(ctx, txHash, 0)
require.EqualValues(t, 10, token.GetBalance(ctx, 0, 6))
verifyTxSignersAreFromGroup(t, ctx, contract.API, txHash, 0, []int{0, 1, 2, 3})
t.Log("elect 0,1,2,3,4,5,6")
newRefTime = GenerateNewManagementReferenceTime(newRefTime)
setElectCommitteeAtAndWait(t, ctx, network, 0, newRefTime, 0, 1, 2, 3, 4, 5, 6)
_, txHash = token.Transfer(ctx, 4, 10, 5, 6)
network.WaitForTransactionInNodeState(ctx, txHash, 4)
require.EqualValues(t, 20, token.GetBalance(ctx, 4, 6))
verifyTxSignersAreFromGroup(t, ctx, contract.API, txHash, 4, []int{0, 1, 2, 3, 4, 5, 6})
t.Log("test done, shutting down")
})
}
func verifyTxSignersAreFromGroup(t testing.TB, ctx context.Context, api callcontract.CallContractAPI, txHash primitives.Sha256, nodeIndex int, allowedIndexes []int) {
response := api.GetTransactionReceiptProof(ctx, txHash, nodeIndex)
signers, err := digest.GetBlockSignersFromReceiptProof(response.PackedProof())
require.NoError(t, err, "failed getting signers from block proof")
signerIndexes := testKeys.NodeAddressesForTestsToIndexes(signers)
require.Subset(t, allowedIndexes, signerIndexes, "tx signers should be subset of allowed group")
}
func setElectCommitteeAtAndWait(t testing.TB, ctx context.Context, network *Network, currentCommitteeMemberId int, refTime primitives.TimestampSeconds, newCommitteeIds ...int) primitives.BlockHeight {
var committee []primitives.NodeAddress
for _, committeeIndex := range newCommitteeIds {
committee = append(committee, testKeys.EcdsaSecp256K1KeyPairForTests(committeeIndex).NodeAddress())
}
err := network.committeeProvider.AddCommittee(refTime, committee)
require.NoError(t, err)
bh, err := network.WaitForManagementChange(ctx, currentCommitteeMemberId, refTime)
require.NoError(t, err)
return bh
}