Skip to content

Commit

Permalink
fix: modify more big.NewInt(0)
Browse files Browse the repository at this point in the history
  • Loading branch information
egonspace committed May 27, 2024
1 parent 237f883 commit 5181ef8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,14 @@ type BriocheConfig struct {
}

func (bc *BriocheConfig) GetBriocheBlockReward(defaultReward *big.Int, num *big.Int) *big.Int {
blockReward := big.NewInt(0).Set(defaultReward) // default brioche block reward
blockReward := new(big.Int).Set(defaultReward) // default brioche block reward
if bc != nil {
if bc.BlockReward != nil {
blockReward = big.NewInt(0).Set(bc.BlockReward)
blockReward = new(big.Int).Set(bc.BlockReward)
}
if bc.FinishRewardBlock != nil &&
bc.FinishRewardBlock.Cmp(num) <= 0 {
blockReward = big.NewInt(0)
blockReward = new(big.Int)
} else if bc.FirstHalvingBlock != nil &&
bc.HalvingPeriod != nil &&
bc.HalvingTimes > 0 &&
Expand Down
8 changes: 4 additions & 4 deletions params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
HalvingRate: 50,
},
blockNum: big.NewInt(100),
expected: big.NewInt(0),
expected: new(big.Int),
},
{
id: 7,
Expand All @@ -259,7 +259,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
HalvingRate: 50,
},
blockNum: big.NewInt(100),
expected: big.NewInt(0).Div(defaultBlockReward, big.NewInt(2)),
expected: new(big.Int).Div(defaultBlockReward, big.NewInt(2)),
},

// no halving
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
HalvingRate: 0, // no reward
},
blockNum: big.NewInt(100),
expected: big.NewInt(0),
expected: new(big.Int),
},
{
id: 14,
Expand All @@ -367,7 +367,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
HalvingRate: 50,
},
blockNum: big.NewInt(200),
expected: big.NewInt(0),
expected: new(big.Int),
},

