Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove RskSystemProperties from many core objects #642

Merged
merged 9 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rskj-core/src/main/java/co/rsk/DoPrune.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ private void doPrune() {
logger.info("Datasource Name {}", dataSourceName);
logger.info("Blockchain height {}", height);

TrieImpl source = new TrieImpl(new TrieStoreImpl(levelDbByName(this.rskSystemProperties, dataSourceName)), true);
TrieImpl source = new TrieImpl(new TrieStoreImpl(levelDbByName(dataSourceName, this.rskSystemProperties.databaseDir())), true);
String targetDataSourceName = dataSourceName + "B";
KeyValueDataSource targetDataSource = levelDbByName(this.rskSystemProperties, targetDataSourceName);
KeyValueDataSource targetDataSource = levelDbByName(targetDataSourceName, this.rskSystemProperties.databaseDir());
TrieStore targetStore = new TrieStoreImpl(targetDataSource);

this.processBlocks(height - blocksToProcess, source, contractAddress, targetStore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.ethereum.core.TransactionExecutor;
import org.ethereum.db.BlockStore;
import org.ethereum.db.ReceiptStore;
import org.ethereum.listener.EthereumListenerAdapter;
import org.ethereum.vm.PrecompiledContracts;
import org.ethereum.vm.program.ProgramResult;
import org.ethereum.vm.program.invoke.ProgramInvokeFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -81,15 +83,10 @@ public ProgramResult executeTransaction(
);

TransactionExecutor executor = new TransactionExecutor(
config,
tx,
0,
coinbase,
repository,
blockStore,
receiptStore,
programInvokeFactory,
executionBlock
tx, 0, coinbase, repository, blockStore, receiptStore,
programInvokeFactory, executionBlock, new EthereumListenerAdapter(), 0, config.getVmConfig(),
config.getBlockchainConfig(), config.playVM(), config.isRemascEnabled(), config.vmTrace(), new PrecompiledContracts(config),
config.databaseDir(), config.vmTraceDir(), config.vmTraceCompressed()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you could rework config.getVmConfig() object to have all vm config data related and reduce parameters

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we even have a remascEnabled option? 😭

Copy link
Contributor

@adauto82 adauto82 Sep 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybee it's time
//TODO: REMOVE THIS WHEN THE LocalBLockTests starts working with REMASC
public boolean isRemascEnabled() { return remascEnabled; }

).setLocalCall(true);

executor.init();
Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/co/rsk/core/RskFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public Wallet getWallet(RskSystemProperties config) {
}

logger.info("Local wallet enabled");
KeyValueDataSource ds = new LevelDbDataSource(config, "wallet");
KeyValueDataSource ds = new LevelDbDataSource("wallet", config.databaseDir());
ds.init();
return new Wallet(ds);
}
Expand Down
32 changes: 12 additions & 20 deletions rskj-core/src/main/java/co/rsk/core/bc/BlockChainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package co.rsk.core.bc;

import co.rsk.blocks.BlockRecorder;
import co.rsk.config.RskSystemProperties;
import co.rsk.core.BlockDifficulty;
import co.rsk.net.Metrics;
import co.rsk.panic.PanicProcessor;
Expand All @@ -34,7 +33,6 @@
import org.ethereum.db.ReceiptStore;
import org.ethereum.db.TransactionInfo;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.AdminInfo;
import org.ethereum.util.RLP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -80,13 +78,11 @@ public class BlockChainImpl implements Blockchain {
private static final Logger logger = LoggerFactory.getLogger("blockchain");
private static final PanicProcessor panicProcessor = new PanicProcessor();

private final RskSystemProperties config;
private final Repository repository;
private final BlockStore blockStore;
private final ReceiptStore receiptStore;
private final TransactionPool transactionPool;
private EthereumListener listener;
private final AdminInfo adminInfo;
private BlockValidator blockValidator;

private volatile BlockChainStatus status = new BlockChainStatus(null, BlockDifficulty.ZERO);
Expand All @@ -95,26 +91,29 @@ public class BlockChainImpl implements Blockchain {
private final Object accessLock = new Object();
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

private final boolean flushEnabled;
private final int flushNumberOfBlocks;
private final BlockExecutor blockExecutor;
private BlockRecorder blockRecorder;
private boolean noValidation;

public BlockChainImpl(RskSystemProperties config,
Repository repository,
public BlockChainImpl(Repository repository,
BlockStore blockStore,
ReceiptStore receiptStore,
TransactionPool transactionPool,
EthereumListener listener,
AdminInfo adminInfo,
BlockValidator blockValidator) {
this.config = config;
BlockValidator blockValidator,
boolean flushEnabled,
int flushNumberOfBlocks,
BlockExecutor blockExecutor) {
this.repository = repository;
this.blockStore = blockStore;
this.receiptStore = receiptStore;
this.listener = listener;
this.adminInfo = adminInfo;
this.blockValidator = blockValidator;
this.blockExecutor = new BlockExecutor(config, repository, receiptStore, blockStore, listener);
this.flushEnabled = flushEnabled;
this.flushNumberOfBlocks = flushNumberOfBlocks;
this.blockExecutor = blockExecutor;
this.transactionPool = transactionPool;
}

Expand All @@ -132,8 +131,6 @@ public Repository getRepository() {

public BlockValidator getBlockValidator() { return blockValidator; }

public AdminInfo getAdminInfo() { return adminInfo; }

@VisibleForTesting
public void setBlockValidator(BlockValidator validator) {
this.blockValidator = validator;
Expand Down Expand Up @@ -281,11 +278,6 @@ private ImportResult internalTryToConnect(Block block) {
}

long totalTime = System.nanoTime() - saveTime;

if (adminInfo != null) {
adminInfo.addBlockExecTime(totalTime);
}

logger.trace("block: num: [{}] hash: [{}], executed after: [{}]nano", block.getNumber(), block.getShortHash(), totalTime);
}

Expand Down Expand Up @@ -568,7 +560,7 @@ private boolean isValid(Block block) {
private int nFlush = 0;

private void flushData() {
if (config.isFlushEnabled() && nFlush == 0) {
if (flushEnabled && nFlush == 0) {
long saveTime = System.nanoTime();
repository.flush();
long totalTime = System.nanoTime() - saveTime;
Expand All @@ -579,7 +571,7 @@ private void flushData() {
logger.trace("blockstore flush: [{}]nano", totalTime);
}
nFlush++;
nFlush = nFlush % config.flushNumberOfBlocks();
nFlush = nFlush % flushNumberOfBlocks;
}

public static byte[] calcTxTrie(List<Transaction> transactions) {
Expand Down
41 changes: 16 additions & 25 deletions rskj-core/src/main/java/co/rsk/core/bc/BlockExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@

package co.rsk.core.bc;

import co.rsk.config.RskSystemProperties;
import co.rsk.core.Coin;
import co.rsk.core.RskAddress;
import org.ethereum.core.*;
import org.ethereum.db.BlockStore;
import org.ethereum.db.ReceiptStore;
import org.ethereum.listener.EthereumListener;
import org.ethereum.vm.program.invoke.ProgramInvokeFactory;
import org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bouncycastle.util.encoders.Hex;
Expand All @@ -45,26 +40,12 @@
public class BlockExecutor {
private static final Logger logger = LoggerFactory.getLogger("blockexecutor");

private final RskSystemProperties config;
private final Repository repository;
private final ReceiptStore receiptStore;
private final BlockStore blockStore;
private final EthereumListener listener;
private final TransactionExecutorFactory transactionExecutorFactory;

private final ProgramInvokeFactory programInvokeFactory = new ProgramInvokeFactoryImpl();

public BlockExecutor(
RskSystemProperties config,
Repository repository,
ReceiptStore receiptStore,
BlockStore blockStore,
EthereumListener listener) {

this.config = config;
public BlockExecutor(Repository repository, TransactionExecutorFactory transactionExecutorFactory) {
this.repository = repository;
this.receiptStore = receiptStore;
this.blockStore = blockStore;
this.listener = listener;
this.transactionExecutorFactory = transactionExecutorFactory;
}

/**
Expand Down Expand Up @@ -209,8 +190,14 @@ private BlockResult execute(Block block, byte[] stateRoot, boolean discardInvali
for (Transaction tx : block.getTransactionsList()) {
logger.trace("apply block: [{}] tx: [{}] ", block.getNumber(), i);

TransactionExecutor txExecutor = new TransactionExecutor(config, tx, txindex++, block.getCoinbase(), track, blockStore, receiptStore, programInvokeFactory, block, listener, totalGasUsed);

TransactionExecutor txExecutor = transactionExecutorFactory.newInstance(
tx,
txindex++,
block.getCoinbase(),
track,
block,
totalGasUsed
);
boolean readyToExecute = txExecutor.init();
if (!ignoreReadyToExecute && !readyToExecute) {
if (discardInvalidTxs) {
Expand Down Expand Up @@ -265,4 +252,8 @@ private BlockResult execute(Block block, byte[] stateRoot, boolean discardInvali

return new BlockResult(executedTransactions, receipts, lastStateRootHash, totalGasUsed, totalPaidFees);
}

public interface TransactionExecutorFactory {
TransactionExecutor newInstance(Transaction tx, int txindex, RskAddress coinbase, Repository track, Block block, long totalGasUsed);
}
}
27 changes: 23 additions & 4 deletions rskj-core/src/main/java/co/rsk/core/bc/TransactionPoolImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import co.rsk.config.RskSystemProperties;
import co.rsk.core.Coin;
import co.rsk.core.RskAddress;
import co.rsk.crypto.Keccak256;
import co.rsk.net.handler.TxPendingValidator;
import co.rsk.trie.Trie;
Expand All @@ -31,8 +30,10 @@
import org.ethereum.db.BlockStore;
import org.ethereum.db.ReceiptStore;
import org.ethereum.listener.EthereumListener;
import org.ethereum.listener.EthereumListenerAdapter;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.vm.PrecompiledContracts;
import org.ethereum.vm.program.invoke.ProgramInvokeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -158,8 +159,26 @@ public PendingState getPendingState() {
new TransactionSet(pendingTransactions),
(repository, tx) ->
new TransactionExecutor(
config, tx, 0, bestBlock.getCoinbase(), repository,
blockStore, receiptStore, programInvokeFactory, createFakePendingBlock(bestBlock))
tx,
0,
bestBlock.getCoinbase(),
repository,
blockStore,
receiptStore,
programInvokeFactory,
createFakePendingBlock(bestBlock),
new EthereumListenerAdapter(),
0,
config.getVmConfig(),
config.getBlockchainConfig(),
config.playVM(),
config.isRemascEnabled(),
config.vmTrace(),
new PrecompiledContracts(config),
config.databaseDir(),
config.vmTraceDir(),
config.vmTraceCompressed()
)
);
}

Expand Down Expand Up @@ -447,7 +466,7 @@ private boolean senderCanPayPendingTransactionsAndNewTx(Transaction newTx) {

private Coin getTxBaseCost(Transaction tx) {
Coin gasCost = tx.getValue();
if (bestBlock == null || tx.transactionCost(config, bestBlock) > 0) {
if (bestBlock == null || tx.transactionCost(bestBlock, config.getBlockchainConfig()) > 0) {
BigInteger gasLimit = new BigInteger(1, tx.getGasLimit());
gasCost = gasCost.add(tx.getGasPrice().multiply(gasLimit));
}
Expand Down
31 changes: 14 additions & 17 deletions rskj-core/src/main/java/co/rsk/db/ContractDetailsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

package co.rsk.db;

import co.rsk.config.RskSystemProperties;
import co.rsk.crypto.Keccak256;
import co.rsk.panic.PanicProcessor;
import co.rsk.trie.*;
import org.ethereum.datasource.DataSourcePool;
import org.ethereum.datasource.HashMapDB;
import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.db.ContractDetails;
Expand Down Expand Up @@ -52,7 +50,8 @@ public class ContractDetailsImpl implements ContractDetails {
private static final PanicProcessor panicProcessor = new PanicProcessor();
private static final Logger logger = LoggerFactory.getLogger("contractdetails");

private final RskSystemProperties config;
private final String databaseDir;
private final int memoryStorageLimit;

private Trie trie;
private byte[] code;
Expand All @@ -64,20 +63,18 @@ public class ContractDetailsImpl implements ContractDetails {
private boolean closed;
private Set<ByteArrayWrapper> keys = new HashSet<>();

public ContractDetailsImpl(RskSystemProperties config, byte[] encoded) {
this.config = config;
public ContractDetailsImpl(byte[] encoded, int memoryStorageLimit, String databaseDir) {
this.memoryStorageLimit = memoryStorageLimit;
this.databaseDir = databaseDir;
decode(encoded);
}

public ContractDetailsImpl(RskSystemProperties config) {
this(config, null, new TrieImpl(new TrieStoreImpl(new HashMapDB()), true), null);
}

public ContractDetailsImpl(RskSystemProperties config, byte[] address, Trie trie, byte[] code) {
this.config = config;
public ContractDetailsImpl(byte[] address, Trie trie, byte[] code, int memoryStorageLimit, String databaseDir) {
this.address = ByteUtils.clone(address);
this.trie = trie;
this.code = ByteUtils.clone(code);
this.memoryStorageLimit = memoryStorageLimit;
this.databaseDir = databaseDir;
}

@Override
Expand Down Expand Up @@ -193,7 +190,7 @@ public final void decode(byte[] rlpBytes) {

if (this.externalStorage) {
Keccak256 snapshotHash = new Keccak256(rlpStorage.getRLPData());
this.trie = new TrieImpl(new TrieStoreImpl(levelDbByName(config, getDataSourceName())), true).getSnapshotTo(snapshotHash);
this.trie = new TrieImpl(new TrieStoreImpl(levelDbByName(getDataSourceName(), databaseDir)), true).getSnapshotTo(snapshotHash);
} else {
this.trie = TrieImpl.deserialize(rlpStorage.getRLPData());
}
Expand Down Expand Up @@ -333,7 +330,7 @@ public synchronized void syncStorage() {
// switching to data source

logger.trace("switching to data source, hash {}, address {}", hashString, addressString);
KeyValueDataSource ds = levelDbByName(config, this.getDataSourceName());
KeyValueDataSource ds = levelDbByName(this.getDataSourceName(), databaseDir);
TrieStoreImpl newStore = new TrieStoreImpl(ds);
TrieStoreImpl originalStore = (TrieStoreImpl)((TrieImpl) this.trie).getStore();
newStore.copyFrom(originalStore);
Expand Down Expand Up @@ -367,14 +364,14 @@ public synchronized ContractDetails getSnapshotTo(byte[] hash) {

this.trie.save();

ContractDetailsImpl details = new ContractDetailsImpl(this.config, this.address, this.trie.getSnapshotTo(new Keccak256(hash)), this.code);
ContractDetailsImpl details = new ContractDetailsImpl(this.address, this.trie.getSnapshotTo(new Keccak256(hash)), this.code, this.memoryStorageLimit, this.databaseDir);
details.keys = new HashSet<>();
details.keys.addAll(this.keys);
details.externalStorage = this.externalStorage;
details.originalExternalStorage = this.originalExternalStorage;

if (this.externalStorage) {
levelDbByName(config, getDataSourceName());
levelDbByName(getDataSourceName(), databaseDir);
}

logger.trace("getting contract details snapshot hash {}, address {}, storage size {}, has external storage {}", details.getStorageHashAsString(), details.getAddressAsString(), details.getStorageSize(), details.hasExternalStorage());
Expand Down Expand Up @@ -404,7 +401,7 @@ private void removeKey(byte[] key) {
}

private void checkExternalStorage() {
this.externalStorage = (keys.size() > config.detailsInMemoryStorageLimit()) || this.externalStorage;
this.externalStorage = (keys.size() > memoryStorageLimit) || this.externalStorage;
}

private String getDataSourceName() {
Expand All @@ -431,7 +428,7 @@ private void checkDataSourceIsOpened() {
}

logger.trace("reopening contract details data source");
KeyValueDataSource ds = levelDbByName(config, this.getDataSourceName());
KeyValueDataSource ds = levelDbByName(this.getDataSourceName(), databaseDir);
TrieStoreImpl newStore = new TrieStoreImpl(ds);
Trie newTrie = newStore.retrieve(this.trie.getHash().getBytes());
this.trie = newTrie;
Expand Down
4 changes: 2 additions & 2 deletions rskj-core/src/main/java/co/rsk/db/PruneService.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public void process() {
long to = this.blockchain.getBestBlock().getNumber() - this.pruneConfiguration.getNoBlocksToAvoidForks();

String dataSourceName = getDataSourceName(contractAddress);
KeyValueDataSource sourceDataSource = levelDbByName(this.rskConfiguration, dataSourceName);
KeyValueDataSource targetDataSource = levelDbByName(this.rskConfiguration, dataSourceName + "B");
KeyValueDataSource sourceDataSource = levelDbByName(dataSourceName, this.rskConfiguration.databaseDir());
KeyValueDataSource targetDataSource = levelDbByName(dataSourceName + "B", this.rskConfiguration.databaseDir());
TrieStore targetStore = new TrieStoreImpl(targetDataSource);

TrieCopier.trieContractStateCopy(targetStore, blockchain, from, to, blockchain.getRepository(), this.contractAddress);
Expand Down
Loading