diff --git a/convex-core/src/main/java/convex/core/crypto/AKeyPair.java b/convex-core/src/main/java/convex/core/crypto/AKeyPair.java index da45d070e..122414ac4 100644 --- a/convex-core/src/main/java/convex/core/crypto/AKeyPair.java +++ b/convex-core/src/main/java/convex/core/crypto/AKeyPair.java @@ -130,6 +130,7 @@ public static AKeyPair create(byte[] keyMaterial) { * @return A new key pair using the given seed */ public static AKeyPair create(Blob ed25519seed) { + if (ed25519seed.count()!=SEED_LENGTH) throw new IllegalArgumentException("seed must 32 bytes"); return Providers.generate(ed25519seed); } @@ -312,4 +313,15 @@ public static PublicKey publicKeyFromBytes(byte[] key) throws BadFormatException } } + /** + * Create a key pair from a hex string seed. Strips whitespace and leading 0x if needed. + * @param seed Hex string containing 32 byte Ed25519 seed + * @return + */ + public static AKeyPair create(String seed) { + Blob b=Blob.parse(seed); + if (b==null) throw new IllegalArgumentException("seed must be a valid Blob"); + return create(b); + } + } diff --git a/convex-core/src/main/java/convex/core/cvm/Peer.java b/convex-core/src/main/java/convex/core/cvm/Peer.java index d6c57635c..4e2309222 100644 --- a/convex-core/src/main/java/convex/core/cvm/Peer.java +++ b/convex-core/src/main/java/convex/core/cvm/Peer.java @@ -108,7 +108,7 @@ public class Peer { */ private final AVector blockResults; - private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long pos, State state, State genesis, long history, AVector results, + private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long statePos, State state, State genesis, long history, AVector results, long timeStamp) { this.keyPair = kp; this.peerKey = kp.getAccountKey(); @@ -118,14 +118,14 @@ private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long pos, State s this.timestamp = timeStamp; this.consensusOrder=consensusOrder; - this.statePosition=pos; + this.statePosition=statePos; this.historyPosition=history; this.blockResults = results; } /** - * Constructs a Peer instance from persisted PEer Data + * Constructs a Peer instance from persisted Peer Data * @param keyPair Key Pair for Peer * @param peerData Peer data map * @return New Peer instance @@ -134,8 +134,8 @@ private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long pos, State s public static Peer fromData(AKeyPair keyPair,AMap peerData) { Belief belief=(Belief) peerData.get(Keywords.BELIEF); AVector results=(AVector) peerData.get(Keywords.RESULTS); - State state=(State) peerData.get(Keywords.STATE); State genesis=(State) peerData.get(Keywords.GENESIS); + State state=(State) peerData.get(Keywords.STATE); long pos=((CVMLong) peerData.get(Keywords.POSITION)).longValue(); Order co=((Order) peerData.get(Keywords.ORDER)); long hpos=((CVMLong) peerData.get(Keywords.HISTORY)).longValue(); diff --git a/convex-core/src/main/java/convex/core/util/FileUtils.java b/convex-core/src/main/java/convex/core/util/FileUtils.java index e60788a63..727796943 100644 --- a/convex-core/src/main/java/convex/core/util/FileUtils.java +++ b/convex-core/src/main/java/convex/core/util/FileUtils.java @@ -8,7 +8,10 @@ import java.nio.file.Path; import java.nio.file.Paths; +import convex.core.data.ACell; import convex.core.data.Blob; +import convex.core.exceptions.BadFormatException; +import convex.core.message.Message; /** * Generic file handling utilities. Used in CLI etc. @@ -44,6 +47,21 @@ public static Blob loadFileAsBlob(Path file) throws IOException { public static byte[] loadFileAsBytes(Path file) throws IOException { return Files.readAllBytes(file); } + + public static T loadCAD3(Path file) throws IOException, BadFormatException { + Blob b=loadFileAsBlob(file); + return Message.create(b).getPayload(); + } + + public static void writeCAD3(Path file,ACell value) throws IOException { + Message m=Message.create(null, value); + Blob b=m.getMessageData(); + byte[] bs=b.getInternalArray(); + if (bs.length!=b.count()) { + bs=b.getBytes(); + } + Files.write(file,bs); + } /** * Write a file as a UTF-8 String to the specified path diff --git a/convex-core/src/test/java/convex/core/cvm/PeerTest.java b/convex-core/src/test/java/convex/core/cvm/PeerTest.java new file mode 100644 index 000000000..9849b785c --- /dev/null +++ b/convex-core/src/test/java/convex/core/cvm/PeerTest.java @@ -0,0 +1,58 @@ +package convex.core.cvm; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + +import convex.core.crypto.AKeyPair; +import convex.core.data.ACell; +import convex.core.data.AMap; +import convex.core.data.AccountKey; +import convex.core.data.Keyword; +import convex.core.exceptions.BadFormatException; +import convex.core.init.Init; +import convex.core.util.FileUtils; + +@TestInstance(Lifecycle.PER_CLASS) +public class PeerTest { + public static String seed="1a44bbe097e38d2ba90e9426e9b1ab0ec12444a25e4d23b77fd121da728737f2"; + + static AKeyPair KP=AKeyPair.create(seed); + static AccountKey KEY=KP.getAccountKey(); + + static State GENESIS=Init.createTestState(List.of(KEY)); + + @Test + public void testBasicPeer() throws IOException, BadFormatException { + Peer p=Peer.create(KP, GENESIS); + + AMap data = p.toData(); + assertNotNull(data); + + Peer p2=Peer.fromData(KP, data); + + assertEquals(GENESIS,p2.getConsensusState()); + assertEquals(0,p2.getStatePosition()); + assertEquals(p.getPeerOrder(),p2.getPeerOrder()); + + Peer p3= p2.updateState(); + assertEquals(GENESIS,p3.getConsensusState()); + + Path temp=Files.createTempFile("peerData", "cad3"); + temp.toFile().deleteOnExit(); + + FileUtils.writeCAD3(temp,data); + AMap data2=FileUtils.loadCAD3(temp); + + Peer p4=Peer.fromData(KP, data2); + assertEquals(p.getBelief(),p4.getBelief()); + } +} diff --git a/convex-core/src/test/java/convex/core/data/PeerStatusTest.java b/convex-core/src/test/java/convex/core/data/PeerStatusTest.java index ef548bfbb..6ff662b64 100644 --- a/convex-core/src/test/java/convex/core/data/PeerStatusTest.java +++ b/convex-core/src/test/java/convex/core/data/PeerStatusTest.java @@ -4,6 +4,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; import convex.core.cvm.Address; import convex.core.cvm.Keywords; @@ -12,6 +14,7 @@ import convex.core.exceptions.InvalidDataException; import convex.core.util.Utils; +@TestInstance(Lifecycle.PER_CLASS) public class PeerStatusTest { @Test public void testEmpty() {