Search in sources :

Example 11 with ProgramInvokeMockImpl

use of org.ethereum.vm.program.invoke.ProgramInvokeMockImpl in project rskj by rsksmart.

the class VMComplexTest method test1.

// TODO #POC9
@Ignore
// contract call recursive
@Test
public void test1() {
    /**
     *       #The code will run
     *       ------------------
     *
     *                 a = contract.storage[999]
     *                 if a > 0:
     *                     contract.storage[999] = a - 1
     *
     *                     # call to contract: 77045e71a7a2c50903d88e564cd72fab11e82051
     *                     send((tx.gas / 10 * 8), 0x77045e71a7a2c50903d88e564cd72fab11e82051, 0)
     *                 else:
     *                     stop
     */
    int expectedGas = 436;
    DataWord key1 = new DataWord(999);
    DataWord value1 = new DataWord(3);
    // Set contract into Database
    String callerAddr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826";
    String contractAddr = "77045e71a7a2c50903d88e564cd72fab11e82051";
    String code = "6103e75460005260006000511115630000004c576001600051036103e755600060006000600060007377045e71a7a2c50903d88e564cd72fab11e820516008600a5a0402f1630000004c00565b00";
    RskAddress contractAddrB = new RskAddress(contractAddr);
    RskAddress callerAddrB = new RskAddress(callerAddr);
    byte[] codeB = Hex.decode(code);
    byte[] codeKey = HashUtil.keccak256(codeB);
    AccountState accountState = new AccountState();
    accountState.setCodeHash(codeKey);
    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractAddrB);
    Repository repository = pi.getRepository();
    repository.createAccount(callerAddrB);
    final BigInteger value = new BigInteger("100000000000000000000");
    repository.addBalance(callerAddrB, new Coin(value));
    repository.createAccount(contractAddrB);
    repository.saveCode(contractAddrB, codeB);
    repository.addStorageRow(contractAddrB, key1, value1);
    // Play the program
    VM vm = getSubject();
    Program program = getProgram(codeB, pi);
    try {
        while (!program.isStopped()) vm.step(program);
    } catch (RuntimeException e) {
        program.setRuntimeFailure(e);
    }
    System.out.println();
    System.out.println("============ Results ============");
    Coin balance = repository.getBalance(callerAddrB);
    System.out.println("*** Used gas: " + program.getResult().getGasUsed());
    System.out.println("*** Contract Balance: " + balance);
    // todo: assert caller balance after contract exec
    repository.close();
    assertEquals(expectedGas, program.getResult().getGasUsed());
}
Also used : Program(org.ethereum.vm.program.Program) AccountState(org.ethereum.core.AccountState) Coin(co.rsk.core.Coin) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with ProgramInvokeMockImpl

use of org.ethereum.vm.program.invoke.ProgramInvokeMockImpl in project rskj by rsksmart.

the class VMCustomTest method setup.

@Before
public void setup() {
    RskAddress ownerAddress = new RskAddress("77045E71A7A2C50903D88E564CD72FAB11E82051");
    byte[] msgData = Hex.decode("00000000000000000000000000000000000000000000000000000000000000A1" + "00000000000000000000000000000000000000000000000000000000000000B1");
    invoke = new ProgramInvokeMockImpl(msgData);
    invoke.setOwnerAddress(ownerAddress);
    invoke.getRepository().createAccount(ownerAddress);
    invoke.getRepository().addBalance(ownerAddress, Coin.valueOf(1000L));
}
Also used : ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) RskAddress(co.rsk.core.RskAddress)

Aggregations

ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)12 RskAddress (co.rsk.core.RskAddress)10 Repository (org.ethereum.core.Repository)9 Program (org.ethereum.vm.program.Program)9 Test (org.junit.Test)9 Coin (co.rsk.core.Coin)8 BigInteger (java.math.BigInteger)8 Ignore (org.junit.Ignore)8 AccountState (org.ethereum.core.AccountState)5 Before (org.junit.Before)2