Skip to content

Commit

Permalink
refactor(types): using options pattern for memo parameter on new tx f…
Browse files Browse the repository at this point in the history
…unctions
  • Loading branch information
maxipaz committed Jul 30, 2024
1 parent b850baa commit e3d4cbf
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 147 deletions.
5 changes: 2 additions & 3 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,7 @@ func TestDuplicateProposal(t *testing.T) {
h := uint32(4)
r := int16(0)
p1 := td.makeProposal(t, h, r)
trx := tx.NewTransferTx(h, td.consX.rewardAddr,
td.RandAccAddress(), 1000, 1000, "proposal changer")
trx := tx.NewTransferTx(h, td.consX.rewardAddr, td.RandAccAddress(), 1000, 1000)
td.HelperSignTransaction(td.consX.valKey.PrivateKey(), trx)

assert.NoError(t, td.txPool.AppendTx(trx))
Expand Down Expand Up @@ -913,7 +912,7 @@ func TestByzantine(t *testing.T) {
// P votes
// Byzantine node create the second proposal and send it to the partitioned node P
byzTrx := tx.NewTransferTx(h,
td.consB.rewardAddr, td.RandAccAddress(), 1000, 1000, "")
td.consB.rewardAddr, td.RandAccAddress(), 1000, 1000)
td.HelperSignTransaction(td.consB.valKey.PrivateKey(), byzTrx)
assert.NoError(t, td.txPool.AppendTx(byzTrx))
p2 := td.makeProposal(t, h, r)
Expand Down
2 changes: 1 addition & 1 deletion consensus/precommit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestPrecommitDuplicatedProposal(t *testing.T) {

p1 := td.makeProposal(t, h, r)
trx := tx.NewTransferTx(h, td.consX.rewardAddr,
td.RandAccAddress(), 1000, 1000, "invalid proposal")
td.RandAccAddress(), 1000, 1000, tx.WithMemo("invalid proposal"))
td.HelperSignTransaction(td.consX.valKey.PrivateKey(), trx)

assert.NoError(t, td.txPool.AppendTx(trx))
Expand Down
29 changes: 13 additions & 16 deletions execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestTransferLockTime(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
trx := tx.NewTransferTx(tc.lockTime, rndAccAddr, ts.RandAccAddress(), 1000, 1000, "")
trx := tx.NewTransferTx(tc.lockTime, rndAccAddr, ts.RandAccAddress(), 1000, 1000)
ts.HelperSignTransaction(rndPrvKey, trx)

strictErr := CheckLockTime(trx, sb, true)
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestSubsidyLockTime(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
trx := tx.NewSubsidyTx(tc.lockTime, ts.RandAccAddress(), 1000, "subsidy-test")
trx := tx.NewSubsidyTx(tc.lockTime, ts.RandAccAddress(), 1000)

strictErr := CheckLockTime(trx, sb, true)
assert.ErrorIs(t, strictErr, tc.strictErr)
Expand All @@ -195,25 +195,25 @@ func TestCheckFee(t *testing.T) {
{
name: "Subsidy transaction with fee",
trx: tx.NewTransferTx(ts.RandHeight(), crypto.TreasuryAddress, ts.RandAccAddress(),
ts.RandAmount(), 1, ""),
ts.RandAmount(), 1),
expectedErr: InvalidFeeError{Fee: 1, Expected: 0},
},
{
name: "Subsidy transaction without fee",
trx: tx.NewTransferTx(ts.RandHeight(), crypto.TreasuryAddress, ts.RandAccAddress(),
ts.RandAmount(), 0, ""),
ts.RandAmount(), 0),
expectedErr: nil,
},
{
name: "Transfer transaction with fee",
trx: tx.NewTransferTx(ts.RandHeight(), ts.RandAccAddress(), ts.RandAccAddress(),
ts.RandAmount(), 0, ""),
ts.RandAmount(), 0),
expectedErr: nil,
},
{
name: "Transfer transaction without fee",
trx: tx.NewTransferTx(ts.RandHeight(), ts.RandAccAddress(), ts.RandAccAddress(),
ts.RandAmount(), 0, ""),
ts.RandAmount(), 0),
expectedErr: nil,
},
}
Expand All @@ -236,14 +236,14 @@ func TestExecute(t *testing.T) {
t.Run("Invalid transaction, Should return error", func(t *testing.T) {
randAddr := ts.RandAccAddress()
trx := tx.NewTransferTx(lockTime, randAddr, ts.RandAccAddress(),
ts.RandAmount(), ts.RandFee(), "invalid-tx")
ts.RandAmount(), ts.RandFee())

err := Execute(trx, sb)
assert.ErrorIs(t, err, executor.AccountNotFoundError{Address: randAddr})
})

