Search in sources :

Example 31 with TransactionBuilder

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());
}
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 32 with TransactionBuilder

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);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 33 with TransactionBuilder

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);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) World(co.rsk.test.World) TestContract(co.rsk.util.TestContract) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 34 with TransactionBuilder

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);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World)

Example 35 with TransactionBuilder

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());
}
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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

TransactionBuilder (co.rsk.test.builders.TransactionBuilder)46 AccountBuilder (co.rsk.test.builders.AccountBuilder)41 Test (org.junit.Test)25 World (co.rsk.test.World)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 Account (org.ethereum.core.Account)17 BlockBuilder (co.rsk.test.builders.BlockBuilder)13 Transaction (org.ethereum.core.Transaction)12 ReceiptStore (org.ethereum.db.ReceiptStore)9 HashMapDB (org.ethereum.datasource.HashMapDB)6 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)6 BigInteger (java.math.BigInteger)5 ArrayList (java.util.ArrayList)5 TransactionResultDTO (org.ethereum.rpc.dto.TransactionResultDTO)5 BlockResultDTO (org.ethereum.rpc.dto.BlockResultDTO)4 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)3 Keccak256 (co.rsk.crypto.Keccak256)3 Trie (co.rsk.trie.Trie)3 Block (org.ethereum.core.Block)3 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)2