1

Currently, I do some experiments with Geth-EVM and opcode execution. During the experiments, I observed that the DIFFICULTY opcode always pushs 0x0 to the EVM stack regardless of how I set the difficulty value in the genesis config.file. How can I interpret this and how can I define the difficulty value?

I execute the EVM with the following command:

/home/user/ethereum/evms/geth-evm-1.8.0-stable 
--code 5a5b5b325932443a4332364333348942594458453441344130349944585b600960d360513900 
--prestate /home/user/ethereum/geth_genesis.json 
--gas 1048575 --sender 0x054fcf7870c50924ecbddd38c26e159d2cc78232 
--receiver 0xa31741b8972eea1991a9093284ad16bab63b9c18 --nomemory --json run

This is my genesis config file:

{"alloc": {
"0x123456789a123456789a123456789a123456789a": {
  "balance": "0x2bcb2dca0cded",
  "code": "0x1337",
  "nonce": "0x0"
},
"0x123456789a123456789a123456789a123456789c": {
  "balance": "0x9a815cb917edb",
  "code": "0x1337",
  "nonce": "0x0"
},
"0x123456789a123456789a123456789a123456789e": {
  "balance": "0x1c63ab403d7514",
  "code": "0x1337",
  "nonce": "0x0"
},
"0x123456789a321232189a123456789a123456789f": {
  "balance": "0xd3f6cdb0ae123",
  "code": "0x1337",
  "nonce": "0x0"
},
"0x333456789a123456789a123453289a1234512893": {
  "balance": "0x26b012410311d",
  "code": "0x1337",
  "nonce": "0x0"
}
},
    "coinbase": "0x0000000000000000000000000000000000000000",
    "config": {
        "byzantiumBlock": 0,
        "daoForkBlock": 0,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip158Block": 0,
        "homesteadBlock": 0
},
    "difficulty": "0x01",
    "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "gasLimit": "0x3D0900",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "nonce": "0x0000000000000000",
    "number": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp": "0x00"
}
1

You can't use difficulty lower than 2048 because the function that calculates the difficulty divides it by 2048 and if you use 1, you will end up with 0 since it is an integer division ( 1 / 2048 = 0). This parameter is configurable in

config/protocol_params.go

DifficultyBoundDivisor

Try to set it to 1, maybe it will work. But if you don't want to have these problems, just set your difficulty to 2100 or above

  • I understand that, but regardless of the value it always pushes 0x0 to the stack. During the last execution I set the value to "0x9999999" and i got 0x0. – kedenk Jul 18 '18 at 13:51
  • try to "push" GasLimit, if it is not 0, then it is the divisor. – Nulik Jul 18 '18 at 14:31
  • I will try that. However, I tested the same bytecode sequence with with the Parity implementation of EVM. This implementation takes exactly the difficult value from the genesis file. Is there a reason why Geth uses the divisor? – kedenk Jul 18 '18 at 18:20
  • the divisor is part of the difficulty function, everybody has it implemented the same way otherwise you will not reach consensus. To run bytecode you have to have a preinitialized environment , in other words , a types.Header, a state.StateDb and vm.Context objects. In your case Difficulty is 0 , this means types.Header is not initialized, Alter the function vm.NewEVM() and dump all the parameters passed to this function to console, this way you will have a better understanding of what part of the enviroment is not configured properly. – Nulik Jul 18 '18 at 20:47
  • I will try this. But actually I use the standalone EVM executable. So types.Header should be initialized?! – kedenk Jul 19 '18 at 18:06

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.