-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (111 loc) · 3.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"flag"
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"os/signal"
tracker "reorg-tracker/reorgtracker"
"time"
)
const MediumPause = 5 * time.Second
// loadFlags is a helper function that sets the verbosity level of the program through user specified flags
func loadFlags() bool {
isVerbose := flag.Bool("verbose", false, "Specifies verbosity of logs. True means Debug Level. "+
"False means Info Level")
flag.Parse()
if *isVerbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
return *isVerbose
}
// getClientLink is a helper function, it loads CLIENT_LINK from .env file
// If the program fails to load client link from .env file,
// then the ClientLink is taken as an input from user
func getClientLink() string {
viper.SetConfigFile(".env")
err := viper.ReadInConfig()
if err != nil {
log.Warn("Error: Unable to read ClientLink from .env file."+
"Error: ", err)
fmt.Println("Please enter Eth ClientLink:")
return getStringInput()
}
return viper.GetString("CLIENT_LINK")
}
// getStringInput is a helper function that takes string input from the user
func getStringInput() string {
var input string
for true {
_, err := fmt.Scan(&input)
if err != nil {
fmt.Println("Invalid Input. Please try again.")
continue
}
break
}
return input
}
// establishConnection is a helper function which keeps on running until a
// successful connection to EthClient is made
func establishConnection(ClientLink string) *ethclient.Client {
var client *ethclient.Client
// This loop keeps running until a successful connection to EthClient is established
for true {
var err error
client, err = ethclient.Dial(ClientLink)
if err != nil {
log.Warn("Encountered Error Connecting to ETH Client. Error: ", err)
log.Warn("Trying again...")
ClientLink = getClientLink()
// This pause is taken before trying again to connect with the ETH Client,
// A pause is taken to prevent sending too many requests to the remote eth client
time.Sleep(MediumPause)
continue
}
err = nil
log.Debug("Successfully connected to EthClient.")
break
}
return client
}
// gracefulShutdown is a helper function that stops the program
// When user presses Ctrl+C, the connection is closed and the program closes
func gracefulShutdown(client *ethclient.Client) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
go func() {
<-sigs
client.Close()
fmt.Println("Chain-Reorg Tracker stopped successfully.")
os.Exit(0)
}()
}
// startTacking is a helper function which calls tracker.StartTracking()
func startTacking(client *ethclient.Client, isVerbose bool) {
for true {
err := tracker.StartTracking(client, isVerbose)
if err != nil {
log.Warn("Encountered Error Tracking Chain ReOrg. Error: ", err)
log.Warn("Restarting the tracker in 5 seconds.")
// This pause is taken before trying again to start the tracker,
// A pause is taken to prevent sending too many requests to the remote eth client
// through StartTracking method
time.Sleep(MediumPause)
continue
}
}
}
// driver code
// This shows usage of reorg-tracker package
func main() {
isVerbose := loadFlags()
ClientLink := getClientLink()
client := establishConnection(ClientLink)
gracefulShutdown(client)
startTacking(client, isVerbose)
}