Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
multi: Upstream parameter abstraction sync
Browse files Browse the repository at this point in the history
Upstream commit a7b35d9.

Also, the merge commit contains necessary decred-specific alterations.
It also has the side effect of correcting a couple of min reduction bugs
even though they can't currently ever be hit due to the min reduction
interval flag being disabled for all networks.
  • Loading branch information
davecgh committed Nov 17, 2016
2 parents 563f4d1 + a7b35d9 commit 60c812e
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 136 deletions.
6 changes: 5 additions & 1 deletion blockchain/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,14 @@ func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain,
}
}

// Copy the chain params to ensure any modifications the tests do to
// the chain parameters do not affect the global instance.
paramsCopy := *params

// Create the main chain instance.
chain, err := blockchain.New(&blockchain.Config{
DB: db,
ChainParams: params,
ChainParams: &paramsCopy,
TimeSource: blockchain.NewMedianTime(),
})

Expand Down
43 changes: 21 additions & 22 deletions blockchain/difficulty.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,10 @@ func (b *BlockChain) calcEasiestDifficulty(bits uint32,
maxRetargetTimespan := int64(b.chainParams.TargetTimespan) *
b.chainParams.RetargetAdjustmentFactor

// The test network rules allow minimum difficulty blocks after more
// than twice the desired amount of time needed to generate a block has
// elapsed.
if b.chainParams.ResetMinDifficulty {
if durationVal > int64(b.chainParams.TimePerBlock)*2 {
// The test network rules allow minimum difficulty blocks once too much
// time has elapsed without mining a block.
if b.chainParams.ReduceMinDifficulty {
if durationVal > int64(b.chainParams.MinDiffReductionTime) {
return b.chainParams.PowLimitBits
}
}
Expand All @@ -206,8 +205,7 @@ func (b *BlockChain) calcEasiestDifficulty(bits uint32,
// did not have the special testnet minimum difficulty rule applied.
//
// This function MUST be called with the chain state lock held (for writes).
func (b *BlockChain) findPrevTestNetDifficulty(startNode *blockNode) (uint32,
error) {
func (b *BlockChain) findPrevTestNetDifficulty(startNode *blockNode) (uint32, error) {
// Search backwards through the chain for the last block without
// the special rule applied.
blocksPerRetarget := b.chainParams.WorkDiffWindowSize *
Expand Down Expand Up @@ -259,23 +257,21 @@ func (b *BlockChain) calcNextRequiredDifficulty(curNode *blockNode,

// We're not at a retarget point, return the oldDiff.
if (curNode.height+1)%b.chainParams.WorkDiffWindowSize != 0 {
// The test network rules allow minimum difficulty blocks after
// more than twice the desired amount of time needed to generate
// a block has elapsed.
if b.chainParams.ResetMinDifficulty {
// Return minimum difficulty when more than twice the
// desired amount of time needed to generate a block has
// elapsed.
allowMinTime := curNode.header.Timestamp.Add(
b.chainParams.TimePerBlock * b.chainParams.MinDiffResetTimeFactor)
// For networks that support it, allow special reduction of the
// required difficulty once too much time has elapsed without
// mining a block.
if b.chainParams.ReduceMinDifficulty {
// Return minimum difficulty when more than the desired
// amount of time has elapsed without mining a block.
reductionTime := b.chainParams.MinDiffReductionTime
allowMinTime := curNode.header.Timestamp.Add(reductionTime)

// For every extra target timespan that passes, we halve the
// difficulty.
if newBlockTime.After(allowMinTime) {
timePassed := newBlockTime.Sub(curNode.header.Timestamp)
timePassed -= (b.chainParams.TimePerBlock *
b.chainParams.MinDiffResetTimeFactor)
shifts := uint((timePassed / b.chainParams.TimePerBlock) + 1)
timePassed -= b.chainParams.MinDiffReductionTime
shifts := uint((timePassed / b.chainParams.TargetTimePerBlock) + 1)

// Scale the difficulty with time passed.
oldTarget := CompactToBig(curNode.header.Bits)
Expand Down Expand Up @@ -371,7 +367,11 @@ func (b *BlockChain) calcNextRequiredDifficulty(curNode *blockNode,
break // Exit for loop when we hit the end.
}

// Get the previous block node.
// Get the previous block node. This function is used over
// simply accessing firstNode.parent directly as it will
// dynamically create previous block nodes as needed. This
// helps allow only the pieces of the chain that are needed
// to remain in memory.
var err error
tempNode := oldNode
oldNode, err = b.getPrevNodeFromNode(oldNode)
Expand Down Expand Up @@ -490,8 +490,7 @@ func mergeDifficulty(oldDiff int64, newDiff1 int64, newDiff2 int64) int64 {
// and then uses it to determine the next stake difficulty.
// TODO: You can combine the first and second for loops below for a speed up
// if you'd like, I'm not sure how much it matters.
func (b *BlockChain) calcNextRequiredStakeDifficulty(curNode *blockNode) (int64,
error) {
func (b *BlockChain) calcNextRequiredStakeDifficulty(curNode *blockNode) (int64, error) {
alpha := b.chainParams.StakeDiffAlpha
stakeDiffStartHeight := int64(b.chainParams.CoinbaseMaturity) +
1
Expand Down
2 changes: 1 addition & 1 deletion blockchain/fullblocktests/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func uniqueOpReturnScript() []byte {
// good tests which exercise that code, so it wouldn't make sense to use the
// same code to generate them.
func (g *testGenerator) calcFullSubsidy(blockHeight uint32) dcrutil.Amount {
iterations := int64(blockHeight) / g.params.ReductionInterval
iterations := int64(blockHeight) / g.params.SubsidyReductionInterval
subsidy := g.params.BaseSubsidy
for i := int64(0); i < iterations; i++ {
subsidy *= g.params.MulSubsidy
Expand Down
18 changes: 9 additions & 9 deletions blockchain/fullblocktests/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,24 @@ var simNetParams = &chaincfg.Params{
CurrentBlockVersion: 0,
PowLimit: simNetPowLimit,
PowLimitBits: 0x207fffff,
ResetMinDifficulty: false,
ReduceMinDifficulty: false,
GenerateSupported: true,
MaximumBlockSize: 1000000,
TimePerBlock: time.Second * 1,
TargetTimePerBlock: time.Second * 1,
WorkDiffAlpha: 1,
WorkDiffWindowSize: 8,
WorkDiffWindows: 4,
TargetTimespan: time.Second * 1 * 8, // TimePerBlock * WindowSize
RetargetAdjustmentFactor: 4,

// Subsidy parameters.
BaseSubsidy: 50000000000,
MulSubsidy: 100,
DivSubsidy: 101,
ReductionInterval: 128,
WorkRewardProportion: 6,
StakeRewardProportion: 3,
BlockTaxProportion: 1,
BaseSubsidy: 50000000000,
MulSubsidy: 100,
DivSubsidy: 101,
SubsidyReductionInterval: 128,
WorkRewardProportion: 6,
StakeRewardProportion: 3,
BlockTaxProportion: 1,

// Checkpoints ordered from oldest to newest.
Checkpoints: nil,
Expand Down
18 changes: 9 additions & 9 deletions blockchain/stake/tickets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,24 +951,24 @@ var simNetParams = &chaincfg.Params{
CurrentBlockVersion: 0,
PowLimit: simNetPowLimit,
PowLimitBits: 0x207fffff,
ResetMinDifficulty: false,
ReduceMinDifficulty: false,
GenerateSupported: true,
MaximumBlockSize: 1000000,
TimePerBlock: time.Second * 1,
TargetTimePerBlock: time.Second * 1,
WorkDiffAlpha: 1,
WorkDiffWindowSize: 8,
WorkDiffWindows: 4,
TargetTimespan: time.Second * 1 * 8, // TimePerBlock * WindowSize
RetargetAdjustmentFactor: 4,

// Subsidy parameters.
BaseSubsidy: 50000000000,
MulSubsidy: 100,
DivSubsidy: 101,
ReductionInterval: 128,
WorkRewardProportion: 6,
StakeRewardProportion: 3,
BlockTaxProportion: 1,
BaseSubsidy: 50000000000,
MulSubsidy: 100,
DivSubsidy: 101,
SubsidyReductionInterval: 128,
WorkRewardProportion: 6,
StakeRewardProportion: 3,
BlockTaxProportion: 1,

// Checkpoints ordered from oldest to newest.
Checkpoints: nil,
Expand Down
8 changes: 4 additions & 4 deletions blockchain/subsidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ func NewSubsidyCache(height int64, params *chaincfg.Params) *SubsidyCache {
params: params,
}

iteration := uint64(height / params.ReductionInterval)
iteration := uint64(height / params.SubsidyReductionInterval)
if iteration < subsidyCacheInitWidth {
return &sc
}

for i := iteration - 4; i <= iteration; i++ {
sc.CalcBlockSubsidy(int64(iteration) * params.ReductionInterval)
sc.CalcBlockSubsidy(int64(iteration) * params.SubsidyReductionInterval)
}

return &sc
Expand All @@ -59,7 +59,7 @@ func NewSubsidyCache(height int64, params *chaincfg.Params) *SubsidyCache {
// has the expected value.
//
// Subsidy calculation for exponential reductions:
// 0 for i in range (0, height / ReductionInterval):
// 0 for i in range (0, height / SubsidyReductionInterval):
// 1 subsidy *= MulSubsidy
// 2 subsidy /= DivSubsidy
//
Expand All @@ -71,7 +71,7 @@ func (s *SubsidyCache) CalcBlockSubsidy(height int64) int64 {
return s.params.BlockOneSubsidy()
}

iteration := uint64(height / s.params.ReductionInterval)
iteration := uint64(height / s.params.SubsidyReductionInterval)

if iteration == 0 {
return s.params.BaseSubsidy
Expand Down
8 changes: 4 additions & 4 deletions blockchain/subsidy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func TestBlockSubsidy(t *testing.T) {
continue
}

if i%mainnet.ReductionInterval == 0 {
numBlocks := mainnet.ReductionInterval
if i%mainnet.SubsidyReductionInterval == 0 {
numBlocks := mainnet.SubsidyReductionInterval
// First reduction internal, which is reduction interval - 2
// to skip the genesis block and block one.
if i == mainnet.ReductionInterval {
if i == mainnet.SubsidyReductionInterval {
numBlocks -= 2
}
height := i - numBlocks
Expand All @@ -45,7 +45,7 @@ func TestBlockSubsidy(t *testing.T) {

// First reduction internal, subtract the stake subsidy for
// blocks before the staking system is enabled.
if i == mainnet.ReductionInterval {
if i == mainnet.SubsidyReductionInterval {
totalSubsidy -= stake * (mainnet.StakeValidationHeight - 2)
}
}
Expand Down
3 changes: 3 additions & 0 deletions blockchain/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,9 @@ func (b *BlockChain) CheckBlockStakeSanity(stakeValidationHeight int64, node *bl
// amount, and verifying the signatures to prove the spender was the owner of
// the decred and therefore allowed to spend them. As it checks the inputs,
// it also calculates the total fees for the transaction and returns that value.
//
// NOTE: The transaction MUST have already been sanity checked with the
// CheckTransactionSanity function prior to calling this function.
func CheckTransactionInputs(subsidyCache *SubsidyCache, tx *dcrutil.Tx,
txHeight int64, utxoView *UtxoViewpoint, checkFraudProof bool,
chainParams *chaincfg.Params) (int64, error) {
Expand Down
18 changes: 9 additions & 9 deletions blockchain/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2050,24 +2050,24 @@ var simNetParams = &chaincfg.Params{
CurrentBlockVersion: 0,
PowLimit: simNetPowLimit,
PowLimitBits: 0x207fffff,
ResetMinDifficulty: false,
ReduceMinDifficulty: false,
GenerateSupported: true,
MaximumBlockSize: 1000000,
TimePerBlock: time.Second * 1,
TargetTimePerBlock: time.Second * 1,
WorkDiffAlpha: 1,
WorkDiffWindowSize: 8,
WorkDiffWindows: 4,
TargetTimespan: time.Second * 1 * 8, // TimePerBlock * WindowSize
RetargetAdjustmentFactor: 4,

// Subsidy parameters.
BaseSubsidy: 50000000000,
MulSubsidy: 100,
DivSubsidy: 101,
ReductionInterval: 128,
WorkRewardProportion: 6,
StakeRewardProportion: 3,
BlockTaxProportion: 1,
BaseSubsidy: 50000000000,
MulSubsidy: 100,
DivSubsidy: 101,
SubsidyReductionInterval: 128,
WorkRewardProportion: 6,
StakeRewardProportion: 3,
BlockTaxProportion: 1,

// Checkpoints ordered from oldest to newest.
Checkpoints: nil,
Expand Down
Loading

0 comments on commit 60c812e

Please sign in to comment.