diff --git a/CHANGELOG.md b/CHANGELOG.md index 29b60f01d..efb7e9083 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Unreleased changes. - [#1350](https://github.com/Zilliqa/zq2/pull/1350): Support `CHAINID`, `BLOCKNUMBER`, and `TIMESTAMP` in Scilla contracts. +- [#1358](https://github.com/Zilliqa/zq2/pull/1358): Support `_codehash` in Scilla contracts. - [#1390](https://github.com/Zilliqa/zq2/pull/1390): Implement `GetNumPeers` API endpoint to get the current number of peers. - [#1391](https://github.com/Zilliqa/zq2/pull/1391): Change response type of `GetMinimumGasPrice` to a string. - [#1389](https://github.com/Zilliqa/zq2/pull/1389): Implement `TxBlockListing` API endpoint to return a paginated list of blocks. diff --git a/evm_scilla_js_tests/test/scilla/Codehash.ts b/evm_scilla_js_tests/test/scilla/Codehash.ts index 58acc736d..7b38aa354 100644 --- a/evm_scilla_js_tests/test/scilla/Codehash.ts +++ b/evm_scilla_js_tests/test/scilla/Codehash.ts @@ -3,7 +3,7 @@ import {ScillaContract} from "hardhat-scilla-plugin"; import hre from "hardhat"; const {createHash} = require("crypto"); -xdescribe("Codehash contract #parallel", () => { +describe("Codehash contract #parallel", () => { let expectedCodeHash: string; let contract: ScillaContract; before(async () => { diff --git a/zilliqa/src/scilla.rs b/zilliqa/src/scilla.rs index 1d8263e50..535693115 100644 --- a/zilliqa/src/scilla.rs +++ b/zilliqa/src/scilla.rs @@ -12,7 +12,7 @@ use std::{ time::Duration, }; -use alloy::primitives::Address; +use alloy::{hex::ToHexExt, primitives::Address}; use anyhow::{anyhow, Result}; use base64::Engine; use bytes::{BufMut, Bytes, BytesMut}; @@ -29,6 +29,8 @@ use serde::{ Deserialize, Deserializer, Serialize, }; use serde_json::Value; +use sha2::Sha256; +use sha3::{digest::DynDigest, Digest}; use tokio::runtime; use tracing::trace; @@ -36,6 +38,7 @@ use crate::{ exec::{PendingState, StorageValue}, scilla_proto::{self, ProtoScillaQuery, ProtoScillaVal, ValType}, serde_util::{bool_as_str, num_as_str}, + state::Code, time::SystemTime, transaction::{ScillaGas, ZilAmount}, }; @@ -733,7 +736,19 @@ impl ActiveCall { Ok(Some((val, "ByStr20".to_owned()))) } "_codehash" => { - todo!() + let code_bytes = match &account.account.code { + Code::Evm(bytes) => bytes.clone(), + Code::Scilla { code, .. } => code.clone().into_bytes(), + }; + + let mut hasher = Sha256::new(); + DynDigest::update(&mut hasher, &code_bytes); + + let mut hash = [0u8; 32]; + DynDigest::finalize_into(hasher, &mut hash[..]).unwrap(); + + let val = scilla_val(format!("\"0x{}\"", hash.encode_hex()).into_bytes()); + Ok(Some((val, "ByStr32".to_owned()))) } _ => self.fetch_value_inner(addr, name.clone(), indices.clone()), }