Skip to content

Commit

Permalink
Removed the NodeInternal
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed May 2, 2024
1 parent 5f85973 commit c267570
Show file tree
Hide file tree
Showing 23 changed files with 104 additions and 379 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import io.hotmoka.node.api.requests.InitialTransactionRequest;
import io.hotmoka.node.api.responses.InitialTransactionResponse;
import io.hotmoka.node.api.transactions.TransactionReference;
import io.hotmoka.node.local.internal.AbstractLocalNodeImpl;
import io.hotmoka.node.local.internal.InitialResponseBuilderImpl;
import io.hotmoka.node.local.internal.NodeInternal;
import io.hotmoka.stores.StoreTransaction;

/**
Expand All @@ -41,7 +41,7 @@ public abstract class AbstractInitialResponseBuilder<Request extends InitialTran
* @param node the node that is creating the response
* @throws TransactionRejectedException if the builder cannot be created
*/
protected AbstractInitialResponseBuilder(TransactionReference reference, Request request, StoreTransaction<?> transaction, NodeInternal node) throws TransactionRejectedException {
protected AbstractInitialResponseBuilder(TransactionReference reference, Request request, StoreTransaction<?> transaction, AbstractLocalNodeImpl<?,?> node) throws TransactionRejectedException {
super(reference, request, transaction, node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.hotmoka.node.api.requests.NonInitialTransactionRequest;
import io.hotmoka.node.api.responses.NonInitialTransactionResponse;
import io.hotmoka.node.api.transactions.TransactionReference;
import io.hotmoka.node.local.internal.NodeInternal;
import io.hotmoka.node.local.internal.AbstractLocalNodeImpl;
import io.hotmoka.node.local.internal.NonInitialResponseBuilderImpl;
import io.hotmoka.stores.StoreTransaction;

Expand All @@ -43,7 +43,7 @@ public abstract class AbstractNonInitialResponseBuilder<Request extends NonIniti
* @param node the node that is creating the response
* @throws TransactionRejectedException if the builder cannot be built
*/
protected AbstractNonInitialResponseBuilder(TransactionReference reference, Request request, StoreTransaction<?> transaction, NodeInternal node) throws TransactionRejectedException {
protected AbstractNonInitialResponseBuilder(TransactionReference reference, Request request, StoreTransaction<?> transaction, AbstractLocalNodeImpl<?,?> node) throws TransactionRejectedException {
super(reference, request, transaction, node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected final void notifyEvent(StorageReference creator, StorageReference even
/**
* The caches of the node.
*/
protected final NodeCache caches;
public final NodeCache caches;

/**
* The store of the node.
Expand Down Expand Up @@ -252,11 +252,6 @@ protected final void notifyEvent(StorageReference creator, StorageReference even
*/
private volatile BigInteger numberOfTransactionsSinceLastReward;

/**
* The view of this node with methods used by the implementation of this module.
*/
final NodeInternal internal = new NodeInternalImpl();

/**
* The amount of gas allowed for the execution of the reward method of the validators
* at each committed block.
Expand Down Expand Up @@ -297,8 +292,8 @@ private AbstractLocalNodeImpl(C config, ConsensusConfig<?,?> consensus, boolean
throw new RuntimeException("Unexpected exception", e);
}

this.storeUtilities = new StoreUtilityImpl(internal);
this.caches = new NodeCachesImpl(internal, consensus);
this.storeUtilities = new StoreUtilityImpl(this);
this.caches = new NodeCachesImpl(this, consensus, config.getRequestCacheSize(), config.getResponseCacheSize());
this.recentCheckTransactionErrors = new LRUCache<>(100, 1000);
this.gasConsumedSinceLastReward = ZERO;
this.coinsSinceLastReward = ZERO;
Expand Down Expand Up @@ -382,6 +377,14 @@ public final ConsensusConfig<?,?> getConfig() throws NodeException {
}
}

public LocalNodeConfig<?,?> getLocalNodeConfig() {
return config;
}

public GasCostModel getGasCostModel() {
return gasCostModel;
}

@Override
public final TransactionReference getTakamakaCode() throws NodeException {
try (var scope = mkScope()) {
Expand Down Expand Up @@ -567,7 +570,7 @@ public final Optional<StorageValue> runInstanceMethodCallTransaction(InstanceMet
var transaction = store.beginTransaction(System.currentTimeMillis());

synchronized (deliverTransactionLock) {
result = getOutcome(new InstanceViewMethodCallResponseBuilder(reference, request, transaction, internal).getResponse());
result = getOutcome(new InstanceViewMethodCallResponseBuilder(reference, request, transaction, this).getResponse());
}

transaction.abort();
Expand All @@ -587,7 +590,7 @@ public final Optional<StorageValue> runStaticMethodCallTransaction(StaticMethodC
var transaction = store.beginTransaction(System.currentTimeMillis());

synchronized (deliverTransactionLock) {
result = getOutcome(new StaticViewMethodCallResponseBuilder(reference, request, transaction, internal).getResponse());
result = getOutcome(new StaticViewMethodCallResponseBuilder(reference, request, transaction, this).getResponse());
}

transaction.abort();
Expand Down Expand Up @@ -798,13 +801,11 @@ else if (minted.signum() < 0) {
// if there is only one update, it is the update of the nonce of the manifest: we prefer not to expand
// the store with the transaction, so that the state stabilizes, which might give
// to the node the chance of suspending the generation of new blocks
if (!(response instanceof TransactionResponseWithUpdates) || ((TransactionResponseWithUpdates) response).getUpdates().count() > 1L)
if (!(response instanceof TransactionResponseWithUpdates trwu) || trwu.getUpdates().count() > 1L)
response = deliverTransaction(request);

if (response instanceof MethodCallTransactionFailedResponse) {
MethodCallTransactionFailedResponse responseAsFailed = (MethodCallTransactionFailedResponse) response;
if (response instanceof MethodCallTransactionFailedResponse responseAsFailed)
LOGGER.log(Level.WARNING, "could not reward the validators: " + responseAsFailed.getWhere() + ": " + responseAsFailed.getClassNameOfCause() + ": " + responseAsFailed.getMessageOfCause());
}
else {
LOGGER.info("units of gas consumed for CPU, RAM or storage since the previous reward: " + gasConsumedSinceLastReward);
LOGGER.info("units of coin rewarded to the validators for their work since the previous reward: " + coinsSinceLastReward);
Expand Down Expand Up @@ -903,19 +904,19 @@ protected void invalidateCachesIfNeeded(TransactionResponse response, EngineClas
*/
protected ResponseBuilder<?,?> responseBuilderFor(TransactionReference reference, TransactionRequest<?> request, StoreTransaction<?> transaction) throws TransactionRejectedException {
if (request instanceof JarStoreInitialTransactionRequest jsitr)
return new JarStoreInitialResponseBuilder(reference, jsitr, transaction, internal);
return new JarStoreInitialResponseBuilder(reference, jsitr, transaction, this);
else if (request instanceof GameteCreationTransactionRequest gctr)
return new GameteCreationResponseBuilder(reference, gctr, transaction, internal);
return new GameteCreationResponseBuilder(reference, gctr, transaction, this);
else if (request instanceof JarStoreTransactionRequest jstr)
return new JarStoreResponseBuilder(reference, jstr, transaction, internal);
return new JarStoreResponseBuilder(reference, jstr, transaction, this);
else if (request instanceof ConstructorCallTransactionRequest cctr)
return new ConstructorCallResponseBuilder(reference, cctr, transaction, internal);
return new ConstructorCallResponseBuilder(reference, cctr, transaction, this);
else if (request instanceof AbstractInstanceMethodCallTransactionRequest aimctr)
return new InstanceMethodCallResponseBuilder(reference, aimctr, transaction, internal);
return new InstanceMethodCallResponseBuilder(reference, aimctr, transaction, this);
else if (request instanceof StaticMethodCallTransactionRequest smctr)
return new StaticMethodCallResponseBuilder(reference, smctr, transaction, internal);
return new StaticMethodCallResponseBuilder(reference, smctr, transaction, this);
else if (request instanceof InitializationTransactionRequest itr)
return new InitializationResponseBuilder(reference, itr, transaction, internal);
return new InitializationResponseBuilder(reference, itr, transaction, this);
else
throw new TransactionRejectedException("Unexpected transaction request of class " + request.getClass().getName());
}
Expand Down Expand Up @@ -949,7 +950,7 @@ protected void setStore(Store<?> store) {
* @param reference the reference of the transaction
* @return the response, if any
*/
private Optional<TransactionResponse> getResponseUncommitted(TransactionReference reference) throws StoreException {
Optional<TransactionResponse> getResponseUncommitted(TransactionReference reference) throws StoreException {
var transaction = getTransaction();
if (transaction != null)
return transaction.getResponseUncommitted(reference);
Expand All @@ -966,7 +967,7 @@ private Optional<TransactionResponse> getResponseUncommitted(TransactionReferenc
* @return the history. Yields an empty stream if there is no history for {@code object}
* @throws StoreException if the store is not able to perform the operation
*/
private Stream<TransactionReference> getHistoryUncommitted(StorageReference object) throws StoreException {
Stream<TransactionReference> getHistoryUncommitted(StorageReference object) throws StoreException {
var transaction = getTransaction();
if (transaction != null)
return transaction.getHistoryUncommitted(object);
Expand All @@ -981,14 +982,26 @@ private Stream<TransactionReference> getHistoryUncommitted(StorageReference obje
* @return the manifest
* @throws StoreException if the store is not able to complete the operation correctly
*/
private Optional<StorageReference> getManifestUncommitted() throws StoreException {
Optional<StorageReference> getManifestUncommitted() throws StoreException {
var transaction = getTransaction();
if (transaction != null)
return transaction.getManifestUncommitted();
else
return store.getManifest();
}

public StoreUtility getStoreUtilities() {
return storeUtilities;
}

public <T> Future<T> submit(Callable<T> task) {
return executors.submit(task);
}

S getStore() {
return store;
}

/**
* Runs a callable and wraps any exception into an {@link TransactionRejectedException},
* if it is not a {@link TransactionException} nor a {@link CodeExecutionException}.
Expand Down Expand Up @@ -1188,101 +1201,4 @@ private void addShutdownHook() {
}
}));
}

/**
* The view of the node with the methods that are useful inside this module.
* This avoids to export such methods as public elsewhere.
*/
private class NodeInternalImpl implements NodeInternal {

@Override
public LocalNodeConfig<?,?> getConfig() {
return config;
}

@Override
public NodeCache getCaches() {
return caches;
}

@Override
public GasCostModel getGasCostModel() {
return gasCostModel;
}

@Override
public Store<?> getStore() {
return store;
}

@Override
public StoreUtility getStoreUtilities() {
return storeUtilities;
}

/**
* Yields the response of the transaction having the given reference.
* This considers also updates inside this transaction, that have not yet been committed.
*
* @param reference the reference of the transaction
* @return the response, if any
*/
public Optional<TransactionResponse> getResponseUncommitted(TransactionReference reference) throws StoreException {
return AbstractLocalNodeImpl.this.getResponseUncommitted(reference);
}

/**
* Yields the history of the given object, that is, the references to the transactions
* that can be used to reconstruct the current values of its fields.
* This considers also updates inside this transaction, that have not yet been committed.
*
* @param object the reference of the object
* @return the history. Yields an empty stream if there is no history for {@code object}
* @throws StoreException if the store is not able to perform the operation
*/
public Stream<TransactionReference> getHistoryUncommitted(StorageReference object) throws StoreException {
return AbstractLocalNodeImpl.this.getHistoryUncommitted(object);
}

/**
* Yields the manifest installed when the node is initialized.
* This considers also updates inside this transaction, that have not yet been committed.
*
* @return the manifest
* @throws StoreException if the store is not able to complete the operation correctly
*/
public Optional<StorageReference> getManifestUncommitted() throws StoreException {
return AbstractLocalNodeImpl.this.getManifestUncommitted();
}

@Override
public TransactionRequest<?> getRequest(TransactionReference reference) throws UnknownReferenceException, NodeException {
return AbstractLocalNodeImpl.this.getRequest(reference);
}

@Override
public TransactionResponse getResponse(TransactionReference reference) throws TransactionRejectedException, UnknownReferenceException, NodeException {
return AbstractLocalNodeImpl.this.getResponse(reference);
}

@Override
public ClassTag getClassTag(StorageReference object) throws NodeException, UnknownReferenceException {
return AbstractLocalNodeImpl.this.getClassTag(object);
}

@Override
public Optional<StorageValue> runInstanceMethodCallTransaction(InstanceMethodCallTransactionRequest request) throws TransactionRejectedException, TransactionException, CodeExecutionException {
return AbstractLocalNodeImpl.this.runInstanceMethodCallTransaction(request);
}

@Override
public <T> Future<T> submit(Callable<T> task) {
return executors.submit(task);
}

@Override
public void submit(Runnable task) {
executors.submit(task);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public final class EngineClassLoaderImpl implements EngineClassLoader {
* @throws NodeException
* @throws NoSuchElementException
*/
public EngineClassLoaderImpl(byte[] jar, Stream<TransactionReference> dependencies, NodeInternal node, boolean reverify, ConsensusConfig<?,?> consensus) throws ClassNotFoundException, UnsupportedVerificationVersionException, IOException, NoSuchElementException, UnknownReferenceException, NodeException {
public EngineClassLoaderImpl(byte[] jar, Stream<TransactionReference> dependencies, AbstractLocalNodeImpl<?,?> node, boolean reverify, ConsensusConfig<?,?> consensus) throws ClassNotFoundException, UnsupportedVerificationVersionException, IOException, NoSuchElementException, UnknownReferenceException, NodeException {
try {
List<TransactionReference> dependenciesAsList = dependencies.collect(Collectors.toList());

Expand Down Expand Up @@ -234,7 +234,7 @@ public EngineClassLoaderImpl(byte[] jar, Stream<TransactionReference> dependenci
* @return the class loader
* @throws ClassNotFoundException if some class of the Takamaka runtime cannot be loaded
*/
private TakamakaClassLoader mkTakamakaClassLoader(Stream<TransactionReference> dependencies, ConsensusConfig<?,?> consensus, byte[] start, NodeInternal node, List<byte[]> jars, ArrayList<TransactionReference> transactionsOfJars) throws ClassNotFoundException {
private TakamakaClassLoader mkTakamakaClassLoader(Stream<TransactionReference> dependencies, ConsensusConfig<?,?> consensus, byte[] start, AbstractLocalNodeImpl<?,?> node, List<byte[]> jars, ArrayList<TransactionReference> transactionsOfJars) throws ClassNotFoundException {
var counter = new AtomicInteger();

if (start != null) {
Expand Down Expand Up @@ -312,7 +312,7 @@ else if (previously != pos)
* @param node the node for which the class loader is created
* @param counter the number of jars that have been encountered up to now, during the recursive descent
*/
private void addJars(TransactionReference classpath, ConsensusConfig<?,?> consensus, List<byte[]> jars, List<TransactionReference> jarTransactions, NodeInternal node, AtomicInteger counter) {
private void addJars(TransactionReference classpath, ConsensusConfig<?,?> consensus, List<byte[]> jars, List<TransactionReference> jarTransactions, AbstractLocalNodeImpl<?,?> node, AtomicInteger counter) {
// consensus might be null if the node is restarting, during the recomputation of its consensus itself
if (consensus != null && counter.incrementAndGet() > consensus.getMaxDependencies())
throw new IllegalArgumentException("too many dependencies in classpath: max is " + consensus.getMaxDependencies());
Expand Down Expand Up @@ -340,11 +340,11 @@ private void addJars(TransactionReference classpath, ConsensusConfig<?,?> consen
* @return the response
* @throws IllegalArgumentException if the transaction does not exist in the store, or did not generate a response with instrumented jar
*/
private TransactionResponseWithInstrumentedJar getResponseWithInstrumentedJarAtUncommitted(TransactionReference reference, NodeInternal node) {
private TransactionResponseWithInstrumentedJar getResponseWithInstrumentedJarAtUncommitted(TransactionReference reference, AbstractLocalNodeImpl<?,?> node) {
// first we check if the response has been reverified and we use the reverified version
TransactionResponse response = reverification.getReverifiedResponse(reference)
// otherwise the response has not been reverified
.or(() -> node.getCaches().getResponseUncommitted(reference))
.or(() -> node.caches.getResponseUncommitted(reference))
.orElseThrow(() -> new IllegalArgumentException("unknown transaction reference " + reference));

if (!(response instanceof TransactionResponseWithInstrumentedJar))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class InitialResponseBuilderImpl<Request extends InitialTransact
* @param node the node that is creating the response
* @throws TransactionRejectedException if the builder cannot be created
*/
protected InitialResponseBuilderImpl(TransactionReference reference, Request request, StoreTransaction<?> transaction, NodeInternal node) throws TransactionRejectedException {
protected InitialResponseBuilderImpl(TransactionReference reference, Request request, StoreTransaction<?> transaction, AbstractLocalNodeImpl<?,?> node) throws TransactionRejectedException {
super(reference, request, transaction, node);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
public class NodeCachesImpl implements NodeCache {
protected final static Logger logger = Logger.getLogger(NodeCachesImpl.class.getName());

private final NodeInternal node;
private final AbstractLocalNodeImpl<?,?> node;

/**
* The cache for the requests.
Expand Down Expand Up @@ -147,10 +147,10 @@ public class NodeCachesImpl implements NodeCache {
* @param node the node
* @param consensus the consensus parameters of the node
*/
public NodeCachesImpl(NodeInternal node, ConsensusConfig<?,?> consensus) {
public NodeCachesImpl(AbstractLocalNodeImpl<?, ?> node, ConsensusConfig<?,?> consensus, int requestCacheSize, int responseCacheSize) {
this.node = node;
this.requests = new LRUCache<>(100, node.getConfig().getRequestCacheSize());
this.responses = new LRUCache<>(100, node.getConfig().getResponseCacheSize());
this.requests = new LRUCache<>(100, requestCacheSize);
this.responses = new LRUCache<>(100, responseCacheSize);
this.checkedSignatures = new LRUCache<>(100, 1000);
this.validators = Optional.empty();
this.versions = Optional.empty();
Expand Down Expand Up @@ -503,8 +503,8 @@ private boolean consensusParametersMightHaveChanged(TransactionResponse response

// we check if there are events of type ConsensusUpdate triggered by the manifest, validators, gas station or versions
try {
if (isInitializedUncommitted() && response instanceof TransactionResponseWithEvents) {
Stream<StorageReference> events = ((TransactionResponseWithEvents) response).getEvents();
if (isInitializedUncommitted() && response instanceof TransactionResponseWithEvents trwe) {
Stream<StorageReference> events = trwe.getEvents();
StorageReference manifest = node.getManifestUncommitted().get();
StorageReference gasStation = getGasStation().get();
StorageReference versions = getVersions().get();
Expand Down
Loading

0 comments on commit c267570

Please sign in to comment.