智能合约之间的互动无效

时间:2018-04-09 04:33:36

标签: ethereum smartcontracts

我部署了两份合同,一份是Callee,另一份是Caller。调用者使用Callee提供的功能。函数调用直接调用Callee是成功的,但是,调用者不起作用。实际上,我从互联网上尝试了不同的案例,但都没有。我是否会错过一些棘手的事情?以下是源代码:

  • Callee.sol

    pragma solidity ^0.4.6;
    
    contract Callee {
        uint[] public values;
    
        function getValue(uint initial) public pure returns(uint) {
            return initial + 150;
        }
        function storeValue(uint value) public {
            values.push(value);
        }
        function getValues() public view returns(uint) {
            return values.length;
        }
    }
    
  • Caller.sol

    pragma solidity ^0.4.6;
    
    contract Caller {
        function someAction(address addr) public returns(uint) {
            Callee c = Callee(addr);
            return c.getValue(100);
        }
    
        function storeAction(address addr) public returns(uint) {
            Callee c = Callee(addr);
            c.storeValue(100);
            return c.getValues();
        }
    
        function someUnsafeAction(address addr) public returns(bool){
            return addr.call(bytes4(keccak256("storeValue(uint256)")), 100);
        }
    }
    
    contract Callee {
        function getValue(uint initialValue) public returns(uint);
        function storeValue(uint value) public;
        function getValues() public returns(uint);
    }
    

1 个答案:

答案 0 :(得分:0)

Caller在技术上有效,但没有做到你期待的事情。

问题是Caller.someAction未标有pureview修饰符,因此您的函数调用会触发新事务。事务调用不能返回值(它编译并运行,但不返回任何内容)。

如果您将Caller.someAction更改为pure功能(并且还要将界面中的Callee.getValue更改为pure以匹配合同),您将获得预期的250返回。

pragma solidity ^0.4.6;

contract Caller {
  function someAction(address addr) public pure returns(uint) {
    Callee  c = Callee(addr);
    return c.getValue(100);
  }

  function storeAction(address addr) public returns(uint) {
    Callee c = Callee(addr);
    c.storeValue(100);
    return c.getValues();
  }

  function someUnsafeAction(address addr) public returns(bool){
    return addr.call(bytes4(keccak256("storeValue(uint256)")), 100);
  }
}

contract Callee {
  function getValue(uint initialValue) public pure returns(uint);
  function storeValue(uint value) public;
  function getValues() public returns(uint);
}

在终端:

$ truffle migrate --reset
Compiling .\contracts\Callee.sol...
Compiling .\contracts\SimpleContract.sol...
Writing artifacts to .\build\contracts

Using network 'development'.

Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0x58a14c93acc733bb08e4bb56978d0bb466f8aca7659673426d989ee7e0e626f3
  Migrations: 0x69ed5e4d6172639ed7d3c456ea8b2f2562c7dbcd
Saving successful migration to network...
  ... 0x9a3831076748cc6179ef3e4b3e466c3a4a871e196376bc22c984e91e95fdc567
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Replacing Caller...
  ... 0x7be6840c86a356decacc13967d50ef4fea30e86d30d69b19fb9998ce500c95e8
  Caller: 0xb8c5e079af71813acac73bff9ca8e9e068660e86
  Replacing Callee...
  ... 0xba7f588f9119e67968a7d6ab0110d79e1ecd3003e10cce3134018d42e966c8be
  Callee: 0xd5c110c2f6566fd56749ec1a11328405f9935385
Saving successful migration to network...
  ... 0xc1526876e365189be1529a0943042e3a25e395a5cc19930ad907a694165104f1
Saving artifacts...

$ truffle console
truffle(development)> var caller = Caller.at('0xb8c5e079af71813acac73bff9ca8e9e068660e86');
undefined
truffle(development)> caller.someAction('0xd5c110c2f6566fd56749ec1a11328405f9935385');
{ [String: '250'] s: 1, e: 2, c: [ 250 ] }