forked from PegaSysEng/pantheon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PRIV] Implement privacy precompiled contract (PegaSysEng#696)
* Implement privacy precompiled contract * Update gradle dependency version * Fix privacy precompiled contract unit tests * Update Privacy Integration test
- Loading branch information
1 parent
8553f6a
commit 1e3933e
Showing
11 changed files
with
225 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...theon/ethereum/mainnet/precompiles/privacy/PrivacyPrecompiledContractIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* 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.ethereum.mainnet.precompiles.privacy; | ||
|
||
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.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; | ||
import java.util.List; | ||
|
||
import com.google.common.collect.Lists; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
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 OrionTestHarness testHarness; | ||
|
||
@BeforeClass | ||
public static void setUpOnce() throws Exception { | ||
folder.create(); | ||
|
||
testHarness = OrionTestHarness.create(folder.newFolder().toPath()); | ||
|
||
orion = new Orion(testHarness.clientUrl()); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownOnce() { | ||
testHarness.getOrion().stop(); | ||
} | ||
|
||
@Test | ||
public void testUpCheck() throws IOException { | ||
assertTrue(orion.upCheck()); | ||
} | ||
|
||
@Test | ||
public void testSendAndReceive() throws IOException { | ||
List<String> publicKeys = testHarness.getPublicKeys(); | ||
|
||
SendRequest sc = | ||
new SendRequest(PAYLOAD, publicKeys.get(0), Lists.newArrayList(publicKeys.get(1))); | ||
SendResponse sr = orion.send(sc); | ||
|
||
PrivacyPrecompiledContract privacyPrecompiledContract = | ||
new PrivacyPrecompiledContract(new SpuriousDragonGasCalculator(), publicKeys.get(0), orion); | ||
|
||
BytesValue result = | ||
privacyPrecompiledContract.compute(BytesValue.wrap(sr.getKey().getBytes(UTF_8))); | ||
|
||
String expected = new String(result.extractArray(), UTF_8); | ||
|
||
assertEquals(PAYLOAD, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...pegasys/pantheon/ethereum/mainnet/precompiles/privacy/PrivacyPrecompiledContractTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* 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.ethereum.mainnet.precompiles.privacy; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import tech.pegasys.pantheon.ethereum.mainnet.SpuriousDragonGasCalculator; | ||
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; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class PrivacyPrecompiledContractTest { | ||
private final String actual = "Test String"; | ||
private final String publicKey = "public key"; | ||
private final BytesValue key = BytesValue.wrap(actual.getBytes(UTF_8)); | ||
private PrivacyPrecompiledContract privacyPrecompiledContract; | ||
private PrivacyPrecompiledContract brokenPrivateTransactionHandler; | ||
|
||
Orion mockOrion() throws IOException { | ||
Orion mockOrion = mock(Orion.class); | ||
ReceiveResponse response = new ReceiveResponse(actual.getBytes(UTF_8)); | ||
when(mockOrion.receive(any(ReceiveRequest.class))).thenReturn(response); | ||
return mockOrion; | ||
} | ||
|
||
Orion brokenMockOrion() throws IOException { | ||
Orion mockOrion = mock(Orion.class); | ||
when(mockOrion.receive(any(ReceiveRequest.class))).thenThrow(IOException.class); | ||
return mockOrion; | ||
} | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
privacyPrecompiledContract = | ||
new PrivacyPrecompiledContract(new SpuriousDragonGasCalculator(), publicKey, mockOrion()); | ||
brokenPrivateTransactionHandler = | ||
new PrivacyPrecompiledContract( | ||
new SpuriousDragonGasCalculator(), publicKey, brokenMockOrion()); | ||
} | ||
|
||
@Test | ||
public void testPrivacyPrecompiledContract() { | ||
|
||
final BytesValue expected = privacyPrecompiledContract.compute(key); | ||
|
||
String exp = new String(expected.extractArray(), UTF_8); | ||
assertThat(exp).isEqualTo(actual); | ||
} | ||
|
||
@Test | ||
public void enclaveIsDownWhileHandling() { | ||
final BytesValue expected = brokenPrivateTransactionHandler.compute(key); | ||
|
||
assertThat(expected).isEqualTo(BytesValue.EMPTY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,4 +82,4 @@ dependencyManagement { | |
|
||
dependency 'org.xerial.snappy:snappy-java:1.1.7.2' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo= |