Search in sources :

Example 56 with RskAddress

use of co.rsk.core.RskAddress 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 57 with RskAddress

use of co.rsk.core.RskAddress 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 58 with RskAddress

use of co.rsk.core.RskAddress 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 59 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class VMTest method testJUMPDEST_2.

// JUMPDEST OP for JUMPI
@Test
public void testJUMPDEST_2() {
    VM vm = getSubject();
    program = getProgram("6023600160095760015b600255");
    String s_expected_key = "0000000000000000000000000000000000000000000000000000000000000002";
    String s_expected_val = "0000000000000000000000000000000000000000000000000000000000000023";
    vm.step(program);
    vm.step(program);
    vm.step(program);
    vm.step(program);
    vm.step(program);
    vm.step(program);
    vm.step(program);
    DataWord key = new DataWord(Hex.decode(s_expected_key));
    DataWord val = program.getStorage().getStorageValue(new RskAddress(invoke.getOwnerAddress()), key);
    assertTrue(program.isStopped());
    assertEquals(s_expected_val, Hex.toHexString(val.getData()).toUpperCase());
}
Also used : RskAddress(co.rsk.core.RskAddress) ByteUtil.oneByteToHexString(org.ethereum.util.ByteUtil.oneByteToHexString)

Example 60 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class VMTest method testSSTORE_2.

// SSTORE OP
@Test
public void testSSTORE_2() {
    VM vm = getSubject();
    program = getProgram("602260AA55602260BB55");
    String s_expected_key = "00000000000000000000000000000000000000000000000000000000000000BB";
    String s_expected_val = "0000000000000000000000000000000000000000000000000000000000000022";
    vm.step(program);
    vm.step(program);
    vm.step(program);
    vm.step(program);
    vm.step(program);
    vm.step(program);
    Repository repository = program.getStorage();
    DataWord key = new DataWord(Hex.decode(s_expected_key));
    DataWord val = repository.getStorageValue(new RskAddress(invoke.getOwnerAddress()), key);
    assertEquals(s_expected_val, Hex.toHexString(val.getData()).toUpperCase());
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) ByteUtil.oneByteToHexString(org.ethereum.util.ByteUtil.oneByteToHexString)

Aggregations

RskAddress (co.rsk.core.RskAddress)174 Test (org.junit.Test)102 Repository (org.ethereum.core.Repository)60 BigInteger (java.math.BigInteger)47 Coin (co.rsk.core.Coin)38 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 DataWord (org.ethereum.vm.DataWord)27 TrieImplHashTest (co.rsk.trie.TrieImplHashTest)24 RepositoryImpl (co.rsk.db.RepositoryImpl)16 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)15 Transaction (org.ethereum.core.Transaction)15 Program (org.ethereum.vm.program.Program)15 InvocationOnMock (org.mockito.invocation.InvocationOnMock)14 AccountState (org.ethereum.core.AccountState)12 HashMapDB (org.ethereum.datasource.HashMapDB)11 ArrayList (java.util.ArrayList)10 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)10 BridgeConstants (co.rsk.config.BridgeConstants)8 RskSystemProperties (co.rsk.config.RskSystemProperties)8 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)8