From 68fdd63a77763db50d3075b501df7c9cb807a6b5 Mon Sep 17 00:00:00 2001 From: Fausto Spoto Date: Fri, 10 May 2024 10:14:45 +0200 Subject: [PATCH] getHIstory gives exception now if the object is unknown --- .../hotmoka/node/disk/internal/DiskStore.java | 3 +-- .../java/io/hotmoka/node/local/api/Store.java | 2 +- .../node/local/AbstractStoreTransaction.java | 24 +++++++++---------- 3 files changed, 13 insertions(+), 16 deletions(-) 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 1ebd10e10..752384a53 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 @@ -149,8 +149,7 @@ public Stream getHistory(StorageReference object) throws U if (history != null) return Stream.of(history); else - return Stream.empty(); - //throw new UnknownReferenceException(object); // TODO + throw new UnknownReferenceException(object); } @Override diff --git a/io-hotmoka-node-local-api/src/main/java/io/hotmoka/node/local/api/Store.java b/io-hotmoka-node-local-api/src/main/java/io/hotmoka/node/local/api/Store.java index 3a33a7c55..0d524a751 100644 --- a/io-hotmoka-node-local-api/src/main/java/io/hotmoka/node/local/api/Store.java +++ b/io-hotmoka-node-local-api/src/main/java/io/hotmoka/node/local/api/Store.java @@ -68,7 +68,7 @@ public interface Store, T extends StoreTransaction> * that can be used to reconstruct the current values of its fields. * * @param object the reference of the object - * @return the history. Yields an empty stream if there is no history for {@code object} + * @return the history * @throws StoreException if the store is not able to perform the operation */ Stream getHistory(StorageReference object) throws UnknownReferenceException, StoreException; 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 52cfcfcb6..59f2a048d 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 @@ -1271,12 +1271,17 @@ private void expandHistory(TransactionReference reference, TransactionResponseWi * @return the simplified history, with {@code added} in front followed by a subset of the old history */ private Stream simplifiedHistory(StorageReference object, TransactionReference added, Stream addedUpdates) throws StoreException { + // if the object has been created at the added transaction, that is its history + if (object.getTransaction().equals(added)) + return Stream.of(added); + Stream old; - + try { old = getHistoryUncommitted(object); } catch (UnknownReferenceException e) { + // the object was created before this transaction: it must have a history or otherwise the store is corrupted throw new StoreException("The computed response reports a modified object that is not in store", e); } @@ -1287,13 +1292,13 @@ private Stream simplifiedHistory(StorageReference object, simplified.add(added); var oldAsArray = old.toArray(TransactionReference[]::new); - int length = oldAsArray.length; - for (int pos = 0; pos < length - 1; pos++) + int lastPos = oldAsArray.length - 1; + for (int pos = 0; pos < lastPos; pos++) addIfUncovered(oldAsArray[pos], object, covered, simplified); // the last is always useful, since it contains at least the class tag of the object - if (length >= 1) - simplified.add(oldAsArray[length - 1]); + if (lastPos >= 0) + simplified.add(oldAsArray[lastPos]); return simplified.stream(); } @@ -1315,15 +1320,8 @@ private void addIfUncovered(TransactionReference reference, StorageReference obj else if (maybeResponse.get() instanceof TransactionResponseWithUpdates trwu) { // we check if there is at least an update for a field of the object // that is not yet covered by another update in a previous element of the history - Set diff = trwu.getUpdates() - .filter(update -> update.getObject().equals(object) && covered.stream().noneMatch(update::sameProperty)) - .collect(Collectors.toSet()); - - if (!diff.isEmpty()) { - // the transaction reference actually adds at least one useful update + if (trwu.getUpdates().filter(update -> update.getObject().equals(object) && covered.stream().noneMatch(update::sameProperty) && covered.add(update)).count() > 0) history.add(reference); - covered.addAll(diff); - } } else throw new StoreException("The history contains a reference to a transaction without updates");