Update keccak of address to be a common.Hash instead of a common.Address

This commit is contained in:
Elizabeth Engelman 2019-08-06 09:20:50 -05:00
parent a8a8fe4ac2
commit 66d695d93b
7 changed files with 56 additions and 54 deletions

View File

@ -62,11 +62,11 @@ func (fetcher GethRpcStorageFetcher) FetchStorageDiffs(out chan<- utils.StorageD
for _, storage := range account.Storage { for _, storage := range account.Storage {
logrus.Trace("adding storage diff to out channel") logrus.Trace("adding storage diff to out channel")
out <- utils.StorageDiff{ out <- utils.StorageDiff{
Contract: common.BytesToAddress(account.Key), KeccakOfContractAddress: common.BytesToHash(account.Key),
BlockHash: stateDiff.BlockHash, BlockHash: stateDiff.BlockHash,
BlockHeight: int(stateDiff.BlockNumber.Int64()), BlockHeight: int(stateDiff.BlockNumber.Int64()),
StorageKey: common.BytesToHash(storage.Key), StorageKey: common.BytesToHash(storage.Key),
StorageValue: common.BytesToHash(storage.Value), StorageValue: common.BytesToHash(storage.Value),
} }
} }
} }

View File

@ -101,20 +101,18 @@ var _ = Describe("Geth RPC Storage Fetcher", func() {
height := test_data.BlockNumber height := test_data.BlockNumber
intHeight := int(height.Int64()) intHeight := int(height.Int64())
expectedStorageDiff := utils.StorageDiff{ expectedStorageDiff := utils.StorageDiff{
//this is not the contract address, but the keccak 256 of the address KeccakOfContractAddress: common.BytesToHash(test_data.ContractLeafKey[:]),
Contract: common.BytesToAddress(test_data.ContractLeafKey[:]), BlockHash: common.HexToHash("0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"),
BlockHash: common.HexToHash("0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"), BlockHeight: intHeight,
BlockHeight: intHeight, StorageKey: common.BytesToHash(test_data.StorageKey),
StorageKey: common.BytesToHash(test_data.StorageKey), StorageValue: common.BytesToHash(test_data.StorageValue),
StorageValue: common.BytesToHash(test_data.StorageValue),
} }
anotherExpectedStorageDiff := utils.StorageDiff{ anotherExpectedStorageDiff := utils.StorageDiff{
//this is not the contract address, but the keccak 256 of the address KeccakOfContractAddress: common.BytesToHash(test_data.AnotherContractLeafKey[:]),
Contract: common.BytesToAddress(test_data.AnotherContractLeafKey[:]), BlockHash: common.HexToHash("0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"),
BlockHash: common.HexToHash("0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"), BlockHeight: intHeight,
BlockHeight: intHeight, StorageKey: common.BytesToHash(test_data.StorageKey),
StorageKey: common.BytesToHash(test_data.StorageKey), StorageValue: common.BytesToHash(test_data.StorageValue),
StorageValue: common.BytesToHash(test_data.StorageValue),
} }
Expect(<-storagediffChan).To(Equal(expectedStorageDiff)) Expect(<-storagediffChan).To(Equal(expectedStorageDiff))
Expect(<-storagediffChan).To(Equal(anotherExpectedStorageDiff)) Expect(<-storagediffChan).To(Equal(anotherExpectedStorageDiff))

View File

@ -25,12 +25,13 @@ import (
const ExpectedRowLength = 5 const ExpectedRowLength = 5
type StorageDiff struct { type StorageDiff struct {
Id int Id int
Contract common.Address Contract common.Address
BlockHash common.Hash `db:"block_hash"` KeccakOfContractAddress common.Hash
BlockHeight int `db:"block_height"` BlockHash common.Hash `db:"block_hash"`
StorageKey common.Hash `db:"storage_key"` BlockHeight int `db:"block_height"`
StorageValue common.Hash `db:"storage_value"` StorageKey common.Hash `db:"storage_key"`
StorageValue common.Hash `db:"storage_value"`
} }
func FromStrings(csvRow []string) (StorageDiff, error) { func FromStrings(csvRow []string) (StorageDiff, error) {

View File

@ -18,6 +18,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/vulcanize/vulcanizedb/libraries/shared/fetcher" "github.com/vulcanize/vulcanizedb/libraries/shared/fetcher"
"github.com/vulcanize/vulcanizedb/libraries/shared/storage" "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/libraries/shared/transformer"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
) )
@ -39,7 +40,7 @@ func NewCsvStorageWatcher(fetcher fetcher.IStorageFetcher, db *postgres.DB) CsvS
return CsvStorageWatcher{StorageWatcher: storageWatcher} return CsvStorageWatcher{StorageWatcher: storageWatcher}
} }
func (storageWatcher StorageWatcher) getCsvTransformer(contractAddress common.Address) (transformer.StorageTransformer, bool) { func (storageWatcher StorageWatcher) getCsvTransformer(diff utils.StorageDiff) (transformer.StorageTransformer, bool) {
storageTransformer, ok := storageWatcher.Transformers[contractAddress] storageTransformer, ok := storageWatcher.Transformers[diff.Contract]
return storageTransformer, ok return storageTransformer, ok
} }

View File

@ -19,6 +19,7 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/vulcanize/vulcanizedb/libraries/shared/fetcher" "github.com/vulcanize/vulcanizedb/libraries/shared/fetcher"
"github.com/vulcanize/vulcanizedb/libraries/shared/storage" "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/libraries/shared/transformer"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
) )
@ -30,7 +31,7 @@ type GethStorageWatcher struct {
func NewGethStorageWatcher(fetcher fetcher.IStorageFetcher, db *postgres.DB) GethStorageWatcher { func NewGethStorageWatcher(fetcher fetcher.IStorageFetcher, db *postgres.DB) GethStorageWatcher {
queue := storage.NewStorageQueue(db) queue := storage.NewStorageQueue(db)
transformers := make(map[common.Address]transformer.StorageTransformer) transformers := make(map[common.Address]transformer.StorageTransformer)
keccakAddressTransformers := make(map[common.Address]transformer.StorageTransformer) keccakAddressTransformers := make(map[common.Hash]transformer.StorageTransformer)
storageWatcher := StorageWatcher{ storageWatcher := StorageWatcher{
db: db, db: db,
StorageFetcher: fetcher, StorageFetcher: fetcher,
@ -42,15 +43,16 @@ func NewGethStorageWatcher(fetcher fetcher.IStorageFetcher, db *postgres.DB) Get
return GethStorageWatcher{StorageWatcher: storageWatcher} return GethStorageWatcher{StorageWatcher: storageWatcher}
} }
func (storageWatcher StorageWatcher) getTransformerForGethWatcher(contractAddress common.Address) (transformer.StorageTransformer, bool) { func (storageWatcher StorageWatcher) getTransformerForGethWatcher(diff utils.StorageDiff) (transformer.StorageTransformer, bool) {
storageTransformer, ok := storageWatcher.KeccakAddressTransformers[contractAddress] keccakOfAddress := diff.KeccakOfContractAddress
storageTransformer, ok := storageWatcher.KeccakAddressTransformers[keccakOfAddress]
if ok { if ok {
return storageTransformer, ok return storageTransformer, ok
} else { } else {
for address, transformer := range storageWatcher.Transformers { for address, transformer := range storageWatcher.Transformers {
keccakOfTransformerAddress := common.BytesToAddress(crypto.Keccak256(address[:])) keccakOfTransformerAddress := common.BytesToHash(crypto.Keccak256(address[:]))
if keccakOfTransformerAddress == contractAddress { if keccakOfTransformerAddress == keccakOfAddress {
storageWatcher.KeccakAddressTransformers[contractAddress] = transformer storageWatcher.KeccakAddressTransformers[keccakOfAddress] = transformer
return transformer, true return transformer, true
} }
} }

View File

@ -56,24 +56,24 @@ var _ = Describe("Geth Storage Watcher", func() {
diffs chan utils.StorageDiff diffs chan utils.StorageDiff
storageWatcher watcher.GethStorageWatcher storageWatcher watcher.GethStorageWatcher
address common.Address address common.Address
keccakOfAddress common.Address keccakOfAddress common.Hash
) )
BeforeEach(func() { BeforeEach(func() {
errs = make(chan error) errs = make(chan error)
diffs = make(chan utils.StorageDiff) diffs = make(chan utils.StorageDiff)
address = common.HexToAddress("0x0123456789abcdef") address = common.HexToAddress("0x0123456789abcdef")
keccakOfAddress = common.BytesToAddress(crypto.Keccak256(address[:])) keccakOfAddress = common.BytesToHash(crypto.Keccak256(address[:]))
mockFetcher = mocks.NewMockStorageFetcher() mockFetcher = mocks.NewMockStorageFetcher()
mockQueue = &mocks.MockStorageQueue{} mockQueue = &mocks.MockStorageQueue{}
mockTransformer = &mocks.MockStorageTransformer{Address: address} mockTransformer = &mocks.MockStorageTransformer{Address: address}
gethDiff = utils.StorageDiff{ gethDiff = utils.StorageDiff{
Id: 1338, Id: 1338,
Contract: keccakOfAddress, KeccakOfContractAddress: keccakOfAddress,
BlockHash: common.HexToHash("0xfedcba9876543210"), BlockHash: common.HexToHash("0xfedcba9876543210"),
BlockHeight: 0, BlockHeight: 0,
StorageKey: common.HexToHash("0xabcdef1234567890"), StorageKey: common.HexToHash("0xabcdef1234567890"),
StorageValue: common.HexToHash("0x9876543210abcdef"), 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) { It("keeps track transformers by the keccak256 hash of their contract address ", func(done Done) {
go storageWatcher.Execute(diffs, errs, time.Hour) go storageWatcher.Execute(diffs, errs, time.Hour)
m := make(map[common.Address]transformer.StorageTransformer) m := make(map[common.Hash]transformer.StorageTransformer)
m[keccakOfAddress] = mockTransformer m[keccakOfAddress] = mockTransformer
Eventually(func() map[common.Address]transformer.StorageTransformer { Eventually(func() map[common.Hash]transformer.StorageTransformer {
return storageWatcher.KeccakAddressTransformers return storageWatcher.KeccakAddressTransformers
}).Should(Equal(m)) }).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) { It("gets the transformer from the known keccak address map first", func(done Done) {
anotherAddress := common.HexToAddress("0xafakeaddress") anotherAddress := common.HexToAddress("0xafakeaddress")
anotherTransformer := &mocks.MockStorageTransformer{Address: anotherAddress} anotherTransformer := &mocks.MockStorageTransformer{Address: anotherAddress}
keccakOfAnotherAddress := common.BytesToAddress(crypto.Keccak256(anotherAddress[:])) keccakOfAnotherAddress := common.BytesToHash(crypto.Keccak256(anotherAddress[:]))
anotherGethDiff := utils.StorageDiff{ anotherGethDiff := utils.StorageDiff{
Id: 1338, Id: 1338,
Contract: keccakOfAnotherAddress, KeccakOfContractAddress: keccakOfAnotherAddress,
BlockHash: common.HexToHash("0xfedcba9876543210"), BlockHash: common.HexToHash("0xfedcba9876543210"),
BlockHeight: 0, BlockHeight: 0,
StorageKey: common.HexToHash("0xabcdef1234567890"), StorageKey: common.HexToHash("0xabcdef1234567890"),
StorageValue: common.HexToHash("0x9876543210abcdef"), StorageValue: common.HexToHash("0x9876543210abcdef"),
} }
mockFetcher.DiffsToReturn = []utils.StorageDiff{anotherGethDiff} mockFetcher.DiffsToReturn = []utils.StorageDiff{anotherGethDiff}
storageWatcher.KeccakAddressTransformers[keccakOfAnotherAddress] = anotherTransformer storageWatcher.KeccakAddressTransformers[keccakOfAnotherAddress] = anotherTransformer

View File

@ -41,8 +41,8 @@ type StorageWatcher struct {
StorageFetcher fetcher.IStorageFetcher StorageFetcher fetcher.IStorageFetcher
Queue storage.IStorageQueue Queue storage.IStorageQueue
Transformers map[common.Address]transformer.StorageTransformer Transformers map[common.Address]transformer.StorageTransformer
KeccakAddressTransformers map[common.Address]transformer.StorageTransformer // keccak hash of an address => transformer KeccakAddressTransformers map[common.Hash]transformer.StorageTransformer // keccak hash of an address => transformer
transformerGetter func(common.Address) (transformer.StorageTransformer, bool) transformerGetter func(diff utils.StorageDiff) (transformer.StorageTransformer, bool)
} }
func (storageWatcher StorageWatcher) AddTransformers(initializers []transformer.StorageTransformerInitializer) { 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) { func (storageWatcher StorageWatcher) getTransformer(diff utils.StorageDiff) (transformer.StorageTransformer, bool) {
return storageWatcher.transformerGetter(contractAddress) return storageWatcher.transformerGetter(diff)
} }
func (storageWatcher StorageWatcher) processRow(diff utils.StorageDiff) { func (storageWatcher StorageWatcher) processRow(diff utils.StorageDiff) {
storageTransformer, ok := storageWatcher.getTransformer(diff.Contract) storageTransformer, ok := storageWatcher.getTransformer(diff)
if !ok { if !ok {
logrus.Debug("ignoring a diff from an unwatched contract") logrus.Debug("ignoring a diff from an unwatched contract")
return return
@ -93,7 +93,7 @@ func (storageWatcher StorageWatcher) processQueue() {
logrus.Warn(fmt.Sprintf("error getting queued storage: %s", fetchErr)) logrus.Warn(fmt.Sprintf("error getting queued storage: %s", fetchErr))
} }
for _, diff := range diffs { for _, diff := range diffs {
storageTransformer, ok := storageWatcher.getTransformer(diff.Contract) storageTransformer, ok := storageWatcher.getTransformer(diff)
if !ok { if !ok {
// delete diff from queue if address no longer watched // delete diff from queue if address no longer watched
storageWatcher.deleteRow(diff.Id) storageWatcher.deleteRow(diff.Id)