use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplScoringTest method createWeb3.
private static Web3Impl createWeb3(PeerScoringManager peerScoringManager) {
SimpleEthereum rsk = new SimpleEthereum();
World world = new World();
rsk.blockchain = world.getBlockChain();
MiningMainchainView miningMainchainView = new MiningMainchainViewImpl(world.getBlockStore(), 2);
Wallet wallet = WalletFactory.createWallet();
TestSystemProperties config = new TestSystemProperties();
PersonalModule pm = new PersonalModuleWalletEnabled(config, rsk, wallet, null);
EthModule em = new EthModule(config.getNetworkConstants().getBridgeConstants(), config.getNetworkConstants().getChainId(), world.getBlockChain(), null, null, new ExecutionBlockRetriever(miningMainchainView, world.getBlockChain(), null, null), null, new EthModuleWalletEnabled(wallet), null, new BridgeSupportFactory(null, config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig()), config.getGasEstimationCap());
TxPoolModule tpm = new TxPoolModuleImpl(Web3Mocks.getMockTransactionPool());
DebugModule dm = new DebugModuleImpl(null, null, Web3Mocks.getMockMessageHandler(), null);
return new Web3RskImpl(rsk, world.getBlockChain(), config, Web3Mocks.getMockMinerClient(), Web3Mocks.getMockMinerServer(), pm, em, null, tpm, null, dm, null, null, Web3Mocks.getMockChannelManager(), peerScoringManager, null, null, null, null, null, null, null, null, null, null, null);
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method callFromAddressInWallet.
@Test
public void callFromAddressInWallet() {
World world = new World();
Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
Block genesis = world.getBlockByName("g00");
/* contract compiled in data attribute of tx
contract Greeter {
address owner;
function greeter() public {
owner = msg.sender;
}
function greet(string memory param) public pure returns (string memory) {
return param;
}
}
*/
Transaction tx = new TransactionBuilder().sender(acc1).gasLimit(BigInteger.valueOf(500000)).gasPrice(BigInteger.ONE).data("608060405234801561001057600080fd5b506101fa806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80631c8499e51461003b578063ead710c414610045575b600080fd5b610043610179565b005b6100fe6004803603602081101561005b57600080fd5b810190808035906020019064010000000081111561007857600080fd5b82018360208201111561008a57600080fd5b803590602001918460018302840111640100000000831117156100ac57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506101bb565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550565b606081905091905056fea265627a7a723158207cbf5ab8312143442836de7909c83aec5160dae50224ecc7c16d7f35a306901e64736f6c63430005100032").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);
web3.personal_newAccountWithSeed("default");
web3.personal_newAccountWithSeed("notDefault");
CallArguments argsForCall = new CallArguments();
argsForCall.setFrom(TypeConverter.toJsonHex(acc1.getAddress().getBytes()));
argsForCall.setTo(TypeConverter.toJsonHex(tx.getContractAddress().getBytes()));
argsForCall.setData("0xead710c40000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000");
String result = web3.eth_call(argsForCall, "latest");
assertEquals("0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000", result);
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method getBlockByHash.
@Test
public void getBlockByHash() {
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();
block1.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
Block block1b = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(block1.getDifficulty().asBigInteger().longValue() - 1).parent(genesis).build();
block1b.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
Block block2b = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(2).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));
String block1HashString = "0x" + block1.getHash();
String block1bHashString = "0x" + block1b.getHash();
String block2bHashString = "0x" + block2b.getHash();
BlockResultDTO bresult = web3.eth_getBlockByHash(block1HashString, false);
assertNotNull(bresult);
assertEquals(block1HashString, bresult.getHash());
assertEquals("0x", bresult.getExtraData());
assertEquals(0, bresult.getTransactions().size());
assertEquals(0, bresult.getUncles().size());
assertEquals("0xa", bresult.getDifficulty());
assertEquals("0xb", bresult.getTotalDifficulty());
bresult = web3.eth_getBlockByHash(block1bHashString, true);
assertNotNull(bresult);
assertEquals(block1bHashString, bresult.getHash());
String hexString = web3.rsk_getRawBlockHeaderByHash(block1bHashString).replace("0x", "");
Keccak256 blockHash = new Keccak256(HashUtil.keccak256(Hex.decode(hexString)));
assertEquals(blockHash.toJsonString(), block1bHashString);
bresult = web3.eth_getBlockByHash(block2bHashString, true);
assertNotNull(bresult);
assertEquals(block2bHashString, bresult.getHash());
hexString = web3.rsk_getRawBlockHeaderByHash(block2bHashString).replace("0x", "");
blockHash = new Keccak256(HashUtil.keccak256(Hex.decode(hexString)));
assertEquals(blockHash.toJsonString(), block2bHashString);
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method chainWithAccount10kBalance.
private ChainParams chainWithAccount10kBalance(boolean isCanonicalBlock) {
final World world = new World();
final String accountAddress = createAccountWith10KBalance(world);
final Block block = isCanonicalBlock ? createChainWithNonCanonicalBlock(world) : createChainWithOneBlock(world);
return new ChainParams(world, accountAddress, block);
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method getTransactionReceipt.
@Test
public void getTransactionReceipt() {
ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
World world = new World(receiptStore);
Web3Impl web3 = createWeb3(world, receiptStore);
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();
TransactionReceiptDTO tr = web3.eth_getTransactionReceipt(hashString);
assertNotNull(tr);
assertEquals("0x" + hashString, tr.getTransactionHash());
String trxFrom = TypeConverter.toJsonHex(tx.getSender().getBytes());
assertEquals(trxFrom, tr.getFrom());
String trxTo = TypeConverter.toJsonHex(tx.getReceiveAddress().getBytes());
assertEquals(trxTo, tr.getTo());
String blockHashString = "0x" + block1.getHash();
assertEquals(blockHashString, tr.getBlockHash());
String blockNumberAsHex = "0x" + Long.toHexString(block1.getNumber());
assertEquals(blockNumberAsHex, tr.getBlockNumber());
String rawTransactionReceipt = web3.rsk_getRawTransactionReceiptByHash(hashString);
String expectedRawTxReceipt = "0xf9010c01825208b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c082520801";
assertEquals(expectedRawTxReceipt, rawTransactionReceipt);
String[] transactionReceiptNodes = web3.rsk_getTransactionReceiptNodesByHash(blockHashString, hashString);
ArrayList<String> expectedRawTxReceiptNodes = new ArrayList<>();
expectedRawTxReceiptNodes.add("0x70078048ee76b19fc451dba9dbee8b3e73084f79ea540d3940b3b36b128e8024e9302500010f");
assertEquals(1, transactionReceiptNodes.length);
assertEquals(expectedRawTxReceiptNodes.get(0), transactionReceiptNodes[0]);
}
Aggregations