Search in sources :

Example 86 with Keccak256

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()));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 87 with Keccak256

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()));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 88 with Keccak256

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;
}
Also used : BigInteger(java.math.BigInteger) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256)

Example 89 with Keccak256

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);
}
Also used : Keccak256(co.rsk.crypto.Keccak256) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 90 with Keccak256

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);
    }
}
Also used : TransactionInfo(org.ethereum.db.TransactionInfo) TransactionResultDTO(org.ethereum.rpc.dto.TransactionResultDTO) Keccak256(co.rsk.crypto.Keccak256)

Aggregations

Keccak256 (co.rsk.crypto.Keccak256)102 Test (org.junit.Test)53 Block (org.ethereum.core.Block)40 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 BigInteger (java.math.BigInteger)14 RskSystemProperties (co.rsk.config.RskSystemProperties)8 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)8 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)8 HashMapDB (org.ethereum.datasource.HashMapDB)8 RepositoryImpl (co.rsk.db.RepositoryImpl)7 ArrayList (java.util.ArrayList)7 Blockchain (org.ethereum.core.Blockchain)7 BlockStore (org.ethereum.db.BlockStore)7 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)7 RLPList (org.ethereum.util.RLPList)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Script (co.rsk.bitcoinj.script.Script)5 Coin (co.rsk.core.Coin)5 IOException (java.io.IOException)5 BlockHeader (org.ethereum.core.BlockHeader)5