-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
182 lines (158 loc) · 5.94 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
// The Licensed Work is (c) 2023 Sygma
// SPDX-License-Identifier: LGPL-3.0-only
package main
import (
"context"
"fmt"
"math/big"
"os"
"os/signal"
"syscall"
"time"
"github.com/attestantio/go-eth2-client/http"
"github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
evmConfig "github.com/sygmaprotocol/spectre-node/chains/evm/config"
"github.com/sygmaprotocol/spectre-node/chains/evm/contracts"
"github.com/sygmaprotocol/spectre-node/chains/evm/executor"
"github.com/sygmaprotocol/spectre-node/chains/evm/lightclient"
"github.com/sygmaprotocol/spectre-node/chains/evm/listener"
hashi "github.com/sygmaprotocol/spectre-node/chains/evm/listener/events/handlers"
"github.com/sygmaprotocol/spectre-node/chains/evm/listener/handlers"
evmMessage "github.com/sygmaprotocol/spectre-node/chains/evm/message"
"github.com/sygmaprotocol/spectre-node/chains/evm/prover"
"github.com/sygmaprotocol/spectre-node/config"
"github.com/sygmaprotocol/spectre-node/health"
"github.com/sygmaprotocol/spectre-node/store"
"github.com/sygmaprotocol/sygma-core/chains/evm"
"github.com/sygmaprotocol/sygma-core/chains/evm/client"
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/gas"
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/monitored"
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/transaction"
"github.com/sygmaprotocol/sygma-core/crypto/secp256k1"
"github.com/sygmaprotocol/sygma-core/observability"
"github.com/sygmaprotocol/sygma-core/relayer"
"github.com/sygmaprotocol/sygma-core/relayer/message"
"github.com/sygmaprotocol/sygma-core/store/lvldb"
"github.com/ybbus/jsonrpc/v3"
)
func main() {
cfg, err := config.LoadConfig()
if err != nil {
panic(err)
}
logLevel, err := zerolog.ParseLevel(cfg.Observability.LogLevel)
if err != nil {
panic(err)
}
observability.ConfigureLogger(logLevel, os.Stdout)
log.Info().Msg("Loaded configuration")
go health.StartHealthEndpoint(cfg.Observability.HealthPort)
var db *lvldb.LVLDB
for {
db, err = lvldb.NewLvlDB(cfg.Store.Path)
if err != nil {
log.Error().Err(err).Msg("Unable to connect to blockstore file, retry in 10 seconds")
time.Sleep(10 * time.Second)
} else {
log.Info().Msg("Successfully connected to blockstore file")
break
}
}
periodStore := store.NewPeriodStore(db)
proverClient := jsonrpc.NewClient(cfg.Prover.URL)
msgChan := make(chan []*message.Message)
chains := make(map[uint8]relayer.RelayedChain)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for id, nType := range cfg.Domains {
switch nType {
case "evm":
{
config, err := evmConfig.LoadEVMConfig(id)
if err != nil {
panic(err)
}
kp, err := secp256k1.NewKeypairFromString(config.Key)
if err != nil {
panic(err)
}
client, err := client.NewEVMClient(config.Endpoint, kp)
if err != nil {
panic(err)
}
gasPricer := gas.NewLondonGasPriceClient(client, &gas.GasPricerOpts{
UpperLimitFeePerGas: big.NewInt(config.MaxGasPrice),
GasPriceFactor: big.NewFloat(config.GasMultiplier),
})
t := monitored.NewMonitoredTransactor(transaction.NewTransaction, gasPricer, client, big.NewInt(config.MaxGasPrice), big.NewInt(config.GasIncreasePercentage))
go t.Monitor(ctx, time.Minute*3, time.Minute*10, time.Minute)
var evmListener *listener.EVMListener
if len(config.TargetDomains) > 0 {
beaconClient, err := http.New(ctx,
http.WithAddress(config.BeaconEndpoint),
http.WithLogLevel(logLevel),
http.WithTimeout(time.Second*30),
)
if err != nil {
panic(err)
}
beaconProvider := beaconClient.(*http.Service)
storedPeriod, err := periodStore.Period(id)
if err != nil {
panic(err)
}
var latestPeriod *big.Int
if (storedPeriod.Uint64() >= config.StartingPeriod) && !config.ForcePeriod {
latestPeriod = storedPeriod
} else {
latestPeriod = big.NewInt(int64(config.StartingPeriod))
}
targetDomains := make([]uint8, len(config.TargetDomains))
for i, d := range config.TargetDomains {
targetDomains[i] = uint8(d)
}
lightClient := lightclient.NewLightClient(config.BeaconEndpoint)
p := prover.NewProver(proverClient, beaconProvider, lightClient, prover.Spec(config.Spec), config.FinalityThreshold, config.SlotsPerEpoch)
domainCollectors := []handlers.DomainCollector{}
if config.Yaho != "" {
domainCollectors = append(domainCollectors, hashi.NewHashiDomainCollector(
id,
common.HexToAddress(config.Yaho),
client,
targetDomains,
))
}
stepHandler := handlers.NewStepEventHandler(msgChan, domainCollectors, beaconProvider, p, id, targetDomains)
rotateHandler := handlers.NewRotateHandler(msgChan, periodStore, p, id, targetDomains, config.CommitteePeriodLength, latestPeriod)
evmListener = listener.NewEVMListener(beaconProvider, []listener.EventHandler{rotateHandler, stepHandler}, id, time.Duration(config.RetryInterval)*time.Second)
}
messageHandler := message.NewMessageHandler()
rotateMessageHandler := evmMessage.EvmRotateHandler{}
stepMessageHandler := evmMessage.EvmStepHandler{}
messageHandler.RegisterMessageHandler(evmMessage.EVMRotateMessage, &rotateMessageHandler)
messageHandler.RegisterMessageHandler(evmMessage.EVMStepMessage, &stepMessageHandler)
spectre := contracts.NewSpectreContract(common.HexToAddress(config.Spectre), t)
executor := executor.NewEVMExecutor(id, spectre)
chain := evm.NewEVMChain(evmListener, messageHandler, executor, id, nil)
chains[id] = chain
}
default:
{
panic(fmt.Sprintf("invalid network type %s for id %d", nType, id))
}
}
}
r := relayer.NewRelayer(chains)
go r.Start(ctx, msgChan)
sysErr := make(chan os.Signal, 1)
signal.Notify(sysErr,
syscall.SIGTERM,
syscall.SIGINT,
syscall.SIGHUP,
syscall.SIGQUIT)
log.Info().Msgf("Started spectre node")
se := <-sysErr
log.Info().Msgf("terminating got ` [%v] signal", se)
}