Skip to content

Commit

Permalink
test(types): refactor account and validator tests (pactus-project#1613)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Dec 7, 2024
1 parent ea101fa commit c7ab5d8
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 143 deletions.
84 changes: 42 additions & 42 deletions committee/committee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func TestProposer(t *testing.T) {
func TestInvalidProposerJoinAndLeave(t *testing.T) {
ts := testsuite.NewTestSuite(t)

val1, _ := ts.GenerateTestValidator(0)
val2, _ := ts.GenerateTestValidator(1)
val3, _ := ts.GenerateTestValidator(2)
val4, _ := ts.GenerateTestValidator(3)
val5, _ := ts.GenerateTestValidator(4)
val1 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(0))
val2 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(1))
val3 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(2))
val4 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(3))
val5 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(4))

cmt, err := committee.NewCommittee([]*validator.Validator{val1, val2, val3, val4}, 4, val5.Address())
assert.Error(t, err)
Expand All @@ -50,13 +50,13 @@ func TestInvalidProposerJoinAndLeave(t *testing.T) {
func TestProposerMove(t *testing.T) {
ts := testsuite.NewTestSuite(t)

val1, _ := ts.GenerateTestValidator(1)
val2, _ := ts.GenerateTestValidator(2)
val3, _ := ts.GenerateTestValidator(3)
val4, _ := ts.GenerateTestValidator(4)
val5, _ := ts.GenerateTestValidator(5)
val6, _ := ts.GenerateTestValidator(6)
val7, _ := ts.GenerateTestValidator(7)
val1 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(1))
val2 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(2))
val3 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(3))
val4 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(4))
val5 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(5))
val6 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(6))
val7 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(7))

cmt, err := committee.NewCommittee(
[]*validator.Validator{val1, val2, val3, val4, val5, val6, val7}, 7, val1.Address())
Expand Down Expand Up @@ -97,10 +97,10 @@ func TestProposerMove(t *testing.T) {
func TestValidatorConsistency(t *testing.T) {
ts := testsuite.NewTestSuite(t)

val1, _ := ts.GenerateTestValidator(1)
val2, _ := ts.GenerateTestValidator(2)
val3, _ := ts.GenerateTestValidator(3)
val4, _ := ts.GenerateTestValidator(4)
val1 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(1))
val2 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(2))
val3 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(3))
val4 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(4))

cmt, _ := committee.NewCommittee([]*validator.Validator{val1, val2, val3, val4}, 4, val1.Address())

Expand All @@ -115,13 +115,13 @@ func TestValidatorConsistency(t *testing.T) {
func TestProposerJoin(t *testing.T) {
ts := testsuite.NewTestSuite(t)

val1, _ := ts.GenerateTestValidator(1)
val2, _ := ts.GenerateTestValidator(2)
val3, _ := ts.GenerateTestValidator(3)
val4, _ := ts.GenerateTestValidator(4)
val5, _ := ts.GenerateTestValidator(5)
val6, _ := ts.GenerateTestValidator(6)
val7, _ := ts.GenerateTestValidator(7)
val1 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(1))
val2 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(2))
val3 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(3))
val4 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(4))
val5 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(5))
val6 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(6))
val7 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(7))

cmt, err := committee.NewCommittee([]*validator.Validator{val1, val2, val3, val4}, 7, val1.Address())
assert.NoError(t, err)
Expand Down Expand Up @@ -315,10 +315,10 @@ func TestProposerJoinAndLeave(t *testing.T) {
func TestIsProposer(t *testing.T) {
ts := testsuite.NewTestSuite(t)

val1, _ := ts.GenerateTestValidator(0)
val2, _ := ts.GenerateTestValidator(1)
val3, _ := ts.GenerateTestValidator(2)
val4, _ := ts.GenerateTestValidator(3)
val1 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(0))
val2 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(1))
val3 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(2))
val4 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(3))

cmt, err := committee.NewCommittee([]*validator.Validator{val1, val2, val3, val4}, 4, val1.Address())
assert.NoError(t, err)
Expand All @@ -334,10 +334,10 @@ func TestIsProposer(t *testing.T) {
func TestCommitters(t *testing.T) {
ts := testsuite.NewTestSuite(t)

val1, _ := ts.GenerateTestValidator(0)
val2, _ := ts.GenerateTestValidator(1)
val3, _ := ts.GenerateTestValidator(2)
val4, _ := ts.GenerateTestValidator(3)
val1 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(0))
val2 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(1))
val3 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(2))
val4 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(3))

