Search in sources :

Example 1 with BlockResultDTO

use of org.ethereum.rpc.dto.BlockResultDTO in project rskj by rsksmart.

the class Web3Impl method eth_getUncleByBlockNumberAndIndex.

@Override
public BlockResultDTO eth_getUncleByBlockNumberAndIndex(String blockId, String uncleIdx) {
    BlockResultDTO s = null;
    try {
        Optional<Block> block = web3InformationRetriever.getBlock(blockId);
        if (!block.isPresent()) {
            return null;
        }
        s = getUncleResultDTO(uncleIdx, block.get());
        return s;
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("eth_getUncleByBlockNumberAndIndex({}, {}): {}", blockId, uncleIdx, s);
        }
    }
}
Also used : BlockResultDTO(org.ethereum.rpc.dto.BlockResultDTO) Block(org.ethereum.core.Block)

Example 2 with BlockResultDTO

use of org.ethereum.rpc.dto.BlockResultDTO in project rskj by rsksmart.

the class Web3ImplTest method getBlockByHash.

@Test
public void getBlockByHash() {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
    block1.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    Block block1b = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(block1.getDifficulty().asBigInteger().longValue() - 1).parent(genesis).build();
    block1b.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    Block block2b = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(2).parent(block1b).build();
    block2b.setBitcoinMergedMiningHeader(new byte[] { 0x02 });
    assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2b));
    String block1HashString = "0x" + block1.getHash();
    String block1bHashString = "0x" + block1b.getHash();
    String block2bHashString = "0x" + block2b.getHash();
    BlockResultDTO bresult = web3.eth_getBlockByHash(block1HashString, false);
    assertNotNull(bresult);
    assertEquals(block1HashString, bresult.getHash());
    assertEquals("0x", bresult.getExtraData());
    assertEquals(0, bresult.getTransactions().size());
    assertEquals(0, bresult.getUncles().size());
    assertEquals("0xa", bresult.getDifficulty());
    assertEquals("0xb", bresult.getTotalDifficulty());
    bresult = web3.eth_getBlockByHash(block1bHashString, true);
    assertNotNull(bresult);
    assertEquals(block1bHashString, bresult.getHash());
    String hexString = web3.rsk_getRawBlockHeaderByHash(block1bHashString).replace("0x", "");
    Keccak256 blockHash = new Keccak256(HashUtil.keccak256(Hex.decode(hexString)));
    assertEquals(blockHash.toJsonString(), block1bHashString);
    bresult = web3.eth_getBlockByHash(block2bHashString, true);
    assertNotNull(bresult);
    assertEquals(block2bHashString, bresult.getHash());
    hexString = web3.rsk_getRawBlockHeaderByHash(block2bHashString).replace("0x", "");
    blockHash = new Keccak256(HashUtil.keccak256(Hex.decode(hexString)));
    assertEquals(blockHash.toJsonString(), block2bHashString);
}
Also used : BlockResultDTO(org.ethereum.rpc.dto.BlockResultDTO) Keccak256(co.rsk.crypto.Keccak256) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with BlockResultDTO

use of org.ethereum.rpc.dto.BlockResultDTO in project rskj by rsksmart.

the class Web3ImplTest method getUncleByBlockHashAndIndexBlockWithUncles.

