2

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?

Malone
  • 1,590
  • 12
  • 23
berrytchaks
  • 123
  • 7

1 Answers1

2

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.

Malone
  • 1,590
  • 12
  • 23
  • thanks very much for the quick reply. How do I validate that this code does not use any real ETH/gas? – berrytchaks Nov 07 '17 at 09:47
  • Please see my updated answer. You'll get an idea of how it works by looking at some of the preexisting code for the StandaloneBlockchain.java tests. – Malone Nov 07 '17 at 10:40
  • Thanks @Malone for the reply. I have inspected the balance before and after the transaction but it is failing at 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
  • If you set the sender before making the call to the smart contract. Then the transaction will be made from that account. Allowing you to check the balance of that account after. Refer to the SolidityContract class to look at how you can interact with the contract. e.g. SolidityCallResult callFunction(String functionName, Object ... args); method. Checkout code that I've wrote relating to this: https://github.com/blmalone/Blockchain-Academic-Verification-Service/blob/master/app/main-blockchain/src/test/java/com/unilog/blockchain/SmartContractTest.java – Malone Nov 07 '17 at 11:56
  • ps. this is a separate blockchain the emulates what things would look like in the actual environment.Therefore the ETH spent on this chain is not mirror on the main net. Therefore, you are not spending 'real' ETH so to speak – Malone Nov 07 '17 at 12:02
  • Just to be clear. You can use contracts from the Ethereum main net on a private network using Ethereumj. The StandaloneBlockchain is just a quick/light way of emulating the blockchain locally. You can run an ethereum client on the JVM to create a private blockchain and have it mine blocks containing contract code from the main net. You'll not be using 'real' ETH because it's not the same blockchain as the main net i.e. differing genesis blocks. – Malone Nov 07 '17 at 12:20