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

Commit

Permalink
Update keccak of address to be a common.Hash instead of a common.Address
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Aug 6, 2019
1 parent 83558f8 commit bc794e4
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 54 deletions.
10 changes: 5 additions & 5 deletions libraries/shared/fetcher/geth_rpc_storage_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down
22 changes: 10 additions & 12 deletions libraries/shared/fetcher/geth_rpc_storage_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
13 changes: 7 additions & 6 deletions libraries/shared/storage/utils/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions libraries/shared/watcher/csv_storage_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
14 changes: 8 additions & 6 deletions libraries/shared/watcher/geth_storage_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
Expand All @@ -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
}
}
Expand Down
34 changes: 17 additions & 17 deletions libraries/shared/watcher/geth_storage_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
})

Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions libraries/shared/watcher/storage_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit bc794e4

Please sign in to comment.