Skip to content
This repository has been archived by the owner on May 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request bitcoinj#2 from Vutov/master
Browse files Browse the repository at this point in the history
Equihash validation
  • Loading branch information
h4x3rotab committed Oct 22, 2018
2 parents ee7ba25 + 8369947 commit 56ee70e
Show file tree
Hide file tree
Showing 12 changed files with 651 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
testImplementation 'org.slf4j:slf4j-jdk14:1.7.25'
testImplementation 'com.h2database:h2:1.3.167'
testImplementation 'org.fusesource.leveldbjni:leveldbjni-all:1.8'
compile 'com.rfksystems:blake2b:1.0.0'
}

sourceCompatibility = 1.7
Expand Down
41 changes: 37 additions & 4 deletions core/src/main/java/org/bitcoinj/core/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.*;
import com.google.common.base.*;
import com.google.common.collect.*;
import org.bitcoinj.params.EquihashDTO;
import org.bitcoinj.script.*;
import org.slf4j.*;

Expand Down Expand Up @@ -541,7 +542,7 @@ public void solve() {
while (true) {
try {
// Is our proof of work valid yet?
if (checkProofOfWork(false))
if (checkProofOfWork(false, false))
return;
// No, so increment the nonce and try again.
setNonce(getNonce() + 1);
Expand All @@ -564,7 +565,7 @@ public BigInteger getDifficultyTargetAsInteger() throws VerificationException {
}

/** Returns true if the hash of the block is OK (lower than difficulty target). */
protected boolean checkProofOfWork(boolean throwException) throws VerificationException {
protected boolean checkProofOfWork(boolean throwException, boolean validateSolution) throws VerificationException {
// This part is key - it is what proves the block was as difficult to make as it claims
// to be. Note however that in the context of this function, the block can claim to be
// as difficult as it wants to be .... if somebody was able to take control of our network
Expand All @@ -584,7 +585,21 @@ protected boolean checkProofOfWork(boolean throwException) throws VerificationEx
else
return false;
}
return true;

if (validateSolution && this.height >= params.getForkHeight()) {
EquihashDTO equihashParams = params.getEquihash();
if (this.height <= params.getEquihashForkHeight()) {
equihashParams = params.getEquihashBeforeFork();
}

Equihash equihash = new Equihash(bitcoinSerialize(), solution, equihashParams);
EquihashResult res = equihash.verify();
if (throwException)
throw new VerificationException("Solution validation failed: " + res.getMessage());
return res.isValid();
} else {
return true;
}
}

private void checkTimestamp() throws VerificationException {
Expand Down Expand Up @@ -709,7 +724,25 @@ public void verifyHeader() throws VerificationException {
//
// 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);
checkProofOfWork(true, false);
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(boolean validateSolution) 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, validateSolution);
checkTimestamp();
}

Expand Down
Loading

0 comments on commit 56ee70e

Please sign in to comment.