Search in sources :

Example 6 with AccountState

use of org.ethereum.core.AccountState in project rskj by rsksmart.

the class TxValidatorAccountBalanceValidatorTest method validAccountBalance.

@Test
public void validAccountBalance() {
    Transaction tx1 = Mockito.mock(Transaction.class);
    Transaction tx2 = Mockito.mock(Transaction.class);
    Transaction tx3 = Mockito.mock(Transaction.class);
    AccountState as = Mockito.mock(AccountState.class);
    Mockito.when(tx1.getGasLimitAsInteger()).thenReturn(BigInteger.valueOf(1));
    Mockito.when(tx2.getGasLimitAsInteger()).thenReturn(BigInteger.valueOf(1));
    Mockito.when(tx3.getGasLimitAsInteger()).thenReturn(BigInteger.valueOf(2));
    Mockito.when(tx1.getGasPrice()).thenReturn(Coin.valueOf(1));
    Mockito.when(tx2.getGasPrice()).thenReturn(Coin.valueOf(10000));
    Mockito.when(tx3.getGasPrice()).thenReturn(Coin.valueOf(5000));
    Mockito.when(as.getBalance()).thenReturn(Coin.valueOf(10000));
    TxValidatorAccountBalanceValidator tvabv = new TxValidatorAccountBalanceValidator();
    Assert.assertTrue(tvabv.validate(tx1, as, null, null, Long.MAX_VALUE, false));
    Assert.assertTrue(tvabv.validate(tx2, as, null, null, Long.MAX_VALUE, false));
    Assert.assertTrue(tvabv.validate(tx3, as, null, null, Long.MAX_VALUE, false));
}
Also used : Transaction(org.ethereum.core.Transaction) AccountState(org.ethereum.core.AccountState) Test(org.junit.Test)

Example 7 with AccountState

use of org.ethereum.core.AccountState in project rskj by rsksmart.

the class TxValidatorAccountStateValidatorTest method validAccountState.

@Test
public void validAccountState() {
    AccountState state = Mockito.mock(AccountState.class);
    Mockito.when(state.isDeleted()).thenReturn(false);
    TxValidatorAccountStateValidator tvasv = new TxValidatorAccountStateValidator();
    Assert.assertTrue(tvasv.validate(null, state, null, null, Long.MAX_VALUE, false));
}
Also used : AccountState(org.ethereum.core.AccountState) Test(org.junit.Test)

Example 8 with AccountState

use of org.ethereum.core.AccountState in project rskj by rsksmart.

the class TxValidatorAccountStateValidatorTest method invalidAccountState.

@Test
public void invalidAccountState() {
    AccountState state = Mockito.mock(AccountState.class);
    Mockito.when(state.isDeleted()).thenReturn(true);
    TxValidatorAccountStateValidator tvasv = new TxValidatorAccountStateValidator();
    Assert.assertFalse(tvasv.validate(null, state, null, null, Long.MAX_VALUE, false));
}
Also used : AccountState(org.ethereum.core.AccountState) Test(org.junit.Test)

Example 9 with AccountState

use of org.ethereum.core.AccountState in project rskj by rsksmart.

the class TxValidatorIntrinsicGasLimitValidatorTest method invalidIntrinsicGasPrice.

@Test
public void invalidIntrinsicGasPrice() {
    Transaction tx1 = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), BigInteger.valueOf(21071).toByteArray(), new ECKey().getAddress(), BigInteger.ZERO.toByteArray(), Hex.decode("0001"), config.getBlockchainConfig().getCommonConstants().getChainId());
    tx1.sign(new ECKey().getPrivKeyBytes());
    Transaction tx2 = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), BigInteger.valueOf(20999).toByteArray(), new ECKey().getAddress(), BigInteger.ZERO.toByteArray(), null, config.getBlockchainConfig().getCommonConstants().getChainId());
    tx2.sign(new ECKey().getPrivKeyBytes());
    Transaction tx3 = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), new ECKey().getAddress(), BigInteger.ZERO.toByteArray(), Hex.decode("0001"), config.getBlockchainConfig().getCommonConstants().getChainId());
    tx3.sign(new ECKey().getPrivKeyBytes());
    Transaction tx4 = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), new ECKey().getAddress(), BigInteger.ZERO.toByteArray(), null, config.getBlockchainConfig().getCommonConstants().getChainId());
    tx4.sign(new ECKey().getPrivKeyBytes());
    TxValidatorIntrinsicGasLimitValidator tvigpv = new TxValidatorIntrinsicGasLimitValidator(config);
    Assert.assertFalse(tvigpv.validate(tx1, new AccountState(), null, null, Long.MAX_VALUE, false));
    Assert.assertFalse(tvigpv.validate(tx2, new AccountState(), null, null, Long.MAX_VALUE, false));
    Assert.assertFalse(tvigpv.validate(tx3, new AccountState(), null, null, Long.MAX_VALUE, false));
    Assert.assertFalse(tvigpv.validate(tx4, new AccountState(), null, null, Long.MAX_VALUE, false));
}
Also used : Transaction(org.ethereum.core.Transaction) ECKey(org.ethereum.crypto.ECKey) AccountState(org.ethereum.core.AccountState) Test(org.junit.Test)

Example 10 with AccountState

use of org.ethereum.core.AccountState in project rskj by rsksmart.

the class VMComplexTest method test8.

// sha3_memSizeQuadraticCost31
// TODO #POC9
@Ignore
// contract call quadratic memory use
@Test
public void test8() {
    int expectedGas = 354;
    DataWord key1 = new DataWord(999);
    DataWord value1 = new DataWord(3);
    // Set contract into Database
    String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681";
    String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
    String code = "60016103c020600055";
    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)

Aggregations

AccountState (org.ethereum.core.AccountState)46 Test (org.junit.Test)20 RskAddress (co.rsk.core.RskAddress)12 BigInteger (java.math.BigInteger)11 Coin (co.rsk.core.Coin)10 Transaction (org.ethereum.core.Transaction)10 Repository (org.ethereum.core.Repository)7 ContractDetails (org.ethereum.db.ContractDetails)7 Program (org.ethereum.vm.program.Program)5 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)5 Ignore (org.junit.Ignore)5 RskSystemProperties (co.rsk.config.RskSystemProperties)3 TrieImplHashTest (co.rsk.trie.TrieImplHashTest)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ECKey (org.ethereum.crypto.ECKey)3 RepositoryImpl (co.rsk.db.RepositoryImpl)2 TxsPerAccount (co.rsk.net.handler.TxsPerAccount)2 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)2 HashMapDB (org.ethereum.datasource.HashMapDB)2