本文整理汇总了Java中org.ethereum.util.ByteUtil.increment方法的典型用法代码示例。如果您正苦于以下问题:Java ByteUtil.increment方法的具体用法?Java ByteUtil.increment怎么用?Java ByteUtil.increment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.ethereum.util.ByteUtil
的用法示例。
在下文中一共展示了ByteUtil.increment方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mine
import org.ethereum.util.ByteUtil; //导入方法依赖的package包/类
/**
* Adds a nonce to given block which complies with the given difficulty
*
* For the PoC series, we use a simplified proof-of-work.
* This is not ASIC resistant and is meant merely as a placeholder.
* It utilizes the bare SHA3 hash function to secure the block chain by requiring
* the SHA3 hash of the concatenation of the nonce and the header’s SHA3 hash to be
* sufficiently low. It is formally defined as PoW:
*
* PoW(H, n) ≡ BE(SHA3(SHA3(RLP(H!n)) ◦ n))
*
* where:
* RLP(H!n) is the RLP encoding of the block header H, not including the
* final nonce component;
* SHA3 is the SHA3 hash function accepting an arbitrary length series of
* bytes and evaluating to a series of 32 bytes (i.e. 256-bit);
* n is the nonce, a series of 32 bytes;
* o is the series concatenation operator;
* BE(X) evaluates to the value equal to X when interpreted as a
* big-endian-encoded integer.
*
* @param newBlock without a valid nonce
* @param difficulty - the mining difficulty
* @return true if valid nonce has been added to the block
*/
public boolean mine(Block newBlock, byte[] difficulty) {
// eval(_root, _nonce) <= (bigint(1) << 256) / _difficulty; }
stop = false;
BigInteger max = BigInteger.valueOf(2).pow(255);
byte[] target = BigIntegers.asUnsignedByteArray(32,
max.divide(new BigInteger(1, difficulty)));
long newGasLimit = Math.max(125000,
(new BigInteger(1, newBlock.getGasLimit()).longValue() * (1024 - 1) + (newBlock.getGasUsed() * 6 / 5)) / 1024);
newBlock.getHeader().setGasLimit(BigInteger.valueOf(newGasLimit).toByteArray());
byte[] hash = sha3(newBlock.getEncodedWithoutNonce());
byte[] testNonce = new byte[32];
byte[] concat;
while (ByteUtil.increment(testNonce) && !stop) {
if (testNonce[31] == 0 && testNonce[30] == 0) {
System.out.println("mining: " + new BigInteger(1, testNonce));
}
if (testNonce[31] == 0)
sleep();
concat = Arrays.concatenate(hash, testNonce);
byte[] result = sha3(concat);
if (FastByteComparisons.compareTo(result, 0, 32, target, 0, 32) < 0) {
newBlock.setNonce(testNonce);
return true;
}
}
return false; // couldn't find a valid nonce
}