Skip to content

Commit

Permalink
Removed the store utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed May 4, 2024
1 parent 5002e42 commit 0af5b4d
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
import java.math.BigInteger;
import java.util.Optional;

import io.hotmoka.node.api.NodeException;
import io.hotmoka.node.api.nodes.ConsensusConfig;
import io.hotmoka.node.api.responses.TransactionResponse;
import io.hotmoka.node.api.transactions.TransactionReference;
import io.hotmoka.node.api.values.StorageReference;
import io.hotmoka.stores.EngineClassLoader;

/**
Expand All @@ -46,62 +43,13 @@ public interface NodeCache {
*/
void recomputeConsensus();

/**
* Yields the response generated for the request for the given transaction.
* If this node has some form of commit, then this method succeeds
* also if the transaction has not been committed yet in the node.
* Nodes are allowed to keep in store all, some or none of the responses
* that they computed during their lifetime.
*
* @param reference the reference of the transaction
* @return the response, if any
*/
Optional<TransactionResponse> getResponseUncommitted(TransactionReference reference);

/**
* Yields the consensus parameters of the node.
*
* @return the consensus parameters
*/
ConsensusConfig<?,?> getConsensusParams();

/**
* Yields the reference to the gamete account of the node.
* This method uses a cache to avoid repeated computations.
*
* @return the reference to the gamete account, if the node is already initialized
* @throws NodeException if the node is not able to complete the operation
*/
Optional<StorageReference> getGamete() throws NodeException;

/**
* Yields the reference to the contract that collects the validators of the node.
* After each transaction that consumes gas, the price of the gas is sent to this
* contract, that can later redistribute the reward to all validators.
* This method uses a cache to avoid repeated computations.
*
* @return the reference to the contract, if the node is already initialized
* @throws NodeException if the node is not able to complete the operation
*/
Optional<StorageReference> getValidatorsUncommitted() throws NodeException;

/**
* Yields the reference to the objects that keeps track of the
* versions of the modules of the node.
*
* @return the reference to the object, if the node is already initialized
* @throws NodeException if the node is not able to complete the operation
*/
Optional<StorageReference> getVersionsUncommitted() throws NodeException;

/**
* Yields the reference to the contract that keeps track of the gas cost.
*
* @return the reference to the contract, if the node is already initialized
* @throws NodeException if the node is not able to complete the operation
*/
Optional<StorageReference> getGasStationUncommitted() throws NodeException;

/**
* Yields the current gas price of the node.
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
import io.hotmoka.node.local.api.LocalNodeConfig;
import io.hotmoka.node.local.api.NodeCache;
import io.hotmoka.node.local.api.ResponseBuilder;
import io.hotmoka.node.local.api.StoreUtility;
import io.hotmoka.node.local.internal.transactions.ConstructorCallResponseBuilder;
import io.hotmoka.node.local.internal.transactions.GameteCreationResponseBuilder;
import io.hotmoka.node.local.internal.transactions.InitializationResponseBuilder;
Expand Down Expand Up @@ -179,11 +178,6 @@ public final Hasher<TransactionRequest<?>> getHasher() {
return hasher;
}

/**
* An object that provides utility methods on {@link #store}.
*/
protected final StoreUtility storeUtilities;

/**
* The caches of the node.
*/
Expand Down Expand Up @@ -290,8 +284,7 @@ private AbstractLocalNodeImpl(C config, ConsensusConfig<?,?> consensus, boolean
throw new RuntimeException("Unexpected exception", e);
}

this.storeUtilities = new StoreUtilityImpl(this);
this.caches = new NodeCachesImpl(this, consensus, config.getResponseCacheSize());
this.caches = new NodeCachesImpl(this, consensus);
this.recentCheckTransactionErrors = new LRUCache<>(100, 1000);
this.gasConsumedSinceLastReward = ZERO;
this.coinsSinceLastReward = ZERO;
Expand Down Expand Up @@ -482,22 +475,22 @@ public final TransactionResponse getResponse(TransactionReference reference) thr
}
}

public Optional<String> getRecentCheckTransactionErrorFor(TransactionReference reference) {
return Optional.ofNullable(recentCheckTransactionErrors.get(reference));
}

