AbstractBitcoinNetParams subclasses: Improve Genesis hash checking

1. Define Sha256 constants for expected hashes
2. Compare hashes in binary form
3. Move hash comparison to immediately follow Block creation
4. Add error message to the check state call
This commit is contained in:
Sean Gilligan 2021-09-07 14:34:51 -07:00 committed by Andreas Schildbach
parent 34184f0949
commit 42bb5b386e
3 changed files with 11 additions and 13 deletions

View File

@ -34,6 +34,7 @@ public class MainNetParams extends AbstractBitcoinNetParams {
public static final int MAINNET_MAJORITY_WINDOW = 1000;
public static final int MAINNET_MAJORITY_REJECT_BLOCK_OUTDATED = 950;
public static final int MAINNET_MAJORITY_ENFORCE_BLOCK_UPGRADE = 750;
public static final Sha256Hash GENESIS_HASH = Sha256Hash.wrap("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
public MainNetParams() {
super();
@ -45,7 +46,8 @@ public class MainNetParams extends AbstractBitcoinNetParams {
genesisBlock.setDifficultyTarget(0x1d00ffffL);
genesisBlock.setTime(1231006505L);
genesisBlock.setNonce(2083236893);
checkState(genesisBlock.getHash().equals(GENESIS_HASH), "Invalid genesis hash");
port = 8333;
packetMagic = 0xf9beb4d9L;
dumpedPrivateKeyHeader = 128;
@ -62,10 +64,6 @@ public class MainNetParams extends AbstractBitcoinNetParams {
majorityRejectBlockOutdated = MAINNET_MAJORITY_REJECT_BLOCK_OUTDATED;
majorityWindow = MAINNET_MAJORITY_WINDOW;
String genesisHash = genesisBlock.getHashAsString();
checkState(genesisHash.equals("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"),
genesisHash);
// This contains (at a minimum) the blocks which are not BIP30 compliant. BIP30 changed how duplicate
// transactions are handled. Duplicated transactions could occur in the case where a coinbase had the same
// extraNonce and the same outputs but appeared at different heights, and greatly complicated re-org handling.

View File

@ -18,6 +18,7 @@
package org.bitcoinj.params;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.Sha256Hash;
import java.math.BigInteger;
@ -28,6 +29,7 @@ import static com.google.common.base.Preconditions.checkState;
*/
public class RegTestParams extends AbstractBitcoinNetParams {
private static final BigInteger MAX_TARGET = new BigInteger("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
public static final Sha256Hash GENESIS_HASH = Sha256Hash.wrap("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206");
public RegTestParams() {
super();
@ -44,7 +46,8 @@ public class RegTestParams extends AbstractBitcoinNetParams {
genesisBlock.setDifficultyTarget(0x207fFFFFL);
genesisBlock.setTime(1296688602L);
genesisBlock.setNonce(2);
checkState(genesisBlock.getHash().equals(GENESIS_HASH), "Invalid genesis hash");
port = 18444;
packetMagic = 0xfabfb5daL;
dumpedPrivateKeyHeader = 239;
@ -61,9 +64,6 @@ public class RegTestParams extends AbstractBitcoinNetParams {
majorityRejectBlockOutdated = MainNetParams.MAINNET_MAJORITY_REJECT_BLOCK_OUTDATED;
majorityWindow = MainNetParams.MAINNET_MAJORITY_WINDOW;
String genesisHash = genesisBlock.getHashAsString();
checkState(genesisHash.equals("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
dnsSeeds = null;
addrSeeds = null;
}

View File

@ -24,6 +24,7 @@ import java.util.Date;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.StoredBlock;
import org.bitcoinj.core.Utils;
import org.bitcoinj.core.VerificationException;
@ -41,6 +42,7 @@ public class TestNet3Params extends AbstractBitcoinNetParams {
public static final int TESTNET_MAJORITY_WINDOW = 100;
public static final int TESTNET_MAJORITY_REJECT_BLOCK_OUTDATED = 75;
public static final int TESTNET_MAJORITY_ENFORCE_BLOCK_UPGRADE = 51;
public static final Sha256Hash GENESIS_HASH = Sha256Hash.wrap("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943");
public TestNet3Params() {
super();
@ -52,7 +54,8 @@ public class TestNet3Params extends AbstractBitcoinNetParams {
genesisBlock.setDifficultyTarget(0x1d00ffffL);
genesisBlock.setTime(1296688602L);
genesisBlock.setNonce(414098458);
checkState(genesisBlock.getHash().equals(GENESIS_HASH), "Invalid genesis hash");
port = 18333;
packetMagic = 0x0b110907;
dumpedPrivateKeyHeader = 239;
@ -69,9 +72,6 @@ public class TestNet3Params extends AbstractBitcoinNetParams {
majorityRejectBlockOutdated = TESTNET_MAJORITY_REJECT_BLOCK_OUTDATED;
majorityWindow = TESTNET_MAJORITY_WINDOW;
String genesisHash = genesisBlock.getHashAsString();
checkState(genesisHash.equals("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"));
dnsSeeds = new String[] {
"testnet-seed.bitcoin.jonasschnelli.ch", // Jonas Schnelli
"seed.tbtc.petertodd.org", // Peter Todd