Java Code Examples for org.ethereum.util.FastByteComparisons#compareTo()
The following examples show how to use
org.ethereum.util.FastByteComparisons#compareTo() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HashMapDBSimple.java From nuls with MIT License | 5 votes |
@Override public V prefixLookup(byte[] key, int prefixBytes) { for (Map.Entry<byte[], V> e : storage.entrySet()) { if (FastByteComparisons.compareTo(key, 0, prefixBytes, e.getKey(), 0, prefixBytes) == 0) { return e.getValue(); } } return null; }
Example 2
Source File: ByteArrayWrapperTest.java From ethereumj with MIT License | 5 votes |
@Test public void testEqualsPerformance() { boolean testEnabled = false; if(testEnabled) { final int ITERATIONS = 10000000; long start1 = System.currentTimeMillis(); for (int i = 0; i < ITERATIONS; i++) { Comparator<byte[]> comparator = UnsignedBytes .lexicographicalComparator(); comparator.compare(wrapper1.getData(), wrapper2.getData()); } System.out.println(System.currentTimeMillis() - start1 + "ms"); long start2 = System.currentTimeMillis(); for (int i = 0; i < ITERATIONS; i++) { Arrays.equals(wrapper1.getData(), wrapper2.getData()); } System.out.println(System.currentTimeMillis() - start2 + "ms"); long start3 = System.currentTimeMillis(); for (int i = 0; i < ITERATIONS; i++) { FastByteComparisons.compareTo(wrapper1.getData(), 0, wrapper1.getData().length, wrapper2.getData(), 0, wrapper1.getData().length); } System.out.println(System.currentTimeMillis() - start3 + "ms"); } }
Example 3
Source File: BlockHeader.java From ethereumj with MIT License | 5 votes |
/** * Verify that block is valid for its difficulty * * @return boolean */ public boolean validateNonce() { BigInteger max = BigInteger.valueOf(2).pow(256); byte[] target = BigIntegers.asUnsignedByteArray(32, max.divide(new BigInteger(1, this.getDifficulty()))); byte[] hash = HashUtil.sha3(this.getEncodedWithoutNonce()); byte[] concat = Arrays.concatenate(hash, this.getNonce()); byte[] result = HashUtil.sha3(concat); return FastByteComparisons.compareTo(result, 0, 32, target, 0, 32) < 0; }
Example 4
Source File: EthHandler.java From ethereumj with MIT License | 5 votes |
private void processBlockHashes(BlockHashesMessage blockHashesMessage) { Blockchain blockchain = WorldManager.getInstance().getBlockchain(); List<byte[]> receivedHashes = blockHashesMessage.getBlockHashes(); BlockQueue chainQueue = blockchain.getQueue(); // result is empty, peer has no more hashes // or peer doesn't have the best hash anymore if (receivedHashes.isEmpty() || !this.peerId.equals(hashRetrievalLock)) { startGetBlockTimer(); // start getting blocks from hash queue return; } Iterator<byte[]> hashIterator = receivedHashes.iterator(); byte[] foundHash, latestHash = blockchain.getLatestBlockHash(); while (hashIterator.hasNext()) { foundHash = hashIterator.next(); if (FastByteComparisons.compareTo(foundHash, 0, 32, latestHash, 0, 32) != 0){ chainQueue.addHash(foundHash); // store unknown hashes in queue until known hash is found } else { logger.trace("Catch up with the hashes until: {[]}", foundHash); // if known hash is found, ignore the rest startGetBlockTimer(); // start getting blocks from hash queue return; } } // no known hash has been reached chainQueue.logHashQueueSize(); sendGetBlockHashes(); // another getBlockHashes with last received hash. }
Example 5
Source File: ByteArrayWrapper.java From ethereumj with MIT License | 5 votes |
public boolean equals(Object other) { if (!(other instanceof ByteArrayWrapper)) return false; byte[] otherData = ((ByteArrayWrapper) other).getData(); return FastByteComparisons.compareTo( data, 0, data.length, otherData, 0, otherData.length) == 0; }
Example 6
Source File: DataWord.java From ethereumj with MIT License | 5 votes |
@Override public int compareTo(DataWord o) { if (o == null || o.getData() == null) return -1; int result = FastByteComparisons.compareTo( data, 0, data.length, o.getData(), 0, o.getData().length); // Convert result into -1, 0 or 1 as is the convention return (int) Math.signum(result); }
Example 7
Source File: ByteArrayWrapper.java From nuls with MIT License | 5 votes |
@Override public boolean equals(Object other) { if (!(other instanceof ByteArrayWrapper)) { return false; } byte[] otherData = ((ByteArrayWrapper) other).getData(); return FastByteComparisons.compareTo( data, 0, data.length, otherData, 0, otherData.length) == 0; }
Example 8
Source File: DataWord.java From nuls-v2 with MIT License | 5 votes |
@Override public int compareTo(DataWord o) { if (o == null) { return -1; } int result = FastByteComparisons.compareTo( data, 0, data.length, o.data, 0, o.data.length); // Convert result into -1, 0 or 1 as is the convention return (int) Math.signum(result); }
Example 9
Source File: HashMapDB.java From nuls with MIT License | 5 votes |
@Override public V prefixLookup(byte[] key, int prefixBytes) { try (ALock l = readLock.lock()) { for (Map.Entry<byte[], V> e : storage.entrySet()) { if (FastByteComparisons.compareTo(key, 0, prefixBytes, e.getKey(), 0, prefixBytes) == 0) { return e.getValue(); } } return null; } }
Example 10
Source File: DataWord.java From nuls with MIT License | 5 votes |
@Override public int compareTo(DataWord o) { if (o == null) { return -1; } int result = FastByteComparisons.compareTo( data, 0, data.length, o.data, 0, o.data.length); // Convert result into -1, 0 or 1 as is the convention return (int) Math.signum(result); }
Example 11
Source File: ByteArrayWrapper.java From wkcwallet-java with Apache License 2.0 | 5 votes |
public boolean equals(Object other) { if (!(other instanceof ByteArrayWrapper)) return false; byte[] otherData = ((ByteArrayWrapper) other).getData(); return FastByteComparisons.compareTo( data, 0, data.length, otherData, 0, otherData.length) == 0; }
Example 12
Source File: ByteArrayWrapper.java From nuls-v2 with MIT License | 5 votes |
@Override public boolean equals(Object other) { if (!(other instanceof ByteArrayWrapper)) { return false; } byte[] otherData = ((ByteArrayWrapper) other).getData(); return FastByteComparisons.compareTo( data, 0, data.length, otherData, 0, otherData.length) == 0; }
Example 13
Source File: HashMapDBSimple.java From nuls-v2 with MIT License | 5 votes |
@Override public V prefixLookup(byte[] key, int prefixBytes) { for (Map.Entry<byte[], V> e : storage.entrySet()) { if (FastByteComparisons.compareTo(key, 0, prefixBytes, e.getKey(), 0, prefixBytes) == 0) { return e.getValue(); } } return null; }
Example 14
Source File: HashMapDB.java From nuls-v2 with MIT License | 5 votes |
@Override public V prefixLookup(byte[] key, int prefixBytes) { try (ALock l = readLock.lock()) { for (Map.Entry<byte[], V> e : storage.entrySet()) { if (FastByteComparisons.compareTo(key, 0, prefixBytes, e.getKey(), 0, prefixBytes) == 0) { return e.getValue(); } } return null; } }
Example 15
Source File: ByteArrayWrapper.java From wkcwallet-java with Apache License 2.0 | 4 votes |
@Override public int compareTo(ByteArrayWrapper o) { return FastByteComparisons.compareTo( data, 0, data.length, o.getData(), 0, o.getData().length); }
Example 16
Source File: ByteArrayWrapper.java From nuls with MIT License | 4 votes |
@Override public int compareTo(ByteArrayWrapper o) { return FastByteComparisons.compareTo( data, 0, data.length, o.getData(), 0, o.getData().length); }
Example 17
Source File: ByteArrayWrapper.java From nuls-v2 with MIT License | 4 votes |
@Override public int compareTo(ByteArrayWrapper o) { return FastByteComparisons.compareTo( data, 0, data.length, o.getData(), 0, o.getData().length); }
Example 18
Source File: ByteArrayWrapper.java From ethereumj with MIT License | 4 votes |
@Override public int compareTo(ByteArrayWrapper o) { return FastByteComparisons.compareTo( data, 0, data.length, o.getData(), 0, o.getData().length); }
Example 19
Source File: Miner.java From ethereumj with MIT License | 4 votes |
/** * 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) { BigInteger max = BigInteger.valueOf(2).pow(256); byte[] target = BigIntegers.asUnsignedByteArray(32, max.divide(new BigInteger(1, difficulty))); byte[] hash = HashUtil.sha3(newBlock.getEncodedWithoutNonce()); byte[] testNonce = new byte[32]; byte[] concat; while(ByteUtil.increment(testNonce)) { concat = Arrays.concatenate(hash, testNonce); byte[] result = HashUtil.sha3(concat); if(FastByteComparisons.compareTo(result, 0, 32, target, 0, 32) < 0) { newBlock.setNonce(testNonce); // System.out.println(Hex.toHexString(newBlock.getEncoded())); return true; } } return false; // couldn't find a valid nonce }