-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
386 lines (347 loc) · 10.6 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
PASL - Personalized Accounts & Secure Ledger
Copyright (C) 2018 PASL Project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/modern-go/concurrent"
"github.com/urfave/cli"
"github.com/pasl-project/pasl/api"
"github.com/pasl-project/pasl/blockchain"
"github.com/pasl-project/pasl/crypto"
"github.com/pasl-project/pasl/defaults"
"github.com/pasl-project/pasl/network"
"github.com/pasl-project/pasl/network/pasl"
"github.com/pasl-project/pasl/safebox"
"github.com/pasl-project/pasl/storage"
"github.com/pasl-project/pasl/utils"
"github.com/pasl-project/pasl/wallet"
)
func exportMain(ctx *cli.Context) error {
return cli.ShowSubcommandHelp(ctx)
}
func exportSafebox(ctx *cli.Context) error {
return withBlockchain(ctx, func(blockchain *blockchain.Blockchain, _ storage.Storage) error {
blob := blockchain.ExportSafebox()
fmt.Fprint(ctx.App.Writer, hex.EncodeToString(blob))
return nil
})
}
var heightFlagValue uint
var heightFlag = cli.UintFlag{
Name: "height",
Usage: "Rescan blockchain and recover safebox at specific height",
Destination: &heightFlagValue,
}
var exportCommand = cli.Command{
Action: exportMain,
Name: "export",
Usage: "Export blockchain data",
Description: "",
Subcommands: []cli.Command{
{
Action: exportSafebox,
Name: "safebox",
Usage: "Export safebox contents",
Description: "",
Flags: []cli.Flag{
heightFlag,
},
},
},
}
func getMain(ctx *cli.Context) error {
return cli.ShowSubcommandHelp(ctx)
}
func getBlock(ctx *cli.Context) error {
if !ctx.Args().Present() {
return errors.New("invalid block index")
}
return withBlockchain(ctx, func(_ *blockchain.Blockchain, s storage.Storage) error {
index, err := strconv.ParseUint(ctx.Args().First(), 10, 32)
if err != nil {
return err
}
data, err := s.GetBlock(uint32(index))
if err != nil {
return err
}
fmt.Fprintf(ctx.App.Writer, "%x\n", data)
return nil
})
}
func getHeight(ctx *cli.Context) error {
return withBlockchain(ctx, func(blockchain *blockchain.Blockchain, _ storage.Storage) error {
height := blockchain.GetHeight()
fmt.Fprintf(ctx.App.Writer, "%d\n", height)
return nil
})
}
var getCommand = cli.Command{
Action: getMain,
Name: "get",
Usage: "Get blockchain info",
Description: "",
Subcommands: []cli.Command{
{
Action: getHeight,
Name: "height",
Usage: "Get current height",
Description: "",
},
{
Action: getBlock,
Name: "block",
Usage: "Get raw block data",
Description: "",
},
},
}
var p2pPortFlag = cli.UintFlag{
Name: "p2p-bind-port",
Usage: "P2P bind port",
Value: uint(defaults.P2PPort),
}
var rpcIPFlag = cli.StringFlag{
Name: "rpc-bind-ip",
Usage: "RPC bind ip",
Value: defaults.RPCBindHost,
}
var dataDirFlag = cli.StringFlag{
Name: "data-dir",
Usage: "Directory to store blockchain files",
}
var exclusiveNodesFlag = cli.StringFlag{
Name: "exclusive-nodes",
Usage: "Comma-separated ip:port list of exclusive nodes to connect to",
}
var walletFileFlag = cli.StringFlag{
Name: "wallet-file",
Usage: "File to store encrypted wallet keys",
Value: "",
}
var passwordFlag = cli.StringFlag{
Name: "password",
Usage: "Password to decrypt wallet keys",
Value: "",
}
func initWallet(ctx *cli.Context, coreRPCAddress string) (*wallet.Wallet, error) {
dataDir, err := getDataDir(ctx, false)
if err != nil {
return nil, err
}
filename := ctx.GlobalString(walletFileFlag.GetName())
if filename == "" {
filename = filepath.Join(dataDir, "wallet.json")
}
file, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, fmt.Errorf("failed to open wallet file '%v': %v", filename, err)
}
contents, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
set := func(contents []byte) error {
if err := file.Truncate(0); err != nil {
return err
}
written, err := file.WriteAt(contents, 0)
if err != nil {
return err
}
if written != len(contents) {
return fmt.Errorf("incomplete write")
}
return nil
}
return wallet.NewWallet(contents, []byte(ctx.GlobalString(passwordFlag.GetName())), set, coreRPCAddress)
}
func getDataDir(ctx *cli.Context, create bool) (string, error) {
dataDir := ctx.GlobalString(dataDirFlag.GetName())
if dataDir == "" {
var err error
if dataDir, err = utils.GetDataDir(); err != nil {
return "", fmt.Errorf("Failed to obtain valid data directory path. Use %s flag to manually specify data directory location. Error: %v", dataDirFlag.GetName(), err)
}
}
if create {
if err := utils.CreateDirectory(&dataDir); err != nil {
return "", fmt.Errorf("Failed to create data directory %v", err)
}
}
return dataDir, nil
}
func withBlockchain(ctx *cli.Context, fn func(blockchain *blockchain.Blockchain, storage storage.Storage) error) error {
dataDir, err := getDataDir(ctx, true)
if err != nil {
return err
}
dbFileName := filepath.Join(dataDir, "storage.db")
err = storage.WithStorage(&dbFileName, func(storage storage.Storage) (err error) {
var blockchainInstance *blockchain.Blockchain
if ctx.IsSet(heightFlag.GetName()) {
var height uint32
height = uint32(heightFlagValue)
blockchainInstance, err = blockchain.NewBlockchain(safebox.NewSafebox, storage, &height)
} else {
blockchainInstance, err = blockchain.NewBlockchain(safebox.NewSafebox, storage, nil)
}
if err != nil {
return err
}
return fn(blockchainInstance, storage)
})
if err != nil {
return err
}
return nil
}
type SignalCancel struct{}
func (SignalCancel) String() string {
return "Cancal"
}
func (SignalCancel) Signal() {
}
func run(cliContext *cli.Context) error {
utils.Ftracef(cliContext.App.Writer, defaults.UserAgent)
utils.Ftracef(cliContext.App.Writer, "Loading blockchain")
return withBlockchain(cliContext, func(blockchain *blockchain.Blockchain, s storage.Storage) error {
height, safeboxHash, cumulativeDifficulty := blockchain.GetState()
utils.Ftracef(cliContext.App.Writer, "Blockchain loaded, height %d safeboxHash %s cumulativeDifficulty %s", height, hex.EncodeToString(safeboxHash), cumulativeDifficulty.String())
p2pPort := uint16(cliContext.GlobalUint(p2pPortFlag.GetName()))
config := network.Config{
ListenAddr: fmt.Sprintf("%s:%d", defaults.P2PBindAddress, p2pPort),
MaxIncoming: defaults.MaxIncoming,
MaxOutgoing: defaults.MaxOutgoing,
TimeoutConnect: defaults.TimeoutConnect,
}
key, err := crypto.NewKeyByType(crypto.NIDsecp256k1)
if err != nil {
return err
}
nonce := utils.Serialize(key.Public)
peers := network.NewPeersList()
peerUpdates := make(chan network.PeerInfo)
return pasl.WithManager(nonce, blockchain, p2pPort, peers, peerUpdates, blockchain.BlocksUpdates, blockchain.TxPoolUpdates, defaults.TimeoutRequest, func(manager *pasl.Manager) error {
return network.WithNode(config, peers, peerUpdates, manager.OnNewConnection, func(node network.Node) error {
cancel := make(chan os.Signal, 2)
coreRPC := api.NewApi(blockchain)
RPCBindAddress := fmt.Sprintf("%s:%d", cliContext.GlobalString(rpcIPFlag.GetName()), defaults.RPCPort)
wallet, err := initWallet(cliContext, RPCBindAddress)
if err != nil {
return fmt.Errorf("failed to initialize wallet: %v", err)
}
defer wallet.Close()
ln, err := net.Listen("tcp", "127.0.0.1:8100")
if err != nil {
return fmt.Errorf("failed to bind Web UI port: %v", err)
}
defer ln.Close()
go func() {
utils.Ftracef(cliContext.App.Writer, fmt.Sprintf("Web UI is available at http://%s", ln.Addr().String()))
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(AssetFile()))
// TODO: handle error
http.Serve(ln, mux)
}()
if cliContext.IsSet(exclusiveNodesFlag.GetName()) {
for _, hostPort := range strings.Split(cliContext.String(exclusiveNodesFlag.GetName()), ",") {
if err = node.AddPeer(hostPort); err != nil {
utils.Ftracef(cliContext.App.Writer, "Failed to add bootstrap peer %s: %v", hostPort, err)
}
}
} else {
populatePeers := concurrent.NewUnboundedExecutor()
populatePeers.Go(func(ctx context.Context) {
s.LoadPeers(func(address []byte, data []byte) {
if err = node.AddPeerSerialized(data); err != nil {
utils.Ftracef(cliContext.App.Writer, "Failed to load peer data: %v", err)
}
})
for _, hostPort := range strings.Split(defaults.BootstrapNodes, ",") {
if err = node.AddPeer(hostPort); err != nil {
utils.Ftracef(cliContext.App.Writer, "Failed to add bootstrap peer %s: %v", hostPort, err)
}
}
})
defer populatePeers.StopAndWaitForever()
defer func() {
peers := node.GetPeersByNetwork("tcp")
s.WithWritable(func(s storage.StorageWritable, ctx interface{}) error {
return s.StorePeers(ctx, func(fn func(address []byte, data []byte)) {
for address := range peers {
fn([]byte(address), utils.Serialize(peers[address]))
}
})
})
}()
}
RPCHandlers := coreRPC.GetHandlers()
for k, v := range wallet.GetHandlers() {
RPCHandlers[k] = v
}
return network.WithRpcServer(RPCBindAddress, RPCHandlers, func() error {
signal.Notify(cancel, os.Interrupt, syscall.SIGTERM)
<-cancel
utils.Ftracef(cliContext.App.Writer, "Exit signal received. Terminating...")
return nil
})
})
})
})
}
func main() {
rand.Seed(time.Now().UnixNano())
app := cli.NewApp()
app.Usage = "PASL command line interface"
app.Version = defaults.UserAgent
app.Action = run
app.Commands = []cli.Command{
exportCommand,
getCommand,
}
app.Flags = []cli.Flag{
dataDirFlag,
exclusiveNodesFlag,
heightFlag,
p2pPortFlag,
rpcIPFlag,
walletFileFlag,
passwordFlag,
}
app.CommandNotFound = func(c *cli.Context, command string) {
cli.ShowAppHelp(c)
os.Exit(1)
}
if err := app.Run(os.Args); err != nil {
utils.Panicf("Error running application: %v", err)
os.Exit(2)
}
os.Exit(0)
}