-
Notifications
You must be signed in to change notification settings - Fork 3
/
tumblebit.go
141 lines (120 loc) · 3.19 KB
/
tumblebit.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
// Copyright (c) 2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"context"
"os"
"runtime"
"github.com/decred/tumblebit/rpc/rpcserver"
"github.com/decred/tumblebit/tumbler"
"github.com/decred/tumblebit/version"
"github.com/decred/tumblebit/wallet"
)
var (
cfg *config
)
func main() {
// Create a context that is cancelled when a shutdown request is received
// through an interrupt signal or an RPC request.
ctx := withShutdownCancel(context.Background())
go shutdownListener()
// Run the server until permanent failure or shutdown is requested.
if err := run(ctx); err != nil && err != context.Canceled {
os.Exit(1)
}
}
// done returns whether the context's Done channel was closed due to
// cancellation or exceeded deadline.
func done(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}
// run is the main startup and teardown logic performed by the main package.
func run(ctx context.Context) error {
// Load configuration and parse command line. This function also
// initializes logging and configures it accordingly.
tcfg, _, err := loadConfig(ctx)
if err != nil {
return err
}
cfg = tcfg
defer func() {
if logRotator != nil {
logRotator.Close()
}
}()
// Show version at startup.
log.Infof("Version %s (Go version %s)", version.String(), runtime.Version())
if done(ctx) {
return ctx.Err()
}
// Connect to the wallet RPC service
walletClient, err := startRPCClient(ctx)
if err != nil {
log.Errorf("Unable to connect to the wallet service: %v", err)
return err
}
defer walletClient.Close()
if done(ctx) {
return ctx.Err()
}
walletCfg := wallet.Config{
Account: cfg.Account,
AccountName: cfg.AccountName,
ChainParams: activeNet.Params,
WalletConnection: walletClient,
WalletPassword: cfg.WalletPassword,
}
// Create a wallet communication object
w, err := wallet.New(ctx, &walletCfg)
if err != nil {
log.Errorf("Failed to communicate with the wallet: %v", err)
return err
}
if done(ctx) {
return ctx.Err()
}
tumblerCfg := tumbler.Config{
ChainParams: activeNet.Params,
EpochDuration: cfg.EpochDuration,
EpochRenewal: cfg.EpochRenewal,
PuzzleDifficulty: cfg.PuzzleDifficulty,
Wallet: w,
}
// Create and start the RPC server to serve client connections.
tumblerServer, err := startRPCServer()
if err != nil {
log.Errorf("Unable to create a Tumbler server: %v", err)
return err
}
tb := tumbler.NewTumbler(&tumblerCfg)
if tumblerServer != nil {
// Start tumbler gRPC services.
rpcserver.StartTumblerService(tumblerServer, tb)
defer func() {
log.Warn("Stopping gRPC server...")
tumblerServer.Stop()
log.Info("gRPC server shutdown")
}()
}
// Fire up the TumbleBit server
err = tb.Run(ctx)
switch err {
case nil:
log.Info("TumbleBit service stopped")
case context.Canceled:
log.Error("TumbleBit service cancelled")
default:
log.Errorf("Failed to setup a TumbleBit service: %v", err)
return err
}
// Wait until shutdown is signaled before returning and running deferred
// shutdown tasks.
<-ctx.Done()
return ctx.Err()
}