Solidity

連結合約不在 go-ethereum 上執行

  • October 18, 2019

一個非常簡單的連結合約範例,其中 A 合約有一個變數,B 合約查詢 A 合約的變數在 Ropsten+myetherwallet 和 Remix 沙箱上執行正常,但在本地 geth 節點上執行時失敗。在 go-ethereum 上部署 A 和 B 時,我可能做錯了,但我找不到它是什麼:

這是 A 和 B 合約的 Solidity 程式碼:

// linkedContracts.sol
pragma solidity ^0.4.24;

contract A {
   // **** DATA
   uint256 public variable; // Some data

   // **** METHODS

   // variable setter
   function setVariable(uint256 _variable) public returns (uint256) {
       variable = _variable;
   }

   // variable getter
   function getVariable() public constant returns (uint256) {
       return variable;
   }
}

contract B {
   // **** DATA
   address public AContract; // Linked Contract

   // **** METHODS

   // To set token contract address
   function setAContract(address _AContract) public {
       AContract = _AContract;
   }

   // Query external contract variable
   function queryA() public constant returns (uint256) {
       return A(AContract).getVariable();
   }
}

用 remix 編譯得到了這些 ABI 和字節碼:

部署 A:

[{"constant": true,"inputs": [],"name": "getVariable","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": false,"inputs": [{"name": "_variable","type": "uint256"}],"name": "setVariable","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": true,"inputs": [],"name": "variable","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"}]
{
   "linkReferences": {},
   "object": "608060405234801561001057600080fd5b5061013c806100206000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063506ef65c1461005c57806386be3f8014610087578063b6854a21146100c8575b600080fd5b34801561006857600080fd5b506100716100f3565b6040518082815260200191505060405180910390f35b34801561009357600080fd5b506100b2600480360381019080803590602001909291905050506100fc565b6040518082815260200191505060405180910390f35b3480156100d457600080fd5b506100dd61010a565b6040518082815260200191505060405180910390f35b60008054905090565b600081600081905550919050565b600054815600a165627a7a723058202418d5e586909f787d5e0138133c4b30b5b60e0880db04277cbeaa234af69cfa0029",
   "opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x13C DUP1 PUSH2 0x20 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN STOP PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x4 CALLDATASIZE LT PUSH2 0x57 JUMPI PUSH1 0x0 CALLDATALOAD PUSH29 0x100000000000000000000000000000000000000000000000000000000 SWAP1 DIV PUSH4 0xFFFFFFFF AND DUP1 PUSH4 0x506EF65C EQ PUSH2 0x5C JUMPI DUP1 PUSH4 0x86BE3F80 EQ PUSH2 0x87 JUMPI DUP1 PUSH4 0xB6854A21 EQ PUSH2 0xC8 JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0x68 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x71 PUSH2 0xF3 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0x93 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0xB2 PUSH1 0x4 DUP1 CALLDATASIZE SUB DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0xFC JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0xD4 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0xDD PUSH2 0x10A JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x0 DUP1 SLOAD SWAP1 POP SWAP1 JUMP JUMPDEST PUSH1 0x0 DUP2 PUSH1 0x0 DUP2 SWAP1 SSTORE POP SWAP2 SWAP1 POP JUMP JUMPDEST PUSH1 0x0 SLOAD DUP2 JUMP STOP LOG1 PUSH6 0x627A7A723058 KECCAK256 0x24 XOR 0xd5 0xe5 DUP7 SWAP1 SWAP16 PUSH25 0x7D5E0138133C4B30B5B60E0880DB04277CBEAA234AF69CFA00 0x29 ",
   "sourceMap": "56:319:0:-;;;;8:9:-1;5:2;;;30:1;27;20:12;5:2;56:319:0;;;;;;;"
}

部署 B:

