Search in sources :

Example 76 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplTest method checkSendTransaction.

private void checkSendTransaction(Byte chainId) {
    BigInteger nonce = BigInteger.ONE;
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    BlockChainImpl blockChain = world.getBlockChain();
    BlockStore blockStore = world.getBlockStore();
    TransactionExecutorFactory transactionExecutorFactory = buildTransactionExecutorFactory(blockStore, world.getBlockTxSignatureCache());
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepositoryLocator(), blockStore, blockFactory, null, transactionExecutorFactory, world.getReceivedTxSignatureCache(), 10, 100);
    Web3Impl web3 = createWeb3(world, transactionPool, receiptStore);
    // **** Initializes data ******************
    String[] accounts = web3.personal_listAccounts();
    String addr1 = accounts[0];
    String addr2 = accounts[1];
    transactionPool.processBest(blockChain.getBestBlock());
    String toAddress = addr2;
    BigInteger value = BigInteger.valueOf(7);
    BigInteger gasPrice = BigInteger.valueOf(8);
    BigInteger gasLimit = BigInteger.valueOf(300000);
    String data = "0xff";
    // ***** Executes the transaction *******************
    CallArguments args = new CallArguments();
    args.setFrom(addr1);
    args.setTo(addr2);
    args.setData(data);
    args.setGas(TypeConverter.toQuantityJsonHex(gasLimit));
    args.setGasPrice(TypeConverter.toQuantityJsonHex(gasPrice));
    args.setValue(value.toString());
    args.setNonce(nonce.toString());
    if (chainId != null) {
        args.setChainId(TypeConverter.toJsonHex(new byte[] { chainId }));
    }
    String txHash = web3.eth_sendTransaction(args);
    // ***** Verifies tx hash
    String to = toAddress.substring(2);
    Transaction tx = Transaction.builder().nonce(nonce).gasPrice(gasPrice).gasLimit(gasLimit).destination(Hex.decode(to)).data(args.getData() == null ? null : Hex.decode(args.getData())).chainId(config.getNetworkConstants().getChainId()).value(value).build();
    tx.sign(wallet.getAccount(new RskAddress(addr1)).getEcKey().getPrivKeyBytes());
    String expectedHash = tx.getHash().toJsonString();
    assertEquals("Method is not creating the expected transaction", 0, expectedHash.compareTo(txHash));
}
Also used : BlockStore(org.ethereum.db.BlockStore) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) BigInteger(java.math.BigInteger) ReceiptStore(org.ethereum.db.ReceiptStore)

Example 77 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplTest method getUnknownTransactionReceipt.

@Test
public void getUnknownTransactionReceipt() {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    Web3Impl web3 = createWeb3(world, receiptStore);
    Account acc1 = new AccountBuilder().name("acc1").build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
    String hashString = tx.getHash().toHexString();
    Assert.assertNull(web3.eth_getTransactionReceipt(hashString));
    Assert.assertNull(web3.rsk_getRawTransactionReceiptByHash(hashString));
}
Also used : ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) TransactionBuilder(co.rsk.test.builders.TransactionBuilder) AccountBuilder(co.rsk.test.builders.AccountBuilder) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStore(org.ethereum.db.ReceiptStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 78 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplTest method getBalanceWithAccountAndGenesisBlock.

@Test
public void getBalanceWithAccountAndGenesisBlock() {
    World world = new World();
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(10000)).build();
    Web3Impl web3 = createWeb3(world);
    String accountAddress = ByteUtil.toHexString(acc1.getAddress().getBytes());
    assertEquals(BALANCE_10K_HEX, web3.eth_getBalance(accountAddress, "0x0"));
}
Also used : AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 79 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplTest method getBlockByNumber.

@Test
public void getBlockByNumber() {
    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()).difficulty(10).parent(genesis).build();
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    Block block1b = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(2).parent(genesis).build();
    block1b.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    Block block2b = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(11).parent(block1b).build();
    block2b.setBitcoinMergedMiningHeader(new byte[] { 0x02 });
    assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2b));
    BlockResultDTO bresult = web3.eth_getBlockByNumber("0x1", false);
    assertNotNull(bresult);
    String blockHash = "0x" + block1b.getHash();
    assertEquals(blockHash, bresult.getHash());
    String bnOrId = "0x2";
    bresult = web3.eth_getBlockByNumber("0x2", true);
    assertNotNull(bresult);
    blockHash = "0x" + block2b.getHash();
    assertEquals(blockHash, bresult.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)

Example 80 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplTest method callWithoutReturn.

@Test
public void callWithoutReturn() {
    World world = new World();
    Account acc1 = new AccountBuilder(world).name("default").balance(Coin.valueOf(10000000)).build();
    Block genesis = world.getBlockByName("g00");
    TestContract noreturn = TestContract.noreturn();
    Transaction tx = new TransactionBuilder().sender(acc1).gasLimit(BigInteger.valueOf(100000)).gasPrice(BigInteger.ONE).data(noreturn.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();
    world.getBlockChain().tryToConnect(block1);
    Web3Impl web3 = createWeb3MockedCallNoReturn(world);
    CallArguments argsForCall = new CallArguments();
    argsForCall.setTo(TypeConverter.toJsonHex(tx.getContractAddress().getBytes()));
    argsForCall.setData(TypeConverter.toJsonHex(noreturn.functions.get("noreturn").encodeSignature()));
    String result = web3.eth_call(argsForCall, "latest");
    org.junit.Assert.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)

Aggregations

World (co.rsk.test.World)198 Test (org.junit.Test)169 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)55 DslParser (co.rsk.test.dsl.DslParser)52 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 HashMapDB (org.ethereum.datasource.HashMapDB)45 AccountBuilder (co.rsk.test.builders.AccountBuilder)36 Block (org.ethereum.core.Block)36 ReceiptStore (org.ethereum.db.ReceiptStore)34 BlockBuilder (co.rsk.test.builders.BlockBuilder)31 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)31 Blockchain (org.ethereum.core.Blockchain)30 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)24 Transaction (org.ethereum.core.Transaction)23 BigInteger (java.math.BigInteger)17 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 Account (org.ethereum.core.Account)15 TestSystemProperties (co.rsk.config.TestSystemProperties)14