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:
Dave Collins 2013-07-30 12:30:31 -05:00
parent 0e7791058a
commit cef901ebad
4 changed files with 45 additions and 31 deletions

View file

@ -339,7 +339,7 @@ func (b *BlockChain) getPrevNodeFromNode(node *blockNode) (*blockNode, error) {
}
// Genesis block.
if node.hash.IsEqual(b.netParams().genesisHash) {
if node.hash.IsEqual(b.chainParams().GenesisHash) {
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) {
// Genesis block.
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

View file

@ -188,7 +188,7 @@ func (b *BlockChain) calcEasiestDifficulty(bits uint32, duration time.Duration)
adjustmentFactor := big.NewInt(retargetAdjustmentFactor)
// 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
// 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) {
// Search backwards through the chain for the last block without
// the special rule applied.
powLimitBits := BigToCompact(b.netParams().powLimit)
powLimitBits := BigToCompact(b.chainParams().PowLimit)
iterNode := startNode
for iterNode != nil && iterNode.height%blocksPerRetarget != 0 && iterNode.bits == powLimitBits {
// 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.
func (b *BlockChain) calcNextRequiredDifficulty(lastNode *blockNode, block *btcutil.Block) (uint32, error) {
// Choose the correct proof of work limit for the active network.
powLimit := b.netParams().powLimit
powLimit := b.chainParams().PowLimit
// Genesis block.
if lastNode == nil {

View file

@ -9,50 +9,64 @@ import (
"math/big"
)
// params is used to group parameters for various networks such as the main
// network and test networks.
type params struct {
genesisBlock *btcwire.MsgBlock
genesisHash *btcwire.ShaHash
powLimit *big.Int
// Params houses parameters unique to various bitcoin networks such as the main
// network and test networks. See ChainParams.
type Params struct {
// GenesisBlock is the genesis block for the specific network.
GenesisBlock *btcwire.MsgBlock
// 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
// (btcwire.MainNet).
var mainNetParams = params{
genesisBlock: &btcwire.GenesisBlock,
genesisHash: &btcwire.GenesisHash,
var mainNetParams = Params{
GenesisBlock: &btcwire.GenesisBlock,
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.
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
// (btcwire.TestNet).
var regressionParams = params{
genesisBlock: &btcwire.TestNetGenesisBlock,
genesisHash: &btcwire.TestNetGenesisHash,
var regressionParams = Params{
GenesisBlock: &btcwire.TestNetGenesisBlock,
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.
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)
// (btcwire.TestNet3).
var testNet3Params = params{
genesisBlock: &btcwire.TestNet3GenesisBlock,
genesisHash: &btcwire.TestNet3GenesisHash,
var testNet3Params = Params{
GenesisBlock: &btcwire.TestNet3GenesisBlock,
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).
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.
func (b *BlockChain) netParams() *params {
switch b.btcnet {
// chainParams returns chain parameters specific to the bitcoin network
// associated with the BlockChain instance.
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:
return &regressionParams

View file

@ -278,7 +278,7 @@ func (b *BlockChain) checkProofOfWork(block *btcutil.Block) error {
}
// The target difficulty must be less than the maximum allowed.
powLimit := b.netParams().powLimit
powLimit := b.chainParams().PowLimit
if target.Cmp(powLimit) > 0 {
str := fmt.Sprintf("block target difficulty of %064x is "+
"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
// now.
if node.hash.IsEqual(b.netParams().genesisHash) {
if node.hash.IsEqual(b.chainParams().GenesisHash) {
return nil
}