Skip to content

Commit

Permalink
[PRIV] Generify orion to enclave (hyperledger#745)
Browse files Browse the repository at this point in the history
* Generify orion to enclave

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
  • Loading branch information
vinistevam authored and macfarla committed Feb 6, 2019
1 parent ad7c5fb commit cd81881
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 105 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.orion;
package tech.pegasys.pantheon.enclave;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import tech.pegasys.orion.testutil.OrionTestHarness;
import tech.pegasys.pantheon.orion.types.ReceiveRequest;
import tech.pegasys.pantheon.orion.types.ReceiveResponse;
import tech.pegasys.pantheon.orion.types.SendRequest;
import tech.pegasys.pantheon.orion.types.SendResponse;
import tech.pegasys.pantheon.enclave.types.ReceiveRequest;
import tech.pegasys.pantheon.enclave.types.ReceiveResponse;
import tech.pegasys.pantheon.enclave.types.SendRequest;
import tech.pegasys.pantheon.enclave.types.SendResponse;

import java.io.IOException;
import java.util.List;
Expand All @@ -32,12 +32,12 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class OrionTest {
public class EnclaveTest {

@ClassRule public static final TemporaryFolder folder = new TemporaryFolder();

private static final String PAYLOAD = "a wonderful transaction";
private static Orion orion;
private static Enclave enclave;

private static OrionTestHarness testHarness;

Expand All @@ -47,7 +47,7 @@ public static void setUpOnce() throws Exception {

testHarness = OrionTestHarness.create(folder.newFolder().toPath());

orion = new Orion(testHarness.clientUrl());
enclave = new Enclave(testHarness.clientUrl());
}

@AfterClass
Expand All @@ -57,7 +57,7 @@ public static void tearDownOnce() {

@Test
public void testUpCheck() throws IOException {
assertTrue(orion.upCheck());
assertTrue(enclave.upCheck());
}

@Test
Expand All @@ -66,17 +66,17 @@ public void testSendAndReceive() throws IOException {

SendRequest sc =
new SendRequest(PAYLOAD, publicKeys.get(0), Lists.newArrayList(publicKeys.get(1)));
SendResponse sr = orion.send(sc);
SendResponse sr = enclave.send(sc);

ReceiveRequest rc = new ReceiveRequest(sr.getKey(), publicKeys.get(1));
ReceiveResponse rr = orion.receive(rc);
ReceiveResponse rr = enclave.receive(rc);

assertEquals(PAYLOAD, new String(rr.getPayload(), UTF_8));
}

@Test(expected = IOException.class)
public void whenUpCheckFailsThrows() throws IOException {
Orion broken = new Orion("http:");
Enclave broken = new Enclave("http:");

broken.upCheck();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.orion;
package tech.pegasys.pantheon.enclave;

import tech.pegasys.pantheon.orion.types.ReceiveRequest;
import tech.pegasys.pantheon.orion.types.ReceiveResponse;
import tech.pegasys.pantheon.orion.types.SendRequest;
import tech.pegasys.pantheon.orion.types.SendResponse;
import tech.pegasys.pantheon.enclave.types.ReceiveRequest;
import tech.pegasys.pantheon.enclave.types.ReceiveResponse;
import tech.pegasys.pantheon.enclave.types.SendRequest;
import tech.pegasys.pantheon.enclave.types.SendResponse;

import java.io.IOException;

Expand All @@ -28,20 +28,16 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Orion {
public class Enclave {
private static final MediaType JSON = MediaType.parse("application/json");
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final Logger LOG = LogManager.getLogger();

private String url;
private final OkHttpClient client;

public Orion() {
this.client = new OkHttpClient();
}

public Orion(final String orionUrl) {
this.url = orionUrl;
public Enclave(final String enclaveUrl) {
this.url = enclaveUrl;
this.client = new OkHttpClient();
}

Expand All @@ -51,7 +47,7 @@ public Boolean upCheck() throws IOException {
try (Response response = client.newCall(request).execute()) {
return response.isSuccessful();
} catch (IOException e) {
LOG.error("Orion failed to execute upcheck");
LOG.error("Enclave failed to execute upcheck");
throw new IOException("Failed to perform upcheck", e);
}
}
Expand All @@ -74,7 +70,7 @@ private <T> T executePost(final String path, final String content, final Class<T
try (Response response = client.newCall(request).execute()) {
return objectMapper.readValue(response.body().string(), responseType);
} catch (IOException e) {
LOG.error("Orion failed to execute ", path);
LOG.error("Enclave failed to execute ", path);
throw new IOException("Failed to execute post", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.orion.types;
package tech.pegasys.pantheon.enclave.types;

public class ReceiveRequest {
private String key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.orion.types;
package tech.pegasys.pantheon.enclave.types;

public class ReceiveResponse {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.orion.types;
package tech.pegasys.pantheon.enclave.types;

import static java.nio.charset.StandardCharsets.UTF_8;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.orion.types;
package tech.pegasys.pantheon.enclave.types;

public class SendResponse {
String key;
Expand Down
2 changes: 1 addition & 1 deletion ethereum/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
implementation project(':ethereum:trie')
implementation project(':ethereum:permissioning')
implementation project(':metrics')
implementation project(':orion')
implementation project(':enclave')
implementation project(':services:kvstore')

implementation 'com.fasterxml.jackson.core:jackson-databind'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import static org.junit.Assert.assertTrue;

import tech.pegasys.orion.testutil.OrionTestHarness;
import tech.pegasys.pantheon.enclave.Enclave;
import tech.pegasys.pantheon.enclave.types.SendRequest;
import tech.pegasys.pantheon.enclave.types.SendResponse;
import tech.pegasys.pantheon.ethereum.mainnet.SpuriousDragonGasCalculator;
import tech.pegasys.pantheon.orion.Orion;
import tech.pegasys.pantheon.orion.types.SendRequest;
import tech.pegasys.pantheon.orion.types.SendResponse;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.io.IOException;
Expand All @@ -38,7 +38,7 @@ public class PrivacyPrecompiledContractIntegrationTest {
@ClassRule public static final TemporaryFolder folder = new TemporaryFolder();

private static final String PAYLOAD = "a wonderful transaction";
private static Orion orion;
private static Enclave enclave;

private static OrionTestHarness testHarness;

Expand All @@ -48,7 +48,7 @@ public static void setUpOnce() throws Exception {

testHarness = OrionTestHarness.create(folder.newFolder().toPath());

orion = new Orion(testHarness.clientUrl());
enclave = new Enclave(testHarness.clientUrl());
}

@AfterClass
Expand All @@ -58,7 +58,7 @@ public static void tearDownOnce() {

@Test
public void testUpCheck() throws IOException {
assertTrue(orion.upCheck());
assertTrue(enclave.upCheck());
}

@Test
Expand All @@ -67,10 +67,11 @@ public void testSendAndReceive() throws IOException {

SendRequest sc =
new SendRequest(PAYLOAD, publicKeys.get(0), Lists.newArrayList(publicKeys.get(1)));
SendResponse sr = orion.send(sc);
SendResponse sr = enclave.send(sc);

PrivacyPrecompiledContract privacyPrecompiledContract =
new PrivacyPrecompiledContract(new SpuriousDragonGasCalculator(), publicKeys.get(0), orion);
new PrivacyPrecompiledContract(
new SpuriousDragonGasCalculator(), publicKeys.get(0), enclave);

BytesValue result =
privacyPrecompiledContract.compute(BytesValue.wrap(sr.getKey().getBytes(UTF_8)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.google.common.io.Files;

public class PrivacyParameters {
private static final String ORION_URL = "http://localhost:8888";
public static final URI DEFAULT_ORION_URL = URI.create(ORION_URL);
private static final String ENCLAVE_URL = "http://localhost:8888";
public static final URI DEFAULT_ENCLAVE_URL = URI.create(ENCLAVE_URL);

private Integer privacyAddress;
private boolean enabled;
Expand All @@ -40,7 +40,7 @@ public void setPublicKeyUsingFile(final File publicKeyFile) throws IOException {
public static PrivacyParameters noPrivacy() {
final PrivacyParameters config = new PrivacyParameters();
config.setEnabled(false);
config.setUrl(ORION_URL);
config.setUrl(ENCLAVE_URL);
config.setPrivacyAddress(Address.PRIVACY);
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

import static java.nio.charset.StandardCharsets.UTF_8;

import tech.pegasys.pantheon.enclave.Enclave;
import tech.pegasys.pantheon.enclave.types.ReceiveRequest;
import tech.pegasys.pantheon.enclave.types.ReceiveResponse;
import tech.pegasys.pantheon.ethereum.core.Gas;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.mainnet.AbstractPrecompiledContract;
import tech.pegasys.pantheon.ethereum.vm.GasCalculator;
import tech.pegasys.pantheon.orion.Orion;
import tech.pegasys.pantheon.orion.types.ReceiveRequest;
import tech.pegasys.pantheon.orion.types.ReceiveResponse;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.io.IOException;
Expand All @@ -29,21 +29,21 @@
import org.apache.logging.log4j.Logger;

public class PrivacyPrecompiledContract extends AbstractPrecompiledContract {
private final Orion orion;
private final String orionPublicKey;
private final Enclave enclave;
private final String enclavePublicKey;

private static final Logger LOG = LogManager.getLogger();

public PrivacyPrecompiledContract(
final GasCalculator gasCalculator, final PrivacyParameters privacyParameters) {
this(gasCalculator, privacyParameters.getPublicKey(), new Orion(privacyParameters.getUrl()));
this(gasCalculator, privacyParameters.getPublicKey(), new Enclave(privacyParameters.getUrl()));
}

PrivacyPrecompiledContract(
final GasCalculator gasCalculator, final String publicKey, final Orion orion) {
final GasCalculator gasCalculator, final String publicKey, final Enclave enclave) {
super("Privacy", gasCalculator);
this.orion = orion;
this.orionPublicKey = publicKey;
this.enclave = enclave;
this.enclavePublicKey = publicKey;
}

@Override
Expand All @@ -55,13 +55,13 @@ public Gas gasRequirement(final BytesValue input) {
public BytesValue compute(final BytesValue input) {
try {
String key = new String(input.extractArray(), UTF_8);
ReceiveRequest receiveRequest = new ReceiveRequest(key, orionPublicKey);
ReceiveResponse receiveResponse = orion.receive(receiveRequest);
ReceiveRequest receiveRequest = new ReceiveRequest(key, enclavePublicKey);
ReceiveResponse receiveResponse = enclave.receive(receiveRequest);
LOG.info("Got the response as ", receiveResponse.getPayload());
return BytesValue.wrap(receiveResponse.getPayload());
// pass it to private tx processor
} catch (IOException e) {
LOG.fatal("Orion threw an unhandled exception.", e);
LOG.fatal("Enclave threw an unhandled exception.", e);
return BytesValue.EMPTY;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
*/
package tech.pegasys.pantheon.ethereum.privacy;

import tech.pegasys.pantheon.enclave.Enclave;
import tech.pegasys.pantheon.enclave.types.SendRequest;
import tech.pegasys.pantheon.enclave.types.SendResponse;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.core.Transaction;
import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput;
import tech.pegasys.pantheon.orion.Orion;
import tech.pegasys.pantheon.orion.types.SendRequest;
import tech.pegasys.pantheon.orion.types.SendResponse;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.bytes.BytesValues;

Expand All @@ -31,25 +31,25 @@

public class PrivateTransactionHandler {

private final Orion orion;
private final Enclave enclave;
private final Address privacyPrecompileAddress;

public PrivateTransactionHandler(final PrivacyParameters privacyParameters) {
this(
new Orion(privacyParameters.getUrl()),
new Enclave(privacyParameters.getUrl()),
Address.privacyPrecompiled(privacyParameters.getPrivacyAddress()));
}

public PrivateTransactionHandler(final Orion orion, final Address privacyPrecompileAddress) {
this.orion = orion;
public PrivateTransactionHandler(final Enclave enclave, final Address privacyPrecompileAddress) {
this.enclave = enclave;
this.privacyPrecompileAddress = privacyPrecompileAddress;
}

public Transaction handle(final PrivateTransaction privateTransaction) throws IOException {
final SendRequest sendRequest = createSendRequest(privateTransaction);
final SendResponse sendResponse;
try {
sendResponse = orion.send(sendRequest);
sendResponse = enclave.send(sendRequest);
} catch (IOException e) {
throw e;
}
Expand Down
Loading

0 comments on commit cd81881

Please sign in to comment.