Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store Testing Framework Solidity and Yul #1052

Merged
merged 16 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
* Copyright Consensys Software Inc.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.zktracer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

import net.consensys.linea.testing.SolidityUtils;
import net.consensys.linea.testing.ToyAccount;
import net.consensys.linea.testing.ToyExecutionEnvironmentV2;
import net.consensys.linea.testing.ToyTransaction;
import net.consensys.linea.testing.generated.FrameworkEntrypoint;
import net.consensys.linea.testing.generated.TestSnippet_Events;
import net.consensys.linea.testing.generated.TestStorage;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.crypto.SECP256K1;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.processing.TransactionProcessingResult;
import org.hyperledger.besu.evm.log.Log;
import org.junit.jupiter.api.Test;
import org.web3j.abi.EventEncoder;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.generated.Uint256;

public class ExampleSolidityTest {

@Test
void test_with_framework_entrypoint() {
gauravahuja marked this conversation as resolved.
Show resolved Hide resolved
KeyPair keyPair = new SECP256K1().generateKeyPair();
Address senderAddress = Address.extract(Hash.hash(keyPair.getPublicKey().getEncodedBytes()));

ToyAccount senderAccount =
ToyAccount.builder().balance(Wei.fromEth(1)).nonce(5).address(senderAddress).build();

ToyAccount frameworkEntrypointAccount =
ToyAccount.builder()
.address(Address.fromHexString("0x22222"))
.balance(Wei.ONE)
.nonce(5)
.code(SolidityUtils.getContractByteCode(FrameworkEntrypoint.class))
.build();

ToyAccount snippetAccount =
ToyAccount.builder()
.address(Address.fromHexString("0x11111"))
.balance(Wei.ONE)
.nonce(6)
.code(SolidityUtils.getContractByteCode(TestSnippet_Events.class))
.build();

Function snippetFunction =
new Function(
TestSnippet_Events.FUNC_EMITDATANOINDEXES,
List.of(new Uint256(BigInteger.valueOf(123456))),
Collections.emptyList());

FrameworkEntrypoint.ContractCall snippetContractCall =
new FrameworkEntrypoint.ContractCall(
/*Address*/ snippetAccount.getAddress().toHexString(),
/*calldata*/ Bytes.fromHexStringLenient(FunctionEncoder.encode(snippetFunction))
.toArray(),
/*gasLimit*/ BigInteger.ZERO,
/*value*/ BigInteger.ZERO,
/*callType*/ BigInteger.ZERO);

List<FrameworkEntrypoint.ContractCall> contractCalls = List.of(snippetContractCall);

Function frameworkEntryPointFunction =
new Function(
FrameworkEntrypoint.FUNC_EXECUTECALLS,
List.of(new DynamicArray<>(FrameworkEntrypoint.ContractCall.class, contractCalls)),
Collections.emptyList());
Bytes txPayload =
Bytes.fromHexStringLenient(FunctionEncoder.encode(frameworkEntryPointFunction));

Transaction tx =
ToyTransaction.builder()
.sender(senderAccount)
.to(frameworkEntrypointAccount)
.payload(txPayload)
.keyPair(keyPair)
.build();

Consumer<TransactionProcessingResult> resultValidator =
(TransactionProcessingResult result) -> {
// One event from the snippet
// One event from the framework entrypoint about contract call
assertEquals(result.getLogs().size(), 2);
for (Log log : result.getLogs()) {
String logTopic = log.getTopics().getFirst().toHexString();
if (EventEncoder.encode(TestSnippet_Events.DATANOINDEXES_EVENT).equals(logTopic)) {
TestSnippet_Events.DataNoIndexesEventResponse response =
TestSnippet_Events.getDataNoIndexesEventFromLog(SolidityUtils.fromBesuLog(log));
assertEquals(response.singleInt, BigInteger.valueOf(123456));
} else if (EventEncoder.encode(FrameworkEntrypoint.CALLEXECUTED_EVENT)
.equals(logTopic)) {
FrameworkEntrypoint.CallExecutedEventResponse response =
FrameworkEntrypoint.getCallExecutedEventFromLog(SolidityUtils.fromBesuLog(log));
assertTrue(response.isSuccess);
assertEquals(response.destination, snippetAccount.getAddress().toHexString());
} else {
fail();
}
}
};

ToyExecutionEnvironmentV2.builder()
.accounts(List.of(senderAccount, frameworkEntrypointAccount, snippetAccount))
.transaction(tx)
.testValidator(resultValidator)
.build()
.run();
}

@Test
void test_snippet_independently() {
gauravahuja marked this conversation as resolved.
Show resolved Hide resolved
KeyPair keyPair = new SECP256K1().generateKeyPair();
Address senderAddress = Address.extract(Hash.hash(keyPair.getPublicKey().getEncodedBytes()));

ToyAccount senderAccount =
ToyAccount.builder().balance(Wei.fromEth(1)).nonce(5).address(senderAddress).build();

ToyAccount contractAccount =
ToyAccount.builder()
.address(Address.fromHexString("0x11111"))
.balance(Wei.ONE)
.nonce(6)
.code(SolidityUtils.getContractByteCode(TestSnippet_Events.class))
.build();

Function function =
new Function(
TestSnippet_Events.FUNC_EMITDATANOINDEXES,
List.of(new Uint256(BigInteger.valueOf(123456))),
Collections.emptyList());
String encodedFunction = FunctionEncoder.encode(function);
Bytes txPayload = Bytes.fromHexStringLenient(encodedFunction);

Transaction tx =
ToyTransaction.builder()
.sender(senderAccount)
.to(contractAccount)
.payload(txPayload)
.keyPair(keyPair)
.build();

Consumer<TransactionProcessingResult> resultValidator =
(TransactionProcessingResult result) -> {
assertEquals(result.getLogs().size(), 1);
TestSnippet_Events.DataNoIndexesEventResponse response =
TestSnippet_Events.getDataNoIndexesEventFromLog(
SolidityUtils.fromBesuLog(result.getLogs().getFirst()));
assertEquals(response.singleInt, BigInteger.valueOf(123456));
};

ToyExecutionEnvironmentV2.builder()
.accounts(List.of(senderAccount, contractAccount))
.transaction(tx)
.testValidator(resultValidator)
.build()
.run();
}

@Test
void test_contract_not_related_to_testing_framework() {
gauravahuja marked this conversation as resolved.
Show resolved Hide resolved
KeyPair senderkeyPair = new SECP256K1().generateKeyPair();
Address senderAddress =
Address.extract(Hash.hash(senderkeyPair.getPublicKey().getEncodedBytes()));

ToyAccount senderAccount =
ToyAccount.builder().balance(Wei.fromEth(1)).nonce(5).address(senderAddress).build();

ToyAccount contractAccount =
ToyAccount.builder()
.address(Address.fromHexString("0x11111"))
.balance(Wei.ONE)
.nonce(6)
.code(SolidityUtils.getContractByteCode(TestStorage.class))
.build();

Function function =
new Function(
TestStorage.FUNC_STORE,
List.of(new Uint256(BigInteger.valueOf(3))),
Collections.emptyList());
String encodedFunction = FunctionEncoder.encode(function);
Bytes txPayload = Bytes.fromHexStringLenient(encodedFunction);
Transaction tx =
ToyTransaction.builder()
.sender(senderAccount)
.to(contractAccount)
.payload(txPayload)
.keyPair(senderkeyPair)
.build();

ToyExecutionEnvironmentV2.builder()
.accounts(List.of(senderAccount, contractAccount))
.transaction(tx)
.build()
.run();
}
}
20 changes: 20 additions & 0 deletions testing/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import org.web3j.solidity.gradle.plugin.EVMVersion
import org.web3j.solidity.gradle.plugin.OutputComponent

