Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Persistent nodes #332

Merged
merged 20 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ repositories {
jcenter()
maven { url "https://repo1.maven.org/maven2/" }
maven { url "https://consensys.bintray.com/consensys" }
maven { url "https://repository.apache.org/content/repositories/snapshots/" }
}

dependencies {
Expand Down
2 changes: 2 additions & 0 deletions gradle/check-licenses.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ downloadLicenses {
],
(edl1): [
'Eclipse Distribution License - v 1.0',
'Eclipse Distribution License v. 1.0',
],
(epl1): [
'Eclipse Public License - v 1.0',
'Eclipse Public License v1.0',
],
(epl2): [
'Eclipse Public License v2.0',
Expand Down
2 changes: 1 addition & 1 deletion gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ dependencyManagement {
dependency 'org.apache.tuweni:tuweni-crypto:0.10.0'
dependency 'org.apache.tuweni:tuweni-io:0.10.0'
dependency 'org.apache.tuweni:tuweni-junit:0.10.0'
dependency 'org.apache.tuweni:tuweni-kv:0.10.0'
dependency 'org.apache.tuweni:tuweni-kv:1.0.0-SNAPSHOT'
atoulme marked this conversation as resolved.
Show resolved Hide resolved
dependency 'org.apache.tuweni:tuweni-net:0.10.0'
dependency 'org.apache.tuweni:tuweni-rlp:0.10.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import java.io.IOException;
import java.net.ServerSocket;
import java.net.URL;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;

Expand All @@ -46,16 +46,16 @@ public static String joinPathsAsTomlListEntry(final Path... paths) {
return builder.toString();
}

private static HttpUrl buildUrl(final String host, final int port) {
private static HttpUrl buildUri(final String host, final int port) {
return new HttpUrl.Builder().scheme("http").host(host).port(port).build();
}

public static String urlString(final String host, final int port) {
return buildUrl(host, port).toString();
return buildUri(host, port).toString();
}

public static URL url(final String host, final int port) {
return buildUrl(host, port).url();
public static URI uri(final String host, final int port) {
return buildUri(host, port).uri();
}

public static Config nodeConfig(
Expand All @@ -70,7 +70,8 @@ public static Config nodeConfig(
final String tls,
final String tlsServerTrust,
final String tlsClientTrust,
final String storage) throws IOException {
final String storage,
final String nodeStorage) throws IOException {
atoulme marked this conversation as resolved.
Show resolved Hide resolved

final Path workDir = tempDir.resolve("acceptance").toAbsolutePath();
Files.createDirectories(workDir);
Expand All @@ -85,6 +86,7 @@ public static Config nodeConfig(
+ "clientport = " + clientPort + "\n"
+ "clientnetworkinterface = \"" + clientNetworkInterface + "\"\n"
+ "storage = \"" + storage + "\"\n"
+ "nodestorage = \"" + nodeStorage + "\"\n"
+ "publickeys = [" + pubKeys + "]\n"
+ "privatekeys = [" + privKeys + "]\n"
+ "workdir= \"" + workDir.toString() + "\"\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ void setUp(@TempDirectory final Path tempDir) throws Exception {
"off",
"tofu",
"tofu",
"sql:" + jdbcUrl);
"sql:" + jdbcUrl,
"memory");

vertx = vertx();
orionLauncher = NodeUtils.startOrion(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ void setUp(@TempDirectory final Path tempDir) throws Exception {
"off",
"tofu",
"tofu",
"sql:" + jdbcUrl);
"sql:" + jdbcUrl,
"memory");

vertx = vertx();
orionLauncher = NodeUtils.startOrion(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import net.consensys.orion.exception.OrionErrorCode;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;

import io.vertx.core.Vertx;
Expand All @@ -49,7 +48,6 @@ class SingleNodeSendTest {
private static final String PK_2_B_64 = "Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=";
private static final String PK_CORRUPTED = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGoAAA=";
private static final String PK_MISSING_PEER = "A1aVtMxLCUlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=";
private static final String HOST_NAME = "127.0.0.1";

private static Config config;

Expand All @@ -76,15 +74,15 @@ static void setUpSingleNode(@TempDirectory final Path tempDir) throws Exception
"off",
"tofu",
"tofu",
"leveldb:database/node1");
"leveldb:database/node1",
"memory");
}


@BeforeEach
void setUp() throws MalformedURLException {
orionLauncher = NodeUtils.startOrion(config);
vertx = vertx();
orionLauncher.addPeer(new URL(NodeUtils.urlString(HOST_NAME, orionLauncher.nodePort())));
httpClient = vertx.createHttpClient();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@
import net.consensys.orion.cmd.Orion;
import net.consensys.orion.config.Config;
import net.consensys.orion.http.server.HttpContentType;
import net.consensys.orion.network.ConcurrentNetworkNodes;
import net.consensys.orion.network.PersistentNetworkNodes;
import net.consensys.orion.network.ReadOnlyNetworkNodes;
import net.consensys.orion.utils.Serializer;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand All @@ -56,6 +59,7 @@
import org.apache.tuweni.crypto.sodium.Box;
import org.apache.tuweni.junit.TempDirectory;
import org.apache.tuweni.junit.TempDirectoryExtension;
import org.apache.tuweni.kv.MapKeyValueStore;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -69,7 +73,7 @@ class DualNodesSendReceiveTest {

private Config firstNodeConfig;
private Config secondNodeConfig;
private ConcurrentNetworkNodes networkNodes;
private PersistentNetworkNodes networkNodes;

private Orion firstOrionLauncher;
private Orion secondOrionLauncher;
Expand All @@ -86,12 +90,18 @@ void setUpDualNodes(@TempDirectory final Path tempDir) throws Exception {
pubKeyStrings = pubKeys.stream().map(pub -> getStringFromResource(pub)).collect(Collectors.toList());
final List<Path> priKeyFileList = priKeys.stream().map(pub -> getPath(tempDir, pub)).collect(Collectors.toList());

final String jdbcUrl = "jdbc:h2:" + tempDir.resolve("node2").toString();
final String jdbcUrl = "jdbc:h2:" + tempDir.resolve("DualNodesSendReceiveTest").toString();
try (final Connection conn = DriverManager.getConnection(jdbcUrl)) {
final Statement st = conn.createStatement();
st.executeUpdate("create table if not exists store(key char(60), value binary, primary key(key))");
}

final String jdbcUrlNodeInfo = "jdbc:h2:" + tempDir.resolve("DualNodesSendReceiveTestNodeInfo").toString();
try (final Connection conn = DriverManager.getConnection(jdbcUrlNodeInfo)) {
final Statement st = conn.createStatement();
st.executeUpdate("create table if not exists store(key char(60), value binary, primary key(key))");
}


firstNodeConfig = NodeUtils.nodeConfig(
tempDir,
Expand All @@ -105,7 +115,8 @@ void setUpDualNodes(@TempDirectory final Path tempDir) throws Exception {
"off",
"tofu",
"tofu",
"leveldb:database/node1");
"leveldb:database/node1",
"memory");
secondNodeConfig = NodeUtils.nodeConfig(
tempDir,
0,
Expand All @@ -118,7 +129,8 @@ void setUpDualNodes(@TempDirectory final Path tempDir) throws Exception {
"off",
"tofu",
"tofu",
"sql:" + jdbcUrl);
"sql:" + jdbcUrl,
"sql:" + jdbcUrlNodeInfo);
vertx = vertx();
firstOrionLauncher = NodeUtils.startOrion(firstNodeConfig);
firstHttpClient = vertx.createHttpClient();
Expand All @@ -129,10 +141,17 @@ void setUpDualNodes(@TempDirectory final Path tempDir) throws Exception {
final Box.PublicKey pk3 = Box.PublicKey.fromBytes(decodeBytes(pubKeyStrings.get(2)));
final Box.PublicKey pk4 = Box.PublicKey.fromBytes(decodeBytes(pubKeyStrings.get(3)));
final Box.PublicKey pk5 = Box.PublicKey.fromBytes(decodeBytes(pubKeyStrings.get(4)));
networkNodes = new ConcurrentNetworkNodes(NodeUtils.url("127.0.0.1", firstOrionLauncher.nodePort()));
networkNodes = new PersistentNetworkNodes(firstNodeConfig, new Box.PublicKey[] {}, MapKeyValueStore.open());
networkNodes.setNodeUrl(NodeUtils.uri("127.0.0.1", firstOrionLauncher.nodePort()), new Box.PublicKey[0]);
Map<Box.PublicKey, URI> pks = new HashMap<>();
pks.put(pk1, NodeUtils.uri("127.0.0.1", firstOrionLauncher.nodePort()));
pks.put(pk2, NodeUtils.uri("127.0.0.1", firstOrionLauncher.nodePort()));
pks.put(pk3, NodeUtils.uri("127.0.0.1", secondOrionLauncher.nodePort()));
pks.put(pk4, NodeUtils.uri("127.0.0.1", secondOrionLauncher.nodePort()));
pks.put(pk5, NodeUtils.uri("127.0.0.1", secondOrionLauncher.nodePort()));

networkNodes.addNode(pks.entrySet());

networkNodes.addNode(Arrays.asList(pk1, pk2), NodeUtils.url("127.0.0.1", firstOrionLauncher.nodePort()));
networkNodes.addNode(Arrays.asList(pk3, pk4, pk5), NodeUtils.url("127.0.0.1", secondOrionLauncher.nodePort()));
// prepare /partyinfo payload (our known peers)
final RequestBody partyInfoBody =
RequestBody.create(MediaType.parse(CBOR.httpHeaderValue), Serializer.serialize(CBOR, networkNodes));
Expand All @@ -142,7 +161,7 @@ void setUpDualNodes(@TempDirectory final Path tempDir) throws Exception {
final String firstNodeBaseUrl = NodeUtils.urlString("127.0.0.1", firstOrionLauncher.nodePort());
final Request request = new Request.Builder().post(partyInfoBody).url(firstNodeBaseUrl + "/partyinfo").build();
// first /partyinfo call may just get the one node, so wait until we get at least 2 nodes
await().atMost(10, TimeUnit.SECONDS).until(() -> getPartyInfoResponse(httpClient, request).nodeURLs().size() == 2);
await().atMost(10, TimeUnit.SECONDS).until(() -> getPartyInfoResponse(httpClient, request).nodeURIs().size() == 2);

}

Expand All @@ -163,13 +182,14 @@ private String getStringFromResource(final String resourceFileName) {
}
}

private ConcurrentNetworkNodes getPartyInfoResponse(final OkHttpClient httpClient, final Request request)
private ReadOnlyNetworkNodes getPartyInfoResponse(final OkHttpClient httpClient, final Request request)
throws Exception {
final Response resp = httpClient.newCall(request).execute();
assertEquals(200, resp.code());

final ConcurrentNetworkNodes partyInfoResponse =
Serializer.deserialize(HttpContentType.CBOR, ConcurrentNetworkNodes.class, resp.body().bytes());
assertEquals("Received " + resp.code(), 200, resp.code());

final ReadOnlyNetworkNodes partyInfoResponse =
Serializer.deserialize(HttpContentType.CBOR, ReadOnlyNetworkNodes.class, resp.body().bytes());
return partyInfoResponse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ static void setUpSingleNode(@TempDirectory final Path tempDir) throws Exception
"off",
"tofu",
"tofu",
"leveldb:database/node1");
"leveldb:database/node1",
"memory");
}

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@
import net.consensys.orion.config.Config;
import net.consensys.orion.http.handler.privacy.PrivacyGroup;
import net.consensys.orion.http.server.HttpContentType;
import net.consensys.orion.network.ConcurrentNetworkNodes;
import net.consensys.orion.network.PersistentNetworkNodes;
import net.consensys.orion.network.ReadOnlyNetworkNodes;
import net.consensys.orion.utils.Serializer;

import java.net.URI;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand All @@ -58,6 +61,7 @@
import okhttp3.Response;
import org.apache.tuweni.crypto.sodium.Box;
import org.apache.tuweni.junit.TempDirectoryExtension;
import org.apache.tuweni.kv.MapKeyValueStore;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -84,7 +88,7 @@ void setUpDualNodes(@TempDir final Path tempDir) throws Exception {
final Path key1key = copyResource("key1.key", tempDir.resolve("key1.key"));
final Path key2pub = copyResource("key2.pub", tempDir.resolve("key2.pub"));
final Path key2key = copyResource("key2.key", tempDir.resolve("key2.key"));
final String jdbcUrl = "jdbc:h2:" + tempDir.resolve("node2").toString();
final String jdbcUrl = "jdbc:h2:" + tempDir.resolve("DualNodesPrivacyGroupsTest").toString();
try (final Connection conn = DriverManager.getConnection(jdbcUrl)) {
final Statement st = conn.createStatement();
st.executeUpdate("create table if not exists store(key char(60), value binary, primary key(key))");
Expand All @@ -102,7 +106,8 @@ void setUpDualNodes(@TempDir final Path tempDir) throws Exception {
"off",
"tofu",
"tofu",
"leveldb:" + tempDir + "database/node1");
"leveldb:" + tempDir + "database/DualNodesPrivacyGroupsTest",
"memory");
final Config secondNodeConfig = NodeUtils.nodeConfig(
tempDir,
0,
Expand All @@ -115,7 +120,8 @@ void setUpDualNodes(@TempDir final Path tempDir) throws Exception {
"off",
"tofu",
"tofu",
"sql:" + jdbcUrl);
"sql:" + jdbcUrl,
"memory");

vertx = vertx();
firstOrionLauncher = NodeUtils.startOrion(firstNodeConfig);
Expand All @@ -124,11 +130,14 @@ void setUpDualNodes(@TempDir final Path tempDir) throws Exception {
secondHttpClient = vertx.createHttpClient();
final Box.PublicKey pk1 = Box.PublicKey.fromBytes(decodeBytes(PK_1_B_64));
final Box.PublicKey pk2 = Box.PublicKey.fromBytes(decodeBytes(PK_2_B_64));
final ConcurrentNetworkNodes networkNodes =
new ConcurrentNetworkNodes(NodeUtils.url("127.0.0.1", firstOrionLauncher.nodePort()));

networkNodes.addNode(Collections.singletonList(pk1), NodeUtils.url("127.0.0.1", firstOrionLauncher.nodePort()));
networkNodes.addNode(Collections.singletonList(pk2), NodeUtils.url("127.0.0.1", secondOrionLauncher.nodePort()));
final PersistentNetworkNodes networkNodes =
new PersistentNetworkNodes(firstNodeConfig, new Box.PublicKey[] {}, MapKeyValueStore.open());
networkNodes.setNodeUrl(NodeUtils.uri("127.0.0.1", firstOrionLauncher.nodePort()), new Box.PublicKey[0]);
Map<Box.PublicKey, URI> pks = new HashMap<>();
pks.put(pk1, NodeUtils.uri("127.0.0.1", firstOrionLauncher.nodePort()));
pks.put(pk2, NodeUtils.uri("127.0.0.1", secondOrionLauncher.nodePort()));

networkNodes.addNode(pks.entrySet());
// prepare /partyinfo payload (our known peers)
final RequestBody partyInfoBody =
RequestBody.create(MediaType.parse(CBOR.httpHeaderValue), Serializer.serialize(CBOR, networkNodes));
Expand All @@ -138,16 +147,16 @@ void setUpDualNodes(@TempDir final Path tempDir) throws Exception {
final String firstNodeBaseUrl = NodeUtils.urlString("127.0.0.1", firstOrionLauncher.nodePort());
final Request request = new Request.Builder().post(partyInfoBody).url(firstNodeBaseUrl + "/partyinfo").build();
// first /partyinfo call may just get the one node, so wait until we get at least 2 nodes
await().atMost(5, TimeUnit.SECONDS).until(() -> getPartyInfoResponse(httpClient, request).nodeURLs().size() == 2);
await().atMost(10, TimeUnit.SECONDS).until(() -> getPartyInfoResponse(httpClient, request).nodeURIs().size() == 2);

}

private ConcurrentNetworkNodes getPartyInfoResponse(final OkHttpClient httpClient, final Request request)
private ReadOnlyNetworkNodes getPartyInfoResponse(final OkHttpClient httpClient, final Request request)
throws Exception {
final Response resp = httpClient.newCall(request).execute();
assertEquals(200, resp.code());

return Serializer.deserialize(HttpContentType.CBOR, ConcurrentNetworkNodes.class, resp.body().bytes());
return Serializer.deserialize(HttpContentType.CBOR, ReadOnlyNetworkNodes.class, resp.body().bytes());
}

@AfterEach
Expand Down
Loading