Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Improve solidity compilation process #270

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 40 additions & 30 deletions bin/build-abi
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
#!/usr/bin/env bash
set -euo pipefail

echo "Compiling contracts ..."

cd $CONTRACTS_DIR

hardhat compile
#!/usr/bin/env python
import json
import os
import subprocess
import sys
from pathlib import Path

try:
subprocess.check_call(["hardhat", "compile"])
except subprocess.CalledProcessError as exc:
print("Command 'hardhat compile' failed")
sys.exit(exc.returncode)

# hardhat removes files in artifacts directory when compiling -> put these files elsewhere
contracts_dir = Path(os.environ["CONTRACTS_DIR"])
abi_dir = contracts_dir / "abi"
artifacts_dir = contracts_dir / "artifacts"

# For each solidity file (respectively its artifact output directory)
for directory in artifacts_dir.rglob("*.sol"):

for hardhat_json in directory.glob("*.json"):
if str(hardhat_json).endswith(".dbg.json"):
continue

# for recursive globbing
shopt -s globstar
contract_name = hardhat_json.stem
contract_file = hardhat_json.parent
out_dir = abi_dir / contract_file.relative_to(artifacts_dir) / contract_name

# glob empty if no match, avoid matches like "*.sol"
shopt -s nullglob
abi_out = out_dir / "abi.json"

# For each solidity file (or its artifact output directory)
for directory in artifacts/**/*.sol; do
if abi_out.exists() and abi_out.stat().st_mtime > hardhat_json.stat().st_mtime:
print(f"Up to date: {contract_name}")
continue

# For each contract in the solidity file
for combined in $directory/*.json; do
print(f"Extracting ABI for {contract_name}")

if [[ $combined == *.dbg.json ]]; then
continue
fi
os.makedirs(out_dir, exist_ok=True)

basename_json="${combined##*/}"
basename="${basename_json/%.json}"
with open(hardhat_json) as f:
output = json.load(f)

outdir="$directory/$basename"
with open(abi_out, "w") as f:
json.dump(output["abi"], f)

echo "$combined -> $outdir"
mkdir -p "$outdir"
with open(out_dir / "bin.txt", "w") as f:
print(output["bytecode"], file=f)

# extract .abi and .bytecode fields
cat "$combined" | jq -r .abi > "$outdir/abi.json"
cat "$combined" | jq -r .bytecode > "$outdir/bin.txt"
done
done

echo "Finished extracting ABIs"
print("Finished extracting ABIs")
3 changes: 3 additions & 0 deletions contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ __pycache__/
typechain-types/

deployments/localhost/

# TODO: remove this when build-abi script is removed
/abi/
3 changes: 0 additions & 3 deletions contracts/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,3 @@ default-features = false

[dev-dependencies]
proptest = "1.0.0"

