2019-07-08 20:34:06 +00:00
|
|
|
// Copyright 2019 Vulcanize
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package fetcher
|
|
|
|
|
|
|
|
import (
|
2019-07-29 19:15:33 +00:00
|
|
|
"fmt"
|
2019-07-08 20:34:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
2019-07-29 19:15:33 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-07-08 20:34:06 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/streamer"
|
|
|
|
)
|
|
|
|
|
2019-07-08 22:52:06 +00:00
|
|
|
type GethRpcStorageFetcher struct {
|
2019-07-08 20:34:06 +00:00
|
|
|
statediffPayloadChan chan statediff.Payload
|
2019-07-08 22:52:06 +00:00
|
|
|
streamer streamer.Streamer
|
2019-07-08 20:34:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 22:52:06 +00:00
|
|
|
func NewGethRpcStorageFetcher(streamer streamer.Streamer, statediffPayloadChan chan statediff.Payload) GethRpcStorageFetcher {
|
2019-07-08 20:34:06 +00:00
|
|
|
return GethRpcStorageFetcher{
|
|
|
|
statediffPayloadChan: statediffPayloadChan,
|
2019-07-08 22:52:06 +00:00
|
|
|
streamer: streamer,
|
2019-07-08 20:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 13:26:12 +00:00
|
|
|
func (fetcher GethRpcStorageFetcher) FetchStorageDiffs(out chan<- utils.StorageDiff, errs chan<- error) {
|
2019-07-08 20:34:06 +00:00
|
|
|
ethStatediffPayloadChan := fetcher.statediffPayloadChan
|
2019-07-29 19:15:33 +00:00
|
|
|
clientSubscription, clientSubErr := fetcher.streamer.Stream(ethStatediffPayloadChan)
|
|
|
|
if clientSubErr != nil {
|
|
|
|
errs <- clientSubErr
|
2019-07-30 20:27:38 +00:00
|
|
|
panic(fmt.Sprintf("Error creating a geth client subscription: %v", clientSubErr))
|
2019-07-08 20:34:06 +00:00
|
|
|
}
|
2019-07-29 19:15:33 +00:00
|
|
|
logrus.Info("Successfully created a geth client subscription: ", clientSubscription)
|
2019-07-08 20:34:06 +00:00
|
|
|
|
2019-07-22 21:48:18 +00:00
|
|
|
for {
|
|
|
|
diff := <-ethStatediffPayloadChan
|
2019-07-29 19:15:33 +00:00
|
|
|
logrus.Trace("received a statediff")
|
2019-07-08 20:34:06 +00:00
|
|
|
stateDiff := new(statediff.StateDiff)
|
2019-07-29 19:15:33 +00:00
|
|
|
decodeErr := rlp.DecodeBytes(diff.StateDiffRlp, stateDiff)
|
|
|
|
if decodeErr != nil {
|
|
|
|
logrus.Warn("Error decoding state diff into RLP: ", decodeErr)
|
|
|
|
errs <- decodeErr
|
2019-07-08 20:34:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 19:15:33 +00:00
|
|
|
accounts := getAccountDiffs(*stateDiff)
|
|
|
|
logrus.Trace(fmt.Sprintf("iterating through %d accounts on stateDiff for block %d", len(accounts), stateDiff.BlockNumber))
|
2019-07-08 20:34:06 +00:00
|
|
|
for _, account := range accounts {
|
2019-07-29 19:15:33 +00:00
|
|
|
logrus.Trace(fmt.Sprintf("iterating through %d Storage values on account", len(account.Storage)))
|
2019-07-08 20:34:06 +00:00
|
|
|
for _, storage := range account.Storage {
|
2019-07-29 19:15:33 +00:00
|
|
|
logrus.Trace("adding storage diff to out channel")
|
2019-07-29 19:28:20 +00:00
|
|
|
out <- utils.StorageDiff{
|
2019-08-06 14:20:50 +00:00
|
|
|
KeccakOfContractAddress: common.BytesToHash(account.Key),
|
|
|
|
BlockHash: stateDiff.BlockHash,
|
|
|
|
BlockHeight: int(stateDiff.BlockNumber.Int64()),
|
|
|
|
StorageKey: common.BytesToHash(storage.Key),
|
|
|
|
StorageValue: common.BytesToHash(storage.Value),
|
2019-07-08 20:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAccountDiffs(stateDiff statediff.StateDiff) []statediff.AccountDiff {
|
2019-07-08 22:52:06 +00:00
|
|
|
accounts := append(stateDiff.CreatedAccounts, stateDiff.UpdatedAccounts...)
|
2019-07-08 20:34:06 +00:00
|
|
|
return append(accounts, stateDiff.DeletedAccounts...)
|
|
|
|
}
|