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();
}
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);
}
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);
}
}
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);
}
}
}
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;
}
Aggregations