2020-11-10 09:07:59 +00:00
|
|
|
// Copyright © 2019 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 (
|
2021-10-19 10:30:05 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2020-11-10 09:07:59 +00:00
|
|
|
|
2020-11-17 01:37:36 +00:00
|
|
|
gethsd "github.com/ethereum/go-ethereum/statediff"
|
2020-11-10 09:07:59 +00:00
|
|
|
ind "github.com/ethereum/go-ethereum/statediff/indexer"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/postgres"
|
2021-10-19 21:40:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2021-10-19 10:30:05 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2021-10-14 06:47:38 +00:00
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
sd "github.com/vulcanize/eth-statediff-service/pkg"
|
|
|
|
)
|
|
|
|
|
|
|
|
var writeCmd = &cobra.Command{
|
|
|
|
Use: "write",
|
|
|
|
Short: "Write statediffs directly to DB for preconfigured block ranges",
|
|
|
|
Long: `Usage
|
|
|
|
|
|
|
|
./eth-statediff-service write --config={path to toml config file}`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
subCommand = cmd.CalledAs()
|
|
|
|
logWithCommand = *logrus.WithField("SubCommand", subCommand)
|
2021-10-19 21:40:56 +00:00
|
|
|
write()
|
2020-11-10 09:07:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-19 10:30:05 +00:00
|
|
|
type blockRange [2]uint64
|
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(writeCmd)
|
2021-10-20 21:25:52 +00:00
|
|
|
writeCmd.PersistentFlags().String("write-api", "", "starts a server which handles write request through endpoints")
|
|
|
|
viper.BindPFlag("write.serve", writeCmd.PersistentFlags().Lookup("write-api"))
|
2020-11-10 09:07:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 21:40:56 +00:00
|
|
|
func write() {
|
2020-11-10 09:07:59 +00:00
|
|
|
logWithCommand.Info("Starting statediff writer")
|
|
|
|
// load params
|
2021-10-20 21:25:52 +00:00
|
|
|
viper.BindEnv("write.serve", WRITE_SERVER)
|
|
|
|
addr := viper.GetString("write.serve")
|
2020-11-10 09:07:59 +00:00
|
|
|
path := viper.GetString("leveldb.path")
|
|
|
|
ancientPath := viper.GetString("leveldb.ancient")
|
|
|
|
if path == "" || ancientPath == "" {
|
|
|
|
logWithCommand.Fatal("require a valid eth leveldb primary datastore path and ancient datastore path")
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeInfo := GetEthNodeInfo()
|
|
|
|
config, err := chainConfig(nodeInfo.ChainID)
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create leveldb reader
|
2020-11-26 10:31:29 +00:00
|
|
|
logWithCommand.Info("Creating leveldb reader")
|
2021-10-19 21:40:56 +00:00
|
|
|
conf := sd.ReaderConfig{
|
|
|
|
TrieConfig: &trie.Config{
|
|
|
|
Cache: viper.GetInt("cache.trie"),
|
|
|
|
Journal: "",
|
|
|
|
Preimages: false,
|
|
|
|
},
|
|
|
|
ChainConfig: config,
|
|
|
|
Path: path,
|
|
|
|
AncientPath: ancientPath,
|
|
|
|
DBCacheSize: viper.GetInt("cache.database"),
|
|
|
|
}
|
|
|
|
lvlDBReader, err := sd.NewLvlDBReader(conf)
|
2020-11-10 09:07:59 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create statediff service
|
2020-11-26 10:31:29 +00:00
|
|
|
logWithCommand.Info("Creating statediff service")
|
2020-11-16 09:49:59 +00:00
|
|
|
db, err := postgres.NewDB(postgres.DbConnectionString(GetDBParams()), GetDBConfig(), nodeInfo)
|
2020-11-10 09:07:59 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
2021-10-14 06:47:38 +00:00
|
|
|
|
|
|
|
indexer, err := ind.NewStateDiffIndexer(config, db)
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:07:59 +00:00
|
|
|
statediffService, err := sd.NewStateDiffService(lvlDBReader, indexer, viper.GetUint("statediff.workers"))
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read all defined block ranges, write statediffs to database
|
2021-10-19 10:30:05 +00:00
|
|
|
var blockRanges []blockRange
|
2020-11-17 01:37:36 +00:00
|
|
|
diffParams := gethsd.Params{ // todo: configurable?
|
2020-11-10 09:07:59 +00:00
|
|
|
IntermediateStateNodes: true,
|
|
|
|
IntermediateStorageNodes: true,
|
|
|
|
IncludeBlock: true,
|
|
|
|
IncludeReceipts: true,
|
|
|
|
IncludeTD: true,
|
|
|
|
IncludeCode: true,
|
|
|
|
}
|
|
|
|
viper.UnmarshalKey("write.ranges", &blockRanges)
|
2020-11-26 10:31:29 +00:00
|
|
|
viper.UnmarshalKey("write.params", &diffParams)
|
2020-11-10 09:07:59 +00:00
|
|
|
|
2021-10-19 10:30:05 +00:00
|
|
|
blockRangesCh := make(chan blockRange, 100)
|
|
|
|
go func() {
|
|
|
|
for _, r := range blockRanges {
|
|
|
|
blockRangesCh <- r
|
|
|
|
}
|
|
|
|
if addr == "" {
|
|
|
|
close(blockRangesCh)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
startServer(addr, blockRangesCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
processRanges(statediffService, diffParams, blockRangesCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func startServer(addr string, blockRangesCh chan<- blockRange) {
|
|
|
|
handler := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
start, err := strconv.Atoi(req.URL.Query().Get("start"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("failed to parse start value: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
end, err := strconv.Atoi(req.URL.Query().Get("end"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("failed to parse end value: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
2020-11-10 09:07:59 +00:00
|
|
|
}
|
2021-10-19 10:30:05 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case blockRangesCh <- blockRange{uint64(start), uint64(end)}:
|
|
|
|
case <-time.After(time.Millisecond * 200):
|
|
|
|
http.Error(w, "server is busy", http.StatusInternalServerError)
|
|
|
|
return
|
2020-11-10 09:07:59 +00:00
|
|
|
}
|
2021-10-19 10:30:05 +00:00
|
|
|
|
|
|
|
fmt.Fprintf(w, "added block range to the queue\n")
|
2020-11-10 09:07:59 +00:00
|
|
|
}
|
2021-10-19 10:30:05 +00:00
|
|
|
|
|
|
|
http.HandleFunc("/writeDiff", handler)
|
|
|
|
logrus.Fatal(http.ListenAndServe(addr, nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
type diffService interface {
|
|
|
|
WriteStateDiffAt(blockNumber uint64, params gethsd.Params) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func processRanges(sds diffService, param gethsd.Params, blockRangesCh chan blockRange) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for rng := range blockRangesCh {
|
|
|
|
if rng[1] < rng[0] {
|
|
|
|
logWithCommand.Fatal("range ending block number needs to be greater than starting block number")
|
|
|
|
}
|
|
|
|
logrus.Infof("Writing statediffs from block %d to %d", rng[0], rng[1])
|
|
|
|
for height := rng[0]; height <= rng[1]; height++ {
|
|
|
|
err := sds.WriteStateDiffAt(height, param)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed to write state diff for range: %v %v", rng, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
2020-11-10 09:07:59 +00:00
|
|
|
}
|