t.Run("Ok", func(t *testing.T) {
trx := tx.NewSubsidyTx(lockTime, ts.RandAccAddress(), 1000, "valid-tx")
trx := tx.NewSubsidyTx(lockTime, ts.RandAccAddress(), 1000)
err := Execute(trx, sb)
assert.NoError(t, err)

Expand All @@ -260,26 +260,23 @@ func TestCheck(t *testing.T) {

t.Run("Invalid lock-time, Should return error", func(t *testing.T) {
invalidLocoTme := lockTime + 1
trx := tx.NewTransferTx(invalidLocoTme, crypto.TreasuryAddress,
ts.RandAccAddress(), ts.RandAmount(), 0, "invalid lock-time")
trx := tx.NewTransferTx(invalidLocoTme, crypto.TreasuryAddress, ts.RandAccAddress(), ts.RandAmount(), 0)

err := CheckAndExecute(trx, sb, true)
assert.ErrorIs(t, err, LockTimeInFutureError{LockTime: invalidLocoTme})
})

t.Run("Invalid fee, Should return error", func(t *testing.T) {
invalidFee := amount.Amount(1)
trx := tx.NewTransferTx(lockTime, crypto.TreasuryAddress,
ts.RandAccAddress(), ts.RandAmount(), invalidFee, "invalid fee")
trx := tx.NewTransferTx(lockTime, crypto.TreasuryAddress, ts.RandAccAddress(), ts.RandAmount(), invalidFee)

err := CheckAndExecute(trx, sb, true)
assert.ErrorIs(t, err, InvalidFeeError{Fee: invalidFee, Expected: 0})
})

t.Run("Invalid transaction, Should return error", func(t *testing.T) {
randAddr := ts.RandAccAddress()
trx := tx.NewTransferTx(lockTime, randAddr, ts.RandAccAddress(),
ts.RandAmount(), ts.RandFee(), "invalid-tx")
trx := tx.NewTransferTx(lockTime, randAddr, ts.RandAccAddress(), ts.RandAmount(), ts.RandFee())

err := CheckAndExecute(trx, sb, true)
assert.ErrorIs(t, err, executor.AccountNotFoundError{Address: randAddr})
Expand All @@ -295,7 +292,7 @@ func TestCheck(t *testing.T) {
})

t.Run("Ok", func(t *testing.T) {
trx := tx.NewSubsidyTx(lockTime, ts.RandAccAddress(), 1000, "valid-tx")
trx := tx.NewSubsidyTx(lockTime, ts.RandAccAddress(), 1000)
err := CheckAndExecute(trx, sb, true)
assert.NoError(t, err)

Expand All @@ -315,7 +312,7 @@ func TestReplay(t *testing.T) {
lockTime := sb.CurrentHeight()

trx := tx.NewTransferTx(lockTime,
rndAccAddr, ts.RandAccAddress(), 10000, 1000, "")
rndAccAddr, ts.RandAccAddress(), 10000, 1000)
ts.HelperSignTransaction(rndPrvKey, trx)

err := Execute(trx, sb)
Expand Down
39 changes: 15 additions & 24 deletions execution/executor/bond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ func TestExecuteBondTx(t *testing.T) {

t.Run("Should fail, unknown address", func(t *testing.T) {
randomAddr := td.RandAccAddress()
trx := tx.NewBondTx(lockTime, randomAddr,
receiverAddr, valPub, amt, fee, "unknown address")
trx := tx.NewBondTx(lockTime, randomAddr, receiverAddr, valPub, amt, fee)

td.check(t, trx, true, AccountNotFoundError{Address: randomAddr})
td.check(t, trx, false, AccountNotFoundError{Address: randomAddr})
})

t.Run("Should fail, public key is not set", func(t *testing.T) {
trx := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, nil, amt, fee, "no public key")
trx := tx.NewBondTx(lockTime, senderAddr, receiverAddr, nil, amt, fee)

td.check(t, trx, true, ErrPublicKeyNotSet)
td.check(t, trx, false, ErrPublicKeyNotSet)
Expand All @@ -43,16 +41,14 @@ func TestExecuteBondTx(t *testing.T) {
val := td.sandbox.MakeNewValidator(randPub)
td.sandbox.UpdateValidator(val)

trx := tx.NewBondTx(lockTime, senderAddr,
randPub.ValidatorAddress(), randPub, amt, fee, "with public key")
trx := tx.NewBondTx(lockTime, senderAddr, randPub.ValidatorAddress(), randPub, amt, fee)

td.check(t, trx, true, ErrPublicKeyAlreadySet)
td.check(t, trx, false, ErrPublicKeyAlreadySet)
})

t.Run("Should fail, insufficient balance", func(t *testing.T) {
trx := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, valPub, senderBalance+1, 0, "insufficient balance")
trx := tx.NewBondTx(lockTime, senderAddr, receiverAddr, valPub, senderBalance+1, 0)

td.check(t, trx, true, ErrInsufficientFunds)
td.check(t, trx, false, ErrInsufficientFunds)
Expand All @@ -63,33 +59,29 @@ func TestExecuteBondTx(t *testing.T) {
val := td.sandbox.MakeNewValidator(randPub)
val.UpdateUnbondingHeight(td.RandHeight())
td.sandbox.UpdateValidator(val)
trx := tx.NewBondTx(lockTime, senderAddr,
randPub.ValidatorAddress(), nil, amt, fee, "unbonded before")
trx := tx.NewBondTx(lockTime, senderAddr, randPub.ValidatorAddress(), nil, amt, fee)

td.check(t, trx, true, ErrValidatorUnbonded)
td.check(t, trx, false, ErrValidatorUnbonded)
})

t.Run("Should fail, amount less than MinimumStake", func(t *testing.T) {
trx := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, valPub, td.sandbox.TestParams.MinimumStake-1, fee, "less than MinimumStake")
trx := tx.NewBondTx(lockTime, senderAddr, receiverAddr, valPub, td.sandbox.TestParams.MinimumStake-1, fee)

td.check(t, trx, true, SmallStakeError{td.sandbox.TestParams.MinimumStake})
td.check(t, trx, false, SmallStakeError{td.sandbox.TestParams.MinimumStake})
})

t.Run("Should fail, validator's stake exceeds the MaximumStake", func(t *testing.T) {
trx := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, valPub, td.sandbox.TestParams.MaximumStake+1, fee, "more than MaximumStake")
trx := tx.NewBondTx(lockTime, senderAddr, receiverAddr, valPub, td.sandbox.TestParams.MaximumStake+1, fee)

td.check(t, trx, true, MaximumStakeError{td.sandbox.TestParams.MaximumStake})
td.check(t, trx, false, MaximumStakeError{td.sandbox.TestParams.MaximumStake})
})

t.Run("Should fail, inside committee", func(t *testing.T) {
pub0 := td.sandbox.Committee().Proposer(0).PublicKey()
trx := tx.NewBondTx(lockTime, senderAddr,
pub0.ValidatorAddress(), nil, amt, fee, "inside committee")
trx := tx.NewBondTx(lockTime, senderAddr, pub0.ValidatorAddress(), nil, amt, fee)

td.check(t, trx, true, ErrValidatorInCommittee)
td.check(t, trx, false, nil)
Expand All @@ -100,15 +92,14 @@ func TestExecuteBondTx(t *testing.T) {
val := td.sandbox.MakeNewValidator(randPub)
td.sandbox.UpdateValidator(val)
td.sandbox.JoinedToCommittee(val.Address())
trx := tx.NewBondTx(lockTime, senderAddr,
randPub.ValidatorAddress(), nil, amt, fee, "inside committee")
trx := tx.NewBondTx(lockTime, senderAddr, randPub.ValidatorAddress(), nil, amt, fee)

td.check(t, trx, true, ErrValidatorInCommittee)
td.check(t, trx, false, nil)
})

t.Run("Ok", func(t *testing.T) {
trx := tx.NewBondTx(lockTime, senderAddr, receiverAddr, valPub, amt, fee, "ok")
trx := tx.NewBondTx(lockTime, senderAddr, receiverAddr, valPub, amt, fee)

td.check(t, trx, true, nil)
td.check(t, trx, false, nil)
Expand All @@ -135,7 +126,7 @@ func TestPowerDeltaBond(t *testing.T) {
td.sandbox.TestParams.MaximumStake)
fee := td.RandFee()
lockTime := td.sandbox.CurrentHeight()
trx := tx.NewBondTx(lockTime, senderAddr, receiverAddr, pub, amt, fee, "ok")
trx := tx.NewBondTx(lockTime, senderAddr, receiverAddr, pub, amt, fee)

td.execute(t, trx)

Expand All @@ -159,23 +150,23 @@ func TestSmallBond(t *testing.T) {

t.Run("Rejects bond transaction with zero amount", func(t *testing.T) {
trx := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, nil, 0, fee, "attacking validator")
receiverAddr, nil, 0, fee, tx.WithMemo("attacking validator"))

td.check(t, trx, true, SmallStakeError{td.sandbox.TestParams.MinimumStake})
td.check(t, trx, false, SmallStakeError{td.sandbox.TestParams.MinimumStake})
})

t.Run("Rejects bond transaction below full validator stake", func(t *testing.T) {
trx := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, nil, 1, fee, "attacking validator")
receiverAddr, nil, 1, fee, tx.WithMemo("attacking validator"))

td.check(t, trx, true, SmallStakeError{td.sandbox.TestParams.MinimumStake})
td.check(t, trx, false, SmallStakeError{td.sandbox.TestParams.MinimumStake})
})

t.Run("Accepts bond transaction reaching full validator stake", func(t *testing.T) {
trx := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, nil, 2, fee, "fulfilling validator stake")
receiverAddr, nil, 2, fee, tx.WithMemo("fulfilling validator stake"))

td.check(t, trx, true, nil)
td.check(t, trx, false, nil)
Expand All @@ -184,7 +175,7 @@ func TestSmallBond(t *testing.T) {

t.Run("Rejects bond transaction with zero amount on full validator", func(t *testing.T) {
trx := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, nil, 0, fee, "attacking validator")
receiverAddr, nil, 0, fee, tx.WithMemo("attacking validator"))

td.check(t, trx, true, SmallStakeError{td.sandbox.TestParams.MinimumStake})
td.check(t, trx, false, SmallStakeError{td.sandbox.TestParams.MinimumStake})
Expand Down
11 changes: 4 additions & 7 deletions execution/executor/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,21 @@ func TestExecuteTransferTx(t *testing.T) {

t.Run("Should fail, unknown address", func(t *testing.T) {
randomAddr := td.RandAccAddress()
trx := tx.NewTransferTx(lockTime, randomAddr,
receiverAddr, amt, fee, "unknown address")
trx := tx.NewTransferTx(lockTime, randomAddr, receiverAddr, amt, fee)

td.check(t, trx, true, AccountNotFoundError{Address: randomAddr})
td.check(t, trx, false, AccountNotFoundError{Address: randomAddr})
})

t.Run("Should fail, insufficient balance", func(t *testing.T) {
trx := tx.NewTransferTx(lockTime, senderAddr,
receiverAddr, senderBalance+1, 0, "insufficient balance")
trx := tx.NewTransferTx(lockTime, senderAddr, receiverAddr, senderBalance+1, 0)

td.check(t, trx, true, ErrInsufficientFunds)
td.check(t, trx, false, ErrInsufficientFunds)
})

t.Run("Ok", func(t *testing.T) {
trx := tx.NewTransferTx(lockTime, senderAddr,
receiverAddr, amt, fee, "ok")
trx := tx.NewTransferTx(lockTime, senderAddr, receiverAddr, amt, fee)

td.check(t, trx, true, nil)
td.check(t, trx, false, nil)
Expand All @@ -61,7 +58,7 @@ func TestTransferToSelf(t *testing.T) {
fee := td.RandFee()
lockTime := td.sandbox.CurrentHeight()

trx := tx.NewTransferTx(lockTime, senderAddr, senderAddr, amt, fee, "ok")
trx := tx.NewTransferTx(lockTime, senderAddr, senderAddr, amt, fee)
td.check(t, trx, true, nil)
td.check(t, trx, false, nil)
td.execute(t, trx)
Expand Down
12 changes: 6 additions & 6 deletions execution/executor/unbond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func TestExecuteUnbondTx(t *testing.T) {

t.Run("Should fail, unknown address", func(t *testing.T) {
randomAddr := td.RandValAddress()
trx := tx.NewUnbondTx(lockTime, randomAddr, "unknown address")
trx := tx.NewUnbondTx(lockTime, randomAddr)

td.check(t, trx, true, ValidatorNotFoundError{Address: randomAddr})
td.check(t, trx, false, ValidatorNotFoundError{Address: randomAddr})
})

t.Run("Should fail, inside committee", func(t *testing.T) {
val0 := td.sandbox.Committee().Proposer(0)
trx := tx.NewUnbondTx(lockTime, val0.Address(), "inside committee")
trx := tx.NewUnbondTx(lockTime, val0.Address())

td.check(t, trx, true, ErrValidatorInCommittee)
td.check(t, trx, false, nil)
Expand All @@ -46,22 +46,22 @@ func TestExecuteUnbondTx(t *testing.T) {
randVal := td.sandbox.MakeNewValidator(randPub)
td.sandbox.UpdateValidator(randVal)
td.sandbox.JoinedToCommittee(randVal.Address())
trx := tx.NewUnbondTx(lockTime, randPub.ValidatorAddress(), "joining committee")
trx := tx.NewUnbondTx(lockTime, randPub.ValidatorAddress())

td.check(t, trx, true, ErrValidatorInCommittee)
td.check(t, trx, false, nil)
})

t.Run("Ok", func(t *testing.T) {
trx := tx.NewUnbondTx(lockTime, valAddr, "Ok")
trx := tx.NewUnbondTx(lockTime, valAddr)

td.check(t, trx, true, nil)
td.check(t, trx, false, nil)
td.execute(t, trx)
})

t.Run("Should fail, Cannot unbond if already unbonded", func(t *testing.T) {
trx := tx.NewUnbondTx(lockTime, valAddr, "Ok")
trx := tx.NewUnbondTx(lockTime, valAddr, tx.WithMemo("Ok"))

td.check(t, trx, true, ErrValidatorUnbonded)
td.check(t, trx, false, ErrValidatorUnbonded)
Expand All @@ -87,7 +87,7 @@ func TestPowerDeltaUnbond(t *testing.T) {
val.AddToStake(amt)
td.sandbox.UpdateValidator(val)
lockTime := td.sandbox.CurrentHeight()
trx := tx.NewUnbondTx(lockTime, valAddr, "Ok")
trx := tx.NewUnbondTx(lockTime, valAddr, tx.WithMemo("Ok"))

td.execute(t, trx)

Expand Down
Loading

0 comments on commit e3d4cbf

Please sign in to comment.