logoAcademy

Implementing getCounter & increment

Learn how to implement getCounter and increment.

Having seen how to retrieve strings from the EVM state with the StoreString precompile, we are now ready to implement getCounter. Also, now that we are familiar with reading and writing to the EVM state, we can implement increment, which requires both read and write operations.

Implementing getCounter

For getCounter, the following thought process is helpful:

  • Create a helper function GetCounterValue, which takes in the current StateDB and returns the integer stored at the storageKeyHash
  • In getCounter, get the current StateDB and pass it to GetCounterValue

Implementing increment

For increment, the following thought process is helpful:

  • Get the current StateDB and pass it to GetCounterValue
  • Once you have the current counter, increment it by one
  • Store the new counter with StoreCounterValue

On this page