Skip to content

Commit

Permalink
break dependency on transactions package
Browse files Browse the repository at this point in the history
  • Loading branch information
jannotti committed Oct 15, 2024
1 parent 6f3f3c1 commit 018c8e2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 64 deletions.
65 changes: 15 additions & 50 deletions data/committee/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"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"
)

Expand All @@ -33,98 +32,64 @@ type selectionParameterListFn func(addr []basics.Address) (bool, []BalanceRecord

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) {
func newAccount(t testing.TB, gen io.Reader, latest basics.Round, keyBatchesForward uint) (basics.Address, *crypto.SignatureSecrets, *crypto.VrfPrivkey) {
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
return addr, s, &v
}

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) {
// testingenv creates a random set of participating accounts 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; if the caller persists seedGen between calls to testingenv,
// each iteration that calls testingenv will exercise a new selection seed.
// formerly, testingenv, generated transactions and one-time secrets as well,
// but they were not used by the tests.
func testingenv(t testing.TB, numAccounts, numTxs int, seedGen io.Reader) (selectionParameterFn, selectionParameterListFn, basics.Round, []basics.Address, []*crypto.SignatureSecrets, []*crypto.VrfPrivkey) {
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) {
func testingenvMoreKeys(t testing.TB, numAccounts, numTxs int, keyBatchesForward uint, seedGen io.Reader) (selectionParameterFn, selectionParameterListFn, basics.Round, []basics.Address, []*crypto.SignatureSecrets, []*crypto.VrfPrivkey) {
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)
addr, sigSec, vrfSec := 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])
for i := 0; i < numTxs; i++ {
seedGen.Read(make([]byte, 4)) // to match output from previous versions, which shared global RNG for seed & note
}

selParams := func(addr basics.Address) (bool, BalanceRecord, Seed, basics.MicroAlgos) {
Expand All @@ -149,7 +114,7 @@ func testingenvMoreKeys(t testing.TB, numAccounts, numTxs int, keyBatchesForward
return
}

return selParams, selParamsList, lookback, addrs, secrets, vrfSecrets, otSecrets, tx
return selParams, selParamsList, lookback, addrs, secrets, vrfSecrets
}

