Search in sources :

Example 6 with BlockResultDTO

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

the class Web3ImplTest method getUncleByBlockNumberAndIndexBlockWithUncles.

@Test
public void getUncleByBlockNumberAndIndexBlockWithUncles() {
    /* 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 blockEhash = "0x" + blockE.getHash();
    String blockBhash = "0x" + blockB.getHash();
    String blockChash = "0x" + blockC.getHash();
    BlockResultDTO result = web3.eth_getUncleByBlockNumberAndIndex("0x03", "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 7 with BlockResultDTO

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

the class Web3ImplTest method getBlockByNumber.

@Test
public void getBlockByNumber() {
    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();
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    Block block1b = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(2).parent(genesis).build();
    block1b.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    Block block2b = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(11).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));
    BlockResultDTO bresult = web3.eth_getBlockByNumber("0x1", false);
    assertNotNull(bresult);
    String blockHash = "0x" + block1b.getHash();
    assertEquals(blockHash, bresult.getHash());
    String bnOrId = "0x2";
    bresult = web3.eth_getBlockByNumber("0x2", true);
    assertNotNull(bresult);
    blockHash = "0x" + block2b.getHash();
    assertEquals(blockHash, bresult.getHash());
    String hexString = web3.rsk_getRawBlockHeaderByNumber(bnOrId).replace("0x", "");
    Keccak256 obtainedBlockHash = new Keccak256(HashUtil.keccak256(Hex.decode(hexString)));
    assertEquals(blockHash, obtainedBlockHash.toJsonString());
}
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 8 with BlockResultDTO

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

the class Web3ImplTest method getUncleByBlockHashAndIndexBlockWithUnclesCorrespondingToAnUnknownBlock.

@Test
public void getUncleByBlockHashAndIndexBlockWithUnclesCorrespondingToAnUnknownBlock() {
    World world = new World();
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(10000)).build();
    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));
    Transaction tx = new TransactionBuilder().sender(acc1).gasLimit(BigInteger.valueOf(100000)).gasPrice(BigInteger.ONE).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    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).transactions(txs).buildWithoutExecution();
    blockE.setBitcoinMergedMiningHeader(new byte[] { 0x05 });
    assertEquals(1, blockE.getTransactionsList().size());
    Assert.assertFalse(Arrays.equals(blockC.getTxTrieRoot(), blockE.getTxTrieRoot()));
    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();
    BlockResultDTO result = web3.eth_getUncleByBlockHashAndIndex(blockFhash, "0x00");
    assertEquals(blockEhash, result.getHash());
    assertEquals(0, result.getUncles().size());
    assertEquals(0, result.getTransactions().size());
    assertEquals("0x" + ByteUtil.toHexString(blockE.getTxTrieRoot()), result.getTransactionsRoot());
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) BlockResultDTO(org.ethereum.rpc.dto.BlockResultDTO) World(co.rsk.test.World) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with BlockResultDTO

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

the class Web3ImplTest method getBlockByNumberBlockDoesNotExists.

@Test
public void getBlockByNumberBlockDoesNotExists() {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    String bnOrId = "0x1234";
    BlockResultDTO blockResult = web3.eth_getBlockByNumber(bnOrId, false);
    Assert.assertNull(blockResult);
    String hexString = web3.rsk_getRawBlockHeaderByNumber(bnOrId);
    Assert.assertNull(hexString);
}
Also used : BlockResultDTO(org.ethereum.rpc.dto.BlockResultDTO) World(co.rsk.test.World) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with BlockResultDTO

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

the class Web3ImplTest method getBlockByHashBlockWithUncles.

@Test
public void getBlockByHashBlockWithUncles() {
    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_getBlockByHash(block2HashString, 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