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 29, 2024
1 parent b850baa commit d916eb4
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 147 deletions.
4 changes: 2 additions & 2 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ func TestDuplicateProposal(t *testing.T) {
r := int16(0)
p1 := td.makeProposal(t, h, r)
trx := tx.NewTransferTx(h, td.consX.rewardAddr,
td.RandAccAddress(), 1000, 1000, "proposal changer")
td.RandAccAddress(), 1000, 1000, tx.WithMemo("proposal changer"))
td.HelperSignTransaction(td.consX.valKey.PrivateKey(), trx)

assert.NoError(t, td.txPool.AppendTx(trx))
Expand Down Expand Up @@ -913,7 +913,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
26 changes: 13 additions & 13 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, tx.WithMemo("subsidy-test"))

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(), tx.WithMemo("invalid-tx"))

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, tx.WithMemo("valid-tx"))
err := Execute(trx, sb)
assert.NoError(t, err)

Expand All @@ -261,7 +261,7 @@ 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")
ts.RandAccAddress(), ts.RandAmount(), 0, tx.WithMemo("invalid lock-time"))

err := CheckAndExecute(trx, sb, true)
assert.ErrorIs(t, err, LockTimeInFutureError{LockTime: invalidLocoTme})
Expand All @@ -270,7 +270,7 @@ func TestCheck(t *testing.T) {
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")
ts.RandAccAddress(), ts.RandAmount(), invalidFee, tx.WithMemo("invalid fee"))

err := CheckAndExecute(trx, sb, true)
assert.ErrorIs(t, err, InvalidFeeError{Fee: invalidFee, Expected: 0})
Expand All @@ -279,7 +279,7 @@ func TestCheck(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(), tx.WithMemo("invalid-tx"))

err := CheckAndExecute(trx, sb, true)
assert.ErrorIs(t, err, executor.AccountNotFoundError{Address: randAddr})
Expand All @@ -295,7 +295,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, tx.WithMemo("valid-tx"))
err := CheckAndExecute(trx, sb, true)
assert.NoError(t, err)

Expand All @@ -315,7 +315,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
30 changes: 15 additions & 15 deletions execution/executor/bond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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")
receiverAddr, valPub, amt, fee, tx.WithMemo("unknown address"))

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")
receiverAddr, nil, amt, fee, tx.WithMemo("no public key"))

td.check(t, trx, true, ErrPublicKeyNotSet)
td.check(t, trx, false, ErrPublicKeyNotSet)
Expand All @@ -44,15 +44,15 @@ func TestExecuteBondTx(t *testing.T) {
td.sandbox.UpdateValidator(val)

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

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")
receiverAddr, valPub, senderBalance+1, 0, tx.WithMemo("insufficient balance"))

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

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")
receiverAddr, valPub, td.sandbox.TestParams.MinimumStake-1, fee, tx.WithMemo("less than MinimumStake"))

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")
receiverAddr, valPub, td.sandbox.TestParams.MaximumStake+1, fee, tx.WithMemo("more than MaximumStake"))

td.check(t, trx, true, MaximumStakeError{td.sandbox.TestParams.MaximumStake})
td.check(t, trx, false, MaximumStakeError{td.sandbox.TestParams.MaximumStake})
Expand All @@ -89,7 +89,7 @@ func TestExecuteBondTx(t *testing.T) {
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")
pub0.ValidatorAddress(), nil, amt, fee, tx.WithMemo("inside committee"))

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

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, tx.WithMemo("ok"))

td.check(t, trx, true, nil)
td.check(t, trx, false, nil)
Expand All @@ -135,7 +135,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, tx.WithMemo("ok"))

td.execute(t, trx)

Expand All @@ -159,23 +159,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 +184,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
8 changes: 4 additions & 4 deletions execution/executor/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ 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")
receiverAddr, amt, fee, tx.WithMemo("unknown address"))

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")
receiverAddr, senderBalance+1, 0, tx.WithMemo("insufficient balance"))

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")
receiverAddr, amt, fee, tx.WithMemo("ok"))

td.check(t, trx, true, nil)
td.check(t, trx, false, nil)
Expand All @@ -61,7 +61,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, tx.WithMemo("ok"))
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, tx.WithMemo("unknown address"))

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(), tx.WithMemo("inside committee"))

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(), tx.WithMemo("joining committee"))

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, tx.WithMemo("Ok"))

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 d916eb4

Please sign in to comment.