-
Notifications
You must be signed in to change notification settings - Fork 0
/
newMain.go
163 lines (143 loc) · 3.61 KB
/
newMain.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"os"
"os/signal"
"time"
"github.com/FactomProject/anchormaker/api"
"github.com/FactomProject/anchormaker/bitcoin"
"github.com/FactomProject/anchormaker/config"
"github.com/FactomProject/anchormaker/database"
//"github.com/FactomProject/anchormaker/ethereum"
"github.com/FactomProject/anchormaker/factom"
"github.com/FactomProject/anchormaker/setup"
)
func main() {
c := config.ReadConfig()
bitcoin.LoadConfig(c)
//ethereum.LoadConfig(c)
factom.LoadConfig(c)
api.SetServer(c.Factom.FactomdAddress)
var err error
err = setup.Setup(c)
if err != nil {
panic(err)
}
dbo := database.NewMapDB()
if c.App.DBType == "Map" {
fmt.Printf("Starting Map database\n")
dbo = database.NewMapDB()
}
if c.App.DBType == "LDB" {
fmt.Printf("Starting Level database\n")
dbo, err = database.NewLevelDB(c.App.LdbPath)
if err != nil {
panic(err)
}
}
if c.App.DBType == "Bolt" {
fmt.Printf("Starting Bolt database\n")
dbo, err = database.NewBoltDB(c.App.BoltPath)
if err != nil {
panic(err)
}
}
var interruptChannel chan os.Signal
interruptChannel = make(chan os.Signal, 1)
signal.Notify(interruptChannel, os.Interrupt)
go interruptLoop()
for {
//ensuring safe interruption
select {
case <-interruptChannel:
return
default:
err := SynchronizationLoop(dbo)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
time.Sleep(10 * time.Second)
continue
}
err = AnchorLoop(dbo, c)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
time.Sleep(10 * time.Second)
continue
}
fmt.Printf("\n\n\n")
time.Sleep(10 * time.Second)
}
}
}
//Function for quickly shutting down the function, disregarding safety
func interruptLoop() {
var interruptChannel chan os.Signal
interruptChannel = make(chan os.Signal, 1)
signal.Notify(interruptChannel, os.Interrupt)
for i := 0; i < 5; i++ {
<-interruptChannel
if i < 4 {
fmt.Printf("Received interrupt signal %v times. The program will shut down safely after a full loop.\nFor emergency shutdown, interrupt %v more times.\n", i+1, 4-i)
}
}
fmt.Printf("Emergency shutdown!\n")
os.Exit(1)
}
//The loop that synchronizes AnchorMaker with all networks
func SynchronizationLoop(dbo *database.AnchorDatabaseOverlay) error {
fmt.Printf("SynchronizationLoop\n")
i := 0
for {
//Iterate until we are fully in synch with all of the networks
//Repeat iteration until there is nothing left to synch
//to make sure all of the networks are in synch at the same time
//(nothing has drifted apart while we were busy with other systems)
fmt.Printf("Loop %v\n", i)
blockCount, err := factom.SynchronizeFactomData(dbo)
if err != nil {
return err
}
fmt.Printf("blockCount - %v\n", blockCount)
/*
txCount, err := ethereum.SynchronizeEthereumData(dbo)
if err != nil {
return err
}
fmt.Printf("txCount - %v\n", txCount)
*/
btcCount, err := bitcoin.SynchronizeBitcoinData(dbo)
if err != nil {
return err
}
fmt.Printf("btcCount - %v\n", btcCount)
//if (blockCount + txCount + btcCount) == 0 {
if (blockCount + btcCount) == 0 {
//if (blockCount + txCount) == 0 {
break
}
i++
}
return nil
}
func AnchorLoop(dbo *database.AnchorDatabaseOverlay, c *config.AnchorConfig) error {
var err error
err = setup.CheckAndTopupBalances(c.Factom.ECBalanceThreshold, c.Factom.FactoidBalanceThreshold, 100)
if err != nil {
return err
}
/*
err = ethereum.AnchorBlocksIntoEthereum(dbo)
if err != nil {
return err
}
*/
err = bitcoin.AnchorBlocksIntoBitcoin(dbo)
if err != nil {
return err
}
err = factom.SaveAnchorsIntoFactom(dbo)
if err != nil {
return err
}
return nil
}