-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
227 lines (209 loc) · 7.39 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
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
218
219
220
221
222
223
224
225
226
227
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"sort"
"syscall"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ronin-chain/ronin-random-beacon/pkg/config"
"github.com/ronin-chain/ronin-random-beacon/pkg/contract"
"github.com/ronin-chain/ronin-random-beacon/pkg/core"
chainInfoDb "github.com/ronin-chain/ronin-random-beacon/pkg/db/chaininfo"
"github.com/ronin-chain/ronin-random-beacon/pkg/dbutils"
"github.com/ronin-chain/ronin-random-beacon/pkg/listener"
"github.com/ronin-chain/ronin-random-beacon/pkg/task"
"github.com/ronin-chain/ronin-random-beacon/pkg/utils"
"github.com/urfave/cli"
)
const (
creatingTaskInterval = 60 // 1 minute
sendingTaskInterval = 60 * 1 // 1 minutes
trackerInterval = 3 // 3s
reorgCheckerInterval = 60 * 5 // 5 minutes
taskSyncerSentInterval = 60 // 1 minutes
creatingTaskBatchSize = 5
sendingTaskBatchSize = 5
reorgTaskBatchSize = 5
taskSyncerSentBatchSize = 5
chainInfoId = "chainInfo"
blockSize = 100
)
var (
ConfigFileFlag = cli.StringFlag{
Name: "config-file",
Usage: "Explicitly defines the path, name and extension of the yaml config file",
Value: "./config.yaml",
}
PeriodFlag = cli.StringFlag{
Name: "period",
Usage: "Period of the random beacon",
Required: true,
}
PrevBeaconFlag = cli.StringFlag{
Name: "prev-beacon",
Usage: "Previous beacon",
Required: true,
}
ChainIdFlag = cli.StringFlag{
Name: "chain-id",
Usage: "Chain id",
Required: true,
}
VerifyingContractFlag = cli.StringFlag{
Name: "verifying-contract",
Usage: "Verifying contract",
Required: true,
}
OutputPathFlag = cli.StringFlag{
Name: "output-path",
Usage: "Output path for the proof file",
}
GenKeyCommand = cli.Command{
Action: generateVRFKey,
Name: "generate-key",
Usage: "Generate secret key and verification key",
Category: "Random Beacon Commands",
Description: "The generate-key command generate the pair of secret key and verification key",
}
StartCommand = cli.Command{
Action: start,
Name: "start",
Usage: "Start the random beacon",
Category: "Random Beacon Commands",
Description: "The start tracking event from Coodirnator contract and submit the proof to the contract",
Flags: []cli.Flag{
ConfigFileFlag,
},
}
RandomCommand = cli.Command{
Action: random,
Name: "random",
Usage: "Generate a random number",
Category: "Random Beacon Commands",
Description: "Random based on the input flag and write out the proof file ./random-result.json",
Flags: []cli.Flag{
ConfigFileFlag,
PeriodFlag,
PrevBeaconFlag,
ChainIdFlag,
VerifyingContractFlag,
OutputPathFlag,
},
}
)
func generateVRFKey(ctx *cli.Context) {
core.GenerateVRFKey()
}
func start(cli *cli.Context) {
config := config.LoadConfig(cli.String(ConfigFileFlag.GetName()))
dbClient := dbutils.Open(config.DatabaseURL)
dbutils.MigrateDB(dbClient, context.Background())
ethClient, err := ethclient.Dial(config.RpcEndpoint)
if err != nil {
panic(fmt.Errorf("Failed to connect to the Ethereum client: %w", err))
}
chainInfoItem, err := dbClient.ChainInfo.Query().Where(chainInfoDb.IDEQ(chainInfoId)).Only(context.Background())
if err != nil {
log.Info("[Main] Chain info not found, creating new one")
dbClient.ChainInfo.Create().SetID(chainInfoId).SetProcessedBlock(config.ProcessedBlock).SaveX(context.Background())
} else {
// Update process block based on config If we had
log.Info("[Main] Loaded chain info", "ProcessedBlock", config.ProcessedBlock)
if config.ProcessedBlock > 0 && config.ProcessedBlock > chainInfoItem.ProcessedBlock {
chainInfoItem.Update().SetProcessedBlock(config.ProcessedBlock).SaveX(context.Background())
log.Info("[Main] Updated processed block", "block", config.ProcessedBlock)
}
}
// Chain listener
chainListener, chainProcessor := listener.NewChainListener(chainInfoId, dbClient, ethClient, contract.Coordinator, config.RpcEndpoint, trackerInterval)
// // Random request listener
tracker := listener.NewRandomRequestListener(chainInfoId, &chainListener, contract.Coordinator.Contract, dbClient, config.CoordinatorStartingBlock, blockSize, trackerInterval)
// Produce task based on tracked RandomRequest.
taskCreator := task.NewTaskCreator(dbClient, config, creatingTaskBatchSize, creatingTaskInterval)
taskSender := task.NewTaskSender(dbClient, sendingTaskBatchSize, contract.Coordinator, chainProcessor, sendingTaskInterval, config.VRFConfig.KeyHash)
taskSyncerSent := task.NewTaskSyncerSent(dbClient, taskSyncerSentBatchSize, ethClient, chainProcessor, taskSyncerSentInterval)
taskReorgChecker := task.NewTaskReorgChecker(dbClient, ethClient, contract.Coordinator, chainProcessor, reorgTaskBatchSize, reorgCheckerInterval)
// Schedule pending tasks to fullfilRandomSeeds onchain.
chainListener.Start()
log.Info("[Main] Waiting 2 second before staring trackers.")
time.Sleep(2 * time.Second)
log.Info("[Main] Waiting 5 second for cleaning tasks before starting.")
tracker.Start()
taskCreator.Start()
taskSender.Start()
taskSyncerSent.Start()
taskReorgChecker.Start()
// Handle grateful shutdown.
signalChannel := make(chan os.Signal, 2)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChannel
log.Info("Stopping... need to stop all listeners and processors, close all connections and write file to disk before shutting down.")
tracker.Stop()
chainListener.Stop()
taskCreator.Stop()
taskSender.Stop()
taskSyncerSent.Stop()
taskReorgChecker.Stop()
ethClient.Close()
contract.Client.Close()
dbClient.Close()
}()
chainListener.Wait()
}
func random(ctx *cli.Context) {
config := config.LoadConfig(ctx.String(ConfigFileFlag.GetName()))
period := ctx.String(PeriodFlag.GetName())
prevBeacon := ctx.String(PrevBeaconFlag.GetName())
chainId := ctx.String(ChainIdFlag.GetName())
verifyingContract := ctx.String(VerifyingContractFlag.GetName())
log.Info("Loaded config ", "period", period, "Prev Beacon", prevBeacon)
res, err := core.GenerateProofResponse(
config.VRFConfig.Key,
utils.ConvertStringToBigInt(period),
utils.ConvertStringToBigInt(prevBeacon),
utils.ConvertStringToBigInt(chainId),
common.HexToAddress(verifyingContract),
config.VRFConfig.KeyHash)
if err != nil {
panic(err)
}
log.Info("Output", "res", res)
outputPath := ctx.String(OutputPathFlag.GetName())
if outputPath != "" {
proof := core.PrettyVRFProof{
[2]string{res.Proof.Pk[0].String(), res.Proof.Pk[1].String()},
[2]string{res.Proof.Gamma[0].String(), res.Proof.Gamma[1].String()},
res.Proof.C.String(),
res.Proof.S.String(),
res.Proof.Seed.String(),
res.Proof.UWitness,
[2]string{res.Proof.CGammaWitness[0].String(), res.Proof.CGammaWitness[1].String()},
[2]string{res.Proof.SHashWitness[0].String(), res.Proof.SHashWitness[1].String()},
res.Proof.ZInv.String(),
}
file, _ := json.MarshalIndent(proof, "", " ")
_ = ioutil.WriteFile(outputPath+"/random-result.json", file, 0644)
}
}
func main() {
app := cli.NewApp()
app.Name = "Ronin Random Beacon"
app.Commands = []cli.Command{
GenKeyCommand,
StartCommand,
RandomCommand,
}
sort.Sort(cli.CommandsByName(app.Commands))
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
log.Error("Failed for starting", err)
}
}