use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method createChainWithACall.
private ChainParams createChainWithACall(boolean isCanonicalBlock) {
World world = new World();
Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
/* 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();
final String contractAddress = TypeConverter.toJsonHex(tx.getContractAddress().getBytes());
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
final Block block = isCanonicalBlock ? createNonCanonicalBlock(world, txs) : createCanonicalBlock(world, txs);
CallArguments argsForCall = new CallArguments();
argsForCall.setFrom(TypeConverter.toJsonHex(acc1.getAddress().getBytes()));
argsForCall.setTo(contractAddress);
argsForCall.setData("0xead710c40000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000");
return new ChainParams(world, contractAddress, block, argsForCall);
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method getCode.
@Test
public void getCode() {
World world = new World();
Web3Impl web3 = createWeb3(world);
Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(100000000)).build();
byte[] code = new byte[] { 0x01, 0x02, 0x03 };
world.getRepository().saveCode(acc1.getAddress(), code);
Block genesis = world.getBlockChain().getBestBlock();
genesis.setStateRoot(world.getRepository().getRoot());
genesis.flushRLP();
world.getBlockStore().saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
Block block1 = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).parent(genesis).build();
assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
String accountAddress = ByteUtil.toHexString(acc1.getAddress().getBytes());
String scode = web3.eth_getCode(accountAddress, "0x1");
assertNotNull(scode);
assertEquals("0x" + ByteUtil.toHexString(code), scode);
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method getBlockByNumberBlockWithUncles.
@Test
public void getBlockByNumberBlockWithUncles() {
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(20).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(10).parent(genesis).build();
block1b.setBitcoinMergedMiningHeader(new byte[] { 0x02 });
assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
Block block1c = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
block1c.setBitcoinMergedMiningHeader(new byte[] { 0x03 });
assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1c));
ArrayList<BlockHeader> uncles = new ArrayList<>();
uncles.add(block1b.getHeader());
uncles.add(block1c.getHeader());
Block block2 = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(block1).uncles(uncles).build();
block2.setBitcoinMergedMiningHeader(new byte[] { 0x04 });
assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2));
String block1HashString = "0x" + block1.getHash();
String block1bHashString = "0x" + block1b.getHash();
String block1cHashString = "0x" + block1c.getHash();
String block2HashString = "0x" + block2.getHash();
BlockResultDTO result = web3.eth_getBlockByNumber("0x02", false);
assertEquals(block2HashString, result.getHash());
assertEquals(block1HashString, result.getParentHash());
assertTrue(result.getUncles().contains(block1bHashString));
assertTrue(result.getUncles().contains(block1cHashString));
assertEquals(TypeConverter.toQuantityJsonHex(30), result.getCumulativeDifficulty());
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method getUncleByBlockNumberAndIndexBlockWithUncles.
@Test
public void getUncleByBlockNumberAndIndexBlockWithUncles() {
/* Structure:
* Genesis
* | | |
* A B C
* | \ / ____/
* D E
* | /
* F
*
* A-D-F mainchain
* B and C uncles of E
* E uncle of F
* */
World world = new World();
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));
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).build();
blockE.setBitcoinMergedMiningHeader(new byte[] { 0x05 });
assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(blockE));
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();
String blockBhash = "0x" + blockB.getHash();
String blockChash = "0x" + blockC.getHash();
BlockResultDTO result = web3.eth_getUncleByBlockNumberAndIndex("0x03", "0x00");
assertEquals(blockEhash, result.getHash());
assertEquals(2, result.getUncles().size());
assertTrue(result.getUncles().contains(blockBhash));
assertTrue(result.getUncles().contains(blockChash));
assertEquals(TypeConverter.toQuantityJsonHex(30), result.getCumulativeDifficulty());
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method getBlockByNumberWhenNumberIsInvalidThrowsException.
@Test(expected = org.ethereum.rpc.exception.RskJsonRpcRequestException.class)
public void getBlockByNumberWhenNumberIsInvalidThrowsException() {
World world = new World();
Web3Impl web3 = createWeb3(world);
String bnOrId = "991234";
web3.eth_getBlockByNumber(bnOrId, false);
}
Aggregations