From 66d695d93b91588d99271af5d855627aa7813fac Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman Date: Tue, 6 Aug 2019 09:20:50 -0500 Subject: [PATCH] Update keccak of address to be a common.Hash instead of a common.Address --- .../fetcher/geth_rpc_storage_fetcher.go | 10 +++--- .../fetcher/geth_rpc_storage_fetcher_test.go | 22 ++++++------ libraries/shared/storage/utils/diff.go | 13 +++---- .../shared/watcher/csv_storage_watcher.go | 5 +-- .../shared/watcher/geth_storage_watcher.go | 14 ++++---- .../watcher/geth_storage_watcher_test.go | 34 +++++++++---------- libraries/shared/watcher/storage_watcher.go | 12 +++---- 7 files changed, 56 insertions(+), 54 deletions(-) diff --git a/libraries/shared/fetcher/geth_rpc_storage_fetcher.go b/libraries/shared/fetcher/geth_rpc_storage_fetcher.go index 1ecbd258..58a77c68 100644 --- a/libraries/shared/fetcher/geth_rpc_storage_fetcher.go +++ b/libraries/shared/fetcher/geth_rpc_storage_fetcher.go @@ -62,11 +62,11 @@ func (fetcher GethRpcStorageFetcher) FetchStorageDiffs(out chan<- utils.StorageD for _, storage := range account.Storage { logrus.Trace("adding storage diff to out channel") out <- utils.StorageDiff{ - Contract: common.BytesToAddress(account.Key), - BlockHash: stateDiff.BlockHash, - BlockHeight: int(stateDiff.BlockNumber.Int64()), - StorageKey: common.BytesToHash(storage.Key), - StorageValue: common.BytesToHash(storage.Value), + KeccakOfContractAddress: common.BytesToHash(account.Key), + BlockHash: stateDiff.BlockHash, + BlockHeight: int(stateDiff.BlockNumber.Int64()), + StorageKey: common.BytesToHash(storage.Key), + StorageValue: common.BytesToHash(storage.Value), } } } diff --git a/libraries/shared/fetcher/geth_rpc_storage_fetcher_test.go b/libraries/shared/fetcher/geth_rpc_storage_fetcher_test.go index 90ae5b07..0e8be2f8 100644 --- a/libraries/shared/fetcher/geth_rpc_storage_fetcher_test.go +++ b/libraries/shared/fetcher/geth_rpc_storage_fetcher_test.go @@ -101,20 +101,18 @@ var _ = Describe("Geth RPC Storage Fetcher", func() { height := test_data.BlockNumber intHeight := int(height.Int64()) expectedStorageDiff := utils.StorageDiff{ - //this is not the contract address, but the keccak 256 of the address - Contract: common.BytesToAddress(test_data.ContractLeafKey[:]), - BlockHash: common.HexToHash("0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"), - BlockHeight: intHeight, - StorageKey: common.BytesToHash(test_data.StorageKey), - StorageValue: common.BytesToHash(test_data.StorageValue), + KeccakOfContractAddress: common.BytesToHash(test_data.ContractLeafKey[:]), + BlockHash: common.HexToHash("0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"), + BlockHeight: intHeight, + StorageKey: common.BytesToHash(test_data.StorageKey), + StorageValue: common.BytesToHash(test_data.StorageValue), } anotherExpectedStorageDiff := utils.StorageDiff{ - //this is not the contract address, but the keccak 256 of the address - Contract: common.BytesToAddress(test_data.AnotherContractLeafKey[:]), - BlockHash: common.HexToHash("0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"), - BlockHeight: intHeight, - StorageKey: common.BytesToHash(test_data.StorageKey), - StorageValue: common.BytesToHash(test_data.StorageValue), + KeccakOfContractAddress: common.BytesToHash(test_data.AnotherContractLeafKey[:]), + BlockHash: common.HexToHash("0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"), + BlockHeight: intHeight, + StorageKey: common.BytesToHash(test_data.StorageKey), + StorageValue: common.BytesToHash(test_data.StorageValue), } Expect(<-storagediffChan).To(Equal(expectedStorageDiff)) Expect(<-storagediffChan).To(Equal(anotherExpectedStorageDiff)) diff --git a/libraries/shared/storage/utils/diff.go b/libraries/shared/storage/utils/diff.go index 0abb728d..7dddfb4a 100644 --- a/libraries/shared/storage/utils/diff.go +++ b/libraries/shared/storage/utils/diff.go @@ -25,12 +25,13 @@ import ( const ExpectedRowLength = 5 type StorageDiff struct { - Id int - Contract common.Address - BlockHash common.Hash `db:"block_hash"` - BlockHeight int `db:"block_height"` - StorageKey common.Hash `db:"storage_key"` - StorageValue common.Hash `db:"storage_value"` + Id int + Contract common.Address + KeccakOfContractAddress common.Hash + BlockHash common.Hash `db:"block_hash"` + BlockHeight int `db:"block_height"` + StorageKey common.Hash `db:"storage_key"` + StorageValue common.Hash `db:"storage_value"` } func FromStrings(csvRow []string) (StorageDiff, error) { diff --git a/libraries/shared/watcher/csv_storage_watcher.go b/libraries/shared/watcher/csv_storage_watcher.go index a08fc938..c85f19ad 100644 --- a/libraries/shared/watcher/csv_storage_watcher.go +++ b/libraries/shared/watcher/csv_storage_watcher.go @@ -18,6 +18,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/vulcanize/vulcanizedb/libraries/shared/fetcher" "github.com/vulcanize/vulcanizedb/libraries/shared/storage" + "github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils" "github.com/vulcanize/vulcanizedb/libraries/shared/transformer" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" ) @@ -39,7 +40,7 @@ func NewCsvStorageWatcher(fetcher fetcher.IStorageFetcher, db *postgres.DB) CsvS return CsvStorageWatcher{StorageWatcher: storageWatcher} } -func (storageWatcher StorageWatcher) getCsvTransformer(contractAddress common.Address) (transformer.StorageTransformer, bool) { - storageTransformer, ok := storageWatcher.Transformers[contractAddress] +func (storageWatcher StorageWatcher) getCsvTransformer(diff utils.StorageDiff) (transformer.StorageTransformer, bool) { + storageTransformer, ok := storageWatcher.Transformers[diff.Contract] return storageTransformer, ok } diff --git a/libraries/shared/watcher/geth_storage_watcher.go b/libraries/shared/watcher/geth_storage_watcher.go index 58076590..d4138726 100644 --- a/libraries/shared/watcher/geth_storage_watcher.go +++ b/libraries/shared/watcher/geth_storage_watcher.go @@ -19,6 +19,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/vulcanize/vulcanizedb/libraries/shared/fetcher" "github.com/vulcanize/vulcanizedb/libraries/shared/storage" + "github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils" "github.com/vulcanize/vulcanizedb/libraries/shared/transformer" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" ) @@ -30,7 +31,7 @@ type GethStorageWatcher struct { func NewGethStorageWatcher(fetcher fetcher.IStorageFetcher, db *postgres.DB) GethStorageWatcher { queue := storage.NewStorageQueue(db) transformers := make(map[common.Address]transformer.StorageTransformer) - keccakAddressTransformers := make(map[common.Address]transformer.StorageTransformer) + keccakAddressTransformers := make(map[common.Hash]transformer.StorageTransformer) storageWatcher := StorageWatcher{ db: db, StorageFetcher: fetcher, @@ -42,15 +43,16 @@ func NewGethStorageWatcher(fetcher fetcher.IStorageFetcher, db *postgres.DB) Get return GethStorageWatcher{StorageWatcher: storageWatcher} } -func (storageWatcher StorageWatcher) getTransformerForGethWatcher(contractAddress common.Address) (transformer.StorageTransformer, bool) { - storageTransformer, ok := storageWatcher.KeccakAddressTransformers[contractAddress] +func (storageWatcher StorageWatcher) getTransformerForGethWatcher(diff utils.StorageDiff) (transformer.StorageTransformer, bool) { + keccakOfAddress := diff.KeccakOfContractAddress + storageTransformer, ok := storageWatcher.KeccakAddressTransformers[keccakOfAddress] if ok { return storageTransformer, ok } else { for address, transformer := range storageWatcher.Transformers { - keccakOfTransformerAddress := common.BytesToAddress(crypto.Keccak256(address[:])) - if keccakOfTransformerAddress == contractAddress { - storageWatcher.KeccakAddressTransformers[contractAddress] = transformer + keccakOfTransformerAddress := common.BytesToHash(crypto.Keccak256(address[:])) + if keccakOfTransformerAddress == keccakOfAddress { + storageWatcher.KeccakAddressTransformers[keccakOfAddress] = transformer return transformer, true } } diff --git a/libraries/shared/watcher/geth_storage_watcher_test.go b/libraries/shared/watcher/geth_storage_watcher_test.go index bc999ec0..cb46ac8e 100644 --- a/libraries/shared/watcher/geth_storage_watcher_test.go +++ b/libraries/shared/watcher/geth_storage_watcher_test.go @@ -56,24 +56,24 @@ var _ = Describe("Geth Storage Watcher", func() { diffs chan utils.StorageDiff storageWatcher watcher.GethStorageWatcher address common.Address - keccakOfAddress common.Address + keccakOfAddress common.Hash ) BeforeEach(func() { errs = make(chan error) diffs = make(chan utils.StorageDiff) address = common.HexToAddress("0x0123456789abcdef") - keccakOfAddress = common.BytesToAddress(crypto.Keccak256(address[:])) + keccakOfAddress = common.BytesToHash(crypto.Keccak256(address[:])) mockFetcher = mocks.NewMockStorageFetcher() mockQueue = &mocks.MockStorageQueue{} mockTransformer = &mocks.MockStorageTransformer{Address: address} gethDiff = utils.StorageDiff{ - Id: 1338, - Contract: keccakOfAddress, - BlockHash: common.HexToHash("0xfedcba9876543210"), - BlockHeight: 0, - StorageKey: common.HexToHash("0xabcdef1234567890"), - StorageValue: common.HexToHash("0x9876543210abcdef"), + Id: 1338, + KeccakOfContractAddress: keccakOfAddress, + BlockHash: common.HexToHash("0xfedcba9876543210"), + BlockHeight: 0, + StorageKey: common.HexToHash("0xabcdef1234567890"), + StorageValue: common.HexToHash("0x9876543210abcdef"), } }) @@ -151,10 +151,10 @@ var _ = Describe("Geth Storage Watcher", func() { It("keeps track transformers by the keccak256 hash of their contract address ", func(done Done) { go storageWatcher.Execute(diffs, errs, time.Hour) - m := make(map[common.Address]transformer.StorageTransformer) + m := make(map[common.Hash]transformer.StorageTransformer) m[keccakOfAddress] = mockTransformer - Eventually(func() map[common.Address]transformer.StorageTransformer { + Eventually(func() map[common.Hash]transformer.StorageTransformer { return storageWatcher.KeccakAddressTransformers }).Should(Equal(m)) @@ -164,15 +164,15 @@ var _ = Describe("Geth Storage Watcher", func() { It("gets the transformer from the known keccak address map first", func(done Done) { anotherAddress := common.HexToAddress("0xafakeaddress") anotherTransformer := &mocks.MockStorageTransformer{Address: anotherAddress} - keccakOfAnotherAddress := common.BytesToAddress(crypto.Keccak256(anotherAddress[:])) + keccakOfAnotherAddress := common.BytesToHash(crypto.Keccak256(anotherAddress[:])) anotherGethDiff := utils.StorageDiff{ - Id: 1338, - Contract: keccakOfAnotherAddress, - BlockHash: common.HexToHash("0xfedcba9876543210"), - BlockHeight: 0, - StorageKey: common.HexToHash("0xabcdef1234567890"), - StorageValue: common.HexToHash("0x9876543210abcdef"), + Id: 1338, + KeccakOfContractAddress: keccakOfAnotherAddress, + BlockHash: common.HexToHash("0xfedcba9876543210"), + BlockHeight: 0, + StorageKey: common.HexToHash("0xabcdef1234567890"), + StorageValue: common.HexToHash("0x9876543210abcdef"), } mockFetcher.DiffsToReturn = []utils.StorageDiff{anotherGethDiff} storageWatcher.KeccakAddressTransformers[keccakOfAnotherAddress] = anotherTransformer diff --git a/libraries/shared/watcher/storage_watcher.go b/libraries/shared/watcher/storage_watcher.go index 9d38cb91..d36da1a0 100644 --- a/libraries/shared/watcher/storage_watcher.go +++ b/libraries/shared/watcher/storage_watcher.go @@ -41,8 +41,8 @@ type StorageWatcher struct { StorageFetcher fetcher.IStorageFetcher Queue storage.IStorageQueue Transformers map[common.Address]transformer.StorageTransformer - KeccakAddressTransformers map[common.Address]transformer.StorageTransformer // keccak hash of an address => transformer - transformerGetter func(common.Address) (transformer.StorageTransformer, bool) + KeccakAddressTransformers map[common.Hash]transformer.StorageTransformer // keccak hash of an address => transformer + transformerGetter func(diff utils.StorageDiff) (transformer.StorageTransformer, bool) } func (storageWatcher StorageWatcher) AddTransformers(initializers []transformer.StorageTransformerInitializer) { @@ -67,12 +67,12 @@ func (storageWatcher StorageWatcher) Execute(diffsChan chan utils.StorageDiff, e } } -func (storageWatcher StorageWatcher) getTransformer(contractAddress common.Address) (transformer.StorageTransformer, bool) { - return storageWatcher.transformerGetter(contractAddress) +func (storageWatcher StorageWatcher) getTransformer(diff utils.StorageDiff) (transformer.StorageTransformer, bool) { + return storageWatcher.transformerGetter(diff) } func (storageWatcher StorageWatcher) processRow(diff utils.StorageDiff) { - storageTransformer, ok := storageWatcher.getTransformer(diff.Contract) + storageTransformer, ok := storageWatcher.getTransformer(diff) if !ok { logrus.Debug("ignoring a diff from an unwatched contract") return @@ -93,7 +93,7 @@ func (storageWatcher StorageWatcher) processQueue() { logrus.Warn(fmt.Sprintf("error getting queued storage: %s", fetchErr)) } for _, diff := range diffs { - storageTransformer, ok := storageWatcher.getTransformer(diff.Contract) + storageTransformer, ok := storageWatcher.getTransformer(diff) if !ok { // delete diff from queue if address no longer watched storageWatcher.deleteRow(diff.Id)