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

Commit d06dddb

Browse files
Rob Mulholandelizabethengelman
authored andcommitted
Always hash storage diff contract addresses
- Enables syncing Geth and Parity diffs with same transformer lookup - Maybe worth always hashing the storage key so we don't need a hashed and not-hashed version in the key lookups?
1 parent f574407 commit d06dddb

19 files changed

+184
-523
lines changed

cmd/composeAndExecute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,15 @@ func composeAndExecute() {
188188
stateDiffStreamer := streamer.NewStateDiffStreamer(rpcClient)
189189
payloadChan := make(chan statediff.Payload)
190190
storageFetcher := fetcher.NewGethRpcStorageFetcher(&stateDiffStreamer, payloadChan)
191-
sw := watcher.NewGethStorageWatcher(storageFetcher, &db)
191+
sw := watcher.NewStorageWatcher(storageFetcher, &db)
192192
sw.AddTransformers(ethStorageInitializers)
193193
wg.Add(1)
194194
go watchEthStorage(&sw, &wg)
195195
default:
196196
log.Debug("fetching storage diffs from csv")
197197
tailer := fs.FileTailer{Path: storageDiffsPath}
198198
storageFetcher := fetcher.NewCsvTailStorageFetcher(tailer)
199-
sw := watcher.NewCsvStorageWatcher(storageFetcher, &db)
199+
sw := watcher.NewStorageWatcher(storageFetcher, &db)
200200
sw.AddTransformers(ethStorageInitializers)
201201
wg.Add(1)
202202
go watchEthStorage(&sw, &wg)

cmd/execute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ func execute() {
132132
stateDiffStreamer := streamer.NewStateDiffStreamer(rpcClient)
133133
payloadChan := make(chan statediff.Payload)
134134
storageFetcher := fetcher.NewGethRpcStorageFetcher(&stateDiffStreamer, payloadChan)
135-
sw := watcher.NewGethStorageWatcher(storageFetcher, &db)
135+
sw := watcher.NewStorageWatcher(storageFetcher, &db)
136136
sw.AddTransformers(ethStorageInitializers)
137137
wg.Add(1)
138138
go watchEthStorage(&sw, &wg)
139139
default:
140140
log.Debug("fetching storage diffs from csv")
141141
tailer := fs.FileTailer{Path: storageDiffsPath}
142142
storageFetcher := fetcher.NewCsvTailStorageFetcher(tailer)
143-
sw := watcher.NewCsvStorageWatcher(storageFetcher, &db)
143+
sw := watcher.NewStorageWatcher(storageFetcher, &db)
144144
sw.AddTransformers(ethStorageInitializers)
145145
wg.Add(1)
146146
go watchEthStorage(&sw, &wg)

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ require (
1414
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
1515
github.com/go-sql-driver/mysql v1.4.1 // indirect
1616
github.com/golang/protobuf v1.3.2 // indirect
17-
github.com/google/uuid v1.0.0 // indirect
1817
github.com/howeyc/fsnotify v0.9.0 // indirect
1918
github.com/hpcloud/tail v1.0.0
2019
github.com/huin/goupnp v1.0.0 // indirect

libraries/shared/factories/storage/transformer.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ package storage
1818

1919
import (
2020
"github.com/ethereum/go-ethereum/common"
21-
21+
"github.com/vulcanize/vulcanizedb/libraries/shared/repository"
2222
"github.com/vulcanize/vulcanizedb/libraries/shared/storage"
2323
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
2424
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
2525
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
2626
)
2727

2828
type Transformer struct {
29-
Address common.Address
30-
Mappings storage.Mappings
31-
Repository Repository
29+
HashedAddress common.Hash
30+
Mappings storage.Mappings
31+
Repository repository.StorageRepository
3232
}
3333

3434
func (transformer Transformer) NewTransformer(db *postgres.DB) transformer.StorageTransformer {
@@ -37,8 +37,8 @@ func (transformer Transformer) NewTransformer(db *postgres.DB) transformer.Stora
3737
return transformer
3838
}
3939

40-
func (transformer Transformer) ContractAddress() common.Address {
41-
return transformer.Address
40+
func (transformer Transformer) KeccakContractAddress() common.Hash {
41+
return transformer.HashedAddress
4242
}
4343

4444
func (transformer Transformer) Execute(diff utils.StorageDiff) error {

libraries/shared/factories/storage/transformer_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ package storage_test
1818

1919
import (
2020
"github.com/ethereum/go-ethereum/common"
21+
"github.com/ethereum/go-ethereum/crypto"
2122
. "github.com/onsi/ginkgo"
2223
. "github.com/onsi/gomega"
23-
2424
"github.com/vulcanize/vulcanizedb/libraries/shared/factories/storage"
2525
"github.com/vulcanize/vulcanizedb/libraries/shared/mocks"
2626
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
@@ -38,17 +38,17 @@ var _ = Describe("Storage transformer", func() {
3838
mappings = &mocks.MockMappings{}
3939
repository = &mocks.MockStorageRepository{}
4040
t = storage.Transformer{
41-
Address: common.Address{},
42-
Mappings: mappings,
43-
Repository: repository,
41+
HashedAddress: common.Hash{},
42+
Mappings: mappings,
43+
Repository: repository,
4444
}
4545
})
4646

4747
It("returns the contract address being watched", func() {
48-
fakeAddress := common.HexToAddress("0x12345")
49-
t.Address = fakeAddress
48+
fakeAddress := common.BytesToHash(crypto.Keccak256(common.FromHex("0x12345")))
49+
t.HashedAddress = fakeAddress
5050

51-
Expect(t.ContractAddress()).To(Equal(fakeAddress))
51+
Expect(t.KeccakContractAddress()).To(Equal(fakeAddress))
5252
})
5353

5454
It("looks up metadata for storage key", func() {
@@ -73,11 +73,11 @@ var _ = Describe("Storage transformer", func() {
7373
fakeBlockNumber := 123
7474
fakeBlockHash := "0x67890"
7575
fakeRow := utils.StorageDiff{
76-
Contract: common.Address{},
77-
BlockHash: common.HexToHash(fakeBlockHash),
78-
BlockHeight: fakeBlockNumber,
79-
StorageKey: common.Hash{},
80-
StorageValue: rawValue.Hash(),
76+
KeccakOfContractAddress: common.Hash{},
77+
BlockHash: common.HexToHash(fakeBlockHash),
78+
BlockHeight: fakeBlockNumber,
79+
StorageKey: common.Hash{},
80+
StorageValue: rawValue.Hash(),
8181
}
8282

8383
err := t.Execute(fakeRow)
@@ -121,11 +121,11 @@ var _ = Describe("Storage transformer", func() {
121121
It("passes the decoded data items to the repository", func() {
122122
mappings.Metadata = fakeMetadata
123123
fakeRow := utils.StorageDiff{
124-
Contract: common.Address{},
125-
BlockHash: common.HexToHash(fakeBlockHash),
126-
BlockHeight: fakeBlockNumber,
127-
StorageKey: common.Hash{},
128-
StorageValue: rawValue.Hash(),
124+
KeccakOfContractAddress: common.Hash{},
125+
BlockHash: common.HexToHash(fakeBlockHash),
126+
BlockHeight: fakeBlockNumber,
127+
StorageKey: common.Hash{},
128+
StorageValue: rawValue.Hash(),
129129
}
130130

131131
err := t.Execute(fakeRow)

libraries/shared/fetcher/csv_tail_storage_fetcher.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
package fetcher
1818

1919
import (
20-
"strings"
21-
22-
log "github.com/sirupsen/logrus"
23-
20+
"github.com/sirupsen/logrus"
2421
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
2522
"github.com/vulcanize/vulcanizedb/pkg/fs"
23+
"strings"
2624
)
2725

2826
type CsvTailStorageFetcher struct {
@@ -38,9 +36,9 @@ func (storageFetcher CsvTailStorageFetcher) FetchStorageDiffs(out chan<- utils.S
3836
if tailErr != nil {
3937
errs <- tailErr
4038
}
41-
log.Debug("fetching storage diffs...")
39+
logrus.Debug("fetching storage diffs...")
4240
for line := range t.Lines {
43-
diff, parseErr := utils.FromStrings(strings.Split(line.Text, ","))
41+
diff, parseErr := utils.FromParityCsvRow(strings.Split(line.Text, ","))
4442
if parseErr != nil {
4543
errs <- parseErr
4644
} else {

libraries/shared/fetcher/csv_tail_storage_fetcher_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@ package fetcher_test
1818

1919
import (
2020
"fmt"
21-
"strings"
22-
"time"
23-
2421
"github.com/ethereum/go-ethereum/common"
2522
"github.com/hpcloud/tail"
2623
. "github.com/onsi/ginkgo"
2724
. "github.com/onsi/gomega"
28-
2925
"github.com/vulcanize/vulcanizedb/libraries/shared/fetcher"
3026
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
3127
"github.com/vulcanize/vulcanizedb/pkg/fakes"
28+
"strings"
29+
"time"
3230
)
3331

3432
var _ = Describe("Csv Tail Storage Fetcher", func() {
@@ -61,7 +59,7 @@ var _ = Describe("Csv Tail Storage Fetcher", func() {
6159
go storageFetcher.FetchStorageDiffs(diffsChannel, errorsChannel)
6260
mockTailer.Lines <- line
6361

64-
expectedRow, err := utils.FromStrings(strings.Split(line.Text, ","))
62+
expectedRow, err := utils.FromParityCsvRow(strings.Split(line.Text, ","))
6563
Expect(err).NotTo(HaveOccurred())
6664
Expect(<-diffsChannel).To(Equal(expectedRow))
6765
close(done)

libraries/shared/fetcher/geth_rpc_storage_fetcher.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package fetcher
1616

1717
import (
1818
"fmt"
19-
"github.com/ethereum/go-ethereum/common"
2019
"github.com/ethereum/go-ethereum/rlp"
2120
"github.com/ethereum/go-ethereum/statediff"
2221
"github.com/sirupsen/logrus"
@@ -61,13 +60,7 @@ func (fetcher GethRpcStorageFetcher) FetchStorageDiffs(out chan<- utils.StorageD
6160
logrus.Trace(fmt.Sprintf("iterating through %d Storage values on account", len(account.Storage)))
6261
for _, storage := range account.Storage {
6362
logrus.Trace("adding storage diff to out channel")
64-
out <- utils.StorageDiff{
65-
KeccakOfContractAddress: common.BytesToHash(account.Key),
66-
BlockHash: stateDiff.BlockHash,
67-
BlockHeight: int(stateDiff.BlockNumber.Int64()),
68-
StorageKey: common.BytesToHash(storage.Key),
69-
StorageValue: common.BytesToHash(storage.Value),
70-
}
63+
out <- utils.FromGethStateDiff(account, stateDiff, storage)
7164
}
7265
}
7366
}

libraries/shared/mocks/storage_transformer.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,24 @@ package mocks
1818

1919
import (
2020
"github.com/ethereum/go-ethereum/common"
21-
2221
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
2322
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
2423
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
2524
)
2625

2726
type MockStorageTransformer struct {
28-
Address common.Address
29-
ExecuteErr error
30-
PassedDiff utils.StorageDiff
27+
KeccakOfAddress common.Hash
28+
ExecuteErr error
29+
PassedDiff utils.StorageDiff
3130
}
3231

3332
func (transformer *MockStorageTransformer) Execute(diff utils.StorageDiff) error {
3433
transformer.PassedDiff = diff
3534
return transformer.ExecuteErr
3635
}
3736

38-
func (transformer *MockStorageTransformer) ContractAddress() common.Address {
39-
return transformer.Address
37+
func (transformer *MockStorageTransformer) KeccakContractAddress() common.Hash {
38+
return transformer.KeccakOfAddress
4039
}
4140

4241
func (transformer *MockStorageTransformer) FakeTransformerInitializer(db *postgres.DB) transformer.StorageTransformer {

libraries/shared/storage/storage_queue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewStorageQueue(db *postgres.DB) StorageQueue {
3838
func (queue StorageQueue) Add(diff utils.StorageDiff) error {
3939
_, err := queue.db.Exec(`INSERT INTO public.queued_storage (contract,
4040
block_hash, block_height, storage_key, storage_value) VALUES
41-
($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING`, diff.Contract.Bytes(), diff.BlockHash.Bytes(),
41+
($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING`, diff.KeccakOfContractAddress.Bytes(), diff.BlockHash.Bytes(),
4242
diff.BlockHeight, diff.StorageKey.Bytes(), diff.StorageValue.Bytes())
4343
return err
4444
}

0 commit comments

Comments
 (0)