Hook with included pre-state

Hi everyone,

Is it possible in some way that in hook add values from pre-state? So, for example, I have a hook where I calculated the sum of ERC20 balances (something like)

hook Sstore _balances[KEY address a] uint256 new_balance
// the old value that balances[a] holds before the store
    (uint256 old_balance) STORAGE {
  sumBalances = sumBalances + new_balance - old_balance;

and that sumBalances is different than the sum of account balances that I have in pre-state.

Thanks in advance for answers.

Hi @Stefan ,
If you mean you would like to require that the sumBalances does change, you can either declare a variable that saves its value before the change and then require it’s different than the new value, or you can require that the new and old balances are different.
Does that answer your question?


Thanks for the real fast answer, but no, I didn’t think about that (maybe and my question was not so good). Here is one image that better describes what I want

So, in this image, _balances[0x2712]: 0x136dcc951d8c0001 (from Storage State) and on the other side, sumUserBaseBalances = 3 (from assume invariant in pre-state) are different. I just wanted that, in this case, sumUserBaseBalances be equal to 0x136dcc951d8c0001 (or if I have few accounts with balances, that sum of all these balances from Storage State to be equal to sumUserBaseBalances in pre-state). I hope that now is better described :slight_smile:

I see now, thanks for the clarification.
One thing you can do (although not sure it will really solve your issue, depending on your rule), is adding to the hook a requirement that the sum is always greater or equal to the value you load.
So, you can add another Sload hook for the balances of that ERC20 and then inside its body just add: require (balances_value <= sumBalance).
This will guarantee that every balance read in storage will be less than the ghost sum. However, it will not guarantee that the actual sum of these values will be bounded by your ghost. You can add a similar requirement in the Sstore hook and see what happens.

Thanks for answer @RoyCERTORA , I’ll try it.