org.ethereum.util.FastByteComparisons Java Examples

The following examples show how to use org.ethereum.util.FastByteComparisons. 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: ProgramExecutorImpl.java    From nuls with MIT License 6 votes vote down vote up
@Override
public ProgramResult stop(byte[] address, byte[] sender) {
    checkThread();
    AccountState accountState = repository.getAccountState(address);
    if (accountState == null) {
        return revert("can't find contract");
    }
    if (!FastByteComparisons.equal(sender, accountState.getOwner())) {
        return revert("only the owner can stop the contract");
    }
    BigInteger balance = getTotalBalance(address, null);
    if (BigInteger.ZERO.compareTo(balance) != 0) {
        return revert("contract balance is not zero");
    }
    if (BigInteger.ZERO.compareTo(accountState.getNonce()) >= 0) {
        return revert("contract has stopped");
    }

    repository.setNonce(address, BigInteger.ZERO);

    ProgramResult programResult = new ProgramResult();

    return programResult;
}
 
Example #2
Source File: ProgramExecutorImpl.java    From nuls-v2 with MIT License 6 votes vote down vote up
@Override
public ProgramResult stop(long blockNumber, byte[] address, byte[] sender) {
    checkThread();
    AccountState accountState = repository.getAccountState(address);
    if (accountState == null) {
        return revert("can't find contract");
    }
    if (!FastByteComparisons.equal(sender, accountState.getOwner())) {
        return revert("only the owner can stop the contract");
    }
    BigInteger balance = getTotalBalance(address, null);
    if (BigInteger.ZERO.compareTo(balance) != 0) {
        return revert("contract balance is not zero");
    }
    if (BigInteger.ZERO.compareTo(accountState.getNonce()) >= 0) {
        return revert("contract has stopped");
    }

    this.blockNumber = blockNumber;
    repository.setNonce(address, BigInteger.ZERO);

    ProgramResult programResult = new ProgramResult();

    return programResult;
}
 
Example #3
Source File: HashMapDBSimple.java    From nuls with MIT License 5 votes vote down vote up
@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 #4
Source File: ByteArrayWrapperTest.java    From ethereumj with MIT License 5 votes vote down vote up
@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 #5
Source File: BlockHeader.java    From ethereumj with MIT License 5 votes vote down vote up
/**
 * 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 #6
Source File: EthHandler.java    From ethereumj with MIT License 5 votes vote down vote up
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 #7
Source File: ByteArrayWrapper.java    From ethereumj with MIT License 5 votes vote down vote up
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 ethereumj with MIT License 5 votes vote down vote up
@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 #9
Source File: BlockHeader.java    From nuls with MIT License 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    BlockHeader that = (BlockHeader) o;
    return FastByteComparisons.equal(getHash(), that.getHash());
}
 
Example #10
Source File: ByteArrayWrapper.java    From nuls with MIT License 5 votes vote down vote up
@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 #11
Source File: ChainItem.java    From nuls with MIT License 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    ChainItem that = (ChainItem) o;
    return FastByteComparisons.equal(hash, that.hash);
}
 
Example #12
Source File: TrieImpl.java    From nuls with MIT License 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }

    TrieImpl trieImpl1 = (TrieImpl) o;

    return FastByteComparisons.equal(getRootHash(), trieImpl1.getRootHash());

}
 
Example #13
Source File: TrieImpl.java    From nuls with MIT License 5 votes vote down vote up
@Override
public void setRoot(byte[] root) {
    if (root != null && !FastByteComparisons.equal(root, EMPTY_TRIE_HASH)) {
        this.root = new Node(root);
    } else {
        this.root = null;
    }

}
 
Example #14
Source File: DataWord.java    From nuls-v2 with MIT License 5 votes vote down vote up
@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 #15
Source File: HashMapDB.java    From nuls with MIT License 5 votes vote down vote up
@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 #16
Source File: HashMapDB.java    From nuls-v2 with MIT License 5 votes vote down vote up
@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 #17
Source File: ByteArrayWrapper.java    From wkcwallet-java with Apache License 2.0 5 votes vote down vote up
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 #18
Source File: BlockHeader.java    From nuls-v2 with MIT License 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    BlockHeader that = (BlockHeader) o;
    return FastByteComparisons.equal(getHash(), that.getHash());
}
 
Example #19
Source File: HashMapDBSimple.java    From nuls-v2 with MIT License 5 votes vote down vote up
@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 #20
Source File: TrieImpl.java    From nuls-v2 with MIT License 5 votes vote down vote up
@Override
public void setRoot(byte[] root) {
    if (root != null && !FastByteComparisons.equal(root, EMPTY_TRIE_HASH)) {
        this.root = new Node(root);
    } else {
        this.root = null;
    }

}
 
Example #21
Source File: ByteArrayWrapper.java    From nuls-v2 with MIT License 5 votes vote down vote up
@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 #22
Source File: TrieImpl.java    From nuls-v2 with MIT License 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }

    TrieImpl trieImpl1 = (TrieImpl) o;

    return FastByteComparisons.equal(getRootHash(), trieImpl1.getRootHash());

}
 
Example #23
Source File: DataWord.java    From nuls with MIT License 5 votes vote down vote up
@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 #24
Source File: ChainItem.java    From nuls-v2 with MIT License 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    ChainItem that = (ChainItem) o;
    return FastByteComparisons.equal(hash, that.hash);
}
 
Example #25
Source File: AccountState.java    From nuls with MIT License 4 votes vote down vote up
public boolean isEmpty() {
    return FastByteComparisons.equal(codeHash, EMPTY_DATA_HASH) &&
            BigInteger.ZERO.equals(balance) &&
            BigInteger.ZERO.equals(nonce);
}
 
Example #26
Source File: Miner.java    From ethereumj with MIT License 4 votes vote down vote up
/**
	 * 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
	}
 
Example #27
Source File: ByteArrayWrapper.java    From ethereumj with MIT License 4 votes vote down vote up
@Override
public int compareTo(ByteArrayWrapper o) {
	return FastByteComparisons.compareTo(
			data, 0, data.length, 
			o.getData(), 0, o.getData().length);
}
 
Example #28
Source File: RepositoryImpl.java    From nuls-v2 with MIT License 4 votes vote down vote up
@Override
public synchronized byte[] getCode(byte[] addr) {
    byte[] codeHash = getCodeHash(addr);
    return codeHash == null || FastByteComparisons.equal(codeHash, HashUtil.EMPTY_DATA_HASH) ?
            ByteUtil.EMPTY_BYTE_ARRAY : codeCache.get(codeKey(codeHash, addr));
}
 
Example #29
Source File: ChainItem.java    From nuls-v2 with MIT License 4 votes vote down vote up
boolean isParentOf(ChainItem that) {
    return FastByteComparisons.equal(hash, that.parentHash);
}
 
Example #30
Source File: ByteArrayWrapper.java    From nuls with MIT License 4 votes vote down vote up
@Override
public int compareTo(ByteArrayWrapper o) {
    return FastByteComparisons.compareTo(
            data, 0, data.length,
            o.getData(), 0, o.getData().length);
}