Search in sources :

Example 11 with Program

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

the class VMComplexTest method test9.

// sha3_memSizeQuadraticCost32
// TODO #POC9
@Ignore
// contract call quadratic memory use
@Test
public void test9() {
    int expectedGas = 356;
    DataWord key1 = new DataWord(9999);
    DataWord value1 = new DataWord(3);
    // Set contract into Database
    String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681";
    String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
    String code = "60016103e020600055";
    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("115792089237316195423570985008687907853269984665640564039457584007913129639935");
    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 Program

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

the class VMComplexTest method test7.

// sha3_memSizeQuadraticCost33
// TODO #POC9
@Ignore
// contract call quadratic memory use
@Test
public void test7() {
    int expectedGas = 357;
    DataWord key1 = new DataWord(999);
    DataWord value1 = new DataWord(3);
    // Set contract into Database
    String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681";
    String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
    String code = "600161040020600055";
    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("115792089237316195423570985008687907853269984665640564039457584007913129639935");
    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 13 with Program

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

the class VMComplexTest method test10.

// sha3_memSizeQuadraticCost32_zeroSize
// TODO #POC9
@Ignore
// contract call quadratic memory use
@Test
public void test10() {
    int expectedGas = 313;
    DataWord key1 = new DataWord(999);
    DataWord value1 = new DataWord(3);
    // Set contract into Database
    String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681";
    String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
    String code = "600061040020600055";
    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("115792089237316195423570985008687907853269984665640564039457584007913129639935");
    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 14 with Program

use of org.ethereum.vm.program.Program 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 15 with Program

use of org.ethereum.vm.program.Program 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)

Aggregations

Program (org.ethereum.vm.program.Program)37 Test (org.junit.Test)27 RskAddress (co.rsk.core.RskAddress)14 DataWord (org.ethereum.vm.DataWord)13 Stack (org.ethereum.vm.program.Stack)13 Coin (co.rsk.core.Coin)12 BigInteger (java.math.BigInteger)12 Repository (org.ethereum.core.Repository)10 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)9 Ignore (org.junit.Ignore)9 BlockchainConfig (org.ethereum.config.BlockchainConfig)7 AccountState (org.ethereum.core.AccountState)5 ProgramInvoke (org.ethereum.vm.program.invoke.ProgramInvoke)4 ContractDetails (org.ethereum.db.ContractDetails)3 VM (org.ethereum.vm.VM)3 RskSystemProperties (co.rsk.config.RskSystemProperties)2 VmConfig (co.rsk.config.VmConfig)2 IOException (java.io.IOException)2 List (java.util.List)2 EMPTY_BYTE_ARRAY (org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY)2