use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class FamilyUtilsTest method getFamilyGetParent.
@Test
public void getFamilyGetParent() {
BlockStore store = createBlockStore();
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
Block block1 = blockGenerator.createChildBlock(genesis);
store.saveBlock(genesis, TEST_DIFFICULTY, true);
store.saveBlock(block1, TEST_DIFFICULTY, true);
Set<Keccak256> family = FamilyUtils.getFamily(store, block1, 6);
Assert.assertNotNull(family);
Assert.assertFalse(family.isEmpty());
Assert.assertEquals(1, family.size());
Assert.assertTrue(family.contains(genesis.getHash()));
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class FamilyUtilsTest method getUncles.
@Test
public void getUncles() {
BlockStore store = createBlockStore();
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
Block block1 = blockGenerator.createChildBlock(genesis);
Block uncle11 = blockGenerator.createChildBlock(genesis);
Block uncle12 = blockGenerator.createChildBlock(genesis);
Block block2 = blockGenerator.createChildBlock(block1);
Block uncle21 = blockGenerator.createChildBlock(block1);
Block uncle22 = blockGenerator.createChildBlock(block1);
Block block3 = blockGenerator.createChildBlock(block2);
Block uncle31 = blockGenerator.createChildBlock(block2);
Block uncle32 = blockGenerator.createChildBlock(block2);
store.saveBlock(genesis, TEST_DIFFICULTY, true);
store.saveBlock(block1, TEST_DIFFICULTY, true);
store.saveBlock(uncle11, TEST_DIFFICULTY, false);
store.saveBlock(uncle12, TEST_DIFFICULTY, false);
store.saveBlock(block2, TEST_DIFFICULTY, true);
store.saveBlock(uncle21, TEST_DIFFICULTY, false);
store.saveBlock(uncle22, TEST_DIFFICULTY, false);
store.saveBlock(block3, TEST_DIFFICULTY, true);
store.saveBlock(uncle31, TEST_DIFFICULTY, false);
store.saveBlock(uncle32, TEST_DIFFICULTY, false);
Set<Keccak256> family = FamilyUtils.getUncles(store, block3, 3);
Assert.assertNotNull(family);
Assert.assertFalse(family.isEmpty());
Assert.assertEquals(4, family.size());
Assert.assertFalse(family.contains(genesis.getHash()));
Assert.assertFalse(family.contains(block1.getHash()));
Assert.assertTrue(family.contains(uncle11.getHash()));
Assert.assertTrue(family.contains(uncle12.getHash()));
Assert.assertFalse(family.contains(block2.getHash()));
Assert.assertTrue(family.contains(uncle21.getHash()));
Assert.assertTrue(family.contains(uncle22.getHash()));
Assert.assertFalse(family.contains(block3.getHash()));
Assert.assertFalse(family.contains(uncle31.getHash()));
Assert.assertFalse(family.contains(uncle32.getHash()));
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class BlockMiner method mineBlock.
public static Block mineBlock(Block block) {
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());
findNonce(bitcoinMergedMiningBlock, targetBI);
// We need to clone to allow modifications
Block newBlock = new Block(block.getEncoded()).cloneBlock();
newBlock.setBitcoinMergedMiningHeader(bitcoinMergedMiningBlock.cloneAsHeader().bitcoinSerialize());
bitcoinMergedMiningCoinbaseTransaction = bitcoinMergedMiningBlock.getTransactions().get(0);
co.rsk.bitcoinj.core.PartialMerkleTree bitcoinMergedMiningMerkleBranch = getBitcoinMergedMerkleBranch(bitcoinMergedMiningBlock);
newBlock.setBitcoinMergedMiningCoinbaseTransaction(compressCoinbase(bitcoinMergedMiningCoinbaseTransaction.bitcoinSerialize()));
newBlock.setBitcoinMergedMiningMerkleProof(bitcoinMergedMiningMerkleBranch.bitcoinSerialize());
return newBlock;
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class BlockTest method checkTxTrieShouldBeEqualForHeaderAndBody.
@Test
public void checkTxTrieShouldBeEqualForHeaderAndBody() {
Block block = new BlockGenerator().createBlock(10, 5);
Keccak256 trieHash = new Keccak256(block.getTxTrieRoot());
Keccak256 trieListHash = Block.getTxTrie(block.getTransactionsList()).getHash();
Assert.assertEquals(trieHash, trieListHash);
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class Web3Impl method eth_getTransactionByHash.
@Override
public TransactionResultDTO eth_getTransactionByHash(String transactionHash) throws Exception {
TransactionResultDTO s = null;
try {
Keccak256 txHash = new Keccak256(stringHexToByteArray(transactionHash));
Block block = null;
TransactionInfo txInfo = blockchain.getTransactionInfo(txHash.getBytes());
if (txInfo == null) {
List<Transaction> txs = this.getTransactionsByJsonBlockId("pending");
for (Transaction tx : txs) {
if (tx.getHash().equals(txHash)) {
return s = new TransactionResultDTO(null, null, tx);
}
}
} else {
block = blockchain.getBlockByHash(txInfo.getBlockHash());
// need to return txes only from main chain
Block mainBlock = blockchain.getBlockByNumber(block.getNumber());
if (!block.getHash().equals(mainBlock.getHash())) {
return null;
}
txInfo.setTransaction(block.getTransactionsList().get(txInfo.getIndex()));
}
if (txInfo == null) {
return null;
}
return s = new TransactionResultDTO(block, txInfo.getIndex(), txInfo.getReceipt().getTransaction());
} finally {
logger.debug("eth_getTransactionByHash({}): {}", transactionHash, s);
}
}
Aggregations