plugins {
id "common-plugins"
id "org.web3j" version "4.12.2"
}

apply from: rootProject.file("gradle/corset.gradle")
Expand Down Expand Up @@ -34,3 +38,19 @@ dependencies {
implementation 'org.mockito:mockito-junit-jupiter'
implementation 'org.assertj:assertj-core'
}

nodeSetup {
mustRunAfter(spotlessBash, spotlessGroovy, spotlessSol)
}



solidity {
optimize = false
prettyJson = true
evmVersion = EVMVersion.LONDON
}

web3j {
generatedPackageName = "net.consensys.linea.testing.generated"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Consensys Software Inc.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.testing;

import java.net.URL;
import java.util.Iterator;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.evm.log.LogTopic;
import org.web3j.protocol.core.methods.response.Log;
import org.web3j.tx.Contract;

public class SolidityUtils {
private static final ObjectMapper objectMapper = new ObjectMapper();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using here JsonConverter class the embeds the ObjectMapper and offers some helpful functionalities.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API for JsonConverter requires the Class that can parsed from the json file. I do not have a Class for that, I am interested in just one particular attribute 'bin-runtime' of a specific contract in the json file.

private static final ClassLoader classLoader = SolidityUtils.class.getClassLoader();

public static Bytes getContractByteCode(Class<? extends Contract> contractClass) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super clear on what's happening here. I think it would be helpful to write some amount of documentation to explain (at a high level) what's going on.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some documentation for this method.

String contractResourcePath = String.format("solidity/%s.json", contractClass.getSimpleName());
gauravahuja marked this conversation as resolved.
Show resolved Hide resolved
URL contractResourceURL = classLoader.getResource(contractResourcePath);
try {
JsonNode jsonRoot = objectMapper.readTree(contractResourceURL);
Iterator<Map.Entry<String, JsonNode>> contracts = jsonRoot.get("contracts").fields();
while (contracts.hasNext()) {
Map.Entry<String, JsonNode> contract = contracts.next();
if (contract.getKey().contains(contractClass.getSimpleName())) {
return Bytes.fromHexStringLenient(contract.getValue().get("bin-runtime").asText());
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
throw new RuntimeException("Could not find contract bytecode");
}

public static Log fromBesuLog(org.hyperledger.besu.evm.log.Log log) {
return new Log(
false,
"",
"",
"",
"",
"",
log.getLogger().toHexString(),
log.getData().toHexString(),
"",
log.getTopics().stream().map(LogTopic::toHexString).toList());
}
}
61 changes: 61 additions & 0 deletions testing/src/main/solidity/DynamicBytecode.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
object "DynamicBytecode" {
code {
// return the bytecode of the contract
datacopy(0x00, dataoffset("runtime"), datasize("runtime"))
return(0x00, datasize("runtime"))
}

object "runtime" {
code {
switch selector()
case 0xa770741d // Write()
{
write()
}

case 0x97deb47b // Read()
{
// store the ID to memory at 0x00
mstore(0x00, sload(0x00))

return (0x00,0x20)
}

default {
// if the function signature sent does not match any
// of the contract functions, revert
revert(0, 0)
}


function write() {
// take no inputs, push the 1234...EF 32 byte integer onto the stack
// add 0 as the slot key to the stack
// store the key/value
verbatim_0i_0o(hex"7f0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF600055")
}

// Return the function selector: the first 4 bytes of the call data
function selector() -> s {
s := div(calldataload(0), 0x100000000000000000000000000000000000000000000000000000000)
}

// Implementation of the require statement from Solidity
function require(condition) {
if iszero(condition) { revert(0, 0) }
}

// Check if the calldata has the correct number of params
function checkParamLength(len) {
require(eq(calldatasize(), add(4, mul(32, len))))
}

// Transfer ether to the caller address
function transfer(amount) {
if iszero(call(gas(), caller(), amount, 0, 0, 0, 0)) {
revert(0,0)
}
}
}
}
}
Loading
Loading