Search in sources :

Example 76 with Coin

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

the class Program method suicide.

public void suicide(DataWord obtainerAddress) {
    RskAddress owner = new RskAddress(getOwnerAddress());
    Coin balance = getStorage().getBalance(owner);
    if (!balance.equals(Coin.ZERO)) {
        RskAddress obtainer = new RskAddress(obtainerAddress);
        logger.info("Transfer to: [{}] heritage: [{}]", obtainer, balance);
        addInternalTx(null, null, owner, obtainer, balance, null, "suicide");
        if (FastByteComparisons.compareTo(owner.getBytes(), 0, 20, obtainer.getBytes(), 0, 20) == 0) {
            // if owner == obtainer just zeroing account according to Yellow Paper
            getStorage().addBalance(owner, balance.negate());
        } else {
            getStorage().transfer(owner, obtainer, balance);
        }
    }
    // In any case, remove the account
    getResult().addDeleteAccount(this.getOwnerAddress());
}
Also used : Coin(co.rsk.core.Coin) RskAddress(co.rsk.core.RskAddress)

Example 77 with Coin

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

the class ProgramInvokeFactoryImpl method createProgramInvoke.

// Invocation by the wire tx
@Override
public ProgramInvoke createProgramInvoke(Transaction tx, int txindex, Block block, Repository repository, BlockStore blockStore) {
    /**
     *         ADDRESS op       **
     */
    // YP: Get address of currently executing account.
    RskAddress addr = tx.isContractCreation() ? tx.getContractAddress() : tx.getReceiveAddress();
    /**
     *         ORIGIN op       **
     */
    // YP: This is the sender of original transaction; it is never a contract.
    byte[] origin = tx.getSender().getBytes();
    /**
     *         CALLER op       **
     */
    // YP: This is the address of the account that is directly responsible for this execution.
    byte[] caller = tx.getSender().getBytes();
    /**
     *         BALANCE op       **
     */
    Coin balance = repository.getBalance(addr);
    /**
     *         GASPRICE op       **
     */
    Coin gasPrice = tx.getGasPrice();
    /**
     * GAS op **
     */
    byte[] gas = tx.getGasLimit();
    /**
     *        CALLVALUE op      **
     */
    Coin callValue = tx.getValue();
    /**
     *     CALLDATALOAD  op   **
     */
    /**
     *     CALLDATACOPY  op   **
     */
    /**
     *     CALLDATASIZE  op   **
     */
    byte[] data = tx.isContractCreation() ? ByteUtil.EMPTY_BYTE_ARRAY : nullToEmpty(tx.getData());
    /**
     *    PREVHASH  op  **
     */
    byte[] lastHash = block.getParentHash().getBytes();
    /**
     *   COINBASE  op **
     */
    byte[] coinbase = block.getCoinbase().getBytes();
    /**
     * TIMESTAMP  op  **
     */
    long timestamp = block.getTimestamp();
    /**
     * NUMBER  op  **
     */
    long number = block.getNumber();
    /**
     * DIFFICULTY  op  **
     */
    byte[] difficulty = block.getDifficulty().getBytes();
    /**
     * GASLIMIT op **
     */
    byte[] gaslimit = block.getGasLimit();
    if (logger.isInfoEnabled()) {
        logger.info("Top level call: \n" + "address={}\n" + "origin={}\n" + "caller={}\n" + "balance={}\n" + "gasPrice={}\n" + "gas={}\n" + "callValue={}\n" + "data={}\n" + "lastHash={}\n" + "coinbase={}\n" + "timestamp={}\n" + "blockNumber={}\n" + "transactionIndex={}\n" + "difficulty={}\n" + "gaslimit={}\n", addr, Hex.toHexString(origin), Hex.toHexString(caller), balance, gasPrice, new BigInteger(1, gas).longValue(), callValue, Hex.toHexString(data), Hex.toHexString(lastHash), Hex.toHexString(coinbase), timestamp, number, txindex, Hex.toHexString(difficulty), gaslimit);
    }
    return new ProgramInvokeImpl(addr.getBytes(), origin, caller, balance.getBytes(), gasPrice.getBytes(), gas, callValue.getBytes(), data, lastHash, coinbase, timestamp, number, txindex, difficulty, gaslimit, repository, blockStore);
}
Also used : Coin(co.rsk.core.Coin) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Example 78 with Coin

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

the class MinerHelper method processBlock.

