Skip to content

Commit

Permalink
contracts: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarMist committed Oct 31, 2023
1 parent c052df3 commit e781383
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 44 deletions.
10 changes: 3 additions & 7 deletions contracts/contracts/Sapphire.sol
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,16 @@ library Sapphire {
*
* Will cause a reversion if the current usage is more than the amount
*/
function gaspad(uint128 toAmount) internal view
{
(bool success,) = GAS_PAD.staticcall(
abi.encode(toAmount)
);
function gaspad(uint128 toAmount) internal view {
(bool success, ) = GAS_PAD.staticcall(abi.encode(toAmount));
require(success, "verify: failed");
}

/**
* @dev Returns the amount of gas currently used by the transaction
* @custom:see @oasisprotocol/oasis-sdk :: precompile/gas.rs :: call_gas_used
*/
function gasused() internal view returns (uint64)
{
function gasused() internal view returns (uint64) {
(bool success, bytes memory v) = GAS_USED.staticcall("");
require(success, "gasused: failed");
return abi.decode(v, (uint64));
Expand Down
10 changes: 3 additions & 7 deletions contracts/contracts/tests/Gas.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import {Sapphire} from "../Sapphire.sol";
contract GasTests {
bytes32 tmp;

function testConstantTime(uint useGas, uint128 padGasAmount)
external
{
if( useGas == 1 )
{
function testConstantTime(uint256 useGas, uint128 padGasAmount) external {
if (useGas == 1) {
bytes32 x;

for( uint i = 0; i < 100; i++ )
{
for (uint256 i = 0; i < 100; i++) {
x = keccak256(abi.encodePacked(x, tmp));
}

Expand Down
60 changes: 30 additions & 30 deletions contracts/test/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@ import { GasTests__factory } from '../typechain-types/factories/contracts/tests/
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);
});
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);
});
});

0 comments on commit e781383

Please sign in to comment.