Skip to content

Commit

Permalink
fix: apaply comment and fix test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
egonspace committed May 20, 2024
1 parent 393fc74 commit fe76fa8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
14 changes: 7 additions & 7 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ var (
BlockReward: big.NewInt(1e18),
FirstHalvingBlock: big.NewInt(53_557_371),
HalvingPeriod: big.NewInt(63_115_200),
NoRewardHereafter: big.NewInt(1_000_000_000), // TODO fix last reward block
FinishRewardBlock: big.NewInt(1_000_000_000), // TODO fix last reward block
HalvingTimes: 16,
HalvingRate: 50,
},
Expand Down Expand Up @@ -195,7 +195,7 @@ var (
BlockReward: big.NewInt(1e18),
FirstHalvingBlock: big.NewInt(60_537_845),
HalvingPeriod: big.NewInt(63_115_200),
NoRewardHereafter: big.NewInt(1_000_000_000), // TODO fix last reward block
FinishRewardBlock: big.NewInt(1_000_000_000), // TODO fix last reward block
HalvingTimes: 16,
HalvingRate: 50,
},
Expand Down Expand Up @@ -434,7 +434,7 @@ type BriocheConfig struct {
BlockReward *big.Int `json:"blockReward,omitempty"` // nil - use default block reward(1e18)
FirstHalvingBlock *big.Int `json:"firstHalvingBlock,omitempty"` // nil - halving is not work. including this block
HalvingPeriod *big.Int `json:"halvingPeriod,omitempty"` // nil - halving is not work
NoRewardHereafter *big.Int `json:"noRewardHereafter,omitempty"` // nil - block reward goes on endlessly
FinishRewardBlock *big.Int `json:"finishRewardBlock,omitempty"` // nil - block reward goes on endlessly
HalvingTimes uint64 `json:"halvingTimes,omitempty"` // 0 - no halving
HalvingRate uint32 `json:"halvingRate,omitempty"` // 0 - no reward on halving; 100 - no halving; >100 - increasing reward
}
Expand All @@ -445,20 +445,20 @@ func (bc *BriocheConfig) GetBriocheBlockReward(defaultReward *big.Int, num *big.
if bc.BlockReward != nil {
blockReward = big.NewInt(0).Set(bc.BlockReward)
}
if bc.NoRewardHereafter != nil &&
bc.NoRewardHereafter.Cmp(num) <= 0 {
if bc.FinishRewardBlock != nil &&
bc.FinishRewardBlock.Cmp(num) <= 0 {
blockReward = big.NewInt(0)
} else if bc.FirstHalvingBlock != nil &&
bc.HalvingPeriod != nil &&
bc.HalvingTimes > 0 &&
num.Cmp(bc.FirstHalvingBlock) >= 0 {
blockReward = bc.halveRewards(blockReward, num)
blockReward = bc.calcHalvedReward(blockReward, num)
}
}
return blockReward
}

