Skip to content

Commit

Permalink
Add tests for the evm simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
IAvecilla committed Sep 19, 2024
1 parent c14fa5a commit d75ed11
Show file tree
Hide file tree
Showing 13 changed files with 1,450 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/tests/ts-integration/evm-contracts/ConstructorRevert.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.7.0;

contract ConstructorRevert {
uint256 value;

constructor() {
revert("Failure string");
}
}
14 changes: 14 additions & 0 deletions core/tests/ts-integration/evm-contracts/CounterFallback.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.0;

contract CounterFallback {
function performCall() external {
uint256 value = 0;
value += 1;
}

fallback() external {
this.performCall();
}
}
39 changes: 39 additions & 0 deletions core/tests/ts-integration/evm-contracts/CounterWithParam.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.7.0;

contract CounterWithParam {
uint256 value;

constructor(uint256 _startingValue) {
value = _startingValue;
}

function increment(uint256 x) public {
value += x;
}

function incrementWithRevertPayable(uint256 x, bool shouldRevert) public payable returns (uint256) {
return incrementWithRevert(x, shouldRevert);
}

function incrementWithRevert(uint256 x, bool shouldRevert) public returns (uint256) {
value += x;
if (shouldRevert) {
revert("This method always reverts");
}
return value;
}

function set(uint256 x) public {
value = x;
}

function get() public view returns (uint256) {
return value;
}

function getBytes() public returns (bytes memory) {
return "Testing";
}
}
19 changes: 19 additions & 0 deletions core/tests/ts-integration/evm-contracts/Creator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.7.0;

contract Creation {
function blockNumber() external view returns (uint256) {
return block.number;
}
}

contract Creator {
function create() external {
new Creation();
}

function getCreationRuntimeCode() external pure returns (bytes memory) {
return type(Creation).runtimeCode;
}
}
20 changes: 20 additions & 0 deletions core/tests/ts-integration/evm-contracts/CreatorFallback.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.7.0;

contract Creation {
function blockNumber() external view returns (uint256) {
return block.number;
}
}

contract CreatorFallback {
function performCall() external {
new Creation();
type(Creation).runtimeCode;
}

fallback() external {
this.performCall();
}
}
78 changes: 78 additions & 0 deletions core/tests/ts-integration/evm-contracts/ERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.7.0;

contract ERC20 {
string public symbol;
string public name;
uint8 public decimals;
uint256 public totalSupply;

mapping(address => uint256) balances;
mapping(address => mapping(address => uint256)) allowed;

event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);

constructor() {
symbol = "TEST";
name = "Test Coin";
decimals = 18;
totalSupply = 1000000;
balances[msg.sender] = totalSupply;
emit Transfer(address(0), msg.sender, totalSupply);
}

function balanceOf(address tokenOwner) public view returns (uint256 balance) {
return balances[tokenOwner];
}

function transfer(address to, uint256 tokens) public returns (bool success) {
balances[msg.sender] = safeSub(balances[msg.sender], tokens);
balances[to] = safeAdd(balances[to], tokens);
emit Transfer(msg.sender, to, tokens);
return true;
}

function approve(address spender, uint256 tokens) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}

function transferFrom(
address from,
address to,
uint256 tokens
) public returns (bool success) {
balances[from] = safeSub(balances[from], tokens);
allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
balances[to] = safeAdd(balances[to], tokens);
emit Transfer(from, to, tokens);
return true;
}

function allowance(address tokenOwner, address spender) public view returns (uint256 remaining) {
return allowed[tokenOwner][spender];
}

function safeAdd(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
require(c >= a);
}

function safeSub(uint256 a, uint256 b) internal pure returns (uint256 c) {
require(b <= a);
c = a - b;
}

function safeMul(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a * b;
require(a == 0 || c / a == b);
}

function safeDiv(uint256 a, uint256 b) internal pure returns (uint256 c) {
require(b > 0);
c = a / b;
}
}
16 changes: 16 additions & 0 deletions core/tests/ts-integration/evm-contracts/GasCaller.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.0;

contract GasCaller {
uint256 _resultGas;

function callAndGetGas(address _to) external returns (uint256) {
uint256 startGas = gasleft();
// Just doing a call to an address
(bool success, ) = _to.call("");
require(success);
_resultGas = startGas - gasleft();
return _resultGas;
}
}
123 changes: 123 additions & 0 deletions core/tests/ts-integration/evm-contracts/OpcodeTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.0;

