Skip to content

Commit e61d237

Browse files
committed
Extra tests for peer data
1 parent 1489108 commit e61d237

File tree

5 files changed

+95
-4
lines changed

5 files changed

+95
-4
lines changed

convex-core/src/main/java/convex/core/crypto/AKeyPair.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public static AKeyPair create(byte[] keyMaterial) {
130130
* @return A new key pair using the given seed
131131
*/
132132
public static AKeyPair create(Blob ed25519seed) {
133+
if (ed25519seed.count()!=SEED_LENGTH) throw new IllegalArgumentException("seed must 32 bytes");
133134
return Providers.generate(ed25519seed);
134135
}
135136

@@ -312,4 +313,15 @@ public static PublicKey publicKeyFromBytes(byte[] key) throws BadFormatException
312313
}
313314
}
314315

316+
/**
317+
* Create a key pair from a hex string seed. Strips whitespace and leading 0x if needed.
318+
* @param seed Hex string containing 32 byte Ed25519 seed
319+
* @return
320+
*/
321+
public static AKeyPair create(String seed) {
322+
Blob b=Blob.parse(seed);
323+
if (b==null) throw new IllegalArgumentException("seed must be a valid Blob");
324+
return create(b);
325+
}
326+
315327
}

convex-core/src/main/java/convex/core/cvm/Peer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public class Peer {
108108
*/
109109
private final AVector<BlockResult> blockResults;
110110

111-
private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long pos, State state, State genesis, long history, AVector<BlockResult> results,
111+
private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long statePos, State state, State genesis, long history, AVector<BlockResult> results,
112112
long timeStamp) {
113113
this.keyPair = kp;
114114
this.peerKey = kp.getAccountKey();
@@ -118,14 +118,14 @@ private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long pos, State s
118118
this.timestamp = timeStamp;
119119

120120
this.consensusOrder=consensusOrder;
121-
this.statePosition=pos;
121+
this.statePosition=statePos;
122122

123123
this.historyPosition=history;
124124
this.blockResults = results;
125125
}
126126

127127
/**
128-
* Constructs a Peer instance from persisted PEer Data
128+
* Constructs a Peer instance from persisted Peer Data
129129
* @param keyPair Key Pair for Peer
130130
* @param peerData Peer data map
131131
* @return New Peer instance
@@ -134,8 +134,8 @@ private Peer(AKeyPair kp, Belief belief, Order consensusOrder, long pos, State s
134134
public static Peer fromData(AKeyPair keyPair,AMap<Keyword, ACell> peerData) {
135135
Belief belief=(Belief) peerData.get(Keywords.BELIEF);
136136
AVector<BlockResult> results=(AVector<BlockResult>) peerData.get(Keywords.RESULTS);
137-
State state=(State) peerData.get(Keywords.STATE);
138137
State genesis=(State) peerData.get(Keywords.GENESIS);
138+
State state=(State) peerData.get(Keywords.STATE);
139139
long pos=((CVMLong) peerData.get(Keywords.POSITION)).longValue();
140140
Order co=((Order) peerData.get(Keywords.ORDER));
141141
long hpos=((CVMLong) peerData.get(Keywords.HISTORY)).longValue();

convex-core/src/main/java/convex/core/util/FileUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import java.nio.file.Path;
99
import java.nio.file.Paths;
1010

11+
import convex.core.data.ACell;
1112
import convex.core.data.Blob;
13+
import convex.core.exceptions.BadFormatException;
14+
import convex.core.message.Message;
1215

1316
/**
1417
* Generic file handling utilities. Used in CLI etc.
@@ -44,6 +47,21 @@ public static Blob loadFileAsBlob(Path file) throws IOException {
4447
public static byte[] loadFileAsBytes(Path file) throws IOException {
4548
return Files.readAllBytes(file);
4649
}
50+
51+
public static <T extends ACell> T loadCAD3(Path file) throws IOException, BadFormatException {
52+
Blob b=loadFileAsBlob(file);
53+
return Message.create(b).getPayload();
54+
}
55+
56+
public static void writeCAD3(Path file,ACell value) throws IOException {
57+
Message m=Message.create(null, value);
58+
Blob b=m.getMessageData();
59+
byte[] bs=b.getInternalArray();
60+
if (bs.length!=b.count()) {
61+
bs=b.getBytes();
62+
}
63+
Files.write(file,bs);
64+
}
4765

4866
/**
4967
* Write a file as a UTF-8 String to the specified path
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package convex.core.cvm;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.util.List;
10+
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.TestInstance;
13+
import org.junit.jupiter.api.TestInstance.Lifecycle;
14+
15+
import convex.core.crypto.AKeyPair;
16+
import convex.core.data.ACell;
17+
import convex.core.data.AMap;
18+
import convex.core.data.AccountKey;
19+
import convex.core.data.Keyword;
20+
import convex.core.exceptions.BadFormatException;
21+
import convex.core.init.Init;
22+
import convex.core.util.FileUtils;
23+
24+
@TestInstance(Lifecycle.PER_CLASS)
25+
public class PeerTest {
26+
public static String seed="1a44bbe097e38d2ba90e9426e9b1ab0ec12444a25e4d23b77fd121da728737f2";
27+
28+
static AKeyPair KP=AKeyPair.create(seed);
29+
static AccountKey KEY=KP.getAccountKey();
30+
31+
static State GENESIS=Init.createTestState(List.of(KEY));
32+
33+
@Test
34+
public void testBasicPeer() throws IOException, BadFormatException {
35+
Peer p=Peer.create(KP, GENESIS);
36+
37+
AMap<Keyword, ACell> data = p.toData();
38+
assertNotNull(data);
39+
40+
Peer p2=Peer.fromData(KP, data);
41+
42+
assertEquals(GENESIS,p2.getConsensusState());
43+
assertEquals(0,p2.getStatePosition());
44+
assertEquals(p.getPeerOrder(),p2.getPeerOrder());
45+
46+
Peer p3= p2.updateState();
47+
assertEquals(GENESIS,p3.getConsensusState());
48+
49+
Path temp=Files.createTempFile("peerData", "cad3");
50+
temp.toFile().deleteOnExit();
51+
52+
FileUtils.writeCAD3(temp,data);
53+
AMap<Keyword, ACell> data2=FileUtils.loadCAD3(temp);
54+
55+
Peer p4=Peer.fromData(KP, data2);
56+
assertEquals(p.getBelief(),p4.getBelief());
57+
}
58+
}

convex-core/src/test/java/convex/core/data/PeerStatusTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

66
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.TestInstance;
8+
import org.junit.jupiter.api.TestInstance.Lifecycle;
79

810
import convex.core.cvm.Address;
911
import convex.core.cvm.Keywords;
@@ -12,6 +14,7 @@
1214
import convex.core.exceptions.InvalidDataException;
1315
import convex.core.util.Utils;
1416

17+
@TestInstance(Lifecycle.PER_CLASS)
1518
public class PeerStatusTest {
1619

1720
@Test public void testEmpty() {

0 commit comments

Comments
 (0)