-
Notifications
You must be signed in to change notification settings - Fork 42
/
ledger.go
1134 lines (879 loc) · 27.3 KB
/
ledger.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 (c) 2019 Perlin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package wavelet
import (
"bytes"
"context"
"encoding/hex"
"sync"
"time"
"github.com/perlin-network/noise"
"github.com/perlin-network/noise/skademlia"
"github.com/perlin-network/wavelet/avl"
"github.com/perlin-network/wavelet/conf"
"github.com/perlin-network/wavelet/internal/backoff"
"github.com/perlin-network/wavelet/internal/cuckoo"
"github.com/perlin-network/wavelet/internal/filebuffer"
"github.com/perlin-network/wavelet/internal/radix"
"github.com/perlin-network/wavelet/internal/stall"
"github.com/perlin-network/wavelet/internal/worker"
"github.com/perlin-network/wavelet/log"
"github.com/perlin-network/wavelet/store"
"github.com/perlin-network/wavelet/sys"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
)
var (
ErrMissingTx = errors.New("missing transaction")
ErrTxInvalidSignature = errors.New("bad tx signature")
)
type Ledger struct {
client *skademlia.Client
metrics *Metrics
indexer *radix.Indexer
accounts *Accounts
blocks *Blocks
transactions *Transactions
db store.KV
gossiper *Gossiper
finalizer *Snowball
consensus sync.WaitGroup
consensusStop chan struct{}
stallDetector *stall.Detector
filePool *filebuffer.Pool
syncManager *SyncManager
stopWG sync.WaitGroup
cancelGC context.CancelFunc
transactionFilterLock sync.RWMutex
transactionFilter *cuckoo.Filter
queryPeerBlockCache *PeerBlockLRU
queryBlockValidCache map[BlockID]struct{}
queryWorkerPool *worker.Pool
collapseResultsLogger *CollapseResultsLogger
}
type config struct {
GCDisabled bool
Genesis *string
MaxMemoryMB uint64
}
type Option func(cfg *config)
// WithoutGC disables GC. Used for testing purposes.
func WithoutGC() Option {
return func(cfg *config) {
cfg.GCDisabled = true
}
}
func WithGenesis(genesis *string) Option {
return func(cfg *config) {
cfg.Genesis = genesis
}
}
func WithMaxMemoryMB(n uint64) Option {
return func(cfg *config) {
cfg.MaxMemoryMB = n
}
}
func NewLedger(kv store.KV, client *skademlia.Client, opts ...Option) (*Ledger, error) {
var cfg config
for _, opt := range opts {
opt(&cfg)
}
metrics := NewMetrics(context.TODO())
indexer := radix.NewIndexer()
accounts := NewAccounts(kv)
var block *Block
blocks, err := NewBlocks(kv, conf.GetPruningLimit())
if err != nil {
if errors.Cause(err) != store.ErrNotFound {
return nil, errors.Wrap(err, "error getting blocks from db")
}
genesis := performInception(accounts.tree, cfg.Genesis)
if err := accounts.Commit(nil); err != nil {
return nil, errors.Wrap(err, "error committing accounts from genesis")
}
ptr := &genesis
if _, err := blocks.Save(ptr); err != nil {
return nil, errors.Wrap(err, "error saving genesis block to db")
}
block = ptr
} else {
block = blocks.Latest()
}
transactions := NewTransactions(*block)
transactions.BatchMarkFinalized(LoadFinalizedTransactionIDs(accounts.tree)...)
gossiper := NewGossiper(context.TODO(), client, metrics)
finalizer := NewSnowball()
filePool := filebuffer.NewPool(sys.SyncPooledFileSize, "")
syncManager := NewSyncManager(client, accounts, blocks, filePool)
ledger := &Ledger{
client: client,
metrics: metrics,
indexer: indexer,
accounts: accounts,
blocks: blocks,
transactions: transactions,
db: kv,
gossiper: gossiper,
finalizer: finalizer,
filePool: filePool,
syncManager: syncManager,
transactionFilter: cuckoo.NewFilter(),
queryPeerBlockCache: NewPeerBlockLRU(16),
queryBlockValidCache: make(map[BlockID]struct{}),
queryWorkerPool: worker.NewWorkerPool(),
collapseResultsLogger: NewCollapseResultsLogger(),
}
var kickstart sync.Once
syncManager.OnStateReconciled = append(syncManager.OnStateReconciled, func(outOfSync bool) {
if outOfSync {
syncManager.logger.Info().Msg("Peers have reported to us that we are out of sync. " +
"Initializing state syncing...")
if ledger.consensusStop != nil {
close(ledger.consensusStop)
ledger.consensus.Wait()
}
} else {
kickstart.Do(func() {
if ledger.consensusStop == nil {
ledger.consensusStop = make(chan struct{})
ledger.PerformConsensus()
}
})
}
})
syncManager.OnSynced = append(syncManager.OnSynced, func(block Block) {
ledger.transactions.ReshufflePending(block)
ledger.transactionFilterLock.Lock()
ledger.transactionFilter.Reset()
ledger.transactions.Iterate(func(tx *Transaction) bool {
ledger.transactionFilter.Insert(tx.ID)
return true
})
ledger.transactionFilterLock.Unlock()
ledger.transactions.BatchMarkFinalized(LoadFinalizedTransactionIDs(accounts.tree)...)
if _, err = ledger.blocks.Save(&block); err != nil {
logger := log.Node()
logger.Error().
Err(err).
Msg("Failed to save preferred block to database")
}
ledger.consensusStop = make(chan struct{})
ledger.PerformConsensus()
})
if !cfg.GCDisabled {
ctx, cancel := context.WithCancel(context.Background())
ledger.stopWG.Add(1)
go accounts.GC(ctx, &ledger.stopWG)
ledger.cancelGC = cancel
}
stallDetector := stall.NewStallDetector(stall.Config{
MaxMemoryMB: cfg.MaxMemoryMB,
}, stall.Delegate{
PrepareShutdown: func(err error) {
logger := log.Node()
logger.Error().Err(err).Msg("Shutting down node...")
},
})
ledger.stopWG.Add(1)
go stallDetector.Run(&ledger.stopWG)
ledger.stallDetector = stallDetector
ledger.queryWorkerPool.Start(16)
go ledger.syncManager.Start()
return ledger, nil
}
// Close stops all goroutines and waits for them to complete.
func (l *Ledger) Close() {
l.syncManager.Stop()
if l.consensusStop != nil {
close(l.consensusStop)
}
l.consensus.Wait()
if l.cancelGC != nil {
l.cancelGC()
}
l.queryWorkerPool.Stop()
l.stallDetector.Stop()
l.collapseResultsLogger.Stop()
l.stopWG.Wait()
}
// AddTransaction adds a transaction to the ledger and adds it's id to a probabilistic
// data structure used to sync transactions.
func (l *Ledger) AddTransaction(txs ...Transaction) {
l.transactions.BatchAdd(txs)
l.transactionFilterLock.Lock()
for _, tx := range txs {
l.transactionFilter.Insert(tx.ID)
l.gossiper.Push(tx)
}
l.transactionFilterLock.Unlock()
}
// Find searches through complete transaction and account indices for a specified
// query string. All indices that queried are in the form of tries. It is safe
// to call this method concurrently.
func (l *Ledger) Find(query string, max int) (results []string) {
var err error
if max > 0 {
results = make([]string, 0, max)
}
prefix := []byte(query)
if len(query)%2 == 1 { // Cut off a single character.
prefix = prefix[:len(prefix)-1]
}
prefix, err = hex.DecodeString(string(prefix))
if err != nil {
return nil
}
fullQuery := append(keyAccounts[:], prefix...)
l.Snapshot().IterateFrom(fullQuery, func(key, _ []byte) bool {
if !bytes.HasPrefix(key, fullQuery) {
return false
}
if max > 0 && len(results) >= max {
return false
}
results = append(results, hex.EncodeToString(key[len(keyAccounts):]))
return true
})
var count = -1
if max > 0 {
count = max - len(results)
}
return append(results, l.indexer.Find(query, count)...)
}
// Protocol returns an implementation of WaveletServer to handle incoming
// RPC and streams for the ledger. The protocol is agnostic to whatever
// choice of network stack is used with Wavelet, though by default it is
// intended to be used with gRPC and Noise.
func (l *Ledger) Protocol() *Protocol {
return &Protocol{ledger: l}
}
// Finalizer returns the Snowball finalizer which finalizes the contents of individual
// blocks.
func (l *Ledger) Finalizer() *Snowball {
return l.finalizer
}
// Blocks returns the block manager for the ledger.
func (l *Ledger) Blocks() *Blocks {
return l.blocks
}
// Transactions returns the transaction manager for the ledger.
func (l *Ledger) Transactions() *Transactions {
return l.transactions
}
// Restart restart wavelet process by means of stall detector (approach is platform dependent)
func (l *Ledger) Restart() error {
return l.stallDetector.TryRestart()
}
// PerformConsensus spawns workers related to performing consensus, such as pulling
// missing transactions and incrementally finalizing intervals of transactions in
// the ledgers graph.
func (l *Ledger) PerformConsensus() {
l.consensus.Add(2)
go l.PullMissingTransactions()
//go l.SyncTransactions()
go l.FinalizeBlocks()
}
func (l *Ledger) Snapshot() *avl.Tree {
return l.accounts.Snapshot()
}
// SyncTransactions is an infinite loop which constantly sends transaction ids from its index
// into a Cuckoo Filter to randomly sampled number of peers and adds to it's state all received
// transactions.
func (l *Ledger) SyncTransactions() { // nolint:gocognit
defer l.consensus.Done()
for {
select {
case <-time.After(1 * time.Second):
case <-l.consensusStop:
return
}
snowballK := conf.GetSnowballK()
peers, err := SelectPeers(l.client.ClosestPeers(), snowballK)
if err != nil {
continue
}
logger := log.Sync("sync_tx")
l.transactionFilterLock.RLock()
cf := l.transactionFilter.MarshalBinary()
l.transactionFilterLock.RUnlock()
if err != nil {
logger.Error().Err(err).Msg("failed to marshal set membership filter data")
continue
}
snapshot := l.Snapshot()
var wg sync.WaitGroup
bfReq := &TransactionsSyncRequest{
Data: &TransactionsSyncRequest_Filter{
Filter: cf,
},
}
for _, p := range peers {
wg.Add(1)
go func(conn *grpc.ClientConn) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), conf.GetDownloadTxTimeout())
defer cancel()
stream, err := NewWaveletClient(conn).SyncTransactions(ctx)
if err != nil {
logger.Error().Err(err).Msg("failed to create sync transactions stream")
return
}
defer func() {
if err := stream.CloseSend(); err != nil {
logger.Error().Err(err).Msg("failed to close sync transaction stream")
}
}()
if err := stream.Send(bfReq); err != nil {
logger.Error().Err(err).Msg("failed to send set membership filter data")
return
}
res, err := stream.Recv()
if err != nil {
logger.Error().Err(err).Msg("failed to receive sync transactions header")
return
}
count := res.GetTransactionsNum()
if count == 0 {
return
}
if count > conf.GetTXSyncLimit() {
logger.Debug().
Uint64("count", count).
Str("peer_address", conn.Target()).
Msg("Bad number of transactions would be received")
return
}
logger.Debug().
Uint64("count", count).
Msg("Requesting transaction(s) to sync.")
for count > 0 {
req := TransactionsSyncRequest_ChunkSize{
ChunkSize: conf.GetTXSyncChunkSize(),
}
if count < req.ChunkSize {
req.ChunkSize = count
}
if err := stream.Send(&TransactionsSyncRequest{Data: &req}); err != nil {
logger.Error().Err(err).Msg("failed to send sync transactions request")
return
}
res, err := stream.Recv()
if err != nil {
logger.Error().Err(err).Msg("failed to receive sync transactions response")
return
}
txResponse := res.GetTransactions()
if txResponse == nil {
return
}
transactions := make([]Transaction, 0, len(txResponse.Transactions))
for _, txBody := range txResponse.Transactions {
tx, err := UnmarshalTransaction(bytes.NewReader(txBody))
if err != nil {
logger.Error().Err(err).Msg("failed to unmarshal synced transaction")
continue
}
if err := ValidateTransaction(snapshot, tx); err != nil && err != ErrContractAlreadyExists {
logger.Error().
Err(err).
Hex("tx_id", tx.ID[:]).
Msg("transaction validation error")
continue
}
transactions = append(transactions, tx)
}
downloadedNum := len(transactions)
if downloadedNum == 0 {
logger.Warn().
Uint64("transaction_to_sync", count).
Msg("No transactions to add while there are still missing transactions")
return
}
count -= uint64(downloadedNum)
l.AddTransaction(transactions...)
l.metrics.downloadedTX.Mark(int64(downloadedNum))
l.metrics.receivedTX.Mark(int64(downloadedNum))
}
}(p.Conn())
}
wg.Wait()
}
}
// PullMissingTransactions is a goroutine which continuously pulls missing transactions
// from randomly sampled peers in the network. It does so by sending a list of
// transaction IDs which node is missing.
func (l *Ledger) PullMissingTransactions() {
defer l.consensus.Done()
for {
select {
case <-time.After(100 * time.Millisecond):
case <-l.consensusStop:
return
}
snowballK := conf.GetSnowballK()
peers, err := SelectPeers(l.client.ClosestPeers(), snowballK)
if err != nil {
continue
}
logger := log.Sync("pull_missing_tx")
// Build list of transaction IDs
missingIDs := l.transactions.MissingIDs()
missingTxPullLimit := conf.GetMissingTxPullLimit()
if uint64(len(missingIDs)) > missingTxPullLimit {
missingIDs = missingIDs[:missingTxPullLimit]
}
req := &TransactionPullRequest{TransactionIds: make([][]byte, 0, len(missingIDs))}
for _, txID := range missingIDs {
txID := txID
req.TransactionIds = append(req.TransactionIds, txID[:])
}
type response struct {
txs []Transaction
}
responseChan := make(chan response)
for _, p := range peers {
go func(conn *grpc.ClientConn) {
var response response
defer func() {
responseChan <- response
}()
client := NewWaveletClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), conf.GetDownloadTxTimeout())
defer cancel()
batch, err := client.PullTransactions(ctx, req)
if err != nil {
logger.Error().Err(err).Msg("failed to download missing transactions")
return
}
response.txs = make([]Transaction, 0, len(batch.Transactions))
for _, buf := range batch.Transactions {
tx, err := UnmarshalTransaction(bytes.NewReader(buf))
if err != nil {
logger.Error().
Err(err).
Hex("tx_id", tx.ID[:]).
Msg("error unmarshaling downloaded tx")
continue
}
response.txs = append(response.txs, tx)
}
}(p.Conn())
}
count := int64(0)
pulled := make(map[TransactionID]Transaction)
for i := 0; i < len(peers); i++ {
res := <-responseChan
for i := range res.txs {
if _, ok := pulled[res.txs[i].ID]; !ok {
pulled[res.txs[i].ID] = res.txs[i]
count += int64(res.txs[i].LogicalUnits())
}
}
}
close(responseChan)
pulledTXs := make([]Transaction, 0, len(pulled))
snapshot := l.Snapshot()
for _, tx := range pulled {
if err := ValidateTransaction(snapshot, tx); err != nil {
if err == ErrTxInvalidSignature {
logger.Error().
Hex("tx_id", tx.ID[:]).
Msg("bad signature")
}
continue
}
pulledTXs = append(pulledTXs, tx)
}
l.AddTransaction(pulledTXs...)
if count > 0 {
logger.Info().
Int64("count", count).
Msg("Pulled missing transaction(s).")
}
l.metrics.downloadedTX.Mark(count)
l.metrics.receivedTX.Mark(count)
}
}
// FinalizeBlocks continuously attempts to finalize blocks.
func (l *Ledger) FinalizeBlocks() {
defer l.consensus.Done()
b := &backoff.Backoff{Min: 0 * time.Second, Max: 200 * time.Millisecond, Factor: 1.25, Jitter: true}
for {
select {
case <-l.consensusStop:
return
default:
}
decided := l.finalizer.Decided()
preferred := l.finalizer.Preferred()
if preferred == nil {
proposedBlock := l.proposeBlock()
if proposedBlock != nil {
logger := log.Consensus("proposal")
logger.Debug().
Hex("block_id", proposedBlock.ID[:]).
Uint64("block_index", proposedBlock.Index).
Int("num_transactions", len(proposedBlock.Transactions)).
Msg("Proposing block...")
l.finalizer.Prefer(&finalizationVote{
voter: l.client.ID(),
block: proposedBlock,
})
b.Reset()
} else {
t := time.NewTicker(b.Duration())
select {
case <-l.consensusStop:
t.Stop()
return
case <-t.C:
t.Stop()
}
}
} else {
if decided {
l.finalize(*preferred.Value().(*Block))
} else {
l.query()
}
}
}
}
// proposeBlock takes all transactions from the first quarter of the mempool
// and creates a new block, which will be proposed to be finalized as the
// next block in the chain.
func (l *Ledger) proposeBlock() *Block {
proposing := l.transactions.ProposableIDs()
if len(proposing) == 0 {
return nil
}
latest := l.blocks.Latest()
results, err := l.collapseTransactions(latest.Index+1, latest, proposing, false)
if err != nil {
logger := log.Node()
logger.Error().
Err(err).
Msg("error collapsing transactions during block proposal")
return nil
}
proposed := NewBlock(latest.Index+1, results.snapshot.Checksum(), proposing...)
return &proposed
}
func (l *Ledger) finalize(block Block) {
current := l.blocks.Latest()
logger := log.Consensus("finalized")
results, err := l.collapseTransactions(block.Index, current, block.Transactions, true)
if err != nil {
logger := log.Node()
logger.Error().
Err(err).
Msg("error collapsing transactions during finalization")
return
}
if checksum := results.snapshot.Checksum(); checksum != block.Merkle {
logger := log.Node()
logger.Error().
Uint64("target_block_id", block.Index).
Hex("expected_merkle_root", block.Merkle[:]).
Hex("yielded_merkle_root", checksum[:]).
Msg("Merkle root does not match")
return
}
pruned := l.transactions.ReshufflePending(block)
l.transactionFilterLock.Lock()
for _, id := range pruned {
l.transactionFilter.Delete(id)
}
l.transactionFilterLock.Unlock()
if _, err = l.blocks.Save(&block); err != nil {
logger := log.Node()
logger.Error().
Err(err).
Msg("Failed to save preferred block to database")
return
}
if err = l.accounts.Commit(results.snapshot); err != nil {
logger := log.Node()
logger.Error().
Err(err).
Msg("Failed to commit collaped state to our database")
return
}
l.metrics.acceptedTX.Mark(int64(results.appliedCount))
l.metrics.finalizedBlocks.Mark(1)
l.LogChanges(results)
// Reset sampler(s).
l.finalizer.Reset()
// Reset querying-related cache(s).
for id := range l.queryBlockValidCache {
delete(l.queryBlockValidCache, id)
}
logger.Info().
Int("num_applied_tx", results.appliedCount).
Int("num_rejected_tx", results.rejectedCount).
Int("num_pruned_tx", len(pruned)).
Uint64("old_block_height", current.Index).
Uint64("new_block_height", block.Index).
Hex("old_block_id", current.ID[:]).
Hex("new_block_id", block.ID[:]).
Msg("Finalized block.")
}
func (l *Ledger) query() {
snowballK := conf.GetSnowballK()
peers, err := SelectPeers(l.client.ClosestPeers(), snowballK)
if err != nil {
return
}
current := l.blocks.Latest()
type response struct {
vote finalizationVote
}
responseChan := make(chan response)
for _, p := range peers {
conn := p.Conn()
cached, _ := l.queryPeerBlockCache.Load(p.ID().Checksum())
f := func() {
var response response
defer func() {
responseChan <- response
}()
req := &QueryRequest{BlockIndex: current.Index + 1}
if cached != nil {
req.CacheBlockId = make([]byte, SizeBlockID)
copy(req.CacheBlockId, cached.ID[:])
response.vote.block = cached
}
f := func() {
client := NewWaveletClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), conf.GetQueryTimeout())
defer cancel()
p := &peer.Peer{}
res, err := client.Query(ctx, req, grpc.Peer(p))
if err != nil {
logger := log.Node()
logger.Error().
Err(err).
Msg("error while querying peer")
return
}
l.metrics.queried.Mark(1)
info := noise.InfoFromPeer(p)
if info == nil {
return
}
voter, ok := info.Get(skademlia.KeyID).(*skademlia.ID)
if !ok {
return
}
response.vote.voter = voter
if res.CacheValid {
return
}
block, err := UnmarshalBlock(bytes.NewReader(res.GetBlock()))
if err != nil {
return
}
if block.ID == ZeroBlockID {
return
}
response.vote.block = &block
}
l.metrics.queryLatency.Time(f)
}
l.queryWorkerPool.Queue(f)
}
votes := make([]Vote, 0, len(peers))
voters := make(map[AccountID]struct{}, len(peers))
for i := 0; i < cap(votes); i++ {
response := <-responseChan
if response.vote.voter == nil {
continue
}
if _, recorded := voters[response.vote.voter.PublicKey()]; recorded {
continue // To make sure the sampling process is fair, only allow one vote per peer.
}
voters[response.vote.voter.PublicKey()] = struct{}{}
if response.vote.block != nil {
l.queryPeerBlockCache.Put(response.vote.voter.Checksum(), response.vote.block)
}
votes = append(votes, &response.vote)
}
// Include our own vote as well.
var preferred *Block = nil
if vote, ok := l.finalizer.Preferred().(*finalizationVote); ok && vote != nil {
preferred = vote.block
}
votes = append(votes, &finalizationVote{voter: l.client.ID(), block: preferred})
l.filterInvalidVotes(current, votes)
l.finalizer.Tick(calculateTallies(l.accounts, votes))
}
// collapseResults is what returned by calling collapseTransactions. Refer to collapseTransactions
// to understand what counts of accepted, rejected, or otherwise ignored transactions truly represent
// after calling collapseTransactions.
type collapseResults struct {
applied []*Transaction
rejected []*Transaction
rejectedErrors []error
appliedCount int
rejectedCount int
snapshot *avl.Tree
ctx *CollapseContext
}
// collapseTransactions takes all transactions recorded within a block, and applies all valid
// and available ones to a snapshot of all accounts stored in the ledger. It returns an updated
// snapshot with all finalized transactions applied, alongside count summaries of the number of
// applied, rejected, or otherwise ignored transactions.
func (l *Ledger) collapseTransactions(
height uint64, current *Block, proposed []TransactionID, logging bool,
) (*collapseResults, error) {
transactions, err := l.transactions.BatchFind(proposed)
if err != nil {
return nil, errors.Wrap(err, "could not find transactions to collapse in node")
}
results, err := collapseTransactions(height, transactions, current, l.accounts)
if err != nil {
return nil, errors.Wrap(err, "failed to collapse transactions")
}
if logging && results != nil {
l.collapseResultsLogger.Log(results)
}
return results, err
}
// LogChanges logs all changes made to an AVL tree state snapshot for the purposes
// of logging out changes to account state to Wavelet's HTTP API.
func (l *Ledger) LogChanges(c *collapseResults) {
balanceLogger := log.Accounts("balance_updated")
gasBalanceLogger := log.Accounts("gas_balance_updated")
stakeLogger := log.Accounts("stake_updated")