Skip to content

Commit

Permalink
Add support for _codehash in scilla contracts (#1358)
Browse files Browse the repository at this point in the history
* Add support for _codehash in scilla contracts

* Add support for _codehash in scilla contracts

* Enable test scilla/Codehash.ts

* Update scilla.rs to follow rustfmt

* Update CHANGELOG.md
  • Loading branch information
joseph-zilliqa authored Sep 5, 2024
1 parent 6c8c343 commit 548bbff
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion evm_scilla_js_tests/test/scilla/Codehash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
19 changes: 17 additions & 2 deletions zilliqa/src/scilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -29,13 +29,16 @@ use serde::{
Deserialize, Deserializer, Serialize,
};
use serde_json::Value;
use sha2::Sha256;
use sha3::{digest::DynDigest, Digest};
use tokio::runtime;
use tracing::trace;

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},
};
Expand Down Expand Up @@ -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()),
}
Expand Down

0 comments on commit 548bbff

Please sign in to comment.