// Package economy contains helpers for computing and formatting token rewards. package economy import ( "fmt" "go-blockchain/blockchain" ) // BlockFeeBreakdown describes the fee income for a committed block validator. // There is no minting — validators earn only the fees paid by transactions. type BlockFeeBreakdown struct { ValidatorPubKey string BlockIndex uint64 TxFees uint64 // sum of all transaction fees in the block IsGenesis bool // true for block 0 (genesis allocation, not fees) GenesisAmount uint64 } // ComputeBlockReward calculates the fee income for a committed block. func ComputeBlockReward(b *blockchain.Block) BlockFeeBreakdown { d := BlockFeeBreakdown{ ValidatorPubKey: b.Validator, BlockIndex: b.Index, TxFees: b.TotalFees, } if b.Index == 0 { d.IsGenesis = true d.GenesisAmount = blockchain.GenesisAllocation } return d } // Total returns how much the validator actually receives for this block. func (d BlockFeeBreakdown) Total() uint64 { if d.IsGenesis { return d.GenesisAmount } return d.TxFees } // Summary prints a human-readable reward summary. func (d BlockFeeBreakdown) Summary() string { if d.IsGenesis { return fmt.Sprintf( "Block #0 (genesis): validator %s...%s receives genesis allocation %s", d.ValidatorPubKey[:8], d.ValidatorPubKey[len(d.ValidatorPubKey)-4:], FormatTokens(d.GenesisAmount), ) } if d.TxFees == 0 { return fmt.Sprintf( "Block #%d committed: validator %s...%s — no fees", d.BlockIndex, d.ValidatorPubKey[:8], d.ValidatorPubKey[len(d.ValidatorPubKey)-4:], ) } return fmt.Sprintf( "Block #%d committed: validator %s...%s earns %s in fees", d.BlockIndex, d.ValidatorPubKey[:8], d.ValidatorPubKey[len(d.ValidatorPubKey)-4:], FormatTokens(d.TxFees), ) } // FormatTokens converts micro-tokens to a human-readable string. func FormatTokens(microTokens uint64) string { whole := microTokens / blockchain.Token frac := microTokens % blockchain.Token return fmt.Sprintf("%d.%06d T", whole, frac) }