-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
217 lines (183 loc) · 5.96 KB
/
main.go
File metadata and controls
217 lines (183 loc) · 5.96 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/LimeChain/merkletree"
"github.com/LimeChain/merkletree/memory"
"github.com/LimeChain/merkletree/postgres"
merkleRestAPI "github.com/LimeChain/merkletree/restapi/baseapi"
validateAPI "github.com/LimeChain/merkletree/restapi/validateapi"
"github.com/ReMe-life/ReMe-Merkle-Tree-Distribution/saver"
env "github.com/Netflix/go-env"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
"github.com/simonleung8/flags"
)
type Environment struct {
Blockchain struct {
NodeURL *string `env:"NODE_URL"`
Secret *string `env:"SECRET_KEY"`
ContractAddress *string `env:"CONTRACT_ADDRESS"`
SavePeriod int `env:"SAVE_PERIOD"`
}
Database struct {
Host *string `env:"DB_HOST"`
Port int `env:"DB_PORT"`
User *string `env:"DB_USER"`
Pass *string `env:"DB_PASS"`
DBName *string `env:"DB_NAME"`
DBExtra *string `env:"DB_EXTRA"`
}
Port int `env:"API_PORT"`
Extras env.EnvSet
}
var treeLen int
type savedResponse struct {
merkleRestAPI.MerkleAPIResponse
Length int `json:"lastSavedIndex"`
}
func getSaved(tree merkletree.ExternalMerkleTree) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if tree.Length() == 0 {
render.JSON(w, r, savedResponse{merkleRestAPI.MerkleAPIResponse{true, ""}, -1})
return
}
render.JSON(w, r, savedResponse{merkleRestAPI.MerkleAPIResponse{true, ""}, treeLen})
return
}
}
func createAndStartAPI(tree merkletree.ExternalMerkleTree, port int) {
router := chi.NewRouter()
router.Use(
render.SetContentType(render.ContentTypeJSON),
middleware.Logger,
middleware.Compress(6, "gzip"),
middleware.RedirectSlashes,
middleware.Recoverer,
middleware.NoCache,
)
router.Route("/v1", func(r chi.Router) {
treeRouter := chi.NewRouter()
treeRouter = merkleRestAPI.MerkleTreeStatus(treeRouter, tree)
treeRouter = merkleRestAPI.MerkleTreeInsert(treeRouter, tree)
treeRouter = merkleRestAPI.MerkleTreeHashes(treeRouter, tree)
treeRouter = merkleRestAPI.MerkleTreeRawInsert(treeRouter, tree)
treeRouter = validateAPI.MerkleTreeValidate(treeRouter, tree)
treeRouter.Get("/saved", getSaved(tree))
r.Mount("/api/merkletree", treeRouter)
})
fmt.Printf("Starting REST Api at port %v\n", port)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), router))
}
func createSaver(tree merkletree.FullMerkleTree, nodeUrl, privateKeyHex, contractAddress string, periodSeconds int) {
treeSaver, err := saver.NewSaver(
nodeUrl,
privateKeyHex,
contractAddress,
tree)
if err != nil {
panic(err)
}
go func() {
len := 0
timeout := time.Duration(periodSeconds) * time.Second
for {
tree.Recalculate()
savedRoot, err := treeSaver.FetchRoot()
if err != nil {
fmt.Println("ERR: Could not save the tree root")
fmt.Println(err.Error())
time.Sleep(timeout)
continue
}
if savedRoot == tree.Root() {
fmt.Printf("Same root (%v) found in the chain. Skipping this iteration\n", savedRoot)
treeLen = tree.Length()
time.Sleep(timeout)
continue
}
treeLen = tree.Length()
if treeLen > len {
fmt.Printf("Submitting new tree root to the chain (%v)\n", tree.Root())
tx, err := treeSaver.TriggerSave()
if err != nil {
fmt.Println("ERR: Could not save the tree root")
fmt.Println(err.Error())
} else {
fmt.Printf("Transaction (%v) mined\n", tx.TxHash.Hex())
fmt.Printf("Gas used (%v)\n", tx.GasUsed)
len = treeLen
}
} else {
fmt.Println("No changes to submit. Skipping this iteration")
}
time.Sleep(timeout)
}
}()
fmt.Printf("Started saver on %v seconds\n", periodSeconds)
fmt.Printf("Node url %v\n", nodeUrl)
fmt.Printf("Verifier contract address %v \n", contractAddress)
}
func setupFlags() flags.FlagContext {
fc := flags.New()
fc.NewBoolFlag("envirnonment-variables", "e", "Connection string for the postgres database")
fc.NewStringFlag("database-connection", "db", "Connection string for the postgres database")
fc.NewStringFlag("node-url", "n", "url to the ethereum node to connect")
fc.NewStringFlag("secret", "s", "private key for the ethereum saver")
fc.NewStringFlag("conntract-address", "c", "Address to the verifier contract")
fc.NewIntFlagWithDefault("port", "ap", "port to run the API on", 8080)
fc.NewIntFlagWithDefault("period", "p", "period to try and save the new root", 15)
fc.Parse(os.Args...)
return fc
}
func loadPostgreTree(connStr string) merkletree.FullMerkleTree {
tree := postgres.LoadMerkleTree(memory.NewMerkleTree(), connStr)
fmt.Printf("Merkle tree loaded. Length : %v, Root : %v\n", tree.Length(), tree.Root())
return tree
}
func main() {
fc := setupFlags()
var connStr, nodeURL, privateKeyHex, contractAddress string
var period, port int
if fc.Bool("e") {
var environment Environment
_, err := env.UnmarshalFromEnviron(&environment)
if err != nil {
log.Fatal(err)
}
connStr = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s %s", *environment.Database.Host, environment.Database.Port, *environment.Database.User, *environment.Database.Pass, *environment.Database.DBName, *environment.Database.DBExtra)
nodeURL = *environment.Blockchain.NodeURL
privateKeyHex = *environment.Blockchain.Secret
contractAddress = *environment.Blockchain.ContractAddress
period = environment.Blockchain.SavePeriod
port = environment.Port
} else {
if !fc.IsSet("db") {
panic(errors.New("No db flag set"))
}
if !fc.IsSet("n") {
panic(errors.New("No node-url flag set"))
}
if !fc.IsSet("s") {
panic(errors.New("No secret flag set"))
}
if !fc.IsSet("c") {
panic(errors.New("No conntract-address flag set"))
}
connStr = fc.String("db")
nodeURL = fc.String("n")
privateKeyHex = fc.String("s")
contractAddress = fc.String("c")
period = fc.Int("p")
port = fc.Int("ap")
}
tree := loadPostgreTree(connStr)
createSaver(tree, nodeURL, privateKeyHex, contractAddress, period)
createAndStartAPI(tree, port)
}