92 lines
2.7 KiB
Go
92 lines
2.7 KiB
Go
// Copyright © 2023 Vulcanize, Inc
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
|
|
statediff "github.com/cerc-io/plugeth-statediff"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
|
|
pkg "github.com/vulcanize/eth-statediff-service/pkg"
|
|
)
|
|
|
|
// serveCmd represents the serve command
|
|
var runCmd = &cobra.Command{
|
|
Use: "run",
|
|
Short: "Produce diffs for a specific block range",
|
|
Long: `Usage
|
|
|
|
./eth-statediff-service run --config={path to toml config file}`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
subCommand = cmd.CalledAs()
|
|
logWithCommand = *logrus.WithField("SubCommand", subCommand)
|
|
runRanges()
|
|
},
|
|
}
|
|
|
|
func runRanges() {
|
|
service := createStateDiffService()
|
|
|
|
// start service and servers
|
|
var wg sync.WaitGroup
|
|
if err := service.Loop(&wg); err != nil {
|
|
logWithCommand.Fatalf("unable to start statediff service: %v", err)
|
|
}
|
|
ranges := getConfiguredRanges()
|
|
service.Run(ranges)
|
|
|
|
// clean shutdown
|
|
shutdown := make(chan os.Signal)
|
|
signal.Notify(shutdown, os.Interrupt)
|
|
<-shutdown
|
|
logWithCommand.Info("Received interrupt signal, shutting down")
|
|
service.Stop()
|
|
wg.Wait()
|
|
}
|
|
|
|
func getConfiguredRanges() []pkg.RangeRequest {
|
|
params := statediff.Params{
|
|
IncludeBlock: viper.GetBool("run.params.includeBlock"),
|
|
IncludeReceipts: viper.GetBool("run.params.includeReceipts"),
|
|
IncludeTD: viper.GetBool("run.params.includeTD"),
|
|
IncludeCode: viper.GetBool("run.params.includeCode"),
|
|
}
|
|
var addrStrs []string
|
|
viper.UnmarshalKey("run.params.watchedAddresses", &addrStrs)
|
|
addrs := make([]common.Address, len(addrStrs))
|
|
for i, addrStr := range addrStrs {
|
|
addrs[i] = common.HexToAddress(addrStr)
|
|
}
|
|
params.WatchedAddresses = addrs
|
|
var rawRanges []blockRange
|
|
viper.UnmarshalKey("run.ranges", &rawRanges)
|
|
blockRanges := make([]pkg.RangeRequest, len(rawRanges))
|
|
for i, rawRange := range rawRanges {
|
|
blockRanges[i] = pkg.RangeRequest{
|
|
Start: rawRange[0],
|
|
Stop: rawRange[1],
|
|
Params: params,
|
|
}
|
|
}
|
|
return blockRanges
|
|
}
|