/* TODO deprecate these types after they have been removed successfully */
Expand Down
20 changes: 10 additions & 10 deletions data/committee/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAccountSelected(t *testing.T) {
seedGen := rand.New(rand.NewSource(1))
N := 1
for i := 0; i < N; i++ {
selParams, _, round, addresses, _, vrfSecrets, _, _ := testingenv(t, 100, 2000, seedGen)
selParams, _, round, addresses, _, vrfSecrets := testingenv(t, 100, 2000, seedGen)
period := Period(0)

leaders := uint64(0)
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestAccountSelected(t *testing.T) {
func TestRichAccountSelected(t *testing.T) {
partitiontest.PartitionTest(t)

selParams, _, round, addresses, _, vrfSecrets, _, _ := testingenv(t, 10, 2000, nil)
selParams, _, round, addresses, _, vrfSecrets := testingenv(t, 10, 2000, nil)

period := Period(0)
ok, record, selectionSeed, _ := selParams(addresses[0])
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestPoorAccountSelectedLeaders(t *testing.T) {
failsLeaders := 0
leaders := make([]uint64, N)
for i := 0; i < N; i++ {
selParams, _, round, addresses, _, vrfSecrets, _, _ := testingenv(t, 100, 2000, seedGen)
selParams, _, round, addresses, _, vrfSecrets := testingenv(t, 100, 2000, seedGen)
period := Period(0)
for j := range addresses {
ok, record, selectionSeed, _ := selParams(addresses[j])
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestPoorAccountSelectedCommittee(t *testing.T) {
N := 1
committee := uint64(0)
for i := 0; i < N; i++ {
selParams, _, round, addresses, _, vrfSecrets, _, _ := testingenv(t, 100, 2000, seedGen)
selParams, _, round, addresses, _, vrfSecrets := testingenv(t, 100, 2000, seedGen)
period := Period(0)

step := Cert
Expand Down Expand Up @@ -250,10 +250,10 @@ func TestNoMoneyAccountNotSelected(t *testing.T) {
seedGen := rand.New(rand.NewSource(1))
N := 1
for i := 0; i < N; i++ {
selParams, _, round, addresses, _, _, _, _ := testingenv(t, 10, 2000, seedGen)
selParams, _, round, addresses, _, _ := testingenv(t, 10, 2000, seedGen)
lookback := basics.Round(2*proto.SeedRefreshInterval + proto.SeedLookback + 1)
gen := rand.New(rand.NewSource(2))
_, _, zeroVRFSecret, _ := newAccount(t, gen, lookback, 5)
_, _, zeroVRFSecret := newAccount(t, gen, lookback, 5)
period := Period(0)
ok, record, selectionSeed, _ := selParams(addresses[i])
if !ok {
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestNoMoneyAccountNotSelected(t *testing.T) {
func TestLeadersSelected(t *testing.T) {
partitiontest.PartitionTest(t)

selParams, _, round, addresses, _, vrfSecrets, _, _ := testingenv(t, 100, 2000, nil)
selParams, _, round, addresses, _, vrfSecrets := testingenv(t, 100, 2000, nil)

period := Period(0)
step := Propose
Expand Down Expand Up @@ -313,7 +313,7 @@ func TestLeadersSelected(t *testing.T) {
func TestCommitteeSelected(t *testing.T) {
partitiontest.PartitionTest(t)

selParams, _, round, addresses, _, vrfSecrets, _, _ := testingenv(t, 100, 2000, nil)
selParams, _, round, addresses, _, vrfSecrets := testingenv(t, 100, 2000, nil)

period := Period(0)
step := Soft
Expand Down Expand Up @@ -345,7 +345,7 @@ func TestCommitteeSelected(t *testing.T) {
func TestAccountNotSelected(t *testing.T) {
partitiontest.PartitionTest(t)

selParams, _, round, addresses, _, vrfSecrets, _, _ := testingenv(t, 100, 2000, nil)
selParams, _, round, addresses, _, vrfSecrets := testingenv(t, 100, 2000, nil)
period := Period(0)
leaders := uint64(0)
for i := range addresses {
Expand Down Expand Up @@ -375,7 +375,7 @@ func TestAccountNotSelected(t *testing.T) {

// TODO update to remove VRF verification overhead
func BenchmarkSortition(b *testing.B) {
selParams, _, round, addresses, _, vrfSecrets, _, _ := testingenv(b, 100, 2000, nil)
selParams, _, round, addresses, _, vrfSecrets := testingenv(b, 100, 2000, nil)

period := Period(0)
step := Soft
Expand Down
8 changes: 4 additions & 4 deletions ledger/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1676,24 +1676,24 @@ func TestActiveChallenge(t *testing.T) {
rules := config.Consensus[nowHeader.CurrentProtocol].Payouts

// simplest test. when interval=X and grace=G, X+G+1 is a challenge
inChallenge := rules.ChallengeInterval + rules.ChallengeGracePeriod + 1
inChallenge := basics.Round(rules.ChallengeInterval + rules.ChallengeGracePeriod + 1)
ch := ActiveChallenge(rules, inChallenge, singleSource(nowHeader))
a.NotZero(ch.round)

// all rounds before that have no challenge
for r := uint64(1); r < inChallenge; r++ {
for r := basics.Round(1); r < inChallenge; r++ {
ch := ActiveChallenge(rules, r, singleSource(nowHeader))
a.Zero(ch.round, r)
}

// ChallengeGracePeriod rounds allow challenges starting with inChallenge
for r := inChallenge; r < inChallenge+rules.ChallengeGracePeriod; r++ {
for r := inChallenge; r < inChallenge+basics.Round(rules.ChallengeGracePeriod); r++ {
ch := ActiveChallenge(rules, r, singleSource(nowHeader))
a.EqualValues(ch.round, rules.ChallengeInterval)
}

// And the next round is again challenge-less
ch = ActiveChallenge(rules, inChallenge+rules.ChallengeGracePeriod, singleSource(nowHeader))
ch = ActiveChallenge(rules, inChallenge+basics.Round(rules.ChallengeGracePeriod), singleSource(nowHeader))
a.Zero(ch.round)

// ignore challenge if upgrade happened
Expand Down

0 comments on commit 018c8e2

Please sign in to comment.