-
Notifications
You must be signed in to change notification settings - Fork 95
/
core.go
840 lines (686 loc) · 24 KB
/
core.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
package node
import (
"fmt"
"reflect"
"sort"
"sync"
"time"
"github.com/mosaicnetworks/babble/src/common"
hg "github.com/mosaicnetworks/babble/src/hashgraph"
"github.com/mosaicnetworks/babble/src/peers"
"github.com/mosaicnetworks/babble/src/proxy"
"github.com/sirupsen/logrus"
)
// core is the object that is used by Node to manipulate the hashgraph
// indirectly.
type core struct {
// validator is a wrapper around the private-key controlling this node.
validator *Validator
// hg is the underlying hashgraph where all the consensus computation and
// data reside.
hg *hg.Hashgraph
// genesisPeers is the validator-set that the hashgraph was initialised
// with.
genesisPeers *peers.PeerSet
// validators reflects the latest validator-set used in the hashgraph
// consensus methods.
validators *peers.PeerSet
// peers is the list of peers that the node will try to gossip with; not
// necessarily the current validator-set.
peers *peers.PeerSet
// peerSelector is the object that decides which peer to talk to next.
peerSelector peerSelector
selectorLock sync.Mutex
// Hash and Index of this instance's head Event
head string
seq int
// acceptedRound is the first round at which the node's last join request
// (InternalTransaction) takes effect. A node will not create SelfEvents
// before reaching AcceptedRound. Default -1.
acceptedRound int
// removedRound is the round at which the node's last leave request takes
// effect (if there is one). Default -1.
removedRound int
// targetRound is the minimum Consensus Round that the node needs to reach.
// It is useful to set this value to a joining peer's accepted-round to
// prevent them from having to wait.
targetRound int
// lastPeerChangeRound is updated whenever a join / leave request is
// accepted
lastPeerChangeRound int
// Events that are not tied to this node's Head. This is managed by the Sync
// method. If the gossip condition is false (there is nothing interesting to
// record), items are added to heads; if the gossip condition is true, items
// are removed from heads and used to record a new self-event. This
// functionality allows to not grow the hashgraph continuously when there is
// nothing to record.
heads map[uint32]*hg.Event
// The transaction pool contains transactions submitted from the app that
// still haven't made it into the hashgraph.
transactionPool [][]byte
// internalTransactionPool is the same as transactionPool but for
// InternalTransactions
internalTransactionPool []hg.InternalTransaction
// selfBlockSignatures is a pool of block-signatures, created by this node,
// that still haven't made it into the hashgraph.
selfBlockSignatures *hg.SigPool
// proxyCommitCallback is called by the hashgraph when a block is committed
proxyCommitCallback proxy.CommitCallback
// maintenanceMode is passed through the constructor to indicate whether the
// user of core is in maintenance mode. This is used here to disable leave
// requests when a node is in maintenance mode
maintenanceMode bool
// promises keeps track of pending JoinRequests while the corresponding
// InternalTransactions go through consensus asynchronously.
promises map[string]*joinPromise
logger *logrus.Entry
}
// newCore creates and initialises a new Core object.
func newCore(
validator *Validator,
peers *peers.PeerSet,
genesisPeers *peers.PeerSet,
store hg.Store,
proxyCommitCallback proxy.CommitCallback,
maintenanceMode bool,
logger *logrus.Entry) *core {
peerSelector := newRandomPeerSelector(peers, validator.ID())
core := &core{
validator: validator,
proxyCommitCallback: proxyCommitCallback,
genesisPeers: genesisPeers,
validators: genesisPeers,
peers: peers,
peerSelector: peerSelector,
transactionPool: [][]byte{},
internalTransactionPool: []hg.InternalTransaction{},
selfBlockSignatures: hg.NewSigPool(),
promises: make(map[string]*joinPromise),
heads: make(map[uint32]*hg.Event),
logger: logger,
head: "",
seq: -1,
acceptedRound: -1,
removedRound: -1,
targetRound: -1,
lastPeerChangeRound: -1,
maintenanceMode: maintenanceMode,
}
core.hg = hg.NewHashgraph(store, core.commit, logger)
core.hg.Init(genesisPeers)
return core
}
// SetHeadAndSeq sets the Head and Seq of a Core object
func (c *core) setHeadAndSeq() error {
head := ""
seq := -1
_, ok := c.hg.Store.RepertoireByID()[c.validator.ID()]
if ok {
last, err := c.hg.Store.LastEventFrom(c.validator.PublicKeyHex())
if err != nil && !common.IsStore(err, common.Empty) {
return err
}
if last != "" {
lastEvent, err := c.getEvent(last)
if err != nil {
return err
}
head = last
seq = lastEvent.Index()
}
} else {
c.logger.Debug("Not in repertoire yet.")
}
c.head = head
c.seq = seq
c.logger.WithFields(logrus.Fields{
"core.Head": c.head,
"core.Seq": c.seq,
}).Debugf("SetHeadAndSeq")
return nil
}
// bootstrap calls the Hashgraph Bootstrap
func (c *core) bootstrap() error {
c.logger.Debug("Bootstrap")
return c.hg.Bootstrap()
}
// setPeers sets the peers property and a New RandomPeerSelector
func (c *core) setPeers(ps *peers.PeerSet) {
c.peers = ps
c.peerSelector = newRandomPeerSelector(c.peers, c.validator.ID())
}
/*******************************************************************************
Busy
*******************************************************************************/
// busy indicates whether there is some unfinished work.
func (c *core) busy() bool {
return c.hg.PendingLoadedEvents > 0 ||
len(c.transactionPool) > 0 ||
len(c.internalTransactionPool) > 0 ||
c.selfBlockSignatures.Len() > 0 ||
(c.hg.LastConsensusRound != nil && *c.hg.LastConsensusRound < c.targetRound)
}
/*******************************************************************************
Sync
*******************************************************************************/
// sync decodes and inserts new Events into the Hashgraph. UnknownEvents are
// expected to be in topoligical order.
func (c *core) sync(fromID uint32, unknownEvents []hg.WireEvent) error {
c.logger.WithField("unknown_events", len(unknownEvents)).Debug("Sync")
var otherHead *hg.Event
for _, we := range unknownEvents {
ev, err := c.hg.ReadWireInfo(we)
if err != nil {
c.logger.WithFields(logrus.Fields{
"wire_event": we,
"error": err,
}).Error("Reading WireEvent")
return err
}
// NormalSelfParentErrors are not reported. They can happen when two
// concurrent pulls are trying to insert the same events.
if err := c.insertEventAndRunConsensus(ev, false); err != nil {
if hg.IsNormalSelfParentError(err) {
continue
} else {
c.logger.WithError(err).Errorf("Inserting Event")
return err
}
}
if we.Body.CreatorID == fromID {
otherHead = ev
}
if h, ok := c.heads[we.Body.CreatorID]; ok &&
h != nil &&
we.Body.Index > h.Index() {
delete(c.heads, we.Body.CreatorID)
}
}
// Do not overwrite a non-empty head with an empty head
if h, ok := c.heads[fromID]; !ok ||
h == nil ||
(otherHead != nil && otherHead.Index() > h.Index()) {
c.heads[fromID] = otherHead
}
c.logger.WithFields(logrus.Fields{
"loaded_events": c.hg.PendingLoadedEvents,
"transaction_pool": len(c.transactionPool),
"internal_transaction_pool": len(c.internalTransactionPool),
"self_signature_pool": c.selfBlockSignatures.Len(),
"target_round": c.targetRound,
}).Debug("Sync")
// Create new event with self head and other head only if there are pending
// loaded events or the pools are not empty
if c.busy() ||
c.seq < 0 {
return c.recordHeads()
}
return nil
}
// recordHeads adds heads as SelfEvents
func (c *core) recordHeads() error {
c.logger.WithField("heads", len(c.heads)).Debug("RecordHeads()")
for id, ev := range c.heads {
op := ""
if ev != nil {
op = ev.Hex()
}
if err := c.addSelfEvent(op); err != nil {
return err
}
delete(c.heads, id)
}
return nil
}
// addSelfEvent adds a self event
func (c *core) addSelfEvent(otherHead string) error {
if c.hg.Store.LastRound() < c.acceptedRound {
c.logger.Debugf("Too early to insert self-event (%d / %d)", c.hg.Store.LastRound(), c.acceptedRound)
return nil
}
// Add own block signatures to next Event
sigs := c.selfBlockSignatures.Slice()
txs := len(c.transactionPool)
itxs := len(c.internalTransactionPool)
// create new event with self head and otherHead, and empty pools in its
// payload
newHead := hg.NewEvent(c.transactionPool,
c.internalTransactionPool,
sigs,
[]string{c.head, otherHead},
c.validator.PublicKeyBytes(),
c.seq+1)
// Inserting the Event, and running consensus methods, can have a
// side-effect of adding items to the transaction pools (via the commit
// callback).
if err := c.signAndInsertSelfEvent(newHead); err != nil {
c.logger.WithError(err).Errorf("Error inserting new head")
return err
}
c.logger.WithFields(logrus.Fields{
"index": newHead.Index(),
"transactions": len(newHead.Transactions()),
"internal_transactions": len(newHead.InternalTransactions()),
"block_signatures": len(newHead.BlockSignatures()),
}).Debug("Created Self-Event")
// do not remove pool elements that were added by CommitCallback
c.transactionPool = c.transactionPool[txs:]
c.internalTransactionPool = c.internalTransactionPool[itxs:]
c.selfBlockSignatures.RemoveSlice(sigs)
return nil
}
// signAndInsertSelfEvent signs a Hashgraph Event, inserts it and runs
// consensus.
func (c *core) signAndInsertSelfEvent(event *hg.Event) error {
if err := event.Sign(c.validator.Key); err != nil {
return err
}
return c.insertEventAndRunConsensus(event, true)
}
// insertEventAndRunConsensus Inserts a hashgraph event and runs consensus
func (c *core) insertEventAndRunConsensus(event *hg.Event, setWireInfo bool) error {
if err := c.hg.InsertEventAndRunConsensus(event, setWireInfo); err != nil {
return err
}
if event.Creator() == c.validator.PublicKeyHex() {
c.head = event.Hex()
c.seq = event.Index()
}
return nil
}
// knownEvents returns known events from the Hashgraph store
func (c *core) knownEvents() map[uint32]int {
return c.hg.Store.KnownEvents()
}
/*******************************************************************************
FastForward
*******************************************************************************/
// fastForward is used whilst in CatchingUp state to reset the underlying
// hashgraph from a Block and associated Frame.
func (c *core) fastForward(block *hg.Block, frame *hg.Frame) error {
c.logger.Debug("Fast Forward", frame.Round)
peerSet := peers.NewPeerSet(frame.Peers)
// Check Block Signatures
err := c.hg.CheckBlock(block, peerSet)
if err != nil {
return err
}
// Check Frame Hash
frameHash, err := frame.Hash()
if err != nil {
return err
}
if !reflect.DeepEqual(block.FrameHash(), frameHash) {
return fmt.Errorf("Invalid Frame Hash")
}
err = c.hg.Reset(block, frame)
if err != nil {
return err
}
err = c.setHeadAndSeq()
if err != nil {
return err
}
// Update peer-selector and validators
c.setPeers(peers.NewPeerSet(frame.Peers))
c.validators = peers.NewPeerSet(frame.Peers)
return nil
}
// getAnchorBlockWithFrame returns GetAnchorBlockWithFrame from the hashgraph
func (c *core) getAnchorBlockWithFrame() (*hg.Block, *hg.Frame, error) {
return c.hg.GetAnchorBlockWithFrame()
}
/*******************************************************************************
Leave
*******************************************************************************/
// leave causes the node to politely leave the network. If the node is not
// alone, it submits an InternalTransaction to be removed from the
// validator-set. Otherwise it does nothing.
func (c *core) leave(leaveTimeout time.Duration) error {
// Do nothing if we are not a validator.
p, ok := c.validators.ByID[c.validator.ID()]
if !ok {
c.logger.Debugf("Leave: not a validator, do nothing")
return nil
}
// Do nothing if we are the only validator.
if len(c.validators.Peers) <= 1 {
c.logger.Debugf("Leave: alone, do nothing")
return nil
}
// Check for maintenance mode, if set no need for a leave request
if c.maintenanceMode {
c.logger.Debugf("Leave: maintenance mode, do nothing")
return nil
}
// Otherwise, submit an InternalTransaction
c.logger.Debugf("Leave: submit InternalTransaction")
itx := hg.NewInternalTransaction(hg.PEER_REMOVE, *p)
itx.Sign(c.validator.Key)
promise := c.addInternalTransaction(itx)
// Wait for the InternalTransaction to go through consensus
timeout := time.After(leaveTimeout)
select {
case resp := <-promise.respCh:
c.logger.WithFields(logrus.Fields{
"leaving_round": resp.acceptedRound,
"peers": len(resp.peers),
}).Debug("leave request processed")
case <-timeout:
err := fmt.Errorf("Timeout waiting for leave request to go through consensus")
c.logger.WithError(err).Error()
return err
}
// Wait for node to reach RemovedRound
if c.peers.Len() >= 1 {
timeout = time.After(leaveTimeout)
for {
select {
case <-timeout:
err := fmt.Errorf("Timeout waiting for leaving node to reach TargetRound")
c.logger.WithError(err).Error()
return err
default:
if c.hg.LastConsensusRound != nil && *c.hg.LastConsensusRound < c.removedRound {
c.logger.Debugf("Waiting to reach RemovedRound: %d/%d", *c.hg.LastConsensusRound, c.removedRound)
time.Sleep(100 * time.Millisecond)
} else {
return nil
}
}
}
}
return nil
}
/*******************************************************************************
Commit
*******************************************************************************/
// commit the Block to the App using the proxyCommitCallback
func (c *core) commit(block *hg.Block) error {
c.logger.WithFields(logrus.Fields{
"block": block.Index(),
"txs": len(block.Transactions()),
"internal_txs": len(block.InternalTransactions()),
}).Info("Commit")
// Commit the Block to the App
commitResponse, err := c.proxyCommitCallback(*block)
if err != nil {
c.logger.WithError(err).Error("Commit response")
}
c.logger.WithFields(logrus.Fields{
"block": block.Index(),
"internal_txs_receipts": len(commitResponse.InternalTransactionReceipts),
"state_hash": common.EncodeToString(commitResponse.StateHash),
}).Info("Commit response")
// Handle the response to set Block StateHash and process receipts which
// might update the PeerSet.
if err == nil {
block.Body.StateHash = commitResponse.StateHash
block.Body.InternalTransactionReceipts = commitResponse.InternalTransactionReceipts
// Sign the block if we belong to its validator-set
blockPeerSet, err := c.hg.Store.GetPeerSet(block.RoundReceived())
if err != nil {
return err
}
if _, ok := blockPeerSet.ByID[c.validator.ID()]; ok {
sig, err := c.signBlock(block)
if err != nil {
return err
}
c.selfBlockSignatures.Add(sig)
}
err = c.hg.SetAnchorBlock(block)
if err != nil {
return err
}
err = c.processAcceptedInternalTransactions(block.RoundReceived(), commitResponse.InternalTransactionReceipts)
if err != nil {
return err
}
}
return err
}
// signBlock signs the block and saves it.
func (c *core) signBlock(block *hg.Block) (hg.BlockSignature, error) {
sig, err := block.Sign(c.validator.Key)
if err != nil {
return hg.BlockSignature{}, err
}
err = block.SetSignature(sig)
if err != nil {
return hg.BlockSignature{}, err
}
err = c.hg.Store.SetBlock(block)
if err != nil {
return sig, err
}
return sig, nil
}
// processAcceptedInternalTransactions processes a list of
// InternalTransactionReceipts from a block, updates the PeerSet for the
// corresponding round (round-received + 6), and responds to eventual promises.
func (c *core) processAcceptedInternalTransactions(roundReceived int, receipts []hg.InternalTransactionReceipt) error {
currentPeers := c.peers
validators := c.validators
// Why +6? According to lemmas 5.15 and 5.17 of the original whitepaper, all
// consistent hashgraphs will have decided the fame of round r witnesses by
// round r+5 or before; so it is safe to set the new peer-set at round r+6.
effectiveRound := roundReceived + 6
changed := false
for _, r := range receipts {
txBody := r.InternalTransaction.Body
if r.Accepted {
c.logger.WithFields(logrus.Fields{
"peer": txBody.Peer,
"round_received": roundReceived,
"type": txBody.Type.String(),
}).Debug("Processing accepted InternalTransaction")
switch txBody.Type {
case hg.PEER_ADD:
validators = validators.WithNewPeer(&txBody.Peer)
currentPeers = currentPeers.WithNewPeer(&txBody.Peer)
case hg.PEER_REMOVE:
validators = validators.WithRemovedPeer(&txBody.Peer)
currentPeers = currentPeers.WithRemovedPeer(&txBody.Peer)
// Update RemovedRound if removing self
if txBody.Peer.ID() == c.validator.ID() {
c.logger.Debugf("Update RemovedRound from %d to %d", c.removedRound, effectiveRound)
c.removedRound = effectiveRound
}
default:
c.logger.Errorf("Unknown InternalTransactionType %s", txBody.Type)
continue
}
changed = true
} else {
c.logger.WithField("peer", txBody.Peer).Debug("InternalTransaction not accepted")
}
}
if changed {
// Record the new validator-set in the underlying Hashgraph and in the
// core's validators field
c.lastPeerChangeRound = effectiveRound
err := c.hg.Store.SetPeerSet(effectiveRound, validators)
if err != nil {
return fmt.Errorf("Updating Store PeerSet: %s", err)
}
c.validators = validators
c.logger.WithFields(logrus.Fields{
"effective_round": effectiveRound,
"validators": len(validators.Peers),
}).Info("Validators changed")
// Update the current list of communicating peers. This is not
// necessarily equal to the latest recorded validator_set.
c.setPeers(currentPeers)
// A new validator-set has been recorded and will only be effective from
// effectiveRound. A joining node will not be able to participate in the
// consensus until the Hashgraph reaches that effectiveRound. Hence, we
// force everyone to reach that round.
if effectiveRound > c.targetRound {
c.logger.Debugf("Update TargetRound from %d to %d", c.targetRound, effectiveRound)
c.targetRound = effectiveRound
}
}
for _, r := range receipts {
//respond to the corresponding promise
if p, ok := c.promises[r.InternalTransaction.HashString()]; ok {
if r.Accepted {
p.respond(true, effectiveRound, c.validators.Peers)
} else {
p.respond(false, 0, []*peers.Peer{})
}
delete(c.promises, r.InternalTransaction.HashString())
}
}
return nil
}
/*******************************************************************************
Diff
*******************************************************************************/
// eventDiff returns Events that we are aware of, and that are not known by
// another. They are returned in topological order. The parameter otherKnown is
// a map containing the last Event index per participant, as seen by another
// peer. We compare this to our view of events and return the diff.
func (c *core) eventDiff(otherKnown map[uint32]int) (events []*hg.Event, err error) {
// unknown is the container for the Events that will be returned by this
// method.
unknown := []*hg.Event{}
myknown := c.knownEvents()
// We loop through our known map first
for id := range myknown {
ct, ok := otherKnown[id]
// If the other is not yet aware of this validator. It will need all
// it's events (starting at index -1).
if !ok {
ct = -1
}
peer, ok := c.hg.Store.RepertoireByID()[id]
if !ok {
continue
}
// get participant Events with index > ct
participantEvents, err := c.hg.Store.ParticipantEvents(peer.PubKeyString(), ct)
if err != nil {
return []*hg.Event{}, err
}
for _, e := range participantEvents {
ev, err := c.hg.Store.GetEvent(e)
if err != nil {
return []*hg.Event{}, err
}
unknown = append(unknown, ev)
}
}
sort.Sort(hg.ByTopologicalOrder(unknown))
return unknown, nil
}
// fromWire takes Wire Events and returns Hashgraph Events
func (c *core) fromWire(wireEvents []hg.WireEvent) ([]hg.Event, error) {
events := make([]hg.Event, len(wireEvents), len(wireEvents))
for i, w := range wireEvents {
ev, err := c.hg.ReadWireInfo(w)
if err != nil {
return nil, err
}
events[i] = *ev
}
return events, nil
}
// toWire takes Hashgraph Events and returns Wire Events
func (c *core) toWire(events []*hg.Event) ([]hg.WireEvent, error) {
wireEvents := make([]hg.WireEvent, len(events), len(events))
for i, e := range events {
wireEvents[i] = e.ToWire()
}
return wireEvents, nil
}
/*******************************************************************************
Pools
*******************************************************************************/
// processSigPool calls Hashgraph ProcessSigPool
func (c *core) processSigPool() error {
return c.hg.ProcessSigPool()
}
// addTransactions appends transactions to the transaction pool
func (c *core) addTransactions(txs [][]byte) {
c.transactionPool = append(c.transactionPool, txs...)
}
// addInternalTransaction adds an InternalTransaction to the pool, and creates
// a corresponding promise.
func (c *core) addInternalTransaction(tx hg.InternalTransaction) *joinPromise {
promise := newJoinPromise(tx)
// Save it to promise store, for later use by the Commit callback
c.promises[tx.HashString()] = promise
// Submit the internal tx to be processed asynchronously by the gossip
// routines
c.internalTransactionPool = append(c.internalTransactionPool, tx)
return promise
}
/*******************************************************************************
Getters
*******************************************************************************/
// getHead returns the head from the hashgraph store
func (c *core) getHead() (*hg.Event, error) {
return c.hg.Store.GetEvent(c.head)
}
// getEvent returns an event from the hashgrapg store
func (c *core) getEvent(hash string) (*hg.Event, error) {
return c.hg.Store.GetEvent(hash)
}
// getEventTransactions returns the transactions for an event
func (c *core) getEventTransactions(hash string) ([][]byte, error) {
var txs [][]byte
ex, err := c.getEvent(hash)
if err != nil {
return txs, err
}
txs = ex.Transactions()
return txs, nil
}
// getConsensusEvents returns consensus events from the hashgragh store
func (c *core) getConsensusEvents() []string {
return c.hg.Store.ConsensusEvents()
}
// getConsensusEventsCount returns the count of consensus events from the
// hashgragh store
func (c *core) getConsensusEventsCount() int {
return c.hg.Store.ConsensusEventsCount()
}
// getUndeterminedEvents returns undetermined events from the hashgraph
func (c *core) getUndeterminedEvents() []string {
return c.hg.UndeterminedEvents
}
// getPendingLoadedEvents returns pendign loading events from the hashgraph
func (c *core) getPendingLoadedEvents() int {
return c.hg.PendingLoadedEvents
}
// getConsensusTransactions returns the transaction from the events returned by
// getConsensusEvents()
func (c *core) getConsensusTransactions() ([][]byte, error) {
txs := [][]byte{}
for _, e := range c.getConsensusEvents() {
eTxs, err := c.getEventTransactions(e)
if err != nil {
return txs, fmt.Errorf("Consensus event not found: %s", e)
}
txs = append(txs, eTxs...)
}
return txs, nil
}
// getLastConsensusRoundIndex returns the Last Consensus Round from the hashgraph
func (c *core) getLastConsensusRoundIndex() *int {
return c.hg.LastConsensusRound
}
// getConsensusTransactionsCount return ConsensusTransacions from the hashgraph
func (c *core) getConsensusTransactionsCount() int {
return c.hg.ConsensusTransactions
}
// getLastCommitedRoundEventsCount returns LastCommitedRoundEvents from the
// hashgraph
func (c *core) getLastCommitedRoundEventsCount() int {
return c.hg.LastCommitedRoundEvents
}
// getLastBlockIndex returns last block index from the hashgraph store
func (c *core) getLastBlockIndex() int {
return c.hg.Store.LastBlockIndex()
}