Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

tests: add tests for gas estimation #3570

Draft
wants to merge 12 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

contract ContractFactory5{
constructor() {}
}
contract ContractFactory4{
address[] addresses;
constructor() {
for(uint i=0; i < 2; i++){
addresses.push(address(new ContractFactory5()));
}
}
}
contract ContractFactory2{
address public test2;
constructor() {
test2 = address(new ContractFactory3());
}
}

contract ContractFactory3{
address public test2;
address[] addresses;
constructor() {
test2 = address(new ContractFactory4());

for(uint i=0; i < 2; i++){
addresses.push(address(new ContractFactory4()));
}
}
}

contract ContractFactory{
event UsefulEvent(address factory1, address factory2);
function createInstance() public {
address test2 = address(new ContractFactory2());
address test3 = address(new ContractFactory4());
emit UsefulEvent(test2, test3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

contract CreateTwo {
event RelayAddress(address addr, uint256 salt);
function deploy(bytes memory code, uint256 salt) public {
address addr;
assembly {
addr := create2(0, add(code, 0x20), mload(code), salt)
if iszero(addr) {
revert(0, 0)
}
}
emit RelayAddress(addr, salt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

contract Donation {
address owner;
event fundMoved(address _to, uint _amount);
modifier onlyowner { if (msg.sender == owner) _; }
address[] _giver;
uint[] _values;

constructor() {
owner = msg.sender;
}

function donate() public payable {
addGiver(msg.value);
}

function moveFund(address _to, uint _amount) public onlyowner {
uint _balance = address(this).balance;
address payable inst = payable(new Fib());
(bool _tosendbal, bytes memory _data) = inst.call{value: _amount}("");
if (_amount <= _balance) {
if (_tosendbal) {
emit fundMoved(_to, _amount);
} else {
revert("Funds were not successfully moved.");
}
} else {
revert("Balance is less than transferred amount.");
}
}

function moveFund2(address payable _to, uint _amount) public onlyowner {
uint _balance = address(this).balance;
if (_amount <= _balance) {
require(_to.send(_amount));
emit fundMoved(_to, _amount);
} else {
revert();
}
}

function addGiver(uint _amount) internal {
_giver.push(msg.sender);
_values.push(_amount);
}
}

contract Fib {
constructor() {}
uint public value = 0;

fallback() external payable {
value = calc(5);
}

function calc(uint index) internal returns(uint){
if (index <= 1) {
return 1;
}
return calc(index - 1) + calc(index - 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

// From https://github.com/ethereumjs/testrpc/issues/58
contract EstimateGas {
event Add(bytes32 name, bytes32 description, uint value, address owner);

struct Test {
bytes32 name;
bytes32 description;
uint[] balances;
mapping(address => uint) owners;
}

uint256 public x;
uint256 public y;
function reset() public {
x = 0;
y = 1;
}
function initialSettingOfX() public {
x = 1;
}
function triggerRsclearRefund() public {
x = gasleft();
reset();
}
function triggerRsclearRefundForX() public {
reset();
x = gasleft();
}
function triggerRsclearRefundForY() public {
y = gasleft();
reset();
}
function triggerRselfdestructRefund() public {
selfdestruct(payable(msg.sender));
}
function triggerAllRefunds() public {
triggerRsclearRefund();
triggerRselfdestructRefund();
}

// https://github.com/trufflesuite/ganache-cli/issues/294
mapping (uint => uint) public uints;
// Sets the uints[1] slot to a value;
function store(uint _uint) public { uints[1] = _uint;}
function clear() public { delete uints[1]; }

mapping(bytes32 => uint) index;
Test[] tests;

constructor() {
tests.push();
}

function add(bytes32 _name, bytes32 _description, uint _value) public returns(bool) {
if (index[_name] != 0) {
return false;
}
uint pos = tests.length;
tests.push();
tests[pos].name = _name;
tests[pos].description = _description;
tests[pos].balances.push();
tests[pos].balances.push(_value);
tests[pos].owners[msg.sender] = 1;
index[_name] = pos;
emit Add(_name, _description, _value, msg.sender);
return true;
}

function transfer(address _to, uint _value, bytes32 _name) public returns(bool) {
uint pos = index[_name];
if (pos == 0) {
return false;
}

uint posFrom = tests[pos].owners[msg.sender];
if (posFrom == 0) {
return false;
}

if (tests[pos].balances[posFrom] < _value) {
return false;
}

uint posTo = tests[pos].owners[_to];
if (posTo == 0) {
uint posBal = tests[pos].balances.length;
tests[pos].balances.push();
tests[pos].owners[_to] = posBal;
posTo = posBal;
}

if (tests[pos].balances[posTo] + _value < tests[pos].balances[posTo]) {
return false;
}
tests[pos].balances[posFrom] -= _value;
tests[pos].balances[posTo] += _value;

return true;
}

function currentBlock() public view returns (uint) {
return block.number;
}

uint public counter;
function runsOutOfGas() public {
consumesGas();
}
function consumesGas() public {
for(uint i = 0; i < 100000; i++){
counter = i;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

contract Fib {
constructor() {}
uint public value = 0;

fallback() external payable {
Bif bif = new Bif();
value = bif.calc(5);
}
}

contract Bif {
constructor() {}

function calc(uint index) public returns(uint){
if (index <= 1) {
return 1;
}
return calc(index - 1) + calc(index - 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
contract GasLeft {
uint x = 0;
function checkGas() public {
require(gasleft() > 100000, "Need 100000 gas");
x = 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
contract Target {
fallback () external payable { }
}
contract NonZero {
Target private theInstance;
constructor() public {
theInstance = new Target();
}
function doCall() external payable {
address(theInstance).call{value:msg.value, gas: 123456}("");
}
function doTransfer() external payable {
payable(theInstance).transfer(msg.value);
}
function doSend() external payable {
payable(theInstance).send(msg.value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
// FROM: https://github.com/trufflesuite/ganache-cli-archive/issues/585
pragma solidity ^0.8.11;
contract SendContract {
function Send() public payable{

}

fallback () external payable{
}

function getBalance() public view returns (uint balance){
balance = address(this).balance;
return balance;
}

function transfer(address payable[] memory receiver, uint256 amount) public payable returns(bool){
for(uint i = 0; i < receiver.length; i++){
receiver[i].transfer(amount);
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

contract TestDepth {
uint256 public x = 1;

function depth(uint256 y) public {
// bool result;
if (y > 0) {
(bool result,) = address(this).delegatecall(abi.encodeWithSignature("depth(uint256)", --y));
require(result, "");
}
else {
// Save the remaining gas in storage so that we can access it later
x = gasleft();
}
}
}
Loading