Search in sources :

Example 91 with Coin

use of co.rsk.core.Coin 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 92 with Coin

use of co.rsk.core.Coin 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 93 with Coin

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

the class SelectionRule method isBrokenSelectionRule.

public static boolean isBrokenSelectionRule(BlockHeader processingBlockHeader, List<Sibling> siblings) {
    int maxUncleCount = 0;
    for (Sibling sibling : siblings) {
        maxUncleCount = Math.max(maxUncleCount, sibling.getUncleCount());
        Coin pfm = processingBlockHeader.getPaidFees().multiply(PAID_FEES_MULTIPLIER_CRITERIA);
        if (sibling.getPaidFees().compareTo(pfm) > 0) {
            return true;
        }
        Coin blockFeesCriteria = sibling.getPaidFees().multiply(PAID_FEES_MULTIPLIER_CRITERIA);
        if (processingBlockHeader.getPaidFees().compareTo(blockFeesCriteria) < 0 && isThisBlockHashSmaller(sibling.getHash(), processingBlockHeader.getHash().getBytes())) {
            return true;
        }
    }
    return maxUncleCount > processingBlockHeader.getUncleCount();
}
Also used : Coin(co.rsk.core.Coin) Sibling(co.rsk.remasc.Sibling)

Example 94 with Coin

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

the class RepositoryImpl method addBalance.

@Override
public synchronized Coin addBalance(RskAddress addr, Coin value) {
    AccountState account = getAccountStateOrCreateNew(addr);
    Coin result = account.addToBalance(value);
    updateAccountState(addr, account);
    return result;
}
Also used : Coin(co.rsk.core.Coin) AccountState(org.ethereum.core.AccountState)

Example 95 with Coin

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

the class MinerUtils method filterTransactions.

public List<org.ethereum.core.Transaction> filterTransactions(List<Transaction> txsToRemove, List<Transaction> txs, Map<RskAddress, BigInteger> accountNonces, Repository originalRepo, Coin minGasPrice) {
    List<org.ethereum.core.Transaction> txsResult = new ArrayList<>();
    for (org.ethereum.core.Transaction tx : txs) {
        try {
            Keccak256 hash = tx.getHash();
            Coin txValue = tx.getValue();
            BigInteger txNonce = new BigInteger(1, tx.getNonce());
            RskAddress txSender = tx.getSender();
            logger.debug("Examining tx={} sender: {} value: {} nonce: {}", hash, txSender, txValue, txNonce);
            BigInteger expectedNonce;
            if (accountNonces.containsKey(txSender)) {
                expectedNonce = accountNonces.get(txSender).add(BigInteger.ONE);
            } else {
                expectedNonce = originalRepo.getNonce(txSender);
            }
            if (!(tx instanceof RemascTransaction) && tx.getGasPrice().compareTo(minGasPrice) < 0) {
                logger.warn("Rejected tx={} because of low gas account {}, removing tx from pending state.", hash, txSender);
                txsToRemove.add(tx);
                continue;
            }
            if (!expectedNonce.equals(txNonce)) {
                logger.warn("Invalid nonce, expected {}, found {}, tx={}", expectedNonce, txNonce, hash);
                continue;
            }
            accountNonces.put(txSender, txNonce);
            logger.debug("Accepted tx={} sender: {} value: {} nonce: {}", hash, txSender, txValue, txNonce);
        } catch (Exception e) {
            // Txs that can't be selected by any reason should be removed from pending state
            logger.warn(String.format("Error when processing tx=%s", tx.getHash()), e);
            if (txsToRemove != null) {
                txsToRemove.add(tx);
            } else {
                logger.error("Can't remove invalid txs from pending state.");
            }
            continue;
        }
        txsResult.add(tx);
    }
    logger.debug("Ending getTransactions {}", txsResult.size());
    return txsResult;
}
Also used : RemascTransaction(co.rsk.remasc.RemascTransaction) Keccak256(co.rsk.crypto.Keccak256) IOException(java.io.IOException) Coin(co.rsk.core.Coin) RemascTransaction(co.rsk.remasc.RemascTransaction) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Transaction(org.ethereum.core.Transaction)

Aggregations

Coin (co.rsk.core.Coin)95 Test (org.junit.Test)46 RskAddress (co.rsk.core.RskAddress)37 BigInteger (java.math.BigInteger)32 Repository (org.ethereum.core.Repository)23 Transaction (org.ethereum.core.Transaction)23 Program (org.ethereum.vm.program.Program)12 AccountState (org.ethereum.core.AccountState)10 ArrayList (java.util.ArrayList)9 Ignore (org.junit.Ignore)9 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)8 Account (org.ethereum.core.Account)7 BlockExecutor (co.rsk.core.bc.BlockExecutor)6 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)6 Keccak256 (co.rsk.crypto.Keccak256)5 Block (org.ethereum.core.Block)5 DataWord (org.ethereum.vm.DataWord)5 ProgramInvoke (org.ethereum.vm.program.invoke.ProgramInvoke)5 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)4 RskSystemProperties (co.rsk.config.RskSystemProperties)4