Search in sources :

Example 6 with ProgramInvokeMockImpl

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

the class VMComplexTest method test2.

// TODO #POC9
@Ignore
// contractB call contractA with data to storage
@Test
public void test2() {
    /**
     *       #The code will run
     *       ------------------
     *
     *                 contract A: 77045e71a7a2c50903d88e564cd72fab11e82051
     *                 ---------------
     *                     a = msg.data[0]
     *                     b = msg.data[1]
     *
     *                     contract.storage[a]
     *                     contract.storage[b]
     *
     *                 contract B: 83c5541a6c8d2dbad642f385d8d06ca9b6c731ee
     *                 -----------
     *                     a = msg((tx.gas / 10 * 8), 0x77045e71a7a2c50903d88e564cd72fab11e82051, 0, [11, 22, 33], 3, 6)
     */
    long expectedVal_1 = 11;
    long expectedVal_2 = 22;
    // Set contract into Database
    String caller_addr_str = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826";
    String contractA_addr_str = "77045e71a7a2c50903d88e564cd72fab11e82051";
    String contractB_addr_str = "83c5541a6c8d2dbad642f385d8d06ca9b6c731ee";
    String code_a = "60006020023560005260016020023560205260005160005560205160015500";
    String code_b = "6000601f5360e05960e05952600060c05901536060596020015980602001600b9052806040016016905280606001602190526080905260007377045e71a7a2c50903d88e564cd72fab11e820516103e8f1602060000260a00160200151600052";
    RskAddress caller_addr = new RskAddress(caller_addr_str);
    RskAddress contractA_addr = new RskAddress(contractA_addr_str);
    byte[] codeA = Hex.decode(code_a);
    RskAddress contractB_addr = new RskAddress(contractB_addr_str);
    byte[] codeB = Hex.decode(code_b);
    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractB_addr);
    Repository repository = pi.getRepository();
    repository.createAccount(contractA_addr);
    repository.saveCode(contractA_addr, codeA);
    repository.createAccount(contractB_addr);
    repository.saveCode(contractB_addr, codeB);
    repository.createAccount(caller_addr);
    final BigInteger value = new BigInteger("100000000000000000000");
    repository.addBalance(caller_addr, new Coin(value));
    // ****************** //
    // 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 ============");
    System.out.println("*** Used gas: " + program.getResult().getGasUsed());
    DataWord value_1 = repository.getStorageValue(contractA_addr, new DataWord(00));
    DataWord value_2 = repository.getStorageValue(contractA_addr, new DataWord(01));
    repository.close();
    assertEquals(expectedVal_1, value_1.longValue());
    assertEquals(expectedVal_2, value_2.longValue());
// TODO: check that the value pushed after exec is 1
}
Also used : Coin(co.rsk.core.Coin) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Repository(org.ethereum.core.Repository) Program(org.ethereum.vm.program.Program) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with ProgramInvokeMockImpl

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

the class VMComplexTest method test3.

@Ignore
// contractB call contractA with return expectation
@Test
public void test3() {
    /**
     *       #The code will run
     *       ------------------
     *
     *         contract A: 77045e71a7a2c50903d88e564cd72fab11e82051
     *         ---------------
     *
     *           a = 11
     *           b = 22
     *           c = 33
     *           d = 44
     *           e = 55
     *           f = 66
     *
     *           [asm  192 0 RETURN asm]
     *
     *         contract B: 83c5541a6c8d2dbad642f385d8d06ca9b6c731ee
     *         -----------
     *             a = msg((tx.gas / 10 * 8), 0x77045e71a7a2c50903d88e564cd72fab11e82051, 0, [11, 22, 33], 3, 6)
     */
    long expectedVal_1 = 11;
    long expectedVal_2 = 22;
    long expectedVal_3 = 33;
    long expectedVal_4 = 44;
    long expectedVal_5 = 55;
    long expectedVal_6 = 66;
    // Set contract into Database
    RskAddress caller_addr = new RskAddress("cd2a3d9f938e13cd947ec05abc7fe734df8dd826");
    RskAddress contractA_addr = new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051");
    RskAddress contractB_addr = new RskAddress("83c5541a6c8d2dbad642f385d8d06ca9b6c731ee");
    byte[] codeA = Hex.decode("600b60005260166020526021604052602c6060526037608052604260a05260c06000f2");
    byte[] codeB = Hex.decode("6000601f5360e05960e05952600060c05901536060596020015980602001600b9052806040016016905280606001602190526080905260007377045e71a7a2c50903d88e564cd72fab11e820516103e8f1602060000260a00160200151600052");
    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractB_addr);
    Repository repository = pi.getRepository();
    repository.createAccount(contractA_addr);
    repository.saveCode(contractA_addr, codeA);
    repository.createAccount(contractB_addr);
    repository.saveCode(contractB_addr, codeB);
    repository.createAccount(caller_addr);
    final BigInteger value = new BigInteger("100000000000000000000");
    repository.addBalance(caller_addr, new Coin(value));
    // ****************** //
    // 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 ============");
    System.out.println("*** Used gas: " + program.getResult().getGasUsed());
    DataWord value1 = program.memoryLoad(new DataWord(32));
    DataWord value2 = program.memoryLoad(new DataWord(64));
    DataWord value3 = program.memoryLoad(new DataWord(96));
    DataWord value4 = program.memoryLoad(new DataWord(128));
    DataWord value5 = program.memoryLoad(new DataWord(160));
    DataWord value6 = program.memoryLoad(new DataWord(192));
    repository.close();
    assertEquals(expectedVal_1, value1.longValue());
    assertEquals(expectedVal_2, value2.longValue());
    assertEquals(expectedVal_3, value3.longValue());
    assertEquals(expectedVal_4, value4.longValue());
    assertEquals(expectedVal_5, value5.longValue());
    assertEquals(expectedVal_6, value6.longValue());
