Skip to content

Commit b4903fa

Browse files
committed
Edits to support --genesis option on peer start
1 parent 48d2d05 commit b4903fa

File tree

5 files changed

+90
-14
lines changed

5 files changed

+90
-14
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ EXPOSE 443
2727
VOLUME ["/etc/ssl/certs"]
2828
VOLUME ["/etc/convex/keystore"]
2929

30-
ENTRYPOINT ["java", "-jar", "convex.jar", "local", "start"]
30+
ENTRYPOINT ["java", "-jar", "convex.jar", "peer", "start"]
3131

convex-cli/src/main/java/convex/cli/peer/PeerStart.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
import convex.cli.ExitCodes;
1212
import convex.cli.mixins.RemotePeerMixin;
1313
import convex.core.crypto.AKeyPair;
14+
import convex.core.cvm.State;
1415
import convex.core.data.AccountKey;
1516
import convex.core.data.Address;
17+
import convex.core.data.Blob;
1618
import convex.core.data.Keyword;
1719
import convex.core.data.Keywords;
20+
import convex.core.init.Init;
1821
import convex.etch.EtchStore;
1922
import convex.peer.API;
2023
import convex.peer.ConfigException;
@@ -56,12 +59,18 @@ public class PeerStart extends APeerCommand {
5659
description = "Port number for the peer. Default is ${DEFAULT-VALUE}. If set to 0, will choose a random port.")
5760
private int port = 0;
5861

59-
@Option(names = { "--url" }, description = "URL for the peer to publish. If not provided, the peer will have no public URL.")
62+
@Option(names = { "--url" },
63+
description = "URL for the peer to publish. If not provided, the peer will have no public URL.")
6064
private String url;
6165

6266
@Option(names = { "--norest" }, description = "Disable REST srever.")
6367
private boolean norest;
6468

