Search in sources :

Example 66 with RskAddress

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

the class TxPoolModuleImpl method content.

/**
 * This method should return 2 dictionaries containing pending and queued transactions
 * Each entry is an origin-address to a batch of scheduled transactions
 * These batches themselves are maps associating nonces with actual transactions.
 * When there are no transactions the answer would be
 * "{"pending": {}, "queued": {}}"
 */
@Override
public String content() {
    Map<String, JsonNode> contentProps = new HashMap<>();
    Map<RskAddress, Map<BigInteger, List<Transaction>>> pendingGrouped = groupTransactions(transactionPool.getPendingTransactions());
    Map<RskAddress, Map<BigInteger, List<Transaction>>> queuedGrouped = groupTransactions(transactionPool.getQueuedTransactions());
    contentProps.put(PENDING, serializeTransactions(pendingGrouped, this::fullSerializer));
    contentProps.put(QUEUED, serializeTransactions(queuedGrouped, this::fullSerializer));
    JsonNode node = jsonNodeFactory.objectNode().setAll(contentProps);
    return node.toString();
}
Also used : Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 67 with RskAddress

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

the class TransactionExecutor method create.

private void create() {
    RskAddress newContractAddress = tx.getContractAddress();
    if (isEmpty(tx.getData())) {
        mEndGas = toBI(tx.getGasLimit()).subtract(BigInteger.valueOf(basicTxCost));
        cacheTrack.createAccount(newContractAddress);
    } else {
        ProgramInvoke programInvoke = programInvokeFactory.createProgramInvoke(tx, txindex, executionBlock, cacheTrack, blockStore);
        this.vm = new VM(vmConfig, precompiledContracts);
        BlockchainConfig configForBlock = config.getBlockchainConfig().getConfigForBlock(executionBlock.getNumber());
        this.program = new Program(vmConfig, precompiledContracts, configForBlock, tx.getData(), programInvoke, tx);
        // reset storage if the contract with the same address already exists
        // TCK test case only - normally this is near-impossible situation in the real network
        ContractDetails contractDetails = program.getStorage().getContractDetails(newContractAddress);
        for (DataWord key : contractDetails.getStorageKeys()) {
            program.storageSave(key, DataWord.ZERO);
        }
    }
    Coin endowment = tx.getValue();
    cacheTrack.transfer(tx.getSender(), newContractAddress, endowment);
}
Also used : ProgramInvoke(org.ethereum.vm.program.invoke.ProgramInvoke) Coin(co.rsk.core.Coin) BlockchainConfig(org.ethereum.config.BlockchainConfig) Program(org.ethereum.vm.program.Program) RskAddress(co.rsk.core.RskAddress) ContractDetails(org.ethereum.db.ContractDetails)

Example 68 with RskAddress

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

the class TransactionExecutor method call.

private void call() {
    if (!readyToExecute) {
        return;
    }
    logger.trace("Call transaction {} {}", toBI(tx.getNonce()), tx.getHash());
    RskAddress targetAddress = tx.getReceiveAddress();
    // DataWord(targetAddress)) can fail with exception:
    // java.lang.RuntimeException: Data word can't exceed 32 bytes:
    // if targetAddress size is greater than 32 bytes.
    // But init() will detect this earlier
    precompiledContract = precompiledContracts.getContractForAddress(new DataWord(targetAddress.getBytes()));
    if (precompiledContract != null) {
        precompiledContract.init(tx, executionBlock, track, blockStore, receiptStore, result.getLogInfoList());
        long requiredGas = precompiledContract.getGasForData(tx.getData());
        BigInteger txGasLimit = toBI(tx.getGasLimit());
        if (!localCall && txGasLimit.compareTo(BigInteger.valueOf(requiredGas)) < 0) {
            // no refund
            // no endowment
            execError(String.format("Out of Gas calling precompiled contract 0x%s, required: %d, left: %s ", targetAddress.toString(), (requiredGas + basicTxCost), mEndGas));
            mEndGas = BigInteger.ZERO;
            return;
        } else {
            long gasUsed = requiredGas + basicTxCost;
            mEndGas = txGasLimit.subtract(BigInteger.valueOf(requiredGas + basicTxCost));
            // FIXME: save return for vm trace
            try {
                byte[] out = precompiledContract.execute(tx.getData());
                result.setHReturn(out);
            } catch (RuntimeException e) {
                result.setException(e);
            }
            result.spendGas(gasUsed);
        }
    } else {
        byte[] code = track.getCode(targetAddress);
        if (isEmpty(code)) {
            mEndGas = toBI(tx.getGasLimit()).subtract(BigInteger.valueOf(basicTxCost));
            result.spendGas(basicTxCost);
        } else {
            ProgramInvoke programInvoke = programInvokeFactory.createProgramInvoke(tx, txindex, executionBlock, cacheTrack, blockStore);
            this.vm = new VM(vmConfig, precompiledContracts);
            BlockchainConfig configForBlock = config.getBlockchainConfig().getConfigForBlock(executionBlock.getNumber());
            this.program = new Program(vmConfig, precompiledContracts, configForBlock, code, programInvoke, tx);
        }
    }
    if (result.getException() == null) {
        Coin endowment = tx.getValue();
        cacheTrack.transfer(tx.getSender(), targetAddress, endowment);
    }
}
Also used : ProgramInvoke(org.ethereum.vm.program.invoke.ProgramInvoke) Coin(co.rsk.core.Coin) BlockchainConfig(org.ethereum.config.BlockchainConfig) Program(org.ethereum.vm.program.Program) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Example 69 with RskAddress

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