cmt, err := committee.NewCommittee([]*validator.Validator{val1, val2, val3, val4}, 4, val1.Address())
assert.NoError(t, err)
Expand All @@ -347,13 +347,13 @@ func TestCommitters(t *testing.T) {
func TestSortJoined(t *testing.T) {
ts := testsuite.NewTestSuite(t)

val1, _ := ts.GenerateTestValidator(0)
val2, _ := ts.GenerateTestValidator(1)
val3, _ := ts.GenerateTestValidator(2)
val4, _ := ts.GenerateTestValidator(3)
val5, _ := ts.GenerateTestValidator(4)
val6, _ := ts.GenerateTestValidator(5)
val7, _ := ts.GenerateTestValidator(6)
val1 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(0))
val2 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(1))
val3 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(2))
val4 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(3))
val5 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(4))
val6 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(5))
val7 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(6))

cmt, err := committee.NewCommittee([]*validator.Validator{val1, val2, val3, val4}, 17, val1.Address())
assert.NoError(t, err)
Expand All @@ -369,10 +369,10 @@ func TestTotalPower(t *testing.T) {

pub, _ := ts.RandBLSKeyPair()
val0 := validator.NewValidator(pub, 0) // Bootstrap validator
val1, _ := ts.GenerateTestValidator(0)
val2, _ := ts.GenerateTestValidator(1)
val3, _ := ts.GenerateTestValidator(2)
val4, _ := ts.GenerateTestValidator(3)
val1 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(0))
val2 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(1))
val3 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(2))
val4 := ts.GenerateTestValidator(testsuite.ValidatorWithNumber(3))

cmt, err := committee.NewCommittee([]*validator.Validator{val0, val1, val2, val3, val4}, 4, val1.Address())
assert.NoError(t, err)
Expand Down
10 changes: 5 additions & 5 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,11 @@ func TestCases(t *testing.T) {
round int16
description string
}{
{1697898884837384019, 2, "1/3+ cp:PRE-VOTE in prepare step"},
{1694848907840926239, 0, "1/3+ cp:PRE-VOTE in precommit step"},
{1694849103290580532, 1, "Conflicting votes, cp-round=0"},
{1697900665869342730, 1, "Conflicting votes, cp-round=1"},
{1697887970998950590, 1, "consP & consB: Change Proposer, consX & consY: Commit (2 block announces)"},
{1697898884837384019, 2, "1/3+ cp:PRE-VOTE in Prepare step"},
{1694848907840926239, 2, "1/3+ cp:PRE-VOTE in Precommit step"},
{1732698646319341342, 1, "Conflicting cp:PRE-VOTE in cp_round 0"},
{1732698786369716238, 1, "Conflicting cp:PRE-VOTE in cp_round 1"},
{1732702222972506364, 1, "consX & consY: Change Proposer, consB & consP: Committed block"},
}

