/** * Checks the block data to ensure it follows the rules laid out in the network parameters. Specifically, * throws an exception if the proof of work is invalid, or if the timestamp is too far from what it should be. * This is <b>not</b> everything that is required for a block to be valid, only what is checkable independent * of the chain and without a transaction index. * * @throws VerificationException */ public void verifyHeader() throws VerificationException { // Prove that this block is OK. It might seem that we can just ignore most of these checks given that the // network is also verifying the blocks, but we cannot as it'd open us to a variety of obscure attacks. // // Firstly we need to ensure this block does in fact represent real work done. If the difficulty is high // enough, it's probably been done by the network. checkProofOfWork(true); checkTimestamp(); }
/** * Checks the block data to ensure it follows the rules laid out in the network parameters. Specifically, * throws an exception if the proof of work is invalid, or if the timestamp is too far from what it should be. * This is <b>not</b> everything that is required for a block to be valid, only what is checkable independent * of the chain and without a transaction index. * * @throws VerificationException */ public void verifyHeader() throws VerificationException { // Prove that this block is OK. It might seem that we can just ignore most of these checks given that the // network is also verifying the blocks, but we cannot as it'd open us to a variety of obscure attacks. // // Firstly we need to ensure this block does in fact represent real work done. If the difficulty is high // enough, it's probably been done by the network. checkProofOfWork(true); checkTimestamp(); }
/** * Checks the block data to ensure it follows the rules laid out in the network parameters. Specifically, * throws an exception if the proof of work is invalid, or if the timestamp is too far from what it should be. * This is <b>not</b> everything that is required for a block to be valid, only what is checkable independent * of the chain and without a transaction index. * * @throws VerificationException */ public void verifyHeader() throws VerificationException { // Prove that this block is OK. It might seem that we can just ignore most of these checks given that the // network is also verifying the blocks, but we cannot as it'd open us to a variety of obscure attacks. // // Firstly we need to ensure this block does in fact represent real work done. If the difficulty is high // enough, it's probably been done by the network. checkProofOfWork(true); checkTimestamp(); }
/** * Checks the block data to ensure it follows the rules laid out in the network parameters. Specifically, * throws an exception if the proof of work is invalid, or if the timestamp is too far from what it should be. * This is <b>not</b> everything that is required for a block to be valid, only what is checkable independent * of the chain and without a transaction index. * * @throws VerificationException */ public void verifyHeader() throws VerificationException { // Prove that this block is OK. It might seem that we can just ignore most of these checks given that the // network is also verifying the blocks, but we cannot as it'd open us to a variety of obscure attacks. // // Firstly we need to ensure this block does in fact represent real work done. If the difficulty is high // enough, it's probably been done by the network. checkProofOfWork(true); checkTimestamp(); }
/** * <p>Finds a value of nonce that makes the blocks hash lower than the difficulty target. This is called mining, but * solve() is far too slow to do real mining with. It exists only for unit testing purposes. * * <p>This can loop forever if a solution cannot be found solely by incrementing nonce. It doesn't change * extraNonce.</p> */ public void solve() { while (true) { try { // Is our proof of work valid yet? if (checkProofOfWork(false)) return; // No, so increment the nonce and try again. setNonce(getNonce() + 1); } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } } }
/** * <p>Finds a value of nonce that makes the blocks hash lower than the difficulty target. This is called mining, but * solve() is far too slow to do real mining with. It exists only for unit testing purposes. * * <p>This can loop forever if a solution cannot be found solely by incrementing nonce. It doesn't change * extraNonce.</p> */ public void solve() { while (true) { try { // Is our proof of work valid yet? if (checkProofOfWork(false)) return; // No, so increment the nonce and try again. setNonce(getNonce() + 1); } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } } }
/** * <p>Finds a value of nonce that makes the blocks hash lower than the difficulty target. This is called mining, but * solve() is far too slow to do real mining with. It exists only for unit testing purposes. * * <p>This can loop forever if a solution cannot be found solely by incrementing nonce. It doesn't change * extraNonce.</p> */ public void solve() { while (true) { try { // Is our proof of work valid yet? if (checkProofOfWork(false)) return; // No, so increment the nonce and try again. setNonce(getNonce() + 1); } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } } }
/** * <p>Finds a value of nonce that makes the blocks hash lower than the difficulty target. This is called mining, but * solve() is far too slow to do real mining with. It exists only for unit testing purposes. * * <p>This can loop forever if a solution cannot be found solely by incrementing nonce. It doesn't change * extraNonce.</p> */ public void solve() { while (true) { try { // Is our proof of work valid yet? if (checkProofOfWork(false)) return; // No, so increment the nonce and try again. setNonce(getNonce() + 1); } catch (VerificationException e) { throw new RuntimeException(e); // Cannot happen. } } }
/** Returns true if the hash of the block is OK (lower than difficulty target). */ protected boolean checkProofOfWork(boolean throwException) throws VerificationException { if (params instanceof AltcoinNetworkParameters) { BigInteger target = getDifficultyTargetAsInteger(); if (params instanceof AuxPoWNetworkParameters) { final AuxPoWNetworkParameters auxParams = (AuxPoWNetworkParameters)this.params; if (auxParams.isAuxPoWBlockVersion(getRawVersion()) && null != auxpow) { return auxpow.checkProofOfWork(this.getHash(), target, throwException); } } final AltcoinNetworkParameters altParams = (AltcoinNetworkParameters)this.params; BigInteger h = altParams.getBlockDifficultyHash(this).toBigInteger(); if (h.compareTo(target) > 0) { // Proof of work check failed! if (throwException) throw new VerificationException("Hash is higher than target: " + getHashAsString() + " vs " + target.toString(16)); else return false; } return true; } else { return super.checkProofOfWork(throwException); } }