Extract helper for converting hex to keccak256 hash

- Also prefer crypto.Keccak256Hash(x) to common.BytesToHash(crypto.Keccak256(x))
This commit is contained in:
Rob Mulholand 2019-08-16 16:30:26 -05:00 committed by Elizabeth Engelman
parent d06dddbfaa
commit 2ff88de859
8 changed files with 20 additions and 25 deletions

View File

@ -18,7 +18,6 @@ package storage_test
import ( import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/libraries/shared/factories/storage" "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() { It("returns the contract address being watched", func() {
fakeAddress := common.BytesToHash(crypto.Keccak256(common.FromHex("0x12345"))) fakeAddress := utils.HexToKeccak256Hash("0x12345")
t.HashedAddress = fakeAddress t.HashedAddress = fakeAddress
Expect(t.KeccakContractAddress()).To(Equal(fakeAddress)) Expect(t.KeccakContractAddress()).To(Equal(fakeAddress))

View File

@ -58,19 +58,17 @@ func AddHashedKeys(currentMappings map[common.Hash]utils.StorageValueMetadata) m
} }
func hashKey(key common.Hash) common.Hash { 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 { func GetMapping(indexOnContract, key string) common.Hash {
keyBytes := common.FromHex(key + indexOnContract) keyBytes := common.FromHex(key + indexOnContract)
encoded := crypto.Keccak256(keyBytes) return crypto.Keccak256Hash(keyBytes)
return common.BytesToHash(encoded)
} }
func GetNestedMapping(indexOnContract, primaryKey, secondaryKey string) common.Hash { func GetNestedMapping(indexOnContract, primaryKey, secondaryKey string) common.Hash {
primaryMappingIndex := crypto.Keccak256(common.FromHex(primaryKey + indexOnContract)) primaryMappingIndex := crypto.Keccak256(common.FromHex(primaryKey + indexOnContract))
secondaryMappingIndex := crypto.Keccak256(common.FromHex(secondaryKey), primaryMappingIndex) return crypto.Keccak256Hash(common.FromHex(secondaryKey), primaryMappingIndex)
return common.BytesToHash(secondaryMappingIndex)
} }
func GetIncrementedKey(original common.Hash, incrementBy int64) common.Hash { func GetIncrementedKey(original common.Hash, incrementBy int64) common.Hash {

View File

@ -18,7 +18,6 @@ package storage_test
import ( import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/libraries/shared/storage" "github.com/vulcanize/vulcanizedb/libraries/shared/storage"
@ -35,10 +34,9 @@ var _ = Describe("Storage queue", func() {
) )
BeforeEach(func() { BeforeEach(func() {
fakeAddr := common.FromHex("0x123456") fakeAddr := "0x123456"
hashedFakeAddr := crypto.Keccak256(fakeAddr)
diff = utils.StorageDiff{ diff = utils.StorageDiff{
KeccakOfContractAddress: common.BytesToHash(hashedFakeAddr), KeccakOfContractAddress: utils.HexToKeccak256Hash(fakeAddr),
BlockHash: common.HexToHash("0x678901"), BlockHash: common.HexToHash("0x678901"),
BlockHeight: 987, BlockHeight: 987,
StorageKey: common.HexToHash("0x654321"), StorageKey: common.HexToHash("0x654321"),
@ -83,10 +81,9 @@ var _ = Describe("Storage queue", func() {
}) })
It("gets all storage diffs from db", func() { It("gets all storage diffs from db", func() {
fakeAddr := common.FromHex("0x234567") fakeAddr := "0x234567"
hashedFakeAddr := crypto.Keccak256(fakeAddr)
diffTwo := utils.StorageDiff{ diffTwo := utils.StorageDiff{
KeccakOfContractAddress: common.BytesToHash(hashedFakeAddr), KeccakOfContractAddress: utils.HexToKeccak256Hash(fakeAddr),
BlockHash: common.HexToHash("0x678902"), BlockHash: common.HexToHash("0x678902"),
BlockHeight: 988, BlockHeight: 988,
StorageKey: common.HexToHash("0x654322"), StorageKey: common.HexToHash("0x654322"),

View File

@ -42,9 +42,8 @@ func FromParityCsvRow(csvRow []string) (StorageDiff, error) {
if err != nil { if err != nil {
return StorageDiff{}, err return StorageDiff{}, err
} }
hashedAddr := crypto.Keccak256(common.FromHex(csvRow[0]))
return StorageDiff{ return StorageDiff{
KeccakOfContractAddress: common.BytesToHash(hashedAddr), KeccakOfContractAddress: HexToKeccak256Hash(csvRow[0]),
BlockHash: common.HexToHash(csvRow[1]), BlockHash: common.HexToHash(csvRow[1]),
BlockHeight: height, BlockHeight: height,
StorageKey: common.HexToHash(csvRow[3]), StorageKey: common.HexToHash(csvRow[3]),
@ -61,3 +60,7 @@ func FromGethStateDiff(account statediff.AccountDiff, stateDiff *statediff.State
StorageValue: common.BytesToHash(storage.Value), StorageValue: common.BytesToHash(storage.Value),
} }
} }
func HexToKeccak256Hash(addr string) common.Hash {
return crypto.Keccak256Hash(common.FromHex(addr))
}

View File

@ -18,7 +18,6 @@ package utils_test
import ( import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/statediff" "github.com/ethereum/go-ethereum/statediff"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@ -41,7 +40,7 @@ var _ = Describe("Storage row parsing", func() {
result, err := utils.FromParityCsvRow(data) result, err := utils.FromParityCsvRow(data)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
expectedKeccakOfContractAddress := common.BytesToHash(crypto.Keccak256(common.FromHex(contract))) expectedKeccakOfContractAddress := utils.HexToKeccak256Hash(contract)
Expect(result.KeccakOfContractAddress).To(Equal(expectedKeccakOfContractAddress)) Expect(result.KeccakOfContractAddress).To(Equal(expectedKeccakOfContractAddress))
Expect(result.BlockHash).To(Equal(common.HexToHash(blockHash))) Expect(result.BlockHash).To(Equal(common.HexToHash(blockHash)))
Expect(result.BlockHeight).To(Equal(789)) Expect(result.BlockHeight).To(Equal(789))

View File

@ -18,7 +18,6 @@ package watcher_test
import ( import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -36,7 +35,7 @@ import (
var _ = Describe("Storage Watcher", func() { var _ = Describe("Storage Watcher", func() {
Describe("AddTransformer", func() { Describe("AddTransformer", func() {
It("adds transformers", func() { It("adds transformers", func() {
fakeHashedAddress := common.BytesToHash(crypto.Keccak256(common.FromHex("0x12345"))) fakeHashedAddress := utils.HexToKeccak256Hash("0x12345")
fakeTransformer := &mocks.MockStorageTransformer{KeccakOfAddress: fakeHashedAddress} fakeTransformer := &mocks.MockStorageTransformer{KeccakOfAddress: fakeHashedAddress}
w := watcher.NewStorageWatcher(mocks.NewMockStorageFetcher(), test_config.NewTestDB(test_config.NewTestNode())) w := watcher.NewStorageWatcher(mocks.NewMockStorageFetcher(), test_config.NewTestDB(test_config.NewTestNode()))
@ -61,7 +60,7 @@ var _ = Describe("Storage Watcher", func() {
BeforeEach(func() { BeforeEach(func() {
errs = make(chan error) errs = make(chan error)
diffs = make(chan utils.StorageDiff) diffs = make(chan utils.StorageDiff)
hashedAddress = common.BytesToHash(crypto.Keccak256(common.FromHex("0x0123456789abcdef"))) hashedAddress = utils.HexToKeccak256Hash("0x0123456789abcdef")
mockFetcher = mocks.NewMockStorageFetcher() mockFetcher = mocks.NewMockStorageFetcher()
mockQueue = &mocks.MockStorageQueue{} mockQueue = &mocks.MockStorageQueue{}
mockTransformer = &mocks.MockStorageTransformer{KeccakOfAddress: hashedAddress} 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) { It("deletes obsolete diff from queue if contract not recognized", func(done Done) {
obsoleteDiff := utils.StorageDiff{ obsoleteDiff := utils.StorageDiff{
Id: csvDiff.Id + 1, Id: csvDiff.Id + 1,
KeccakOfContractAddress: common.BytesToHash(crypto.Keccak256(common.FromHex("0xfedcba9876543210"))), KeccakOfContractAddress: utils.HexToKeccak256Hash("0xfedcba9876543210"),
} }
mockQueue.DiffsToReturn = []utils.StorageDiff{obsoleteDiff} 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) { It("logs error if deleting obsolete diff fails", func(done Done) {
obsoleteDiff := utils.StorageDiff{ obsoleteDiff := utils.StorageDiff{
Id: csvDiff.Id + 1, Id: csvDiff.Id + 1,
KeccakOfContractAddress: common.BytesToHash(crypto.Keccak256(common.FromHex("0xfedcba9876543210"))), KeccakOfContractAddress: utils.HexToKeccak256Hash("0xfedcba9876543210"),
} }
mockQueue.DiffsToReturn = []utils.StorageDiff{obsoleteDiff} mockQueue.DiffsToReturn = []utils.StorageDiff{obsoleteDiff}
mockQueue.DeleteErr = fakes.FakeError mockQueue.DeleteErr = fakes.FakeError

View File

@ -92,5 +92,5 @@ func (e Event) Sig() common.Hash {
types[i] = input.Type.String() 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, ","))))
} }

View File

@ -107,5 +107,5 @@ func (m Method) Sig() common.Hash {
i++ 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, ","))))
} }