Skip to content

Commit

Permalink
add swquoia hf test
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubFornadel committed Nov 27, 2024
1 parent 28e040a commit 073e212
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion taraxa/state/chain_config/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions taraxa/state/contracts/dpos/precompiled/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions taraxa/state/contracts/dpos/precompiled/dpos_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
48 changes: 48 additions & 0 deletions taraxa/state/contracts/tests/dpos/dpos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ var (
GeneratedRewards: big.NewInt(0),
},
CornusHfBlockNum: 1000,
SequoiaHf: chain_config.SequoiaHfConfig{
BlockNum: 0,
DelegationLockingPeriod: 4,
},
},
}
)
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions taraxa/state/contracts/tests/slashing/slashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
)
Expand Down

0 comments on commit 073e212

Please sign in to comment.