From b4a11e771aac222ef373b8acec8b74c131dc2ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phan=20Ph=C3=BA=20Th=C3=A0nh?= Date: Wed, 16 Oct 2024 16:18:03 +0700 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20eth:=20calculate=20slot=20h?= =?UTF-8?q?ash=20(#88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: thanhpp --- pkg/eth/slothash.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 pkg/eth/slothash.go diff --git a/pkg/eth/slothash.go b/pkg/eth/slothash.go new file mode 100644 index 0000000..b9e1e76 --- /dev/null +++ b/pkg/eth/slothash.go @@ -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 +}