// halving rate variations
Expand Down
14 changes: 7 additions & 7 deletions wemix/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ type rewardParameters struct {

var (
// "Wemix Registry"
magic, _ = big.NewInt(0).SetString("0x57656d6978205265676973747279", 0)
magic, _ = new(big.Int).SetString("0x57656d6978205265676973747279", 0)
etcdClusterName = "Wemix"
big0 = big.NewInt(0)
big0 = new(big.Int)
nilAddress = common.Address{}
defaultBriocheBlockReward = big.NewInt(1e18)
admin *wemixAdmin
Expand Down Expand Up @@ -1056,7 +1056,7 @@ func distributeRewards(height *big.Int, rp *rewardParameters, blockReward *big.I

var rewards []reward
if n := len(rp.members); n > 0 {
stakeTotal, equalStakes := big.NewInt(0), true
stakeTotal, equalStakes := new(big.Int), true
for i := 0; i < n; i++ {
if equalStakes && i < n-1 && rp.members[i].Stake.Cmp(rp.members[i+1].Stake) != 0 {
equalStakes = false
Expand All @@ -1065,7 +1065,7 @@ func distributeRewards(height *big.Int, rp *rewardParameters, blockReward *big.I
}

if equalStakes {
v0, v1 := big.NewInt(0), big.NewInt(1)
v0, v1 := new(big.Int), big.NewInt(1)
vn := big.NewInt(int64(n))
b := new(big.Int).Set(minerAmount)
d := new(big.Int)
Expand All @@ -1084,7 +1084,7 @@ func distributeRewards(height *big.Int, rp *rewardParameters, blockReward *big.I
}
} else {
// rewards distributed according to stakes
v0, v1 := big.NewInt(0), big.NewInt(1)
v0, v1 := new(big.Int), big.NewInt(1)
remainder := new(big.Int).Set(minerAmount)
for i := 0; i < n; i++ {
memberReward := new(big.Int).Mul(minerAmount, rp.members[i].Stake)
Expand Down Expand Up @@ -1427,8 +1427,8 @@ func getBlockBuildParameters(height *big.Int) (blockInterval int64, maxBaseFee,

// default values
blockInterval = 15
maxBaseFee = big.NewInt(0)
gasLimit = big.NewInt(0)
maxBaseFee = new(big.Int)
gasLimit = new(big.Int)
baseFeeMaxChangeRate = 0
gasTargetPercentage = 100

Expand Down
22 changes: 11 additions & 11 deletions wemix/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,21 @@ func TestDistributeRewards(t *testing.T) {
t.Errorf("distributeRewards() failed: %v, %v <-> %v", err, tt.want, string(rewardsString))
}

distTotal := big.NewInt(0)
distTotal := new(big.Int)
for _, dist := range tt.rp.distributionMethod {
distTotal.Add(distTotal, dist)
}
totalRewards := big.NewInt(0)
memberRewards := big.NewInt(0)
totalRewards := new(big.Int)
memberRewards := new(big.Int)
for i, reward := range rewards {
totalRewards.Add(totalRewards, reward.Reward)
if i < len(tt.rp.members) {
memberRewards.Add(memberRewards, reward.Reward)
}
}
totalAmount := big.NewInt(0).Set(tt.rp.rewardAmount)
totalAmount := new(big.Int).Set(tt.rp.rewardAmount)
totalAmount.Add(totalAmount, tt.fees)
memberAmount := big.NewInt(0).Set(tt.rp.rewardAmount)
memberAmount := new(big.Int).Set(tt.rp.rewardAmount)
memberAmount = memberAmount.Mul(memberAmount, tt.rp.distributionMethod[0])
memberAmount = memberAmount.Div(memberAmount, distTotal)
if memberRewards.Cmp(memberAmount) != 0 {
Expand Down Expand Up @@ -226,13 +226,13 @@ func TestRewardValidation(t *testing.T) {
BriocheBlock: common.Big0,
Brioche: &params.BriocheConfig{
BlockReward: big.NewInt(100),
FirstHalvingBlock: big.NewInt(0),
FirstHalvingBlock: new(big.Int),
HalvingPeriod: big.NewInt(10),
FinishRewardBlock: big.NewInt(30),
HalvingTimes: 3,
HalvingRate: 50,
}},
Alloc: core.GenesisAlloc{address: {Balance: funds}, deleteAddr: {Balance: big.NewInt(0)}},
Alloc: core.GenesisAlloc{address: {Balance: funds}, deleteAddr: {Balance: new(big.Int)}},
}
genesis = gspec.MustCommit(db)
signer = types.LatestSigner(gspec.Config)
Expand Down Expand Up @@ -331,21 +331,21 @@ func TestBriocheHardFork(t *testing.T) {
HalvingTimes: 2,
HalvingRate: 50,
}},
Alloc: core.GenesisAlloc{address: {Balance: funds}, deleteAddr: {Balance: big.NewInt(0)}},
Alloc: core.GenesisAlloc{address: {Balance: funds}, deleteAddr: {Balance: new(big.Int)}},
}
genesis = gspec.MustCommit(db)
signer = types.LatestSigner(gspec.Config)
)

expectedBlockReward := []*big.Int{
big.NewInt(0), // zero block reward; not used
new(big.Int), // zero block reward; not used
big.NewInt(1e18),
big.NewInt(4e17),
big.NewInt(4e17),
big.NewInt(2e17),
big.NewInt(2e17),
big.NewInt(1e17),
big.NewInt(0),
new(big.Int),
}

miners := []common.Address{
Expand All @@ -371,7 +371,7 @@ func TestBriocheHardFork(t *testing.T) {
},
},
blocksPer: 1,
distributionMethod: []*big.Int{big.NewInt(5000), big.NewInt(0), big.NewInt(2500), big.NewInt(2500)}, // miner, staker, eco, maintenance
distributionMethod: []*big.Int{big.NewInt(5000), new(big.Int), big.NewInt(2500), big.NewInt(2500)}, // miner, staker, eco, maintenance
}

wemixminer.CalculateRewardsFunc = makeCalculateRewardFunc(rp)
Expand Down

0 comments on commit 5181ef8

Please sign in to comment.