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

evm: Save contract artifacts to build tree #2371

Merged
merged 4 commits into from
Aug 30, 2023
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
9 changes: 6 additions & 3 deletions lib/ain-contracts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

for (file_path, contract_name) in contracts {
let solc = Solc::new(env::var("SOLC_PATH")?);
let output_path = env::var("CARGO_TARGET_DIR")?;
let root = PathBuf::from(file_path);
if !root.exists() {
return Err("Project root {root:?} does not exists!".into());
Expand All @@ -40,13 +41,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let abi = artifact.abi.ok_or_else(|| format_err!("ABI not found"))?;
let bytecode = artifact.deployed_bytecode.expect("No bytecode found");

fs::create_dir_all(format!("{file_path}/output/"))?;
fs::create_dir_all(format!("{output_path}/ain_contracts/{file_path}"))?;
fs::write(
PathBuf::from(format!("{file_path}/output/bytecode.json")),
PathBuf::from(format!(
"{output_path}/ain_contracts/{file_path}/bytecode.json"
)),
serde_json::to_string(&bytecode).unwrap().as_bytes(),
)?;
fs::write(
PathBuf::from(format!("{file_path}/output/abi.json")),
PathBuf::from(format!("{output_path}/ain_contracts/{file_path}/abi.json")),
serde_json::to_string(&abi).unwrap().as_bytes(),
)?;
}
Expand Down
25 changes: 20 additions & 5 deletions lib/ain-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,43 @@ pub fn get_bytecode(input: &str) -> Result<Vec<u8>> {
}

pub fn get_counter_bytecode() -> Result<Vec<u8>> {
get_bytecode(include_str!("../dfi_intrinsics/output/bytecode.json"))
get_bytecode(include_str!(concat!(
env!("CARGO_TARGET_DIR"),
"/ain_contracts/dfi_intrinsics/bytecode.json"
)))
}

pub fn get_dst20_bytecode() -> Result<Vec<u8>> {
get_bytecode(include_str!("../dst20/output/bytecode.json"))
get_bytecode(include_str!(concat!(
env!("CARGO_TARGET_DIR"),
"/ain_contracts/dst20/bytecode.json"
)))
}

pub fn get_dst20_input() -> Result<Vec<u8>> {
get_bytecode(include_str!("../dst20/input.json"))
}

pub fn get_system_reserved_bytecode() -> Result<Vec<u8>> {
get_bytecode(include_str!("../system_reserved/output/bytecode.json"))
get_bytecode(include_str!(concat!(
env!("CARGO_TARGET_DIR"),
"/ain_contracts/system_reserved/bytecode.json"
)))
}

pub fn get_system_reserved_codehash() -> Result<H256> {
let bytecode = get_bytecode(include_str!("../system_reserved/output/bytecode.json"))?;
let bytecode = get_bytecode(include_str!(concat!(
env!("CARGO_TARGET_DIR"),
"/ain_contracts/system_reserved/bytecode.json"
)))?;
Ok(Blake2Hasher::hash(&bytecode))
}

pub fn get_dst20_codehash() -> Result<H256> {
let bytecode = get_bytecode(include_str!("../dst20/output/bytecode.json"))?;
let bytecode = get_bytecode(include_str!(concat!(
env!("CARGO_TARGET_DIR"),
"/ain_contracts/dst20/bytecode.json"
)))?;
Ok(Blake2Hasher::hash(&bytecode))
}

Expand Down
51 changes: 36 additions & 15 deletions test/functional/feature_dst20.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,27 +787,48 @@ def run_test(self):
)

# Contract ABI
# Temp. workaround
self.abi = open(
f"{os.path.dirname(__file__)}/../../lib/ain-contracts/dst20/output/abi.json",
"r",
encoding="utf8",
).read()
self.reserved_bytecode = json.loads(
open(
f"{os.path.dirname(__file__)}/../../lib/ain-contracts/system_reserved/output/bytecode.json",
if os.getenv("BUILD_DIR"):
build_dir = os.getenv("BUILD_DIR")
self.abi = open(
f"{build_dir}/ain_contracts/dst20/abi.json",
"r",
encoding="utf8",
).read()
)["object"]

self.bytecode = json.loads(
open(
f"{os.path.dirname(__file__)}/../../lib/ain-contracts/dst20/output/bytecode.json",
self.bytecode = json.loads(
open(
Copy link
Member

Choose a reason for hiding this comment

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

Let's abstract the path finder into a function, instead of if else of the whole codepath.

f"{build_dir}/ain_contracts/dst20/bytecode.json",
"r",
encoding="utf8",
).read()
)["object"]
self.reserved_bytecode = json.loads(
open(
f"{build_dir}/ain_contracts/system_reserved/bytecode.json",
"r",
encoding="utf8",
).read()
)["object"]
else:
# fall back to using relative path
self.abi = open(
f"{os.path.dirname(__file__)}/../../build/lib/target/ain_contracts/dst20/abi.json",
"r",
encoding="utf8",
).read()
)["object"]
self.bytecode = json.loads(
open(
f"{os.path.dirname(__file__)}/../../build/lib/target/ain_contracts/dst20/bytecode.json",
"r",
encoding="utf8",
).read()
)["object"]
self.reserved_bytecode = json.loads(
open(
f"{os.path.dirname(__file__)}/../../build/lib/target/ain_contracts/system_reserved/bytecode.json",
"r",
encoding="utf8",
).read()
)["object"]

# Generate chain
self.node.generate(150)
Expand Down
19 changes: 13 additions & 6 deletions test/functional/feature_evm_dfi_intrinsics.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ def run_test(self):
node.generate(1)

# check counter contract
# Temp. workaround
abi = open(
f"{os.path.dirname(__file__)}/../../lib/ain-contracts/dfi_intrinsics/output/abi.json",
"r",
encoding="utf8",
).read()
if os.getenv("BUILD_DIR"):
abi = open(
f"{os.getenv('BUILD_DIR')}/ain_contracts/dfi_intrinsics/abi.json",
"r",
encoding="utf8",
).read()
else:
abi = open(
f"{os.path.dirname(__file__)}/../../build/lib/target/ain_contracts/dfi_intrinsics/abi.json",
"r",
encoding="utf8",
).read()

counter_contract = node.w3.eth.contract(
address="0x0000000000000000000000000000000000000301", abi=abi
)
Expand Down
Loading