[{"constant": false,"inputs": [{"name": "_AContract","type": "address"}],"name": "setAContract","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": true,"inputs": [],"name": "queryA","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": true,"inputs": [],"name": "AContract","outputs": [{"name": "","type": "address"}],"payable": false,"stateMutability": "view","type": "function"}]
{
   "linkReferences": {},
   "object": "608060405234801561001057600080fd5b5061027c806100206000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806349f3ff7d1461005c5780638cbe89ce1461009f578063b424fb93146100ca575b600080fd5b34801561006857600080fd5b5061009d600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610121565b005b3480156100ab57600080fd5b506100b4610164565b6040518082815260200191505060405180910390f35b3480156100d657600080fd5b506100df61022b565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663506ef65c6040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b1580156101eb57600080fd5b505af11580156101ff573d6000803e3d6000fd5b505050506040513d602081101561021557600080fd5b8101908080519060200190929190505050905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff16815600a165627a7a72305820ff0e67b7b7e2914252b739fc8030ccbaf9f2f668ed7f2cb11603509ace8c3f110029",
   "opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x27C DUP1 PUSH2 0x20 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN STOP PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x4 CALLDATASIZE LT PUSH2 0x57 JUMPI PUSH1 0x0 CALLDATALOAD PUSH29 0x100000000000000000000000000000000000000000000000000000000 SWAP1 DIV PUSH4 0xFFFFFFFF AND DUP1 PUSH4 0x49F3FF7D EQ PUSH2 0x5C JUMPI DUP1 PUSH4 0x8CBE89CE EQ PUSH2 0x9F JUMPI DUP1 PUSH4 0xB424FB93 EQ PUSH2 0xCA JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0x68 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x9D PUSH1 0x4 DUP1 CALLDATASIZE SUB DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP PUSH2 0x121 JUMP JUMPDEST STOP JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0xAB JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0xB4 PUSH2 0x164 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST CALLVALUE DUP1 ISZERO PUSH2 0xD6 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0xDF PUSH2 0x22B JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 DUP3 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND DUP2 MSTORE PUSH1 0x20 ADD SWAP2 POP POP PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST DUP1 PUSH1 0x0 DUP1 PUSH2 0x100 EXP DUP2 SLOAD DUP2 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF MUL NOT AND SWAP1 DUP4 PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND MUL OR SWAP1 SSTORE POP POP JUMP JUMPDEST PUSH1 0x0 DUP1 PUSH1 0x0 SWAP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH4 0x506EF65C PUSH1 0x40 MLOAD DUP2 PUSH4 0xFFFFFFFF AND PUSH29 0x100000000000000000000000000000000000000000000000000000000 MUL DUP2 MSTORE PUSH1 0x4 ADD PUSH1 0x20 PUSH1 0x40 MLOAD DUP1 DUP4 SUB DUP2 PUSH1 0x0 DUP8 DUP1 EXTCODESIZE ISZERO DUP1 ISZERO PUSH2 0x1EB JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP GAS CALL ISZERO DUP1 ISZERO PUSH2 0x1FF JUMPI RETURNDATASIZE PUSH1 0x0 DUP1 RETURNDATACOPY RETURNDATASIZE PUSH1 0x0 REVERT JUMPDEST POP POP POP POP PUSH1 0x40 MLOAD RETURNDATASIZE PUSH1 0x20 DUP2 LT ISZERO PUSH2 0x215 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP SWAP1 POP SWAP1 JUMP JUMPDEST PUSH1 0x0 DUP1 SWAP1 SLOAD SWAP1 PUSH2 0x100 EXP SWAP1 DIV PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND DUP2 JUMP STOP LOG1 PUSH6 0x627A7A723058 KECCAK256 SELFDESTRUCT 0xe PUSH8 0xB7B7E2914252B739 0xfc DUP1 ADDRESS 0xcc 0xba 0xf9 CALLCODE 0xf6 PUSH9 0xED7F2CB11603509ACE DUP13 0x3f GT STOP 0x29 ",
   "sourceMap": "258:356:0:-;;;;8:9:-1;5:2;;;30:1;27;20:12;5:2;258:356:0;;;;;;;"
}

這是geth終端上的部署過程:

