-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathserver.go
362 lines (321 loc) · 10.4 KB
/
server.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package tapcfg
import (
"context"
"database/sql"
"fmt"
"github.com/btcsuite/btclog"
"github.com/lightninglabs/lndclient"
tap "github.com/lightninglabs/taproot-assets"
"github.com/lightninglabs/taproot-assets/address"
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/tapdb"
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
"github.com/lightninglabs/taproot-assets/tapfreighter"
"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/lightninglabs/taproot-assets/universe"
"github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/signal"
"github.com/lightningnetwork/lnd/ticker"
)
// databaseBackend is an interface that contains all methods our different
// database backends implement.
type databaseBackend interface {
tapdb.BatchedQuerier
WithTx(tx *sql.Tx) *sqlc.Queries
}
// genServerConfig generates a server config from the given tapd config.
//
// NOTE: The RPCConfig and SignalInterceptor fields must be set by the caller
// after genereting the server config.
func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
lndServices *lndclient.LndServices,
mainErrChan chan<- error) (*tap.Config, error) {
var err error
// Now that we know where the database will live, we'll go ahead and
// open up the default implementation of it.
var db databaseBackend
switch cfg.DatabaseBackend {
case DatabaseBackendSqlite:
cfgLogger.Infof("Opening sqlite3 database at: %v",
cfg.Sqlite.DatabaseFileName)
db, err = tapdb.NewSqliteStore(cfg.Sqlite)
case DatabaseBackendPostgres:
cfgLogger.Infof("Opening postgres database at: %v",
cfg.Postgres.DSN(true))
db, err = tapdb.NewPostgresStore(cfg.Postgres)
default:
return nil, fmt.Errorf("unknown database backend: %s",
cfg.DatabaseBackend)
}
if err != nil {
return nil, fmt.Errorf("unable to open database: %v", err)
}
rksDB := tapdb.NewTransactionExecutor(
db, func(tx *sql.Tx) tapdb.KeyStore {
return db.WithTx(tx)
},
)
mintingStore := tapdb.NewTransactionExecutor(
db, func(tx *sql.Tx) tapdb.PendingAssetStore {
return db.WithTx(tx)
},
)
assetMintingStore := tapdb.NewAssetMintingStore(mintingStore)
assetDB := tapdb.NewTransactionExecutor(
db, func(tx *sql.Tx) tapdb.ActiveAssetsStore {
return db.WithTx(tx)
},
)
addrBookDB := tapdb.NewTransactionExecutor(
db, func(tx *sql.Tx) tapdb.AddrBook {
return db.WithTx(tx)
},
)
tapChainParams := address.ParamsForChain(cfg.ActiveNetParams.Name)
tapdbAddrBook := tapdb.NewTapAddressBook(
addrBookDB, &tapChainParams,
)
keyRing := tap.NewLndRpcKeyRing(lndServices)
walletAnchor := tap.NewLndRpcWalletAnchor(lndServices)
chainBridge := tap.NewLndRpcChainBridge(lndServices)
addrBook := address.NewBook(address.BookConfig{
Store: tapdbAddrBook,
StoreTimeout: tapdb.DefaultStoreTimeout,
KeyRing: keyRing,
Chain: tapChainParams,
})
assetStore := tapdb.NewAssetStore(assetDB)
uniDB := tapdb.NewTransactionExecutor(
db, func(tx *sql.Tx) tapdb.BaseUniverseStore {
return db.WithTx(tx)
},
)
uniForestDB := tapdb.NewTransactionExecutor(
db, func(tx *sql.Tx) tapdb.BaseUniverseForestStore {
return db.WithTx(tx)
},
)
uniForest := tapdb.NewBaseUniverseForest(uniForestDB)
uniStatsDB := tapdb.NewTransactionExecutor(
db, func(tx *sql.Tx) tapdb.UniverseStatsStore {
return db.WithTx(tx)
},
)
universeStats := tapdb.NewUniverseStats(uniStatsDB)
headerVerifier := tapgarden.GenHeaderVerifier(
context.Background(), chainBridge,
)
uniCfg := universe.MintingArchiveConfig{
NewBaseTree: func(id universe.Identifier) universe.BaseBackend {
return tapdb.NewBaseUniverseTree(
uniDB, id,
)
},
HeaderVerifier: headerVerifier,
UniverseForest: uniForest,
UniverseStats: universeStats,
}
federationStore := tapdb.NewTransactionExecutor(db,
func(tx *sql.Tx) tapdb.UniverseServerStore {
return db.WithTx(tx)
},
)
federationDB := tapdb.NewUniverseFederationDB(federationStore)
proofFileStore, err := proof.NewFileArchiver(cfg.networkDir)
if err != nil {
return nil, fmt.Errorf("unable to open disk archive: %v", err)
}
proofArchive := proof.NewMultiArchiver(
&proof.BaseVerifier{}, tapdb.DefaultStoreTimeout,
assetStore, proofFileStore,
)
var hashMailCourier proof.Courier[proof.Recipient]
if cfg.HashMailCourier != nil {
hashMailBox, err := proof.NewHashMailBox(
cfg.HashMailCourier.Addr,
cfg.HashMailCourier.TlsCertPath,
)
if err != nil {
return nil, fmt.Errorf("unable to make mailbox: %v",
err)
}
hashMailCourier, err = proof.NewHashMailCourier(
cfg.HashMailCourier, hashMailBox, assetStore,
)
if err != nil {
return nil, fmt.Errorf("unable to make hashmail "+
"courier: %v", err)
}
}
baseUni := universe.NewMintingArchive(uniCfg)
universeSyncer := universe.NewSimpleSyncer(universe.SimpleSyncCfg{
LocalDiffEngine: baseUni,
NewRemoteDiffEngine: tap.NewRpcUniverseDiff,
LocalRegistrar: baseUni,
})
federationMembers := cfg.Universe.FederationServers
switch cfg.ChainConf.Network {
case "testnet":
cfgLogger.Infof("Configuring %v as initial Universe "+
"federation server", defaultTestnetFederationServer)
federationMembers = append(
federationMembers, defaultTestnetFederationServer,
)
}
universeFederation := universe.NewFederationEnvoy(
universe.FederationConfig{
FederationDB: federationDB,
UniverseSyncer: universeSyncer,
LocalRegistrar: baseUni,
SyncInterval: cfg.Universe.SyncInterval,
NewRemoteRegistrar: tap.NewRpcUniverseRegistar,
StaticFederationMembers: federationMembers,
ErrChan: mainErrChan,
},
)
virtualTxSigner := tap.NewLndRpcVirtualTxSigner(lndServices)
coinSelect := tapfreighter.NewCoinSelect(assetStore)
assetWallet := tapfreighter.NewAssetWallet(&tapfreighter.WalletConfig{
CoinSelector: coinSelect,
AssetProofs: proofArchive,
AddrBook: tapdbAddrBook,
KeyRing: keyRing,
Signer: virtualTxSigner,
TxValidator: &tap.ValidatorV0{},
Wallet: walletAnchor,
ChainParams: &tapChainParams,
})
return &tap.Config{
DebugLevel: cfg.DebugLevel,
AcceptRemoteUniverseProofs: cfg.Universe.AcceptRemoteProofs,
Lnd: lndServices,
ChainParams: cfg.ActiveNetParams,
AssetMinter: tapgarden.NewChainPlanter(tapgarden.PlanterConfig{
GardenKit: tapgarden.GardenKit{
Wallet: walletAnchor,
ChainBridge: chainBridge,
Log: assetMintingStore,
KeyRing: keyRing,
GenSigner: tap.NewLndRpcGenSigner(
lndServices,
),
ProofFiles: proofFileStore,
Universe: universeFederation,
},
BatchTicker: ticker.NewForce(cfg.BatchMintingInterval),
ErrChan: mainErrChan,
}),
AssetCustodian: tapgarden.NewCustodian(
&tapgarden.CustodianConfig{
ChainParams: &tapChainParams,
WalletAnchor: walletAnchor,
ChainBridge: chainBridge,
AddrBook: addrBook,
ProofArchive: proofArchive,
ProofNotifier: assetStore,
ErrChan: mainErrChan,
ProofCourier: hashMailCourier,
},
),
ChainBridge: chainBridge,
AddrBook: addrBook,
ProofArchive: proofArchive,
AssetWallet: assetWallet,
ChainPorter: tapfreighter.NewChainPorter(
&tapfreighter.ChainPorterConfig{
CoinSelector: coinSelect,
Signer: virtualTxSigner,
TxValidator: &tap.ValidatorV0{},
ExportLog: assetStore,
ChainBridge: chainBridge,
Wallet: walletAnchor,
KeyRing: keyRing,
AssetWallet: assetWallet,
AssetProofs: proofFileStore,
ProofCourier: hashMailCourier,
ErrChan: mainErrChan,
},
),
BaseUniverse: baseUni,
UniverseSyncer: universeSyncer,
UniverseFederation: universeFederation,
UniverseStats: universeStats,
LogWriter: cfg.LogWriter,
DatabaseConfig: &tap.DatabaseConfig{
RootKeyStore: tapdb.NewRootKeyStore(rksDB),
MintingStore: assetMintingStore,
AssetStore: assetStore,
TapAddrBook: tapdbAddrBook,
UniverseForest: uniForest,
FederationDB: federationDB,
},
}, nil
}
// CreateServerFromConfig creates a new Taproot Asset server from the given CLI
// config.
func CreateServerFromConfig(cfg *Config, cfgLogger btclog.Logger,
shutdownInterceptor signal.Interceptor,
mainErrChan chan<- error) (*tap.Server, error) {
// Given the config above, grab the TLS config which includes the set
// of dial options, and also the listeners we'll use to listen on the
// RPC system.
serverOpts, restDialOpts, restListen, err := getTLSConfig(
cfg, cfgLogger,
)
if err != nil {
return nil, fmt.Errorf("unable to load TLS credentials: %v",
err)
}
cfgLogger.Infof("Attempting to establish connection to lnd...")
lndConn, err := getLnd(
cfg.ChainConf.Network, cfg.Lnd, shutdownInterceptor,
)
if err != nil {
return nil, fmt.Errorf("unable to connect to lnd node: %v", err)
}
cfgLogger.Infof("lnd connection initialized")
serverCfg, err := genServerConfig(
cfg, cfgLogger, &lndConn.LndServices, mainErrChan,
)
if err != nil {
return nil, fmt.Errorf("unable to generate server config: %v",
err)
}
serverCfg.SignalInterceptor = shutdownInterceptor
serverCfg.RPCConfig = &tap.RPCConfig{
LisCfg: &lnd.ListenerCfg{},
RPCListeners: cfg.rpcListeners,
RESTListeners: cfg.restListeners,
GrpcServerOpts: serverOpts,
RestDialOpts: restDialOpts,
RestListenFunc: restListen,
WSPingInterval: cfg.RpcConf.WSPingInterval,
WSPongWait: cfg.RpcConf.WSPongWait,
RestCORS: cfg.RpcConf.RestCORS,
NoMacaroons: cfg.RpcConf.NoMacaroons,
MacaroonPath: cfg.RpcConf.MacaroonPath,
LetsEncryptDir: cfg.RpcConf.LetsEncryptDir,
LetsEncryptListen: cfg.RpcConf.LetsEncryptListen,
LetsEncryptEmail: cfg.RpcConf.LetsEncryptEmail,
LetsEncryptDomain: cfg.RpcConf.LetsEncryptDomain,
}
return tap.NewServer(serverCfg), nil
}
// CreateServerFromConfig creates a new Taproot Asset server from the given CLI
// config.
func CreateSubServerFromConfig(cfg *Config, cfgLogger btclog.Logger,
lndServices *lndclient.LndServices,
mainErrChan chan<- error) (*tap.Server, error) {
serverCfg, err := genServerConfig(
cfg, cfgLogger, lndServices, mainErrChan,
)
if err != nil {
return nil, fmt.Errorf("unable to generate server config: %v",
err)
}
serverCfg.RPCConfig = &tap.RPCConfig{
NoMacaroons: cfg.RpcConf.NoMacaroons,
MacaroonPath: cfg.RpcConf.MacaroonPath,
}
return tap.NewServer(serverCfg), nil
}