-
Notifications
You must be signed in to change notification settings - Fork 19
/
nodecli.go
1095 lines (832 loc) · 24.5 KB
/
nodecli.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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/gelembjuk/oursql/lib/net"
"github.com/gelembjuk/oursql/lib/nodeclient"
"github.com/gelembjuk/oursql/lib/remoteclient"
"github.com/gelembjuk/oursql/lib/utils"
"github.com/gelembjuk/oursql/node/config"
"github.com/gelembjuk/oursql/node/consensus"
"github.com/gelembjuk/oursql/node/nodemanager"
"github.com/gelembjuk/oursql/node/server"
)
var allowWithoutBCReady = []string{"initblockchain",
"importblockchain",
"interactiveautocreate",
"restoreblockchain",
"createwallet",
config.CommandImportWallet,
config.CommandExportWallet,
"listaddresses",
"nodestate"}
var disableWithBCReady = []string{"initblockchain",
"initblockchain",
"importblockchain",
"importandstart",
"restoreblockchain"}
var commandsInteractiveMode = []string{
"initblockchain",
"importblockchain",
config.CommandRestoreBlockchain,
config.CommandDumpBlockchain,
"exportconsensusconfig",
"pullupdates",
"printchain",
"makeblock",
"reindexcache",
"send",
"sql",
"getbalance",
"getbalances",
"createwallet",
config.CommandImportWallet,
config.CommandExportWallet,
"listaddresses",
"unapprovedtransactions",
"mineblock",
"canceltransaction",
"dropblock",
"addrhistory",
"showunspent",
"shownodes",
"addnode",
"removenode"}
var commandNodeManageMode = []string{
"interactiveautocreate",
"importandstart",
"startnode",
"startintnode",
"stopnode",
config.Daemonprocesscommandline,
"nodestate"}
type NodeCLI struct {
Input config.AppInput
Logger *utils.LoggerMan
ConfigDir string
ConseususConfigFile string
ConseususConfigFilePresent bool
Command string
AlreadyRunningPort int
NodeAuthStr string
Node *nodemanager.Node
}
/*
* Creates a client object
*/
func getNodeCLI(input config.AppInput) NodeCLI {
cli := NodeCLI{}
cli.Input = input
cli.ConfigDir = input.ConfigDir
cli.Command = input.Command
cli.Logger = utils.CreateLogger()
cli.Logger.EnableLogs(input.Logs)
if input.Args.LogDest != "stdout" {
cli.Logger.LogToFiles(cli.ConfigDir, "log_trace.txt", "log_traceext.txt", "log_info.txt", "log_warning.txt", "log_error.txt")
} else {
cli.Logger.LogToStdout()
}
cli.ConseususConfigFile = input.ConseususConfigFile
cli.ConseususConfigFilePresent = input.ConseususConfigFilePresent
cli.Node = nil
// check if Daemon is already running
nd := server.NodeDaemon{}
nd.ConfigDir = cli.ConfigDir
nd.Logger = cli.Logger
port, authstr := nd.GetRunningProcessInfo()
cli.AlreadyRunningPort = port
cli.NodeAuthStr = authstr
cli.Logger.Trace.Println("Node CLI inited")
return cli
}
/*
* Createes node object. Node does all work related to acces to bockchain and DB
*/
func (c *NodeCLI) CreateNode() error {
if c.Node != nil {
//already created
return nil
}
node := nodemanager.Node{}
node.ConfigDir = c.ConfigDir
node.DBConn = &nodemanager.Database{}
node.DBConn.SetLogger(c.Logger)
node.DBConn.SetConfig(c.Input.Database)
// at this place DB doesn't do a connection attempt
node.DBConn.Init()
node.Logger = c.Logger
node.MinterAddress = c.Input.MinterAddress
var err error
// load consensus config
if c.ConseususConfigFilePresent {
node.ConsensusConfig, err = consensus.NewConfigFromFile(c.ConseususConfigFile)
} else {
node.ConsensusConfig, err = consensus.NewConfigDefault()
}
if err != nil {
c.Logger.Error.Printf("Error when init consensus config %s", err.Error())
return err
}
node.ConsensusConfig.SetConfigFilePath(c.Input.ConseususConfigFile)
node.Init()
node.InitNodes(c.Input.Nodes, false)
node.NodeClient.SetAuthStr(c.NodeAuthStr)
c.Node = &node
c.setNodeProxyKeys()
return nil
}
func (c *NodeCLI) getApplicationName() string {
c.CreateNode()
return c.Node.ConsensusConfig.Application.Name
}
// Check if there is internal keys pair to sign DB proxy transactions. Attach if it is set
func (c *NodeCLI) setNodeProxyKeys() error {
c.Node.ProxyPubKey = []byte{}
if c.Input.ProxyKey != "" {
walletscli, err := c.getWalletsCLI()
if err == nil {
walletobj, err := walletscli.WalletsObj.GetWallet(c.Input.ProxyKey)
if err == nil {
c.Node.ProxyPubKey = walletobj.GetPublicKey()
c.Node.ProxyPrivateKey = walletobj.GetPrivateKey()
}
}
}
return nil
}
// Detects if this request is not related to node server management and must return response right now
func (c NodeCLI) isInteractiveMode() bool {
return utils.StringInSlice(c.Command, commandsInteractiveMode)
}
// Detects if it is a node management command
func (c NodeCLI) isNodeManageMode() bool {
if utils.StringInSlice(c.Command, commandNodeManageMode) {
return true
}
return false
}
// Executes the client command in interactive mode
func (c NodeCLI) ExecuteCommand() error {
err := c.CreateNode() // init node struct
if err != nil {
return err
}
bcexists := c.Node.BlockchainExist()
if !utils.StringInSlice(c.Command, allowWithoutBCReady) && !bcexists {
return errors.New("Blockchain is not found. Must be created or inited")
} else if bcexists && utils.StringInSlice(c.Command, disableWithBCReady) {
return errors.New("Blockchain already exists")
}
defer c.Node.DBConn.CloseConnection()
switch c.Command {
case "initblockchain":
return c.commandInitBlockchain()
case "importblockchain":
return c.commandImportBlockchain()
case config.CommandRestoreBlockchain:
return c.commandRestoreBlockchain()
case config.CommandDumpBlockchain:
return c.commandDumpBlockchain()
case "exportconsensusconfig":
return c.commandExportConsensusConfig()
case "pullupdates":
return c.commandPullUpdates()
case "printchain":
return c.commandPrintChain()
case "reindexcache":
return c.commandReindexCache()
case "getbalance":
return c.commandGetBalance()
case "getbalances":
return c.commandAddressesBalance()
case "listaddresses":
return c.forwardCommandToWallet()
case "createwallet":
return c.forwardCommandToWallet()
case config.CommandImportWallet:
return c.forwardCommandToWallet()
case config.CommandExportWallet:
return c.forwardCommandToWallet()
case "send":
return c.commandSend()
case "sql":
return c.commandSQL()
case "unapprovedtransactions":
return c.commandUnapprovedTransactions()
case "makeblock":
return c.commandMakeBlock()
case "dropblock":
return c.commandDropBlock()
case "canceltransaction":
return c.commandCancelTransaction()
case "addrhistory":
return c.commandAddressHistory()
case "showunspent":
return c.commandShowUnspent()
case "shownodes":
return c.commandShowNodes()
case "addnode":
return c.commandAddNode()
case "removenode":
return c.commandRemoveNode()
}
return errors.New("Unknown management command")
}
/*
* Creates node server daemon manager
*/
func (c NodeCLI) createDaemonManager() (*server.NodeDaemon, error) {
nd := server.NodeDaemon{}
if !c.Node.BlockchainExist() {
return nil, errors.New("Blockchain is not found. Must be created or inited")
}
nd.ConfigDir = c.ConfigDir
nd.Logger = c.Logger
nd.Port = c.Input.Port
nd.Host = c.Input.Host
nd.LocalPort = c.Input.LocalPort
nd.Node = c.Node
nd.DBProxyAddr = c.Input.DBProxyAddress
nd.DBAddr = c.Input.Database.GetServerAddress()
nd.Init()
return &nd, nil
}
// Execute server management command
func (c NodeCLI) ExecuteManageCommand() error {
err := c.CreateNode()
if err != nil {
return err
}
if c.Command == "importandstart" {
return c.commandImportStartInteractive()
} else if c.Command == "interactiveautocreate" {
return c.commandInitIfNeededStartInteractive()
}
noddaemon, err := c.createDaemonManager()
if err != nil {
return err
}
if c.Command == "startnode" {
return noddaemon.StartServer()
} else if c.Command == "startintnode" {
return noddaemon.StartServerInteractive()
} else if c.Command == "stopnode" {
return noddaemon.StopServer()
} else if c.Command == config.Daemonprocesscommandline {
return noddaemon.DaemonizeServer()
} else if c.Command == "nodestate" {
return c.commandShowState(noddaemon)
}
return errors.New("Unknown node manage command")
}
// Creates wallet object for operation related to wallets list management
func (c *NodeCLI) getWalletsCLI() (*remoteclient.WalletCLI, error) {
winput := remoteclient.AppInput{}
winput.Command = c.Input.Command
winput.Address = c.Input.Args.Address
winput.ConfigDir = c.Input.ConfigDir
winput.NodePort = c.Input.LocalPort
winput.NodeHost = "localhost"
winput.Amount = c.Input.Args.Amount
winput.ToAddress = c.Input.Args.To
winput.SQL = c.Input.Args.SQL
winput.Filepath = c.Input.Args.FilePath
if c.Input.Args.From != "" {
winput.Address = c.Input.Args.From
}
//c.Logger.Trace.Println("Running port ", c.AlreadyRunningPort)
walletscli := remoteclient.WalletCLI{}
if c.AlreadyRunningPort > 0 {
winput.NodePort = c.AlreadyRunningPort
winput.NodeHost = "localhost"
}
walletscli.Init(c.Logger, winput)
walletscli.NodeMode = true
return &walletscli, nil
}
// Forwards a command to wallet object. This is needed for cases when a node does some
// operation with local wallets
func (c *NodeCLI) forwardCommandToWallet() error {
walletscli, err := c.getWalletsCLI()
if err != nil {
return err
}
c.Logger.Trace.Println("Execute command as a client")
return walletscli.ExecuteCommand()
}
// Create Network Client object. We do this when a node server is running and we need to send
// command to it (indtead of accessing database directly)
func (c *NodeCLI) getLocalNetworkClient() nodeclient.NodeClient {
nc := *c.Node.NodeClient
nc.NodeAddress.Port = c.AlreadyRunningPort
nc.NodeAddress.Host = "localhost"
return nc
}
// To create new blockchain from scratch
func (c *NodeCLI) commandInitBlockchain() error {
walletscli, err := c.getWalletsCLI()
if err != nil {
return err
}
walletobj, err := walletscli.WalletsObj.GetWallet(c.Input.MinterAddress)
if err != nil {
return err
}
err = c.Node.CreateBlockchain(c.Input.MinterAddress, walletobj.GetPublicKey(), walletobj.GetPrivateKey())
if err != nil {
return err
}
// some argument are posted with command line. we remember now all of them and will use later
c.Input.UpdateConfig()
fmt.Println("Done!")
return nil
}
// To init blockchain loaded from other node. Is executed for new nodes if blockchain already exists
func (c *NodeCLI) commandImportBlockchain() error {
if c.Input.Args.NodePort == 0 || c.Input.Args.NodeHost == "" {
addr := c.Node.ConsensusConfig.GetRandomInitialAddress()
if addr == nil {
return errors.New("No address to import from")
}
c.Input.Args.NodePort = addr.Port
c.Input.Args.NodeHost = addr.Host
}
alldone, err := c.Node.InitBlockchainFromOther(c.Input.Args.NodeHost, c.Input.Args.NodePort)
if err != nil {
return err
}
if alldone {
fmt.Println("Done! ")
} else {
fmt.Println("Done! First part of bockchain loaded. Next part will be loaded on background when node started")
}
c.Input.UpdateConfig()
return nil
}
// To restore blockchain from full dump to empty database
func (c *NodeCLI) commandRestoreBlockchain() error {
if c.Input.Args.DumpFile == "" {
return errors.New("Dump file name required")
}
err := c.Node.RestoreBlockchain(c.Input.Args.DumpFile)
if err != nil {
return err
}
c.Input.UpdateConfig()
fmt.Println("Blockchain was inited from DB dump")
return nil
}
// To create full dump of a blockchain
func (c *NodeCLI) commandDumpBlockchain() error {
if c.Input.Args.DumpFile == "" {
return errors.New("Dump file name required")
}
err := c.Node.DumpBlockchain(c.Input.Args.DumpFile)
if err != nil {
return err
}
fmt.Println("Blockchain DB was dumped to a file")
return nil
}
// Pull updates from all other known nodes
func (c *NodeCLI) commandPullUpdates() error {
c.Node.GetCommunicationManager().CheckForChangesOnOtherNodes(time.Now().Unix() - 3600)
fmt.Println("Updates Pull Complete")
return nil
}
// Print full blockchain
func (c *NodeCLI) commandPrintChain() error {
bci, err := c.Node.GetBlockChainIterator()
if err != nil {
return err
}
blocks := []string{}
for {
blockfull, err := bci.Next()
if err != nil {
return err
}
if blockfull == nil {
fmt.Printf("Somethign went wrong. Next block can not be loaded\n")
break
}
block := blockfull.GetSimpler()
if c.Input.Args.View == "short" {
fmt.Printf("===============\n")
fmt.Printf("Hash: %x\n", block.Hash)
fmt.Printf("Height: %d, Transactions: %d\n", block.Height, len(block.Transactions)-1)
fmt.Printf("Prev: %x\n", block.PrevBlockHash)
fmt.Printf("\n")
} else if c.Input.Args.View == "shortr" {
b := fmt.Sprintf("Hash: %x\n", block.Hash)
b = b + fmt.Sprintf("Height: %d, Transactions: %d\n", block.Height, len(block.Transactions)-1)
b = b + fmt.Sprintf("Prev: %x\n", block.PrevBlockHash)
blocks = append(blocks, b)
} else {
fmt.Printf("============ Block %x ============\n", block.Hash)
fmt.Printf("Height: %d\n", block.Height)
fmt.Printf("Prev. block: %x\n", block.PrevBlockHash)
for _, tx := range block.Transactions {
fmt.Println(tx)
}
fmt.Printf("\n\n")
}
if len(block.PrevBlockHash) == 0 {
break
}
}
if c.Input.Args.View == "shortr" {
for i := len(blocks) - 1; i >= 0; i-- {
block := blocks[i]
fmt.Printf("===============\n")
fmt.Print(block)
fmt.Printf("\n")
}
}
return nil
}
// Show contents of a cache of unapproved transactions (transactions pool)
func (c *NodeCLI) commandUnapprovedTransactions() error {
c.Logger.Trace.Println("Show unapproved transactions")
if c.Input.Args.Clean {
// clean cache
return c.Node.GetTransactionsManager().CleanUnapprovedCache()
}
total, _ := c.Node.GetTransactionsManager().ForEachUnapprovedTransaction(
func(txhash, txstr string) error {
fmt.Println(txstr)
return nil
})
fmt.Printf("\nTotal transactions: %d\n", total)
return nil
}
// Show all wallets and balances for each of them
func (c *NodeCLI) commandAddressesBalance() error {
if c.AlreadyRunningPort > 0 {
// run in wallet mode.
return c.forwardCommandToWallet()
}
walletscli, err := c.getWalletsCLI()
if err != nil {
return err
}
// get addresses in local wallets
result := map[string]remoteclient.WalletBalance{}
for _, address := range walletscli.WalletsObj.GetAddresses() {
balance, err := c.Node.GetTransactionsManager().GetAddressBalance(address)
if err != nil {
return err
}
result[string(address)] = balance
}
fmt.Println("Balance for all addresses:")
fmt.Println()
for address, balance := range result {
fmt.Printf("%s: %.8f (Approved - %.8f, Pending - %.8f)\n", address, balance.Total, balance.Approved, balance.Pending)
}
return nil
}
// Show history for a wallet
func (c *NodeCLI) commandAddressHistory() error {
if c.AlreadyRunningPort > 0 {
c.Input.Command = "showhistory"
// run in wallet mode.
return c.forwardCommandToWallet()
}
result, err := c.Node.NodeBC.GetAddressHistory(c.Input.Args.Address)
if err != nil {
return err
}
fmt.Println("History of transactions:")
for _, rec := range result {
if rec.IOType {
fmt.Printf("%f\t In from\t%s\n", rec.Value, rec.Address)
} else {
fmt.Printf("%f\t Out To \t%s\n", rec.Value, rec.Address)
}
}
return nil
}
// Show unspent transactions outputs for address
func (c *NodeCLI) commandShowUnspent() error {
if c.AlreadyRunningPort > 0 {
// run in wallet mode.
return c.forwardCommandToWallet()
}
balance := float64(0)
err := c.Node.GetTransactionsManager().ForEachUnspentOutput(c.Input.Args.Address,
func(fromaddr string, value float64, txID []byte, output int, isbase bool) error {
fmt.Printf("%f\t from\t%s in transaction %x output #%d\n", value, fromaddr, txID, output)
balance += value
return nil
})
if err != nil {
return err
}
fmt.Printf("\nBalance - %f\n", balance)
return nil
}
// Display balance for address
func (c *NodeCLI) commandGetBalance() error {
if c.AlreadyRunningPort > 0 {
// run in wallet mode.
return c.forwardCommandToWallet()
}
balance, err := c.Node.GetTransactionsManager().GetAddressBalance(c.Input.Args.Address)
if err != nil {
return err
}
fmt.Printf("Balance of '%s': \nTotal - %.8f\n", c.Input.Args.Address, balance.Total)
fmt.Printf("Approved - %.8f\n", balance.Approved)
fmt.Printf("Pending - %.8f\n", balance.Pending)
return nil
}
// Send money to other address
func (c *NodeCLI) commandSend() error {
if c.AlreadyRunningPort > 0 {
// run in wallet mode.
return c.forwardCommandToWallet()
}
c.Logger.Trace.Println("Send with dirct access to DB ")
// else, access directtly to the DB
walletscli, err := c.getWalletsCLI()
if err != nil {
return err
}
walletobj, err := walletscli.WalletsObj.GetWallet(c.Input.Args.From)
if err != nil {
return err
}
txid, err := c.Node.Send(walletobj.GetPublicKey(), walletobj.GetPrivateKey(),
c.Input.Args.To, c.Input.Args.Amount)
if err != nil {
return err
}
fmt.Printf("Success. New transaction: %x\n", txid)
return nil
}
// Reindex cache of transactions information
func (c *NodeCLI) commandReindexCache() error {
info, err := c.Node.GetTransactionsManager().ReindexData()
if err != nil {
return err
}
fmt.Printf("Done! There are %d transactions in the UTXO set.\n", info["unspentoutputs"])
return nil
}
// Try to mine a block if there is anough unapproved transactions
func (c *NodeCLI) commandMakeBlock() error {
block, err := c.Node.TryToMakeBlock([]byte{}, nil)
if err != nil {
return err
}
if len(block) > 0 {
fmt.Printf("Done! New block mined with the hash %x.\n", block)
} else {
fmt.Printf("Not enough transactions to mine a block.\n")
}
return nil
}
// Cancel transaction if it is not yet in a block
func (c *NodeCLI) commandCancelTransaction() error {
txID, err := hex.DecodeString(c.Input.Args.Transaction)
if err != nil {
return err
}
err = c.Node.GetTransactionsManager().CancelTransaction(txID, true)
if err != nil {
return err
}
fmt.Printf("Done!\n")
fmt.Printf("NOTE. This canceled transaction only from local node. If it was already sent to other nodes, than a transaction still can be completed!\n")
return nil
}
// Drops last block from the top of blockchain
func (c *NodeCLI) commandDropBlock() error {
err := c.Node.DropBlock()
if err != nil {
return err
}
bci, err := c.Node.GetBlockChainIterator()
if err != nil {
return err
}
blockFull, _ := bci.Next()
if blockFull == nil {
return errors.New("This was last block!")
}
block := blockFull.GetSimpler()
fmt.Printf("Done!\n")
fmt.Printf("============ Last Block %x ============\n", block.Hash)
fmt.Printf("Height: %d\n", block.Height)
fmt.Printf("Prev. block: %x\n", block.PrevBlockHash)
for _, tx := range block.Transactions {
fmt.Println(tx)
}
fmt.Printf("\n\n")
return nil
}
// Shows server state
func (c *NodeCLI) commandShowState(daemon *server.NodeDaemon) error {
Runnning, ProcessID, Port, err := daemon.GetServerState()
fmt.Println("Node Server State:")
var info nodeclient.ComGetNodeState
if Runnning {
fmt.Printf("Server is running. Process: %d, listening on the port %d\n", ProcessID, Port)
// request state from the node
nc := c.getLocalNetworkClient()
info, err = nc.SendGetState()
} else {
fmt.Println("Server is not running")
info, err = c.Node.GetNodeState()
}
if err != nil {
return err
}
fmt.Println("Blockchain state:")
fmt.Printf(" Number of blocks - %d\n", info.BlocksNumber)
if info.ExpectingBlocksHeight > info.BlocksNumber {
fmt.Printf(" Loaded %d of %d blocks\n", info.BlocksNumber, info.ExpectingBlocksHeight+1)
}
fmt.Printf(" Number of unapproved transactions - %d\n", info.TransactionsCached)
fmt.Printf(" Number of unspent transactions outputs - %d\n", info.UnspentOutputs)
return nil
}
// Displays list of nodes (connections)
func (c *NodeCLI) commandShowNodes() error {
var nodes []net.NodeAddr
var err error
if c.AlreadyRunningPort > 0 {
// connect to node to get nodes list
nc := c.getLocalNetworkClient()
nodes, err = nc.SendGetNodes()
if err != nil {
return err
}
} else {
nodes = c.Node.NodeNet.GetNodes()
}
fmt.Println("Nodes:")
for _, n := range nodes {
fmt.Println(" ", n.NodeAddrToString())
}
return nil
}
// Add a node to connections
func (c *NodeCLI) commandAddNode() error {
newaddr := net.NewNodeAddr(c.Input.Args.NodeHost, c.Input.Args.NodePort)
if c.AlreadyRunningPort > 0 {
nc := c.getLocalNetworkClient()
err := nc.SendAddNode(newaddr)
if err != nil {
return err
}
} else {
c.Node.AddNodeToKnown(newaddr, false)
}
fmt.Println("Success!")
return nil
}
// Remove a node from connections
func (c *NodeCLI) commandRemoveNode() error {
remaddr := net.NewNodeAddr(c.Input.Args.NodeHost, c.Input.Args.NodePort)
fmt.Printf("Remove %s %d", c.Input.Args.NodeHost, c.Input.Args.NodePort)
fmt.Println(remaddr)
if c.AlreadyRunningPort > 0 {
nc := c.getLocalNetworkClient()
err := nc.SendRemoveNode(remaddr)
if err != nil {
return err
}
} else {
c.Node.NodeNet.RemoveNodeFromKnown(remaddr)
}
fmt.Println("Success!")
return nil
}
// Execute new SQL command
func (c *NodeCLI) commandSQL() error {
if c.AlreadyRunningPort > 0 {
// run in wallet mode.
return c.forwardCommandToWallet()
}
c.Logger.Trace.Println("Execute SQL with manager tool")
walletscli, err := c.getWalletsCLI()
if err != nil {
return err
}
walletobj, err := walletscli.WalletsObj.GetWallet(c.Input.Args.From)
if err != nil {
return err
}
txid, err := c.Node.SQLTransaction(walletobj.GetPublicKey(), walletobj.GetPrivateKey(), c.Input.Args.SQL)
if err != nil {
return err
}
if txid == nil {
fmt.Printf("The query was executed without a transaction\n")
} else {
fmt.Printf("Success. New transaction: %x\n", txid)
}
return nil
}
// Prepare wallet, import BC and start interactive. If BC exists we just start a server (do nothign before it)
func (c *NodeCLI) commandImportStartInteractive() error {
if c.Input.Args.LogDestDefault {
// we always log to stdout for this command
// if default logging is used (it is files by defaut)
c.Logger.LogToStdout()
}
bcexists := c.Node.BlockchainExist()
// check if BC exists
if !bcexists {
err := c.commandImportBlockchain()
if err != nil {
return err
}
// check if there is at least 1 wallet . if no, create new one
walletscli, err := c.getWalletsCLI()
if err != nil {
return err
}
addresses := walletscli.WalletsObj.GetAddresses()
// get addresses in local wallets
if len(addresses) == 0 {
c.Input.MinterAddress, err = walletscli.WalletsObj.CreateWallet()
if err != nil {