public void processBlock(Block block, Block parent) {
    latestStateRootHash = null;
    totalGasUsed = 0;
    totalPaidFees = Coin.ZERO;
    txReceipts = new ArrayList<>();
    // Repository originalRepo  = ((Repository) ethereum.getRepository()).getSnapshotTo(parent.getStateRoot());
    // This creates a snapshot WITHOUT history of the current "parent" reponsitory.
    Repository originalRepo = repository.getSnapshotTo(parent.getStateRoot());
    Repository track = originalRepo.startTracking();
    // Repository track = new RepositoryTrack((Repository)ethereum.getRepository());
    // this variable is set before iterating transactions in case list is empty
    latestStateRootHash = originalRepo.getRoot();
    // RSK test, remove
    String stateHash1 = Hex.toHexString(blockchain.getBestBlock().getStateRoot());
    String stateHash2 = Hex.toHexString(repository.getRoot());
    if (stateHash1.compareTo(stateHash2) != 0) {
        logger.error("Strange state in block {} {}", block.getNumber(), block.getHash());
        panicProcessor.panic("minerserver", String.format("Strange state in block %d %s", block.getNumber(), block.getHash()));
    }
    int txindex = 0;
    for (Transaction tx : block.getTransactionsList()) {
        TransactionExecutor executor = new TransactionExecutor(config, tx, txindex++, block.getCoinbase(), track, null, null, null, block, new EthereumListenerAdapter(), totalGasUsed);
        executor.init();
        executor.execute();
        executor.go();
        executor.finalization();
        long gasUsed = executor.getGasUsed();
        Coin paidFees = executor.getPaidFees();
        totalGasUsed += gasUsed;
        totalPaidFees = totalPaidFees.add(paidFees);
        track.commit();
        TransactionReceipt receipt = new TransactionReceipt();
        receipt.setGasUsed(gasUsed);
        receipt.setCumulativeGas(totalGasUsed);
        latestStateRootHash = originalRepo.getRoot();
        receipt.setPostTxState(latestStateRootHash);
        receipt.setTxStatus(executor.getReceipt().isSuccessful());
        receipt.setStatus(executor.getReceipt().getStatus());
        receipt.setTransaction(tx);
        receipt.setLogInfoList(executor.getVMLogs());
        txReceipts.add(receipt);
    }
}
Also used : Coin(co.rsk.core.Coin) EthereumListenerAdapter(org.ethereum.listener.EthereumListenerAdapter)

Example 79 with Coin

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

the class AccountStateTest method encodeDecodeStateWithZeroInStateFlags.

@Test
public void encodeDecodeStateWithZeroInStateFlags() {
    AccountState acct = new AccountState(BigInteger.ZERO, new Coin(BigInteger.valueOf(2).pow(200)));
    AccountState result = new AccountState(acct.getEncoded());
    assertEquals(BigInteger.ZERO, result.getNonce());
    assertEquals(BigInteger.valueOf(2).pow(200), result.getBalance().asBigInteger());
    assertEquals(0, result.getStateFlags());
}
Also used : Coin(co.rsk.core.Coin) Test(org.junit.Test)

Example 80 with Coin

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

the class BlockTest method testPremineFromJSON.

@Test
public void testPremineFromJSON() throws ParseException {
    JSONParser parser = new JSONParser();
    JSONObject genesisMap = (JSONObject) parser.parse(TEST_GENESIS);
    Set keys = genesisMap.keySet();
    Trie state = new TrieImpl(null, true);
    for (Object key : keys) {
        JSONObject val = (JSONObject) genesisMap.get(key);
        String denom = (String) val.keySet().toArray()[0];
        String value = (String) val.values().toArray()[0];
        BigInteger wei = Denomination.valueOf(denom.toUpperCase()).value().multiply(new BigInteger(value));
        AccountState accountState = new AccountState(BigInteger.ZERO, new Coin(wei));
        byte[] encodedAccountState = accountState.getEncoded();
        byte[] accountKey = Hex.decode(key.toString());
        state = state.put(accountKey, encodedAccountState);
        Assert.assertArrayEquals(encodedAccountState, state.get(accountKey));
    }
    logger.info("root: {}", state.getHash());
    assertEquals(GENESIS_STATE_ROOT, state.getHash());
}
Also used : Coin(co.rsk.core.Coin) TrieImpl(co.rsk.trie.TrieImpl) Set(java.util.Set) JSONObject(org.json.simple.JSONObject) BigInteger(java.math.BigInteger) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

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