From 2005d97388238f7709491cb3ef51b66c5cfecb4b Mon Sep 17 00:00:00 2001 From: kehiy Date: Wed, 5 Jul 2023 04:48:52 -0500 Subject: [PATCH 1/7] feat: MinimumStake amount --- execution/executor/bond.go | 3 +++ execution/executor/bond_test.go | 8 ++++++++ types/param/param.go | 2 ++ 3 files changed, 13 insertions(+) diff --git a/execution/executor/bond.go b/execution/executor/bond.go index b81630559..9ec06b44c 100644 --- a/execution/executor/bond.go +++ b/execution/executor/bond.go @@ -70,6 +70,9 @@ func (e *BondExecutor) Execute(trx *tx.Tx, sb sandbox.Sandbox) error { if receiverVal.Stake()+pld.Stake > sb.Params().MaximumStake { return errors.Errorf(errors.ErrInvalidTx, "validator's stake can't be more than %v", sb.Params().MaximumStake) + } else if pld.Stake < sb.Params().MinimumStake { + return errors.Errorf(errors.ErrInvalidTx, + "validator's stake can't be less than %v", sb.Params().MinimumStake) } senderAcc.IncSequence() diff --git a/execution/executor/bond_test.go b/execution/executor/bond_test.go index 97bc14130..0a4908d5a 100644 --- a/execution/executor/bond_test.go +++ b/execution/executor/bond_test.go @@ -88,6 +88,14 @@ func TestExecuteBondTx(t *testing.T) { assert.Error(t, exe.Execute(trx, td.sandbox), "Execute again, should fail") }) + t.Run("Should fail, amount less than MinimumStake", func(t *testing.T) { + trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + receiverAddr, pub, 1000, fee, "less than MinimumStake") + + err := exe.Execute(trx, td.sandbox) + assert.Equal(t, errors.Code(err), errors.ErrInvalidTx) + }) + t.Run("Should fail, public key should not set for existing validators", func(t *testing.T) { trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+2, senderAddr, receiverAddr, pub, amt, fee, "with public key") diff --git a/types/param/param.go b/types/param/param.go index 36b4be57e..b3f59a005 100644 --- a/types/param/param.go +++ b/types/param/param.go @@ -14,6 +14,7 @@ type Params struct { FeeFraction float64 `cbor:"9,keyasint"` MinimumFee int64 `cbor:"10,keyasint"` MaximumFee int64 `cbor:"11,keyasint"` + MinimumStake int64 `cobr:"12,keyasint"` MaximumStake int64 `cbor:"12,keyasint"` } @@ -30,6 +31,7 @@ func DefaultParams() Params { FeeFraction: 0.0001, MinimumFee: 1000, MaximumFee: 1000000, + MinimumStake: 1000000000, MaximumStake: 1000000000000, } } From 060c4293407380f3b3328c031ad2021cac7454b4 Mon Sep 17 00:00:00 2001 From: Cosmos Date: Fri, 14 Jul 2023 21:17:39 +0800 Subject: [PATCH 2/7] fix: cbor to 13 Co-authored-by: b00f --- types/param/param.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/param/param.go b/types/param/param.go index b3f59a005..704d3964c 100644 --- a/types/param/param.go +++ b/types/param/param.go @@ -15,7 +15,7 @@ type Params struct { MinimumFee int64 `cbor:"10,keyasint"` MaximumFee int64 `cbor:"11,keyasint"` MinimumStake int64 `cobr:"12,keyasint"` - MaximumStake int64 `cbor:"12,keyasint"` + MaximumStake int64 `cbor:"13,keyasint"` } func DefaultParams() Params { From 8f495938b6cd758e1409d1c930398e9cb8b03613 Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 6 Aug 2023 19:30:00 +0800 Subject: [PATCH 3/7] test: fix minstake test --- execution/executor/bond_test.go | 23 +++++++++++------------ execution/executor/sortition_test.go | 2 +- execution/executor/transfer_test.go | 14 +++++++++----- execution/executor/withdraw_test.go | 2 +- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/execution/executor/bond_test.go b/execution/executor/bond_test.go index 0a4908d5a..f1cf8f100 100644 --- a/execution/executor/bond_test.go +++ b/execution/executor/bond_test.go @@ -17,8 +17,7 @@ func TestExecuteBondTx(t *testing.T) { senderBalance := senderAcc.Balance() pub, _ := td.RandomBLSKeyPair() receiverAddr := pub.Address() - fee, amt := td.randomAmountAndFee(senderBalance / 2) - + fee, amt := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake, senderBalance) t.Run("Should fail, invalid sender", func(t *testing.T) { trx := tx.NewBondTx(td.stamp500000, 1, td.RandomAddress(), receiverAddr, pub, amt, fee, "invalid sender") @@ -80,20 +79,20 @@ func TestExecuteBondTx(t *testing.T) { assert.Equal(t, errors.Code(err), errors.ErrInvalidPublicKey) }) - t.Run("Ok", func(t *testing.T) { + t.Run("Should fail, amount less than MinimumStake", func(t *testing.T) { trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, - receiverAddr, pub, amt, fee, "ok") + receiverAddr, pub, 1000, fee, "less than MinimumStake") - assert.NoError(t, exe.Execute(trx, td.sandbox), "Ok") - assert.Error(t, exe.Execute(trx, td.sandbox), "Execute again, should fail") + err := exe.Execute(trx, td.sandbox) + assert.Equal(t, errors.ErrInvalidTx, errors.Code(err)) }) - t.Run("Should fail, amount less than MinimumStake", func(t *testing.T) { + t.Run("Ok", func(t *testing.T) { trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, - receiverAddr, pub, 1000, fee, "less than MinimumStake") + receiverAddr, pub, amt, fee, "ok") - err := exe.Execute(trx, td.sandbox) - assert.Equal(t, errors.Code(err), errors.ErrInvalidTx) + assert.NoError(t, exe.Execute(trx, td.sandbox), "Ok") + assert.Error(t, exe.Execute(trx, td.sandbox), "Execute again, should fail") }) t.Run("Should fail, public key should not set for existing validators", func(t *testing.T) { @@ -122,7 +121,7 @@ func TestBondInsideCommittee(t *testing.T) { exe2 := NewBondExecutor(false) senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc() senderBalance := senderAcc.Balance() - fee, amt := td.randomAmountAndFee(senderBalance) + fee, amt := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee, senderBalance) pub := td.sandbox.Committee().Proposer(0).PublicKey() trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, @@ -143,7 +142,7 @@ func TestBondJoiningCommittee(t *testing.T) { senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc() senderBalance := senderAcc.Balance() pub, _ := td.RandomBLSKeyPair() - fee, amt := td.randomAmountAndFee(senderBalance) + fee, amt := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee, senderBalance) val := td.sandbox.MakeNewValidator(pub) val.UpdateLastJoinedHeight(td.sandbox.CurrentHeight()) diff --git a/execution/executor/sortition_test.go b/execution/executor/sortition_test.go index 1d73b4bcc..a353e567e 100644 --- a/execution/executor/sortition_test.go +++ b/execution/executor/sortition_test.go @@ -17,7 +17,7 @@ func TestExecuteSortitionTx(t *testing.T) { pub, _ := td.RandomBLSKeyPair() newVal := td.sandbox.MakeNewValidator(pub) accAddr, acc := td.sandbox.TestStore.RandomTestAcc() - amt, fee := td.randomAmountAndFee(acc.Balance()) + amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee, acc.Balance()) newVal.AddToStake(amt + fee) acc.SubtractFromBalance(amt + fee) td.sandbox.UpdateAccount(accAddr, acc) diff --git a/execution/executor/transfer_test.go b/execution/executor/transfer_test.go index 3ed0aac0f..a290145c4 100644 --- a/execution/executor/transfer_test.go +++ b/execution/executor/transfer_test.go @@ -44,10 +44,14 @@ func (td *testData) checkTotalCoin(t *testing.T, fee int64) { assert.Equal(t, total+fee, int64(21000000*1e9)) } -func (td *testData) randomAmountAndFee(max int64) (int64, int64) { - amt := td.RandInt64(max / 2) +func (td *testData) randomAmountAndFee(min int64, max int64) (int64, int64) { + amt := td.RandInt64NonZero(max) + for amt < min { + amt = td.RandInt64NonZero(max) + } + fee := int64(float64(amt) * td.sandbox.Params().FeeFraction) - return amt, fee + return fee, amt } func TestExecuteTransferTx(t *testing.T) { @@ -57,7 +61,7 @@ func TestExecuteTransferTx(t *testing.T) { senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc() senderBalance := senderAcc.Balance() receiverAddr := td.RandomAddress() - amt, fee := td.randomAmountAndFee(senderBalance) + amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee, senderBalance) t.Run("Should fail, Sender has no account", func(t *testing.T) { trx := tx.NewTransferTx(td.stamp500000, 1, td.RandomAddress(), @@ -102,7 +106,7 @@ func TestTransferToSelf(t *testing.T) { senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc() senderBalance := senderAcc.Balance() - amt, fee := td.randomAmountAndFee(senderBalance) + amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee ,senderBalance) trx := tx.NewTransferTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, senderAddr, amt, fee, "ok") assert.NoError(t, exe.Execute(trx, td.sandbox)) diff --git a/execution/executor/withdraw_test.go b/execution/executor/withdraw_test.go index 0ae6e7d04..67733f337 100644 --- a/execution/executor/withdraw_test.go +++ b/execution/executor/withdraw_test.go @@ -16,7 +16,7 @@ func TestExecuteWithdrawTx(t *testing.T) { pub, _ := td.RandomBLSKeyPair() val := td.sandbox.MakeNewValidator(pub) accAddr, acc := td.sandbox.TestStore.RandomTestAcc() - amt, fee := td.randomAmountAndFee(acc.Balance()) + amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee, acc.Balance()) val.AddToStake(amt + fee) acc.SubtractFromBalance(amt + fee) td.sandbox.UpdateAccount(accAddr, acc) From a5e140748ca95caf580ed3d2a067b30e2e4ddc74 Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 6 Aug 2023 23:28:50 +0800 Subject: [PATCH 4/7] fix: tests --- execution/executor/bond_test.go | 4 ++-- execution/executor/sortition_test.go | 2 +- execution/executor/transfer_test.go | 4 ++-- execution/executor/withdraw_test.go | 2 +- state/state_test.go | 2 +- txpool/pool_test.go | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/execution/executor/bond_test.go b/execution/executor/bond_test.go index f1cf8f100..f49bcf310 100644 --- a/execution/executor/bond_test.go +++ b/execution/executor/bond_test.go @@ -121,7 +121,7 @@ func TestBondInsideCommittee(t *testing.T) { exe2 := NewBondExecutor(false) senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc() senderBalance := senderAcc.Balance() - fee, amt := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee, senderBalance) + fee, amt := td.randomAmountAndFee(0, senderBalance) pub := td.sandbox.Committee().Proposer(0).PublicKey() trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, @@ -142,7 +142,7 @@ func TestBondJoiningCommittee(t *testing.T) { senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc() senderBalance := senderAcc.Balance() pub, _ := td.RandomBLSKeyPair() - fee, amt := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee, senderBalance) + fee, amt := td.randomAmountAndFee(0, senderBalance) val := td.sandbox.MakeNewValidator(pub) val.UpdateLastJoinedHeight(td.sandbox.CurrentHeight()) diff --git a/execution/executor/sortition_test.go b/execution/executor/sortition_test.go index a353e567e..5b664bb79 100644 --- a/execution/executor/sortition_test.go +++ b/execution/executor/sortition_test.go @@ -17,7 +17,7 @@ func TestExecuteSortitionTx(t *testing.T) { pub, _ := td.RandomBLSKeyPair() newVal := td.sandbox.MakeNewValidator(pub) accAddr, acc := td.sandbox.TestStore.RandomTestAcc() - amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee, acc.Balance()) + amt, fee := td.randomAmountAndFee(0, acc.Balance()) newVal.AddToStake(amt + fee) acc.SubtractFromBalance(amt + fee) td.sandbox.UpdateAccount(accAddr, acc) diff --git a/execution/executor/transfer_test.go b/execution/executor/transfer_test.go index a290145c4..514d193e5 100644 --- a/execution/executor/transfer_test.go +++ b/execution/executor/transfer_test.go @@ -61,7 +61,7 @@ func TestExecuteTransferTx(t *testing.T) { senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc() senderBalance := senderAcc.Balance() receiverAddr := td.RandomAddress() - amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee, senderBalance) + amt, fee := td.randomAmountAndFee(0, senderBalance) t.Run("Should fail, Sender has no account", func(t *testing.T) { trx := tx.NewTransferTx(td.stamp500000, 1, td.RandomAddress(), @@ -106,7 +106,7 @@ func TestTransferToSelf(t *testing.T) { senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc() senderBalance := senderAcc.Balance() - amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee ,senderBalance) + amt, fee := td.randomAmountAndFee(0, senderBalance) trx := tx.NewTransferTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, senderAddr, amt, fee, "ok") assert.NoError(t, exe.Execute(trx, td.sandbox)) diff --git a/execution/executor/withdraw_test.go b/execution/executor/withdraw_test.go index 67733f337..35be0ccdb 100644 --- a/execution/executor/withdraw_test.go +++ b/execution/executor/withdraw_test.go @@ -16,7 +16,7 @@ func TestExecuteWithdrawTx(t *testing.T) { pub, _ := td.RandomBLSKeyPair() val := td.sandbox.MakeNewValidator(pub) accAddr, acc := td.sandbox.TestStore.RandomTestAcc() - amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumFee, acc.Balance()) + amt, fee := td.randomAmountAndFee(0, acc.Balance()) val.AddToStake(amt + fee) acc.SubtractFromBalance(amt + fee) td.sandbox.UpdateAccount(accAddr, acc) diff --git a/state/state_test.go b/state/state_test.go index 07b4bd2bc..cbbaae70f 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -398,7 +398,7 @@ func TestSortition(t *testing.T) { for ; height <= 11; height++ { if height == 2 { trx := tx.NewBondTx(td.state1.lastInfo.BlockHash().Stamp(), 1, td.valSigner1.Address(), - pub.Address(), pub, 10000000, 1000, "") + pub.Address(), pub, 1000000000, 100000, "") td.valSigner1.SignMsg(trx) assert.NoError(t, td.commonTxPool.AppendTx(trx)) diff --git a/txpool/pool_test.go b/txpool/pool_test.go index 55ebfc9aa..2f414f30a 100644 --- a/txpool/pool_test.go +++ b/txpool/pool_test.go @@ -154,7 +154,7 @@ func TestPrepareBlockTransactions(t *testing.T) { pub, _ := td.RandomBLSKeyPair() bondTx := tx.NewBondTx(block1000000.Stamp(), acc1.Sequence()+2, acc1Signer.Address(), - pub.Address(), pub, 1000, 1000, "bond-tx") + pub.Address(), pub, 1000000000, 100000, "bond-tx") acc1Signer.SignMsg(bondTx) unbondTx := tx.NewUnbondTx(block1000000.Stamp(), val1.Sequence()+1, val1.Address(), "unbond-tx") From 6776bc18c64f0ecce825c25f9207d32db3137b26 Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 7 Aug 2023 00:59:11 +0800 Subject: [PATCH 5/7] fix: tests done --- execution/execution_test.go | 4 ++-- execution/executor/bond_test.go | 32 ++++++++++++++--------------- execution/executor/transfer_test.go | 2 +- genesis/genesis_test.go | 5 +++-- state/execution_test.go | 15 +++++++++----- state/state_test.go | 6 +++--- tests/main_test.go | 1 + 7 files changed, 36 insertions(+), 29 deletions(-) diff --git a/execution/execution_test.go b/execution/execution_test.go index 8c7c0ed03..9a04a8535 100644 --- a/execution/execution_test.go +++ b/execution/execution_test.go @@ -119,12 +119,12 @@ func TestChecker(t *testing.T) { t.Run("In strict mode transaction should be rejected.", func(t *testing.T) { signer := ts.RandomSigner() acc := sb.MakeNewAccount(signer.Address()) - acc.AddToBalance(10000) + acc.AddToBalance(10000000000) sb.UpdateAccount(signer.Address(), acc) valPub := sb.TestCommitteeSigners[0].PublicKey() trx := tx.NewBondTx(block1000.Stamp(), acc.Sequence()+1, signer.Address(), - valPub.Address(), nil, 1000, 1000, "") + valPub.Address(), nil, 1000000000, 100000, "") signer.SignMsg(trx) assert.Error(t, executor.Execute(trx, sb)) assert.NoError(t, checker.Execute(trx, sb)) diff --git a/execution/executor/bond_test.go b/execution/executor/bond_test.go index f49bcf310..4ed062630 100644 --- a/execution/executor/bond_test.go +++ b/execution/executor/bond_test.go @@ -17,9 +17,9 @@ func TestExecuteBondTx(t *testing.T) { senderBalance := senderAcc.Balance() pub, _ := td.RandomBLSKeyPair() receiverAddr := pub.Address() - fee, amt := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake, senderBalance) + amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake, senderBalance) t.Run("Should fail, invalid sender", func(t *testing.T) { - trx := tx.NewBondTx(td.stamp500000, 1, td.RandomAddress(), + trx := tx.NewBondTx(td.RandomStamp(), 1, td.RandomAddress(), receiverAddr, pub, amt, fee, "invalid sender") err := exe.Execute(trx, td.sandbox) @@ -27,7 +27,7 @@ func TestExecuteBondTx(t *testing.T) { }) t.Run("Should fail, treasury address as receiver", func(t *testing.T) { - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+1, senderAddr, crypto.TreasuryAddress, nil, amt, fee, "invalid ") err := exe.Execute(trx, td.sandbox) @@ -35,7 +35,7 @@ func TestExecuteBondTx(t *testing.T) { }) t.Run("Should fail, invalid sequence", func(t *testing.T) { - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+2, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+2, senderAddr, receiverAddr, pub, amt, fee, "invalid sequence") err := exe.Execute(trx, td.sandbox) @@ -43,7 +43,7 @@ func TestExecuteBondTx(t *testing.T) { }) t.Run("Should fail, insufficient balance", func(t *testing.T) { - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+1, senderAddr, receiverAddr, pub, senderBalance+1, 0, "insufficient balance") err := exe.Execute(trx, td.sandbox) @@ -52,7 +52,7 @@ func TestExecuteBondTx(t *testing.T) { t.Run("Should fail, inside committee", func(t *testing.T) { pub := td.sandbox.Committee().Proposer(0).PublicKey() - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+1, senderAddr, pub.Address(), nil, amt, fee, "inside committee") err := exe.Execute(trx, td.sandbox) @@ -64,7 +64,7 @@ func TestExecuteBondTx(t *testing.T) { val := td.sandbox.MakeNewValidator(pub) val.UpdateUnbondingHeight(td.sandbox.CurrentHeight()) td.sandbox.UpdateValidator(val) - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+1, senderAddr, pub.Address(), nil, amt, fee, "unbonded before") err := exe.Execute(trx, td.sandbox) @@ -72,7 +72,7 @@ func TestExecuteBondTx(t *testing.T) { }) t.Run("Should fail, public key is not set", func(t *testing.T) { - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+1, senderAddr, receiverAddr, nil, amt, fee, "no public key") err := exe.Execute(trx, td.sandbox) @@ -80,7 +80,7 @@ func TestExecuteBondTx(t *testing.T) { }) t.Run("Should fail, amount less than MinimumStake", func(t *testing.T) { - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+1, senderAddr, receiverAddr, pub, 1000, fee, "less than MinimumStake") err := exe.Execute(trx, td.sandbox) @@ -88,7 +88,7 @@ func TestExecuteBondTx(t *testing.T) { }) t.Run("Ok", func(t *testing.T) { - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+1, senderAddr, receiverAddr, pub, amt, fee, "ok") assert.NoError(t, exe.Execute(trx, td.sandbox), "Ok") @@ -96,7 +96,7 @@ func TestExecuteBondTx(t *testing.T) { }) t.Run("Should fail, public key should not set for existing validators", func(t *testing.T) { - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+2, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+2, senderAddr, receiverAddr, pub, amt, fee, "with public key") err := exe.Execute(trx, td.sandbox) @@ -121,10 +121,10 @@ func TestBondInsideCommittee(t *testing.T) { exe2 := NewBondExecutor(false) senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc() senderBalance := senderAcc.Balance() - fee, amt := td.randomAmountAndFee(0, senderBalance) + amt, fee := td.randomAmountAndFee(0, senderBalance) pub := td.sandbox.Committee().Proposer(0).PublicKey() - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+1, senderAddr, pub.Address(), nil, amt, fee, "inside committee") assert.Error(t, exe1.Execute(trx, td.sandbox)) @@ -142,13 +142,13 @@ func TestBondJoiningCommittee(t *testing.T) { senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc() senderBalance := senderAcc.Balance() pub, _ := td.RandomBLSKeyPair() - fee, amt := td.randomAmountAndFee(0, senderBalance) + amt, fee := td.randomAmountAndFee(0, senderBalance) val := td.sandbox.MakeNewValidator(pub) val.UpdateLastJoinedHeight(td.sandbox.CurrentHeight()) td.sandbox.UpdateValidator(val) - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+1, senderAddr, pub.Address(), nil, amt, fee, "joining committee") assert.Error(t, exe1.Execute(trx, td.sandbox)) @@ -168,7 +168,7 @@ func TestStakeExceeded(t *testing.T) { td.sandbox.UpdateAccount(senderAddr, senderAcc) pub, _ := td.RandomBLSKeyPair() - trx := tx.NewBondTx(td.stamp500000, senderAcc.Sequence()+1, senderAddr, + trx := tx.NewBondTx(td.RandomStamp(), senderAcc.Sequence()+1, senderAddr, pub.Address(), pub, amt, fee, "stake exceeded") assert.Error(t, exe.Execute(trx, td.sandbox)) diff --git a/execution/executor/transfer_test.go b/execution/executor/transfer_test.go index 514d193e5..8fd5dc434 100644 --- a/execution/executor/transfer_test.go +++ b/execution/executor/transfer_test.go @@ -51,7 +51,7 @@ func (td *testData) randomAmountAndFee(min int64, max int64) (int64, int64) { } fee := int64(float64(amt) * td.sandbox.Params().FeeFraction) - return fee, amt + return amt, fee } func TestExecuteTransferTx(t *testing.T) { diff --git a/genesis/genesis_test.go b/genesis/genesis_test.go index 8e27e32fb..267425278 100644 --- a/genesis/genesis_test.go +++ b/genesis/genesis_test.go @@ -2,6 +2,7 @@ package genesis_test import ( "encoding/json" + "fmt" "testing" "time" @@ -57,8 +58,8 @@ func TestGenesisTestNet(t *testing.T) { genTime, _ := time.Parse("2006-01-02", "2023-05-08") assert.Equal(t, g.GenesisTime(), genTime) assert.Equal(t, g.Params().BondInterval, uint32(120)) - - expected, _ := hash.FromString("d5b41d8c2d45a951329512915ae7b2f4a445aeda380a1d40c50b5a5743dfb3fa") + fmt.Println(g.Hash()) + expected, _ := hash.FromString("63f32d682050bd03eb832c9d99fc3b48855d7f4193e5c09205be68d6e16546af") assert.Equal(t, g.Hash(), expected) } diff --git a/state/execution_test.go b/state/execution_test.go index cddb08b9a..6dd501f93 100644 --- a/state/execution_test.go +++ b/state/execution_test.go @@ -13,9 +13,14 @@ import ( func TestProposeBlock(t *testing.T) { td := setup(t) + curHeight := uint32(7) + for i := uint32(0); i < curHeight; i++ { + td.moveToNextHeightForAllStates(t) + } b1, c1 := td.makeBlockAndCertificate(t, 0, td.valSigner1, td.valSigner2, td.valSigner3) - assert.NoError(t, td.state1.CommitBlock(1, b1, c1)) - assert.NoError(t, td.state2.CommitBlock(1, b1, c1)) + assert.NoError(t, td.state1.CommitBlock(curHeight+1, b1, c1)) + assert.NoError(t, td.state2.CommitBlock(curHeight+1, b1, c1)) + assert.Equal(t, td.state1.LastBlockHeight(), curHeight+1) invSubsidyTx := tx.NewSubsidyTx(td.state1.lastInfo.BlockHash().Stamp(), 1, td.valSigner2.Address(), td.state1.params.BlockReward, "duplicated subsidy transaction") @@ -27,7 +32,7 @@ func TestProposeBlock(t *testing.T) { trx1 := tx.NewTransferTx(b1.Stamp(), 1, td.valSigner1.Address(), td.valSigner1.Address(), 1, 1000, "") td.valSigner1.SignMsg(trx1) - trx2 := tx.NewBondTx(b1.Stamp(), 2, td.valSigner1.Address(), pub.Address(), pub, 1000, 1000, "") + trx2 := tx.NewBondTx(b1.Stamp(), 2, td.valSigner1.Address(), pub.Address(), pub, 1000000000, 100000, "") td.valSigner1.SignMsg(trx2) assert.NoError(t, td.state1.txPool.AppendTx(invTransferTx)) @@ -41,9 +46,9 @@ func TestProposeBlock(t *testing.T) { assert.Equal(t, b2.Header().PrevBlockHash(), b1.Hash()) assert.Equal(t, b2.Transactions()[1:], block.Txs{trx1, trx2}) assert.True(t, b2.Transactions()[0].IsSubsidyTx()) - assert.NoError(t, td.state1.CommitBlock(2, b2, c2)) + assert.NoError(t, td.state1.CommitBlock(curHeight+2, b2, c2)) - assert.Equal(t, td.state1.TotalPower(), int64(1004)) + assert.Equal(t, td.state1.TotalPower(), int64(1000000004)) assert.Equal(t, td.state1.committee.TotalPower(), int64(4)) } diff --git a/state/state_test.go b/state/state_test.go index cbbaae70f..f96a09c7e 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -395,8 +395,8 @@ func TestSortition(t *testing.T) { assert.Equal(t, td.state1.CommitteePower(), int64(4)) height := uint32(1) - for ; height <= 11; height++ { - if height == 2 { + for ; height <= 15; height++ { + if height == 6 { trx := tx.NewBondTx(td.state1.lastInfo.BlockHash().Stamp(), 1, td.valSigner1.Address(), pub.Address(), pub, 1000000000, 100000, "") td.valSigner1.SignMsg(trx) @@ -458,7 +458,7 @@ func TestSortition(t *testing.T) { assert.NoError(t, td.state3.CommitBlock(height, b14, c14)) assert.NoError(t, td.state4.CommitBlock(height, b14, c14)) - assert.Equal(t, td.state1.CommitteePower(), int64(10000004)) + assert.Equal(t, td.state1.CommitteePower(), int64(1000000004)) assert.Equal(t, td.state1.TotalValidators(), int32(5)) } diff --git a/tests/main_test.go b/tests/main_test.go index 21e387e4e..e727b54b2 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -119,6 +119,7 @@ func TestMain(m *testing.M) { vals[2] = validator.NewValidator(tSigners[tNodeIdx3][0].PublicKey().(*bls.PublicKey), 2) vals[3] = validator.NewValidator(tSigners[tNodeIdx4][0].PublicKey().(*bls.PublicKey), 3) params := param.DefaultParams() + params.MinimumStake = 1000 params.BlockTimeInSecond = 2 params.BondInterval = 8 params.CommitteeSize = tCommitteeSize From c19727ac7adcaa7abbaa52dc60b1cda3a3e78f88 Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 7 Aug 2023 01:49:23 +0800 Subject: [PATCH 6/7] fix: trnasfer --- execution/executor/transfer.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/execution/executor/transfer.go b/execution/executor/transfer.go index 9c74e81c2..d9dba07f8 100644 --- a/execution/executor/transfer.go +++ b/execution/executor/transfer.go @@ -54,6 +54,9 @@ func (e *TransferExecutor) Execute(trx *tx.Tx, sb sandbox.Sandbox) error { return errors.Errorf(errors.ErrInvalidSequence, "expected: %v, got: %v", senderAcc.Sequence()+1, trx.Sequence()) } + if senderAcc.Balance() < pld.Amount+trx.Fee() { + return errors.Error(errors.ErrInsufficientFunds) + } senderAcc.IncSequence() senderAcc.SubtractFromBalance(pld.Amount + trx.Fee()) From 232ea13820b08e029945e53de827766e373396cb Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 7 Aug 2023 14:37:47 +0800 Subject: [PATCH 7/7] fix: resolve --- execution/executor/transfer.go | 3 --- genesis/genesis_test.go | 2 -- 2 files changed, 5 deletions(-) diff --git a/execution/executor/transfer.go b/execution/executor/transfer.go index d9dba07f8..86a1353a2 100644 --- a/execution/executor/transfer.go +++ b/execution/executor/transfer.go @@ -47,9 +47,6 @@ func (e *TransferExecutor) Execute(trx *tx.Tx, sb sandbox.Sandbox) error { receiverAcc = sb.MakeNewAccount(pld.Receiver) } } - if senderAcc.Balance() < pld.Amount+trx.Fee() { - return errors.Error(errors.ErrInsufficientFunds) - } if senderAcc.Sequence()+1 != trx.Sequence() { return errors.Errorf(errors.ErrInvalidSequence, "expected: %v, got: %v", senderAcc.Sequence()+1, trx.Sequence()) diff --git a/genesis/genesis_test.go b/genesis/genesis_test.go index 267425278..ec32a64a4 100644 --- a/genesis/genesis_test.go +++ b/genesis/genesis_test.go @@ -2,7 +2,6 @@ package genesis_test import ( "encoding/json" - "fmt" "testing" "time" @@ -58,7 +57,6 @@ func TestGenesisTestNet(t *testing.T) { genTime, _ := time.Parse("2006-01-02", "2023-05-08") assert.Equal(t, g.GenesisTime(), genTime) assert.Equal(t, g.Params().BondInterval, uint32(120)) - fmt.Println(g.Hash()) expected, _ := hash.FromString("63f32d682050bd03eb832c9d99fc3b48855d7f4193e5c09205be68d6e16546af") assert.Equal(t, g.Hash(), expected) }