From 8111f4ec5ee52d5c738a8ca96aa74a4f546321bf Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Tue, 13 Aug 2019 18:09:40 -0500 Subject: [PATCH] Add function for adding hashed keys to mapping - Tool to facilitate parsing diffs from Geth patch that emits hashed versions of storage keys --- libraries/shared/storage/mappings.go | 15 +++++++++++++++ libraries/shared/storage/mappings_test.go | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/libraries/shared/storage/mappings.go b/libraries/shared/storage/mappings.go index 0c8abc37..24740c59 100644 --- a/libraries/shared/storage/mappings.go +++ b/libraries/shared/storage/mappings.go @@ -46,6 +46,21 @@ const ( IndexEleven = "000000000000000000000000000000000000000000000000000000000000000b" ) +func AddHashedKeys(currentMappings map[common.Hash]utils.StorageValueMetadata) map[common.Hash]utils.StorageValueMetadata { + copyOfCurrentMappings := make(map[common.Hash]utils.StorageValueMetadata) + for k, v := range currentMappings { + copyOfCurrentMappings[k] = v + } + for k, v := range copyOfCurrentMappings { + currentMappings[hashKey(k)] = v + } + return currentMappings +} + +func hashKey(key common.Hash) common.Hash { + return common.BytesToHash(crypto.Keccak256(key.Bytes())) +} + func GetMapping(indexOnContract, key string) common.Hash { keyBytes := common.FromHex(key + indexOnContract) encoded := crypto.Keccak256(keyBytes) diff --git a/libraries/shared/storage/mappings_test.go b/libraries/shared/storage/mappings_test.go index 7114a39b..bc077f1f 100644 --- a/libraries/shared/storage/mappings_test.go +++ b/libraries/shared/storage/mappings_test.go @@ -5,9 +5,31 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/vulcanize/vulcanizedb/libraries/shared/storage" + "github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils" ) var _ = Describe("Mappings", func() { + Describe("AddHashedKeys", func() { + It("returns a copy of the map with an additional slot for the hashed version of every key", func() { + fakeMap := map[common.Hash]utils.StorageValueMetadata{} + fakeStorageKey := common.HexToHash("72c72de6b203d67cb6cd54fc93300109fcc6fd6eac88e390271a3d548794d800") + var fakeMappingKey utils.Key = "fakeKey" + fakeMetadata := utils.StorageValueMetadata{ + Name: "fakeName", + Keys: map[utils.Key]string{fakeMappingKey: "fakeValue"}, + Type: utils.Uint48, + } + fakeMap[fakeStorageKey] = fakeMetadata + + result := storage.AddHashedKeys(fakeMap) + + Expect(len(result)).To(Equal(2)) + expectedHashedStorageKey := common.HexToHash("2165edb4e1c37b99b60fa510d84f939dd35d5cd1d1c8f299d6456ea09df65a76") + Expect(fakeMap[fakeStorageKey]).To(Equal(fakeMetadata)) + Expect(fakeMap[expectedHashedStorageKey]).To(Equal(fakeMetadata)) + }) + }) + Describe("GetMapping", func() { It("returns the storage key for a mapping when passed the mapping's index on the contract and the desired value's key", func() { // ex. solidity: