Is it possible to take a contract from the main ethereum blockchain and used that on my private ethereum network with ethereumj?
What I want to do is, run a contract with some input without changing the blockchain and spending gas.
Any clues?
Is it possible to take a contract from the main ethereum blockchain and used that on my private ethereum network with ethereumj?
What I want to do is, run a contract with some input without changing the blockchain and spending gas.
Any clues?
Ethereumj provides a class called StandAloneBlockchain.java. You can see it in use here.
StandaloneBlockchain sb = new StandaloneBlockchain().withAutoblock(true);
SolidityContract a = sb.submitNewContract(
"contract A {" +
" uint public a;" +
" uint public b;" +
" function A(uint a_, uint b_) {a = a_; b = b_; }" +
"}",
"A", 555, 777
);
With this you'll be able to test for expected results without actually using any real ETH/gas. Therefore you'll have an idea of how the real transaction may behave on the network.
You can do this by inspecting the balance before and after the transaction is executed. The code below asserts that an amount was sent; However you can conduct a more detailed inspection of the account balances to get an idea for how much the transaction cost.
BigInteger aliceInitBal = sb.getBlockchain().getRepository().getBalance(alice.getAddress());
BigInteger bobInitBal = sb.getBlockchain().getRepository().getBalance(bob.getAddress());
assert convert(123, ETHER).equals(bobInitBal);
sb.setSender(bob);
sb.sendEther(alice.getAddress(), BigInteger.ONE);
sb.createBlock();
assert convert(123, ETHER).compareTo(sb.getBlockchain().getRepository().getBalance(bob.getAddress())) > 0;
assert aliceInitBal.add(BigInteger.ONE).equals(sb.getBlockchain().getRepository().getBalance(alice.getAddress()));
The StandAloneBlockchain.java class is also useful for unit testing your Solidity contract.
Leave a comment if you need any clarification.
assert convert(123, ETHER).equals(bobInitBal);when I added the smart contract but works fine without the smart contract. – berrytchaks Nov 07 '17 at 11:40