Skip to content

Commit

Permalink
fix: move halving logic to BriocheConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
egonspace committed May 16, 2024
1 parent 098f5e8 commit c1bed51
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 372 deletions.
26 changes: 0 additions & 26 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
blockchain.reportBlock(block, receipts, err)
return err
}

err = blockchain.validator.ValidateState(block, statedb, receipts, usedGas, fees)
if err != nil {
blockchain.reportBlock(block, receipts, err)
Expand Down Expand Up @@ -3771,28 +3770,3 @@ func TestSetCanonical(t *testing.T) {
chain.SetCanonical(canon[DefaultCacheConfig.TriesInMemory-1])
verify(canon[DefaultCacheConfig.TriesInMemory-1])
}

func TestRewardValidation(t *testing.T) {
// Configure and generate a sample block chain
var (
db = rawdb.NewMemoryDatabase()
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
address = crypto.PubkeyToAddress(key.PublicKey)
funds = big.NewInt(1000000000)
deleteAddr = common.Address{1}
gspec = &Genesis{
Config: &params.ChainConfig{ChainID: big.NewInt(1), EIP150Block: big.NewInt(0), EIP155Block: big.NewInt(2), HomesteadBlock: new(big.Int)},
Alloc: GenesisAlloc{address: {Balance: funds}, deleteAddr: {Balance: new(big.Int)}},
}
genesis = gspec.MustCommit(db)
)

blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
defer blockchain.Stop()

blocks, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 1, nil)

if _, err := blockchain.InsertChain(blocks); err != nil {
t.Fatal(err)
}
}
43 changes: 42 additions & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ 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
HalvingTimes: 16,
HalvingRate: 50,
},
}

Expand All @@ -186,12 +189,15 @@ var (
LondonBlock: big.NewInt(0),
PangyoBlock: big.NewInt(10_000_000),
ApplepieBlock: big.NewInt(26_240_268),
BriocheBlock: big.NewInt(60_537_845), // 24-06-17 02:00:00 (UTC) expected
BriocheBlock: big.NewInt(60_537_845), // TODO fix hardfork date
Ethash: new(EthashConfig),
Brioche: &BriocheConfig{
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
HalvingTimes: 16,
HalvingRate: 50,
},
}

Expand Down Expand Up @@ -433,6 +439,41 @@ type BriocheConfig struct {
HalvingRate uint32 `json:"halvingRate,omitempty"` // 0<=HalvingRate<=100; 0 - no reward on halving; 100 - no halving
}

func (bc *BriocheConfig) GetBriocheBlockReward(defaultReward *big.Int, num *big.Int) *big.Int {
blockReward := big.NewInt(0).Set(defaultReward) // default brioche block reward
if bc != nil {
if bc.BlockReward != nil {
blockReward = big.NewInt(0).Set(bc.BlockReward)
}
if bc.NoRewardHereafter != nil &&
bc.NoRewardHereafter.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)
}
}
return blockReward
}

func (bc *BriocheConfig) halveRewards(baseReward *big.Int, num *big.Int) *big.Int {
result := big.NewInt(0).Set(baseReward)
past := big.NewInt(0).Set(num)
past.Sub(past, bc.FirstHalvingBlock)
halvingTimes := bc.HalvingTimes
for ; halvingTimes > 0; halvingTimes-- {
result = result.Mul(result, big.NewInt(int64(bc.HalvingRate)))
result = result.Div(result, big.NewInt(100))
if past.Cmp(bc.HalvingPeriod) < 0 {
break
}
past = past.Sub(past, bc.HalvingPeriod)
}
return result
}

// String implements the stringer interface, returning the consensus engine details.
func (c *EthashConfig) String() string {
return "ethash"
Expand Down
Loading

0 comments on commit c1bed51

Please sign in to comment.