Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 8111f4e

Browse files
Rob Mulholandelizabethengelman
authored andcommitted
Add function for adding hashed keys to mapping
- Tool to facilitate parsing diffs from Geth patch that emits hashed versions of storage keys
1 parent 3fb8e13 commit 8111f4e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

libraries/shared/storage/mappings.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ const (
4646
IndexEleven = "000000000000000000000000000000000000000000000000000000000000000b"
4747
)
4848

49+
func AddHashedKeys(currentMappings map[common.Hash]utils.StorageValueMetadata) map[common.Hash]utils.StorageValueMetadata {
50+
copyOfCurrentMappings := make(map[common.Hash]utils.StorageValueMetadata)
51+
for k, v := range currentMappings {
52+
copyOfCurrentMappings[k] = v
53+
}
54+
for k, v := range copyOfCurrentMappings {
55+
currentMappings[hashKey(k)] = v
56+
}
57+
return currentMappings
58+
}
59+
60+
func hashKey(key common.Hash) common.Hash {
61+
return common.BytesToHash(crypto.Keccak256(key.Bytes()))
62+
}
63+
4964
func GetMapping(indexOnContract, key string) common.Hash {
5065
keyBytes := common.FromHex(key + indexOnContract)
5166
encoded := crypto.Keccak256(keyBytes)

libraries/shared/storage/mappings_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,31 @@ import (
55
. "github.com/onsi/ginkgo"
66
. "github.com/onsi/gomega"
77
"github.com/vulcanize/vulcanizedb/libraries/shared/storage"
8+
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
89
)
910

1011
var _ = Describe("Mappings", func() {
12+
Describe("AddHashedKeys", func() {
13+
It("returns a copy of the map with an additional slot for the hashed version of every key", func() {
14+
fakeMap := map[common.Hash]utils.StorageValueMetadata{}
15+
fakeStorageKey := common.HexToHash("72c72de6b203d67cb6cd54fc93300109fcc6fd6eac88e390271a3d548794d800")
16+
var fakeMappingKey utils.Key = "fakeKey"
17+
fakeMetadata := utils.StorageValueMetadata{
18+
Name: "fakeName",
19+
Keys: map[utils.Key]string{fakeMappingKey: "fakeValue"},
20+
Type: utils.Uint48,
21+
}
22+
fakeMap[fakeStorageKey] = fakeMetadata
23+
24+
result := storage.AddHashedKeys(fakeMap)
25+
26+
Expect(len(result)).To(Equal(2))
27+
expectedHashedStorageKey := common.HexToHash("2165edb4e1c37b99b60fa510d84f939dd35d5cd1d1c8f299d6456ea09df65a76")
28+
Expect(fakeMap[fakeStorageKey]).To(Equal(fakeMetadata))
29+
Expect(fakeMap[expectedHashedStorageKey]).To(Equal(fakeMetadata))
30+
})
31+
})
32+
1133
Describe("GetMapping", func() {
1234
It("returns the storage key for a mapping when passed the mapping's index on the contract and the desired value's key", func() {
1335
// ex. solidity:

0 commit comments

Comments
 (0)