Search in sources :

Example 6 with TransactionResultDTO

use of org.ethereum.rpc.dto.TransactionResultDTO 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)

Example 7 with TransactionResultDTO

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

the class Web3ImplTest method getTransactionByBlockHashAndIndex.

@Test
public void getTransactionByBlockHashAndIndex() throws Exception {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(2000000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String hashString = tx.getHash().toHexString();
    String blockHashString = block1.getHash().toHexString();
    TransactionResultDTO tr = web3.eth_getTransactionByBlockHashAndIndex(blockHashString, "0x0");
    Assert.assertNotNull(tr);
    org.junit.Assert.assertEquals("0x" + hashString, tr.hash);
    org.junit.Assert.assertEquals("0x" + blockHashString, tr.blockHash);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) TransactionResultDTO(org.ethereum.rpc.dto.TransactionResultDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 8 with TransactionResultDTO

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

the class Web3ImplTest method getTransactionByBlockNumberAndIndex.

@Test
public void getTransactionByBlockNumberAndIndex() throws Exception {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(2000000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String hashString = tx.getHash().toHexString();
    String blockHashString = block1.getHash().toHexString();
    TransactionResultDTO tr = web3.eth_getTransactionByBlockNumberAndIndex("0x01", "0x0");
    Assert.assertNotNull(tr);
    org.junit.Assert.assertEquals("0x" + hashString, tr.hash);
    org.junit.Assert.assertEquals("0x" + blockHashString, tr.blockHash);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) TransactionResultDTO(org.ethereum.rpc.dto.TransactionResultDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 9 with TransactionResultDTO

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

the class Web3ImplTest method getTransactionByHashNotInMainBlockchain.

@Test
public void getTransactionByHashNotInMainBlockchain() throws Exception {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(2000000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).difficulty(10).parent(genesis).transactions(txs).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    Block block1b = new BlockBuilder(world).difficulty(block1.getDifficulty().asBigInteger().longValue() - 1).parent(genesis).build();
    Block block2b = new BlockBuilder(world).difficulty(block1.getDifficulty().asBigInteger().longValue() + 1).parent(block1b).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2b));
    String hashString = tx.getHash().toHexString();
    TransactionResultDTO tr = web3.eth_getTransactionByHash(hashString);
    Assert.assertNull(tr);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) TransactionResultDTO(org.ethereum.rpc.dto.TransactionResultDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 10 with TransactionResultDTO

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

the class Web3ImplTest method getUnknownTransactionByBlockHashAndIndex.

@Test
public void getUnknownTransactionByBlockHashAndIndex() throws Exception {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).parent(genesis).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String blockHashString = block1.getHash().toString();
    TransactionResultDTO tr = web3.eth_getTransactionByBlockHashAndIndex(blockHashString, "0x0");
    Assert.assertNull(tr);
}
Also used : TransactionResultDTO(org.ethereum.rpc.dto.TransactionResultDTO) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

TransactionResultDTO (org.ethereum.rpc.dto.TransactionResultDTO)11 World (co.rsk.test.World)7 Test (org.junit.Test)7 BlockBuilder (co.rsk.test.builders.BlockBuilder)6 AccountBuilder (co.rsk.test.builders.AccountBuilder)5 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)5 Coin (co.rsk.core.Coin)1 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)1 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)1 Keccak256 (co.rsk.crypto.Keccak256)1 TransactionInfo (org.ethereum.db.TransactionInfo)1