use of co.rsk.test.builders.TransactionBuilder 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());
}
use of co.rsk.test.builders.TransactionBuilder in project rskj by rsksmart.
the class Web3ImplTest method getTransactionCount.
@Test
public void getTransactionCount() {
World world = new World();
Web3Impl web3 = createWeb3(world);
Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(100000000)).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 block1 = createCanonicalBlock(world, txs);
String accountAddress = ByteUtil.toHexString(acc1.getAddress().getBytes());
String count = web3.eth_getTransactionCount(accountAddress, "0x1");
assertNotNull(count);
assertEquals("0x1", count);
count = web3.eth_getTransactionCount(accountAddress, "0x0");
assertNotNull(count);
assertEquals("0x0", count);
}
use of co.rsk.test.builders.TransactionBuilder in project rskj by rsksmart.
the class Web3ImplTest method callNoneContractReturn.
@Test
public void callNoneContractReturn() {
World world = new World();
Account acc1 = new AccountBuilder(world).name("default").balance(Coin.valueOf(10000000)).build();
Block genesis = world.getBlockByName("g00");
TestContract contract = TestContract.noReturn();
Transaction tx = new TransactionBuilder().sender(acc1).gasLimit(BigInteger.valueOf(500000)).gasPrice(BigInteger.ONE).data(Hex.decode(contract.bytecode)).build();
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
Block block1 = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).parent(genesis).transactions(txs).build();
assertTrue(world.getBlockChain().tryToConnect(block1).isSuccessful());
Web3Impl web3 = createWeb3Mocked(world);
CallTransaction.Function func = contract.functions.get("noreturn");
CallArguments argsForCall = new CallArguments();
argsForCall.setTo(TypeConverter.toUnformattedJsonHex(tx.getContractAddress().getBytes()));
argsForCall.setData(TypeConverter.toUnformattedJsonHex(func.encode()));
String result = web3.eth_call(argsForCall, "latest");
assertEquals("0x", result);
}
use of co.rsk.test.builders.TransactionBuilder in project rskj by rsksmart.
the class Web3ImplTest method createChainWithATransaction.
private ChainParams createChainWithATransaction(boolean isCanonicalBlock) {
final World world = new World();
Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(100000000)).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);
final String accountAddress = ByteUtil.toHexString(acc1.getAddress().getBytes());
final Block block = isCanonicalBlock ? createNonCanonicalBlock(world, txs) : createCanonicalBlock(world, txs);
return new ChainParams(world, accountAddress, block);
}
use of co.rsk.test.builders.TransactionBuilder in project rskj by rsksmart.
the class Web3ImplTest method getTransactionByBlockNumberAndIndex.
@Test
public void getTransactionByBlockNumberAndIndex() {
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 block1 = createCanonicalBlock(world, txs);
String hashString = tx.getHash().toHexString();
String blockHashString = block1.getHash().toHexString();
TransactionResultDTO tr = web3.eth_getTransactionByBlockNumberAndIndex("0x01", "0x0");
assertNotNull(tr);
assertEquals("0x" + hashString, tr.getHash());
assertEquals("0x" + blockHashString, tr.getBlockHash());
}
Aggregations