Skip to content

Commit

Permalink
Fix codehash to get only contract code hash (#3744) (#3747)
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan-zilliqa committed Aug 16, 2023
1 parent d5a7b3b commit 1e715f5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/libPersistence/ContractStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,12 @@ bool ContractStorage::FetchExternalStateValue(
type = "ByStr20";
}
} else if (query.name() == "_codehash") {
dev::h256 codeHash = account->GetCodeHash();
special_query = "\"0x" + codeHash.hex() + "\"";
type = "ByStr32";
dev::h256 codeHash;
if (account->GetContractCodeHash(codeHash)) {
special_query = "\"0x" + codeHash.hex() + "\"";
type = "ByStr32";
} else
return false;
} else if (query.name() == "_code") {
// Get the code directly from the account storage.
zbytes code = account->GetCode();
Expand Down
56 changes: 56 additions & 0 deletions tests/Data/AccountData/Test_Account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,62 @@ BOOST_AUTO_TEST_CASE(testInit) {
BOOST_CHECK_EQUAL(true, acc1.GetInitJson() == Json::arrayValue);
}

BOOST_AUTO_TEST_CASE(testContractCodeHash) {
LOG_MARKER();

Account acc1 = Account();

// Not contract
BOOST_CHECK_EQUAL(acc1.GetInitJson(), Json::arrayValue);
BOOST_CHECK_EQUAL(true, acc1.GetRawStorage(dev::h256(), true).empty());
BOOST_CHECK_EQUAL(acc1.GetStateJson(true), Json::arrayValue);
BOOST_CHECK_EQUAL(0, acc1.GetCode().size());

// Not contract
vector<StateEntry> entries;
entries.emplace_back("count", true, "Int32", "0");
BOOST_CHECK_EQUAL(false, acc1.SetStorage(entries));

std::pair<Json::Value, Json::Value> roots;
BOOST_CHECK_EQUAL(false, acc1.GetStorageJson(roots, true));

zbytes code = {'h', 'e', 'l', 'l', 'o'};
std::string message =
"[{\"vname\":\"_scilla_version\",\"type\":\"Uint32\",\"value\":\"0\"}]";
zbytes data = zbytes(message.begin(), message.end());

PubKey pubKey1 = Schnorr::GenKeyPair().second;
Address addr1 = Account::GetAddressFromPublicKey(pubKey1);

acc1.SetImmutable(code, data);

BOOST_CHECK_EQUAL(false, code == acc1.GetCode());
BOOST_CHECK_EQUAL(false, addr1 == acc1.GetAddress());

BOOST_CHECK_EQUAL(true,
acc1.InitContract(code, data, addr1, 0, scilla_version));
BOOST_CHECK_EQUAL(false,
acc1.InitContract(code, data, addr1, 0, scilla_version));

SHA256Calculator sha2_one, sha2_two;
sha2_one.Update(code);
sha2_one.Update(data);
dev::h256 code_hash = dev::h256(sha2_one.Finalize());
sha2_two.Update(code);
dev::h256 contract_code_hash = dev::h256(sha2_two.Finalize());

BOOST_CHECK_EQUAL(true, code == acc1.GetCode());
BOOST_CHECK_EQUAL(true, addr1 == acc1.GetAddress());
LOG_GENERAL(INFO,"GetContractCodeHash = "<< acc1.GetContractCodeHash());
LOG_GENERAL(INFO,"GetCodeHash = "<< acc1.GetCodeHash());
BOOST_CHECK_MESSAGE(
acc2.GetContractCodeHash() == contract_code_hash,
"expected: " << contract_code_hash << " actual: " << acc2.GetContractCodeHash() << "\n");
BOOST_CHECK_MESSAGE(
acc2.GetCodeHash() == code_hash,
"expected: " << code_hash << " actual: " << acc2.GetCodeHash() << "\n");
}

BOOST_AUTO_TEST_CASE(testBalance) {
LOG_MARKER();

Expand Down
5 changes: 5 additions & 0 deletions tests/EvmAcceptanceTests/test/scilla/Codehash.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
const {assert, expect} = require("chai");
import {ScillaContract} from "hardhat-scilla-plugin";
import {parallelizer} from "../../helpers";
const { createHash } = require('crypto');

describe("Codehash contract", () => {
let expectedCodeHash: string
let contract: ScillaContract;
it("Deploy codehash contract", async () => {
contract = await parallelizer.deployScillaContract("Codehash");
expectedCodeHash = '0x' + createHash('sha256').update(contract.code).digest('hex')
assert.isTrue(contract.address !== undefined);
});

it("Call code hash contract - Foo transition", async () => {
let tx1 = await contract.foo(contract.address!.toLowerCase());
const codeHash1 = tx1.receipt.event_logs[0].params[0].value;
expect(tx1.receipt.success).equal(true);
expect(expectedCodeHash).to.be.eq(codeHash1);
let tx2 = await contract.foo2(contract.address!.toLowerCase());
const codeHash2 = tx2.receipt.event_logs[0].params[0].value;
expect(codeHash1).to.be.eq(codeHash2);
expect(expectedCodeHash).to.be.eq(codeHash2);
});
});

0 comments on commit 1e715f5

Please sign in to comment.