question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement `State` object

See original GitHub issue

What is wrong?

Currently, the VM class is part of the constructor for the Computation object. Here are the places where it is currently used.

evm/logic/block.py:7:    block_hash = computation.vm.get_ancestor_hash(block_number)
evm/logic/block.py:13:    computation.stack.push(computation.vm.block.header.coinbase)
evm/logic/block.py:17:    computation.stack.push(computation.vm.block.header.timestamp)
evm/logic/block.py:21:    computation.stack.push(computation.vm.block.header.block_number)
evm/logic/block.py:25:    computation.stack.push(computation.vm.block.header.difficulty)
evm/logic/block.py:29:    computation.stack.push(computation.vm.block.header.gas_limit)
evm/logic/system.py:87:    computation.vm.logger.debug(
evm/logic/system.py:141:            computation.vm.logger.debug(
evm/vm/forks/spurious_dragon/utils.py:22:        yield computation.vm.block.header.coinbase
evm/vm/computation.py:76:        self.vm = vm
evm/vm/computation.py:197:            child_computation = self.vm.apply_create_message(child_msg)
evm/vm/computation.py:199:            child_computation = self.vm.apply_message(child_msg)
evm/vm/computation.py:280:        with self.vm.state_db(read_only, self.msg.access_list) as state_db:

Giving computation direct access to the VM is problematic for a number of reasons.

  • poor isolation for what data and APIs are available during EVM execution.
  • makes implementation of stateless clients difficult.
  • makes VM execution inherently non-pure.

How can it be fixed

To fix this, lets create a new State object. Similar to how the Transaction class is specified for each VM, the State object should be similarly overrideable.

  • VM.get_state_class()
  • VM.get_state()
  • VM.state (this could be a convenience property that just delegates to VM.get_state().

The State object will need the following things.

  • Access to the current block header information (coinbase/timestamp/block_number/difficulty/gas_limit.
  • The ancestry hashes for the current block number.
  • Access to the StateDB

The State object will also need the following APIs available to it.

  • VM.apply_message
  • VM.apply_create_message

I think the best way to accomplish this is to relocate the following APIs to live directly on the State object itself, removing them from the VM object.

  • VM.apply_message
  • VM.apply_create_message
  • VM.apply_computation

Note that VM.apply_computation needs access to VM.get_opcode_fn() and VM.precompiles. This indicates we’ll need to supply the State object with a way to get at this data. The simplest solution that comes to mind is to add them as extra arguments:

class State:
    def apply_computation(self, message, opcodes, precompiles):
        ...

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
hwwhwwcommented, Dec 22, 2017

@pipermerriam Your solution works well and clean! I apply it by replacing _state_class:

HomesteadStateForTesting = HomesteadState.configure(
    name='HomesteadStateForTesting',
    apply_message=apply_message_for_testing,
    apply_create_message=apply_create_message_for_testing,
    get_ancestor_hash=get_block_hash_for_testing,
)
HomesteadVMForTesting = HomesteadVM.configure(
    name='HomesteadVMForTesting',
    _state_class=HomesteadStateForTesting,
)

Thank you so much.

0reactions
hwwhwwcommented, Jan 13, 2018

close via #247

Read more comments on GitHub >

github_iconTop Results From Across the Web

State
State is a behavioral design pattern that lets an object alter its behavior when its internal state changes. It appears as if the...
Read more >
Design Patterns - State Pattern
In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes. Implementation....
Read more >
State Design Pattern
The two patterns use the same structure to solve different problems: State allows an object's behavior to change along with its state, while...
Read more >
State Design Pattern
State design pattern is used when an Object changes its behavior based ... In below example, we have implemented a mobile state scenario...
Read more >
State pattern
The state pattern is a behavioral software design pattern that allows an object to alter its behavior when its internal state changes. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found