3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-30 23:02:15 +00:00

Add AuxPoW chain ID checks.

This commit is contained in:
Ross Nicoll 2015-06-01 22:54:54 +01:00
parent c6659bad12
commit 573d6f6edf
5 changed files with 38 additions and 9 deletions

View File

@ -93,4 +93,9 @@ public class DogecoinMainNetParams extends AbstractDogecoinParams {
// TODO: CHANGE THIS
return ID_DOGE_MAINNET;
}
@Override
public boolean isTestNet() {
return false;
}
}

View File

@ -71,4 +71,9 @@ public class DogecoinTestNet3Params extends AbstractDogecoinParams {
// TODO: CHANGE ME
return PAYMENT_PROTOCOL_ID_TESTNET;
}
@Override
public boolean isTestNet() {
return true;
}
}

View File

@ -44,7 +44,9 @@ import static org.bitcoinj.core.Utils.reverseBytes;
*/
public class AltcoinBlock extends org.bitcoinj.core.Block {
/** Bit used to indicate that a block contains an AuxPoW section, where the network supports AuxPoW */
public static final int BLOCK_FLAG_AUXPOW = (1 << 8);
public static final int BLOCK_VERSION_CHAIN_START = (1 << 16);
public static final int BLOCK_VERSION_CHAIN_END = (1 << 30);
public static final int BLOCK_VERSION_AUXPOW = (1 << 8);
private boolean auxpowParsed = false;
private boolean auxpowBytesValid = false;

View File

@ -31,4 +31,6 @@ public interface AltcoinNetworkParameters {
* SHA256 twice (Bitcoin standard) for proof of work.
*/
Sha256Hash getBlockDifficultyHash(Block block);
public boolean isTestNet();
}

View File

@ -190,9 +190,9 @@ public class AuxPoW extends ChildMessage implements Serializable {
AuxPoW input = (AuxPoW) o;
if (!transaction.equals(input.transaction)) return false;
if (!hashBlock.equals(input.hashBlock)) return false;
if (!coinbaseBranch.equals(input.hashBlock)) return false;
if (!chainMerkleBranch.equals(input.hashBlock)) return false;
if (!parentBlockHeader.equals(input.hashBlock)) return false;
if (!coinbaseBranch.equals(input.coinbaseBranch)) return false;
if (!chainMerkleBranch.equals(input.chainMerkleBranch)) return false;
if (!parentBlockHeader.equals(input.parentBlockHeader)) return false;
return getHash().equals(input.getHash());
}
@ -280,6 +280,15 @@ public class AuxPoW extends ChildMessage implements Serializable {
*/
protected boolean checkProofOfWork(Sha256Hash hashAuxBlock,
BigInteger target, boolean throwException) throws VerificationException {
if (!(params instanceof AltcoinNetworkParameters)) {
if (throwException) {
// Should be impossible
throw new VerificationException("Network parameters are not an instance of AltcoinNetworkParameters, AuxPoW support is not available.");
}
return false;
}
final AltcoinNetworkParameters altcoinParams = (AltcoinNetworkParameters) params;
if (0 != this.getCoinbaseBranch().getIndex()) {
if (throwException) {
// I don't like the message, but it correlates with what's in the reference client.
@ -288,13 +297,13 @@ public class AuxPoW extends ChildMessage implements Serializable {
return false;
}
/* if (!TestNet()
parentBlockHeader.getChainID() == ((AuxPoWNetworkParameters) params).getChainID()) {
if (!altcoinParams.isTestNet()
&& getChainID(parentBlockHeader) == altcoinParams.getChainID()) {
if (throwException) {
throw new VerificationException("Aux POW parent has our chain ID");
}
return false;
} */
}
if (this.getChainMerkleBranch().size() > 30) {
if (throwException) {
@ -401,8 +410,7 @@ public class AuxPoW extends ChildMessage implements Serializable {
return false;
}
BigInteger h = ((AltcoinNetworkParameters) params)
.getBlockDifficultyHash(getParentBlockHeader())
BigInteger h = altcoinParams.getBlockDifficultyHash(getParentBlockHeader())
.toBigInteger();
if (h.compareTo(target) > 0) {
// Proof of work check failed!
@ -436,4 +444,11 @@ public class AuxPoW extends ChildMessage implements Serializable {
}
return true;
}
/**
* Get the chain ID from a block header.
*/
protected long getChainID(final Block blockHeader) {
return blockHeader.getVersion() / AltcoinBlock.BLOCK_VERSION_CHAIN_START;
}
}