Search in sources :

Example 71 with Coin

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

the class TransactionPoolImplTest method executeContractWithFakeBlock.

@Test
public void executeContractWithFakeBlock() {
    BlockChainImpl blockchain = createBlockchain();
    Coin balance = Coin.valueOf(1000000);
    TransactionPoolImpl transactionPool = createSampleNewTransactionPoolWithAccounts(2, balance, blockchain);
    transactionPool.processBest(blockchain.getBestBlock());
    // "NUMBER PUSH1 0x00 SSTORE" compiled to bytecodes
    String code = "43600055";
    Transaction tx = createSampleTransactionWithData(1, 0, code);
    transactionPool.addTransaction(tx);
    Assert.assertNotNull(tx.getContractAddress().getBytes());
    // Stored value at 0 position should be 1, one more than the blockchain best block
    Assert.assertEquals(DataWord.ONE, transactionPool.getRepository().getStorageValue(tx.getContractAddress(), DataWord.ZERO));
}
Also used : Coin(co.rsk.core.Coin) Transaction(org.ethereum.core.Transaction) Test(org.junit.Test)

Example 72 with Coin

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

the class BlockGenerator method createBlock.

public Block createBlock(int number, int ntxs) {
    Bloom logBloom = new Bloom();
    Block parent = getGenesisBlock();
    List<Transaction> txs = new ArrayList<>();
    for (int ntx = 0; ntx < ntxs; ntx++) {
        txs.add(new SimpleRskTransaction(null));
    }
    Coin previousMGP = parent.getMinimumGasPrice() != null ? parent.getMinimumGasPrice() : Coin.valueOf(10L);
    Coin minimumGasPrice = new MinimumGasPriceCalculator().calculate(previousMGP, Coin.valueOf(100L));
    return new Block(// parent hash
    parent.getHash().getBytes(), // uncle hash
    EMPTY_LIST_HASH, // coinbase
    parent.getCoinbase().getBytes(), // logs bloom
    logBloom.getData(), // difficulty
    parent.getDifficulty().getBytes(), number, parent.getGasLimit(), parent.getGasUsed(), parent.getTimestamp() + ++count, // extraData
    EMPTY_BYTE_ARRAY, // mixHash
    EMPTY_BYTE_ARRAY, // provisory nonce
    BigInteger.ZERO.toByteArray(), // receipts root
    EMPTY_TRIE_HASH, // transaction receipts
    EMPTY_TRIE_HASH, // state root
    EMPTY_TRIE_HASH, // transaction list
    txs, // uncle list
    null, minimumGasPrice.getBytes(), Coin.ZERO);
}
Also used : Coin(co.rsk.core.Coin) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) MinimumGasPriceCalculator(co.rsk.mine.MinimumGasPriceCalculator) ArrayList(java.util.ArrayList) SimpleBlock(co.rsk.peg.simples.SimpleBlock)

Example 73 with Coin

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

the class Web3Impl method getBlockResult.

