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,7 +62,7 @@ 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),
KeccakOfContractAddress: common.BytesToHash(account.Key),
BlockHash: stateDiff.BlockHash,
BlockHeight: int(stateDiff.BlockNumber.Int64()),
StorageKey: common.BytesToHash(storage.Key),

View File

@ -101,16 +101,14 @@ 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[:]),
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[:]),
KeccakOfContractAddress: common.BytesToHash(test_data.AnotherContractLeafKey[:]),
BlockHash: common.HexToHash("0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"),
BlockHeight: intHeight,
StorageKey: common.BytesToHash(test_data.StorageKey),

View File

@ -27,6 +27,7 @@ const ExpectedRowLength = 5
type StorageDiff struct {
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"`

View File

@ -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
}

View File

@ -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
}
}

View File

@ -56,20 +56,20 @@ 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,
KeccakOfContractAddress: keccakOfAddress,
BlockHash: common.HexToHash("0xfedcba9876543210"),
BlockHeight: 0,
StorageKey: common.HexToHash("0xabcdef1234567890"),
@ -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,11 +164,11 @@ 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,
KeccakOfContractAddress: keccakOfAnotherAddress,
BlockHash: common.HexToHash("0xfedcba9876543210"),
BlockHeight: 0,
StorageKey: common.HexToHash("0xabcdef1234567890"),

View File

@ -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)