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
@@ -1,18 +1,77 @@
package org.unicitylabs.sdk.api.bft.trustbase;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.unicitylabs.sdk.api.NetworkId;
import org.unicitylabs.sdk.util.HexConverter;

/**
* The release table of this SDK build, one row per public network. It is empty until the release
* assets are generated and approved (PR 8 of the Java parity stack); a session can still be opened
* over an explicit table, a private pin or a local bundle.
* The release table of this SDK build, one row per public network: the approved genesis bundles of
* unicity-ids (PR 11, source commit recorded in {@code release-pins/manifest.json}) with their
* digests, EntryIDs and epochs. The assets are resources of the library, byte-identical to the
* ones the JavaScript and Rust SDKs ship (the manifest has the same sha256). Nothing is fetched and
* nothing is trusted from beside a download: the table comes with the authenticated release.
*
* <p>The rows are not verified here; a session verifies the row it uses (digest, the full chain
* from the pinned genesis, both identities) when it opens, and a failure is fatal
* ({@link ReleasePinInvalidException}). The manifest is regenerated from the bundles by
* {@code ReleasePinsTest} (set {@code UTB_RELEASE_PINS_UPDATE=1}), which fails on any difference.
* A future pin must extend the previous checkpoint.
*/
public final class ReleasePins {

private static final String DIRECTORY = "/release-pins/";

/** The rows of the built-in table. */
public static final List<ReleasePin> ROWS = Collections.emptyList();
public static final List<ReleasePin> ROWS = load();

private ReleasePins() {
}

private static List<ReleasePin> load() {
JsonNode manifest = parse(read("manifest.json"));
if (manifest.path("version").asInt() != 1) {
throw new IllegalStateException("Unsupported release pin manifest version.");
}
List<ReleasePin> rows = new ArrayList<>();
for (JsonNode row : manifest.get("networks")) {
rows.add(new ReleasePin(NetworkId.fromId((short) row.get("networkId").asInt()),
Long.parseUnsignedLong(row.get("genesisEpoch").asText()),
HexConverter.decode(row.get("genesisEntryId").asText()),
Long.parseUnsignedLong(row.get("pinnedEpoch").asText()),
HexConverter.decode(row.get("pinnedEntryId").asText()),
read(row.get("bundle").asText()), HexConverter.decode(row.get("bundleSha256").asText())));
}
return Collections.unmodifiableList(rows);
}

private static JsonNode parse(byte[] bytes) {
try {
return new ObjectMapper().readTree(bytes);
} catch (IOException e) {
throw new IllegalStateException("The release pin manifest is not JSON.", e);
}
}

private static byte[] read(String name) {
try (InputStream in = ReleasePins.class.getResourceAsStream(DIRECTORY + name)) {
if (in == null) {
throw new IllegalStateException("Missing release asset " + name + ".");
}
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
return out.toByteArray();
} catch (IOException e) {
throw new IllegalStateException("Cannot read release asset " + name + ".", e);
}
}
}
30 changes: 30 additions & 0 deletions src/main/resources/release-pins/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"networks": [
{
"bundle": "unicity-trustbase.mainnet.cbor",
"bundleSha256": "9d01b8713cab42dab269e1ad6e244cf11e7f8c4b580682a98402db348b37bd52",
"genesisEntryId": "0b6f8143e19e2a585be3534e8f1062f36c3b9621e506158f10abfbc9a56f4e4c",
"genesisEpoch": "1",
"name": "mainnet",
"networkId": 1,
"pinnedEntryId": "0b6f8143e19e2a585be3534e8f1062f36c3b9621e506158f10abfbc9a56f4e4c",
"pinnedEpoch": "1"
},
{
"bundle": "unicity-trustbase.testnet2.cbor",
"bundleSha256": "5b0a4fb7bd6c2d71068c0e65659aa60733dcc9083e9290fc22c2dd8e8e60f08a",
"genesisEntryId": "4b49eb2bb0729051b89a48668fd6ef1fda284aad2e10b7d5f23aad4b5faef37f",
"genesisEpoch": "1",
"name": "testnet2",
"networkId": 4,
"pinnedEntryId": "4b49eb2bb0729051b89a48668fd6ef1fda284aad2e10b7d5f23aad4b5faef37f",
"pinnedEpoch": "1"
}
],
"source": {
"commit": "513dc4fcf01ee802a97d59a5bc4e50ba64786d45",
"pullRequest": 11,
"repository": "unicitynetwork/unicity-ids"
},
"version": 1
}
Binary file not shown.
Binary file not shown.
154 changes: 154 additions & 0 deletions src/test/java/org/unicitylabs/sdk/api/RealSealsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.unicitylabs.sdk.api;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.unicitylabs.sdk.api.NonInclusionVerificationException.Reason;
import org.unicitylabs.sdk.api.bft.UnicitySeal;
import org.unicitylabs.sdk.api.bft.trustbase.TrustBaseBundle;
import org.unicitylabs.sdk.api.bft.trustbase.TrustBaseBundleLimits;
import org.unicitylabs.sdk.api.bft.trustbase.TrustBaseHistory;
import org.unicitylabs.sdk.serializer.cbor.CborSerializer;
import org.unicitylabs.sdk.util.HexConverter;