69+
@Option(names = { "--genesis" },
70+
defaultValue = "${env:CONVEX_GENESIS_SEED}",
71+
description = "Governance seed for network genesis. For testing use only.")
72+
private String genesis;
73+
6574
@Option(names = { "--api-port" },
6675
defaultValue = "8080",
6776
description = "Port for REST API.")
@@ -115,15 +124,25 @@ public void execute() throws InterruptedException {
115124

116125
storeMixin.ensureKeyStore();
117126
try (EtchStore store = etchMixin.getEtchStore()) {
118-
119-
AKeyPair peerKey=findPeerKey(store);
120-
if (peerKey==null) {
121-
informWarning("No --peer-key specified or inferred from Etch Store "+store);
122-
showUsage();
123-
return;
127+
AKeyPair peerKey;
128+
AKeyPair genesisKey=null;
129+
if (genesis!=null&&(!genesis.isEmpty())) {
130+
paranoia("Should't use Genesis Seed in strict security mode! Consider key compromised!");
131+
Blob seed=Blob.parse(genesis);
132+
if (seed.count()!=32) {
133+
throw new CLIError("Genesis seed must be 32 byte hex blob");
134+
}
135+
peerKey = AKeyPair.create(seed);
136+
genesisKey=peerKey;
137+
informWarning("Using test genesis seed: "+seed);
138+
} else {
139+
peerKey=findPeerKey(store);
140+
if (peerKey==null) {
141+
informWarning("No --peer-key specified or inferred from Etch Store "+store);
142+
showUsage();
143+
return;
144+
}
124145
}
125-
126-
inform("Preparing to start peer: "+peerKey.getAccountKey());
127146

128147
Address controller=Address.parse(controllerAddress);
129148
if (controller==null) {
@@ -136,6 +155,12 @@ public void execute() throws InterruptedException {
136155
HashMap<Keyword,Object> config=new HashMap<>();
137156
config.put(Keywords.KEYPAIR, peerKey);
138157
config.put(Keywords.STORE, store);
158+
if (genesisKey!=null) {
159+
AccountKey gpk=genesisKey.getAccountKey();
160+
State state=Init.createState(gpk,gpk,List.of(gpk));
161+
informWarning("Greated genesis State: "+state.getHash());
162+
config.put(Keywords.STATE, state);
163+
}
139164
Server server=API.launchPeer(config);
140165

141166
if (!norest) {

convex-core/src/main/java/convex/core/data/MapTree.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import convex.core.exceptions.BadFormatException;
1414
import convex.core.exceptions.InvalidDataException;
1515
import convex.core.exceptions.TODOException;
16+
import convex.core.lang.RT;
1617
import convex.core.exceptions.Panic;
1718
import convex.core.util.Bits;
1819
import convex.core.util.MergeFunction;
@@ -859,8 +860,10 @@ protected void validateWithPrefix(Hash prefix, int shift) throws InvalidDataExce
859860
throw new InvalidDataException(
860861
"Expected AHashMap child at " + prefix + Utils.toHexChar(digitForIndex(i, mask)), this);
861862
}
862-
@SuppressWarnings("unchecked")
863-
AHashMap<K, V> child = (AHashMap<K, V>) o;
863+
AHashMap<K, V> child = RT.ensureHashMap(o);
864+
if (child==null) {
865+
throw new InvalidDataException("Non-hashmap child at position "+i,this);
866+
}
864867
if (child.isEmpty())
865868
throw new InvalidDataException("Empty child at " + prefix + Utils.toHexChar(digitForIndex(i, mask)),
866869
this);

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616
import convex.core.lang.RT;
1717
import convex.test.Samples;
1818

19+
/**
20+
* Tests for general set behaviour and logic
21+
*/
1922
public class SetsTest {
2023

2124
@Test
2225
public void testEmptySet() {
2326
ASet<ACell> e = Sets.empty();
2427
assertEquals(0, e.size());
2528
assertFalse(e.contains(null));
29+
assertSame(e,Sets.create(Vectors.empty()));
30+
assertSame(e,Sets.of(1).exclude(CVMLong.ONE));
2631
}
2732

2833
@Test
@@ -35,18 +40,23 @@ public void testIncludeExclude() {
3540
assertEquals("#{1}", s.toString());
3641
s = s.include(RT.cvm(2L));
3742
assertEquals("#{2,1}", s.toString());
43+
assertSame(s,s.exclude(null));
44+
3845
s = s.exclude(RT.cvm(1L));
3946
assertEquals("#{2}", s.toString());
4047
s = s.exclude(RT.cvm(2L));
41-
assertTrue(s.isEmpty());
42-
assertSame(s, Sets.empty());
48+
assertSame(Sets.empty(),s);
49+
50+
s = s.exclude(RT.cvm(2L));
51+
assertSame(Sets.empty(),s);
4352
}
4453

4554
@Test
4655
public void testSetEncoding() {
4756
// Set should be encoded as a map with different tag and extra value Ref(s)
4857
ASet<?> s=Sets.of(123);
4958
AMap<?,?> m=Maps.of(123,null);
59+
// compare encodings ignoring tag
5060
assertEquals(m.getEncoding().slice(1),s.getEncoding().append(Blob.SINGLE_ZERO).slice(1));
5161
}
5262

@@ -139,6 +149,14 @@ public void testMergingIdentity() {
139149
assertSame(a, a.includeAll(Sets.of(1L, 3L)));
140150
}
141151

152+
@Test
153+
public void testNilMembership() {
154+
ASet<CVMLong> a = Sets.of(1, 2, 3, null);
155+
assertTrue(a.containsKey(null));
156+
a=a.exclude(null);
157+
assertEquals(3,a.size());
158+
}
159+
142160
@Test
143161
public void testIntersection() {
144162
ASet<CVMLong> a = Sets.of(1, 2, 3);
@@ -184,10 +202,16 @@ public void testBigSlice() {
184202
ObjectsTest.doEqualityTests(s,s1.includeAll(s2).includeAll(s3));
185203
}
186204

205+
@SuppressWarnings("unchecked")
187206
@Test
188207
public void testBigMerging() {
189208
ASet<CVMLong> s = Sets.create(Samples.INT_VECTOR_300);
209+
assertEquals(0,((SetTree<CVMLong>)s).shift);
190210
SetsTest.doSetTests(s);
211+
212+
SetTree<CVMLong> child=(SetTree<CVMLong>)(s.getRef(1).getValue());
213+
assertEquals(1,child.shift);
214+
SetsTest.doSetTests(child);
191215

192216
ASet<CVMLong> s2 = s.includeAll(Sets.of(1, 2, 3, 100));
193217
assertEquals(s, s2);
@@ -231,6 +255,29 @@ public void testIncrementalBuilding() {
231255
}
232256

233257
doSetTests(set);
258+
259+
// now build the same set in hash order
260+
ASet<CVMLong> set2=Sets.empty();
261+
for (int i=0; i<320; i++) {
262+
assertEquals(i,set2.size());
263+
264+
// extend set with one new element
265+
CVMLong v=set.get(i);
266+
set2=set2.conj(v);
267+
}
268+
assertEquals(set2,set);
269+
doSetTests(set); // check nothing is odd
270+
271+
// now deconstruct the set in hash order
272+
ASet<CVMLong> set3=set2;
273+
for (int i=0; i<320; i++) {
274+
assertEquals(320-i,set3.size());
275+
276+
// extend set with one new element
277+
CVMLong v=set.get(i);
278+
set3=set3.exclude(v);
279+
}
280+
assertSame(Sets.EMPTY,set3);
234281
}
235282

236283
/**

convex-core/src/test/java/convex/core/lang/CoreTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,7 @@ public void testInto() {
19351935
assertEquals(Lists.of(2L, 1L, 3L, 4L), eval("(into '(3 4) '(1 2))"));
19361936

19371937
assertEquals(Sets.of(1L, 2L, 3L), eval("(into #{} [1 2 1 2 3])"));
1938+
assertEquals(Sets.of(1L, 2L, 3L), eval("(into #{} #{1 2 3})"));
19381939

19391940
// map as data structure
19401941
assertEquals(Maps.empty(), eval("(into {} [])"));

0 commit comments

Comments
 (0)