0

A very simple linked contracts example where A contract has a variable and B contract queries the A contract's variable is running ok on Ropsten+myetherwallet and Remix sandbox, but it fails when running on local geth node. Probably I'm doing something wrong when deploying A and B on go-ethereum but I cannot find what it is:

This is the A and B contracts Solidity code:

// 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();
    }
}

Compiled with remix got these ABIs and Bytecodes:

Deployment 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;;;;;;;"
}

Deployment 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;;;;;;;"
}

And this is the deployment process on geth terminal:

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

When testing the linked contracts functionality, it fails to get the current A contract variable from a B contract call:

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

Output is 0 but it should be 11. On Remix and Ropsten it works fine. On geth it doesn't. Any hint? Thx!

solc 0.4.24

geth 1.9.0-unstable

genesis.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"
}
  • 1
    What's geth version? What's solc version? What's the genesis file? It sounds like your blockchain is missing some byzantium opcodes RETURNDATACOPY/RETURNDATASIZE. – Ismael Oct 4 at 12:47
  • solc 0.4.24 geth 1.9.0-unstable and genesis.json published at the end of the question. Thx! – Juan Ignacio Pérez Sacristán Oct 4 at 14:38
  • 1
    It seems to at least from solc v0.4.21 byzantium opcodes are the default github.com/ethereum/solidity/releases/tag/v0.4.21. You can try chaging the solc target to "spuriousDragon" or older. – Ismael Oct 4 at 15:44
  • I've just tried with solc 0.5.12 (last version on remix) and got this error when executing: a.setVariable(11); Error: gas required exceeds allowance (4215972105) or always failing transaction And balance seems enough to pay the transaction web3.fromWei(eth.getBalance("0x8703f7b22fc5613497aee971e80480a46b226d3c").toNumber(), "wei") 1000000000000000000000000000 – Juan Ignacio Pérez Sacristán Oct 4 at 16:14
  • 1
    Newer compiler like solc v0.5.xx also require constantinople/petersburg opcodes, they started to use the new shift operators. If you can't upgrade your blockchain to support new opcodes the command line solc has an option to target older EVM --evm-version XXX, remix also has a dropdown that allow to select an older EVM version. – Ismael Oct 4 at 18:36
0

Solved issue when added

"ByzantiumBlock": 0,

to genesis.json. Otherwise geth node is not capable of calling contracts from another contract. More details on https://github.com/alastria/alastria-node/issues/278

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.