Skip to content

Commit

Permalink
fix governance unit test (#130)
Browse files Browse the repository at this point in the history
* fix conflict

* update with genesis config
  • Loading branch information
siovanus authored Oct 12, 2023
1 parent f44aa66 commit d364dfb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
48 changes: 43 additions & 5 deletions contracts/native/governance/node_manager/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
21 changes: 12 additions & 9 deletions contracts/native/governance/node_manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d364dfb

Please sign in to comment.