github.com/core-coin/go-core/v2@v2.1.9/tests/ylem/contracts/OpCodes.sol (about)

     1  pragma solidity >=0.4.21 <0.6.0;
     2  
     3  contract Test1 {
     4    function isSameAddress(address a, address b) public returns(bool){  //Simply add the two arguments and return
     5        if (a == b) return true;
     6        return false;
     7    }
     8  }
     9  
    10  contract OpCodes {
    11  
    12      Test1 test1;
    13  
    14      constructor() public {  //Constructor function
    15        test1 = new Test1();  //Create new "Test1" function
    16      }
    17  
    18     modifier onlyOwner(address _owner) {
    19        require(msg.sender == _owner);
    20        _;
    21     }
    22     // Add a todo to the list
    23     function test() public {
    24  
    25       //simple_instructions
    26       /*assembly { pop(sub(dup1, mul(dup1, dup1))) }*/
    27  
    28       //keywords
    29       assembly { pop(address) return(2, byte(2,1)) }
    30  
    31       //label_complex
    32       /*assembly { 7 abc: 8 eq jump(abc) jumpi(eq(7, 8), abc) pop }
    33       assembly { pop(jumpi(eq(7, 8), abc)) jump(abc) }*/
    34  
    35       //functional
    36       /*assembly { let x := 2 add(7, mul(6, x)) mul(7, 8) add =: x }*/
    37  
    38       //for_statement
    39       assembly { for { let i := 1 } lt(i, 5) { i := add(i, 1) } {} }
    40       assembly { for { let i := 6 } gt(i, 5) { i := add(i, 1) } {} }
    41       assembly { for { let i := 1 } slt(i, 5) { i := add(i, 1) } {} }
    42       assembly { for { let i := 6 } sgt(i, 5) { i := add(i, 1) } {} }
    43  
    44       //no_opcodes_in_strict
    45       assembly { pop(callvalue()) }
    46  
    47       //no_dup_swap_in_strict
    48       /*assembly { swap1() }*/
    49  
    50       //print_functional
    51       assembly { let x := mul(sload(0x12), 7) }
    52  
    53       //print_if
    54       assembly { if 2 { pop(mload(0)) }}
    55  
    56       //function_definitions_multiple_args
    57       assembly { function f(a, d){ mstore(a, d) } function g(a, d) -> x, y {}}
    58  
    59       //sstore
    60       assembly { function f(a, d){ sstore(a, d) } function g(a, d) -> x, y {}}
    61  
    62       //mstore8
    63       assembly { function f(a, d){ mstore8(a, d) } function g(a, d) -> x, y {}}
    64  
    65       //calldatacopy
    66       assembly {
    67         let a := mload(0x40)
    68         let b := add(a, 32)
    69         calldatacopy(a, 4, 32)
    70         /*calldatacopy(b, add(4, 32), 32)*/
    71         /*result := add(mload(a), mload(b))*/
    72       }
    73  
    74       //codecopy
    75       assembly {
    76         let a := mload(0x40)
    77         let b := add(a, 32)
    78         codecopy(a, 4, 32)
    79       }
    80  
    81       //codecopy
    82       assembly {
    83         let a := mload(0x40)
    84         let b := add(a, 32)
    85         extcodecopy(0, a, 4, 32)
    86       }
    87  
    88       //for_statement
    89       assembly { let x := calldatasize() for { let i := 0} lt(i, x) { i := add(i, 1) } { mstore(i, 2) } }
    90  
    91       //SHA3
    92       assembly { pop(SHA3(0,0)) }
    93  
    94       //returndatasize
    95       assembly { let r := returndatasize }
    96  
    97       //returndatacopy
    98       assembly { returndatacopy(64, 32, 0) }
    99       //staticcall
   100       assembly { pop(staticcall(10000, 0x123, 64, 0x10, 128, 0x10)) }
   101  
   102       /*//create2
   103       assembly { pop(create2(10, 0x123, 32, 64)) }*/
   104  
   105       //create
   106       assembly { pop(create(10, 0x123, 32)) }
   107  
   108       //shift
   109       /*assembly { pop(shl(10, 32)) }
   110       assembly { pop(shr(10, 32)) }
   111       assembly { pop(sar(10, 32)) }*/
   112  
   113  
   114       //not
   115       assembly { pop( not(0x1f)) }
   116  
   117       //exp
   118       assembly { pop( exp(2, 226)) }
   119  
   120       //mod
   121       assembly { pop( mod(3, 9)) }
   122  
   123       //smod
   124       assembly { pop( smod(3, 9)) }
   125  
   126       //div
   127       assembly { pop( div(4, 2)) }
   128  
   129       //sdiv
   130       assembly { pop( sdiv(4, 2)) }
   131  
   132       //iszero
   133       assembly { pop(iszero(1)) }
   134  
   135       //and
   136       assembly { pop(and(2,3)) }
   137  
   138       //or
   139       assembly { pop(or(3,3)) }
   140  
   141       //xor
   142       assembly { pop(xor(3,3)) }
   143  
   144       //addmod
   145       assembly { pop(addmod(3,3,6)) }
   146  
   147       //mulmod
   148       assembly { pop(mulmod(3,3,3)) }
   149  
   150       //signextend
   151       assembly { pop(signextend(1, 10)) }
   152  
   153       //sha3
   154       assembly { pop(calldataload(0)) }
   155  
   156       //blockhash
   157       assembly { pop(blockhash(sub(number(), 1))) }
   158  
   159       //balance
   160       assembly { pop(balance(0x0)) }
   161  
   162       //caller
   163       assembly { pop(caller()) }
   164  
   165       //codesize
   166       assembly { pop(codesize()) }
   167  
   168       //extcodesize
   169       assembly { pop(extcodesize(0x1)) }
   170  
   171       //origin
   172       assembly { pop(origin()) }
   173  
   174       //energy
   175       assembly {  pop(energy())}
   176  
   177       //msize
   178       assembly {  pop(msize())}
   179  
   180       //pc
   181       assembly {  pop(pc())}
   182  
   183       //energyprice
   184       assembly {  pop(energyprice())}
   185  
   186       //coinbase
   187       assembly {  pop(coinbase())}
   188  
   189       //timestamp
   190       assembly {  pop(timestamp())}
   191  
   192       //number
   193       assembly {  pop(number())}
   194  
   195       //difficulty
   196       assembly {  pop(difficulty())}
   197  
   198       //energylimit
   199       assembly {  pop(energylimit())}
   200  
   201       //call
   202       address contractAddr = address(test1);
   203       bytes4 sig = bytes4(SHA3("isSameAddress(address,address)")); //Function signature
   204       address a = msg.sender;
   205  
   206       assembly {
   207           let x := mload(0x40)   //Find empty storage location using "free memory pointer"
   208           mstore(x,sig) //Place signature at begining of empty storage
   209           mstore(add(x,0x04),a) // first address parameter. just after signature
   210           mstore(add(x,0x24),a) // 2nd address parameter - first padded. add 32 bytes (not 20 bytes)
   211           mstore(0x40,add(x,0x64)) // this is missing in other examples. Set free pointer before function call. so it is used by called function.
   212            // new free pointer position after the output values of the called function.
   213  
   214           let success := call(
   215                           5000, //5k energy
   216                           contractAddr, //To addr
   217                           0,    //No ore passed
   218                           x,    // Inputs are at location x
   219                           0x44, //Inputs size two padded, so 68 bytes
   220                           x,    //Store output over input
   221                           0x20) //Output is 32 bytes long
   222       }
   223  
   224       //callcode
   225       assembly {
   226           let x := mload(0x40)   //Find empty storage location using "free memory pointer"
   227           mstore(x,sig) //Place signature at begining of empty storage
   228           mstore(add(x,0x04),a) // first address parameter. just after signature
   229           mstore(add(x,0x24),a) // 2nd address parameter - first padded. add 32 bytes (not 20 bytes)
   230           mstore(0x40,add(x,0x64)) // this is missing in other examples. Set free pointer before function call. so it is used by called function.
   231            // new free pointer position after the output values of the called function.
   232  
   233           let success := callcode(
   234                           5000, //5k energy
   235                           contractAddr, //To addr
   236                           0,    //No ore passed
   237                           x,    // Inputs are at location x
   238                           0x44, //Inputs size two padded, so 68 bytes
   239                           x,    //Store output over input
   240                           0x20) //Output is 32 bytes long
   241       }
   242  
   243       //delegatecall
   244       assembly {
   245           let x := mload(0x40)   //Find empty storage location using "free memory pointer"
   246           mstore(x,sig) //Place signature at begining of empty storage
   247           mstore(add(x,0x04),a) // first address parameter. just after signature
   248           mstore(add(x,0x24),a) // 2nd address parameter - first padded. add 32 bytes (not 20 bytes)
   249           mstore(0x40,add(x,0x64)) // this is missing in other examples. Set free pointer before function call. so it is used by called function.
   250            // new free pointer position after the output values of the called function.
   251  
   252           let success := delegatecall(
   253                           5000, //5k energy
   254                           contractAddr, //To addr
   255                           x,    // Inputs are at location x
   256                           0x44, //Inputs size two padded, so 68 bytes
   257                           x,    //Store output over input
   258                           0x20) //Output is 32 bytes long
   259       }
   260  
   261       uint256 _id = 0x420042;
   262  
   263       //log0
   264       log0(
   265           bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20)
   266       );
   267  
   268       //log1
   269       log1(
   270           bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
   271           bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20)
   272       );
   273  
   274       //log2
   275       log2(
   276           bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
   277           bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
   278           bytes32(uint256(msg.sender))
   279       );
   280  
   281       //log3
   282       log3(
   283           bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
   284           bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
   285           bytes32(uint256(msg.sender)),
   286           bytes32(_id)
   287       );
   288  
   289       //log4
   290       log4(
   291           bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
   292           bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
   293           bytes32(uint256(msg.sender)),
   294           bytes32(_id),
   295           bytes32(_id)
   296  
   297       );
   298  
   299       //selfdestruct
   300       assembly { selfdestruct(0x02) }
   301     }
   302  
   303    function test_revert() public {
   304  
   305      //revert
   306      assembly{ revert(0, 0) }
   307    }
   308  
   309    function test_invalid() public {
   310  
   311      //revert
   312      assembly{ invalid() }
   313    }
   314  
   315    function test_stop() public {
   316  
   317      //revert
   318      assembly{ stop() }
   319    }
   320  
   321  }