Skip to content

Commit

Permalink
Added the node inside the stores
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed May 5, 2024
1 parent fe4b2ca commit d53a14d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public DiskNodeConfig getLocalConfig() {

@Override
protected DiskStore mkStore() {
return new DiskStore(getLocalConfig().getDir());
return new DiskStore(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* while the histories are kept in RAM.
*/
@Immutable
class DiskStore extends AbstractStore<DiskStore> {
class DiskStore extends AbstractStore<DiskStore, DiskNodeImpl> {

/**
* The path where the database of the store gets created.
Expand Down Expand Up @@ -80,8 +80,10 @@ class DiskStore extends AbstractStore<DiskStore> {
*
* @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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,39 @@
import io.hotmoka.node.local.api.EngineClassLoader;
import io.hotmoka.node.local.api.Store;

public abstract class AbstractStore<T extends AbstractStore<T>> implements Store<T> {
public abstract class AbstractStore<T extends AbstractStore<T, N>, N extends AbstractLocalNode<?, T>> implements Store<T> {

/**
* 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<SignedTransactionRequest<?>, Boolean> checkedSignatures;
final LRUCache<SignedTransactionRequest<?>, Boolean> checkedSignatures; // TODO: possibly store by reference

/**
* The cache for the class loaders. This can be shared across distinct stores since
* jars installed in store remain valid over time.
*/
final LRUCache<TransactionReference, EngineClassLoader> 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<T> toClone) {
protected AbstractStore(AbstractStore<T, N> toClone) {
this.node = toClone.node;
this.checkedSignatures = toClone.checkedSignatures;
this.classLoaders = toClone.classLoaders;
}

protected final N getNode() {
return node;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends AbstractStore<T>> implements StoreTransaction<T> {
public abstract class AbstractStoreTransaction<T extends AbstractStore<T, ?>> implements StoreTransaction<T> {
private final static Logger LOGGER = Logger.getLogger(AbstractStoreTransaction.class.getName());
private final T store;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T extends AbstractTrieBasedStore<T>> extends AbstractStore<T> {
public abstract class AbstractTrieBasedStore<T extends AbstractTrieBasedStore<T, N>, N extends AbstractLocalNode<?, T>> extends AbstractStore<T, N> {

/**
* The Xodus environment that holds the store.
Expand Down Expand Up @@ -131,8 +130,10 @@ public abstract class AbstractTrieBasedStore<T extends AbstractTrieBasedStore<T>
*
* @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<io.hotmoka.xodus.env.Store>();
var roots = new AtomicReference<Optional<byte[]>>();
Expand Down Expand Up @@ -192,7 +193,7 @@ protected AbstractTrieBasedStore(Path dir) {
}
}

protected AbstractTrieBasedStore(AbstractTrieBasedStore<T> toClone) {
protected AbstractTrieBasedStore(AbstractTrieBasedStore<T, N> toClone) {
super(toClone);

this.env = toClone.env;
Expand All @@ -208,7 +209,7 @@ protected AbstractTrieBasedStore(AbstractTrieBasedStore<T> toClone) {
this.rootOfRequests = toClone.rootOfRequests;
}

protected AbstractTrieBasedStore(AbstractTrieBasedStore<T> toClone, Optional<byte[]> rootOfResponses, Optional<byte[]> rootOfInfo, Optional<byte[]> rootOfErrors, Optional<byte[]> rootOfHistories, Optional<byte[]> rootOfRequests) {
protected AbstractTrieBasedStore(AbstractTrieBasedStore<T, N> toClone, Optional<byte[]> rootOfResponses, Optional<byte[]> rootOfInfo, Optional<byte[]> rootOfErrors, Optional<byte[]> rootOfHistories, Optional<byte[]> rootOfRequests) {
super(toClone);

this.env = toClone.env;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import io.hotmoka.xodus.ExodusException;
import io.hotmoka.xodus.env.Transaction;

public abstract class AbstractTrieBasedStoreTransaction<T extends AbstractTrieBasedStore<T>> extends AbstractStoreTransaction<T> {
public abstract class AbstractTrieBasedStoreTransaction<T extends AbstractTrieBasedStore<T, ?>> extends AbstractStoreTransaction<T> {

/**
* The Xodus transaction where the updates get recorded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public TendermintPoster getPoster() {

@Override
protected TendermintStore mkStore() {
return new TendermintStore(config.getDir(), this);
return new TendermintStore(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,12 +34,7 @@
* Tendermint, since it keeps such information inside its blocks.
*/
@Immutable
public class TendermintStore extends AbstractTrieBasedStore<TendermintStore> {

/**
* An object that can be used to send post requests to Tendermint
*/
private final TendermintNodeImpl node;
public class TendermintStore extends AbstractTrieBasedStore<TendermintStore, TendermintNodeImpl> {

/**
* The hasher used to merge the hashes of the many tries.
Expand All @@ -51,13 +45,10 @@ public class TendermintStore extends AbstractTrieBasedStore<TendermintStore> {
* 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());
Expand All @@ -70,20 +61,19 @@ public class TendermintStore extends AbstractTrieBasedStore<TendermintStore> {
private TendermintStore(TendermintStore toClone, Optional<byte[]> rootOfResponses, Optional<byte[]> rootOfInfo, Optional<byte[]> rootOfErrors, Optional<byte[]> rootOfHistories, Optional<byte[]> rootOfRequests) {
super(toClone, rootOfResponses, rootOfInfo, rootOfErrors, rootOfHistories, rootOfRequests);

this.node = toClone.node;
this.hasherOfHashes = toClone.hasherOfHashes;
}

@Override
public Optional<String> 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<TransactionRequest<?>> getRequest(TransactionReference reference) {
// requests are held inside the Tendermint blockchain
return node.getPoster().getRequest(reference.getHash());
return getNode().getPoster().getRequest(reference.getHash());
}

/**
Expand Down

0 comments on commit d53a14d

Please sign in to comment.