Files
dchain/economy/rewards.go
vsecoder 7e7393e4f8 chore: initial commit for v0.0.1
DChain single-node blockchain + React Native messenger client.

Core:
- PBFT consensus with multi-sig validator admission + equivocation slashing
- BadgerDB + schema migration scaffold (CurrentSchemaVersion=0)
- libp2p gossipsub (tx/v1, blocks/v1, relay/v1, version/v1)
- Native Go contracts (username_registry) alongside WASM (wazero)
- WebSocket gateway with topic-based fanout + Ed25519-nonce auth
- Relay mailbox with NaCl envelope encryption (X25519 + Ed25519)
- Prometheus /metrics, per-IP rate limit, body-size cap

Deployment:
- Single-node compose (deploy/single/) with Caddy TLS + optional Prometheus
- 3-node dev compose (docker-compose.yml) with mocked internet topology
- 3-validator prod compose (deploy/prod/) for federation
- Auto-update from Gitea via /api/update-check + systemd timer
- Build-time version injection (ldflags → node --version)
- UI / Swagger toggle flags (DCHAIN_DISABLE_UI, DCHAIN_DISABLE_SWAGGER)

Client (client-app/):
- Expo / React Native / NativeWind
- E2E NaCl encryption, typing indicator, contact requests
- Auto-discovery of canonical contracts, chain_id aware, WS reconnect on node switch

Documentation:
- README.md, CHANGELOG.md, CONTEXT.md
- deploy/single/README.md with 6 operator scenarios
- deploy/UPDATE_STRATEGY.md with 4-layer forward-compat design
- docs/contracts/*.md per contract
2026-04-17 14:16:44 +03:00

72 lines
2.0 KiB
Go

// 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)
}