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: remove undelegations v2 #245

Merged
merged 1 commit into from
Sep 23, 2024
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
5 changes: 4 additions & 1 deletion taraxa/state/contracts/dpos/precompiled/undelegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func (self *Undelegations) Init(stor *contract_storage.StorageWrapper, prefix []
self.delegator_v2_undelegations_field = append(prefix, []byte{2}...)
self.delegator_v2_undelegations_ids_field = append(prefix, []byte{3}...)
self.delegator_v2_undelegations_last_uniqe_id_field = append(prefix, []byte{4}...)
self.v1_undelegations_map = make(map[common.Address]*contract_storage.AddressesIMap)
self.v2_undelegations_map = make(map[common.Address]*DelegatorV2Undelegations)
}

// Returns true if for given values there is undelegation in queue
Expand Down Expand Up @@ -198,7 +200,6 @@ func (self *Undelegations) removeUndelegationV1(delegator_address *common.Addres

func (self *Undelegations) removeUndelegationV2(delegator_address *common.Address, validator_address *common.Address, undelegation_id uint64) {
self.removeUndelegationObject(self.genUndelegationV2Key(delegator_address, validator_address, undelegation_id))

validators_map, ids_map := self.GetUndelegationsV2Maps(delegator_address, validator_address)
if ids_map.RemoveId(undelegation_id) == 0 {
if validators_map.RemoveAccount(validator_address) == 0 {
Expand Down Expand Up @@ -242,6 +243,7 @@ func (self *Undelegations) getUndelegationsV1ValidatorsMap(delegator_address *co

v1_undelegations_validators = new(contract_storage.AddressesIMap)
v1_undelegations_validators.Init(self.storage, v1_undelegations_validators_prefix)
self.v1_undelegations_map[*delegator_address] = v1_undelegations_validators
}

return v1_undelegations_validators
Expand All @@ -255,6 +257,7 @@ func (self *Undelegations) GetUndelegationsV2Maps(delegator_address *common.Addr
v2_undelegations_validators_prefix := append(self.delegator_v2_undelegations_field, delegator_address[:]...)
v2_undelegations_validators.Validators = new(contract_storage.AddressesIMap)
v2_undelegations_validators.Validators.Init(self.storage, v2_undelegations_validators_prefix)
self.v2_undelegations_map[*delegator_address] = v2_undelegations_validators
}
validators_map = v2_undelegations_validators.Validators

Expand Down
22 changes: 19 additions & 3 deletions taraxa/state/contracts/tests/dpos/dpos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,14 @@ func TestCornusHardfork(t *testing.T) {
val_owner := addr(1)
val_addr, proof := generateAddrAndProof()

test.ExecuteAndCheck(val_owner, DefaultValidatorMaximumStake, test.Pack("registerValidator", val_addr, proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString(""))
test.CheckContractBalance(DefaultValidatorMaximumStake)
val_owner2 := addr(2)
val_addr2, proof2 := generateAddrAndProof()

test.ExecuteAndCheck(val_owner, DefaultEligibilityBalanceThreshold, test.Pack("registerValidator", val_addr, proof, DefaultVrfKey, uint16(10), "test", "test"), util.ErrorString(""), util.ErrorString(""))
test.CheckContractBalance(DefaultEligibilityBalanceThreshold)
test.ExecuteAndCheck(val_owner2, DefaultEligibilityBalanceThreshold, test.Pack("registerValidator", val_addr2, proof2, DefaultVrfKey, uint16(10), "test3", "test3"), util.ErrorString(""), util.ErrorString(""))

test.ExecuteAndCheck(val_owner, DefaultMinimumDeposit, test.Pack("delegate", val_addr2), util.ErrorString(""), util.ErrorString(""))

// ErrMethodNotSupported
test.ExecuteAndCheck(val_owner, big.NewInt(0), test.Pack("confirmUndelegateV2", val_addr, uint64(1)), dpos.ErrMethodNotSupported, util.ErrorString(""))
Expand All @@ -628,6 +634,13 @@ func TestCornusHardfork(t *testing.T) {
test.Unpack(undelegation_id_parsed, "undelegateV2", undelegate_v2_res.CodeRetval)
tc.Assert.Equal(uint64(1), *undelegation_id_parsed)

undelegate_v2_res2 := test.ExecuteAndCheck(val_owner, big.NewInt(0), test.Pack("undelegateV2", val_addr2, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString(""))
tc.Assert.Equal(len(undelegate_v2_res2.Logs), 1)
tc.Assert.Equal(undelegate_v2_res2.Logs[0].Topics[0], UndelegatedV2EventHash)
undelegation_id_parsed2 := new(uint64)
test.Unpack(undelegation_id_parsed2, "undelegateV2", undelegate_v2_res2.CodeRetval)
tc.Assert.Equal(uint64(2), *undelegation_id_parsed2)

// Confirm V1 undelegation
confirm_res := test.ExecuteAndCheck(val_owner, big.NewInt(0), test.Pack("confirmUndelegate", val_addr), util.ErrorString(""), util.ErrorString(""))
tc.Assert.Equal(len(confirm_res.Logs), 1)
Expand All @@ -637,7 +650,7 @@ func TestCornusHardfork(t *testing.T) {
get_undelegations_v2_result := test.ExecuteAndCheck(val_owner, big.NewInt(0), test.Pack("getUndelegationsV2", val_owner, uint32(0) /* batch */), util.ErrorString(""), util.ErrorString(""))
get_undelegations_v2_result_parsed := new(GetUndelegationsV2Ret)
test.Unpack(get_undelegations_v2_result_parsed, "getUndelegationsV2", get_undelegations_v2_result.CodeRetval)
tc.Assert.Equal(1, len(get_undelegations_v2_result_parsed.UndelegationsV2))
tc.Assert.Equal(2, len(get_undelegations_v2_result_parsed.UndelegationsV2))
tc.Assert.Equal(true, get_undelegations_v2_result_parsed.End)
undelegation_id := get_undelegations_v2_result_parsed.UndelegationsV2[0].UndelegationId
tc.Assert.Equal(*undelegation_id_parsed, undelegation_id)
Expand All @@ -648,8 +661,11 @@ func TestCornusHardfork(t *testing.T) {

// Confirm V2 undelegation
confirm_res = test.ExecuteAndCheck(val_owner, big.NewInt(0), test.Pack("confirmUndelegateV2", val_addr, undelegation_id), util.ErrorString(""), util.ErrorString(""))
confirm_res2 := test.ExecuteAndCheck(val_owner, big.NewInt(0), test.Pack("confirmUndelegateV2", val_addr2, undelegation_id_parsed2), util.ErrorString(""), util.ErrorString(""))
tc.Assert.Equal(len(confirm_res.Logs), 1)
tc.Assert.Equal(confirm_res.Logs[0].Topics[0], UndelegateConfirmedV2EventHash)
tc.Assert.Equal(len(confirm_res2.Logs), 1)
tc.Assert.Equal(confirm_res2.Logs[0].Topics[0], UndelegateConfirmedV2EventHash)
}

func TestConfirmUndelegate(t *testing.T) {
Expand Down