for no, tt := range tests {
Expand Down
7 changes: 5 additions & 2 deletions consensus/voteset/voteset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ func setupCommittee(ts *testsuite.TestSuite, stakes ...amount.Amount) (
totalPower := int64(0)
for i, s := range stakes {
pub, prv := ts.RandBLSKeyPair()
val := validator.NewValidator(pub, int32(i))
val.AddToStake(s)
val := ts.GenerateTestValidator(
testsuite.ValidatorWithNumber(int32(i)),
testsuite.ValidatorWithPublicKey(pub),
testsuite.ValidatorWithStake(s),
)
valsMap[val.Address()] = val
totalPower += val.Power()
valKeys = append(valKeys, bls.NewValidatorKey(prv))
Expand Down
11 changes: 7 additions & 4 deletions genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import (
func TestMarshaling(t *testing.T) {
ts := testsuite.NewTestSuite(t)

acc, prv := ts.GenerateTestAccount(0)
acc.AddToBalance(100000)
val, _ := ts.GenerateTestValidator(0)
acc, addr := ts.GenerateTestAccount(
testsuite.AccountWithNumber(0),
testsuite.AccountWithBalance(100000))
val := ts.GenerateTestValidator(
testsuite.ValidatorWithNumber(0),
)
gen1 := genesis.MakeGenesis(util.RoundNow(10),
map[crypto.Address]*account.Account{prv: acc},
map[crypto.Address]*account.Account{addr: acc},
[]*validator.Validator{val}, genesis.DefaultGenesisParams())
gen2 := new(genesis.Genesis)

Expand Down
8 changes: 4 additions & 4 deletions sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestAccountChange(t *testing.T) {
})

t.Run("Retrieve an account from store and update it", func(t *testing.T) {
acc, addr := td.GenerateTestAccount(td.RandInt32(10000))
acc, addr := td.GenerateTestAccount()
bal := acc.Balance()
td.store.UpdateAccount(addr, acc)

Expand Down Expand Up @@ -164,7 +164,7 @@ func TestValidatorChange(t *testing.T) {
})

t.Run("Retrieve an validator from store and update it", func(t *testing.T) {
val, _ := td.GenerateTestValidator(td.RandInt32(10000))
val := td.GenerateTestValidator()
addr := val.Address()
stk := val.Stake()
td.store.UpdateValidator(val)
Expand Down Expand Up @@ -278,14 +278,14 @@ func TestUpdateFromOutsideTheSandbox(t *testing.T) {

t.Run("Try update an account from outside the sandbox, Should panic", func(t *testing.T) {
assert.Panics(t, func() {
acc, addr := td.GenerateTestAccount(td.RandInt32(10000))
acc, addr := td.GenerateTestAccount()
td.sbx.UpdateAccount(addr, acc)
})
})

t.Run("Try update a validator from outside the sandbox, Should panic", func(t *testing.T) {
assert.Panics(t, func() {
val, _ := td.GenerateTestValidator(td.RandInt32(10000))
val := td.GenerateTestValidator()
td.sbx.UpdateValidator(val)
})
})
Expand Down
4 changes: 1 addition & 3 deletions state/lastinfo/last_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/store"
"github.com/pactus-project/pactus/types/block"
Expand Down Expand Up @@ -41,7 +40,6 @@ func setup(t *testing.T) *testData {
pub2, _ := ts.RandBLSKeyPair()
pub3, _ := ts.RandBLSKeyPair()
pub4, prv4 := ts.RandBLSKeyPair()
valKey := bls.NewValidatorKey(prv4)

val0 := validator.NewValidator(pub0, 0)
val1 := validator.NewValidator(pub1, 1)
Expand Down Expand Up @@ -80,7 +78,7 @@ func setup(t *testing.T) *testData {
ts.RandHash(),
prevCert, lastSeed, val2.Address())

sig := valKey.Sign([]byte("fatdog"))
sig := ts.RandBLSSignature()
lastCert := certificate.NewBlockCertificate(lastHeight, 0)
lastCert.SetSignature(committers, []int32{}, sig)
mockStore.SaveBlock(lastBlock, lastCert)
Expand Down
13 changes: 6 additions & 7 deletions store/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/types/account"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAccountCounter(t *testing.T) {
td := setup(t, nil)

num := td.RandInt32(1000)
acc, addr := td.GenerateTestAccount(num)
acc, addr := td.GenerateTestAccount()

t.Run("Add new account, should increase the total accounts number", func(t *testing.T) {
assert.Zero(t, td.store.TotalAccounts())
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestAccountBatchSaving(t *testing.T) {
total := td.RandInt32NonZero(100)
t.Run("Add some accounts", func(t *testing.T) {
for i := int32(0); i < total; i++ {
acc, addr := td.GenerateTestAccount(i)
acc, addr := td.GenerateTestAccount(testsuite.AccountWithNumber(i))
td.store.UpdateAccount(addr, acc)
}
assert.NoError(t, td.store.WriteBatch())
Expand All @@ -69,7 +69,7 @@ func TestAccountByAddress(t *testing.T) {
var lastAddr crypto.Address
t.Run("Add some accounts", func(t *testing.T) {
for i := int32(0); i < total; i++ {
acc, addr := td.GenerateTestAccount(i)
acc, addr := td.GenerateTestAccount(testsuite.AccountWithNumber(i))
td.store.UpdateAccount(addr, acc)

lastAddr = addr
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestIterateAccounts(t *testing.T) {
total := td.RandInt32NonZero(100)
hashes1 := []hash.Hash{}
for i := int32(0); i < total; i++ {
acc, addr := td.GenerateTestAccount(i)
acc, addr := td.GenerateTestAccount(testsuite.AccountWithNumber(i))
td.store.UpdateAccount(addr, acc)
hashes1 = append(hashes1, acc.Hash())
}
Expand Down Expand Up @@ -133,8 +133,7 @@ func TestIterateAccounts(t *testing.T) {
func TestAccountDeepCopy(t *testing.T) {
td := setup(t, nil)

num := td.RandInt32(1000)
acc1, addr := td.GenerateTestAccount(num)
acc1, addr := td.GenerateTestAccount()
td.store.UpdateAccount(addr, acc1)

acc2, _ := td.store.Account(addr)
Expand Down
4 changes: 2 additions & 2 deletions store/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,14 @@ func (*MockStore) WriteBatch() error {
}

func (m *MockStore) AddTestValidator() *validator.Validator {
val, _ := m.ts.GenerateTestValidator(m.ts.RandInt32(10000))
val := m.ts.GenerateTestValidator()
m.UpdateValidator(val)

return val
}

func (m *MockStore) AddTestAccount() (*account.Account, crypto.Address) {
acc, addr := m.ts.GenerateTestAccount(m.ts.RandInt32(10000))
acc, addr := m.ts.GenerateTestAccount()
m.UpdateAccount(addr, acc)

return acc, addr
Expand Down
Loading

0 comments on commit c7ab5d8

Please sign in to comment.