From d364dfb14138f2408b21352b09cca4b9f1ad9010 Mon Sep 17 00:00:00 2001 From: siovanus Date: Thu, 12 Oct 2023 16:19:29 +0800 Subject: [PATCH] fix governance unit test (#130) * fix conflict * update with genesis config --- .../governance/node_manager/external.go | 48 +++++++++++++++++-- .../governance/node_manager/manager_test.go | 21 ++++---- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/contracts/native/governance/node_manager/external.go b/contracts/native/governance/node_manager/external.go index 7bd7a741..8dee1b08 100644 --- a/contracts/native/governance/node_manager/external.go +++ b/contracts/native/governance/node_manager/external.go @@ -34,16 +34,16 @@ var ( GenesisMaxCommissionChange, _ = new(big.Int).SetString("500", 10) // 5% GenesisMinInitialStake = new(big.Int).Mul(big.NewInt(100000), params.ZNT1) GenesisMinProposalStake = new(big.Int).Mul(big.NewInt(1000), params.ZNT1) - GenesisBlockPerEpoch = new(big.Int).SetUint64(40) + GenesisBlockPerEpoch = new(big.Int).SetUint64(400000) GenesisConsensusValidatorNum uint64 = 4 GenesisVoterValidatorNum uint64 = 4 // const - MaxDescLength int = 2000 - MaxValidatorNum int = 300 - MaxUnlockingNum int = 100 + MaxDescLength int = 2000 + MaxValidatorNum int = 300 + MaxUnlockingNum int = 100 MaxStakeRate utils.Dec = utils.NewDecFromBigInt(new(big.Int).SetUint64(6)) // user stake can not more than 5 times of self stake - MinBlockPerEpoch = new(big.Int).SetUint64(10000) + MinBlockPerEpoch = new(big.Int).SetUint64(10000) ) func init() { @@ -108,3 +108,41 @@ func StoreGenesisGlobalConfig(s *state.StateDB) error { } return nil } + +func StoreGenesisEpochForTest(s *state.StateDB, peers []common.Address, signers []common.Address, + blockPerEpoch *big.Int) (*EpochInfo, error) { + cache := (*state.CacheDB)(s) + epoch := &EpochInfo{ + ID: StartEpochID, + Validators: peers, + Signers: signers, + Voters: signers, + Proposers: signers, + StartHeight: new(big.Int), + EndHeight: blockPerEpoch, + } + + // store current epoch and epoch info + if err := setGenesisEpochInfo(cache, epoch); err != nil { + return nil, err + } + return epoch, nil +} + +func StoreGenesisGlobalConfigForTest(s *state.StateDB, blockPerEpoch *big.Int) error { + cache := (*state.CacheDB)(s) + globalConfig := &GlobalConfig{ + MaxCommissionChange: GenesisMaxCommissionChange, + MinInitialStake: GenesisMinInitialStake, + MinProposalStake: GenesisMinProposalStake, + BlockPerEpoch: blockPerEpoch, + ConsensusValidatorNum: GenesisConsensusValidatorNum, + VoterValidatorNum: GenesisVoterValidatorNum, + } + + // store current epoch and epoch info + if err := setGenesisGlobalConfig(cache, globalConfig); err != nil { + return err + } + return nil +} diff --git a/contracts/native/governance/node_manager/manager_test.go b/contracts/native/governance/node_manager/manager_test.go index 26aa4101..79fdc2e1 100644 --- a/contracts/native/governance/node_manager/manager_test.go +++ b/contracts/native/governance/node_manager/manager_test.go @@ -24,12 +24,11 @@ import ( "math/big" "testing" - "github.com/ethereum/go-ethereum/contracts/native/contract" - "github.com/ethereum/go-ethereum/contracts/native/governance/community" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/contracts/native" + "github.com/ethereum/go-ethereum/contracts/native/contract" . "github.com/ethereum/go-ethereum/contracts/native/go_abi/node_manager_abi" + "github.com/ethereum/go-ethereum/contracts/native/governance/community" "github.com/ethereum/go-ethereum/contracts/native/utils" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/crypto" @@ -52,8 +51,8 @@ func Init() { sdb = native.NewTestStateDB() testGenesisPeers, _ = native.GenerateTestPeers(testGenesisNum) community.StoreCommunityInfo(sdb, big.NewInt(2000), common.EmptyAddress) - StoreGenesisEpoch(sdb, testGenesisPeers, testGenesisPeers) - StoreGenesisGlobalConfig(sdb) + StoreGenesisEpochForTest(sdb, testGenesisPeers, testGenesisPeers, big.NewInt(400000)) + StoreGenesisGlobalConfigForTest(sdb, big.NewInt(400000)) } func TestCheckGenesis(t *testing.T) { @@ -70,7 +69,7 @@ func TestCheckGenesis(t *testing.T) { globalConfig, err := GetGlobalConfigImpl(contract) assert.Nil(t, err) - assert.Equal(t, globalConfig.BlockPerEpoch, GenesisBlockPerEpoch) + assert.Equal(t, globalConfig.BlockPerEpoch, big.NewInt(400000)) assert.Equal(t, globalConfig.MaxCommissionChange, GenesisMaxCommissionChange) assert.Equal(t, globalConfig.MinInitialStake, GenesisMinInitialStake) assert.Equal(t, globalConfig.VoterValidatorNum, GenesisVoterValidatorNum) @@ -94,7 +93,7 @@ func TestCheckGenesis(t *testing.T) { globalConfig2 := new(GlobalConfig) err = globalConfig2.Decode(ret) assert.Nil(t, err) - assert.Equal(t, globalConfig2.BlockPerEpoch, GenesisBlockPerEpoch) + assert.Equal(t, globalConfig2.BlockPerEpoch, big.NewInt(400000)) assert.Equal(t, globalConfig2.MaxCommissionChange, GenesisMaxCommissionChange) assert.Equal(t, globalConfig2.MinInitialStake, GenesisMinInitialStake) assert.Equal(t, globalConfig2.VoterValidatorNum, GenesisVoterValidatorNum) @@ -130,7 +129,7 @@ func TestCheckGenesis(t *testing.T) { globalConfig, err = GetGlobalConfigFromDB(sdb) assert.Nil(t, err) - assert.Equal(t, globalConfig.BlockPerEpoch, GenesisBlockPerEpoch) + assert.Equal(t, globalConfig.BlockPerEpoch, big.NewInt(400000)) } func TestStake(t *testing.T) { @@ -599,6 +598,7 @@ func TestDistribute(t *testing.T) { // first add 1000 balance of node_manager contract to distribute sdb.AddBalance(utils.NodeManagerContractAddress, new(big.Int).Mul(big.NewInt(1000), params.ZNT1)) // call endblock + sdb.SubBalance(utils.NodeManagerContractAddress, big.NewInt(800000000000000000)) param3 := new(EndBlockParam) input, err = param3.Encode() assert.Nil(t, err) @@ -889,6 +889,7 @@ func TestDistribute(t *testing.T) { // add 2000 balance of node_manager contract to distribute sdb.AddBalance(utils.NodeManagerContractAddress, new(big.Int).Mul(big.NewInt(1000), params.ZNT1)) // call endblock + sdb.SubBalance(utils.NodeManagerContractAddress, big.NewInt(800000000000000000)) param9 := new(EndBlockParam) input, err = param9.Encode() assert.Nil(t, err) @@ -898,6 +899,7 @@ func TestDistribute(t *testing.T) { sdb.AddBalance(utils.NodeManagerContractAddress, new(big.Int).Mul(big.NewInt(1000), params.ZNT1)) // call endblock + sdb.SubBalance(utils.NodeManagerContractAddress, big.NewInt(800000000000000000)) param10 := new(EndBlockParam) input, err = param10.Encode() assert.Nil(t, err) @@ -959,7 +961,7 @@ func TestDistribute(t *testing.T) { b7, _ := new(big.Int).SetString("1000092307692307692280000", 10) assert.Equal(t, sdb.GetBalance(stakeAddress), b6) assert.Equal(t, sdb.GetBalance(stakeAddress2), b7) - assert.Equal(t, sdb.GetBalance(common.EmptyAddress), new(big.Int).SetUint64(180000)) + assert.Equal(t, sdb.GetBalance(common.EmptyAddress), new(big.Int).SetUint64(600000000000180000)) _, found, err := getValidator(contractQuery, validatorsKey[0].ConsensusAddr) assert.Nil(t, err) assert.Equal(t, found, false) @@ -1025,6 +1027,7 @@ func TestPerformance(t *testing.T) { assert.Nil(t, err) // call endblock + sdb.SubBalance(utils.NodeManagerContractAddress, big.NewInt(800000000000000000)) sdb.AddBalance(utils.NodeManagerContractAddress, new(big.Int).Mul(big.NewInt(10000000), params.ZNT1)) param := new(EndBlockParam) input, err = param.Encode()