var aContract = web3.eth.contract([{"constant": true,"inputs": [],"name": "getVariable","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": false,"inputs": [{"name": "_variable","type": "uint256"}],"name": "setVariable","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": true,"inputs": [],"name": "variable","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"}]);
var a = aContract.new(
  {
    from: '0x8703f7b22fc5613497aee971e80480a46b226d3c', 
    data: '0x608060405234801561001057600080fd5b5061013c806100206000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063506ef65c1461005c57806386be3f8014610087578063b6854a21146100c8575b600080fd5b34801561006857600080fd5b506100716100f3565b6040518082815260200191505060405180910390f35b34801561009357600080fd5b506100b2600480360381019080803590602001909291905050506100fc565b6040518082815260200191505060405180910390f35b3480156100d457600080fd5b506100dd61010a565b6040518082815260200191505060405180910390f35b60008054905090565b600081600081905550919050565b600054815600a165627a7a723058202418d5e586909f787d5e0138133c4b30b5b60e0880db04277cbeaa234af69cfa0029', 
    gas: 4700000
  }, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
    console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
})

miner.start(1);
...

Contract mined! address: 0x88e726de6cbadc47159c6ccd4f7868ae7a037730 transactionHash: 0x42cfb268f70ebfb35030fbddd3f7131020f630150bc2a6227b67cfae24d96f29


var bContract = web3.eth.contract([{"constant": false,"inputs": [{"name": "_AContract","type": "address"}],"name": "setAContract","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": true,"inputs": [],"name": "queryA","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"},{"constant": true,"inputs": [],"name": "AContract","outputs": [{"name": "","type": "address"}],"payable": false,"stateMutability": "view","type": "function"}]);
var b = bContract.new(
  {
    from: '0x8703f7b22fc5613497aee971e80480a46b226d3c', 
    data: '0x608060405234801561001057600080fd5b5061027c806100206000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806349f3ff7d1461005c5780638cbe89ce1461009f578063b424fb93146100ca575b600080fd5b34801561006857600080fd5b5061009d600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610121565b005b3480156100ab57600080fd5b506100b4610164565b6040518082815260200191505060405180910390f35b3480156100d657600080fd5b506100df61022b565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663506ef65c6040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b1580156101eb57600080fd5b505af11580156101ff573d6000803e3d6000fd5b505050506040513d602081101561021557600080fd5b8101908080519060200190929190505050905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff16815600a165627a7a72305820ff0e67b7b7e2914252b739fc8030ccbaf9f2f668ed7f2cb11603509ace8c3f110029', 
    gas: 4700000
  }, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
    console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
})

miner.start(1);
...

Contract mined! address: 0x2312e2b60bb069599cf52cc568c990ce61acb2c0 transactionHash: 0x7384c099ffd18dbbef131a556cde5a186e5f013c39bd167876c9ae61707947d7

在測試連結合約功能時,它無法從 B 合約呼叫中獲取目前的 A 合約變數:

a.getVariable();
   0

web3.eth.defaultAccount = "0x8703f7b22fc5613497aee971e80480a46b226d3c"; // My own wallet to send txs...

a.setVariable(11);
   0x97d6a60fb282d93c3f38f8af033b3303b7d50c058083bc9013a51839772731b4

   miner.start(1);
   ...

a.getVariable();
   11


b.setAContract("0x88e726de6cbadc47159c6ccd4f7868ae7a037730")
   0x4a9b0f2c538a315f5021fb7fab4321bfbe7929f6dd8b528b4815a2e78528317f

miner.start(1);
...

b.AContract()
   "0x88e726de6cbadc47159c6ccd4f7868ae7a037730"

b.queryA()
   0

輸出為 0 但應該為 11。在 Remix 和 Ropsten 上它工作正常。在geth它沒有。有什麼提示嗎?謝謝!

溶膠 0.4.24

geth 1.9.0-不穩定

創世紀.json:

{
"alloc": {
   "0x8703f7b22fc5613497aee971e80480a46b226d3c": {
     "balance": "1000000000000000000000000000"
   },
   "0x5FBF29a8Ad77EA087275858d874AcD55526cFbDF": {
     "balance": "1000000000000000000000000000"
   }
 },
"config": {
  "chainID": 99,
  "homesteadBlock": 0,
  "eip155Block": 0,
  "eip158Block": 0
},
"nonce": "0x0000000000000000",
"difficulty": "0x0400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"criptoEuro": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0xffffffff"
}

添加時解決的問題

"ByzantiumBlock": 0,

到 genesis.json。否則 geth 節點無法從另一個合約呼叫合約。有關https://github.com/alastria/alastria-node/issues/278的更多詳細資訊

引用自:https://ethereum.stackexchange.com/questions/76478