mirror of https://github.com/hyperledger/besu
[PRIV] Implement privacy precompiled contract (#696)
* Implement privacy precompiled contract * Update gradle dependency version * Fix privacy precompiled contract unit tests * Update Privacy Integration test Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>pull/2/head
parent
fcb6efe532
commit
61bdcc879a
@ -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); |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
} |
@ -0,0 +1 @@ |
||||
A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo= |
Loading…
Reference in new issue