From d53a14df0268cdf64b2cb7302f39687d85870290 Mon Sep 17 00:00:00 2001 From: Fausto Spoto Date: Sun, 5 May 2024 13:38:08 +0200 Subject: [PATCH] Added the node inside the stores --- .../node/disk/internal/DiskNodeImpl.java | 2 +- .../hotmoka/node/disk/internal/DiskStore.java | 8 +++++--- .../io/hotmoka/node/local/AbstractStore.java | 19 ++++++++++++++---- .../node/local/AbstractStoreTransaction.java | 2 +- .../node/local/AbstractTrieBasedStore.java | 13 ++++++------ .../AbstractTrieBasedStoreTransaction.java | 2 +- .../internal/TendermintNodeImpl.java | 2 +- .../tendermint/internal/TendermintStore.java | 20 +++++-------------- 8 files changed, 36 insertions(+), 32 deletions(-) diff --git a/io-hotmoka-node-disk/src/main/java/io/hotmoka/node/disk/internal/DiskNodeImpl.java b/io-hotmoka-node-disk/src/main/java/io/hotmoka/node/disk/internal/DiskNodeImpl.java index 39e7dcf8c..0dd1e41eb 100644 --- a/io-hotmoka-node-disk/src/main/java/io/hotmoka/node/disk/internal/DiskNodeImpl.java +++ b/io-hotmoka-node-disk/src/main/java/io/hotmoka/node/disk/internal/DiskNodeImpl.java @@ -81,7 +81,7 @@ public DiskNodeConfig getLocalConfig() { @Override protected DiskStore mkStore() { - return new DiskStore(getLocalConfig().getDir()); + return new DiskStore(this); } @Override diff --git a/io-hotmoka-node-disk/src/main/java/io/hotmoka/node/disk/internal/DiskStore.java b/io-hotmoka-node-disk/src/main/java/io/hotmoka/node/disk/internal/DiskStore.java index 01f050244..17a1924d6 100644 --- a/io-hotmoka-node-disk/src/main/java/io/hotmoka/node/disk/internal/DiskStore.java +++ b/io-hotmoka-node-disk/src/main/java/io/hotmoka/node/disk/internal/DiskStore.java @@ -47,7 +47,7 @@ * while the histories are kept in RAM. */ @Immutable -class DiskStore extends AbstractStore { +class DiskStore extends AbstractStore { /** * The path where the database of the store gets created. @@ -80,8 +80,10 @@ class DiskStore extends AbstractStore { * * @param dir the path where the database of the store gets created */ - DiskStore(Path dir) { - this.dir = dir; + DiskStore(DiskNodeImpl node) { + super(node); + + this.dir = node.getLocalConfig().getDir(); this.requests = new ConcurrentHashMap<>(); this.responses = new ConcurrentHashMap<>(); this.histories = new ConcurrentHashMap<>(); diff --git a/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractStore.java b/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractStore.java index 0cc63bfb1..b7616de07 100644 --- a/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractStore.java +++ b/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractStore.java @@ -21,14 +21,14 @@ import io.hotmoka.node.local.api.EngineClassLoader; import io.hotmoka.node.local.api.Store; -public abstract class AbstractStore> implements Store { +public abstract class AbstractStore, N extends AbstractLocalNode> implements Store { /** * Cached recent requests that have had their signature checked. * This can be shared across distinct stores since valid signatures * remain valid over time. */ - final LRUCache, Boolean> checkedSignatures; + final LRUCache, Boolean> checkedSignatures; // TODO: possibly store by reference /** * The cache for the class loaders. This can be shared across distinct stores since @@ -36,13 +36,24 @@ public abstract class AbstractStore> implements Store */ final LRUCache classLoaders; - protected AbstractStore() { + /** + * The node having this store. + */ + private final N node; + + protected AbstractStore(N node) { + this.node = node; this.checkedSignatures = new LRUCache<>(100, 1000); this.classLoaders = new LRUCache<>(100, 1000); } - protected AbstractStore(AbstractStore toClone) { + protected AbstractStore(AbstractStore toClone) { + this.node = toClone.node; this.checkedSignatures = toClone.checkedSignatures; this.classLoaders = toClone.classLoaders; } + + protected final N getNode() { + return node; + } } \ No newline at end of file diff --git a/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractStoreTransaction.java b/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractStoreTransaction.java index 8369d4cc5..16a5e837a 100644 --- a/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractStoreTransaction.java +++ b/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractStoreTransaction.java @@ -65,7 +65,7 @@ * its hash is held in the node, if consensus is needed. Stores must be thread-safe, since they can * be used concurrently for executing more requests. */ -public abstract class AbstractStoreTransaction> implements StoreTransaction { +public abstract class AbstractStoreTransaction> implements StoreTransaction { private final static Logger LOGGER = Logger.getLogger(AbstractStoreTransaction.class.getName()); private final T store; diff --git a/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractTrieBasedStore.java b/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractTrieBasedStore.java index 9c4bc50ef..35d024669 100644 --- a/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractTrieBasedStore.java +++ b/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractTrieBasedStore.java @@ -16,7 +16,6 @@ package io.hotmoka.node.local; -import java.nio.file.Path; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Stream; @@ -62,7 +61,7 @@ * This class is meant to be subclassed by specifying where errors, requests and histories are kept. */ @Immutable -public abstract class AbstractTrieBasedStore> extends AbstractStore { +public abstract class AbstractTrieBasedStore, N extends AbstractLocalNode> extends AbstractStore { /** * The Xodus environment that holds the store. @@ -131,8 +130,10 @@ public abstract class AbstractTrieBasedStore * * @param dir the path where the database of the store is kept */ - protected AbstractTrieBasedStore(Path dir) { - this.env = new Environment(dir + "/store"); + protected AbstractTrieBasedStore(N node) { + super(node); + + this.env = new Environment(node.getLocalNodeConfig().getDir() + "/store"); var storeOfInfo = new AtomicReference(); var roots = new AtomicReference>(); @@ -192,7 +193,7 @@ protected AbstractTrieBasedStore(Path dir) { } } - protected AbstractTrieBasedStore(AbstractTrieBasedStore toClone) { + protected AbstractTrieBasedStore(AbstractTrieBasedStore toClone) { super(toClone); this.env = toClone.env; @@ -208,7 +209,7 @@ protected AbstractTrieBasedStore(AbstractTrieBasedStore toClone) { this.rootOfRequests = toClone.rootOfRequests; } - protected AbstractTrieBasedStore(AbstractTrieBasedStore toClone, Optional rootOfResponses, Optional rootOfInfo, Optional rootOfErrors, Optional rootOfHistories, Optional rootOfRequests) { + protected AbstractTrieBasedStore(AbstractTrieBasedStore toClone, Optional rootOfResponses, Optional rootOfInfo, Optional rootOfErrors, Optional rootOfHistories, Optional rootOfRequests) { super(toClone); this.env = toClone.env; diff --git a/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractTrieBasedStoreTransaction.java b/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractTrieBasedStoreTransaction.java index 470c1f81d..f97fad8cd 100644 --- a/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractTrieBasedStoreTransaction.java +++ b/io-hotmoka-node-local/src/main/java/io/hotmoka/node/local/AbstractTrieBasedStoreTransaction.java @@ -17,7 +17,7 @@ import io.hotmoka.xodus.ExodusException; import io.hotmoka.xodus.env.Transaction; -public abstract class AbstractTrieBasedStoreTransaction> extends AbstractStoreTransaction { +public abstract class AbstractTrieBasedStoreTransaction> extends AbstractStoreTransaction { /** * The Xodus transaction where the updates get recorded. diff --git a/io-hotmoka-node-tendermint/src/main/java/io/hotmoka/node/tendermint/internal/TendermintNodeImpl.java b/io-hotmoka-node-tendermint/src/main/java/io/hotmoka/node/tendermint/internal/TendermintNodeImpl.java index 812be4832..3808f69c8 100644 --- a/io-hotmoka-node-tendermint/src/main/java/io/hotmoka/node/tendermint/internal/TendermintNodeImpl.java +++ b/io-hotmoka-node-tendermint/src/main/java/io/hotmoka/node/tendermint/internal/TendermintNodeImpl.java @@ -229,7 +229,7 @@ public TendermintPoster getPoster() { @Override protected TendermintStore mkStore() { - return new TendermintStore(config.getDir(), this); + return new TendermintStore(this); } @Override diff --git a/io-hotmoka-node-tendermint/src/main/java/io/hotmoka/node/tendermint/internal/TendermintStore.java b/io-hotmoka-node-tendermint/src/main/java/io/hotmoka/node/tendermint/internal/TendermintStore.java index 8496c9dc9..78ea9cfd7 100644 --- a/io-hotmoka-node-tendermint/src/main/java/io/hotmoka/node/tendermint/internal/TendermintStore.java +++ b/io-hotmoka-node-tendermint/src/main/java/io/hotmoka/node/tendermint/internal/TendermintStore.java @@ -16,7 +16,6 @@ package io.hotmoka.node.tendermint.internal; -import java.nio.file.Path; import java.security.NoSuchAlgorithmException; import java.util.Optional; import java.util.function.Function; @@ -35,12 +34,7 @@ * Tendermint, since it keeps such information inside its blocks. */ @Immutable -public class TendermintStore extends AbstractTrieBasedStore { - - /** - * An object that can be used to send post requests to Tendermint - */ - private final TendermintNodeImpl node; +public class TendermintStore extends AbstractTrieBasedStore { /** * The hasher used to merge the hashes of the many tries. @@ -51,13 +45,10 @@ public class TendermintStore extends AbstractTrieBasedStore { * Creates a store for the Tendermint blockchain. * It is initialized to the view of the last checked out root. * - * @param dir the path where the database of the store gets created * @param node an object that can be used to send post requests to Tendermint */ - TendermintStore(Path dir, TendermintNodeImpl node) { - super(dir); - - this.node = node; + TendermintStore(TendermintNodeImpl node) { + super(node); try { this.hasherOfHashes = HashingAlgorithms.sha256().getHasher(Function.identity()); @@ -70,20 +61,19 @@ public class TendermintStore extends AbstractTrieBasedStore { private TendermintStore(TendermintStore toClone, Optional rootOfResponses, Optional rootOfInfo, Optional rootOfErrors, Optional rootOfHistories, Optional rootOfRequests) { super(toClone, rootOfResponses, rootOfInfo, rootOfErrors, rootOfHistories, rootOfRequests); - this.node = toClone.node; this.hasherOfHashes = toClone.hasherOfHashes; } @Override public Optional getError(TransactionReference reference) { // error messages are held inside the Tendermint blockchain - return node.getPoster().getErrorMessage(reference.getHash()); + return getNode().getPoster().getErrorMessage(reference.getHash()); } @Override public Optional> getRequest(TransactionReference reference) { // requests are held inside the Tendermint blockchain - return node.getPoster().getRequest(reference.getHash()); + return getNode().getPoster().getRequest(reference.getHash()); } /**