Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up PoW verification hashing #160

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions app/src/main/java/com/samourai/wallet/JSONRPC/PoW.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ public String calcHash(JSONObject resultObj) {
String hash = null;

try {
strVersionHex = Hex.toHexString(reverse(Hex.decode(resultObj.getString("versionHex"))));
strVersionHex = reverseHexBytes(resultObj.getString("versionHex"));
Log.i("PoW", "version:" + strVersionHex);
strPrevBlock = Hex.toHexString(reverse(Hex.decode(resultObj.getString("previousblockhash"))));
strPrevBlock = reverseHexBytes(resultObj.getString("previousblockhash"));
Log.i("PoW", "prev block:" + strPrevBlock);
strMerkleRoot = Hex.toHexString(reverse(Hex.decode(resultObj.getString("merkleroot"))));
strMerkleRoot = reverseHexBytes(resultObj.getString("merkleroot"));
Log.i("PoW", "merkle root:" + strMerkleRoot);

ts = resultObj.getLong("time");
strTS = Hex.toHexString(reverse(Hex.decode(String.format("%08X", ts))));
strTS = reverseHexBytes(String.format("%08X", ts));
Log.i("PoW", "timestamp:" + strTS);

strBits = Hex.toHexString(reverse(Hex.decode(resultObj.getString("bits"))));
strBits = reverseHexBytes(resultObj.getString("bits"));
Log.i("PoW", "bits:" + strBits);

nonce = resultObj.getLong("nonce");
strNonce = Hex.toHexString(reverse(Hex.decode(String.format("%08X", nonce))));
strNonce = reverseHexBytes(String.format("%08X", nonce));
Log.i("PoW", "nonce:" + strNonce);

String strHeader = strVersionHex + strPrevBlock + strMerkleRoot + strTS + strBits + strNonce;
Expand Down Expand Up @@ -142,4 +142,11 @@ else if(new Sha256Hash(hash).toBigInteger().compareTo(target) > 0) {

}

private static String reverseHexBytes(final String hex) {
final StringBuilder sb = new StringBuilder(hex.length());
for (int i = hex.length(); i > 0; i -= 2) {
sb.append(hex.charAt(i - 2)).append(hex.charAt(i - 1));
}
return sb.toString();
}
}