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