// TODO: check that the value pushed after exec is 1
}
Also used : Coin(co.rsk.core.Coin) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Repository(org.ethereum.core.Repository) Program(org.ethereum.vm.program.Program) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with ProgramInvokeMockImpl

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

the class VMExecutionTest method setup.

@Before
public void setup() {
    invoke = new ProgramInvokeMockImpl();
    compiler = new BytecodeCompiler();
}
Also used : ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Before(org.junit.Before)

Example 9 with ProgramInvokeMockImpl

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

the class VMComplexTest method test6.

@Ignore
// contractB call itself with code from contractA
@Test
public void test6() {
    /**
     *       #The code will run
     *       ------------------
     *
     *         contract A: 945304eb96065b2a98b57a48a06ae28d285a71b5
     *         ---------------
     *
     *         PUSH1 0 CALLDATALOAD SLOAD NOT PUSH1 9 JUMPI STOP
     *         PUSH1 32 CALLDATALOAD PUSH1 0 CALLDATALOAD SSTORE
     *
     *         contract B: 0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6
     *         -----------
     *             { (MSTORE 0 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
     *               (MSTORE 32 0xaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa)
     *               [[ 0 ]] (CALLSTATELESS 1000000 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 64 64 0)
     *             }
     */
    // Set contract into Database
    RskAddress caller_addr = new RskAddress("cd1722f3947def4cf144679da39c4c32bdc35681");
    RskAddress contractA_addr = new RskAddress("945304eb96065b2a98b57a48a06ae28d285a71b5");
    RskAddress contractB_addr = new RskAddress("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6");
    byte[] codeA = Hex.decode("60003554156009570060203560003555");
    byte[] codeB = Hex.decode("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6000527faaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa6020526000604060406000601773945304eb96065b2a98b57a48a06ae28d285a71b5620f4240f3600055");
    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractB_addr);
    pi.setGasLimit(10000000000000l);
    Repository repository = pi.getRepository();
    repository.createAccount(contractA_addr);
    repository.saveCode(contractA_addr, codeA);
    repository.addBalance(contractA_addr, Coin.valueOf(23));
    repository.createAccount(contractB_addr);
    repository.saveCode(contractB_addr, codeB);
    final BigInteger value = new BigInteger("1000000000000000000");
    repository.addBalance(contractB_addr, new Coin(value));
    repository.createAccount(caller_addr);
    final BigInteger value1 = new BigInteger("100000000000000000000");
    repository.addBalance(caller_addr, new Coin(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 ============");
    System.out.println("*** Used gas: " + program.getResult().getGasUsed());
    DataWord memValue1 = program.memoryLoad(new DataWord(0));
    DataWord memValue2 = program.memoryLoad(new DataWord(32));
    DataWord storeValue1 = repository.getStorageValue(contractB_addr, new DataWord(00));
    repository.close();
    assertEquals("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", memValue1.toString());
    assertEquals("aaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa", memValue2.toString());
    assertEquals("0x1", storeValue1.shortHex());
// TODO: check that the value pushed after exec is 1
}
Also used : Coin(co.rsk.core.Coin) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Repository(org.ethereum.core.Repository) Program(org.ethereum.vm.program.Program) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with ProgramInvokeMockImpl

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

the class VMComplexTest method test4.

// CREATE magic
@Test
public void test4() {
    /**
     *       #The code will run
     *       ------------------
     *
     *         contract A: 77045e71a7a2c50903d88e564cd72fab11e82051
     *         -----------
     *
     *             a = 0x7f60c860005461012c6020540000000000000000000000000000000000000000
     *             b = 0x0060005460206000f20000000000000000000000000000000000000000000000
     *             create(100, 0 41)
     *
     *         contract B: (the contract to be created the addr will be defined to: 8e45367623a2865132d9bf875d5cfa31b9a0cd94)
     *         -----------
     *             a = 200
     *             b = 300
     */
    // Set contract into Database
    RskAddress caller_addr = new RskAddress("cd2a3d9f938e13cd947ec05abc7fe734df8dd826");
    RskAddress contractA_addr = new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051");
    byte[] codeA = Hex.decode("7f7f60c860005461012c602054000000000000" + "00000000000000000000000000006000547e60" + "005460206000f2000000000000000000000000" + "0000000000000000000000602054602960006064f0");
    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractA_addr);
    Repository repository = pi.getRepository();
    repository.createAccount(contractA_addr);
    repository.saveCode(contractA_addr, codeA);
    repository.createAccount(caller_addr);
    // ****************** //
    // Play the program  //
    // ****************** //
    VM vm = getSubject();
    Program program = getProgram(codeA, pi);
    try {
        while (!program.isStopped()) vm.step(program);
    } catch (RuntimeException e) {
        program.setRuntimeFailure(e);
    }
    logger.info("============ Results ============");
    System.out.println("*** Used gas: " + program.getResult().getGasUsed());
    // TODO: check that the value pushed after exec is the new address
    repository.close();
}
Also used : ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Repository(org.ethereum.core.Repository) Program(org.ethereum.vm.program.Program) RskAddress(co.rsk.core.RskAddress) Test(org.junit.Test)

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