@Test
public void getUncleByBlockHashAndIndexBlockWithUncles() {
    /* Structure:
         *    Genesis
         * |     |     |
         * A     B     C
         * | \  / ____/
         * D  E
         * | /
         * F
         *
         * A-D-F mainchain
         * B and C uncles of E
         * E uncle of F
         * */
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Block genesis = world.getBlockChain().getBestBlock();
    Block blockA = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
    blockA.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(blockA));
    Block blockB = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
    blockB.setBitcoinMergedMiningHeader(new byte[] { 0x02 });
    assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(blockB));
    Block blockC = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
    blockC.setBitcoinMergedMiningHeader(new byte[] { 0x03 });
    assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(blockC));
    // block D must have a higher difficulty than block E and its uncles so it doesn't fall behind due to a reorg
    Block blockD = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(100).parent(blockA).build();
    blockD.setBitcoinMergedMiningHeader(new byte[] { 0x04 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(blockD));
    List<BlockHeader> blockEUncles = Arrays.asList(blockB.getHeader(), blockC.getHeader());
    Block blockE = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(blockA).uncles(blockEUncles).build();
    blockE.setBitcoinMergedMiningHeader(new byte[] { 0x05 });
    assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(blockE));
    List<BlockHeader> blockFUncles = Arrays.asList(blockE.getHeader());
    Block blockF = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(blockD).uncles(blockFUncles).build();
    blockF.setBitcoinMergedMiningHeader(new byte[] { 0x06 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(blockF));
    String blockFhash = "0x" + blockF.getHash();
    String blockEhash = "0x" + blockE.getHash();
    String blockBhash = "0x" + blockB.getHash();
    String blockChash = "0x" + blockC.getHash();
    BlockResultDTO result = web3.eth_getUncleByBlockHashAndIndex(blockFhash, "0x00");
    assertEquals(blockEhash, result.getHash());
    assertEquals(2, result.getUncles().size());
    assertTrue(result.getUncles().contains(blockBhash));
    assertTrue(result.getUncles().contains(blockChash));
    assertEquals(TypeConverter.toQuantityJsonHex(30), result.getCumulativeDifficulty());
}
Also used : BlockResultDTO(org.ethereum.rpc.dto.BlockResultDTO) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with BlockResultDTO

use of org.ethereum.rpc.dto.BlockResultDTO in project rskj by rsksmart.

the class Web3ImplTest method getBlockByHashWithTransactionsHashAsResult.

@Test
public void getBlockByHashWithTransactionsHashAsResult() {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(220000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(0)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).parent(genesis).transactions(txs).build();
    block1.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String block1HashString = block1.getHashJsonString();
    BlockResultDTO bresult = web3.eth_getBlockByHash(block1HashString, false);
    assertNotNull(bresult);
    assertEquals(block1HashString, bresult.getHash());
    assertEquals(1, bresult.getTransactions().size());
    assertEquals(tx.getHash().toJsonString(), bresult.getTransactions().get(0));
    assertEquals(0, bresult.getUncles().size());
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) BlockResultDTO(org.ethereum.rpc.dto.BlockResultDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with BlockResultDTO

use of org.ethereum.rpc.dto.BlockResultDTO in project rskj by rsksmart.

the class Web3ImplTest method getBlockByNumberBlockWithUncles.

@Test
public void getBlockByNumberBlockWithUncles() {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(20).parent(genesis).build();
    block1.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    Block block1b = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
    block1b.setBitcoinMergedMiningHeader(new byte[] { 0x02 });
    assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
    Block block1c = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
    block1c.setBitcoinMergedMiningHeader(new byte[] { 0x03 });
    assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1c));
    ArrayList<BlockHeader> uncles = new ArrayList<>();
    uncles.add(block1b.getHeader());
    uncles.add(block1c.getHeader());
    Block block2 = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(block1).uncles(uncles).build();
    block2.setBitcoinMergedMiningHeader(new byte[] { 0x04 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2));
    String block1HashString = "0x" + block1.getHash();
    String block1bHashString = "0x" + block1b.getHash();
    String block1cHashString = "0x" + block1c.getHash();
    String block2HashString = "0x" + block2.getHash();
    BlockResultDTO result = web3.eth_getBlockByNumber("0x02", false);
    assertEquals(block2HashString, result.getHash());
    assertEquals(block1HashString, result.getParentHash());
    assertTrue(result.getUncles().contains(block1bHashString));
    assertTrue(result.getUncles().contains(block1cHashString));
    assertEquals(TypeConverter.toQuantityJsonHex(30), result.getCumulativeDifficulty());
}
Also used : BlockResultDTO(org.ethereum.rpc.dto.BlockResultDTO) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

BlockResultDTO (org.ethereum.rpc.dto.BlockResultDTO)16 World (co.rsk.test.World)14 Test (org.junit.Test)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 BlockBuilder (co.rsk.test.builders.BlockBuilder)12 AccountBuilder (co.rsk.test.builders.AccountBuilder)4 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)4 Keccak256 (co.rsk.crypto.Keccak256)3 Block (org.ethereum.core.Block)2