forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_test.go
211 lines (184 loc) · 7.38 KB
/
common_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package committee
import (
"io"
"math/rand"
"testing"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/protocol"
)
type selectionParameterFn func(addr basics.Address) (bool, BalanceRecord, Seed, basics.MicroAlgos)
type selectionParameterListFn func(addr []basics.Address) (bool, []BalanceRecord, Seed, basics.MicroAlgos)
var proto = config.Consensus[protocol.ConsensusCurrentVersion]
func newAccount(t testing.TB, gen io.Reader, latest basics.Round, keyBatchesForward uint) (basics.Address, *crypto.SignatureSecrets, *crypto.VrfPrivkey, *crypto.OneTimeSignatureSecrets) {
var seed crypto.Seed
gen.Read(seed[:])
s := crypto.GenerateSignatureSecrets(seed)
_, v := crypto.VrfKeygenFromSeed(seed)
o := crypto.GenerateOneTimeSignatureSecrets(basics.OneTimeIDForRound(latest, proto.DefaultKeyDilution).Batch, uint64(keyBatchesForward))
addr := basics.Address(s.SignatureVerifier)
return addr, s, &v, o
}
func signTx(s *crypto.SignatureSecrets, t transactions.Transaction) transactions.SignedTxn {
return t.Sign(s)
}
// testingenv creates a random set of participating accounts and random transactions between them, and
// the associated selection parameters for use testing committee membership and credential validation.
// seedGen is provided as an external source of randomness for the selection seed and transaction notes;
// if the caller persists seedGen between calls to testingenv, each iteration that calls testingenv will
// exercise a new selection seed.
func testingenv(t testing.TB, numAccounts, numTxs int, seedGen io.Reader) (selectionParameterFn, selectionParameterListFn, basics.Round, []basics.Address, []*crypto.SignatureSecrets, []*crypto.VrfPrivkey, []*crypto.OneTimeSignatureSecrets, []transactions.SignedTxn) {
return testingenvMoreKeys(t, numAccounts, numTxs, uint(5), seedGen)
}
func testingenvMoreKeys(t testing.TB, numAccounts, numTxs int, keyBatchesForward uint, seedGen io.Reader) (selectionParameterFn, selectionParameterListFn, basics.Round, []basics.Address, []*crypto.SignatureSecrets, []*crypto.VrfPrivkey, []*crypto.OneTimeSignatureSecrets, []transactions.SignedTxn) {
if seedGen == nil {
seedGen = rand.New(rand.NewSource(1)) // same source as setting GODEBUG=randautoseed=0, same as pre-Go 1.20 default seed
}
P := numAccounts // n accounts
TXs := numTxs // n txns
maxMoneyAtStart := 100000 // max money start
minMoneyAtStart := 10000 // max money start
transferredMoney := 100 // max money/txn
maxFee := 10 // max maxFee/txn
E := basics.Round(50) // max round
// generate accounts
genesis := make(map[basics.Address]basics.AccountData)
gen := rand.New(rand.NewSource(2))
addrs := make([]basics.Address, P)
secrets := make([]*crypto.SignatureSecrets, P)
vrfSecrets := make([]*crypto.VrfPrivkey, P)
otSecrets := make([]*crypto.OneTimeSignatureSecrets, P)
proto := config.Consensus[protocol.ConsensusCurrentVersion]
lookback := basics.Round(2*proto.SeedRefreshInterval + proto.SeedLookback + 1)
var total basics.MicroAlgos
for i := 0; i < P; i++ {
addr, sigSec, vrfSec, otSec := newAccount(t, gen, lookback, keyBatchesForward)
addrs[i] = addr
secrets[i] = sigSec
vrfSecrets[i] = vrfSec
otSecrets[i] = otSec
startamt := uint64(minMoneyAtStart + (gen.Int() % (maxMoneyAtStart - minMoneyAtStart)))
short := addr
genesis[short] = basics.AccountData{
Status: basics.Online,
MicroAlgos: basics.MicroAlgos{Raw: startamt},
SelectionID: vrfSec.Pubkey(),
VoteID: otSec.OneTimeSignatureVerifier,
}
total.Raw += startamt
}
var seed Seed
seedGen.Read(seed[:])
tx := make([]transactions.SignedTxn, TXs)
for i := 0; i < TXs; i++ {
send := gen.Int() % P
recv := gen.Int() % P
saddr := addrs[send]
raddr := addrs[recv]
amt := basics.MicroAlgos{Raw: uint64(gen.Int() % transferredMoney)}
fee := basics.MicroAlgos{Raw: uint64(gen.Int() % maxFee)}
t := transactions.Transaction{
Type: protocol.PaymentTx,
Header: transactions.Header{
Sender: saddr,
Fee: fee,
FirstValid: 0,
LastValid: E,
Note: make([]byte, 4),
},
PaymentTxnFields: transactions.PaymentTxnFields{
Receiver: raddr,
Amount: amt,
},
}
seedGen.Read(t.Note) // to match output from previous versions, which shared global RNG for seed & note
tx[i] = t.Sign(secrets[send])
}
selParams := func(addr basics.Address) (bool, BalanceRecord, Seed, basics.MicroAlgos) {
data, ok := genesis[addr]
if !ok {
return false, BalanceRecord{}, Seed{}, basics.MicroAlgos{Raw: 0}
}
return true, BalanceRecord{Addr: addr, OnlineAccountData: data.OnlineAccountData()}, seed, total
}
selParamsList := func(addrs []basics.Address) (ok bool, records []BalanceRecord, seed Seed, total basics.MicroAlgos) {
records = make([]BalanceRecord, len(addrs))
for i, addr := range addrs {
var record BalanceRecord
ok, record, seed, total = selParams(addr)
if !ok {
return false, nil, Seed{}, basics.MicroAlgos{Raw: 0}
}
records[i] = record
}
ok = true
return
}
return selParams, selParamsList, lookback, addrs, secrets, vrfSecrets, otSecrets, tx
}
/* TODO deprecate these types after they have been removed successfully */
type (
// Step is a sequence number denoting distinct stages in Algorand
Step uint64
// Period is used to track progress with a given round in the protocol
Period uint64
// RoundPeriod represents a specific Period in a specific Round of the protocol
RoundPeriod struct {
basics.Round
Period
}
)
// An AgreementSelector is the input used to define
// proposers and members of voting committees.
type AgreementSelector struct {
Seed Seed `codec:"seed"`
Round basics.Round `codec:"rnd"`
Period Period `codec:"per"`
Step Step `codec:"step"`
}
// ToBeHashed implements the crypto.Hashable interface.
func (sel AgreementSelector) ToBeHashed() (protocol.HashID, []byte) {
return protocol.AgreementSelector, protocol.EncodeReflect(&sel)
}
// CommitteeSize returns the size of the committee,
// which is determined by AgreementSelector.Step.
func (sel AgreementSelector) CommitteeSize(proto config.ConsensusParams) uint64 {
return sel.Step.CommitteeSize(proto)
}
// Algorand 2.0 steps
const (
Propose Step = iota
Soft
Cert
Next
)
// CommitteeSize returns the size of the committee required for the Step
func (s Step) CommitteeSize(proto config.ConsensusParams) uint64 {
switch s {
case Propose:
return proto.NumProposers
case Soft:
return proto.SoftCommitteeSize
case Cert:
return proto.CertCommitteeSize
default:
return proto.NextCommitteeSize
}
}