forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
3154 lines (2687 loc) · 92.7 KB
/
state.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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package state
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"sync"
"time"
"github.com/FactomProject/factomd/events"
"github.com/FactomProject/factomd/common/constants/runstate"
"github.com/FactomProject/factomd/activations"
"github.com/FactomProject/factomd/common/adminBlock"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/globals"
. "github.com/FactomProject/factomd/common/identity"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/messages"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/database/boltdb"
"github.com/FactomProject/factomd/database/databaseOverlay"
"github.com/FactomProject/factomd/database/leveldb"
"github.com/FactomProject/factomd/database/mapdb"
"github.com/FactomProject/factomd/p2p"
"github.com/FactomProject/factomd/util"
"github.com/FactomProject/factomd/util/atomic"
"github.com/FactomProject/factomd/wsapi"
"github.com/FactomProject/logrustash"
"github.com/FactomProject/factomd/Utilities/CorrectChainHeads/correctChainHeads"
log "github.com/sirupsen/logrus"
)
// packageLogger is the general logger for all package related logs. You can add additional fields,
// or create more context loggers off of this
var packageLogger = log.WithFields(log.Fields{"package": "state"})
var _ = fmt.Print
type State struct {
Logger *log.Entry
RunState runstate.RunState
NetworkController *p2p.Controller
Salt interfaces.IHash
Cfg interfaces.IFactomConfig
ConfigFilePath string // $HOME/.factom/m2/factomd.conf by default
Prefix string
FactomNodeName string
FactomdVersion string
LogPath string
LdbPath string
BoltDBPath string
LogLevel string
ConsoleLogLevel string
NodeMode string
DBType string
CheckChainHeads struct {
CheckChainHeads bool
Fix bool
}
CloneDBType string
ExportData bool
ExportDataSubpath string
LogBits int64 // Bit zero is for logging the Directory Block on DBSig [5]
DBStatesSent []*interfaces.DBStateSent
DBStatesReceivedBase int
DBStatesReceived []*messages.DBStateMsg
LocalServerPrivKey string
DirectoryBlockInSeconds int
PortNumber int
Replay *Replay
FReplay *Replay
CrossReplay *CrossReplayFilter
DropRate int
Delay int64 // Simulation delays sending messages this many milliseconds
ControlPanelPort int
ControlPanelSetting int
// Keeping the last display state lets us know when to send over the new blocks
LastDisplayState *DisplayState
ControlPanelChannel chan DisplayState
ControlPanelDataRequest bool // If true, update Display state
// Network Configuration
Network string
MainNetworkPort string
PeersFile string
MainSeedURL string
MainSpecialPeers string
TestNetworkPort string
TestSeedURL string
TestSpecialPeers string
LocalNetworkPort string
LocalSeedURL string
LocalSpecialPeers string
CustomNetworkPort string
CustomSeedURL string
CustomSpecialPeers string
CustomNetworkID []byte
CustomBootstrapIdentity string
CustomBootstrapKey string
IdentityChainID interfaces.IHash // If this node has an identity, this is it
//Identities []*Identity // Identities of all servers in management chain
// Authorities []*Authority // Identities of all servers in management chain
AuthorityServerCount int // number of federated or audit servers allowed
IdentityControl *IdentityManager
// Just to print (so debugging doesn't drive functionality)
Status int // Return a status (0 do nothing, 1 provide queues, 2 provide consensus data)
serverPrt string
StatusMutex sync.Mutex
StatusStrs []string
Starttime time.Time
transCnt int
lasttime time.Time
tps float64
longTps float64
ResetTryCnt int
ResetCnt int
// pending entry/transaction api calls for the holding queue do not have proper scope
// This is used to create a temporary, correctly scoped holding queue snapshot for the calls on demand
HoldingMutex sync.RWMutex
HoldingLast int64
HoldingMap map[[32]byte]interfaces.IMsg
// Elections are managed through the Elections Structure
EFactory interfaces.IElectionsFactory
Elections interfaces.IElections
Election0 string // Title
Election1 string // Election state for display
Election2 string // Election state for display
Election3 string // Election leader list
// pending entry/transaction api calls for the ack queue do not have proper scope
// This is used to create a temporary, correctly scoped ackqueue snapshot for the calls on demand
AcksMutex sync.RWMutex
AcksLast int64
AcksMap map[[32]byte]interfaces.IMsg
DBStateAskCnt int
DBStateReplyCnt int
DBStateIgnoreCnt int
DBStateAppliedCnt int
MissingRequestAskCnt int
MissingRequestReplyCnt int
MissingRequestIgnoreCnt int
MissingResponseAppliedCnt int
ResendCnt int
ExpireCnt int
tickerQueue chan int
timerMsgQueue chan interfaces.IMsg
TimeOffset interfaces.Timestamp
MaxTimeOffset interfaces.Timestamp
networkOutMsgQueue NetOutMsgQueue
networkInvalidMsgQueue chan interfaces.IMsg
inMsgQueue InMsgMSGQueue
inMsgQueue2 InMsgMSGQueue
electionsQueue ElectionQueue
apiQueue APIMSGQueue
ackQueue chan interfaces.IMsg
msgQueue chan interfaces.IMsg
dataQueue chan interfaces.IMsg
// prioritizedMsgQueue contains messages we know we need for consensus. (missing from processlist)
// Currently messages from MMR handling can be put in here to fast track
// them to the front.
prioritizedMsgQueue chan interfaces.IMsg
ShutdownChan chan int // For gracefully halting Factom
JournalFile string
Journaling bool
ServerPrivKey *primitives.PrivateKey
ServerPubKey *primitives.PublicKey
serverPendingPrivKeys []*primitives.PrivateKey
serverPendingPubKeys []*primitives.PublicKey
// RPC connection config
RpcUser string
RpcPass string
RpcAuthHash []byte
FactomdTLSEnable bool
FactomdTLSKeyFile string
FactomdTLSCertFile string
FactomdLocations string
CorsDomains []string
// Server State
StartDelay int64 // Time in Milliseconds since the last DBState was applied
StartDelayLimit int64
DBFinished bool
RunLeader bool
BootTime int64 // Time in seconds that we last booted
EOMIssueTime int64
EOMSyncEnd int64
// Ignore missing messages for a period to allow rebooting a network where your
// own messages from the previously executing network can confuse you.
IgnoreDone bool
IgnoreMissing bool
// Timout and Limit for outstanding missing DBState requests
RequestTimeout int // timeout in seconds
RequestLimit int
LLeaderHeight uint32
Leader bool
LeaderVMIndex int
LeaderPL *ProcessList
PLProcessHeight uint32
// Height cutoff where no missing messages below this height
DBHeightAtBoot uint32
TimestampAtBoot interfaces.Timestamp
OneLeader bool
OutputAllowed bool
CurrentMinute int
// These are the start times for blocks and minutes
PreviousMinuteStartTime int64
CurrentMinuteStartTime int64
CurrentBlockStartTime int64
EOMsyncing bool
EOMSyncTime int64
EOM bool // Set to true when the first EOM is encountered
EOMLimit int
EOMProcessed int
EOMDone bool
EOMMinute int
EOMSys bool // At least one EOM has covered the System List
DBSig bool
DBSigLimit int
DBSigProcessed int // Number of DBSignatures received and processed.
DBSigDone bool
DBSigSys bool // At least one DBSig has covered the System List
CreatedLastBlockFromDBState bool
// By default, this is false, which means DBstates are discarded
// when a majority of leaders disagree with the hash we have via DBSigs
KeepMismatch bool
DBSigFails int // Keep track of how many blockhash mismatches we've had to correct
Saving bool // True if we are in the process of saving to the database
Syncing bool // Looking for messages from leaders to sync
NetStateOff bool // Disable if true, Enable if false
DebugConsensus bool // If true, dump consensus trace
FactoidTrans int
ECCommits int
ECommits int
FCTSubmits int
NewEntryChains int
NewEntries int
LeaderTimestamp interfaces.Timestamp
messageFilterTimestamp interfaces.Timestamp
// Maps
// ====
// For Follower
ResendHolding interfaces.Timestamp // Timestamp to gate resending holding to neighbors
HoldingList chan [32]byte // Queue to process Holding in order
HoldingVM int // VM used to build current holding list
Holding map[[32]byte]interfaces.IMsg // Hold Messages
XReview []interfaces.IMsg // After the EOM, we must review the messages in Holding
Acks map[[32]byte]interfaces.IMsg // Hold Acknowledgements
Commits *SafeMsgMap // map[[32]byte]interfaces.IMsg // Commit Messages
InvalidMessages map[[32]byte]interfaces.IMsg
InvalidMessagesMutex sync.RWMutex
AuditHeartBeats []interfaces.IMsg // The checklist of HeartBeats for this period
FaultTimeout int
FaultWait int
EOMfaultIndex int
LastTiebreak int64
AuthoritySetString string
// Network MAIN = 0, TEST = 1, LOCAL = 2, CUSTOM = 3
NetworkNumber int // Encoded into Directory Blocks(s.Cfg.(*util.FactomdConfig)).String()
// Database
DB interfaces.DBOverlaySimple
Anchor interfaces.IAnchor
// Directory Block State
DBStates *DBStateList // Holds all DBStates not yet processed.
StatesMissing *StatesMissing
StatesWaiting *StatesWaiting
StatesReceived *StatesReceived
// Having all the state for a particular directory block stored in one structure
// makes creating the next state, updating the various states, and setting up the next
// state much more simple.
//
// Functions that provide state information take a dbheight param. I use the current
// DBHeight to ensure that I return the proper information for the right directory block
// height, even if it changed out from under the calling code.
//
// Process list previous [0], present(@DBHeight) [1], and future (@DBHeight+1) [2]
ResetRequest bool // Set to true to trigger a reset
ProcessLists *ProcessLists
highestKnown uint32
highestAck uint32
AuthorityDeltas string
// Factom State
FactoidState interfaces.IFactoidState
NumTransactions int
// Permanent balances from processing blocks.
RestoreFCT map[[32]byte]int64
RestoreEC map[[32]byte]int64
FactoidBalancesPapi map[[32]byte]int64
FactoidBalancesP map[[32]byte]int64
FactoidBalancesPMutex sync.Mutex
ECBalancesPapi map[[32]byte]int64
ECBalancesP map[[32]byte]int64
ECBalancesPMutex sync.Mutex
TempBalanceHash interfaces.IHash
Balancehash interfaces.IHash
// Web Services
Port int
// For Replay / journal
IsReplaying bool
ReplayTimestamp interfaces.Timestamp
// State for the Entry Syncing process
EntrySyncState *EntrySync
MissingEntryBlockRepeat interfaces.Timestamp
// DBlock Height at which node has a complete set of eblocks+entries
EntryBlockDBHeightComplete uint32
// DBlock Height at which we have started asking for entry blocks
EntryBlockDBHeightProcessing uint32
// Entry Blocks we don't have that we are asking our neighbors for
MissingEntryBlocks []MissingEntryBlock
MissingEntryRepeat interfaces.Timestamp
// DBlock Height at which node has a complete set of eblocks+entries
EntryDBHeightComplete uint32
// DBlock Height at which we have started asking for or have all entries
EntryDBHeightProcessing uint32
// Height in the Directory Block where we have
// Entries we don't have that we are asking our neighbors for
MissingEntries chan *MissingEntry
// Holds leaders and followers up until all missing entries are processed, if true
WaitForEntries bool
UpdateEntryHash chan *EntryUpdate // Channel for updating entry Hashes tracking (repeats and such)
WriteEntry chan interfaces.IEBEntry
// MessageTally causes the node to keep track of (and display) running totals of each
// type of message received during the tally interval
MessageTally bool
MessageTalliesReceived [constants.NUM_MESSAGES]int
MessageTalliesSent [constants.NUM_MESSAGES]int
LastPrint string
LastPrintCnt int
// FER section
FactoshisPerEC uint64
FERChainId string
ExchangeRateAuthorityPublicKey string
FERChangeHeight uint32
FERChangePrice uint64
FERPriority uint32
FERPrioritySetHeight uint32
AckChange uint32
StateSaverStruct StateSaverStruct
// Logstash
UseLogstash bool
LogstashURL string
// Plugins
useTorrents bool
torrentUploader bool
Uploader *UploadController // Controls the uploads of torrents. Prevents backups
DBStateManager interfaces.IManagerController
HighestCompletedTorrent uint32
FastBoot bool
FastBootLocation string
FastSaveRate int
// These stats are collected when we write the dbstate to the database.
NumNewChains int // Number of new Chains in this block
NumNewEntries int // Number of new Entries, not counting the first entry in a chain
NumEntries int // Number of entries in this block (including the entries that create chains)
NumEntryBlocks int // Number of Entry Blocks
NumFCTTrans int // Number of Factoid Transactions in this block
// debug message about state status rolling queue for ControlPanel
pstate string
SyncingState [256]string
SyncingStateCurrent int
ProcessListProcessCnt int64 // count of attempts to process .. so we can see if the thread is running
StateProcessCnt int64
StateUpdateState int64
ValidatorLoopSleepCnt int64
processCnt int64 // count of attempts to process .. so we can see if the thread is running
ProcessTime interfaces.Timestamp
MMRInfo // fields for MMR processing
reportedActivations [activations.ACTIVATION_TYPE_COUNT + 1]bool // flags about which activations we have reported (+1 because we don't use 0)
validatorLoopThreadID string
OutputRegEx *regexp.Regexp
OutputRegExString string
InputRegEx *regexp.Regexp
InputRegExString string
executeRecursionDetection map[[32]byte]interfaces.IMsg
Hold HoldingList
EventService events.EventService
// MissingMessageResponse is a cache of the last 1000 msgs we receive such that when
// we send out a missing message, we can find that message locally before we ask the net
RecentMessage
// MissingMessageResponseHandler is a cache of the last 2 blocks of processed acks.
// It can handle and respond to missing message requests on it's own thread.
MissingMessageResponseHandler *MissingMessageResponseCache
ChainCommits Last100
Reveals Last100
}
var _ interfaces.IState = (*State)(nil)
type EntryUpdate struct {
Hash interfaces.IHash
Timestamp interfaces.Timestamp
}
func (s *State) GetConfigPath() string {
return s.ConfigFilePath
}
func (s *State) GetRunState() runstate.RunState {
return s.RunState
}
func (s *State) Clone(cloneNumber int) interfaces.IState {
newState := new(State)
number := fmt.Sprintf("%02d", cloneNumber)
simConfigPath := util.GetHomeDir() + "/.factom/m2/simConfig/"
configfile := fmt.Sprintf("%sfactomd%03d.conf", simConfigPath, cloneNumber)
if cloneNumber == 1 {
os.Stderr.WriteString(fmt.Sprintf("Looking for Config File %s\n", configfile))
}
if _, err := os.Stat(simConfigPath); os.IsNotExist(err) {
os.Stderr.WriteString("Creating simConfig directory\n")
os.MkdirAll(simConfigPath, 0775)
}
newState.FactomNodeName = s.Prefix + "FNode" + number
config := false
if _, err := os.Stat(configfile); !os.IsNotExist(err) {
os.Stderr.WriteString(fmt.Sprintf(" Using the %s config file.\n", configfile))
newState.LoadConfig(configfile, s.GetNetworkName())
config = true
}
if s.LogPath == "stdout" {
newState.LogPath = "stdout"
} else {
newState.LogPath = s.LogPath + "/Sim" + number
}
newState.FactomNodeName = s.Prefix + "FNode" + number
newState.FactomdVersion = s.FactomdVersion
newState.RunState = runstate.New // reset runstate since this clone will be started by sim node
newState.DropRate = s.DropRate
newState.LdbPath = s.LdbPath + "/Sim" + number
newState.JournalFile = s.LogPath + "/journal" + number + ".log"
newState.Journaling = s.Journaling
newState.BoltDBPath = s.BoltDBPath + "/Sim" + number
newState.LogLevel = s.LogLevel
newState.ConsoleLogLevel = s.ConsoleLogLevel
newState.NodeMode = "FULL"
newState.CloneDBType = s.CloneDBType
newState.DBType = s.CloneDBType
newState.CheckChainHeads = s.CheckChainHeads
newState.ExportData = s.ExportData
newState.ExportDataSubpath = s.ExportDataSubpath + "sim-" + number
newState.Network = s.Network
newState.MainNetworkPort = s.MainNetworkPort
newState.PeersFile = s.PeersFile
newState.MainSeedURL = s.MainSeedURL
newState.MainSpecialPeers = s.MainSpecialPeers
newState.TestNetworkPort = s.TestNetworkPort
newState.TestSeedURL = s.TestSeedURL
newState.TestSpecialPeers = s.TestSpecialPeers
newState.LocalNetworkPort = s.LocalNetworkPort
newState.LocalSeedURL = s.LocalSeedURL
newState.LocalSpecialPeers = s.LocalSpecialPeers
newState.CustomNetworkPort = s.CustomNetworkPort
newState.CustomSeedURL = s.CustomSeedURL
newState.CustomSpecialPeers = s.CustomSpecialPeers
newState.StartDelayLimit = s.StartDelayLimit
newState.CustomNetworkID = s.CustomNetworkID
newState.CustomBootstrapIdentity = s.CustomBootstrapIdentity
newState.CustomBootstrapKey = s.CustomBootstrapKey
newState.DirectoryBlockInSeconds = s.DirectoryBlockInSeconds
newState.PortNumber = s.PortNumber
newState.ControlPanelPort = s.ControlPanelPort
newState.ControlPanelSetting = s.ControlPanelSetting
newState.EventService = s.EventService
//newState.Identities = s.Identities
//newState.Authorities = s.Authorities
newState.AuthorityServerCount = s.AuthorityServerCount
newState.IdentityControl = s.IdentityControl.Clone()
newState.FaultTimeout = s.FaultTimeout
newState.FaultWait = s.FaultWait
newState.EOMfaultIndex = s.EOMfaultIndex
if !config {
newState.IdentityChainID = primitives.Sha([]byte(newState.FactomNodeName))
s.LogPrintf("AckChange", "Default3 IdentityChainID %v", s.IdentityChainID.String())
//generate and use a new deterministic PrivateKey for this clone
shaHashOfNodeName := primitives.Sha([]byte(newState.FactomNodeName)) //seed the private key with node name
clonePrivateKey := primitives.NewPrivateKeyFromHexBytes(shaHashOfNodeName.Bytes())
newState.LocalServerPrivKey = clonePrivateKey.PrivateKeyString()
s.initServerKeys()
}
newState.TimestampAtBoot = primitives.NewTimestampFromMilliseconds(s.TimestampAtBoot.GetTimeMilliUInt64())
newState.LeaderTimestamp = primitives.NewTimestampFromMilliseconds(s.LeaderTimestamp.GetTimeMilliUInt64())
newState.SetMessageFilterTimestamp(s.GetMessageFilterTimestamp())
newState.FactoshisPerEC = s.FactoshisPerEC
newState.Port = s.Port
newState.OneLeader = s.OneLeader
newState.OneLeader = s.OneLeader
newState.RpcUser = s.RpcUser
newState.RpcPass = s.RpcPass
newState.RpcAuthHash = s.RpcAuthHash
newState.RequestTimeout = s.RequestTimeout
newState.RequestLimit = s.RequestLimit
newState.FactomdTLSEnable = s.FactomdTLSEnable
newState.FactomdTLSKeyFile = s.FactomdTLSKeyFile
newState.FactomdTLSCertFile = s.FactomdTLSCertFile
newState.FactomdLocations = s.FactomdLocations
newState.FastSaveRate = s.FastSaveRate
newState.CorsDomains = s.CorsDomains
switch newState.DBType {
case "LDB":
newState.StateSaverStruct.FastBoot = s.StateSaverStruct.FastBoot
newState.StateSaverStruct.FastBootLocation = newState.LdbPath
break
case "Bolt":
newState.StateSaverStruct.FastBoot = s.StateSaverStruct.FastBoot
newState.StateSaverStruct.FastBootLocation = newState.BoltDBPath
break
}
if globals.Params.WriteProcessedDBStates {
path := filepath.Join(newState.LdbPath, newState.Network, "dbstates")
os.MkdirAll(path, 0775)
}
return newState
}
func (s *State) GetEventService() events.EventService {
return s.EventService
}
func (s *State) EmitDirectoryBlockEventsFromHeight(height uint32, end uint32) {
i := height
msgCount := 0
for i <= end {
d, err := s.DB.FetchDBlockByHeight(i)
if err != nil || d == nil {
break
}
a, err := s.DB.FetchABlockByHeight(i)
if err != nil || a == nil {
break
}
f, err := s.DB.FetchFBlockByHeight(i)
if err != nil || f == nil {
break
}
ec, err := s.DB.FetchECBlockByHeight(i)
if err != nil || ec == nil {
break
}
var eblocks []interfaces.IEntryBlock
var entries []interfaces.IEBEntry
ebs := d.GetEBlockDBEntries()
for _, eb := range ebs {
eblock, _ := s.DB.FetchEBlock(eb.GetKeyMR())
if eblock != nil {
eblocks = append(eblocks, eblock)
for _, e := range eblock.GetEntryHashes() {
ent, _ := s.DB.FetchEntry(e)
if ent != nil {
entries = append(entries, ent)
}
}
}
}
msg := messages.NewDBStateMsg(d.GetTimestamp(), d, a, f, ec, eblocks, entries, nil)
i++
msgCount++
s.EventService.EmitReplayDirectoryBlockCommit(msg)
}
}
func (s *State) AddPrefix(prefix string) {
s.Prefix = prefix
}
func (s *State) GetFactomNodeName() string {
return s.FactomNodeName
}
func (s *State) GetDBStatesSent() []*interfaces.DBStateSent {
return s.DBStatesSent
}
func (s *State) SetDBStatesSent(sents []*interfaces.DBStateSent) {
s.DBStatesSent = sents
}
func (s *State) GetDelay() int64 {
return s.Delay
}
func (s *State) SetDelay(delay int64) {
s.Delay = delay
}
func (s *State) GetBootTime() int64 {
return s.BootTime
}
func (s *State) GetDropRate() int {
return s.DropRate
}
func (s *State) SetDropRate(droprate int) {
s.DropRate = droprate
}
func (s *State) SetAuthoritySetString(authSet string) {
s.AuthoritySetString = authSet
}
func (s *State) GetAuthoritySetString() string {
return s.AuthoritySetString
}
func (s *State) AddAuthorityDelta(authSet string) {
s.AuthorityDeltas += fmt.Sprintf("\n%s", authSet)
}
func (s *State) GetAuthorityDeltas() string {
return s.AuthorityDeltas
}
func (s *State) GetNetStateOff() bool { // If true, all network communications are disabled
return s.NetStateOff
}
func (s *State) SetNetStateOff(net bool) {
//flag this in everyone!
s.LogPrintf("executeMsg", "State.SetNetStateOff(%v)", net)
s.LogPrintf("election", "State.SetNetStateOff(%v)", net)
s.LogPrintf("InMsgQueue", "State.SetNetStateOff(%v)", net)
s.LogPrintf("NetworkInputs", "State.SetNetStateOff(%v)", net)
s.NetStateOff = net
}
func (s *State) GetRpcUser() string {
return s.RpcUser
}
func (s *State) GetCorsDomains() []string {
return s.CorsDomains
}
func (s *State) GetRpcPass() string {
return s.RpcPass
}
func (s *State) SetRpcAuthHash(authHash []byte) {
s.RpcAuthHash = authHash
}
func (s *State) GetRpcAuthHash() []byte {
return s.RpcAuthHash
}
func (s *State) GetTlsInfo() (bool, string, string) {
return s.FactomdTLSEnable, s.FactomdTLSKeyFile, s.FactomdTLSCertFile
}
func (s *State) GetFactomdLocations() string {
return s.FactomdLocations
}
func (s *State) GetCurrentBlockStartTime() int64 {
return s.CurrentBlockStartTime
}
func (s *State) GetCurrentMinute() int {
return s.CurrentMinute
}
func (s *State) GetCurrentMinuteStartTime() int64 {
return s.CurrentMinuteStartTime
}
func (s *State) GetPreviousMinuteStartTime() int64 {
return s.PreviousMinuteStartTime
}
func (s *State) GetCurrentTime() int64 {
return time.Now().UnixNano()
}
func (s *State) IsSyncing() bool {
return s.Syncing
}
func (s *State) IsSyncingEOMs() bool {
return s.Syncing && s.EOM && !s.EOMDone
}
func (s *State) IsSyncingDBSigs() bool {
return s.Syncing && s.DBSig && !s.DBSigDone
}
func (s *State) DidCreateLastBlockFromDBState() bool {
return s.CreatedLastBlockFromDBState
}
func (s *State) IncDBStateAnswerCnt() {
s.DBStateReplyCnt++
}
func (s *State) IncFCTSubmits() {
s.FCTSubmits++
}
func (s *State) IncECCommits() {
s.ECCommits++
}
func (s *State) IncECommits() {
s.ECommits++
}
func (s *State) GetAckChange() (bool, error) {
var flag bool
change, err := util.GetChangeAcksHeight(s.ConfigFilePath)
if err != nil {
return flag, err
}
flag = s.AckChange != change
s.AckChange = change
return flag, nil
}
func (s *State) LoadConfig(filename string, networkFlag string) {
// s.FactomNodeName = s.Prefix + "FNode0" // Default Factom Node Name for Simulation
if len(filename) > 0 {
s.ConfigFilePath = filename
s.ReadCfg(filename)
// Get our factomd configuration information.
cfg := s.GetCfg().(*util.FactomdConfig)
s.Network = cfg.App.Network
if 0 < len(networkFlag) { // Command line overrides the config file.
s.Network = networkFlag
globals.Params.NetworkName = networkFlag // in case it did not come from there.
} else {
globals.Params.NetworkName = s.Network
}
fmt.Printf("\n\nNetwork : %s\n", s.Network)
networkName := strings.ToLower(s.Network) + "-"
// TODO: improve the paths after milestone 1
cfg.App.LdbPath = cfg.App.HomeDir + networkName + cfg.App.LdbPath
cfg.App.BoltDBPath = cfg.App.HomeDir + networkName + cfg.App.BoltDBPath
cfg.App.DataStorePath = cfg.App.HomeDir + networkName + cfg.App.DataStorePath
cfg.Log.LogPath = cfg.App.HomeDir + networkName + cfg.Log.LogPath
cfg.App.ExportDataSubpath = cfg.App.HomeDir + networkName + cfg.App.ExportDataSubpath
cfg.App.PeersFile = cfg.App.HomeDir + networkName + cfg.App.PeersFile
cfg.App.ControlPanelFilesPath = cfg.App.HomeDir + cfg.App.ControlPanelFilesPath
s.LogPath = cfg.Log.LogPath + s.Prefix
s.LdbPath = cfg.App.LdbPath + s.Prefix
s.BoltDBPath = cfg.App.BoltDBPath + s.Prefix
s.LogLevel = cfg.Log.LogLevel
s.ConsoleLogLevel = cfg.Log.ConsoleLogLevel
s.NodeMode = cfg.App.NodeMode
s.DBType = cfg.App.DBType
s.ExportData = cfg.App.ExportData // bool
s.ExportDataSubpath = cfg.App.ExportDataSubpath
s.MainNetworkPort = cfg.App.MainNetworkPort
s.PeersFile = cfg.App.PeersFile
s.MainSeedURL = cfg.App.MainSeedURL
s.MainSpecialPeers = cfg.App.MainSpecialPeers
s.TestNetworkPort = cfg.App.TestNetworkPort
s.TestSeedURL = cfg.App.TestSeedURL
s.TestSpecialPeers = cfg.App.TestSpecialPeers
s.CustomBootstrapIdentity = cfg.App.CustomBootstrapIdentity
s.CustomBootstrapKey = cfg.App.CustomBootstrapKey
s.LocalNetworkPort = cfg.App.LocalNetworkPort
s.LocalSeedURL = cfg.App.LocalSeedURL
s.LocalSpecialPeers = cfg.App.LocalSpecialPeers
s.LocalServerPrivKey = cfg.App.LocalServerPrivKey
s.CustomNetworkPort = cfg.App.CustomNetworkPort
s.CustomSeedURL = cfg.App.CustomSeedURL
s.CustomSpecialPeers = cfg.App.CustomSpecialPeers
s.FactoshisPerEC = cfg.App.ExchangeRate
s.DirectoryBlockInSeconds = cfg.App.DirectoryBlockInSeconds
s.PortNumber = cfg.App.PortNumber
s.ControlPanelPort = cfg.App.ControlPanelPort
s.RpcUser = cfg.App.FactomdRpcUser
s.RpcPass = cfg.App.FactomdRpcPass
s.RequestTimeout = cfg.App.RequestTimeout
s.RequestLimit = cfg.App.RequestLimit
s.StateSaverStruct.FastBoot = cfg.App.FastBoot
s.StateSaverStruct.FastBootLocation = cfg.App.FastBootLocation
s.FastBoot = cfg.App.FastBoot
s.FastBootLocation = cfg.App.FastBootLocation
// to test run curl -H "Origin: http://anotherexample.com" -H "Access-Control-Request-Method: POST" /
// -H "Access-Control-Request-Headers: X-Requested-With" -X POST /
// --data-binary '{"jsonrpc": "2.0", "id": 0, "method": "heights"}' -H 'content-type:text/plain;' /
// --verbose http://localhost:8088/v2
// while the config file has http://anotherexample.com in parameter CorsDomains the response should contain the string
// < Access-Control-Allow-Origin: http://anotherexample.com
if len(cfg.App.CorsDomains) > 0 {
domains := strings.Split(cfg.App.CorsDomains, ",")
s.CorsDomains = make([]string, len(domains))
for _, domain := range domains {
s.CorsDomains = append(s.CorsDomains, strings.Trim(domain, " "))
}
}
s.FactomdTLSEnable = cfg.App.FactomdTlsEnabled
FactomdTLSKeyFile := cfg.App.FactomdTlsPrivateKey
if cfg.App.FactomdTlsPrivateKey == "/full/path/to/factomdAPIpriv.key" {
FactomdTLSKeyFile = fmt.Sprint(cfg.App.HomeDir, "factomdAPIpriv.key")
}
if s.FactomdTLSKeyFile != FactomdTLSKeyFile {
if s.FactomdTLSEnable {
if _, err := os.Stat(FactomdTLSKeyFile); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Configured file does not exits: %s\n", FactomdTLSKeyFile)
}
}
s.FactomdTLSKeyFile = FactomdTLSKeyFile // set state
}
FactomdTLSCertFile := cfg.App.FactomdTlsPublicCert
if cfg.App.FactomdTlsPublicCert == "/full/path/to/factomdAPIpub.cert" {
s.FactomdTLSCertFile = fmt.Sprint(cfg.App.HomeDir, "factomdAPIpub.cert")
}
if s.FactomdTLSCertFile != FactomdTLSCertFile {
if s.FactomdTLSEnable {
if _, err := os.Stat(FactomdTLSCertFile); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Configured file does not exits: %s\n", FactomdTLSCertFile)
}
}
s.FactomdTLSCertFile = FactomdTLSCertFile // set state
}
s.FactomdTLSEnable = cfg.App.FactomdTlsEnabled
s.FactomdTLSKeyFile = cfg.App.FactomdTlsPrivateKey
externalIP := strings.Split(cfg.Walletd.FactomdLocation, ":")[0]
if externalIP != "localhost" {
s.FactomdLocations = externalIP
}
switch cfg.App.ControlPanelSetting {
case "disabled":
s.ControlPanelSetting = 0
case "readonly":
s.ControlPanelSetting = 1
case "readwrite":
s.ControlPanelSetting = 2
default:
s.ControlPanelSetting = 1
}
s.FERChainId = cfg.App.ExchangeRateChainId
s.ExchangeRateAuthorityPublicKey = cfg.App.ExchangeRateAuthorityPublicKey
identity, err := primitives.HexToHash(cfg.App.IdentityChainID)
if err != nil {
s.IdentityChainID = primitives.Sha([]byte(s.FactomNodeName))
s.LogPrintf("AckChange", "Bad IdentityChainID in config \"%v\"", cfg.App.IdentityChainID)
s.LogPrintf("AckChange", "Default2 IdentityChainID \"%v\"", s.IdentityChainID.String())
} else {
s.IdentityChainID = identity
s.LogPrintf("AckChange", "Load IdentityChainID \"%v\"", s.IdentityChainID.String())
}
if cfg.App.P2PIncoming > 0 {
p2p.MaxNumberIncomingConnections = cfg.App.P2PIncoming
}
if cfg.App.P2POutgoing > 0 {
p2p.NumberPeersToConnect = cfg.App.P2POutgoing
}
} else {
s.LogPath = "database/"
s.LdbPath = "database/ldb"
s.BoltDBPath = "database/bolt"
s.LogLevel = "none"
s.ConsoleLogLevel = "standard"
s.NodeMode = "SERVER"
s.DBType = "Map"
s.ExportData = false
s.ExportDataSubpath = "data/export"
s.Network = "TEST"
s.MainNetworkPort = "8108"
s.PeersFile = "peers.json"
s.MainSeedURL = "https://raw.githubusercontent.com/FactomProject/factomproject.github.io/master/seed/mainseed.txt"
s.MainSpecialPeers = ""
s.TestNetworkPort = "8109"
s.TestSeedURL = "https://raw.githubusercontent.com/FactomProject/factomproject.github.io/master/seed/testseed.txt"
s.TestSpecialPeers = ""
s.LocalNetworkPort = "8110"
s.LocalSeedURL = "https://raw.githubusercontent.com/FactomProject/factomproject.github.io/master/seed/localseed.txt"
s.LocalSpecialPeers = ""
s.LocalServerPrivKey = "4c38c72fc5cdad68f13b74674d3ffb1f3d63a112710868c9b08946553448d26d"
s.FactoshisPerEC = 006666
s.FERChainId = "111111118d918a8be684e0dac725493a75862ef96d2d3f43f84b26969329bf03"
s.ExchangeRateAuthorityPublicKey = "3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29"
s.DirectoryBlockInSeconds = 6
s.PortNumber = 8088
s.ControlPanelPort = 8090
s.ControlPanelSetting = 1
// TODO: Actually load the IdentityChainID from the config file
s.IdentityChainID = primitives.Sha([]byte(s.FactomNodeName))
s.LogPrintf("AckChange", "Default IdentityChainID %v", s.IdentityChainID.String())
}
s.JournalFile = s.LogPath + "/journal0" + ".log"
s.updateNetworkControllerConfig()
}
func (s *State) GetSalt(ts interfaces.Timestamp) uint32 {