Skip to content

Solidity Cortex Tutorial

Truman TIAN edited this page Jun 15, 2018 · 1 revision
cd example
../CortexTheseus/build/bin/evm --code $(../solidity/build/solc/solc infer-mapping-asm.sol --bin-runtime 2>&1 | grep AIContract -n2 | tail -1 | cut -d'-' -f2) --input 14132cc40000000000000000000000000000000000000000000000000000000000002001  --prestate g.json --debug run 2>&1 | less

the above code:

  1. compile solidity code into bytecodes, and extract code of entry contract AIContract
  2. using evm to run bytecodes given a pre-state specific in genesis file g.json, with abi 14132cc4 and input data(stored in address 0000000000000000000000000000000000000000000000000000000000002001)

reference:

infer-mapping-asm.sol

pragma solidity ^0.4.18;
contract Owned {
    function owned() public { owner = msg.sender; }
    address owner;

    modifier onlyOwner {
        require(
                msg.sender == owner,
                "Only owner can call this function."
               );
        _;
    }
}

contract AIContract is Owned {
    mapping (address => uint256) reward;
    uint256 targetLabel_;

    // update target, e.g. label:530 is bird
    function SetTargetLabel(uint256 targetLabel) onlyOwner public {
        targetLabel_ = targetLabel;
    }

    function Call(address inputData) public returns (uint256 ret) {
        uint256 result = makeInfer(inputData);
        address caller =  msg.sender;
        if (result == targetLabel_)
            reward[caller] += 2**16;
        else
            reward[caller] += 2**0;
    }

    // internal function for calling operation infer
        function makeInfer(address input) private constant returns (uint256 ret) {
        // model address
        address model =  0x0000000000000000000000000000000000001001;
        assembly {
            ret := infer(model, input)
        }
        }
}
Clone this wiki locally