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

Commit 2ff88de

Browse files
Rob Mulholandelizabethengelman
authored andcommitted
Extract helper for converting hex to keccak256 hash
- Also prefer crypto.Keccak256Hash(x) to common.BytesToHash(crypto.Keccak256(x))
1 parent d06dddb commit 2ff88de

File tree

8 files changed

+20
-25
lines changed

8 files changed

+20
-25
lines changed

libraries/shared/factories/storage/transformer_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package storage_test
1818

1919
import (
2020
"github.com/ethereum/go-ethereum/common"
21-
"github.com/ethereum/go-ethereum/crypto"
2221
. "github.com/onsi/ginkgo"
2322
. "github.com/onsi/gomega"
2423
"github.com/vulcanize/vulcanizedb/libraries/shared/factories/storage"
@@ -45,7 +44,7 @@ var _ = Describe("Storage transformer", func() {
4544
})
4645

4746
It("returns the contract address being watched", func() {
48-
fakeAddress := common.BytesToHash(crypto.Keccak256(common.FromHex("0x12345")))
47+
fakeAddress := utils.HexToKeccak256Hash("0x12345")
4948
t.HashedAddress = fakeAddress
5049

5150
Expect(t.KeccakContractAddress()).To(Equal(fakeAddress))

libraries/shared/storage/mappings.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,17 @@ func AddHashedKeys(currentMappings map[common.Hash]utils.StorageValueMetadata) m
5858
}
5959

6060
func hashKey(key common.Hash) common.Hash {
61-
return common.BytesToHash(crypto.Keccak256(key.Bytes()))
61+
return crypto.Keccak256Hash(key.Bytes())
6262
}
6363

6464
func GetMapping(indexOnContract, key string) common.Hash {
6565
keyBytes := common.FromHex(key + indexOnContract)
66-
encoded := crypto.Keccak256(keyBytes)
67-
return common.BytesToHash(encoded)
66+
return crypto.Keccak256Hash(keyBytes)
6867
}
6968

7069
func GetNestedMapping(indexOnContract, primaryKey, secondaryKey string) common.Hash {
7170
primaryMappingIndex := crypto.Keccak256(common.FromHex(primaryKey + indexOnContract))
72-
secondaryMappingIndex := crypto.Keccak256(common.FromHex(secondaryKey), primaryMappingIndex)
73-
return common.BytesToHash(secondaryMappingIndex)
71+
return crypto.Keccak256Hash(common.FromHex(secondaryKey), primaryMappingIndex)
7472
}
7573

7674
func GetIncrementedKey(original common.Hash, incrementBy int64) common.Hash {

libraries/shared/storage/storage_queue_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package storage_test
1818

1919
import (
2020
"github.com/ethereum/go-ethereum/common"
21-
"github.com/ethereum/go-ethereum/crypto"
2221
. "github.com/onsi/ginkgo"
2322
. "github.com/onsi/gomega"
2423
"github.com/vulcanize/vulcanizedb/libraries/shared/storage"
@@ -35,10 +34,9 @@ var _ = Describe("Storage queue", func() {
3534
)
3635

3736
BeforeEach(func() {
38-
fakeAddr := common.FromHex("0x123456")
39-
hashedFakeAddr := crypto.Keccak256(fakeAddr)
37+
fakeAddr := "0x123456"
4038
diff = utils.StorageDiff{
41-
KeccakOfContractAddress: common.BytesToHash(hashedFakeAddr),
39+
KeccakOfContractAddress: utils.HexToKeccak256Hash(fakeAddr),
4240
BlockHash: common.HexToHash("0x678901"),
4341
BlockHeight: 987,
4442
StorageKey: common.HexToHash("0x654321"),
@@ -83,10 +81,9 @@ var _ = Describe("Storage queue", func() {
8381
})
8482

8583
It("gets all storage diffs from db", func() {
86-
fakeAddr := common.FromHex("0x234567")
87-
hashedFakeAddr := crypto.Keccak256(fakeAddr)
84+
fakeAddr := "0x234567"
8885
diffTwo := utils.StorageDiff{
89-
KeccakOfContractAddress: common.BytesToHash(hashedFakeAddr),
86+
KeccakOfContractAddress: utils.HexToKeccak256Hash(fakeAddr),
9087
BlockHash: common.HexToHash("0x678902"),
9188
BlockHeight: 988,
9289
StorageKey: common.HexToHash("0x654322"),

libraries/shared/storage/utils/diff.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ func FromParityCsvRow(csvRow []string) (StorageDiff, error) {
4242
if err != nil {
4343
return StorageDiff{}, err
4444
}
45-
hashedAddr := crypto.Keccak256(common.FromHex(csvRow[0]))
4645
return StorageDiff{
47-
KeccakOfContractAddress: common.BytesToHash(hashedAddr),
46+
KeccakOfContractAddress: HexToKeccak256Hash(csvRow[0]),
4847
BlockHash: common.HexToHash(csvRow[1]),
4948
BlockHeight: height,
5049
StorageKey: common.HexToHash(csvRow[3]),
@@ -61,3 +60,7 @@ func FromGethStateDiff(account statediff.AccountDiff, stateDiff *statediff.State
6160
StorageValue: common.BytesToHash(storage.Value),
6261
}
6362
}
63+
64+
func HexToKeccak256Hash(addr string) common.Hash {
65+
return crypto.Keccak256Hash(common.FromHex(addr))
66+
}

libraries/shared/storage/utils/diff_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package utils_test
1818

1919
import (
2020
"github.com/ethereum/go-ethereum/common"
21-
"github.com/ethereum/go-ethereum/crypto"
2221
"github.com/ethereum/go-ethereum/statediff"
2322
. "github.com/onsi/ginkgo"
2423
. "github.com/onsi/gomega"
@@ -41,7 +40,7 @@ var _ = Describe("Storage row parsing", func() {
4140
result, err := utils.FromParityCsvRow(data)
4241

4342
Expect(err).NotTo(HaveOccurred())
44-
expectedKeccakOfContractAddress := common.BytesToHash(crypto.Keccak256(common.FromHex(contract)))
43+
expectedKeccakOfContractAddress := utils.HexToKeccak256Hash(contract)
4544
Expect(result.KeccakOfContractAddress).To(Equal(expectedKeccakOfContractAddress))
4645
Expect(result.BlockHash).To(Equal(common.HexToHash(blockHash)))
4746
Expect(result.BlockHeight).To(Equal(789))

libraries/shared/watcher/storage_watcher_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package watcher_test
1818

1919
import (
2020
"github.com/ethereum/go-ethereum/common"
21-
"github.com/ethereum/go-ethereum/crypto"
2221
. "github.com/onsi/ginkgo"
2322
. "github.com/onsi/gomega"
2423
"github.com/sirupsen/logrus"
@@ -36,7 +35,7 @@ import (
3635
var _ = Describe("Storage Watcher", func() {
3736
Describe("AddTransformer", func() {
3837
It("adds transformers", func() {
39-
fakeHashedAddress := common.BytesToHash(crypto.Keccak256(common.FromHex("0x12345")))
38+
fakeHashedAddress := utils.HexToKeccak256Hash("0x12345")
4039
fakeTransformer := &mocks.MockStorageTransformer{KeccakOfAddress: fakeHashedAddress}
4140
w := watcher.NewStorageWatcher(mocks.NewMockStorageFetcher(), test_config.NewTestDB(test_config.NewTestNode()))
4241

@@ -61,7 +60,7 @@ var _ = Describe("Storage Watcher", func() {
6160
BeforeEach(func() {
6261
errs = make(chan error)
6362
diffs = make(chan utils.StorageDiff)
64-
hashedAddress = common.BytesToHash(crypto.Keccak256(common.FromHex("0x0123456789abcdef")))
63+
hashedAddress = utils.HexToKeccak256Hash("0x0123456789abcdef")
6564
mockFetcher = mocks.NewMockStorageFetcher()
6665
mockQueue = &mocks.MockStorageQueue{}
6766
mockTransformer = &mocks.MockStorageTransformer{KeccakOfAddress: hashedAddress}
@@ -192,7 +191,7 @@ var _ = Describe("Storage Watcher", func() {
192191
It("deletes obsolete diff from queue if contract not recognized", func(done Done) {
193192
obsoleteDiff := utils.StorageDiff{
194193
Id: csvDiff.Id + 1,
195-
KeccakOfContractAddress: common.BytesToHash(crypto.Keccak256(common.FromHex("0xfedcba9876543210"))),
194+
KeccakOfContractAddress: utils.HexToKeccak256Hash("0xfedcba9876543210"),
196195
}
197196
mockQueue.DiffsToReturn = []utils.StorageDiff{obsoleteDiff}
198197

@@ -207,7 +206,7 @@ var _ = Describe("Storage Watcher", func() {
207206
It("logs error if deleting obsolete diff fails", func(done Done) {
208207
obsoleteDiff := utils.StorageDiff{
209208
Id: csvDiff.Id + 1,
210-
KeccakOfContractAddress: common.BytesToHash(crypto.Keccak256(common.FromHex("0xfedcba9876543210"))),
209+
KeccakOfContractAddress: utils.HexToKeccak256Hash("0xfedcba9876543210"),
211210
}
212211
mockQueue.DiffsToReturn = []utils.StorageDiff{obsoleteDiff}
213212
mockQueue.DeleteErr = fakes.FakeError

pkg/contract_watcher/shared/types/event.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,5 @@ func (e Event) Sig() common.Hash {
9292
types[i] = input.Type.String()
9393
}
9494

95-
return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ",")))))
95+
return crypto.Keccak256Hash([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ","))))
9696
}

pkg/contract_watcher/shared/types/method.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,5 @@ func (m Method) Sig() common.Hash {
107107
i++
108108
}
109109

110-
return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", m.Name, strings.Join(types, ",")))))
110+
return crypto.Keccak256Hash([]byte(fmt.Sprintf("%v(%v)", m.Name, strings.Join(types, ","))))
111111
}

0 commit comments

Comments
 (0)