-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added new contract to showcase assembly address operations (#459)…
… (#484) * feat: added new contract to showcase assembly address operations (#459) Signed-off-by: Logan Nguyen <[email protected]> * docs: added empty line and comment Signed-off-by: Logan Nguyen <[email protected]> * test: added @solidityevmequivx tag to unit test Signed-off-by: Logan Nguyen <[email protected]> --------- Signed-off-by: Logan Nguyen <[email protected]>
- Loading branch information
1 parent
46ee829
commit dd2e502
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.20; | ||
|
||
contract AssemblyAddress { | ||
function codesizeat(address addr) external view returns(uint256 size) { | ||
assembly { | ||
size := extcodesize(addr) | ||
} | ||
} | ||
|
||
function codehashat(address addr) external view returns (bytes32 hash) { | ||
assembly { | ||
hash := extcodehash(addr) | ||
} | ||
} | ||
|
||
function codecopyat(address addr) external view returns (bytes memory code) { | ||
assembly { | ||
// retrieve the size of the code, this needs assembly | ||
let size := extcodesize(addr) | ||
// allocate output byte array - this could also be done without assembly | ||
// by using code = new bytes(size) | ||
code := mload(0x40) | ||
// new "memory end" including padding | ||
mstore(0x40, add(code, and(add(add(size, 0x20), 0x1f), not(0x1f)))) | ||
// store length in memory | ||
mstore(code, size) | ||
// actually retrieve the code, this needs assembly | ||
extcodecopy(addr, add(code, 0x20), 0, size) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/*- | ||
* | ||
* Hedera JSON RPC Relay - Hardhat Example | ||
* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
const { expect } = require('chai') | ||
const { ethers } = require('hardhat') | ||
const Constants = require('../../constants') | ||
|
||
describe('@solidityevmequiv3 AssemblyAddress', () => { | ||
let assemblyAddressContract, expectedContractBytecode | ||
|
||
before(async () => { | ||
const assemblyAddressContractFactory = await ethers.getContractFactory( | ||
Constants.Contract.AssemblyAddress | ||
) | ||
|
||
assemblyAddressContract = await assemblyAddressContractFactory.deploy() | ||
expectedContractBytecode = await ethers.provider.getCode( | ||
assemblyAddressContract.address | ||
) | ||
}) | ||
|
||
it("Should get contract's code size at contract address", async () => { | ||
const contractCodeSize = await assemblyAddressContract.codesizeat( | ||
assemblyAddressContract.address | ||
) | ||
|
||
// @notice Remove the '0x' prefix from the expected contract bytecode, then calculate the length in bytes | ||
// @notice Since each hexadeimal character represents 4 bits, and each bute is represented by 2 hexadecimal characters. | ||
// Therefore, the length of bytecode in bytes is half of the length of the bytecode in hexadecimal characters. | ||
const expectedContractCodeSize = | ||
expectedContractBytecode.replace('0x', '').length / 2 | ||
|
||
expect(contractCodeSize).to.eq(expectedContractCodeSize) | ||
}) | ||
|
||
it("Should get contract's code hash at contract address", async () => { | ||
const contractCodeHash = await assemblyAddressContract.codehashat( | ||
assemblyAddressContract.address | ||
) | ||
|
||
const expectedContractCodeHash = ethers.utils.keccak256( | ||
expectedContractBytecode | ||
) | ||
|
||
expect(contractCodeHash).to.eq(expectedContractCodeHash) | ||
}) | ||
|
||
it("Should get contract's code at contract address", async () => { | ||
const contractCode = await assemblyAddressContract.codecopyat( | ||
assemblyAddressContract.address | ||
) | ||
|
||
expect(contractCode).to.eq(expectedContractBytecode) | ||
}) | ||
}) |