-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlightnode.go
212 lines (191 loc) · 7.13 KB
/
lightnode.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
package lightnode
import (
"context"
"database/sql"
"fmt"
"github.com/go-redis/redis/v7"
"github.com/renproject/darknode/binding"
"github.com/renproject/darknode/jsonrpc"
"github.com/renproject/kv"
"github.com/renproject/lightnode/cacher"
v0 "github.com/renproject/lightnode/compat/v0"
v1 "github.com/renproject/lightnode/compat/v1"
"github.com/renproject/lightnode/confirmer"
"github.com/renproject/lightnode/db"
"github.com/renproject/lightnode/dispatcher"
"github.com/renproject/lightnode/resolver"
"github.com/renproject/lightnode/store"
"github.com/renproject/lightnode/updater"
"github.com/renproject/lightnode/watcher"
"github.com/renproject/multichain"
"github.com/renproject/phi"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
solanaRPC "github.com/dfuse-io/solana-go/rpc"
)
// Lightnode is the top level container that encapsulates the functionality of
// the lightnode.
type Lightnode struct {
options Options
logger logrus.FieldLogger
db db.DB
server *jsonrpc.Server
updater updater.Updater
confirmer confirmer.Confirmer
watchers map[multichain.Chain]map[multichain.Asset]watcher.Watcher
// Tasks
cacher phi.Task
dispatcher phi.Task
}
// New constructs a new Lightnode.
func New(options Options, ctx context.Context, logger logrus.FieldLogger, sqlDB *sql.DB, client redis.Cmdable) Lightnode {
switch options.Network {
case multichain.NetworkMainnet, multichain.NetworkTestnet, multichain.NetworkDevnet, multichain.NetworkLocalnet:
default:
panic("unknown network")
}
if options.DistPubKey == nil {
panic("distributed public key not specified")
}
if options.Port == "" {
panic("port not specified")
}
if len(options.BootstrapAddrs) == 0 {
panic("bootstrap addresses not specified")
}
// Define the options used for all Phi tasks.
opts := phi.Options{Cap: options.Cap}
// Initialise the database.
db := db.New(sqlDB, options.MaxGatewayCount)
if err := db.Init(); err != nil {
logger.Panicf("failed to initialise db: %v", err)
}
// Define the options used for the server.
serverOptions := jsonrpc.DefaultOptions().
WithMaxBatchSize(options.MaxBatchSize).
WithMaxPageSize(options.MaxPageSize).
WithTimeout(options.ServerTimeout)
// Initialise the multi-address store.
table := kv.NewTable(kv.NewMemDB(kv.JSONCodec), "addresses")
multiStore := store.New(table, options.BootstrapAddrs)
// Initialise the blockchain adapter.
loggerConfig := zap.NewProductionConfig()
loggerConfig.DisableCaller = true
loggerConfig.DisableStacktrace = true
loggerConfig.Encoding = "console"
loggerConfig.EncoderConfig.TimeKey = "timestamp"
loggerConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
bindingsLogger, err := loggerConfig.Build()
if err != nil {
panic(fmt.Errorf("cannot init logger: %v", err))
}
bindingsOpts := binding.DefaultOptions().
WithLogger(bindingsLogger).
WithNetwork(options.Network)
for chain, chainOpts := range options.Chains {
bindingsOpts = bindingsOpts.WithChainOptions(chain, chainOpts)
}
bindings := binding.New(bindingsOpts)
// ==== BEGIN GROSS HACK
//
// TODO: For now we use a custom set of bindings for the transaction
// verifier (with confirmations set to zero) as we want the initial
// verification to succeed even if the transaction has not received any
// confirmations.
//
verifierBindingsOpts := binding.DefaultOptions().
WithLogger(bindingsLogger).
WithNetwork(options.Network)
for chain, chainOpts := range options.Chains {
chainOpts.Confirmations = 0
verifierBindingsOpts = verifierBindingsOpts.WithChainOptions(chain, chainOpts)
}
verifierBindings := binding.New(verifierBindingsOpts)
// ==== END GROSS HACK
//
updater := updater.New(logger, multiStore, options.UpdaterPollRate, options.ClientTimeout)
dispatcher := dispatcher.New(logger, options.ClientTimeout, multiStore, opts)
ttlCache := kv.NewTTLCache(ctx, kv.NewMemDB(kv.JSONCodec), "cacher", options.TTL)
cacher := cacher.New(dispatcher, logger, ttlCache, opts, db)
versionStore := v0.NewCompatStore(db, client, options.TransactionExpiry)
gpubkeyStore := v1.NewCompatStore(client)
hostChains := map[multichain.Chain]bool{}
for _, selector := range options.Whitelist {
if selector.IsLock() && selector.IsMint() {
hostChains[selector.Destination()] = true
}
}
verifier := resolver.NewVerifier(hostChains, verifierBindings)
resolverI := resolver.New(options.Network, logger, cacher, multiStore, db, serverOptions, versionStore, gpubkeyStore, bindings, verifier)
limiter := resolver.NewRateLimiter(resolver.RateLimiterConf{
GlobalMethodRate: options.LimiterGlobalRates,
IpMethodRate: options.LimiterIPRates,
Ttl: options.LimiterTTL,
MaxClients: options.LimiterMaxClients,
})
server := jsonrpc.NewServer(serverOptions, resolverI, resolver.NewValidator(options.Network, verifierBindings, options.DistPubKey, versionStore, gpubkeyStore, &limiter, logger))
confirmer := confirmer.New(
confirmer.DefaultOptions().
WithLogger(logger).
WithPollInterval(options.ConfirmerPollRate).
WithExpiry(options.TransactionExpiry),
dispatcher,
db,
bindings,
)
watchers := map[multichain.Chain]map[multichain.Asset]watcher.Watcher{}
solClient := solanaRPC.NewClient(bindingsOpts.Chains[multichain.Solana].RPC.String())
for _, selector := range options.Whitelist {
if !selector.IsBurn() || !selector.IsRelease() {
continue
}
chain := selector.Source()
asset := selector.Asset()
if watchers[chain] == nil {
watchers[chain] = map[multichain.Asset]watcher.Watcher{}
}
if _, ok := watchers[chain][asset]; ok {
continue
}
var burnLogFetcher watcher.BurnLogFetcher
var blockHeightFetcher watcher.BlockHeightFetcher
if chain == multichain.Solana {
if bindings.ContractGateway(chain, asset) == "" {
continue
}
burnLogFetcher = watcher.NewSolFetcher(solClient, string(bindings.ContractGateway(chain, asset)))
blockHeightFetcher = watcher.NewSolFetcher(solClient, string(bindings.ContractGateway(chain, asset)))
} else {
burnLogFetcher = watcher.NewEthBurnLogFetcher(bindings.EthereumGateway(chain, asset))
blockHeightFetcher = watcher.NewEthBlockHeightFetcher(bindings.EthereumClient(chain))
}
watchers[chain][selector.Asset()] = watcher.NewWatcher(logger, options.Network, selector, verifierBindings, burnLogFetcher, blockHeightFetcher, resolverI, client, options.WatcherPollRate, options.WatcherMaxBlockAdvance, options.WatcherConfidenceInterval)
logger.Info("watching", selector)
}
return Lightnode{
options: options,
logger: logger,
db: db,
updater: updater,
dispatcher: dispatcher,
cacher: cacher,
server: server,
confirmer: confirmer,
watchers: watchers,
}
}
// Run starts the `Lightnode`. This function call is blocking.
func (lightnode Lightnode) Run(ctx context.Context) {
go lightnode.updater.Run(ctx)
go lightnode.cacher.Run(ctx)
go lightnode.dispatcher.Run(ctx)
// Note: the following should be disabled when running locally.
go lightnode.confirmer.Run(ctx)
for _, assetMap := range lightnode.watchers {
for _, watcher := range assetMap {
go watcher.Run(ctx)
}
}
lightnode.server.Listen(ctx, fmt.Sprintf(":%s", lightnode.options.Port))
}