Skip to content

Commit

Permalink
Clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed May 6, 2024
1 parent 49b7854 commit 6208718
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

import io.hotmoka.node.TransactionReferences;
import io.hotmoka.node.api.TransactionRejectedException;
Expand Down Expand Up @@ -63,7 +64,7 @@ class Mempool {
*/
private final Thread deliverer;

private final Set<TransactionReference> completed = ConcurrentHashMap.newKeySet();
private final Set<TransactionReference> includedInBlock = ConcurrentHashMap.newKeySet();

private final int transactionsPerBlock;

Expand Down Expand Up @@ -115,14 +116,10 @@ private void check() {
}
}
catch (TransactionRejectedException e) {
synchronized (completed) {
completed.add(TransactionReferences.of(node.getHasher().hash(current)));
}
node.signalOutcomeIsReady(Stream.of(TransactionReferences.of(node.getHasher().hash(current))));
}
catch (Throwable t) {
synchronized (completed) {
completed.add(TransactionReferences.of(node.getHasher().hash(current)));
}
node.signalOutcomeIsReady(Stream.of(TransactionReferences.of(node.getHasher().hash(current))));
logger.log(Level.WARNING, "Failed to check transaction request", t);
}
}
Expand All @@ -147,12 +144,8 @@ private void deliver() {
if (counter > 0)
node.rewardValidators("", "");
node.setStore(transaction.commit());

synchronized (completed) {
node.signalOutcomeIsReady(completed.stream());
completed.clear();
}

node.signalOutcomeIsReady(includedInBlock.stream());
includedInBlock.clear();
transaction.notifyAllEvents(node::notifyEvent);
transaction = node.getStore().beginTransaction(System.currentTimeMillis());
node.transaction = transaction;
Expand All @@ -163,9 +156,7 @@ private void deliver() {
node.deliverTransaction(current);
}
finally {
synchronized (completed) {
completed.add(TransactionReferences.of(node.getHasher().hash(current)));
}
includedInBlock.add(TransactionReferences.of(node.getHasher().hash(current)));
}

counter = (counter + 1) % transactionsPerBlock; // TODO: transactionsPerBlock should be int
Expand All @@ -174,11 +165,8 @@ private void deliver() {
if (counter > 0)
node.rewardValidators("", "");
node.setStore(transaction.commit());

synchronized (completed) {
node.signalOutcomeIsReady(completed.stream());
}

node.signalOutcomeIsReady(includedInBlock.stream());
includedInBlock.clear();
transaction.notifyAllEvents(node::notifyEvent);
transaction = node.getStore().beginTransaction(System.currentTimeMillis());
node.transaction = transaction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ else if (request instanceof JarStoreTransactionRequest jstr)
else if (request instanceof ConstructorCallTransactionRequest cctr)
return new ConstructorCallResponseBuilder(reference, cctr, this);
else if (request instanceof AbstractInstanceMethodCallTransactionRequest aimctr)
return new InstanceMethodCallResponseBuilder(reference, aimctr, this);
return new InstanceMethodCallResponseBuilder(reference, aimctr, this);
else if (request instanceof StaticMethodCallTransactionRequest smctr)
return new StaticMethodCallResponseBuilder(reference, smctr, this);
else if (request instanceof InitializationTransactionRequest itr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ private Optional<String> getRecentCheckTransactionErrorFor(TransactionReference
return Optional.ofNullable(recentCheckTransactionErrors.get(reference));
}

private void storeCheckTransactionError(TransactionReference reference, Throwable e) {
recentCheckTransactionErrors.put(reference, trimmedMessage(e));
}

/**
* Adds, to the given set, the updates of the fields of the object at the given reference,
* occurred during the execution of a given transaction.
Expand Down Expand Up @@ -651,46 +655,38 @@ public final MethodFuture postStaticMethodCallTransaction(StaticMethodCallTransa
*/
public final void checkTransaction(TransactionRequest<?> request) throws TransactionRejectedException {
var reference = TransactionReferences.of(hasher.hash(request));
getRecentCheckTransactionErrorFor(reference).ifPresent(TransactionRejectedException::new);

try {
LOGGER.info(reference + ": checking start (" + request.getClass().getSimpleName() + ')');

var previousError = recentCheckTransactionErrors.get(reference);
if (previousError != null)
throw new TransactionRejectedException(previousError);

var storeTransaction = store.beginTransaction(System.currentTimeMillis());
storeTransaction.responseBuilderFor(reference, request);
storeTransaction.abort();

LOGGER.info(reference + ": checking success");
}
catch (TransactionRejectedException e) {
// we wake up who was waiting for the outcome of the request
//signalSemaphore(reference);
// we do not store the error message, since a failed checkTransaction
// means that nobody is paying for this and we cannot expand the store;
// we just take note of the failure to avoid polling for the response
recentCheckTransactionErrors.put(reference, trimmedMessage(e));
storeCheckTransactionError(reference, e);
LOGGER.warning(reference + ": checking failed: " + trimmedMessage(e));
throw e;
}
catch (StoreException e) { // TODO: probably becomes NodeException
// we wake up who was waiting for the outcome of the request
//signalSemaphore(reference);
// we do not store the error message, since a failed checkTransaction
// means that nobody is paying for this and we cannot expand the store;
// we just take note of the failure to avoid polling for the response
recentCheckTransactionErrors.put(reference, trimmedMessage(e));
storeCheckTransactionError(reference, e);
LOGGER.log(Level.WARNING, reference + ": checking failed with unexpected exception", e);
throw new RuntimeException(e);
}
catch (RuntimeException e) {
// we wake up who was waiting for the outcome of the request
//signalSemaphore(reference);
// we do not store the error message, since a failed checkTransaction
// means that nobody is paying for this and we cannot expand the store;
// we just take note of the failure to avoid polling for the response
recentCheckTransactionErrors.put(reference, trimmedMessage(e));
storeCheckTransactionError(reference, e);
LOGGER.log(Level.WARNING, reference + ": checking failed with unexpected exception", e);
throw e;
}
Expand Down Expand Up @@ -786,7 +782,7 @@ public final boolean rewardValidators(String behaving, String misbehaving) {
StorageReference caller = manifest.get();
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
TransactionReference takamakaCode = getStoreTransaction().getTakamakaCodeUncommitted().get();

// we determine how many coins have been minted during the last reward:
// it is the price of the gas distributed minus the same price without inflation
Expand Down Expand Up @@ -816,7 +812,6 @@ else if (minted.signum() < 0) {
StorageValues.stringOf(behaving), StorageValues.stringOf(misbehaving),
StorageValues.bigIntegerOf(gasConsumedSinceLastReward), StorageValues.bigIntegerOf(numberOfTransactionsSinceLastReward));

checkTransaction(request);
ResponseBuilder<?,?> responseBuilder = getStoreTransaction().responseBuilderFor(TransactionReferences.of(hasher.hash(request)), request);
TransactionResponse response = responseBuilder.getResponse();
// if there is only one update, it is the update of the nonce of the manifest: we prefer not to expand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class TendermintApplication extends ABCI {
*/
private volatile TendermintValidator[] validatorsAtPreviousBlock;

private final Set<TransactionReference> completed = ConcurrentHashMap.newKeySet();
private final Set<TransactionReference> includedInBlock = ConcurrentHashMap.newKeySet();

/**
* The current transaction, if any.
Expand Down Expand Up @@ -218,10 +218,7 @@ protected ResponseCheckTx checkTx(RequestCheckTx request) {
node.checkTransaction(hotmokaRequest);
}
catch (Throwable t) {
synchronized (completed) {
completed.add(TransactionReferences.of(node.getHasher().hash(hotmokaRequest)));
}

node.signalOutcomeIsReady(Stream.of(TransactionReferences.of(node.getHasher().hash(hotmokaRequest))));
throw t;
}

Expand Down Expand Up @@ -269,9 +266,7 @@ protected ResponseDeliverTx deliverTx(RequestDeliverTx request) {
node.deliverTransaction(hotmokaRequest);
}
finally {
synchronized (completed) {
completed.add(TransactionReferences.of(node.getHasher().hash(hotmokaRequest)));
}
includedInBlock.add(TransactionReferences.of(node.getHasher().hash(hotmokaRequest)));
}

responseBuilder.setCode(0);
Expand Down Expand Up @@ -318,15 +313,8 @@ protected ResponseCommit commit(RequestCommit request) {
var newStore = transaction.commit();
node.setStore(newStore);
newStore.moveRootBranchToThis();

Stream<TransactionReference> toSignal;

synchronized (completed) {
toSignal = completed.stream();
completed.clear();
}

node.signalOutcomeIsReady(toSignal);
node.signalOutcomeIsReady(includedInBlock.stream());
includedInBlock.clear();
transaction.notifyAllEvents(node::notifyEvent);
}
catch (StoreException e) {
Expand Down

0 comments on commit 6208718

Please sign in to comment.