Skip to content

Commit

Permalink
feat: ✨ eth: calculate slot hash (#88)
Browse files Browse the repository at this point in the history
Signed-off-by: thanhpp <thanhphanphu18@gmail.com>
  • Loading branch information
thanhpp authored Oct 16, 2024
1 parent 3bc14f6 commit b4a11e7
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/eth/slothash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eth

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

func OverrideSet(stateDiff map[common.Hash]common.Hash, storageSlot int, key common.Hash) {
// for a set the first slot is used to store the array to support enumerable.
// The slot right after it is used to store the map (need to override this map).
OverrideMap(stateDiff, storageSlot+1, key, common.BigToHash(common.Big1))
}

func OverrideVariable(stateDiff map[common.Hash]common.Hash, storageSlot int, value common.Hash) {
slotHash := common.BigToHash(big.NewInt(int64(storageSlot)))
stateDiff[slotHash] = value
}

func OverrideMap(stateDiff map[common.Hash]common.Hash, storageSlot int, key, value common.Hash) {
overrideSlot32 := common.BigToHash(big.NewInt(int64(storageSlot)))
overrideSlotHash := crypto.Keccak256Hash(key[:], overrideSlot32[:])
stateDiff[overrideSlotHash] = value
}

0 comments on commit b4a11e7

Please sign in to comment.