Skip to content

Commit

Permalink
Extra tests for peer data
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jan 9, 2025
1 parent 1489108 commit e61d237
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
12 changes: 12 additions & 0 deletions convex-core/src/main/java/convex/core/crypto/AKeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

}
8 changes: 4 additions & 4 deletions convex-core/src/main/java/convex/core/cvm/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class Peer {
*/
private final AVector<BlockResult> blockResults;

private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long pos, State state, State genesis, long history, AVector<BlockResult> results,
private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long statePos, State state, State genesis, long history, AVector<BlockResult> results,
long timeStamp) {
this.keyPair = kp;
this.peerKey = kp.getAccountKey();
Expand All @@ -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
Expand All @@ -134,8 +134,8 @@ private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long pos, State s
public static Peer fromData(AKeyPair keyPair,AMap<Keyword, ACell> peerData) {
Belief belief=(Belief) peerData.get(Keywords.BELIEF);
AVector<BlockResult> results=(AVector<BlockResult>) 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();
Expand Down
18 changes: 18 additions & 0 deletions convex-core/src/main/java/convex/core/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 extends ACell> 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
Expand Down
58 changes: 58 additions & 0 deletions convex-core/src/test/java/convex/core/cvm/PeerTest.java
Original file line number Diff line number Diff line change
@@ -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<Keyword, ACell> 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<Keyword, ACell> data2=FileUtils.loadCAD3(temp);

Peer p4=Peer.fromData(KP, data2);
assertEquals(p.getBelief(),p4.getBelief());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down

0 comments on commit e61d237

Please sign in to comment.