Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added DataAllocation solidity example contract #525

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions contracts/yul/data-allocation/DataAllocation.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

contract DataAllocation {
uint256 a = 0;
uint256 b = 12;

/// mstore - mem[p…(p+32)) := v
/// mload - mem[p…(p+32))
function allocateMemory(uint256 p, uint256 v) external pure returns (uint256 n) {
assembly {
mstore(p, v)
n := mload(p)
}
return n;
}

/// mstore8 - mem[p] := v & 0xff (only modifies a single byte)
/// mload - mem[p…(p+32))
function allocateMemory8(uint256 p, uint8 v) external pure returns (uint8 n) {
bytes1 value;
assembly {
mstore8(p, v)
value := mload(p)
}
n = uint8(value);
}

/// sload - storage[p]
function sload(uint256 p) external view returns (uint256 n) {
assembly {
n := sload(p)
}
}

/// sstore - storage[p] := v
function sstore(uint256 p, uint256 v) external {
assembly {
sstore(p, v)
}
}
}

69 changes: 69 additions & 0 deletions test/yul/data-allocation/DataAllocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*-
*
* Hedera Smart Contracts
*
* 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')

describe('@solidityequiv5 Data Allocation Tests', () => {
let dataAllocationContract
const P = 32
const V = 72
const SLOT_0_KEY = 0
const SLOT_1_KEY = 1

before(async () => {
const dataAllocationContractFactory = await ethers.getContractFactory(
'DataAllocation'
)

dataAllocationContract = await dataAllocationContractFactory.deploy()
})

it('Should execute allocateMemory', async () => {
const result = await dataAllocationContract.allocateMemory(P, V)

expect(result).to.eq(V)
})

it('Should execute allocateMemory8', async () => {
const result = await dataAllocationContract.allocateMemory8(P, V)

expect(result).to.eq(V)
})

it('Should execute sload', async () => {
const EXPECTED_SLOT_0_VALUE = 0 // state variable `a`
const EXPECTED_SLOT_1_VALUE = 12 // state variable `b`

const result0 = await dataAllocationContract.sload(SLOT_0_KEY)
const result1 = await dataAllocationContract.sload(SLOT_1_KEY)

expect(result0).to.eq(EXPECTED_SLOT_0_VALUE)
expect(result1).to.eq(EXPECTED_SLOT_1_VALUE)
})

it('Should execute sstore', async () => {
await dataAllocationContract.sstore(SLOT_0_KEY, V)

const result = await dataAllocationContract.sload(SLOT_0_KEY)

expect(result).to.eq(V)
})
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Extra line.

Loading