/**
* A real seal of each existing network verifies through the V2 bundle that describes its live
* authority (owner &sect;44/&sect;46). The fixtures are those of sdk-rust (9e92ae2): per network the
* unicity-ids genesis as a one-entry bundle, and the answer of the network's public gateway (no key)
* to {@code get_non_inclusion_proof.v1} for a random state id, which carries a unicity certificate
* signed by the live validators. See {@code vectors/real-seals/README.md}. These prove that existing
* seals authenticate under the V2 genesis (epoch 1); they do not show a live epoch-2 handoff.
*/
class RealSealsTest {

private static final TrustBaseBundleLimits LIMITS = new TrustBaseBundleLimits(1 << 20, 16);
private static final String TESTNET2_ID =
"4b49eb2bb0729051b89a48668fd6ef1fda284aad2e10b7d5f23aad4b5faef37f";
private static final String MAINNET_ID =
"0b6f8143e19e2a585be3534e8f1062f36c3b9621e506158f10abfbc9a56f4e4c";

private static byte[] read(String name) {
try (InputStream in = RealSealsTest.class
.getResourceAsStream("/vectors/real-seals/" + name)) {
return in.readAllBytes();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}

private static byte[] hexOf(String name) {
return HexConverter.decode(new String(read(name), StandardCharsets.UTF_8).trim());
}

private static TrustBaseHistory historyOf(String network, String entryId) {
TrustBaseBundle bundle = TrustBaseBundle.decode(read("unicity-trustbase." + network + ".cbor"),
LIMITS);
return TrustBaseHistory.fromBundle(bundle, HexConverter.decode(entryId));
}

private static StateId stateIdOf(String network) {
return StateId.fromCbor(CborSerializer.encodeByteString(hexOf(network + ".stateid.hex")));
}

private static NonInclusionProof proofOf(String network, byte[] wire) {
return new NonInclusionProof(stateIdOf(network), wire);
}

/** Verify with the shard the gateway answered for pinned, as an application does for a sharded network. */
private static NonInclusionOutcome verify(NonInclusionProof proof, TrustBaseHistory history) {
NonInclusionContext context = NonInclusionContext.unsharded()
.withShard(proof.decode().unicityCertificate.getShardTreeCertificate().getShard());
return NonInclusionVerifier.verify(history, proof, context);
}

@Test
void theBundlesAreTheReleaseAssets() {
for (String network : new String[] {"testnet2", "mainnet"}) {
try (InputStream in = RealSealsTest.class
.getResourceAsStream("/release-pins/unicity-trustbase." + network + ".cbor")) {
assertArrayEquals(in.readAllBytes(), read("unicity-trustbase." + network + ".cbor"));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}

@Test
void aRealTestnet2SealVerifiesThroughTheV2Bundle() {
TrustBaseHistory history = historyOf("testnet2", TESTNET2_ID);
assertEquals(4, history.getNetworkId().get().getId());
NonInclusionOutcome outcome = verify(proofOf("testnet2", hexOf("testnet2.non-inclusion.hex")),
history);
assertEquals(NonInclusionOutcome.Kind.ABSENT_AT_SNAPSHOT, outcome.getKind());
assertEquals(4, outcome.getSnapshot().getNetworkId().getId());
assertEquals(1, outcome.getSnapshot().getRootEpoch());
}

@Test
void aRealMainnetSealVerifiesThroughTheV2Bundle() {
TrustBaseHistory history = historyOf("mainnet", MAINNET_ID);
assertEquals(1, history.getNetworkId().get().getId());
NonInclusionOutcome outcome = verify(proofOf("mainnet", hexOf("mainnet.non-inclusion.hex")),
history);
assertEquals(NonInclusionOutcome.Kind.ABSENT_AT_SNAPSHOT, outcome.getKind());
assertEquals(1, outcome.getSnapshot().getNetworkId().getId());
assertEquals(1, outcome.getSnapshot().getRootEpoch());
}

@Test
void controlsAnotherNetworksBundleOrDamagedSignaturesDoNotVerify() {
// testnet2's seal under mainnet's bundle
NonInclusionProof testnet2 = proofOf("testnet2", hexOf("testnet2.non-inclusion.hex"));
NonInclusionVerificationException other = assertThrows(NonInclusionVerificationException.class,
() -> verify(testnet2, historyOf("mainnet", MAINNET_ID)));
assertEquals(Reason.AUTHORITY_REJECTED, other.getReason());

// damaged signatures: the proof still decodes, but once fewer than a quorum verify the seal no
// longer authenticates (genesis threshold floor(2*4/3)+1 = 3 of 4)
byte[] raw = hexOf("testnet2.non-inclusion.hex");
List<byte[]> signatures = new ArrayList<>();
for (UnicitySeal.SignatureEntry entry : testnet2.decode().unicityCertificate.getUnicitySeal()
.getSignatures()) {
signatures.add(entry.getSignature().encode());
}
assertEquals(4, signatures.size());
TrustBaseHistory history = historyOf("testnet2", TESTNET2_ID);
for (int damagedCount = 0; damagedCount <= 4; damagedCount++) {
byte[] bad = raw.clone();
for (byte[] signature : signatures.subList(0, damagedCount)) {
bad[indexOf(bad, signature) + 10] ^= 1;
}
NonInclusionProof damaged = proofOf("testnet2", bad);
if (damagedCount <= 1) {
assertEquals(NonInclusionOutcome.Kind.ABSENT_AT_SNAPSHOT,
verify(damaged, history).getKind(), damagedCount + " damaged");
} else {
NonInclusionVerificationException e = assertThrows(NonInclusionVerificationException.class,
() -> verify(damaged, history));
assertEquals(Reason.AUTHORITY_REJECTED, e.getReason(), damagedCount + " damaged");
}
}
assertTrue(signatures.get(0).length > 10);
}

private static int indexOf(byte[] haystack, byte[] needle) {
for (int i = 0; i + needle.length <= haystack.length; i++) {
boolean match = true;
for (int j = 0; j < needle.length && match; j++) {
match = haystack[i + j] == needle[j];
}
if (match) {
return i;
}
}
throw new IllegalStateException("signature not found in the proof bytes");
}
}
Loading
Loading