diff --git a/chain.go b/chain.go index 691e2c6f..f67b4330 100644 --- a/chain.go +++ b/chain.go @@ -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 diff --git a/difficulty.go b/difficulty.go index 4497da13..0ed4fdda 100644 --- a/difficulty.go +++ b/difficulty.go @@ -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 { diff --git a/params.go b/params.go index 5307e590..c879db2a 100644 --- a/params.go +++ b/params.go @@ -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 ®ressionParams diff --git a/validate.go b/validate.go index 63b32735..c30e3ab6 100644 --- a/validate.go +++ b/validate.go @@ -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 }