Search in sources :

Example 11 with BlockResultDTO

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

the class Web3ImplTest method getBlockByHashBlockDoesNotExists.

@Test
public void getBlockByHashBlockDoesNotExists() {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    String blockHash = "0x1234000000000000000000000000000000000000000000000000000000000000";
    BlockResultDTO blockResult = web3.eth_getBlockByHash(blockHash, false);
    Assert.assertNull(blockResult);
    String hexString = web3.rsk_getRawBlockHeaderByHash(blockHash);
    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 12 with BlockResultDTO

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

the class Web3ImplTest method getBlockByNumberRetrieveLatestBlock.

@Test
public void getBlockByNumberRetrieveLatestBlock() {
    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()).parent(genesis).build();
    block1.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    BlockResultDTO blockResult = web3.eth_getBlockByNumber("latest", false);
    assertNotNull(blockResult);
    String blockHash = TypeConverter.toJsonHex(block1.getHash().toString());
    assertEquals(blockHash, blockResult.getHash());
}
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 13 with BlockResultDTO

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

the class Web3ImplTest method getUncleByBlockNumberAndIndexBlockWithUnclesCorrespondingToAnUnknownBlock.

@Test
public void getUncleByBlockNumberAndIndexBlockWithUnclesCorrespondingToAnUnknownBlock() {
    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 blockEhash = "0x" + blockE.getHash();
    BlockResultDTO result = web3.eth_getUncleByBlockNumberAndIndex("0x" + blockF.getNumber(), "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 14 with BlockResultDTO

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

the class Web3ImplTest method getBlockByHashWithFullTransactionsAsResult.

@Test
public void getBlockByHashWithFullTransactionsAsResult() {
    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, true);
    assertNotNull(bresult);
    assertEquals(block1HashString, bresult.getHash());
    assertEquals(1, bresult.getTransactions().size());
    assertEquals(block1HashString, ((TransactionResultDTO) bresult.getTransactions().get(0)).getBlockHash());
    assertEquals(0, bresult.getUncles().size());
    assertEquals("0x0", ((TransactionResultDTO) bresult.getTransactions().get(0)).getValue());
}
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 15 with BlockResultDTO

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

the class Web3ImplTest method getBlockByNumberRetrieveEarliestBlock.

@Test
public void getBlockByNumberRetrieveEarliestBlock() {
    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()).parent(genesis).build();
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String bnOrId = "earliest";
    BlockResultDTO blockResult = web3.eth_getBlockByNumber(bnOrId, false);
    assertNotNull(blockResult);
    String blockHash = genesis.getHashJsonString();
    assertEquals(blockHash, blockResult.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)

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