From 79934b3ff179e3d79b9c3c79e82405aa9bde1aca Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Fri, 20 Sep 2024 10:54:43 +0200 Subject: [PATCH] fix: remove undelegations v2 --- .../dpos/precompiled/undelegations.go | 5 ++++- .../state/contracts/tests/dpos/dpos_test.go | 22 ++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/taraxa/state/contracts/dpos/precompiled/undelegations.go b/taraxa/state/contracts/dpos/precompiled/undelegations.go index 3ae9adc82..950e4b0cd 100644 --- a/taraxa/state/contracts/dpos/precompiled/undelegations.go +++ b/taraxa/state/contracts/dpos/precompiled/undelegations.go @@ -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 @@ -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 { @@ -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 @@ -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 diff --git a/taraxa/state/contracts/tests/dpos/dpos_test.go b/taraxa/state/contracts/tests/dpos/dpos_test.go index d1a3062c6..590e584a2 100644 --- a/taraxa/state/contracts/tests/dpos/dpos_test.go +++ b/taraxa/state/contracts/tests/dpos/dpos_test.go @@ -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("")) @@ -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) @@ -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) @@ -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) {