github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/cmd/votereward/main.go (about)

     1  package main
     2  
     3  import (
     4  	"time"
     5  
     6  	log "github.com/sirupsen/logrus"
     7  	"github.com/spf13/cobra"
     8  	"github.com/tendermint/tmlibs/cli"
     9  
    10  	"github.com/bytom/bytom/consensus"
    11  	"github.com/bytom/bytom/toolbar/common"
    12  	cfg "github.com/bytom/bytom/toolbar/vote_reward/config"
    13  	"github.com/bytom/bytom/toolbar/vote_reward/settlementvotereward"
    14  	"github.com/bytom/bytom/toolbar/vote_reward/synchron"
    15  )
    16  
    17  const logModule = "reward"
    18  
    19  var (
    20  	rewardStartHeight uint64
    21  	rewardEndHeight   uint64
    22  	configFile        string
    23  )
    24  
    25  var RootCmd = &cobra.Command{
    26  	Use:   "reward",
    27  	Short: "distribution of reward.",
    28  	RunE:  runReward,
    29  }
    30  
    31  func init() {
    32  	RootCmd.Flags().Uint64Var(&rewardStartHeight, "reward_start_height", 0, "The starting height of the distributive income reward interval, It is a multiple of the pos consensus cycle(100). example: 600")
    33  	RootCmd.Flags().Uint64Var(&rewardEndHeight, "reward_end_height", 0, "The end height of the distributive income reward interval, It is a multiple of the pos consensus cycle(100). example: 1200")
    34  	RootCmd.Flags().StringVar(&configFile, "config_file", "reward.json", "config file. default: reward.json")
    35  }
    36  
    37  func runReward(cmd *cobra.Command, args []string) error {
    38  	log.Info("This tool belongs to an open-source project, we can not guarantee this tool is bug-free. Please check the code before using, developers will not be responsible for any asset loss due to bug!")
    39  	startTime := time.Now()
    40  	config := &cfg.Config{}
    41  	if err := cfg.LoadConfigFile(configFile, config); err != nil {
    42  		log.WithFields(log.Fields{"module": logModule, "config": configFile, "error": err}).Fatal("Failded to load config file.")
    43  	}
    44  
    45  	if err := consensus.InitActiveNetParams(config.ChainID); err != nil {
    46  		log.WithFields(log.Fields{"module": logModule, "error": err}).Fatal("Init ActiveNetParams.")
    47  	}
    48  	if rewardStartHeight >= rewardEndHeight || rewardStartHeight%consensus.ActiveNetParams.BlocksOfEpoch != 0 || rewardEndHeight%consensus.ActiveNetParams.BlocksOfEpoch != 0 {
    49  		log.Fatal("Please check the height range, which must be multiple of the number of block rounds.")
    50  	}
    51  
    52  	db, err := common.NewMySQLDB(config.MySQLConfig)
    53  	if err != nil {
    54  		log.WithFields(log.Fields{"module": logModule, "error": err}).Fatal("Failded to initialize mysql db.")
    55  	}
    56  
    57  	db.LogMode(true)
    58  
    59  	keeper, err := synchron.NewChainKeeper(db, config, rewardEndHeight)
    60  	if err != nil {
    61  		log.WithFields(log.Fields{"module": logModule, "error": err}).Fatal("Failded to initialize NewChainKeeper.")
    62  	}
    63  
    64  	if err := keeper.SyncBlock(); err != nil {
    65  		log.WithFields(log.Fields{"module": logModule, "error": err}).Fatal("Failded to sync block.")
    66  	}
    67  
    68  	s := settlementvotereward.NewSettlementReward(db, config, rewardStartHeight, rewardEndHeight)
    69  
    70  	if err := s.Settlement(); err != nil {
    71  		log.WithFields(log.Fields{"module": logModule, "error": err}).Fatal("Settlement vote rewards failure.")
    72  	}
    73  
    74  	log.WithFields(log.Fields{
    75  		"module":   logModule,
    76  		"duration": time.Since(startTime),
    77  	}).Info("Settlement vote reward complete")
    78  
    79  	return nil
    80  }
    81  
    82  func main() {
    83  	cmd := cli.PrepareBaseCmd(RootCmd, "REWARD", "./")
    84  	cmd.Execute()
    85  }