-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (128 loc) · 3.81 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"os"
"os/signal"
"time"
"github.com/FactomProject/anchormaker-eth/api"
"github.com/FactomProject/anchormaker-eth/config"
"github.com/FactomProject/anchormaker-eth/database"
"github.com/FactomProject/anchormaker-eth/ethereum"
"github.com/FactomProject/anchormaker-eth/factom"
"github.com/FactomProject/anchormaker-eth/setup"
)
func main() {
c := config.ReadConfig()
ethereum.LoadConfig(c)
factom.LoadConfig(c)
api.SetServer(c.App.FactomdNodeURL)
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()
// TODO: eliminate loops and locking, make the app reactive instead with channels
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)
}
// SynchronizationLoop ensures AnchorMaker is up to date with all relevant events that
// have occurred on both the Factom and Ethereum networks
func SynchronizationLoop(dbo *database.AnchorDatabaseOverlay) error {
fmt.Println("--------------------------------------------------------------------------------")
fmt.Println("SynchronizationLoop():")
i := 0
for {
// Repeat iteration until we are fully in sync with all both Factom and Ethereum
// to make sure all of the networks are in sync at the same time
// (nothing has drifted apart while we were busy with other systems)
fmt.Printf("Loop %v started\n", i)
blockCount, err := factom.SynchronizeFactomData(dbo)
if err != nil {
return err
}
fmt.Printf("New Factom blocks found = %v\n", blockCount)
txCount, err := ethereum.SynchronizeEthereumData(dbo)
if err != nil {
return err
}
fmt.Printf("New Ethereum contract txs found = %v\n", txCount)
if (blockCount + txCount) == 0 {
break
}
fmt.Printf("Loop %v ended with %v new DBlocks and %v new Ethereum txs found\n", i, blockCount, txCount)
i++
}
return nil
}
// AnchorLoop submits Ethereum anchors for all new directory blocks found during the SynchronizationLoop,
// and then submits Factom entries (anchor records) for all newly confirmed contract txs found during the
// SynchronizationLoop
func AnchorLoop(dbo *database.AnchorDatabaseOverlay, c *config.AnchorConfig) error {
fmt.Println("--------------------------------------------------------------------------------")
fmt.Println("AnchorLoop():")
err := ethereum.AnchorBlocksIntoEthereum(dbo)
if err != nil {
return err
}
err = factom.SaveAnchorsIntoFactom(dbo)
if err != nil {
return err
}
return nil
}