dcrd/blockchain/subsidy.go
Dave Collins b6d426241d blockchain: Rework to use new db interface.
This commit is the first stage of several that are planned to convert
the blockchain package into a concurrent safe package that will
ultimately allow support for multi-peer download and concurrent chain
processing.  The goal is to update btcd proper after each step so it can
take advantage of the enhancements as they are developed.

In addition to the aforementioned benefit, this staged approach has been
chosen since it is absolutely critical to maintain consensus.
Separating the changes into several stages makes it easier for reviewers
to logically follow what is happening and therefore helps prevent
consensus bugs.  Naturally there are significant automated tests to help
prevent consensus issues as well.

The main focus of this stage is to convert the blockchain package to use
the new database interface and implement the chain-related functionality
which it no longer handles.  It also aims to improve efficiency in
various areas by making use of the new database and chain capabilities.

The following is an overview of the chain changes:

- Update to use the new database interface
- Add chain-related functionality that the old database used to handle
  - Main chain structure and state
  - Transaction spend tracking
- Implement a new pruned unspent transaction output (utxo) set
  - Provides efficient direct access to the unspent transaction outputs
  - Uses a domain specific compression algorithm that understands the
    standard transaction scripts in order to significantly compress them
  - Removes reliance on the transaction index and paves the way toward
    eventually enabling block pruning
- Modify the New function to accept a Config struct instead of
  inidividual parameters
- Replace the old TxStore type with a new UtxoViewpoint type that makes
  use of the new pruned utxo set
- Convert code to treat the new UtxoViewpoint as a rolling view that is
  used between connects and disconnects to improve efficiency
- Make best chain state always set when the chain instance is created
  - Remove now unnecessary logic for dealing with unset best state
- Make all exported functions concurrent safe
  - Currently using a single chain state lock as it provides a straight
    forward and easy to review path forward however this can be improved
    with more fine grained locking
- Optimize various cases where full blocks were being loaded when only
  the header is needed to help reduce the I/O load
- Add the ability for callers to get a snapshot of the current best
  chain stats in a concurrent safe fashion
  - Does not block callers while new blocks are being processed
- Make error messages that reference transaction outputs consistently
  use <transaction hash>:<output index>
- Introduce a new AssertError type an convert internal consistency
  checks to use it
- Update tests and examples to reflect the changes
- Add a full suite of tests to ensure correct functionality of the new
  code

The following is an overview of the btcd changes:

- Update to use the new database and chain interfaces
- Temporarily remove all code related to the transaction index
- Temporarily remove all code related to the address index
- Convert all code that uses transaction stores to use the new utxo
  view
- Rework several calls that required the block manager for safe
  concurrency to use the chain package directly now that it is
  concurrent safe
- Change all calls to obtain the best hash to use the new best state
  snapshot capability from the chain package
- Remove workaround for limits on fetching height ranges since the new
  database interface no longer imposes them
- Correct the gettxout RPC handler to return the best chain hash as
  opposed the hash the txout was found in
- Optimize various RPC handlers:
  - Change several of the RPC handlers to use the new chain snapshot
    capability to avoid needlessly loading data
  - Update several handlers to use new functionality to avoid accessing
    the block manager so they are able to return the data without
    blocking when the server is busy processing blocks
  - Update non-verbose getblock to avoid deserialization and
    serialization overhead
  - Update getblockheader to request the block height directly from
    chain and only load the header
  - Update getdifficulty to use the new cached data from chain
  - Update getmininginfo to use the new cached data from chain
  - Update non-verbose getrawtransaction to avoid deserialization and
    serialization overhead
  - Update gettxout to use the new utxo store versus loading
    full transactions using the transaction index

The following is an overview of the utility changes:
- Update addblock to use the new database and chain interfaces
- Update findcheckpoint to use the new database and chain interfaces
- Remove the dropafter utility which is no longer supported

NOTE: The transaction index and address index will be reimplemented in
another commit.
2016-08-18 15:42:18 -04:00

372 lines
11 KiB
Go

// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockchain
import (
"bytes"
"fmt"
"sync"
"github.com/decred/dcrd/blockchain/stake"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/txscript"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrutil"
)
// The number of values to precalculate on initialization of the subsidy
// cache.
const subsidyCacheInitWidth = 4
// SubsidyCache is a structure that caches calculated values of subsidy so that
// they're not constantly recalculated. The blockchain struct itself possesses a
// pointer to a preinitialized SubsidyCache.
type SubsidyCache struct {
subsidyCache map[uint64]int64
subsidyCacheLock sync.RWMutex
params *chaincfg.Params
}
// NewSubsidyCache initializes a new subsidy cache for a given height. It
// precalculates the values of the subsidy that are most likely to be seen by
// the client when it connects to the network.
func NewSubsidyCache(height int64, params *chaincfg.Params) *SubsidyCache {
scm := make(map[uint64]int64)
sc := SubsidyCache{
subsidyCache: scm,
params: params,
}
iteration := uint64(height / params.ReductionInterval)
if iteration < subsidyCacheInitWidth {
return &sc
}
for i := iteration - 4; i <= iteration; i++ {
sc.CalcBlockSubsidy(int64(iteration) * params.ReductionInterval)
}
return &sc
}
// CalcBlockSubsidy returns the subsidy amount a block at the provided height
// should have. This is mainly used for determining how much the coinbase for
// newly generated blocks awards as well as validating the coinbase for blocks
// has the expected value.
//
// Subsidy calculation for exponential reductions:
// 0 for i in range (0, height / ReductionInterval):
// 1 subsidy *= MulSubsidy
// 2 subsidy /= DivSubsidy
//
// Safe for concurrent access.
func (s *SubsidyCache) CalcBlockSubsidy(height int64) int64 {
// Block height 1 subsidy is 'special' and used to
// distribute initial tokens, if any.
if height == 1 {
return s.params.BlockOneSubsidy()
}
iteration := uint64(height / s.params.ReductionInterval)
if iteration == 0 {
return s.params.BaseSubsidy
}
// First, check the cache.
s.subsidyCacheLock.RLock()
cachedValue, existsInCache := s.subsidyCache[iteration]
s.subsidyCacheLock.RUnlock()
if existsInCache {
return cachedValue
}
// Is the previous one in the cache? If so, calculate
// the subsidy from the previous known value and store
// it in the database and the cache.
s.subsidyCacheLock.RLock()
cachedValue, existsInCache = s.subsidyCache[iteration-1]
s.subsidyCacheLock.RUnlock()
if existsInCache {
cachedValue *= s.params.MulSubsidy
cachedValue /= s.params.DivSubsidy
s.subsidyCacheLock.Lock()
s.subsidyCache[iteration] = cachedValue
s.subsidyCacheLock.Unlock()
return cachedValue
}
// Calculate the subsidy from scratch and store in the
// cache. TODO If there's an older item in the cache,
// calculate it from that to save time.
subsidy := s.params.BaseSubsidy
for i := uint64(0); i < iteration; i++ {
subsidy *= s.params.MulSubsidy
subsidy /= s.params.DivSubsidy
}
s.subsidyCacheLock.Lock()
s.subsidyCache[iteration] = subsidy
s.subsidyCacheLock.Unlock()
return subsidy
}
// CalcBlockWorkSubsidy calculates the proof of work subsidy for a block as a
// proportion of the total subsidy.
func CalcBlockWorkSubsidy(subsidyCache *SubsidyCache, height int64,
voters uint16, params *chaincfg.Params) int64 {
subsidy := subsidyCache.CalcBlockSubsidy(height)
proportionWork := int64(params.WorkRewardProportion)
proportions := int64(params.TotalSubsidyProportions())
subsidy *= proportionWork
subsidy /= proportions
// Ignore the voters field of the header before we're at a point
// where there are any voters.
if height < params.StakeValidationHeight {
return subsidy
}
// If there are no voters, subsidy is 0. The block will fail later anyway.
if voters == 0 {
return 0
}
// Adjust for the number of voters. This shouldn't ever overflow if you start
// with 50 * 10^8 Atoms and voters and potentialVoters are uint16.
potentialVoters := params.TicketsPerBlock
actual := (int64(voters) * subsidy) / int64(potentialVoters)
return actual
}
// CalcStakeVoteSubsidy calculates the subsidy for a stake vote based on the height
// of its input SStx.
//
// Safe for concurrent access.
func CalcStakeVoteSubsidy(subsidyCache *SubsidyCache, height int64,
params *chaincfg.Params) int64 {
// Calculate the actual reward for this block, then further reduce reward
// proportional to StakeRewardProportion.
// Note that voters/potential voters is 1, so that vote reward is calculated
// irrespective of block reward.
subsidy := subsidyCache.CalcBlockSubsidy(height)
proportionStake := int64(params.StakeRewardProportion)
proportions := int64(params.TotalSubsidyProportions())
subsidy *= proportionStake
subsidy /= (proportions * int64(params.TicketsPerBlock))
return subsidy
}
// CalcBlockTaxSubsidy calculates the subsidy for the organization address in the
// coinbase.
//
// Safe for concurrent access.
func CalcBlockTaxSubsidy(subsidyCache *SubsidyCache, height int64, voters uint16,
params *chaincfg.Params) int64 {
if params.BlockTaxProportion == 0 {
return 0
}
subsidy := subsidyCache.CalcBlockSubsidy(height)
proportionTax := int64(params.BlockTaxProportion)
proportions := int64(params.TotalSubsidyProportions())
subsidy *= proportionTax
subsidy /= proportions
// Assume all voters 'present' before stake voting is turned on.
if height < params.StakeValidationHeight {
voters = 5
}
// If there are no voters, subsidy is 0. The block will fail later anyway.
if voters == 0 && height >= params.StakeValidationHeight {
return 0
}
// Adjust for the number of voters. This shouldn't ever overflow if you start
// with 50 * 10^8 Atoms and voters and potentialVoters are uint16.
potentialVoters := params.TicketsPerBlock
adjusted := (int64(voters) * subsidy) / int64(potentialVoters)
return adjusted
}
// BlockOneCoinbasePaysTokens checks to see if the first block coinbase pays
// out to the network initial token ledger.
func BlockOneCoinbasePaysTokens(tx *dcrutil.Tx,
params *chaincfg.Params) error {
// If no ledger is specified, just return true.
if len(params.BlockOneLedger) == 0 {
return nil
}
if tx.MsgTx().LockTime != 0 {
errStr := fmt.Sprintf("block 1 coinbase has invalid locktime")
return ruleError(ErrBlockOneTx, errStr)
}
if tx.MsgTx().Expiry != wire.NoExpiryValue {
errStr := fmt.Sprintf("block 1 coinbase has invalid expiry")
return ruleError(ErrBlockOneTx, errStr)
}
if tx.MsgTx().TxIn[0].Sequence != wire.MaxTxInSequenceNum {
errStr := fmt.Sprintf("block 1 coinbase not finalized")
return ruleError(ErrBlockOneInputs, errStr)
}
if len(tx.MsgTx().TxOut) == 0 {
errStr := fmt.Sprintf("coinbase outputs empty in block 1")
return ruleError(ErrBlockOneOutputs, errStr)
}
ledger := params.BlockOneLedger
if len(ledger) != len(tx.MsgTx().TxOut) {
errStr := fmt.Sprintf("wrong number of outputs in block 1 coinbase; "+
"got %v, expected %v", len(tx.MsgTx().TxOut), len(ledger))
return ruleError(ErrBlockOneOutputs, errStr)
}
// Check the addresses and output amounts against those in the ledger.
for i, txout := range tx.MsgTx().TxOut {
if txout.Version != txscript.DefaultScriptVersion {
errStr := fmt.Sprintf("bad block one output version; want %v, got %v",
txscript.DefaultScriptVersion, txout.Version)
return ruleError(ErrBlockOneOutputs, errStr)
}
// There should only be one address.
_, addrs, _, err :=
txscript.ExtractPkScriptAddrs(txout.Version, txout.PkScript, params)
if len(addrs) != 1 {
errStr := fmt.Sprintf("too many addresses in output")
return ruleError(ErrBlockOneOutputs, errStr)
}
addrLedger, err := dcrutil.DecodeAddress(ledger[i].Address, params)
if err != nil {
return err
}
if !bytes.Equal(addrs[0].ScriptAddress(), addrLedger.ScriptAddress()) {
errStr := fmt.Sprintf("address in output %v has non matching "+
"address; got %v (hash160 %x), want %v (hash160 %x)",
i,
addrs[0].EncodeAddress(),
addrs[0].ScriptAddress(),
addrLedger.EncodeAddress(),
addrLedger.ScriptAddress())
return ruleError(ErrBlockOneOutputs, errStr)
}
if txout.Value != ledger[i].Amount {
errStr := fmt.Sprintf("address in output %v has non matching "+
"amount; got %v, want %v", i, txout.Value, ledger[i].Amount)
return ruleError(ErrBlockOneOutputs, errStr)
}
}
return nil
}
// CoinbasePaysTax checks to see if a given block's coinbase correctly pays
// tax to the developer organization.
func CoinbasePaysTax(subsidyCache *SubsidyCache, tx *dcrutil.Tx, height uint32,
voters uint16, params *chaincfg.Params) error {
// Taxes only apply from block 2 onwards.
if height <= 1 {
return nil
}
// Tax is disabled.
if params.BlockTaxProportion == 0 {
return nil
}
if len(tx.MsgTx().TxOut) == 0 {
errStr := fmt.Sprintf("invalid coinbase (no outputs)")
return ruleError(ErrNoTxOutputs, errStr)
}
// Coinbase output 0 must be the subsidy to the dev organization.
taxPkVersion := tx.MsgTx().TxOut[0].Version
taxPkScript := tx.MsgTx().TxOut[0].PkScript
class, addrs, _, err :=
txscript.ExtractPkScriptAddrs(taxPkVersion, taxPkScript, params)
// The script can't be a weird class.
if !(class == txscript.ScriptHashTy ||
class == txscript.PubKeyHashTy ||
class == txscript.PubKeyTy) {
errStr := fmt.Sprintf("wrong script class for tax output")
return ruleError(ErrNoTax, errStr)
}
// There should only be one address.
if len(addrs) != 1 {
errStr := fmt.Sprintf("no or too many addresses in output")
return ruleError(ErrNoTax, errStr)
}
// Decode the organization address.
addrOrg, err := dcrutil.DecodeAddress(params.OrganizationAddress, params)
if err != nil {
return err
}
if !bytes.Equal(addrs[0].ScriptAddress(), addrOrg.ScriptAddress()) {
errStr := fmt.Sprintf("address in output 0 has non matching org "+
"address; got %v (hash160 %x), want %v (hash160 %x)",
addrs[0].EncodeAddress(),
addrs[0].ScriptAddress(),
addrOrg.EncodeAddress(),
addrOrg.ScriptAddress())
return ruleError(ErrNoTax, errStr)
}
// Get the amount of subsidy that should have been paid out to
// the organization, then check it.
orgSubsidy := CalcBlockTaxSubsidy(subsidyCache, int64(height), voters, params)
amountFound := tx.MsgTx().TxOut[0].Value
if orgSubsidy != amountFound {
errStr := fmt.Sprintf("amount in output 0 has non matching org "+
"calculated amount; got %v, want %v", amountFound, orgSubsidy)
return ruleError(ErrNoTax, errStr)
}
return nil
}
// CalculateAddedSubsidy calculates the amount of subsidy added by a block
// and its parent. The blocks passed to this function MUST be valid blocks
// that have already been confirmed to abide by the consensus rules of the
// network, or the function might panic.
func CalculateAddedSubsidy(block, parent *dcrutil.Block) int64 {
var subsidy int64
regularTxTreeValid := dcrutil.IsFlagSet16(block.MsgBlock().Header.VoteBits,
dcrutil.BlockValid)
if regularTxTreeValid {
subsidy += parent.MsgBlock().Transactions[0].TxIn[0].ValueIn
}
for _, stx := range block.STransactions() {
if isSSGen, _ := stake.IsSSGen(stx); isSSGen {
subsidy += stx.MsgTx().TxIn[0].ValueIn
}
}
return subsidy
}