use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class ProofOfWorkRuleTest method mineBlockWithCoinbaseTransactionWithCompressedCoinbaseTransactionPrefix.
private static Block mineBlockWithCoinbaseTransactionWithCompressedCoinbaseTransactionPrefix(Block block, byte[] compressed) {
Keccak256 blockMergedMiningHash = new Keccak256(block.getHashForMergedMining());
co.rsk.bitcoinj.core.NetworkParameters bitcoinNetworkParameters = co.rsk.bitcoinj.params.RegTestParams.get();
co.rsk.bitcoinj.core.BtcTransaction bitcoinMergedMiningCoinbaseTransaction = MinerUtils.getBitcoinMergedMiningCoinbaseTransaction(bitcoinNetworkParameters, blockMergedMiningHash.getBytes());
co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = MinerUtils.getBitcoinMergedMiningBlock(bitcoinNetworkParameters, bitcoinMergedMiningCoinbaseTransaction);
BigInteger targetBI = DifficultyUtils.difficultyToTarget(block.getDifficulty());
BlockMiner.findNonce(bitcoinMergedMiningBlock, targetBI);
// We need to clone to allow modifications
Block newBlock = new Block(block.getEncoded()).cloneBlock();
newBlock.setBitcoinMergedMiningHeader(bitcoinMergedMiningBlock.cloneAsHeader().bitcoinSerialize());
co.rsk.bitcoinj.core.PartialMerkleTree bitcoinMergedMiningMerkleBranch = getBitcoinMergedMerkleBranch(bitcoinMergedMiningBlock);
newBlock.setBitcoinMergedMiningCoinbaseTransaction(org.spongycastle.util.Arrays.concatenate(compressed, blockMergedMiningHash.getBytes()));
newBlock.setBitcoinMergedMiningMerkleProof(bitcoinMergedMiningMerkleBranch.bitcoinSerialize());
return newBlock;
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class TrieImplSnapshotTest method getSnapshotToTrieWithLongValues.
@Test
public void getSnapshotToTrieWithLongValues() {
TrieStore store = new TrieStoreImpl(new HashMapDB());
Trie trie = new TrieImpl(store, false);
trie = trie.put("foo".getBytes(), TrieImplValueTest.makeValue(100));
Keccak256 hash = trie.getHash();
trie.save();
trie = trie.put("bar".getBytes(), TrieImplValueTest.makeValue(200));
Assert.assertNotNull(trie.get("foo".getBytes()));
Assert.assertNotNull(trie.get("bar".getBytes()));
Trie snapshot = trie.getSnapshotTo(hash);
Assert.assertNotNull(snapshot);
Assert.assertEquals(hash, snapshot.getHash());
Assert.assertNotNull(snapshot.get("foo".getBytes()));
Assert.assertNull(snapshot.get("bar".getBytes()));
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class TrieImplSnapshotTest method getSnapshotToTrieUsingDeserializedTrie.
@Test
public void getSnapshotToTrieUsingDeserializedTrie() {
TrieStore store = new TrieStoreImpl(new HashMapDB());
Trie trie = new TrieImpl(store, false);
trie = trie.put("foo".getBytes(), "bar".getBytes());
Keccak256 hash = trie.getHash();
trie.save();
trie = trie.put("bar".getBytes(), "foo".getBytes());
Assert.assertNotNull(trie.get("foo".getBytes()));
Assert.assertNotNull(trie.get("bar".getBytes()));
Trie snapshot = TrieImpl.deserialize(trie.serialize()).getSnapshotTo(hash);
Assert.assertNotNull(snapshot);
Assert.assertEquals(hash, snapshot.getHash());
Assert.assertNotNull(snapshot.get("foo".getBytes()));
Assert.assertNull(snapshot.get("bar".getBytes()));
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class TrieImplSnapshotTest method getSnapshotToTrieUsingDeserializedTrieWithLongValues.
@Test
public void getSnapshotToTrieUsingDeserializedTrieWithLongValues() {
byte[] value1 = TrieImplValueTest.makeValue(100);
byte[] value2 = TrieImplValueTest.makeValue(200);
TrieStore store = new TrieStoreImpl(new HashMapDB());
Trie trie = new TrieImpl(store, false);
trie = trie.put("foo".getBytes(), value1);
Keccak256 hash = trie.getHash();
trie.save();
trie = trie.put("bar".getBytes(), value2);
Assert.assertNotNull(trie.get("foo".getBytes()));
Assert.assertArrayEquals(value1, trie.get("foo".getBytes()));
Assert.assertNotNull(trie.get("bar".getBytes()));
Assert.assertArrayEquals(value2, trie.get("bar".getBytes()));
Trie snapshot = TrieImpl.deserialize(trie.serialize()).getSnapshotTo(hash);
Assert.assertNotNull(snapshot);
Assert.assertEquals(hash, snapshot.getHash());
Assert.assertNotNull(snapshot.get("foo".getBytes()));
Assert.assertArrayEquals(value1, snapshot.get("foo".getBytes()));
Assert.assertNull(snapshot.get("bar".getBytes()));
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class TrieImpl method getNodeCount.
/**
* getNodeCount the number of direct subnodes in this node
*
* @return the number of direct subnodes in this node
*
* Takes into account that a node could be not present as an object and
* only be referenced via its unique hash
*/
private int getNodeCount() {
int count = 0;
for (int k = 0; k < ARITY; k++) {
TrieImpl node = this.getNode(k);
Keccak256 localHash = this.getHash(k);
if (node != null && !isEmptyTrie(node.value, node.nodes, node.hashes) || localHash != null) {
count++;
}
}
return count;
}
Aggregations