From 2ff88de8595a51a4ce72a77205f4fc131cd53d77 Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Fri, 16 Aug 2019 16:30:26 -0500 Subject: [PATCH] Extract helper for converting hex to keccak256 hash - Also prefer crypto.Keccak256Hash(x) to common.BytesToHash(crypto.Keccak256(x)) --- .../shared/factories/storage/transformer_test.go | 3 +-- libraries/shared/storage/mappings.go | 8 +++----- libraries/shared/storage/storage_queue_test.go | 11 ++++------- libraries/shared/storage/utils/diff.go | 7 +++++-- libraries/shared/storage/utils/diff_test.go | 3 +-- libraries/shared/watcher/storage_watcher_test.go | 9 ++++----- pkg/contract_watcher/shared/types/event.go | 2 +- pkg/contract_watcher/shared/types/method.go | 2 +- 8 files changed, 20 insertions(+), 25 deletions(-) diff --git a/libraries/shared/factories/storage/transformer_test.go b/libraries/shared/factories/storage/transformer_test.go index b67ad02d..a0c2daa1 100644 --- a/libraries/shared/factories/storage/transformer_test.go +++ b/libraries/shared/factories/storage/transformer_test.go @@ -18,7 +18,6 @@ package storage_test import ( "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/vulcanize/vulcanizedb/libraries/shared/factories/storage" @@ -45,7 +44,7 @@ var _ = Describe("Storage transformer", func() { }) It("returns the contract address being watched", func() { - fakeAddress := common.BytesToHash(crypto.Keccak256(common.FromHex("0x12345"))) + fakeAddress := utils.HexToKeccak256Hash("0x12345") t.HashedAddress = fakeAddress Expect(t.KeccakContractAddress()).To(Equal(fakeAddress)) diff --git a/libraries/shared/storage/mappings.go b/libraries/shared/storage/mappings.go index 9a534925..f8b089ae 100644 --- a/libraries/shared/storage/mappings.go +++ b/libraries/shared/storage/mappings.go @@ -58,19 +58,17 @@ func AddHashedKeys(currentMappings map[common.Hash]utils.StorageValueMetadata) m } func hashKey(key common.Hash) common.Hash { - return common.BytesToHash(crypto.Keccak256(key.Bytes())) + return crypto.Keccak256Hash(key.Bytes()) } func GetMapping(indexOnContract, key string) common.Hash { keyBytes := common.FromHex(key + indexOnContract) - encoded := crypto.Keccak256(keyBytes) - return common.BytesToHash(encoded) + return crypto.Keccak256Hash(keyBytes) } func GetNestedMapping(indexOnContract, primaryKey, secondaryKey string) common.Hash { primaryMappingIndex := crypto.Keccak256(common.FromHex(primaryKey + indexOnContract)) - secondaryMappingIndex := crypto.Keccak256(common.FromHex(secondaryKey), primaryMappingIndex) - return common.BytesToHash(secondaryMappingIndex) + return crypto.Keccak256Hash(common.FromHex(secondaryKey), primaryMappingIndex) } func GetIncrementedKey(original common.Hash, incrementBy int64) common.Hash { diff --git a/libraries/shared/storage/storage_queue_test.go b/libraries/shared/storage/storage_queue_test.go index 693f31d0..be53c01e 100644 --- a/libraries/shared/storage/storage_queue_test.go +++ b/libraries/shared/storage/storage_queue_test.go @@ -18,7 +18,6 @@ package storage_test import ( "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/vulcanize/vulcanizedb/libraries/shared/storage" @@ -35,10 +34,9 @@ var _ = Describe("Storage queue", func() { ) BeforeEach(func() { - fakeAddr := common.FromHex("0x123456") - hashedFakeAddr := crypto.Keccak256(fakeAddr) + fakeAddr := "0x123456" diff = utils.StorageDiff{ - KeccakOfContractAddress: common.BytesToHash(hashedFakeAddr), + KeccakOfContractAddress: utils.HexToKeccak256Hash(fakeAddr), BlockHash: common.HexToHash("0x678901"), BlockHeight: 987, StorageKey: common.HexToHash("0x654321"), @@ -83,10 +81,9 @@ var _ = Describe("Storage queue", func() { }) It("gets all storage diffs from db", func() { - fakeAddr := common.FromHex("0x234567") - hashedFakeAddr := crypto.Keccak256(fakeAddr) + fakeAddr := "0x234567" diffTwo := utils.StorageDiff{ - KeccakOfContractAddress: common.BytesToHash(hashedFakeAddr), + KeccakOfContractAddress: utils.HexToKeccak256Hash(fakeAddr), BlockHash: common.HexToHash("0x678902"), BlockHeight: 988, StorageKey: common.HexToHash("0x654322"), diff --git a/libraries/shared/storage/utils/diff.go b/libraries/shared/storage/utils/diff.go index 720ef197..029dfbde 100644 --- a/libraries/shared/storage/utils/diff.go +++ b/libraries/shared/storage/utils/diff.go @@ -42,9 +42,8 @@ func FromParityCsvRow(csvRow []string) (StorageDiff, error) { if err != nil { return StorageDiff{}, err } - hashedAddr := crypto.Keccak256(common.FromHex(csvRow[0])) return StorageDiff{ - KeccakOfContractAddress: common.BytesToHash(hashedAddr), + KeccakOfContractAddress: HexToKeccak256Hash(csvRow[0]), BlockHash: common.HexToHash(csvRow[1]), BlockHeight: height, StorageKey: common.HexToHash(csvRow[3]), @@ -61,3 +60,7 @@ func FromGethStateDiff(account statediff.AccountDiff, stateDiff *statediff.State StorageValue: common.BytesToHash(storage.Value), } } + +func HexToKeccak256Hash(addr string) common.Hash { + return crypto.Keccak256Hash(common.FromHex(addr)) +} diff --git a/libraries/shared/storage/utils/diff_test.go b/libraries/shared/storage/utils/diff_test.go index bcb0f49c..24df7451 100644 --- a/libraries/shared/storage/utils/diff_test.go +++ b/libraries/shared/storage/utils/diff_test.go @@ -18,7 +18,6 @@ package utils_test import ( "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/statediff" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -41,7 +40,7 @@ var _ = Describe("Storage row parsing", func() { result, err := utils.FromParityCsvRow(data) Expect(err).NotTo(HaveOccurred()) - expectedKeccakOfContractAddress := common.BytesToHash(crypto.Keccak256(common.FromHex(contract))) + expectedKeccakOfContractAddress := utils.HexToKeccak256Hash(contract) Expect(result.KeccakOfContractAddress).To(Equal(expectedKeccakOfContractAddress)) Expect(result.BlockHash).To(Equal(common.HexToHash(blockHash))) Expect(result.BlockHeight).To(Equal(789)) diff --git a/libraries/shared/watcher/storage_watcher_test.go b/libraries/shared/watcher/storage_watcher_test.go index 98b30aa8..1d2d5f1b 100644 --- a/libraries/shared/watcher/storage_watcher_test.go +++ b/libraries/shared/watcher/storage_watcher_test.go @@ -18,7 +18,6 @@ package watcher_test import ( "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/sirupsen/logrus" @@ -36,7 +35,7 @@ import ( var _ = Describe("Storage Watcher", func() { Describe("AddTransformer", func() { It("adds transformers", func() { - fakeHashedAddress := common.BytesToHash(crypto.Keccak256(common.FromHex("0x12345"))) + fakeHashedAddress := utils.HexToKeccak256Hash("0x12345") fakeTransformer := &mocks.MockStorageTransformer{KeccakOfAddress: fakeHashedAddress} w := watcher.NewStorageWatcher(mocks.NewMockStorageFetcher(), test_config.NewTestDB(test_config.NewTestNode())) @@ -61,7 +60,7 @@ var _ = Describe("Storage Watcher", func() { BeforeEach(func() { errs = make(chan error) diffs = make(chan utils.StorageDiff) - hashedAddress = common.BytesToHash(crypto.Keccak256(common.FromHex("0x0123456789abcdef"))) + hashedAddress = utils.HexToKeccak256Hash("0x0123456789abcdef") mockFetcher = mocks.NewMockStorageFetcher() mockQueue = &mocks.MockStorageQueue{} mockTransformer = &mocks.MockStorageTransformer{KeccakOfAddress: hashedAddress} @@ -192,7 +191,7 @@ var _ = Describe("Storage Watcher", func() { It("deletes obsolete diff from queue if contract not recognized", func(done Done) { obsoleteDiff := utils.StorageDiff{ Id: csvDiff.Id + 1, - KeccakOfContractAddress: common.BytesToHash(crypto.Keccak256(common.FromHex("0xfedcba9876543210"))), + KeccakOfContractAddress: utils.HexToKeccak256Hash("0xfedcba9876543210"), } mockQueue.DiffsToReturn = []utils.StorageDiff{obsoleteDiff} @@ -207,7 +206,7 @@ var _ = Describe("Storage Watcher", func() { It("logs error if deleting obsolete diff fails", func(done Done) { obsoleteDiff := utils.StorageDiff{ Id: csvDiff.Id + 1, - KeccakOfContractAddress: common.BytesToHash(crypto.Keccak256(common.FromHex("0xfedcba9876543210"))), + KeccakOfContractAddress: utils.HexToKeccak256Hash("0xfedcba9876543210"), } mockQueue.DiffsToReturn = []utils.StorageDiff{obsoleteDiff} mockQueue.DeleteErr = fakes.FakeError diff --git a/pkg/contract_watcher/shared/types/event.go b/pkg/contract_watcher/shared/types/event.go index b70c2a0c..dfca596d 100644 --- a/pkg/contract_watcher/shared/types/event.go +++ b/pkg/contract_watcher/shared/types/event.go @@ -92,5 +92,5 @@ func (e Event) Sig() common.Hash { types[i] = input.Type.String() } - return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ","))))) + return crypto.Keccak256Hash([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ",")))) } diff --git a/pkg/contract_watcher/shared/types/method.go b/pkg/contract_watcher/shared/types/method.go index 795b5b7f..7b962b17 100644 --- a/pkg/contract_watcher/shared/types/method.go +++ b/pkg/contract_watcher/shared/types/method.go @@ -107,5 +107,5 @@ func (m Method) Sig() common.Hash { i++ } - return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", m.Name, strings.Join(types, ","))))) + return crypto.Keccak256Hash([]byte(fmt.Sprintf("%v(%v)", m.Name, strings.Join(types, ",")))) }