mirror of
https://github.com/LBRYFoundation/lbcd.git
synced 2025-08-23 17:47:24 +00:00
Export chain parameters.
This commit exports the chain parameters used depending on the specific bitcoin network so callers can have access to this information if desired.
This commit is contained in:
parent
0e7791058a
commit
cef901ebad
4 changed files with 45 additions and 31 deletions
4
chain.go
4
chain.go
|
@ -339,7 +339,7 @@ func (b *BlockChain) getPrevNodeFromNode(node *blockNode) (*blockNode, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Genesis block.
|
// Genesis block.
|
||||||
if node.hash.IsEqual(b.netParams().genesisHash) {
|
if node.hash.IsEqual(b.chainParams().GenesisHash) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -393,7 +393,7 @@ func (b *BlockChain) isMajorityVersion(minVer uint32, startNode *blockNode, numR
|
||||||
func (b *BlockChain) calcPastMedianTime(startNode *blockNode) (time.Time, error) {
|
func (b *BlockChain) calcPastMedianTime(startNode *blockNode) (time.Time, error) {
|
||||||
// Genesis block.
|
// Genesis block.
|
||||||
if startNode == nil {
|
if startNode == nil {
|
||||||
return b.netParams().genesisBlock.Header.Timestamp, nil
|
return b.chainParams().GenesisBlock.Header.Timestamp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a slice of the previous few block timestamps used to calculate
|
// Create a slice of the previous few block timestamps used to calculate
|
||||||
|
|
|
@ -188,7 +188,7 @@ func (b *BlockChain) calcEasiestDifficulty(bits uint32, duration time.Duration)
|
||||||
adjustmentFactor := big.NewInt(retargetAdjustmentFactor)
|
adjustmentFactor := big.NewInt(retargetAdjustmentFactor)
|
||||||
|
|
||||||
// Choose the correct proof of work limit for the active network.
|
// Choose the correct proof of work limit for the active network.
|
||||||
powLimit := b.netParams().powLimit
|
powLimit := b.chainParams().PowLimit
|
||||||
|
|
||||||
// The test network rules allow minimum difficulty blocks after more
|
// The test network rules allow minimum difficulty blocks after more
|
||||||
// than twice the desired amount of time needed to generate a block has
|
// than twice the desired amount of time needed to generate a block has
|
||||||
|
@ -225,7 +225,7 @@ func (b *BlockChain) calcEasiestDifficulty(bits uint32, duration time.Duration)
|
||||||
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
|
// Search backwards through the chain for the last block without
|
||||||
// the special rule applied.
|
// the special rule applied.
|
||||||
powLimitBits := BigToCompact(b.netParams().powLimit)
|
powLimitBits := BigToCompact(b.chainParams().PowLimit)
|
||||||
iterNode := startNode
|
iterNode := startNode
|
||||||
for iterNode != nil && iterNode.height%blocksPerRetarget != 0 && iterNode.bits == powLimitBits {
|
for iterNode != nil && iterNode.height%blocksPerRetarget != 0 && iterNode.bits == powLimitBits {
|
||||||
// Get the previous block node. This function is used over
|
// Get the previous block node. This function is used over
|
||||||
|
@ -254,7 +254,7 @@ func (b *BlockChain) findPrevTestNetDifficulty(startNode *blockNode) (uint32, er
|
||||||
// after the passed previous block node based on the difficulty retarget rules.
|
// after the passed previous block node based on the difficulty retarget rules.
|
||||||
func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, block *btcutil.Block) (uint32, error) {
|
func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, block *btcutil.Block) (uint32, error) {
|
||||||
// Choose the correct proof of work limit for the active network.
|
// Choose the correct proof of work limit for the active network.
|
||||||
powLimit := b.netParams().powLimit
|
powLimit := b.chainParams().PowLimit
|
||||||
|
|
||||||
// Genesis block.
|
// Genesis block.
|
||||||
if lastNode == nil {
|
if lastNode == nil {
|
||||||
|
|
62
params.go
62
params.go
|
@ -9,50 +9,64 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
)
|
)
|
||||||
|
|
||||||
// params is used to group parameters for various networks such as the main
|
// Params houses parameters unique to various bitcoin networks such as the main
|
||||||
// network and test networks.
|
// network and test networks. See ChainParams.
|
||||||
type params struct {
|
type Params struct {
|
||||||
genesisBlock *btcwire.MsgBlock
|
// GenesisBlock is the genesis block for the specific network.
|
||||||
genesisHash *btcwire.ShaHash
|
GenesisBlock *btcwire.MsgBlock
|
||||||
powLimit *big.Int
|
|
||||||
|
// GenesisHash is the genesis block hash for the specific network.
|
||||||
|
GenesisHash *btcwire.ShaHash
|
||||||
|
|
||||||
|
// PowLimit is the highest proof of work value a bitcoin block can have
|
||||||
|
// for the specific network.
|
||||||
|
PowLimit *big.Int
|
||||||
}
|
}
|
||||||
|
|
||||||
// mainNetParams contains parameters specific to the main network
|
// mainNetParams contains parameters specific to the main network
|
||||||
// (btcwire.MainNet).
|
// (btcwire.MainNet).
|
||||||
var mainNetParams = params{
|
var mainNetParams = Params{
|
||||||
genesisBlock: &btcwire.GenesisBlock,
|
GenesisBlock: &btcwire.GenesisBlock,
|
||||||
genesisHash: &btcwire.GenesisHash,
|
GenesisHash: &btcwire.GenesisHash,
|
||||||
|
|
||||||
// powLimit is the highest proof of work value a bitcoin block can have.
|
// PowLimit is the highest proof of work value a bitcoin block can have.
|
||||||
// It is the value 2^224 - 1 for the main network.
|
// It is the value 2^224 - 1 for the main network.
|
||||||
powLimit: new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne),
|
PowLimit: new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne),
|
||||||
}
|
}
|
||||||
|
|
||||||
// regressionParams contains parameters specific to the regression test network
|
// regressionParams contains parameters specific to the regression test network
|
||||||
// (btcwire.TestNet).
|
// (btcwire.TestNet).
|
||||||
var regressionParams = params{
|
var regressionParams = Params{
|
||||||
genesisBlock: &btcwire.TestNetGenesisBlock,
|
GenesisBlock: &btcwire.TestNetGenesisBlock,
|
||||||
genesisHash: &btcwire.TestNetGenesisHash,
|
GenesisHash: &btcwire.TestNetGenesisHash,
|
||||||
|
|
||||||
// powLimit is the highest proof of work value a bitcoin block can have.
|
// PowLimit is the highest proof of work value a bitcoin block can have.
|
||||||
// It is the value 2^256 - 1 for the regression test network.
|
// It is the value 2^256 - 1 for the regression test network.
|
||||||
powLimit: new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne),
|
PowLimit: new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne),
|
||||||
}
|
}
|
||||||
|
|
||||||
// testNet3Params contains parameters specific to the test network (version 3)
|
// testNet3Params contains parameters specific to the test network (version 3)
|
||||||
// (btcwire.TestNet3).
|
// (btcwire.TestNet3).
|
||||||
var testNet3Params = params{
|
var testNet3Params = Params{
|
||||||
genesisBlock: &btcwire.TestNet3GenesisBlock,
|
GenesisBlock: &btcwire.TestNet3GenesisBlock,
|
||||||
genesisHash: &btcwire.TestNet3GenesisHash,
|
GenesisHash: &btcwire.TestNet3GenesisHash,
|
||||||
|
|
||||||
// powLimit is the highest proof of work value a bitcoin block can have.
|
// PowLimit is the highest proof of work value a bitcoin block can have.
|
||||||
// It is the value 2^224 - 1 for the test network (version 3).
|
// It is the value 2^224 - 1 for the test network (version 3).
|
||||||
powLimit: new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne),
|
PowLimit: new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne),
|
||||||
}
|
}
|
||||||
|
|
||||||
// netParams returns parameters specific to the passed bitcoin network.
|
// chainParams returns chain parameters specific to the bitcoin network
|
||||||
func (b *BlockChain) netParams() *params {
|
// associated with the BlockChain instance.
|
||||||
switch b.btcnet {
|
func (b *BlockChain) chainParams() *Params {
|
||||||
|
return ChainParams(b.btcnet)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChainParams returns chain parameters specific to the passed bitcoin network.
|
||||||
|
// It returns the parameters for btcwire.MainNet if the passed network is not
|
||||||
|
// supported.
|
||||||
|
func ChainParams(btcnet btcwire.BitcoinNet) *Params {
|
||||||
|
switch btcnet {
|
||||||
case btcwire.TestNet:
|
case btcwire.TestNet:
|
||||||
return ®ressionParams
|
return ®ressionParams
|
||||||
|
|
||||||
|
|
|
@ -278,7 +278,7 @@ func (b *BlockChain) checkProofOfWork(block *btcutil.Block) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// The target difficulty must be less than the maximum allowed.
|
// The target difficulty must be less than the maximum allowed.
|
||||||
powLimit := b.netParams().powLimit
|
powLimit := b.chainParams().PowLimit
|
||||||
if target.Cmp(powLimit) > 0 {
|
if target.Cmp(powLimit) > 0 {
|
||||||
str := fmt.Sprintf("block target difficulty of %064x is "+
|
str := fmt.Sprintf("block target difficulty of %064x is "+
|
||||||
"higher than max of %064x", target, powLimit)
|
"higher than max of %064x", target, powLimit)
|
||||||
|
@ -712,7 +712,7 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block) er
|
||||||
|
|
||||||
// The coinbase for the Genesis block is not spendable, so just return
|
// The coinbase for the Genesis block is not spendable, so just return
|
||||||
// now.
|
// now.
|
||||||
if node.hash.IsEqual(b.netParams().genesisHash) {
|
if node.hash.IsEqual(b.chainParams().GenesisHash) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue