forked from codeaudit/suncoin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
suncoin.go
880 lines (779 loc) · 25.1 KB
/
suncoin.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"runtime/pprof"
"sync"
"syscall"
"time"
"github.com/skycoin/skycoin/src/api/webrpc"
"github.com/skycoin/skycoin/src/cipher"
"github.com/skycoin/skycoin/src/coin"
"github.com/skycoin/skycoin/src/daemon"
"github.com/skycoin/skycoin/src/gui"
"github.com/skycoin/skycoin/src/util/browser"
"github.com/skycoin/skycoin/src/util/cert"
"github.com/skycoin/skycoin/src/util/file"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/skycoin/skycoin/src/visor"
)
var (
// Version node version which will be set when build wallet by LDFLAGS
Version = "0.21.1"
// Commit id
Commit = ""
help = false
logger = logging.MustGetLogger("main")
logFormat = "[suncoin.%{module}:%{level}] %{message}"
logModules = []string{
"main",
"daemon",
"coin",
"gui",
"util",
"visor",
"wallet",
"gnet",
"pex",
"webrpc",
}
//TODO: Move time and other genesis block settigns from visor, to here
GenesisSignatureStr = "3a2c8762df667edb5aa0cda6db52c36d490951bb35ff27ade65e76963f2bb7170be73e85474c45784cab2acbd9dbe1853d073e954badf8a395e9db7cb3261d1700"
GenesisAddressStr = "5L1jvbwtGS8eL3afA2gsqTBc8KEPFDDRjZ"
BlockchainPubkeyStr = "0255434580f86e14a26e1d5c59b0626dfa28003741c475155aeedaa92af797d043"
BlockchainSeckeyStr = ""
GenesisTimestamp uint64 = 1494861716
GenesisCoinVolume uint64 = 300e12
//GenesisTimestamp: 1426562704,
//GenesisCoinVolume: 100e12, //100e6 * 10e6
DefaultConnections = []string{
"116.62.220.158:7200",
"119.23.23.184:7200",
}
)
// Command line interface arguments
type Config struct {
// Disable peer exchange
DisablePEX bool
// Download peer list
DownloadPeerList bool
// Download the peers list from this URL
PeerListURL string
// Don't make any outgoing connections
DisableOutgoingConnections bool
// Don't allowing incoming connections
DisableIncomingConnections bool
// Disables networking altogether
DisableNetworking bool
// Only run on localhost and only connect to others on localhost
LocalhostOnly bool
// Which address to serve on. Leave blank to automatically assign to a
// public interface
Address string
//gnet uses this for TCP incoming and outgoing
Port int
//max outgoing connections to maintain
MaxOutgoingConnections int
// How often to make outgoing connections
OutgoingConnectionsRate time.Duration
// PeerlistSize represents the maximum number of peers that the pex would maintain
PeerlistSize int
// Wallet Address Version
//AddressVersion string
// Remote web interface
WebInterface bool
WebInterfacePort int
WebInterfaceAddr string
WebInterfaceCert string
WebInterfaceKey string
WebInterfaceHTTPS bool
RPCInterface bool
RPCInterfacePort int
RPCInterfaceAddr string
// Launch System Default Browser after client startup
LaunchBrowser bool
// If true, print the configured client web interface address and exit
PrintWebInterfaceAddress bool
// Data directory holds app data -- defaults to ~/.suncoin
DataDirectory string
// GUI directory contains assets for the html gui
GUIDirectory string
// Logging
ColorLog bool
// This is the value registered with flag, it is converted to LogLevel after parsing
LogLevel string
DisablePingPong bool
// Wallets
// Defaults to ${DataDirectory}/wallets/
WalletDirectory string
RunMaster bool
GenesisSignature cipher.Sig
GenesisTimestamp uint64
GenesisAddress cipher.Address
BlockchainPubkey cipher.PubKey
BlockchainSeckey cipher.SecKey
/* Developer options */
// Enable cpu profiling
ProfileCPU bool
// Where the file is written to
ProfileCPUFile string
// HTTP profiling interface (see http://golang.org/pkg/net/http/pprof/)
HTTPProf bool
// Will force it to connect to this ip:port, instead of waiting for it
// to show up as a peer
ConnectTo string
DBPath string
Arbitrating bool
RPCThreadNum uint // rpc number
Logtofile bool
Logtogui bool
LogBuffSize int
}
func (c *Config) register() {
flag.BoolVar(&help, "help", false, "Show help")
flag.BoolVar(&c.DisablePEX, "disable-pex", c.DisablePEX,
"disable PEX peer discovery")
flag.BoolVar(&c.DownloadPeerList, "download-peerlist", c.DownloadPeerList, "download a peers.txt from -peerlist-url")
flag.StringVar(&c.PeerListURL, "peerlist-url", c.PeerListURL, "with -download-peerlist=true, download a peers.txt file from this url")
flag.BoolVar(&c.DisableOutgoingConnections, "disable-outgoing",
c.DisableOutgoingConnections, "Don't make outgoing connections")
flag.BoolVar(&c.DisableIncomingConnections, "disable-incoming",
c.DisableIncomingConnections, "Don't make incoming connections")
flag.BoolVar(&c.DisableNetworking, "disable-networking", c.DisableNetworking, "Disable all network activity")
flag.StringVar(&c.Address, "address", c.Address,
"IP Address to run application on. Leave empty to default to a public interface")
flag.IntVar(&c.Port, "port", c.Port, "Port to run application on")
flag.BoolVar(&c.WebInterface, "web-interface", c.WebInterface,
"enable the web interface")
flag.IntVar(&c.WebInterfacePort, "web-interface-port",
c.WebInterfacePort, "port to serve web interface on")
flag.StringVar(&c.WebInterfaceAddr, "web-interface-addr",
c.WebInterfaceAddr, "addr to serve web interface on")
flag.StringVar(&c.WebInterfaceCert, "web-interface-cert",
c.WebInterfaceCert, "cert.pem file for web interface HTTPS. "+
"If not provided, will use cert.pem in -data-directory")
flag.StringVar(&c.WebInterfaceKey, "web-interface-key",
c.WebInterfaceKey, "key.pem file for web interface HTTPS. "+
"If not provided, will use key.pem in -data-directory")
flag.BoolVar(&c.WebInterfaceHTTPS, "web-interface-https",
c.WebInterfaceHTTPS, "enable HTTPS for web interface")
flag.BoolVar(&c.RPCInterface, "rpc-interface", c.RPCInterface,
"enable the rpc interface")
flag.IntVar(&c.RPCInterfacePort, "rpc-interface-port", c.RPCInterfacePort,
"port to serve rpc interface on")
flag.StringVar(&c.RPCInterfaceAddr, "rpc-interface-addr", c.RPCInterfaceAddr,
"addr to serve rpc interface on")
flag.UintVar(&c.RPCThreadNum, "rpc-thread-num", 5, "rpc thread number")
flag.BoolVar(&c.LaunchBrowser, "launch-browser", c.LaunchBrowser,
"launch system default webbrowser at client startup")
flag.BoolVar(&c.PrintWebInterfaceAddress, "print-web-interface-address",
c.PrintWebInterfaceAddress, "print configured web interface address and exit")
flag.StringVar(&c.DataDirectory, "data-dir", c.DataDirectory,
"directory to store app data (defaults to ~/.suncoin)")
flag.StringVar(&c.ConnectTo, "connect-to", c.ConnectTo,
"connect to this ip only")
flag.BoolVar(&c.ProfileCPU, "profile-cpu", c.ProfileCPU,
"enable cpu profiling")
flag.StringVar(&c.ProfileCPUFile, "profile-cpu-file",
c.ProfileCPUFile, "where to write the cpu profile file")
flag.BoolVar(&c.HTTPProf, "http-prof", c.HTTPProf,
"Run the http profiling interface")
flag.StringVar(&c.LogLevel, "log-level", c.LogLevel,
"Choices are: debug, info, notice, warning, error, critical")
flag.BoolVar(&c.ColorLog, "color-log", c.ColorLog,
"Add terminal colors to log output")
flag.BoolVar(&c.DisablePingPong, "no-ping-log", false,
`disable "reply to ping" and "received pong" log messages`)
flag.BoolVar(&c.Logtofile, "logtofile", false, "log to file")
flag.StringVar(&c.GUIDirectory, "gui-dir", c.GUIDirectory,
"static content directory for the html gui")
//Key Configuration Data
flag.BoolVar(&c.RunMaster, "master", c.RunMaster,
"run the daemon as blockchain master server")
flag.StringVar(&BlockchainPubkeyStr, "master-public-key", BlockchainPubkeyStr,
"public key of the master chain")
flag.StringVar(&BlockchainSeckeyStr, "master-secret-key", BlockchainSeckeyStr,
"secret key, set for master")
flag.StringVar(&GenesisAddressStr, "genesis-address", GenesisAddressStr,
"genesis address")
flag.StringVar(&GenesisSignatureStr, "genesis-signature", GenesisSignatureStr,
"genesis block signature")
flag.Uint64Var(&c.GenesisTimestamp, "genesis-timestamp", c.GenesisTimestamp,
"genesis block timestamp")
flag.StringVar(&c.WalletDirectory, "wallet-dir", c.WalletDirectory,
"location of the wallet files. Defaults to ~/.suncoin/wallet/")
flag.IntVar(&c.MaxOutgoingConnections, "max-outgoing-connections", 16, "The maximum outgoing connections allowed")
flag.IntVar(&c.PeerlistSize, "peerlist-size", 65535, "The peer list size")
flag.DurationVar(&c.OutgoingConnectionsRate, "connection-rate",
c.OutgoingConnectionsRate, "How often to make an outgoing connection")
flag.BoolVar(&c.LocalhostOnly, "localhost-only", c.LocalhostOnly,
"Run on localhost and only connect to localhost peers")
flag.BoolVar(&c.Arbitrating, "arbitrating", c.Arbitrating, "Run node in arbitrating mode")
flag.BoolVar(&c.Logtogui, "logtogui", true, "log to gui")
flag.IntVar(&c.LogBuffSize, "logbufsize", c.LogBuffSize, "Log size saved in memeory for gui show")
}
var devConfig Config = Config{
// Disable peer exchange
DisablePEX: false,
// Don't make any outgoing connections
DisableOutgoingConnections: false,
// Don't allowing incoming connections
DisableIncomingConnections: false,
// Disables networking altogether
DisableNetworking: false,
// Only run on localhost and only connect to others on localhost
LocalhostOnly: false,
// Which address to serve on. Leave blank to automatically assign to a
// public interface
Address: "",
//gnet uses this for TCP incoming and outgoing
Port: 7200,
// MaxOutgoingConnections is the maximum outgoing connections allowed.
MaxOutgoingConnections: 16,
DownloadPeerList: false,
PeerListURL: "",
// How often to make outgoing connections, in seconds
OutgoingConnectionsRate: time.Second * 5,
PeerlistSize: 65535,
// Wallet Address Version
//AddressVersion: "test",
// Remote web interface
WebInterface: true,
WebInterfacePort: 7620,
WebInterfaceAddr: "127.0.0.1",
WebInterfaceCert: "",
WebInterfaceKey: "",
WebInterfaceHTTPS: false,
PrintWebInterfaceAddress: false,
RPCInterface: true,
RPCInterfacePort: 7630,
RPCInterfaceAddr: "127.0.0.1",
RPCThreadNum: 5,
LaunchBrowser: true,
// Data directory holds app data -- defaults to ~/.suncoin
DataDirectory: ".suncoin",
// Web GUI static resources
GUIDirectory: "./src/gui/static/",
// Logging
ColorLog: true,
LogLevel: "DEBUG",
// Wallets
WalletDirectory: "",
// Centralized network configuration
RunMaster: false,
BlockchainPubkey: cipher.PubKey{},
BlockchainSeckey: cipher.SecKey{},
GenesisAddress: cipher.Address{},
GenesisTimestamp: GenesisTimestamp,
GenesisSignature: cipher.Sig{},
/* Developer options */
// Enable cpu profiling
ProfileCPU: false,
// Where the file is written to
ProfileCPUFile: "suncoin.prof",
// HTTP profiling interface (see http://golang.org/pkg/net/http/pprof/)
HTTPProf: false,
// Will force it to connect to this ip:port, instead of waiting for it
// to show up as a peer
ConnectTo: "",
LogBuffSize: 8388608, //1024*1024*8
}
func (c *Config) Parse() {
c.register()
flag.Parse()
if help {
flag.Usage()
os.Exit(0)
}
c.postProcess()
}
func (c *Config) postProcess() {
var err error
if GenesisSignatureStr != "" {
c.GenesisSignature, err = cipher.SigFromHex(GenesisSignatureStr)
panicIfError(err, "Invalid Signature")
}
if GenesisAddressStr != "" {
c.GenesisAddress, err = cipher.DecodeBase58Address(GenesisAddressStr)
panicIfError(err, "Invalid Address")
}
if BlockchainPubkeyStr != "" {
c.BlockchainPubkey, err = cipher.PubKeyFromHex(BlockchainPubkeyStr)
panicIfError(err, "Invalid Pubkey")
}
if BlockchainSeckeyStr != "" {
c.BlockchainSeckey, err = cipher.SecKeyFromHex(BlockchainSeckeyStr)
panicIfError(err, "Invalid Seckey")
BlockchainSeckeyStr = ""
}
if BlockchainSeckeyStr != "" {
c.BlockchainSeckey = cipher.SecKey{}
}
c.DataDirectory, err = file.InitDataDir(c.DataDirectory)
panicIfError(err, "Invalid DataDirectory")
if c.WebInterfaceCert == "" {
c.WebInterfaceCert = filepath.Join(c.DataDirectory, "cert.pem")
}
if c.WebInterfaceKey == "" {
c.WebInterfaceKey = filepath.Join(c.DataDirectory, "key.pem")
}
if c.WalletDirectory == "" {
c.WalletDirectory = filepath.Join(c.DataDirectory, "wallets/")
}
if c.DBPath == "" {
c.DBPath = filepath.Join(c.DataDirectory, "data.db")
}
}
func panicIfError(err error, msg string, args ...interface{}) {
if err != nil {
log.Panicf(msg+": %v", append(args, err)...)
}
}
func printProgramStatus() {
fn := "goroutine.prof"
logger.Debug("Writing goroutine profile to %s", fn)
p := pprof.Lookup("goroutine")
f, err := os.Create(fn)
defer f.Close()
if err != nil {
logger.Error("%v", err)
return
}
err = p.WriteTo(f, 2)
if err != nil {
logger.Error("%v", err)
return
}
}
func catchInterrupt(quit chan<- struct{}) {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
<-sigchan
signal.Stop(sigchan)
close(quit)
}
// Catches SIGUSR1 and prints internal program state
func catchDebug() {
sigchan := make(chan os.Signal, 1)
//signal.Notify(sigchan, syscall.SIGUSR1)
signal.Notify(sigchan, syscall.Signal(0xa)) // SIGUSR1 = Signal(0xa)
for {
select {
case <-sigchan:
printProgramStatus()
}
}
}
// init logging settings
func initLogging(dataDir string, level string, color, logtofile, logtogui bool, logbuf *bytes.Buffer) (func(), error) {
logCfg := logging.DevLogConfig(logModules)
logCfg.Format = logFormat
logCfg.Colors = color
logCfg.Level = level
var fd *os.File
if logtofile {
logDir := filepath.Join(dataDir, "logs")
if err := createDirIfNotExist(logDir); err != nil {
log.Println("initial logs folder failed", err)
return nil, fmt.Errorf("init log folder fail, %v", err)
}
// open log file
tf := "2006-01-02-030405"
logfile := filepath.Join(logDir,
fmt.Sprintf("%s-v%s.log", time.Now().Format(tf), Version))
var err error
fd, err = os.OpenFile(logfile, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return nil, err
}
if logtogui {
logCfg.Output = io.MultiWriter(os.Stdout, fd, logbuf)
} else {
logCfg.Output = io.MultiWriter(os.Stdout, fd)
}
} else {
if logtogui {
logCfg.Output = io.MultiWriter(os.Stdout, logbuf)
}
}
logCfg.InitLogger()
return func() {
logger.Info("Log file closed")
if fd != nil {
fd.Close()
}
}, nil
}
func initProfiling(httpProf, profileCPU bool, profileCPUFile string) {
if profileCPU {
f, err := os.Create(profileCPUFile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if httpProf {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
}
func configureDaemon(c *Config) daemon.Config {
//cipher.SetAddressVersion(c.AddressVersion)
dc := daemon.NewConfig()
dc.Pex.DataDirectory = c.DataDirectory
dc.Pex.Disabled = c.DisablePEX
dc.Pex.Port = c.Port
dc.Pex.Max = c.PeerlistSize
dc.Pex.DownloadPeerList = c.DownloadPeerList
dc.Pex.PeerListURL = c.PeerListURL
dc.Daemon.DisableOutgoingConnections = c.DisableOutgoingConnections
dc.Daemon.DisableIncomingConnections = c.DisableIncomingConnections
dc.Daemon.DisableNetworking = c.DisableNetworking
dc.Daemon.Port = c.Port
dc.Daemon.Address = c.Address
dc.Daemon.LocalhostOnly = c.LocalhostOnly
dc.Daemon.OutgoingMax = c.MaxOutgoingConnections
dc.Daemon.DataDirectory = c.DataDirectory
dc.Daemon.LogPings = !c.DisablePingPong
if c.OutgoingConnectionsRate == 0 {
c.OutgoingConnectionsRate = time.Millisecond
}
dc.Daemon.OutgoingRate = c.OutgoingConnectionsRate
dc.Visor.Config.IsMaster = c.RunMaster
dc.Visor.Config.BlockchainPubkey = c.BlockchainPubkey
dc.Visor.Config.BlockchainSeckey = c.BlockchainSeckey
dc.Visor.Config.GenesisAddress = c.GenesisAddress
dc.Visor.Config.GenesisSignature = c.GenesisSignature
dc.Visor.Config.GenesisTimestamp = c.GenesisTimestamp
dc.Visor.Config.GenesisCoinVolume = GenesisCoinVolume
dc.Visor.Config.DBPath = c.DBPath
dc.Visor.Config.Arbitrating = c.Arbitrating
dc.Visor.Config.WalletDirectory = c.WalletDirectory
dc.Visor.Config.BuildInfo = visor.BuildInfo{
Version: Version,
Commit: Commit,
}
return dc
}
// Run starts the suncoin node
func Run(c *Config) {
defer func() {
// try catch panic in main thread
if r := recover(); r != nil {
logger.Error("recover: %v\nstack:%v", r, string(debug.Stack()))
}
}()
c.GUIDirectory = file.ResolveResourceDirectory(c.GUIDirectory)
scheme := "http"
if c.WebInterfaceHTTPS {
scheme = "https"
}
host := fmt.Sprintf("%s:%d", c.WebInterfaceAddr, c.WebInterfacePort)
fullAddress := fmt.Sprintf("%s://%s", scheme, host)
logger.Critical("Full address: %s", fullAddress)
if c.PrintWebInterfaceAddress {
fmt.Println(fullAddress)
return
}
initProfiling(c.HTTPProf, c.ProfileCPU, c.ProfileCPUFile)
var wg sync.WaitGroup
// If the user Ctrl-C's, shutdown properly
quit := make(chan struct{})
wg.Add(1)
go func() {
defer wg.Done()
catchInterrupt(quit)
}()
// Watch for SIGUSR1
wg.Add(1)
func() {
defer wg.Done()
go catchDebug()
}()
// creates blockchain instance
dconf := configureDaemon(c)
db, err := visor.OpenDB(dconf.Visor.Config.DBPath)
if err != nil {
logger.Error("Database failed to open: %v. Is another suncoin instance running?", err)
return
}
d, err := daemon.NewDaemon(dconf, db, DefaultConnections)
if err != nil {
logger.Error("%v", err)
return
}
closelog, err := initLogging(c.DataDirectory, c.LogLevel, c.ColorLog, c.Logtofile, c.Logtogui, &d.LogBuff)
if err != nil {
fmt.Println(err)
return
}
if c.Logtogui {
go func(buf *bytes.Buffer, quit chan struct{}) {
for {
select {
case <-quit:
logger.Info("Logbuff service closed normally")
return
case <-time.After(1 * time.Second): //insure logbuff size not exceed required size, like lru
for buf.Len() > c.LogBuffSize {
_, err := buf.ReadString(byte('\n')) //discard one line
if err != nil {
continue
}
}
}
}
}(&d.LogBuff, quit)
}
errC := make(chan error, 1)
wg.Add(1)
go func() {
defer wg.Done()
d.Run()
}()
var rpc *webrpc.WebRPC
// start the webrpc
if c.RPCInterface {
rpcAddr := fmt.Sprintf("%v:%v", c.RPCInterfaceAddr, c.RPCInterfacePort)
rpc, err = webrpc.New(rpcAddr, d.Gateway)
if err != nil {
logger.Error("%v", err)
return
}
rpc.ChanBuffSize = 1000
rpc.WorkerNum = c.RPCThreadNum
wg.Add(1)
go func() {
defer wg.Done()
if err := rpc.Run(); err != nil {
errC <- err
}
}()
}
// Debug only - forces connection on start. Violates thread safety.
if c.ConnectTo != "" {
if err := d.Pool.Pool.Connect(c.ConnectTo); err != nil {
logger.Error("Force connect %s failed, %v", c.ConnectTo, err)
return
}
}
if c.WebInterface {
var err error
if c.WebInterfaceHTTPS {
// Verify cert/key parameters, and if neither exist, create them
errs := cert.CreateCertIfNotExists(host, c.WebInterfaceCert, c.WebInterfaceKey, "Suncoind")
if len(errs) != 0 {
for _, err := range errs {
logger.Error(err.Error())
}
logger.Error("gui.CreateCertIfNotExists failure")
return
}
err = gui.LaunchWebInterfaceHTTPS(host, c.GUIDirectory, d, c.WebInterfaceCert, c.WebInterfaceKey)
} else {
err = gui.LaunchWebInterface(host, c.GUIDirectory, d)
}
if err != nil {
logger.Error(err.Error())
logger.Error("Failed to start web GUI")
return
}
if c.LaunchBrowser {
wg.Add(1)
go func() {
defer wg.Done()
// Wait a moment just to make sure the http interface is up
time.Sleep(time.Millisecond * 100)
logger.Info("Launching System Browser with %s", fullAddress)
if err := browser.Open(fullAddress); err != nil {
logger.Error(err.Error())
return
}
}()
}
}
/*
time.Sleep(5)
tx := InitTransaction()
_ = tx
err, _ = d.Visor.Visor.InjectTxn(tx)
if err != nil {
log.Panic(err)
}
*/
//first transaction
// if c.RunMaster == true {
// go func() {
// for d.Visor.HeadBkSeq() < 2 {
// time.Sleep(5 * time.Second)
// tx := InitTransaction()
// _, err := d.Visor.InjectTransaction(tx, d.Pool)
// if err != nil {
// log.Panic(err)
// }
// logger.Critical("Inject transaction success")
// }
// }()
// }
select {
case <-quit:
case err := <-errC:
logger.Error("%v", err)
}
logger.Info("Shutting down...")
if rpc != nil {
rpc.Shutdown()
}
gui.Shutdown()
d.Shutdown()
closelog()
wg.Wait()
logger.Info("Goodbye")
}
func main() {
devConfig.Parse()
Run(&devConfig)
}
//addresses for storage of coins
var AddrList []string = []string{
"2JEc8JFzN2TGFy3wqeoe6eru3vwgq45sVSR",
"2TvPvWdA4zvaqpcwTfPLkgHGQtDAzdqQCb7",
"79xKvR3NQ7h4KD4vNt2PBLDtoKFDk1gG43",
"27CnhwPzuZV6zVh5Pe6JmM8HeFu35z3Jw8K",
"2M3pKMyx4NvfUFbWVvkT9Yv1STknFbL6VJp",
"agGeGte7zwoCKbQPQkd8L5dTKL49uLHYua",
"2GfRVqmmui6nddJkjjkjQ2fMbJFsWxezNV6",
"2Xvd17c6tVoJRfQ4npZWGjcz1LYrsqdYGXx",
"2Wgk1ghWPpD8NhZQi1ALbEKt8aNX9Np3Vim",
"2RDA7WebLA6unbyezKSNUoQvMDZZZWdQuzH",
"JSTLJ4FNxVwuhJdhBEvTVkwFWnqAhaDzic",
"2bcCTbYxByGXNAYNfSAVA759qsCXD15XApt",
"2UjMumrXD4r9CZKxPdBJwU4VHsVtMDB4Lv3",
"jRDhoRhcpmRHjgcF6Emn7Yuj3mHRGzm262",
"25T8PTJyLf6K4QisCZnv9J2EGLaramLt7fR",
"TN5nFx6j5xoHj1jL5YZykaqeQ9UXKxyNC7",
"rXLbAYaGehJwzja7Gqkc3NHBi9hUmrAFDR",
"2S2Bfa4kvf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7jDSddh6ZCy26M",
"b8KG4qRuxkHt29mpP5LNUuwQwBZhUG6RJB",
"2LfmBFMFAsNf2ZDyNEwoH8RWNHmxSbQvqSb",
"ruKpSMdayPGyFtYG9EH2QTpcrmTvF4EjAV",
"2iej2x3fEk4sraSXeShVu65KH2NUAvJFg9N",
"qcu4nqYcX5rGC3EmoXU8q22vDcPUMpPYXY",
"2YhrGpKUWXfHrtW6c9FKVKqYrxVHVBXGind",
"2H5mmwbibJz9KxpwRAuZ4ezvTSDL8iNUQGH",
"zeh9v9afW7a3Ji5e7SGdzb3Sk2xajTXUMf",
"xV3XQzAR86Y6r7qA49BsFcLxpQ4xhtASq7",
"EtNKuCeca61htJigtYuove3Yb6H2Scpitk",
"kfzfkb7TBQLGaBSN4ssjyfMNfscYjdYcy5",
"27AKWwt4pxtixUM9PAG4J1at7w8oBivJms5",
"2F5HzZU4RNYZbjmbCCXbDAsHAcqmAMZkpkp",
"bnEUiH3HVq95pySibLRxHrTLgxQJhsTgRi",
"7n9m5HZrVDVCNmVYirkG6WB18fHRnF3ZwK",
"2GpNJcvfoBLTd21oVFvGmvyshmu5GjUDNgA",
"2LM8uSr35BvyXNihhzTdSNasbYVX4yEGuUi",
"zhixmLqy3fYcRUAMZZZuABtwVnxwfXriV3",
"9n5yUAhSwbGZMMhC7KFNCjZnqC918tnjoY",
"2m6PiUioXSQKAQCqVj7T5fzrVMAi5DX8tU5",
"2EvQ2MwPeqXqaHs9qRFzWAoByz4ph64jSKU",
"KR6sPjZvE2KfpAEpmMCdqAvCrtASpNADHK",
"rk973tk7wCJsRU9ExFHBKuPgvLeehjjRMy",
"PLUcUcFNSnK2rXoP2Fd5ugqGCMzn1ksfM1",
"KZULDtdtgSqhUzvVJLBhscFaXGHdVHZ9TU",
}
func InitTransaction() coin.Transaction {
var tx coin.Transaction
output := cipher.MustSHA256FromHex("7c2f4421e420e77380791488772f177362e5c6fb61bb46612bbe21b5eabc3050")
tx.PushInput(output)
for i := 0; i < 100; i++ {
addr := cipher.MustDecodeBase58Address(AddrList[i])
tx.PushOutput(addr, 3e12, 1) // 10e6*10e6
}
// seckeys := make([]cipher.SecKey, 1)
// seckey := ""
// seckeys[0] = cipher.MustSecKeyFromHex(seckey)
// tx.SignInputs(seckeys)
txs := make([]cipher.Sig, 1)
sig := "f1f3342332af4b773513008079772b12feabe40dd7a81ee0b3449f8609995d8b55c9c8436921bcbf31fd6c485017c1ef6e251070bd93033abd7ea3825813bb8e00"
txs[0] = cipher.MustSigFromHex(sig)
tx.Sigs = txs
tx.UpdateHeader()
err := tx.Verify()
if err != nil {
log.Panic(err)
}
return tx
}
func createDirIfNotExist(dir string) error {
if _, err := os.Stat(dir); !os.IsNotExist(err) {
return nil
}
return os.Mkdir(dir, 0777)
}