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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations