实体装配。从存储和委托调用中读取结构

时间:2018-08-24 09:56:11

标签: ethereum solidity

我正在使用低级的委托调用将调用转发到库,但是我对内联Assemblyl不够熟悉,无法使其正常工作。

contract BountyProxy {
  using BountyLibrary for BountyLibrary.Bounty;
  BountyLibrary.Bounty bounty;
  address impl;

  constructor(bytes32 _reference, address _issuer, uint _deadline, address _lib) public payable {
        impl = _lib;
        bounty.init(_reference, _issuer, _deadline);
  }

  function forward(bytes32 _sig, bytes _data) external payable {
    bytes memory data = _data;
    bytes4 sig = bytes4(_sig);
    address _impl = impl;
    require(_impl != address(0));
    assembly {

      let _message := mload(0x40) //find empty memory location
      mstore(_message, sig) //place sig at beginning of empty storage
      //add _bounty
      mstore(add(_message, 4), sload(bounty_slot))
      //add _data
      mstore(add(_message, 36), data)
      let size := returndatasize
      let result := delegatecall(gas, _impl, _message, msize, _message, size)
      //return data ??????
      returndatacopy(_message, 0, size)
      switch result
      case 0 {revert(_message, size) }
      default { return(_message, size) }
    }
  }

  function getBounty() external view returns (bytes32, address, uint, uint, uint, BountyLibrary.statusOptions) {
    return bounty.getBounty();
  }
}

完整的代码可以在以下小提琴中找到:https://ethfiddle.com/sd1ATrDtjy 此外,可以在以下位置找到迁移:https://gist.github.com/kyriediculous/d2286a8a580d28a3903395490b15bcaa

我需要(按此顺序):

  • 从内存中将bytes4 sig加载到_message
  • 从存储中向_message加载赏金(STRUCT)
  • 将字节_data加载到_message
  • 使用新消息进行电话呼叫
  • 获取返回值

合同和库的工作由我添加的getBounty()函数证明。货运代理就是我遇到的麻烦。

  • 如何从存储中加载赏金结构?
  • 如何正确地将前4个字节的calldata拆分,将其复制到内存中并将赏金结构放在两者之间?

当我调用getStorageAt时似乎已解压缩该结构,是否需要重新打包?

0x00
0x687474703a2f2f6170692e6269746275636b65742e636f6d2f33303433303430
0xe1bb4acb579f780cbaf21cef9ac3641b8e3fcd69
0x5b7eb10c
0x3334303330340000000000000000000000000000000000000000000000000000
0x012a05f200
0x00
0x00
0x059ce9161d4fb0
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00
0x00

也请随时参考stackexchange:https://ethereum.stackexchange.com/questions/57197/get-return-data-from-delegatecall

非常感谢。

0 个答案:

没有答案