Allow checkpoints to work with all networks.

Previously the main network checkpoints were being used for unrecognized
networks.  This commit changes the code so that no checkpoints are used in
that scenario.
This commit is contained in:
Dave Collins 2013-07-30 15:29:15 -05:00
parent 1e891b4e0b
commit 357160257c

View file

@ -60,8 +60,9 @@ var checkpointDataMainNet = checkpointData{
checkpointsByHeight: nil, // Automatically generated in init. checkpointsByHeight: nil, // Automatically generated in init.
} }
// checkpointDataTestNet contains checkpoint data for the test network. // checkpointDataTestNet3 contains checkpoint data for the test network (version
var checkpointDataTestNet = checkpointData{ // 3).
var checkpointDataTestNet3 = checkpointData{
checkpoints: []Checkpoint{ checkpoints: []Checkpoint{
{546, newShaHashFromStr("000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70")}, {546, newShaHashFromStr("000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70")},
}, },
@ -89,18 +90,18 @@ func (b *BlockChain) DisableCheckpoints(disable bool) {
func (b *BlockChain) checkpointData() *checkpointData { func (b *BlockChain) checkpointData() *checkpointData {
switch b.btcnet { switch b.btcnet {
case btcwire.TestNet3: case btcwire.TestNet3:
return &checkpointDataTestNet return &checkpointDataTestNet3
case btcwire.MainNet: case btcwire.MainNet:
fallthrough
default:
return &checkpointDataMainNet return &checkpointDataMainNet
} }
return nil
} }
// LatestCheckpoint returns the most recent checkpoint (regardless of whether it // LatestCheckpoint returns the most recent checkpoint (regardless of whether it
// is already known). When checkpoints are disabled it will return nil. // is already known). When checkpoints are disabled or there are no checkpoints
// for the active network, it will return nil.
func (b *BlockChain) LatestCheckpoint() *Checkpoint { func (b *BlockChain) LatestCheckpoint() *Checkpoint {
if b.noCheckpoints { if b.noCheckpoints || b.checkpointData() == nil {
return nil return nil
} }
@ -112,7 +113,7 @@ func (b *BlockChain) LatestCheckpoint() *Checkpoint {
// match the hard-coded checkpoint data. It also returns true if there is no // match the hard-coded checkpoint data. It also returns true if there is no
// checkpoint data for the passed block height. // checkpoint data for the passed block height.
func (b *BlockChain) verifyCheckpoint(height int64, hash *btcwire.ShaHash) bool { func (b *BlockChain) verifyCheckpoint(height int64, hash *btcwire.ShaHash) bool {
if b.noCheckpoints { if b.noCheckpoints || b.checkpointData() == nil {
return true return true
} }
@ -130,7 +131,7 @@ func (b *BlockChain) verifyCheckpoint(height int64, hash *btcwire.ShaHash) bool
// associated block. It returns nil if a checkpoint can't be found (this should // associated block. It returns nil if a checkpoint can't be found (this should
// really only happen for blocks before the first checkpoint). // really only happen for blocks before the first checkpoint).
func (b *BlockChain) findLatestKnownCheckpoint() (*btcutil.Block, error) { func (b *BlockChain) findLatestKnownCheckpoint() (*btcutil.Block, error) {
if b.noCheckpoints { if b.noCheckpoints || b.checkpointData() == nil {
return nil, nil return nil, nil
} }
@ -248,7 +249,7 @@ func init() {
// when the package loads. // when the package loads.
checkpointInitializeList := []*checkpointData{ checkpointInitializeList := []*checkpointData{
&checkpointDataMainNet, &checkpointDataMainNet,
&checkpointDataTestNet, &checkpointDataTestNet3,
} }
for _, data := range checkpointInitializeList { for _, data := range checkpointInitializeList {
data.checkpointsByHeight = make(map[int64]*Checkpoint) data.checkpointsByHeight = make(map[int64]*Checkpoint)