@Override
public final ClassTag getClassTag(StorageReference reference) throws UnknownReferenceException, NodeException {
try (var scope = mkScope()) {
Objects.requireNonNull(reference);

if (isNotCommitted(reference.getTransaction()))
var maybeResponse = store.getResponse(reference.getTransaction());
if (maybeResponse.isEmpty())
throw new UnknownReferenceException(reference);

return storeUtilities.getClassTagUncommitted(reference);
}
catch (NoSuchElementException e) {
throw new UnknownReferenceException(reference);
else if (maybeResponse.get() instanceof TransactionResponseWithUpdates trwu)
return trwu.getUpdates()
.filter(update -> update instanceof ClassTag && update.getObject().equals(reference))
.map(update -> (ClassTag) update)
.findFirst()
.orElseThrow(() -> new NodeException("Object " + reference + " has not class tag in store"));
else
throw new NodeException("The creation of object " + reference + " does not contain updates");
}
}

Expand All @@ -506,10 +499,13 @@ public final Stream<Update> getState(StorageReference reference) throws UnknownR
try (var scope = mkScope()) {
Objects.requireNonNull(reference);
try {
if (isNotCommitted(reference.getTransaction()))
if (isNotCommitted(reference.getTransaction())) // TODO: remove after making history optional
throw new UnknownReferenceException(reference);

return getStateCommitted(reference);
Stream<TransactionReference> history = store.getHistory(reference);
var updates = new HashSet<Update>();
CheckRunnable.check(StoreException.class, () -> history.forEachOrdered(UncheckConsumer.uncheck(transaction -> addUpdatesCommitted(reference, transaction, updates))));
return updates.stream();
}
catch (NoSuchElementException e) {
throw new UnknownReferenceException(reference);
Expand All @@ -524,11 +520,8 @@ public final Stream<Update> getState(StorageReference reference) throws UnknownR
}
}

private Stream<Update> getStateCommitted(StorageReference object) throws StoreException {
Stream<TransactionReference> history = store.getHistory(object);
var updates = new HashSet<Update>();
CheckRunnable.check(StoreException.class, () -> history.forEachOrdered(UncheckConsumer.uncheck(transaction -> addUpdatesCommitted(object, transaction, updates))));
return updates.stream();
private Optional<String> getRecentCheckTransactionErrorFor(TransactionReference reference) {
return Optional.ofNullable(recentCheckTransactionErrors.get(reference));
}

/**
Expand Down Expand Up @@ -790,10 +783,12 @@ public final boolean rewardValidators(String behaving, String misbehaving) {
try {
Optional<StorageReference> manifest = getStoreTransaction().getManifestUncommitted();
if (manifest.isPresent()) {
var storeTransaction = getStoreTransaction();

// we use the manifest as caller, since it is an externally-owned account
StorageReference caller = manifest.get();
BigInteger nonce = storeUtilities.getNonceUncommitted(caller);
StorageReference validators = caches.getValidatorsUncommitted().get(); // ok, since the manifest is present
BigInteger nonce = storeTransaction.getNonceUncommitted(caller);
StorageReference validators = storeTransaction.getValidatorsUncommitted().get(); // ok, since the manifest is present
TransactionReference takamakaCode = validators.getTransaction(); // TODO: refer to getTakamakaCodeUncommitted() of the store transaction later

// we determine how many coins have been minted during the last reward:
Expand All @@ -804,7 +799,7 @@ public final boolean rewardValidators(String behaving, String misbehaving) {
// as final supply: in that case we truncate the minted coins so that the current
// supply reaches the final supply, exactly; this might occur from below (positive inflation)
// or from above (negative inflation)
BigInteger currentSupply = storeUtilities.getCurrentSupplyUncommitted(validators);
BigInteger currentSupply = storeTransaction.getCurrentSupplyUncommitted(validators);
if (minted.signum() > 0) {
BigInteger finalSupply = caches.getConsensusParams().getFinalSupply();
BigInteger extra = finalSupply.subtract(currentSupply.add(minted));
Expand Down Expand Up @@ -993,10 +988,6 @@ Optional<StorageReference> getManifestUncommitted() throws StoreException {
return store.getManifest();
}

public StoreUtility getStoreUtilities() {
return storeUtilities;
}

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

0 comments on commit 0af5b4d

Please sign in to comment.