public BlockResult getBlockResult(Block b, boolean fullTx) {
    if (b == null) {
        return null;
    }
    byte[] mergeHeader = b.getBitcoinMergedMiningHeader();
    boolean isPending = (mergeHeader == null || mergeHeader.length == 0) && !b.isGenesis();
    BlockResult br = new BlockResult();
    br.number = isPending ? null : TypeConverter.toJsonHex(b.getNumber());
    br.hash = isPending ? null : b.getHashJsonString();
    br.parentHash = b.getParentHashJsonString();
    br.sha3Uncles = TypeConverter.toJsonHex(b.getUnclesHash());
    br.logsBloom = isPending ? null : TypeConverter.toJsonHex(b.getLogBloom());
    br.transactionsRoot = TypeConverter.toJsonHex(b.getTxTrieRoot());
    br.stateRoot = TypeConverter.toJsonHex(b.getStateRoot());
    br.receiptsRoot = TypeConverter.toJsonHex(b.getReceiptsRoot());
    br.miner = isPending ? null : TypeConverter.toJsonHex(b.getCoinbase().getBytes());
    br.difficulty = TypeConverter.toJsonHex(b.getDifficulty().getBytes());
    br.totalDifficulty = TypeConverter.toJsonHex(this.blockchain.getBlockStore().getTotalDifficultyForHash(b.getHash().getBytes()).asBigInteger());
    br.extraData = TypeConverter.toJsonHex(b.getExtraData());
    br.size = TypeConverter.toJsonHex(b.getEncoded().length);
    br.gasLimit = TypeConverter.toJsonHex(b.getGasLimit());
    Coin mgp = b.getMinimumGasPrice();
    br.minimumGasPrice = mgp != null ? mgp.asBigInteger().toString() : "";
    br.gasUsed = TypeConverter.toJsonHex(b.getGasUsed());
    br.timestamp = TypeConverter.toJsonHex(b.getTimestamp());
    List<Object> txes = new ArrayList<>();
    if (fullTx) {
        for (int i = 0; i < b.getTransactionsList().size(); i++) {
            txes.add(new TransactionResultDTO(b, i, b.getTransactionsList().get(i)));
        }
    } else {
        for (Transaction tx : b.getTransactionsList()) {
            txes.add(tx.getHash().toJsonString());
        }
    }
    br.transactions = txes.toArray();
    List<String> ul = new ArrayList<>();
    for (BlockHeader header : b.getUncleList()) {
        ul.add(toJsonHex(header.getHash().getBytes()));
    }
    br.uncles = ul.toArray(new String[ul.size()]);
    return br;
}
Also used : Coin(co.rsk.core.Coin) TransactionResultDTO(org.ethereum.rpc.dto.TransactionResultDTO)

Example 74 with Coin

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

the class Program method callToPrecompiledAddress.

public void callToPrecompiledAddress(MessageCall msg, PrecompiledContract contract) {
    if (getCallDeep() == MAX_DEPTH) {
        stackPushZero();
        this.refundGas(msg.getGas().longValue(), " call deep limit reach");
        return;
    }
    Repository track = getStorage().startTracking();
    RskAddress senderAddress = new RskAddress(getOwnerAddress());
    RskAddress codeAddress = new RskAddress(msg.getCodeAddress());
    RskAddress contextAddress = msg.getType().isStateless() ? senderAddress : codeAddress;
    Coin endowment = new Coin(msg.getEndowment().getData());
    Coin senderBalance = track.getBalance(senderAddress);
    if (senderBalance.compareTo(endowment) < 0) {
        stackPushZero();
        this.refundGas(msg.getGas().longValue(), "refund gas from message call");
        return;
    }
    byte[] data = this.memoryChunk(msg.getInDataOffs().intValue(), msg.getInDataSize().intValue());
    // Charge for endowment - is not reversible by rollback
    track.transfer(senderAddress, contextAddress, new Coin(msg.getEndowment().getData()));
    if (byTestingSuite()) {
        // This keeps track of the calls created for a test
        this.getResult().addCallCreate(data, codeAddress.getBytes(), msg.getGas().longValueSafe(), msg.getEndowment().getNoLeadZeroesData());
        stackPushOne();
        return;
    }
    long requiredGas = contract.getGasForData(data);
    if (requiredGas > msg.getGas().longValue()) {
        // matches cpp logic
        this.refundGas(0, "call pre-compiled");
        this.stackPushZero();
        track.rollback();
    } else {
        this.refundGas(msg.getGas().longValue() - requiredGas, "call pre-compiled");
        if (contract instanceof Bridge || contract instanceof RemascContract) {
            // CREATE CALL INTERNAL TRANSACTION
            InternalTransaction internalTx = addInternalTx(null, getGasLimit(), senderAddress, contextAddress, endowment, EMPTY_BYTE_ARRAY, "call");
            Block executionBlock = new Block(getPrevHash().getData(), EMPTY_BYTE_ARRAY, getCoinbase().getData(), EMPTY_BYTE_ARRAY, getDifficulty().getData(), getNumber().longValue(), getGasLimit().getData(), 0, getTimestamp().longValue(), EMPTY_BYTE_ARRAY, EMPTY_BYTE_ARRAY, EMPTY_BYTE_ARRAY, new ArrayList<>(), new ArrayList<>(), null);
            contract.init(internalTx, executionBlock, track, this.invoke.getBlockStore(), null, null);
        }
        byte[] out = contract.execute(data);
        this.memorySave(msg.getOutDataOffs().intValue(), out);
        this.stackPushOne();
        track.commit();
    }
}
Also used : Coin(co.rsk.core.Coin) Repository(org.ethereum.core.Repository) RemascContract(co.rsk.remasc.RemascContract) RskAddress(co.rsk.core.RskAddress) Block(org.ethereum.core.Block) Bridge(co.rsk.peg.Bridge)