[build-dependencies]
ethers-contract-abigen = { git = "https://github.com/gakonst/ethers-rs" }
2 changes: 1 addition & 1 deletion contracts/rust/src/bn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn deploy_contract() -> Result<TestBN254<SignerMiddleware<Provider<Http>,
let client = get_funded_deployer().await.unwrap();
let contract = deploy(
client.clone(),
Path::new("../artifacts/contracts/mocks/TestBN254.sol/TestBN254"),
Path::new("../abi/contracts/mocks/TestBN254.sol/TestBN254"),
(),
)
.await
Expand Down
4 changes: 2 additions & 2 deletions contracts/rust/src/cape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ mod tests {
let client = get_funded_deployer().await.unwrap();
let call = deploy(
client.clone(),
Path::new("../artifacts/contracts/mocks/TestCAPE.sol/TestCAPE"),
Path::new("../abi/contracts/mocks/TestCAPE.sol/TestCAPE"),
CAPEConstructorArgs::new(5, 2).generic_into::<(u8, u64)>(),
)
.await;
Expand Down Expand Up @@ -768,7 +768,7 @@ mod tests {
let client = get_funded_deployer().await.unwrap();
let contract = deploy(
client.clone(),
Path::new("../artifacts/contracts/mocks/TestCapeTypes.sol/TestCapeTypes"),
Path::new("../abi/contracts/mocks/TestCapeTypes.sol/TestCapeTypes"),
(),
)
.await
Expand Down
2 changes: 1 addition & 1 deletion contracts/rust/src/cape_e2e_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async fn test_2user_maybe_submit(should_submit: bool) -> Result<()> {
let contract_address: Address = deploy(
client.clone(),
// TODO using mock contract to be able to manually add root
Path::new("../artifacts/contracts/mocks/TestCAPE.sol/TestCAPE"),
Path::new("../abi/contracts/mocks/TestCAPE.sol/TestCAPE"),
CAPEConstructorArgs::new(
MERKLE_HEIGHT,
CapeContractState::RECORD_ROOT_HISTORY_SIZE as u64,
Expand Down
2 changes: 1 addition & 1 deletion contracts/rust/src/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async fn link_unlinked_libraries<M: 'static + Middleware>(
Ok(val) => val.parse::<Address>()?,
Err(_) => deploy(
client.clone(),
Path::new("../artifacts/contracts/libraries/RescueLib.sol/RescueLib"),
Path::new("../abi/contracts/libraries/RescueLib.sol/RescueLib"),
(),
)
.await?
Expand Down
2 changes: 1 addition & 1 deletion contracts/rust/src/records_merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) async fn get_contract_records_merkle_tree(
let client = ethereum::get_funded_deployer().await.unwrap();
let contract = ethereum::deploy(
client.clone(),
Path::new("../artifacts/contracts/mocks/TestRecordsMerkleTree.sol/TestRecordsMerkleTree"),
Path::new("../abi/contracts/mocks/TestRecordsMerkleTree.sol/TestRecordsMerkleTree"),
height,
)
.await
Expand Down
2 changes: 1 addition & 1 deletion contracts/rust/src/records_merkle_tree/rescue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod tests {
let client = ethereum::get_funded_deployer().await.unwrap();
let contract = ethereum::deploy(
client.clone(),
Path::new("../artifacts/contracts/mocks/TestRescue.sol/TestRescue"),
Path::new("../abi/contracts/mocks/TestRescue.sol/TestRescue"),
(),
)
.await
Expand Down
2 changes: 1 addition & 1 deletion contracts/rust/src/root_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async fn test_root_store() -> Result<()> {
let client = get_funded_deployer().await?;
let contract = deploy(
client.clone(),
Path::new("../artifacts/contracts/mocks/TestRootStore.sol/TestRootStore"),
Path::new("../abi/contracts/mocks/TestRootStore.sol/TestRootStore"),
(3u64,), /* num_roots */
)
.await?;
Expand Down
2 changes: 1 addition & 1 deletion contracts/rust/src/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn deploy() -> TestTranscript<SignerMiddleware<Provider<Http>, Wallet<Sign
let client = ethereum::get_funded_deployer().await.unwrap();
let contract = ethereum::deploy(
client.clone(),
Path::new("../artifacts/contracts/mocks/TestTranscript.sol/TestTranscript"),
Path::new("../abi/contracts/mocks/TestTranscript.sol/TestTranscript"),
(),
)
.await
Expand Down
20 changes: 10 additions & 10 deletions contracts/rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,43 @@ const GATE_WIDTH: usize = 4;

abigen!(
TestBN254,
"../artifacts/contracts/mocks/TestBN254.sol/TestBN254/abi.json",
"../abi/contracts/mocks/TestBN254.sol/TestBN254/abi.json",
event_derives(serde::Deserialize, serde::Serialize);

TestRecordsMerkleTree,
"../artifacts/contracts/mocks/TestRecordsMerkleTree.sol/TestRecordsMerkleTree/abi.json",
"../abi/contracts/mocks/TestRecordsMerkleTree.sol/TestRecordsMerkleTree/abi.json",
event_derives(serde::Deserialize, serde::Serialize);

TestRescue,
"../artifacts/contracts/mocks/TestRescue.sol/TestRescue/abi.json",
"../abi/contracts/mocks/TestRescue.sol/TestRescue/abi.json",
event_derives(serde::Deserialize, serde::Serialize);

TestRootStore,
"../artifacts/contracts/mocks/TestRootStore.sol/TestRootStore/abi.json",
"../abi/contracts/mocks/TestRootStore.sol/TestRootStore/abi.json",
event_derives(serde::Deserialize, serde::Serialize);

TestTranscript,
"../artifacts/contracts/mocks/TestTranscript.sol/TestTranscript/abi.json",
"../abi/contracts/mocks/TestTranscript.sol/TestTranscript/abi.json",
event_derives(serde::Deserialize, serde::Serialize);

CAPE,
"../artifacts/contracts/CAPE.sol/CAPE/abi.json",
"../abi/contracts/CAPE.sol/CAPE/abi.json",
event_derives(serde::Deserialize, serde::Serialize);

PlonkVerifier,
"../artifacts/contracts/verifier/PlonkVerifier.sol/PlonkVerifier/abi.json",
"../abi/contracts/verifier/PlonkVerifier.sol/PlonkVerifier/abi.json",
event_derives(serde::Deserialize, serde::Serialize);

TestCapeTypes,
"../artifacts/contracts/mocks/TestCapeTypes.sol/TestCapeTypes/abi.json",
"../abi/contracts/mocks/TestCapeTypes.sol/TestCapeTypes/abi.json",
event_derives(serde::Deserialize, serde::Serialize);

TestCAPE,
"../artifacts/contracts/mocks/TestCAPE.sol/TestCAPE/abi.json",
"../abi/contracts/mocks/TestCAPE.sol/TestCAPE/abi.json",
event_derives(serde::Deserialize, serde::Serialize);

Greeter,
"../artifacts/contracts/Greeter.sol/Greeter/abi.json",
"../abi/contracts/Greeter.sol/Greeter/abi.json",
event_derives(serde::Deserialize, serde::Serialize);
);

Expand Down
2 changes: 1 addition & 1 deletion contracts/rust/tests/ethereum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async fn deploy_contract() -> Result<Greeter<SignerMiddleware<Provider<Http>, Wa
let client = get_funded_deployer().await.unwrap();
let contract = deploy(
client.clone(),
Path::new("../artifacts/contracts/Greeter.sol/Greeter"),
Path::new("../abi/contracts/Greeter.sol/Greeter"),
("Initial Greeting".to_string(),),
)
.await
Expand Down
1 change: 1 addition & 0 deletions treefmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ includes = [
"**/*.py",
"bin/hdwallet-derive",
"bin/make-genesis-block",
"bin/build-abi",
]

[formatter.prettier]
Expand Down