kaspad/domain/dagconfig/consensus_defaults.go
stasatdaglabs 3dbc42b4f7
Implement the new block subsidy function (#1830)
* Replace the old blockSubsidy parameters with the new ones.

* Return subsidyGenesisReward if blockHash is the genesis hash.

* Traverse a block's past for the subsidy calculation.

* Partially implement SubsidyStore.

* Refer to SubsidyStore from CoinbaseManager.

* Wrap calcBlockSubsidy in getBlockSubsidy, which first checks the database.

* Fix finalityStore not calling GenerateShardingID.

* Implement calculateAveragePastSubsidy.

* Implement calculateMergeSetSubsidySum.

* Implement calculateSubsidyRandomVariable.

* Implement calcBlockSubsidy.

* Add a TODO about floats.

* Update the calcBlockSubsidy TODO.

* Use binary.LittleEndian in calculateSubsidyRandomVariable.

* Fix bad range in calculateSubsidyRandomVariable.

* Replace float64 with big.Rat everywhere except for subsidyRandomVariable.

* Fix a nil dereference.

* Use a random walk to approximate the normal distribution.

* In order to avoid unsupported fractional results from powInt64, flip the numerator and the denominator manually.

* Set standardDeviation to 0.25, MaxSompi to 10_000_000_000 * SompiPerKaspa and defaultSubsidyGenesisReward to 1_000.

* Set the standard deviation to 0.2.

* Use a binomial distribution instead of trying to estimate the normal distribution.

* Change some values around.

* Clamp the block subsidy.

* Remove the fake duplicate constants in the util package.

* Reduce MaxSompi to only 100m Kaspa to avoid hitting the uint64 ceiling.

* Lower MaxSompi further to avoid new and exciting ways for the uint64 ceiling to be hit.

* Remove debug logs.

* Fix a couple of failing tests.

* Fix TestBlockWindow.

* Fix limitTransactionCount sometimes crashing on index-out-of-bounds.

* In TrustedDataDataDAABlock, replace BlockHeader with DomainBlock

* In calculateAveragePastSubsidy, use blockWindow instead of doing a BFS manually.

* Remove the reference to DAGTopologyManager in coinbaseManager.

* Add subsidy to the coinbase payload.

* Get rid of the subsidy store and extract subsidies out of coinbase transactions.

* Keep a blockWindow amount of blocks under the virtual for IBD purposes.

* Manually remove the virtual genesis from the merge set.

* Fix simnet genesis.

* Fix TestPruning.

* Fix TestCheckBlockIsNotPruned.

* Fix TestBlockWindow.

* Fix TestCalculateSignatureHashSchnorr.

* Fix TestCalculateSignatureHashECDSA.

* Fix serializing the wrong value into the coinbase payload.

* Rename coinbaseOutputForBlueBlock to coinbaseOutputAndSubsidyForBlueBlock.

* Add a TODO about optimizing trusted data DAA window blocks.

* Expand on a comment in TestCheckBlockIsNotPruned.

* In calcBlockSubsidy, divide the big.Int numerator by the big.Int denominator instead of converting to float64.

* Clarify a comment.

* Rename SubsidyMinGenesisReward to MinSubsidy.

* Properly handle trusted data blocks in calculateMergeSetSubsidySum.

* Use the first two bytes of the selected parent's hash for randomness instead of math/rand.

* Restore maxSompi to what it used to be.

* Fix TestPruning.

* Fix TestAmountCreation.

* Fix TestBlockWindow.

* Fix TestAmountUnitConversions.

* Increase the timeout in many-tips to 30 minutes.

* Check coinbase subsidy for every block

* Re-rename functions

* Use shift instead of powInt64 to determine subsidyRandom

Co-authored-by: Ori Newman <orinewman1@gmail.com>
2021-10-30 10:16:47 +03:00

78 lines
4.9 KiB
Go

package dagconfig
import (
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
"time"
)
// The documentation refers to the following constants which aren't explicated in the code:
// d - an upper bound on the round trip time of a block
// delta - the expected fraction of time the width of the network exceeds defaultGHOSTDAGK
//
// For more information about defaultGHOSTDAGK, and its dependency on delta and defaultTargetTimePerBlock
// please refer to the PHANTOM paper: https://eprint.iacr.org/2018/104.pdf
//
// For more information about the DAA constants defaultDifficultyAdjustmentWindowSize, defaultTimestampDeviationTolerance,
// and their relation to defaultGHOSTDAGK and defaultTargetTimePerBlock see:
// https://research.kas.pa/t/handling-timestamp-manipulations/97
//
// For more information about defaultMergeSetSizeLimit, defaultFinalityDuration and their relation to pruning, see:
// https://research.kas.pa/t/a-proposal-for-finality-in-ghostdag/66/17
// https://research.kas.pa/t/some-of-the-intuition-behind-the-design-of-the-invalidation-rules-for-pruning/95
//
const (
defaultMaxCoinbasePayloadLength = 150
// defaultMaxBlockMass is a bound on the mass of a block, larger values increase the bound d
// on the round trip time of a block, which affects the other parameters as described below
defaultMaxBlockMass = 500_000
// defaultMassPerTxByte, defaultMassPerScriptPubKeyByte and defaultMassPerSigOp define the number of grams per
// transaction byte, script pub key byte and sig op respectively.
// These values are used when calculating a transactions mass.
defaultMassPerTxByte = 1
defaultMassPerScriptPubKeyByte = 10
defaultMassPerSigOp = 1000
// defaultMaxBlockParents is the number of blocks any block can point to.
// Should be about d/defaultTargetTimePerBlock where d is a bound on the round trip time of a block.
defaultMaxBlockParents = 10
// defaultGHOSTDAGK is a bound on the number of blue blocks in the anticone of a blue block. Approximates the maximal
// width of the network.
// Formula (1) in section 4.2 of the PHANTOM paper shows how to calculate defaultGHOSTDAGK. The delta term represents a bound
// on the expected fraction of the network life in which the width was higher than defaultGHOSTDAGK. The current value of K
// was calculated for d = 5 seconds and delta = 0.05.
defaultGHOSTDAGK = 18
// defaultMergeSetSizeLimit is a bound on the size of the past of a block and the size of the past
// of its selected parent. Any block which violates this bound is invalid.
// Should be at least an order of magnitude smaller than defaultFinalityDuration/defaultTargetTimePerBlock.
// (Higher values make pruning attacks easier by a constant, lower values make merging after a split or a spike
// in block take longer)
defaultMergeSetSizeLimit = defaultGHOSTDAGK * 10
defaultSubsidyGenesisReward = 1 * constants.SompiPerKaspa
defaultMinSubsidy = 1 * constants.SompiPerKaspa
defaultMaxSubsidy = 1000 * constants.SompiPerKaspa
defaultCoinbasePayloadScriptPublicKeyMaxLength = 150
// defaultDifficultyAdjustmentWindowSize is the number of blocks in a block's past used to calculate its difficulty
// target.
// The DAA should take the median of 2640 blocks, so in order to do that we need 2641 window size.
defaultDifficultyAdjustmentWindowSize = 2641
// defaultTimestampDeviationTolerance is the allowed deviance of an inconming block's timestamp, measured in block delays.
// A new block can't hold a timestamp lower than the median timestamp of the (defaultTimestampDeviationTolerance*2-1) blocks
// with highest accumulated blue work in its past, such blocks are considered invalid.
// A new block can't hold a timestamp higher than the local system time + defaultTimestampDeviationTolerance/defaultTargetTimePerBlock,
// such blocks are not marked as invalid but are rejected.
defaultTimestampDeviationTolerance = 132
// defaultFinalityDuration is an approximate lower bound of how old the finality block is. The finality block is chosen to
// be the newest block in the selected chain whose blue score difference from the selected tip is at least
// defaultFinalityDuration/defaultTargetTimePerBlock.
// The pruning block is selected similarly, with the following duration:
// pruning block duration =
// 2*defaultFinalityDuration/defaultTargetTimePerBlock + 4*defaultMergeSetSizeLimit*defaultGHOSTDAGK + 2*defaultGHOSTDAGK + 2
defaultFinalityDuration = 24 * time.Hour
// defaultTargetTimePerBlock represents how much time should pass on average between two consecutive block creations.
// Should be parametrized such that the average width of the DAG is about defaultMaxBlockParents and such that most of the
// time the width of the DAG is at most defaultGHOSTDAGK.
defaultTargetTimePerBlock = 1 * time.Second
defaultPruningProofM = 1000
)