Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.unicitylabs.sdk.api.bft.trustbase;

/** The compare-and-swap lost the race on every attempt; retry later. */
public class CacheContendedException extends TrustBaseException {

/**
* Create the exception.
*
* @param message what failed
*/
public CacheContendedException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.unicitylabs.sdk.api.bft.trustbase;

/** A non-fatal observation of open or flush; the application decides how to present it. */
public final class CacheDiagnostic {

private final CacheRejection rejection;
private final Throwable cause;

private CacheDiagnostic(CacheRejection rejection, Throwable cause) {
this.rejection = rejection;
this.cause = cause;
}

/**
* The cache was rejected and the embedded history used.
*
* @param rejection why
* @return the diagnostic
*/
public static CacheDiagnostic rejected(CacheRejection rejection) {
return new CacheDiagnostic(rejection, null);
}

/**
* The cache could not be read; the embedded history is used.
*
* @param cause the failure
* @return the diagnostic
*/
public static CacheDiagnostic unavailable(Throwable cause) {
return new CacheDiagnostic(null, cause);
}

/**
* Tell whether the cache was rejected (as opposed to unreadable).
*
* @return true for a rejection
*/
public boolean isRejection() {
return this.rejection != null;
}

/**
* Get why the cache was rejected.
*
* @return the reason, or null when the cache was unreadable
*/
public CacheRejection getRejection() {
return this.rejection;
}

/**
* Get the failure that made the cache unreadable.
*
* @return the failure, or null for a rejection
*/
public Throwable getCause() {
return this.cause;
}

@Override
public boolean equals(Object o) {
return o instanceof CacheDiagnostic && ((CacheDiagnostic) o).rejection == this.rejection
&& ((CacheDiagnostic) o).cause == this.cause;
}

@Override
public int hashCode() {
return java.util.Objects.hash(this.rejection, this.cause);
}

@Override
public String toString() {
return this.rejection != null ? "CacheRejected:" + this.rejection : "CacheUnavailable";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.unicitylabs.sdk.api.bft.trustbase;

/**
* The stored cache is a valid history that must not be overwritten (over the budget, or an
* authenticated chain diverging at or before the checkpoint): nothing is written; the application
* decides.
*/
public class CacheRejectedException extends TrustBaseException {

private final CacheRejection reason;

/**
* Create the exception.
*
* @param reason why the cache is kept
*/
public CacheRejectedException(CacheRejection reason) {
super("Cache rejected: " + reason + ".");
this.reason = reason;
}

/**
* Get why the cache is kept.
*
* @return the reason
*/
public CacheRejection getReason() {
return this.reason;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.unicitylabs.sdk.api.bft.trustbase;

/** Why a cache was not used. */
public enum CacheRejection {
/** Not a complete canonical bundle that verifies strictly from its genesis. */
MALFORMED,
/** A bundle of another network. */
WRONG_NETWORK,
/** A bundle of another genesis. */
WRONG_GENESIS,
/** Diverges from, or does not reach, the release checkpoint. */
PIN_MISMATCH,
/** Larger than the read budget. */
OVERSIZE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.unicitylabs.sdk.api.bft.trustbase;

import java.util.Collections;
import java.util.List;

/** The outcome of {@link TrustBaseSession#flush}. A failed save does not undo the verified in-memory history. */
public final class FlushResult {

private final boolean saved;
private final boolean wrote;
private final List<CacheDiagnostic> diagnostics;
private final Throwable error;

private FlushResult(boolean saved, boolean wrote, List<CacheDiagnostic> diagnostics,
Throwable error) {
this.saved = saved;
this.wrote = wrote;
this.diagnostics = Collections.unmodifiableList(diagnostics);
this.error = error;
}

static FlushResult saved(boolean wrote, List<CacheDiagnostic> diagnostics) {
return new FlushResult(true, wrote, diagnostics, null);
}

static FlushResult pending(Throwable error) {
return new FlushResult(false, false, Collections.emptyList(), error);
}

/**
* Tell whether the store holds the session's complete history.
*
* @return true if saved; false if the session stays dirty (and verified) and may retry
*/
public boolean isSaved() {
return this.saved;
}

/**
* Tell whether bytes were written.
*
* @return false when the store already had everything, or nothing here was unsaved
*/
public boolean isWrote() {
return this.wrote;
}

/**
* Get a damaged cache the flush found and replaced, reported before the replacement.
*
* @return the diagnostics
*/
public List<CacheDiagnostic> getDiagnostics() {
return this.diagnostics;
}

/**
* Get why nothing was saved.
*
* @return the failure, or null when saved
*/
public Throwable getError() {
return this.error;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.unicitylabs.sdk.api.bft.trustbase;

/**
* The session is conflicted by an authenticated fork after the release checkpoint: every append,
* refresh and verification fails, and nothing is written. The evidence is
* {@link #getConflict()}. The latch belongs to the live session only.
*/
public class HistoryConflictedException extends TrustBaseException {

private final TrustBaseConflictException conflict;

/**
* Create the exception.
*
* @param conflict the fork that closed the session, with its evidence
*/
public HistoryConflictedException(TrustBaseConflictException conflict) {
super("The history is conflicted at epoch " + Long.toUnsignedString(conflict.getEpoch()) + ".",
conflict);
this.conflict = conflict;
}

/**
* Get the fork that closed the session.
*
* @return the conflict, with its evidence
*/
public TrustBaseConflictException getConflict() {
return this.conflict;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.unicitylabs.sdk.api.bft.trustbase;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/** The reference store: a map in memory, copying on the way in and out, with the same contract as any other. */
public final class MemoryTrustBaseStore implements TrustBaseStore {

private final Map<String, byte[]> values = new HashMap<>();

/**
* Put bytes under a key regardless of the revision (a competing writer, in tests).
*
* @param key the key
* @param bytes the bytes, copied
*/
public synchronized void put(String key, byte[] bytes) {
this.values.put(key, Arrays.copyOf(bytes, bytes.length));
}

/**
* Get a copy of the bytes under a key.
*
* @param key the key
* @return the bytes, or null when absent
*/
public synchronized byte[] get(String key) {
byte[] held = this.values.get(key);
return held == null ? null : Arrays.copyOf(held, held.length);
}

@Override
public synchronized CompletableFuture<Optional<Stored>> read(String key, int maxBytes) {
Objects.requireNonNull(key, "key cannot be null");
byte[] held = this.values.get(key);
if (held == null) {
return CompletableFuture.completedFuture(Optional.empty());
}
if (held.length > maxBytes) {
return CompletableFuture.failedFuture(new TrustBaseStoreException(
TrustBaseStoreException.Reason.OVERSIZE,
held.length + " bytes exceed the budget of " + maxBytes + "."));
}
return CompletableFuture.completedFuture(Optional.of(
new Stored(Arrays.copyOf(held, held.length), TrustBaseCacheKey.revision(held))));
}

@Override
public synchronized CompletableFuture<CasOutcome> compareAndSwap(String key,
String expectedRevision, byte[] bytes) {
Objects.requireNonNull(key, "key cannot be null");
Objects.requireNonNull(bytes, "bytes cannot be null");
byte[] held = this.values.get(key);
String current = held == null ? null : TrustBaseCacheKey.revision(held);
if (!Objects.equals(current, expectedRevision)) {
return CompletableFuture.completedFuture(CasOutcome.CHANGED);
}
this.values.put(key, Arrays.copyOf(bytes, bytes.length));
return CompletableFuture.completedFuture(CasOutcome.WRITTEN);
}
}
Loading
Loading