OSDN Git Service

fix the bug (#372)
[bytom/vapor.git] / cmd / votereward / main.go
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/vapor/consensus"
11         "github.com/vapor/toolbar/common"
12         cfg "github.com/vapor/toolbar/vote_reward/config"
13         "github.com/vapor/toolbar/vote_reward/settlementvotereward"
14         "github.com/vapor/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 dpos consensus cycle(1200). example: 1200")
33         RootCmd.Flags().Uint64Var(&rewardEndHeight, "reward_end_height", 0, "The end height of the distributive income reward interval, It is a multiple of the dpos consensus cycle(1200). example: 2400")
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         startTime := time.Now()
39         config := &cfg.Config{}
40         if err := cfg.LoadConfigFile(configFile, config); err != nil {
41                 log.WithFields(log.Fields{"module": logModule, "config": configFile, "error": err}).Fatal("Failded to load config file.")
42         }
43
44         if err := consensus.InitActiveNetParams(config.ChainID); err != nil {
45                 log.WithFields(log.Fields{"module": logModule, "error": err}).Fatal("Init ActiveNetParams.")
46         }
47         if rewardStartHeight >= rewardEndHeight || rewardStartHeight%consensus.ActiveNetParams.RoundVoteBlockNums != 0 || rewardEndHeight%consensus.ActiveNetParams.RoundVoteBlockNums != 0 {
48                 log.Fatal("Please check the height range, which must be multiple of the number of block rounds.")
49         }
50
51         db, err := common.NewMySQLDB(config.MySQLConfig)
52         if err != nil {
53                 log.WithFields(log.Fields{"module": logModule, "error": err}).Fatal("Failded to initialize mysql db.")
54         }
55
56         sync, err := synchron.NewChainKeeper(db, config, rewardEndHeight)
57         if err != nil {
58                 log.WithFields(log.Fields{"module": logModule, "error": err}).Fatal("Failded to initialize NewChainKeeper.")
59         }
60
61         if err := sync.SyncBlock(); err != nil {
62                 log.WithFields(log.Fields{"module": logModule, "error": err}).Fatal("Failded to sync block.")
63         }
64
65         s := settlementvotereward.NewSettlementReward(db, config, rewardStartHeight, rewardEndHeight)
66
67         if err := s.Settlement(); err != nil {
68                 log.WithFields(log.Fields{"module": logModule, "error": err}).Fatal("Settlement vote rewards failure.")
69         }
70
71         log.WithFields(log.Fields{
72                 "module":   logModule,
73                 "duration": time.Since(startTime),
74         }).Info("Settlement vote reward complete")
75
76         return nil
77 }
78
79 func main() {
80         cmd := cli.PrepareBaseCmd(RootCmd, "REWARD", "./")
81         cmd.Execute()
82 }