contract OpcodeTest {
function execute() external {
uint256 loaded = 1;
uint256 tmp;
uint256 prevBlock = block.number - 1;
assembly {
loaded := add(loaded, 1)
loaded := mul(loaded, 2)
loaded := sub(loaded, 1)
loaded := div(loaded, 2)
loaded := sdiv(loaded, 2)
loaded := mod(loaded, 2)
// ADDMOD
// MULMOD
loaded := exp(loaded, 2)
loaded := signextend(loaded, 2)
tmp := lt(loaded, 2)
tmp := gt(loaded, 2)
tmp := slt(loaded, 2)
tmp := sgt(loaded, 2)
tmp := eq(loaded, 2)
tmp := iszero(tmp)
tmp := and(1, 1)
tmp := or(1, 1)
tmp := xor(1, 1)
tmp := not(tmp)
tmp := byte(tmp, 1)
tmp := shl(tmp, 1)
tmp := shr(tmp, 1)
tmp := sar(tmp, 1)
tmp := keccak256(0, 0x40)
tmp := address()
tmp := balance(0x00)
tmp := origin()
tmp := caller()
tmp := callvalue()
// CALLDATALOAD
tmp := calldatasize()
// CALLDATACOPY
tmp := codesize()
// CODECOPY
tmp := gasprice()
// EXTCODESIZE
// EXTCODECOPY
tmp := returndatasize()
// RETURNDATACOPY
// EXTCODEHASH
tmp := blockhash(prevBlock)
tmp := coinbase()
tmp := timestamp()
tmp := number()
tmp := prevrandao()
tmp := gaslimit()
tmp := chainid()
tmp := selfbalance()
tmp := basefee()
// POP
tmp := mload(1)
mstore(1024, 1)
mstore8(10242, 1)
tmp := sload(0)
sstore(0, 1)
// JUMP
// JUMPI
// PC
tmp := msize()
tmp := gas()
// JUMPDEST
// PUSH0...PUSH32
// DUP1...DUP16
// SWAP1...SWAP16
// LOG0...LOG4
// CREATE
// CALL
// CALLCODE
// RETURN
// DELEGATECALL
// CREATE2
// STATICCALL
// REVERT
// INVALID
// selfdestruct(sender)
}

// tmp = 0;
// tmp = 0x11;
// tmp = 0x2211;
// tmp = 0x332211;
// tmp = 0x44332211;
// tmp = 0x5544332211;
// tmp = 0x665544332211;
// tmp = 0x77665544332211;
// tmp = 0x8877665544332211;
// tmp = 0x998877665544332211;
// tmp = 0xaa998877665544332211;
// tmp = 0xbbaa998877665544332211;
// tmp = 0xccbbaa998877665544332211;
// tmp = 0xddccbbaa998877665544332211;
// tmp = 0xeeddccbbaa998877665544332211;
// tmp = 0xffeeddccbbaa998877665544332211;
// tmp = 0x11ffeeddccbbaa998877665544332211;
// tmp = 0x2211ffeeddccbbaa998877665544332211;
// tmp = 0x332211ffeeddccbbaa998877665544332211;
// tmp = 0x44332211ffeeddccbbaa998877665544332211;
// tmp = uint256(uint160(0x5544332211FFeeDDCcbbAa998877665544332211));
// tmp = 0x665544332211ffeeddccbbaa998877665544332211;
// tmp = 0x77665544332211ffeeddccbbaa998877665544332211;
// tmp = 0x8877665544332211ffeeddccbbaa998877665544332211;
// tmp = 0x998877665544332211ffeeddccbbaa998877665544332211;
// tmp = 0xff998877665544332211ffeeddccbbaa998877665544332211;
// tmp = 0x11ff998877665544332211ffeeddccbbaa998877665544332211;
// tmp = 0x2211ff998877665544332211ffeeddccbbaa998877665544332211;
// tmp = 0x332211ff998877665544332211ffeeddccbbaa998877665544332211;
// tmp = 0x44332211ff998877665544332211ffeeddccbbaa998877665544332211;
// tmp = 0x5544332211ff998877665544332211ffeeddccbbaa998877665544332211;
// tmp = 0x665544332211ff998877665544332211ffeeddccbbaa998877665544332211;
// tmp = 0x77665544332211ff998877665544332211ffeeddccbbaa998877665544332211;
}
}
Loading

0 comments on commit d75ed11

Please sign in to comment.