From 6718bc7965023fa13fd50fdb00d4a449bb263c83 Mon Sep 17 00:00:00 2001 From: lvaccaro Date: Sun, 5 Mar 2017 00:33:27 +0100 Subject: [PATCH] Sort on attestations --- .gitignore | 1 + .../BitcoinBlockHeaderAttestation.java | 8 ++++++++ .../java/com/eternitywall/PendingAttestation.java | 8 ++++++++ .../java/com/eternitywall/TimeAttestation.java | 14 +++++++++++++- src/main/java/com/eternitywall/Timestamp.java | 2 ++ .../java/com/eternitywall/UnknownAttestation.java | 9 +++++++++ 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 92322c4..5c56d96 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +out/ .idea/ target/ diff --git a/src/main/java/com/eternitywall/BitcoinBlockHeaderAttestation.java b/src/main/java/com/eternitywall/BitcoinBlockHeaderAttestation.java index 68eb435..b9cf250 100644 --- a/src/main/java/com/eternitywall/BitcoinBlockHeaderAttestation.java +++ b/src/main/java/com/eternitywall/BitcoinBlockHeaderAttestation.java @@ -2,6 +2,7 @@ * Created by luca on 25/02/2017. */ +import java.util.Arrays; import java.util.logging.Logger; /** @@ -59,4 +60,11 @@ public void serializePayload(StreamSerializationContext ctx) { public String toString() { return "com.eternitywall.BitcoinBlockHeaderAttestation(" + this.height + ")"; } + + + @Override + public int compareTo(TimeAttestation o) { + BitcoinBlockHeaderAttestation ob = (BitcoinBlockHeaderAttestation) o; + return this.height - ob.height; + } } diff --git a/src/main/java/com/eternitywall/PendingAttestation.java b/src/main/java/com/eternitywall/PendingAttestation.java index cd93010..b5ba912 100644 --- a/src/main/java/com/eternitywall/PendingAttestation.java +++ b/src/main/java/com/eternitywall/PendingAttestation.java @@ -1,5 +1,7 @@ package com.eternitywall; +import org.bouncycastle.util.Arrays; + import java.nio.charset.StandardCharsets; import java.util.logging.Logger; @@ -78,4 +80,10 @@ public void serializePayload(StreamSerializationContext ctx) { public String toString() { return "PendingAttestation(\'" + new String(this.uri, StandardCharsets.UTF_8) + "\')"; } + + @Override + public int compareTo(TimeAttestation o) { + PendingAttestation opa = (PendingAttestation) o; + return Arrays.compareUnsigned(this.uri, opa.uri) ; + } } diff --git a/src/main/java/com/eternitywall/TimeAttestation.java b/src/main/java/com/eternitywall/TimeAttestation.java index db31718..7e483ec 100644 --- a/src/main/java/com/eternitywall/TimeAttestation.java +++ b/src/main/java/com/eternitywall/TimeAttestation.java @@ -8,11 +8,13 @@ */ +import com.eternitywall.op.Op; + import java.util.Arrays; import java.util.logging.Logger; /** Class representing com.eternitywall.Timestamp signature verification */ -class TimeAttestation { +class TimeAttestation implements Comparable { private static Logger log = Logger.getLogger(TimeAttestation.class.getName()); @@ -70,4 +72,14 @@ public void serialize(StreamSerializationContext ctx) { public void serializePayload(StreamSerializationContext ctxPayload) { } + + @Override + public int compareTo(TimeAttestation o) { + int deltaTag = org.bouncycastle.util.Arrays.compareUnsigned(this._TAG(),o._TAG()); + if( deltaTag == 0){ + return this.compareTo(o); + } else { + return deltaTag; + } + } } diff --git a/src/main/java/com/eternitywall/Timestamp.java b/src/main/java/com/eternitywall/Timestamp.java index 4c1e1e1..1fbecb6 100644 --- a/src/main/java/com/eternitywall/Timestamp.java +++ b/src/main/java/com/eternitywall/Timestamp.java @@ -88,6 +88,8 @@ public void serialize(StreamSerializationContext ctx) { // sort List sortedAttestations = this.attestations; + Collections.sort(sortedAttestations); + if (sortedAttestations.size() > 1) { for (int i = 0; i < sortedAttestations.size(); i++) { ctx.writeBytes(new byte[]{(byte) 0xff, (byte) 0x00}); diff --git a/src/main/java/com/eternitywall/UnknownAttestation.java b/src/main/java/com/eternitywall/UnknownAttestation.java index 475d57d..196e2c3 100644 --- a/src/main/java/com/eternitywall/UnknownAttestation.java +++ b/src/main/java/com/eternitywall/UnknownAttestation.java @@ -1,5 +1,7 @@ package com.eternitywall; +import org.bouncycastle.util.Arrays; + import java.util.logging.Logger; /** @@ -39,4 +41,11 @@ public static UnknownAttestation deserialize(StreamDeserializationContext ctxPay public String toString() { return "com.eternitywall.UnknownAttestation " + this._TAG() + ' ' + this.payload; } + + + @Override + public int compareTo(TimeAttestation o) { + UnknownAttestation ota = (UnknownAttestation) o; + return Arrays.compareUnsigned(this.payload, ota.payload) ; + } }