Example 75 with Coin

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

the class Program method callToAddress.

/**
 * That method is for internal code invocations
 * <p/>
 * - Normal calls invoke a specified contract which updates itself
 * - Stateless calls invoke code from another contract, within the context of the caller
 *
 * @param msg is the message call object
 */
public void callToAddress(MessageCall msg) {
    if (getCallDeep() == MAX_DEPTH) {
        stackPushZero();
        refundGas(msg.getGas().longValue(), " call deep limit reach");
        return;
    }
    byte[] data = memoryChunk(msg.getInDataOffs().intValue(), msg.getInDataSize().intValue());
    // FETCH THE SAVED STORAGE
    RskAddress codeAddress = new RskAddress(msg.getCodeAddress());
    RskAddress senderAddress = new RskAddress(getOwnerAddress());
    RskAddress contextAddress = msg.getType().isStateless() ? senderAddress : codeAddress;
    if (isLogEnabled) {
        logger.info(msg.getType().name() + " for existing contract: address: [{}], outDataOffs: [{}], outDataSize: [{}]  ", contextAddress, msg.getOutDataOffs().longValue(), msg.getOutDataSize().longValue());
    }
    Repository track = getStorage().startTracking();
    // 2.1 PERFORM THE VALUE (endowment) PART
    Coin endowment = new Coin(msg.getEndowment().getData());
    Coin senderBalance = track.getBalance(senderAddress);
    if (isNotCovers(senderBalance, endowment)) {
        stackPushZero();
        refundGas(msg.getGas().longValue(), "refund gas from message call");
        return;
    }
    // FETCH THE CODE
    byte[] programCode = getStorage().isExist(codeAddress) ? getStorage().getCode(codeAddress) : EMPTY_BYTE_ARRAY;
    // Always first remove funds from sender
    track.addBalance(senderAddress, endowment.negate());
    Coin contextBalance;
    if (byTestingSuite()) {
        // This keeps track of the calls created for a test
        getResult().addCallCreate(data, contextAddress.getBytes(), msg.getGas().longValueSafe(), msg.getEndowment().getNoLeadZeroesData());
        return;
    }
    contextBalance = track.addBalance(contextAddress, endowment);
    // CREATE CALL INTERNAL TRANSACTION
    InternalTransaction internalTx = addInternalTx(null, getGasLimit(), senderAddress, contextAddress, endowment, programCode, "call");
    boolean callResult;
    if (isNotEmpty(programCode)) {
        callResult = executeCode(msg, contextAddress, contextBalance, internalTx, track, programCode, senderAddress, data);
    } else {
        track.commit();
        callResult = true;
        refundGas(msg.getGas().longValue(), "remaining gas from the internal call");
    }
    // 4. THE FLAG OF SUCCESS IS ONE PUSHED INTO THE STACK
    if (callResult) {
        stackPushOne();
    } else {
        stackPushZero();
    }
}
Also used : Coin(co.rsk.core.Coin) Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress)

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