From 073e2123c17250eba96f357b524d37d44689b8c3 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 27 Nov 2024 12:53:04 +0100 Subject: [PATCH] add swquoia hf test --- taraxa/state/chain_config/chain_config.go | 2 +- .../state/contracts/dpos/precompiled/api.go | 1 + .../dpos/precompiled/dpos_contract.go | 10 ++-- .../state/contracts/tests/dpos/dpos_test.go | 48 +++++++++++++++++++ .../contracts/tests/slashing/slashing_test.go | 4 ++ 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/taraxa/state/chain_config/chain_config.go b/taraxa/state/chain_config/chain_config.go index eceb639b2..a16888647 100644 --- a/taraxa/state/chain_config/chain_config.go +++ b/taraxa/state/chain_config/chain_config.go @@ -94,7 +94,7 @@ func (c *HardforksConfig) IsCornusHardfork(block types.BlockNum) bool { return block == c.CornusHfBlockNum } -func (c *HardforksConfig) IsSequoiaHardfork(block types.BlockNum) bool { +func (c *HardforksConfig) IsOnSequoiaHardfork(block types.BlockNum) bool { return block >= c.SequoiaHf.BlockNum } diff --git a/taraxa/state/contracts/dpos/precompiled/api.go b/taraxa/state/contracts/dpos/precompiled/api.go index f3b21eccd..7c594de74 100644 --- a/taraxa/state/contracts/dpos/precompiled/api.go +++ b/taraxa/state/contracts/dpos/precompiled/api.go @@ -32,6 +32,7 @@ type GenesisTransfer = struct { func (api *API) Init(cfg chain_config.ChainConfig) *API { asserts.Holds(cfg.DPOS.DelegationDelay <= cfg.DPOS.DelegationLockingPeriod) + asserts.Holds(cfg.DPOS.DelegationDelay <= cfg.Hardforks.SequoiaHf.DelegationLockingPeriod) asserts.Holds(cfg.DPOS.EligibilityBalanceThreshold != nil) asserts.Holds(cfg.DPOS.VoteEligibilityBalanceStep != nil) diff --git a/taraxa/state/contracts/dpos/precompiled/dpos_contract.go b/taraxa/state/contracts/dpos/precompiled/dpos_contract.go index f9240e354..9c20675a2 100644 --- a/taraxa/state/contracts/dpos/precompiled/dpos_contract.go +++ b/taraxa/state/contracts/dpos/precompiled/dpos_contract.go @@ -1203,16 +1203,16 @@ func (self *Contract) undelegate(ctx vm.CallFrame, block types.BlockNum, args dp // Create undelegation request undelegation_id := uint64(0) - delegation_locking_period := self.cfg.DPOS.DelegationLockingPeriod - if self.cfg.Hardforks.IsOnSequoiaHardfork(block) { - delegation_locking_period = self.cfg.Hardforks.SequoiaHf.DelegationLockingPeriod + delegationLockingPeriod := self.cfg.Hardforks.SequoiaHf.DelegationLockingPeriod + if !self.cfg.Hardforks.IsOnSequoiaHardfork(block) { + delegationLockingPeriod = self.cfg.DPOS.DelegationLockingPeriod } if v2 { - undelegation_id = self.undelegations.CreateUndelegationV2(ctx.CallerAccount.Address(), &args.Validator, block+uint64(self.cfg.DPOS.DelegationLockingPeriod), args.Amount) + undelegation_id = self.undelegations.CreateUndelegationV2(ctx.CallerAccount.Address(), &args.Validator, block+uint64(delegationLockingPeriod), args.Amount) self.evm.AddLog(self.logs.MakeUndelegatedV2Log(ctx.CallerAccount.Address(), &args.Validator, undelegation_id, args.Amount)) } else { - self.undelegations.CreateUndelegationV1(ctx.CallerAccount.Address(), &args.Validator, block+uint64(self.cfg.DPOS.DelegationLockingPeriod), args.Amount) + self.undelegations.CreateUndelegationV1(ctx.CallerAccount.Address(), &args.Validator, block+uint64(delegationLockingPeriod), args.Amount) self.evm.AddLog(self.logs.MakeUndelegatedV1Log(ctx.CallerAccount.Address(), &args.Validator, args.Amount)) } diff --git a/taraxa/state/contracts/tests/dpos/dpos_test.go b/taraxa/state/contracts/tests/dpos/dpos_test.go index 0584ac727..dbd597edb 100644 --- a/taraxa/state/contracts/tests/dpos/dpos_test.go +++ b/taraxa/state/contracts/tests/dpos/dpos_test.go @@ -127,6 +127,10 @@ var ( GeneratedRewards: big.NewInt(0), }, CornusHfBlockNum: 1000, + SequoiaHf: chain_config.SequoiaHfConfig{ + BlockNum: 0, + DelegationLockingPeriod: 4, + }, }, } ) @@ -2844,3 +2848,47 @@ func TestNonPayableMethods(t *testing.T) { test.ExecuteAndCheck(caller, big.NewInt(1), test.MethodId(method), dpos.ErrNonPayableMethod, util.ErrorString("")) } } + +func TestSequoiaHardfork(t *testing.T) { + cfg := CopyDefaultChainConfig() + cfg.Hardforks.CornusHfBlockNum = 0 + cfg.Hardforks.SequoiaHf.BlockNum = 5 + cfg.Hardforks.SequoiaHf.DelegationLockingPeriod = 100 + tc, test := test_utils.Init_test(dpos.ContractAddress(), dpos_sol.TaraxaDposClientMetaData, t, cfg) + defer test.End() + + 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("")) + totalBalance := DefaultValidatorMaximumStake + test.CheckContractBalance(totalBalance) + + // Create undelegation before sequoia hardfork + test.ExecuteAndCheck(val_owner, big.NewInt(0), test.Pack("undelegateV2", val_addr, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString("")) + undelegate1_expected_id := uint64(1) + undelegate1_expected_lockup_block := test.BlockNumber() + uint64(cfg.DPOS.DelegationLockingPeriod) + + get_undelegation1_result := test.ExecuteAndCheck(val_owner, big.NewInt(0), test.Pack("getUndelegationV2", val_owner, val_addr, undelegate1_expected_id), util.ErrorString(""), util.ErrorString("")) + get_undelegation1_parsed_result := new(GetUndelegationV2Ret) + test.Unpack(get_undelegation1_parsed_result, "getUndelegationV2", get_undelegation1_result.CodeRetval) + tc.Assert.Equal(undelegate1_expected_id, get_undelegation1_parsed_result.UndelegationV2.UndelegationId) + tc.Assert.Equal(undelegate1_expected_lockup_block, get_undelegation1_parsed_result.UndelegationV2.UndelegationData.Block) + + // Pass sequoia hardfork + tc.Assert.Less(test.BlockNumber(), cfg.Hardforks.SequoiaHf.BlockNum) + for i := test.BlockNumber(); i < cfg.Hardforks.SequoiaHf.BlockNum; i++ { + test.AdvanceBlock(nil, nil) + } + + // Create undelegation after sequoia hardfork + test.ExecuteAndCheck(val_owner, big.NewInt(0), test.Pack("undelegateV2", val_addr, DefaultMinimumDeposit), util.ErrorString(""), util.ErrorString("")) + undelegate2_expected_id := uint64(2) + undelegate2_expected_lockup_block := test.BlockNumber() + uint64(cfg.Hardforks.SequoiaHf.DelegationLockingPeriod) + + get_undelegation2_result := test.ExecuteAndCheck(val_owner, big.NewInt(0), test.Pack("getUndelegationV2", val_owner, val_addr, undelegate2_expected_id), util.ErrorString(""), util.ErrorString("")) + get_undelegation2_parsed_result := new(GetUndelegationV2Ret) + test.Unpack(get_undelegation2_parsed_result, "getUndelegationV2", get_undelegation2_result.CodeRetval) + tc.Assert.Equal(undelegate2_expected_id, get_undelegation2_parsed_result.UndelegationV2.UndelegationId) + tc.Assert.Equal(undelegate2_expected_lockup_block, get_undelegation2_parsed_result.UndelegationV2.UndelegationData.Block) +} diff --git a/taraxa/state/contracts/tests/slashing/slashing_test.go b/taraxa/state/contracts/tests/slashing/slashing_test.go index 520748dbb..dcaefb007 100644 --- a/taraxa/state/contracts/tests/slashing/slashing_test.go +++ b/taraxa/state/contracts/tests/slashing/slashing_test.go @@ -70,6 +70,10 @@ var ( MaxSupply: new(big.Int).Mul(big.NewInt(12e+9), big.NewInt(1e+18)), GeneratedRewards: big.NewInt(0), }, + SequoiaHf: chain_config.SequoiaHfConfig{ + BlockNum: 0, + DelegationLockingPeriod: 4, + }, }, } )