func (bc *BriocheConfig) halveRewards(baseReward *big.Int, num *big.Int) *big.Int {
func (bc *BriocheConfig) calcHalvedReward(baseReward *big.Int, num *big.Int) *big.Int {
elapsed := new(big.Int).Sub(num, bc.FirstHalvingBlock)
times := new(big.Int).Add(common.Big1, new(big.Int).Div(elapsed, bc.HalvingPeriod))
if times.Uint64() > bc.HalvingTimes {
Expand Down
40 changes: 20 additions & 20 deletions params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ func TestHalveRewards(t *testing.T) {
HalvingTimes: tc.times,
HalvingRate: tc.rate,
}
halved := brioche.halveRewards(tc.reward, tc.past)
halved := brioche.calcHalvedReward(tc.reward, tc.past)
if tc.expected.Cmp(halved) != 0 {
t.Errorf("halveRewards mismatched (expected=%v, actual=%v)", tc.expected, halved)
t.Errorf("halved reward mismatched (expected=%v, actual=%v)", tc.expected, halved)
}
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(1e18),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(101),
FinishRewardBlock: big.NewInt(101),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -200,7 +200,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(1e18),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(201),
FinishRewardBlock: big.NewInt(201),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -215,7 +215,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(7e18),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(101),
FinishRewardBlock: big.NewInt(101),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -228,7 +228,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(3),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(101),
FinishRewardBlock: big.NewInt(101),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -241,7 +241,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(1),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(101),
FinishRewardBlock: big.NewInt(101),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -254,7 +254,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: nil, // it will use the default block reward
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(101),
FinishRewardBlock: big.NewInt(101),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -269,7 +269,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: nil, // it will use the default block reward
FirstHalvingBlock: nil,
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(101),
FinishRewardBlock: big.NewInt(101),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -282,7 +282,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(10),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: nil,
NoRewardHereafter: big.NewInt(101),
FinishRewardBlock: big.NewInt(101),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -295,7 +295,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(10),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(101),
FinishRewardBlock: big.NewInt(101),
HalvingTimes: 0,
HalvingRate: 50,
},
Expand All @@ -308,7 +308,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(10),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(200),
FinishRewardBlock: big.NewInt(200),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -321,7 +321,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(10),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(200),
FinishRewardBlock: big.NewInt(200),
HalvingTimes: 10,
HalvingRate: 100, // no halving rate
},
Expand All @@ -336,7 +336,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(10),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(200),
FinishRewardBlock: big.NewInt(200),
HalvingTimes: 10,
HalvingRate: 0, // no reward
},
Expand All @@ -349,7 +349,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(10),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(200),
FinishRewardBlock: big.NewInt(200),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -362,7 +362,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(10),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(200),
FinishRewardBlock: big.NewInt(200),
HalvingTimes: 10,
HalvingRate: 50,
},
Expand All @@ -377,7 +377,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(1e18),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(1000),
FinishRewardBlock: big.NewInt(1000),
HalvingTimes: 10,
HalvingRate: 10,
},
Expand All @@ -390,7 +390,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(1e18),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(1000),
FinishRewardBlock: big.NewInt(1000),
HalvingTimes: 10,
HalvingRate: 10,
},
Expand All @@ -403,7 +403,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(1e18),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(1000),
FinishRewardBlock: big.NewInt(1000),
HalvingTimes: 10,
HalvingRate: 1,
},
Expand All @@ -416,7 +416,7 @@ func TestGetBriocheBlockReward(t *testing.T) {
BlockReward: big.NewInt(1e18),
FirstHalvingBlock: big.NewInt(100),
HalvingPeriod: big.NewInt(100),
NoRewardHereafter: big.NewInt(1000),
FinishRewardBlock: big.NewInt(1000),
HalvingTimes: 10,
HalvingRate: 99,
},
Expand Down
12 changes: 6 additions & 6 deletions wemix/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestDistributeRewards(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Call the distributeRewards function
rewards, err := distributeRewards(tt.height, tt.rp, defaultBriocheBlockReward, tt.fees)
rewards, err := distributeRewards(tt.height, tt.rp, tt.rp.rewardAmount, tt.fees)
rewardsString, _ := json.Marshal(rewards)
if string(rewardsString) != tt.want {
t.Errorf("distributeRewards() failed: %v, %v <-> %v", err, tt.want, string(rewardsString))
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestRewardValidation(t *testing.T) {
BlockReward: big.NewInt(100),
FirstHalvingBlock: big.NewInt(0),
HalvingPeriod: big.NewInt(10),
NoRewardHereafter: big.NewInt(30),
FinishRewardBlock: big.NewInt(30),
HalvingTimes: 3,
HalvingRate: 50,
}},
Expand Down Expand Up @@ -280,7 +280,7 @@ func TestRewardValidation(t *testing.T) {
BlockReward: big.NewInt(200), // different reward!!
FirstHalvingBlock: gspec.Config.Brioche.FirstHalvingBlock,
HalvingPeriod: gspec.Config.Brioche.HalvingPeriod,
NoRewardHereafter: gspec.Config.Brioche.NoRewardHereafter,
FinishRewardBlock: gspec.Config.Brioche.FinishRewardBlock,
HalvingTimes: gspec.Config.Brioche.HalvingTimes,
HalvingRate: gspec.Config.Brioche.HalvingRate,
}}
Expand All @@ -293,11 +293,11 @@ func TestRewardValidation(t *testing.T) {
})

if _, err := blockchain.InsertChain(blocks); err != nil {
if !strings.HasPrefix(err.Error(), "Remote block hash is different") {
if !strings.HasPrefix(err.Error(), "remote block hash is different") {
t.Fatal(err)
}
} else {
t.Fatal("Reward validation failed")
t.Fatal("reward validation failed")
}
}

Expand Down Expand Up @@ -327,7 +327,7 @@ func TestBriocheHardFork(t *testing.T) {
BlockReward: big.NewInt(4e17),
FirstHalvingBlock: big.NewInt(4),
HalvingPeriod: big.NewInt(2),
NoRewardHereafter: big.NewInt(7),
FinishRewardBlock: big.NewInt(7),
HalvingTimes: 2,
HalvingRate: 50,
}},
Expand Down

0 comments on commit fe76fa8

Please sign in to comment.