Skip to content

Commit

Permalink
feat: added new contract to showcase assembly address operations (#459)…
Browse files Browse the repository at this point in the history
… (#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
quiet-node authored Oct 20, 2023
1 parent 46ee829 commit dd2e502
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
33 changes: 33 additions & 0 deletions contracts/solidity/address/AssemblyAddress.sol
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)
}
}
}

1 change: 1 addition & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const Contract = {
Transaction: 'Transaction',
MessageFrameAddresses: 'MessageFrameAddresses',
New: 'New',
AssemblyAddress: 'AssemblyAddress',
AddressContract: 'AddressContract',
Recipient: 'Recipient',
Inheritance: 'Inheritance',
Expand Down
72 changes: 72 additions & 0 deletions test/solidity/address/AssemblyAddress.js
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)
})
})

0 comments on commit dd2e502

Please sign in to comment.