Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(execution): fixing issue #869 #870

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions execution/executor/bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func (e *BondExecutor) Execute(trx *tx.Tx, sb sandbox.Sandbox) error {
}

receiverVal := sb.Validator(pld.To)
if receiverVal == nil {
if receiverVal == nil || receiverVal.LastBondingHeight() == 0 {
if pld.PublicKey == nil {
return errors.Errorf(errors.ErrInvalidPublicKey,
"public key is not set")
}
receiverVal = sb.MakeNewValidator(pld.PublicKey)
} else if pld.PublicKey != nil {
return errors.Errorf(errors.ErrInvalidPublicKey,
"public key set")
"public key is set")
}
if receiverVal.UnbondingHeight() > 0 {
return errors.Errorf(errors.ErrInvalidHeight,
Expand Down
26 changes: 26 additions & 0 deletions execution/executor/bond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestExecuteBondTx(t *testing.T) {
t.Run("Should fail, unbonded before", func(t *testing.T) {
unbondedPub, _ := td.RandBLSKeyPair()
val := td.sandbox.MakeNewValidator(unbondedPub)
val.UpdateLastBondingHeight(1)
val.UpdateUnbondingHeight(td.sandbox.CurrentHeight())
td.sandbox.UpdateValidator(val)
trx := tx.NewBondTx(lockTime, senderAddr,
Expand Down Expand Up @@ -140,6 +141,7 @@ func TestBondJoiningCommittee(t *testing.T) {
lockTime := td.sandbox.CurrentHeight()

val := td.sandbox.MakeNewValidator(pub)
val.UpdateLastBondingHeight(1)
val.UpdateLastSortitionHeight(td.sandbox.CurrentHeight())
td.sandbox.UpdateValidator(val)
td.sandbox.JoinedToCommittee(val.Address())
Expand Down Expand Up @@ -189,3 +191,27 @@ func TestPowerDeltaBond(t *testing.T) {

assert.Equal(t, amt, td.sandbox.PowerDelta())
}

// Test case for issue #869: https://github.com/pactus-project/pactus/issues/869
func TestIssue869(t *testing.T) {
td := setup(t)
exe := NewBondExecutor(false)

senderAddr, senderAcc := td.sandbox.TestStore.RandomTestAcc()
senderBalance := senderAcc.Balance()
pub, _ := td.RandBLSKeyPair()
receiverAddr := pub.ValidatorAddress()
amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake, senderBalance)
lockTime := td.sandbox.CurrentHeight()
trx1 := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, pub, 1000e9, fee, "insufficient funds") // make sure

trx2 := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, pub, amt, fee, "ok")

err := exe.Execute(trx1, td.sandbox)
assert.Error(t, err, ErrInsufficientFunds)

err = exe.Execute(trx2, td.sandbox)
assert.NoError(t, err)
}
20 changes: 17 additions & 3 deletions sandbox/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type MockSandbox struct {
TestJoinedValidators map[crypto.Address]bool
TestCommittedTrxs map[tx.ID]*tx.Tx
TestPowerDelta int64
TestSandboxVals map[crypto.Address]*validator.Validator
TestSandboxAccs map[crypto.Address]*account.Account
}

func MockingSandbox(ts *testsuite.TestSuite) *MockSandbox {
Expand All @@ -38,6 +40,8 @@ func MockingSandbox(ts *testsuite.TestSuite) *MockSandbox {
TestCommittee: cmt,
TestJoinedValidators: make(map[crypto.Address]bool),
TestCommittedTrxs: make(map[tx.ID]*tx.Tx),
TestSandboxVals: make(map[crypto.Address]*validator.Validator),
TestSandboxAccs: make(map[crypto.Address]*account.Account),
}

treasuryAmt := int64(21000000 * 1e9)
Expand All @@ -60,11 +64,16 @@ func MockingSandbox(ts *testsuite.TestSuite) *MockSandbox {

func (m *MockSandbox) Account(addr crypto.Address) *account.Account {
acc, _ := m.TestStore.Account(addr)
if acc == nil {
return m.TestSandboxAccs[addr]
}
return acc
}

func (m *MockSandbox) MakeNewAccount(_ crypto.Address) *account.Account {
return account.NewAccount(m.TestStore.TotalAccounts())
func (m *MockSandbox) MakeNewAccount(addr crypto.Address) *account.Account {
acc := account.NewAccount(m.TestStore.TotalAccounts())
m.TestSandboxAccs[addr] = acc
return acc
}

func (m *MockSandbox) UpdateAccount(addr crypto.Address, acc *account.Account) {
Expand All @@ -80,6 +89,9 @@ func (m *MockSandbox) AnyRecentTransaction(txID tx.ID) bool {

func (m *MockSandbox) Validator(addr crypto.Address) *validator.Validator {
val, _ := m.TestStore.Validator(addr)
if val == nil {
return m.TestSandboxVals[addr]
}
return val
}

Expand All @@ -92,7 +104,9 @@ func (m *MockSandbox) IsJoinedCommittee(addr crypto.Address) bool {
}

func (m *MockSandbox) MakeNewValidator(pub *bls.PublicKey) *validator.Validator {
return validator.NewValidator(pub, m.TestStore.TotalValidators())
val := validator.NewValidator(pub, m.TestStore.TotalValidators())
m.TestSandboxVals[val.Address()] = val
return val
}

func (m *MockSandbox) UpdateValidator(val *validator.Validator) {
Expand Down
Loading