-
Notifications
You must be signed in to change notification settings - Fork 3
/
dcrtxmatcher.go
167 lines (145 loc) · 4.37 KB
/
dcrtxmatcher.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
package main
import (
"context"
"fmt"
"net"
"net/http"
"os"
//"time"
_ "net/http/pprof"
pb "github.com/decred/dcrwallet/dcrtxclient/api/matcherrpc"
//"github.com/decred/dcrwallet/dcrtxclient/util"
"github.com/gorilla/websocket"
"github.com/raedahgroup/dcrtxmatcher/coinjoin"
"github.com/raedahgroup/dcrtxmatcher/matcher"
"google.golang.org/grpc"
)
func main() {
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
// 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()
if err := run(ctx); err != nil && err != context.Canceled {
os.Exit(1)
}
}
// run executes dcrtxmatcher server. Base on setting on config file
// server uses coinshuffle++ if blind server option is enabled, if not using normal coin join method.
func run(ctx context.Context) error {
config, _, err := loadConfig(ctx)
if err != nil {
mtlog.Errorf("Can not load the config data: %v", err)
return err
}
if done(ctx) {
return ctx.Err()
}
// Within coinshuffle++, the dicemix protocol is used for the participants to exchange information.
// Dicemix uses the flint library to solve polynomial to get the roots and the peer's output address.
// The flint library is a required dependency and is the method that is suggested by the
// authors of the coinshuffle++ paper.
if config.BlindServer {
log.Infof("MinParticipants %d", config.MinParticipants)
cfg := &coinjoin.Config{JoinTicker: config.JoinTicker, RoundTimeOut: config.WaitingTimer, MinParticipants: config.MinParticipants}
cfg.ServerPublish = config.ServerPublish
diceMix := coinjoin.NewDiceMix(cfg)
joinQueue := coinjoin.NewJoinQueue(config.JoinTicker)
go diceMix.Run(joinQueue)
// Create websocket server
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{
ReadBufferSize: 0,
WriteBufferSize: 0,
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
mtlog.Errorf("Can not upgrade from remote address %v", r.RemoteAddr)
return
}
// Add websocket connection to peer data.
peer := coinjoin.NewPeer(conn)
peer.IPAddr = r.RemoteAddr
joinQueue.NewPeerChan <- peer
})
if done(ctx) {
return ctx.Err()
}
intf := fmt.Sprintf(":%d", config.Port)
log.Infof("Listening on %s", intf)
go func() {
err := http.ListenAndServe(intf, nil)
if err != nil {
log.Errorf("Can not start server: %v", err)
os.Exit(1)
}
}()
}
// With normal coin join method, the server knows the output addresses of all participants.
// After received transaction input and output from participants,
// Server swaps randomly the transaction input, output index of all participants.
// The joined transaction created from this then sent back to each participants to sign.
if !config.BlindServer {
mcfg := &matcher.Config{
MinParticipants: config.MinParticipants,
RandomIndex: config.RandomIndex,
JoinTicker: config.JoinTicker,
WaitingTimer: config.WaitingTimer,
}
ticketJoiner := matcher.NewTicketJoiner(mcfg)
joinQueue := matcher.NewJoinQueue()
intf := fmt.Sprintf(":%d", config.Port)
lis, err := net.Listen("tcp", intf)
if err != nil {
mtlog.Errorf("Error listening: %v", err)
return err
}
if done(ctx) {
return ctx.Err()
}
go ticketJoiner.Run(joinQueue)
server := grpc.NewServer()
pb.RegisterSplitTxMatcherServiceServer(server, NewSplitTxMatcherService(ticketJoiner, joinQueue))
if done(ctx) {
return ctx.Err()
}
mtlog.Infof("Listening on %s", intf)
go func() {
err := server.Serve(lis)
if err != nil {
mtlog.Errorf("Can not start server: %v", err)
os.Exit(1)
}
}()
if server != nil {
defer func() {
mtlog.Info("Stop Grpc server...")
server.Stop()
mtlog.Info("Grpc server stops")
}()
}
if ticketJoiner != nil {
defer func() {
mtlog.Info("Stop Ticket joiner...")
ticketJoiner.Stop(config.CompleteJoin)
mtlog.Info("Ticket joiner stops")
}()
}
}
// Wait until shutdown is signaled before returning and running deferred
// shutdown tasks.
<-ctx.Done()
return ctx.Err()
}
// 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
}
}