-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contracts: add gaspad and gasused precompiles
- Loading branch information
Showing
3 changed files
with
96 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
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,27 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import {Sapphire} from "../Sapphire.sol"; | ||
|
||
contract GasTests { | ||
bytes32 tmp; | ||
|
||
function testConstantTime(uint useGas, uint128 padGasAmount) | ||
external | ||
{ | ||
if( useGas == 1 ) | ||
{ | ||
bytes32 x; | ||
|
||
for( uint i = 0; i < 100; i++ ) | ||
{ | ||
x = keccak256(abi.encodePacked(x, tmp)); | ||
} | ||
|
||
tmp = x; | ||
} | ||
|
||
Sapphire.gaspad(padGasAmount); | ||
} | ||
} |
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,39 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { expect } from 'chai'; | ||
import { ethers } from 'hardhat'; | ||
import { GasTests__factory } from '../typechain-types/factories/contracts/tests/Gas.sol'; | ||
import { GasTests } from '../typechain-types/contracts/tests/Gas.sol/GasTests'; | ||
|
||
describe('Gas Padding', function () { | ||
let contract: GasTests; | ||
|
||
before(async () => { | ||
const factory = (await ethers.getContractFactory( | ||
'GasTests', | ||
)) as GasTests__factory; | ||
contract = await factory.deploy(); | ||
}); | ||
|
||
it('Gas Padding works as Expected', async () => { | ||
const expectedGas = 122735; | ||
|
||
let tx = await contract.testConstantTime(1, 100000); | ||
let receipt = await tx.wait(); | ||
expect(receipt.cumulativeGasUsed).eq(expectedGas); | ||
|
||
tx = await contract.testConstantTime(2, 100000); | ||
receipt = await tx.wait(); | ||
expect(receipt.cumulativeGasUsed).eq(expectedGas); | ||
|
||
tx = await contract.testConstantTime(1, 100000); | ||
receipt = await tx.wait(); | ||
expect(receipt.cumulativeGasUsed).eq(expectedGas); | ||
|
||
// Note: calldata isn't included in gas padding | ||
// Thus when the value is 0 it will use 4 gas instead of 16 gas | ||
tx = await contract.testConstantTime(0, 100000); | ||
receipt = await tx.wait(); | ||
expect(receipt.cumulativeGasUsed).eq(expectedGas - 12); | ||
}); | ||
}); |