the class TransactionSet method removeTransactionByHash.

public void removeTransactionByHash(Keccak256 hash) {
    Transaction transaction = this.transactionsByHash.get(hash);
    if (transaction == null) {
        return;
    }
    this.transactionsByHash.remove(hash);
    RskAddress senderAddress = transaction.getSender();
    List<Transaction> txs = this.transactionsByAddress.get(senderAddress);
    if (txs != null) {
        txs.remove(transaction);
        if (txs.isEmpty()) {
            this.transactionsByAddress.remove(senderAddress);
        }
    }
}
Also used : RskAddress(co.rsk.core.RskAddress)

Example 70 with RskAddress

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

the class BlockChainLoader method loadBlockchain.

public BlockChainImpl loadBlockchain() {
    BlockChainImpl blockchain = new BlockChainImpl(config, repository, blockStore, receiptStore, transactionPool, listener, adminInfo, blockValidator);
    if (!config.databaseReset()) {
        blockStore.load();
    }
    Block bestBlock = blockStore.getBestBlock();
    if (bestBlock == null) {
        logger.info("DB is empty - adding Genesis");
        BigInteger initialNonce = config.getBlockchainConfig().getCommonConstants().getInitialNonce();
        Genesis genesis = GenesisLoader.loadGenesis(config, config.genesisInfo(), initialNonce, true);
        for (RskAddress addr : genesis.getPremine().keySet()) {
            repository.createAccount(addr);
            InitialAddressState initialAddressState = genesis.getPremine().get(addr);
            repository.addBalance(addr, initialAddressState.getAccountState().getBalance());
            AccountState accountState = repository.getAccountState(addr);
            accountState.setNonce(initialAddressState.getAccountState().getNonce());
            if (initialAddressState.getContractDetails() != null) {
                repository.updateContractDetails(addr, initialAddressState.getContractDetails());
                accountState.setStateRoot(initialAddressState.getAccountState().getStateRoot());
                accountState.setCodeHash(initialAddressState.getAccountState().getCodeHash());
            }
            repository.updateAccountState(addr, accountState);
        }
        genesis.setStateRoot(repository.getRoot());
        genesis.flushRLP();
        blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
        blockchain.setBestBlock(genesis);
        blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());
        listener.onBlock(genesis, new ArrayList<TransactionReceipt>());
        repository.dumpState(genesis, 0, 0, null);
        logger.info("Genesis block loaded");
    } else {
        BlockDifficulty totalDifficulty = blockStore.getTotalDifficultyForHash(bestBlock.getHash().getBytes());
        blockchain.setBestBlock(bestBlock);
        blockchain.setTotalDifficulty(totalDifficulty);
        logger.info("*** Loaded up to block [{}] totalDifficulty [{}] with stateRoot [{}]", blockchain.getBestBlock().getNumber(), blockchain.getTotalDifficulty().toString(), Hex.toHexString(blockchain.getBestBlock().getStateRoot()));
    }
    String rootHash = config.rootHashStart();
    if (StringUtils.isNotBlank(rootHash)) {
        // update world state by dummy hash
        byte[] rootHashArray = Hex.decode(rootHash);
        logger.info("Loading root hash from property file: [{}]", rootHash);
        this.repository.syncToRoot(rootHashArray);
    } else {
        // todo this is just a workaround, move EMPTY_TRIE_HASH logic to Trie implementation
        if (!Arrays.equals(blockchain.getBestBlock().getStateRoot(), EMPTY_TRIE_HASH)) {
            this.repository.syncToRoot(blockchain.getBestBlock().getStateRoot());
        }
    }
    return blockchain;
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

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