1

I know that when smart contracts run in Ethereum, there are parts that create and runtime, and use function selector to find and execute body of function. I have built geth on my computer from a source code, and then modified to print the running opcode.

After that, I compiled the following smart country contents and made a js file.

  contract A {
      uint num;
      constructor (uint n) public {
          num = n;
      }

      function add(uint n) public {
          num += n;
      }

      function nums() public view returns (uint) {
          return num;
      }
 }

js file is like,

var aBin="608060405234801561001057600080fd5b506040516101293803806101298339818101604052602081101561003357600080fd5b8101908080519060200190929190505050806000819055505060cf8061005a6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c80631003e2d2146037578063d5b13259146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686090565b6040518082815260200191505060405180910390f35b80600080828254019250508190555050565b6000805490509056fea264697066735822122021d02222a0c22422593d1d3ea567fddf11eff7a3a15eeb52997e7e8fc164ea6464736f6c63430006010033"
  var aAbi =[{"inputs": [{"internalType": "uint256","name": "n","type": "uint256" }], "stateMutability": "no    npayable","type": "constructor"}, {"inputs": [{"internalType": "uint256", "name": "n","type": "uint256"}],    "name": "add","outputs": [],"stateMutability": "nonpayable","type": "function"},{"inputs": [],"name": "num    s","outputs": [{"internalType": "uint256","name": "", "type": "uint256"}],"stateMutability": "view","type"    : "function"}]

And,

> loadScript("test1/A.js")
true
> var aAbi = aAbi
undefined
> var aBin = '0x'+aBin
undefined
> var conA = eth.contract(aAbi)
undefined
> personal.unlockAccount(eth.accounts[0], "*mypassword*")
true
> var contractA = conA.new(2,{from:eth.accounts[0], data:aBin, gas:'7000000'})

when I try to execute add function,

> contractA.add.call(1)

Opcodes looks like this

[op: PUSH1, operation: 943840]
[op: PUSH1, operation: 943840]
[op: MSTORE, operation: 93ee00]
[op: CALLVALUE, operation: 93c020]
[op: DUP1, operation: 94c830]
[op: ISZERO, operation: 9398f0]
[op: PUSH1, operation: 943840]
[op: JUMPI, operation: 93f570]
[op: JUMPDEST, operation: 93f720]
[op: POP, operation: 93ebe0]
[op: PUSH1, operation: 943840]
[op: CALLDATASIZE, operation: 93c500]
[op: LT, operation: 938d10]
[op: PUSH1, operation: 943840]
[op: JUMPI, operation: 93f570]
[op: PUSH1, operation: 943840]
[op: CALLDATALOAD, operation: 93c2d0]
[op: PUSH1, operation: 943840]
[]

If you look at the output opcode, it was finished before reach to function selector (1003e2d2).

If performed correctly, ADD opcode should exist but is not visible in the output results.

What do I have to do to execute function?

  • In web3.js, contractA.add.call(1) would return a Promise object which you would then need to resolve in order for the RPC to the node to complete. Perhaps that is why. – goodvibration Mar 19 at 8:26
  • In addition to that, you have created an eth.contract object named contractA but you haven't used it in order to actually deploy the contract that this object represents, so I don't see how it can work regardless of the issue that you've mentioned. – goodvibration Mar 19 at 8:27
  • So that is mean, there's no way to call a function from within the console and execute it? – atti Mar 19 at 8:45
  • I believe so, but it still doesn't explain why you're not seeing the function-selector in the bytecode of the function-call. – goodvibration Mar 19 at 8:47
  • Oh wait - that [] at the end of your "Opcodes" output - are you sure it's not just a truncation of the printout by geth console (so the function-selector is further down there, but just wasn't printed)? – goodvibration Mar 19 at 8:48
1

It is because geth setting was wrong. I need to set genesis block to other instead of Homestead.

| improve this answer | |

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.