go-blockchain/consensus/reward_test.go
Claude cf20259e96
feat(consensus): block reward with size penalty
BaseReward returns premine at genesis, fixed 1 LTHN otherwise.
BlockReward applies the C++ size penalty using 128-bit arithmetic.
MinerReward handles pre/post HF4 fee treatment.

Co-Authored-By: Charon <charon@lethean.io>
2026-02-21 00:42:46 +00:00

65 lines
1.9 KiB
Go

//go:build !integration
package consensus
import (
"testing"
"forge.lthn.ai/core/go-blockchain/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBaseReward_Good(t *testing.T) {
assert.Equal(t, config.Premine, BaseReward(0), "genesis returns premine")
assert.Equal(t, config.BlockReward, BaseReward(1), "block 1 returns standard reward")
assert.Equal(t, config.BlockReward, BaseReward(10000), "arbitrary height")
}
func TestBlockReward_Good(t *testing.T) {
base := config.BlockReward
// Small block: full reward.
reward, err := BlockReward(base, 1000, config.BlockGrantedFullRewardZone)
require.NoError(t, err)
assert.Equal(t, base, reward)
// Block at exactly the zone boundary: full reward.
reward, err = BlockReward(base, config.BlockGrantedFullRewardZone, config.BlockGrantedFullRewardZone)
require.NoError(t, err)
assert.Equal(t, base, reward)
}
func TestBlockReward_Bad(t *testing.T) {
base := config.BlockReward
median := config.BlockGrantedFullRewardZone
// Block larger than 2*median: rejected.
_, err := BlockReward(base, 2*median+1, median)
assert.Error(t, err)
assert.Contains(t, err.Error(), "too large")
}
func TestBlockReward_Ugly(t *testing.T) {
base := config.BlockReward
median := config.BlockGrantedFullRewardZone
// Block slightly over zone: penalty applied, reward < base.
reward, err := BlockReward(base, median+10_000, median)
require.NoError(t, err)
assert.Less(t, reward, base, "penalty should reduce reward")
assert.Greater(t, reward, uint64(0), "reward should be positive")
}
func TestMinerReward_Good(t *testing.T) {
base := config.BlockReward
fees := uint64(50_000_000_000) // 0.05 LTHN
// Pre-HF4: fees added.
total := MinerReward(base, fees, false)
assert.Equal(t, base+fees, total)
// Post-HF4: fees burned.
total = MinerReward(base, fees, true)
assert.Equal(t, base, total)
}