-
Notifications
You must be signed in to change notification settings - Fork 296
/
get_successors_handler.rs
926 lines (849 loc) · 35 KB
/
get_successors_handler.rs
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
use std::{
collections::{HashSet, VecDeque},
sync::Arc,
};
use bitcoin::{Block, BlockHash, BlockHeader, Network};
use ic_btc_validation::is_beyond_last_checkpoint;
use ic_metrics::MetricsRegistry;
use tokio::sync::{mpsc::Sender, Mutex};
use tonic::{Code, Status};
use crate::{
common::BlockHeight, config::Config, metrics::GetSuccessorMetrics, BlockchainManagerRequest,
BlockchainState,
};
// Max size of the `GetSuccessorsResponse` message.
// This number is slightly less than the maximum payload size a canister can send (2MiB)
// to leave a small buffer for the additional space that candid encoding may need.
//
// NOTE: Should be = the `MAX_RESPONSE_SIZE` defined in `replicated_state/bitcoin.rs`
// for pagination on the replica side to work as expected.
const MAX_RESPONSE_SIZE: usize = 2_000_000;
// Max number of next block headers that can be returned in the `GetSuccessorsResponse`.
const MAX_NEXT_BLOCK_HEADERS_LENGTH: usize = 100;
// Max number of blocks that can be returned in the `GetSuccessorsResponse`.
// We limit the number of blocks because serializing many blocks to pb can take some time.
const MAX_BLOCKS_LENGTH: usize = 100;
const BLOCK_HEADER_SIZE: usize = 80;
// The maximum number of bytes the `next` field in a response can take.
const MAX_NEXT_BYTES: usize = MAX_NEXT_BLOCK_HEADERS_LENGTH * BLOCK_HEADER_SIZE;
// The maximum number of bytes the `blocks` in a response can take.
// NOTE: This is a soft limit, and is only honored if there's > 1 blocks already in the response.
// Having this as a soft limit as necessary to prevent large blocks from stalling consensus.
const MAX_BLOCKS_BYTES: usize = MAX_RESPONSE_SIZE - MAX_NEXT_BYTES;
// Max height for sending multiple blocks when connecting the Bitcoin mainnet.
const MAINNET_MAX_MULTI_BLOCK_ANCHOR_HEIGHT: BlockHeight = 750_000;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GetSuccessorsRequest {
/// Hash of the most recent stable block in the Bitcoin canister.
pub anchor: BlockHash,
/// Most recent block hashes that have been processed by the canister.
pub processed_block_hashes: Vec<BlockHash>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GetSuccessorsResponse {
/// Blocks found in the block cache.
pub blocks: Vec<Block>,
/// Next set of headers to be sent to the canister.
pub next: Vec<BlockHeader>,
}
/// Contains the functionality to respond to GetSuccessorsRequests via the RPC
/// server.
pub struct GetSuccessorsHandler {
state: Arc<Mutex<BlockchainState>>,
blockchain_manager_tx: Sender<BlockchainManagerRequest>,
last_request: std::sync::Mutex<Option<(GetSuccessorsRequest, GetSuccessorsResponse)>>,
network: Network,
metrics: GetSuccessorMetrics,
}
impl GetSuccessorsHandler {
/// Creates a GetSuccessorsHandler to be used to access the blockchain state
/// inside of the adapter when a `GetSuccessorsRequest` is received.
pub fn new(
config: &Config,
state: Arc<Mutex<BlockchainState>>,
blockchain_manager_tx: Sender<BlockchainManagerRequest>,
metrics_registry: &MetricsRegistry,
) -> Self {
Self {
state,
blockchain_manager_tx,
last_request: std::sync::Mutex::new(None),
network: config.network,
metrics: GetSuccessorMetrics::new(metrics_registry),
}
}
// TODO: ER-2157: GetSuccessors should only sync after the adapter is synced past the
// highest checkpoint.
// TODO: ER-2479: Pruning blocks from the cache should also consider the height of the anchor hash.
/// Handles a request for get successors. The response will contain the blocks that the adapter
/// currently contains in its cache as well as the headers for the next blocks.
/// If the channels are full, PruneOldBlocks and EnqueueNewBlocksToDownload will not be executed.
pub async fn get_successors(
&self,
request: GetSuccessorsRequest,
) -> Result<GetSuccessorsResponse, Status> {
self.metrics
.processed_block_hashes
.observe(request.processed_block_hashes.len() as f64);
if self.network == Network::Testnet || self.network == Network::Regtest {
// A cache entry has to be discarded after after a new request.
// Indexing the cache based on the request is not safe since the response
// depends on the internal state of the adapter.
if let Some((cached_request, cached_response)) =
self.last_request.lock().unwrap().take()
{
if cached_request == request {
return Ok(cached_response);
}
}
}
let response = {
let state = self.state.lock().await;
let anchor_height = state
.get_cached_header(&request.anchor)
.map_or(0, |cached| cached.height);
// Wait with downloading blocks until we synced the header chain above the last checkpoint
// to make sure we are following the correct chain.
if !is_beyond_last_checkpoint(&self.network, state.get_active_chain_tip().height) {
return Err(Status::new(
Code::Unavailable,
"Header chain not yet synced past last checkpoint",
));
}
let allow_multiple_blocks = are_multiple_blocks_allowed(self.network, anchor_height);
let blocks = get_successor_blocks(
&state,
&request.anchor,
&request.processed_block_hashes,
allow_multiple_blocks,
);
let next = get_next_headers(
&state,
&request.anchor,
&request.processed_block_hashes,
&blocks,
);
GetSuccessorsResponse { blocks, next }
};
self.metrics
.response_blocks
.observe(response.blocks.len() as f64);
// Set cache value
if self.network == Network::Testnet || self.network == Network::Regtest {
self.last_request
.lock()
.unwrap()
.replace((request.clone(), response.clone()));
}
if !response.next.is_empty() {
// TODO: better handling of full channel as the receivers are never closed.
self.blockchain_manager_tx
.try_send(BlockchainManagerRequest::EnqueueNewBlocksToDownload(
response.next.clone(),
))
.ok();
}
// TODO: better handling of full channel as the receivers are never closed.
self.blockchain_manager_tx
.try_send(BlockchainManagerRequest::PruneBlocks(
request.anchor,
request.processed_block_hashes,
))
.ok();
Ok(response)
}
}
// Performs a breadth-first search to retrieve blocks from the block cache.
//
// If blocks are available and `allow_multiple_blocks` is `true`, then as many blocks are returned
// as possible that fit in the `MAX_BLOCKS_BYTES` limit, with a minimum of one block. Otherwise, a
// single block is returned.
fn get_successor_blocks(
state: &BlockchainState,
anchor: &BlockHash,
processed_block_hashes: &[BlockHash],
allow_multiple_blocks: bool,
) -> Vec<Block> {
let seen: HashSet<BlockHash> = processed_block_hashes.iter().copied().collect();
let mut successor_blocks = vec![];
// Block hashes that should be looked at in subsequent breadth-first searches.
let mut response_block_size: usize = 0;
let mut queue: VecDeque<BlockHash> = state
.get_cached_header(anchor)
.map(|c| c.children.clone())
.unwrap_or_default()
.into_iter()
.collect();
// Compute the blocks by starting a breadth-first search.
while let Some(block_hash) = queue.pop_front() {
if !seen.contains(&block_hash) {
// Retrieve the block from the cache.
match state.get_block(&block_hash) {
Some(block) => {
let block_size = block.size();
if response_block_size == 0
|| (response_block_size + block_size <= MAX_BLOCKS_BYTES
&& successor_blocks.len() < MAX_BLOCKS_LENGTH
&& allow_multiple_blocks)
{
successor_blocks.push(block.clone());
response_block_size += block_size;
} else {
break;
}
}
None => {
// Cache miss has occurred. This block or any of its successors cannot
// be returned. Discarding this subtree from the BFS.
continue;
}
}
}
queue.extend(
state
.get_cached_header(&block_hash)
.map(|header| header.children.clone())
.unwrap_or_default(),
);
}
successor_blocks
}
/// Get the next headers for blocks that may possibly be sent in upcoming GetSuccessor responses.
fn get_next_headers(
state: &BlockchainState,
anchor: &BlockHash,
processed_block_hashes: &[BlockHash],
blocks: &[Block],
) -> Vec<BlockHeader> {
let seen: HashSet<BlockHash> = processed_block_hashes
.iter()
.copied()
.chain(blocks.iter().map(|b| b.block_hash()))
.collect();
let mut queue: VecDeque<BlockHash> = state
.get_cached_header(anchor)
.map(|c| c.children.clone())
.unwrap_or_default()
.into_iter()
.collect();
let mut next_headers = vec![];
while let Some(block_hash) = queue.pop_front() {
if next_headers.len() >= MAX_NEXT_BLOCK_HEADERS_LENGTH {
break;
}
if let Some(header_node) = state.get_cached_header(&block_hash) {
if !seen.contains(&block_hash) {
next_headers.push(header_node.header);
}
queue.extend(header_node.children.clone());
}
}
next_headers
}
/// Helper used to determine if multiple blocks should be returned.
fn are_multiple_blocks_allowed(network: Network, anchor_height: BlockHeight) -> bool {
match network {
Network::Bitcoin => anchor_height <= MAINNET_MAX_MULTI_BLOCK_ANCHOR_HEIGHT,
Network::Testnet | Network::Signet | Network::Regtest => true,
}
}
#[cfg(test)]
mod test {
use super::*;
use std::sync::Arc;
use bitcoin::Network;
use ic_metrics::MetricsRegistry;
use tokio::sync::{mpsc::channel, Mutex};
use crate::config::test::ConfigBuilder;
use ic_btc_adapter_test_utils::{
generate_headers, generate_large_block_blockchain, headers_to_hashes,
};
/// This tests ensures that `BlockchainManager::get_successors(...)` will return relevant blocks
/// with the next headers of many forks and enqueue missing block hashes.
#[tokio::test]
async fn test_get_successors() {
let config = ConfigBuilder::new().with_network(Network::Regtest).build();
let blockchain_state = BlockchainState::new(&config, &MetricsRegistry::default());
let genesis = *blockchain_state.genesis();
let genesis_hash = genesis.block_hash();
let (blockchain_manager_tx, _blockchain_manager_rx) =
channel::<BlockchainManagerRequest>(10);
let handler = GetSuccessorsHandler::new(
&config,
Arc::new(Mutex::new(blockchain_state)),
blockchain_manager_tx,
&MetricsRegistry::default(),
);
// Set up the following chain:
// |--> 1'---> 2'
// 0 -> 1 ---> 2 ---> 3 -> 4
// |--> 1'' -> 2'' -> 3''
let mut previous_hashes = vec![];
let main_chain = generate_headers(genesis_hash, genesis.time, 4, &[]);
previous_hashes.extend(
main_chain
.iter()
.map(|h| h.block_hash())
.collect::<Vec<_>>(),
);
let side_chain = generate_headers(genesis_hash, genesis.time, 2, &previous_hashes);
previous_hashes.extend(
side_chain
.iter()
.map(|h| h.block_hash())
.collect::<Vec<_>>(),
);
let side_chain_2 = generate_headers(genesis_hash, genesis.time, 3, &previous_hashes);
let main_block_2 = Block {
header: main_chain[1],
txdata: vec![],
};
let side_1 = side_chain.get(0).cloned().expect("Should have 1 header");
let side_block_1 = Block {
header: side_1,
txdata: vec![],
};
// Create a request with the anchor block as the block 0 and processed block hashes contain
// block 1 and 2.
let request = GetSuccessorsRequest {
anchor: genesis_hash,
processed_block_hashes: vec![main_chain[0].block_hash(), main_chain[1].block_hash()],
};
{
let mut blockchain = handler.state.lock().await;
blockchain.add_headers(&main_chain);
blockchain.add_headers(&side_chain);
blockchain.add_headers(&side_chain_2);
// Add main block 2
blockchain.add_block(main_block_2).expect("invalid block");
// Add side block 1
blockchain
.add_block(side_block_1.clone())
.expect("invalid block");
}
let response = handler.get_successors(request).await.unwrap();
// Check that blocks contain block 1.
assert_eq!(response.blocks.len(), 1);
assert!(
matches!(response.blocks.get(0), Some(block) if block.block_hash() == side_block_1.block_hash())
);
assert_eq!(response.next.len(), 6);
let next_hashes = response
.next
.iter()
.map(|h| h.block_hash())
.collect::<Vec<BlockHash>>();
assert_eq!(
next_hashes,
vec![
side_chain_2[0].block_hash(), // 1''
side_chain[1].block_hash(), // 2'
side_chain_2[1].block_hash(), // 2''
main_chain[2].block_hash(), // 3
side_chain_2[2].block_hash(), // 3'
main_chain[3].block_hash(), // 4
],
"main = {:#?}, side = {:#?}, side 2 = {:#?}, next hashes = {:#?}",
main_chain
.iter()
.map(|h| h.block_hash())
.collect::<Vec<BlockHash>>(),
side_chain
.iter()
.map(|h| h.block_hash())
.collect::<Vec<BlockHash>>(),
side_chain_2
.iter()
.map(|h| h.block_hash())
.collect::<Vec<BlockHash>>(),
next_hashes
);
}
#[tokio::test]
async fn test_get_successors_wait_header_sync_testnet() {
let config = ConfigBuilder::new().with_network(Network::Testnet).build();
let blockchain_state = BlockchainState::new(&config, &MetricsRegistry::default());
let genesis = *blockchain_state.genesis();
let genesis_hash = genesis.block_hash();
let (blockchain_manager_tx, _) = channel::<BlockchainManagerRequest>(10);
let handler = GetSuccessorsHandler::new(
&config,
Arc::new(Mutex::new(blockchain_state)),
blockchain_manager_tx,
&MetricsRegistry::default(),
);
// Set up the following chain:
// 0 -> 1 ---> 2 ---> 3 -> 4
let mut previous_hashes = vec![];
let main_chain = generate_headers(genesis_hash, genesis.time, 4, &[]);
previous_hashes.extend(
main_chain
.iter()
.map(|h| h.block_hash())
.collect::<Vec<_>>(),
);
// Create a request with the anchor block as the block 0 and processed block hashes contain
// block 1 and 2.x
let request = GetSuccessorsRequest {
anchor: genesis_hash,
processed_block_hashes: vec![],
};
{
let mut blockchain = handler.state.lock().await;
blockchain.add_headers(&main_chain);
}
let response = handler.get_successors(request).await;
// Since adapter is not yet passed highest checkpoint it should still be unavailbale.
// Highest checkpoint for testnet is 546.
assert_eq!(response.err().unwrap().code(), Code::Unavailable);
}
#[tokio::test]
async fn test_get_successors_wait_header_sync_regtest() {
let config = ConfigBuilder::new().with_network(Network::Regtest).build();
let blockchain_state = BlockchainState::new(&config, &MetricsRegistry::default());
let genesis = *blockchain_state.genesis();
let genesis_hash = genesis.block_hash();
let (blockchain_manager_tx, _blockchain_manager_rx) =
channel::<BlockchainManagerRequest>(10);
let handler = GetSuccessorsHandler::new(
&config,
Arc::new(Mutex::new(blockchain_state)),
blockchain_manager_tx,
&MetricsRegistry::default(),
);
// Set up the following chain:
// 0 -> 1 -> 2 -> 3 -> 4 -> 5
let main_chain = generate_headers(genesis_hash, genesis.time, 5, &[]);
let main_block_1 = Block {
header: main_chain[0],
txdata: vec![],
};
let main_block_2 = Block {
header: main_chain[1],
txdata: vec![],
};
{
let mut blockchain = handler.state.lock().await;
blockchain.add_headers(&main_chain);
blockchain
.add_block(main_block_1.clone())
.expect("invalid block");
blockchain
.add_block(main_block_2.clone())
.expect("invalid block");
}
let request = GetSuccessorsRequest {
anchor: genesis_hash,
processed_block_hashes: vec![],
};
let response = handler.get_successors(request).await.unwrap();
// Response should be contain the blocks and next headers since the regtest network does not have checkpoints.
assert_eq!(response.blocks.len(), 2);
assert_eq!(response.next.len(), 3);
}
#[tokio::test]
async fn test_get_successors_cache() {
let config = ConfigBuilder::new().with_network(Network::Regtest).build();
let blockchain_state = BlockchainState::new(&config, &MetricsRegistry::default());
let genesis = *blockchain_state.genesis();
let genesis_hash = genesis.block_hash();
let (blockchain_manager_tx, _blockchain_manager_rx) =
channel::<BlockchainManagerRequest>(10);
let handler = GetSuccessorsHandler::new(
&config,
Arc::new(Mutex::new(blockchain_state)),
blockchain_manager_tx,
&MetricsRegistry::default(),
);
// Set up the following chain:
// 0 -> 1 -> 2 -> 3 -> 4 -> 5
let main_chain = generate_headers(genesis_hash, genesis.time, 5, &[]);
let main_block_1 = Block {
header: main_chain[0],
txdata: vec![],
};
let main_block_2 = Block {
header: main_chain[1],
txdata: vec![],
};
{
let mut blockchain = handler.state.lock().await;
blockchain.add_headers(&main_chain);
blockchain
.add_block(main_block_1.clone())
.expect("invalid block");
blockchain
.add_block(main_block_2.clone())
.expect("invalid block");
}
let request = GetSuccessorsRequest {
anchor: genesis_hash,
processed_block_hashes: vec![],
};
let cached_response = handler.get_successors(request.clone()).await.unwrap();
assert!(cached_response.blocks.len() == 2);
// Check that cached value is returned even though the state has changed
{
let mut blockchain = handler.state.lock().await;
let main_block_3 = Block {
header: main_chain[2],
txdata: vec![],
};
blockchain
.add_block(main_block_3.clone())
.expect("invalid block");
}
let response_from_cache = handler.get_successors(request.clone()).await.unwrap();
assert_eq!(cached_response, response_from_cache);
assert!(response_from_cache.blocks.len() == 2);
let non_cached_response = handler.get_successors(request).await.unwrap();
assert!(cached_response != non_cached_response);
assert!(non_cached_response.blocks.len() == 3);
}
/// This tests ensures that `BlockchainManager::handle_client_request(...)` returns multiple
/// blocks from the main chain and a fork. Order should be preserved.
#[tokio::test]
async fn test_get_successors_multiple_blocks() {
let config = ConfigBuilder::new().with_network(Network::Regtest).build();
let blockchain_state = BlockchainState::new(&config, &MetricsRegistry::default());
let genesis = *blockchain_state.genesis();
let genesis_hash = genesis.block_hash();
let (blockchain_manager_tx, _blockchain_manager_rx) =
channel::<BlockchainManagerRequest>(10);
let handler = GetSuccessorsHandler::new(
&config,
Arc::new(Mutex::new(blockchain_state)),
blockchain_manager_tx,
&MetricsRegistry::default(),
);
// Set up the following chain:
// |-> 1'
// 0 -> 1 -> 2
let main_chain = generate_headers(genesis_hash, genesis.time, 2, &[]);
let main_block_1 = Block {
header: main_chain[0],
txdata: vec![],
};
let main_block_2 = Block {
header: main_chain[1],
txdata: vec![],
};
let side_chain = generate_headers(
genesis_hash,
genesis.time,
1,
&headers_to_hashes(&main_chain),
);
let side_1 = side_chain.get(0).cloned().expect("Should have 1 header");
let side_block_1 = Block {
header: side_1,
txdata: vec![],
};
{
let mut blockchain = handler.state.lock().await;
blockchain.add_headers(&main_chain);
blockchain.add_headers(&side_chain);
blockchain
.add_block(main_block_1.clone())
.expect("invalid block");
blockchain
.add_block(main_block_2.clone())
.expect("invalid block");
blockchain
.add_block(side_block_1.clone())
.expect("invalid block");
}
// |-> 1'
// If chain is 0 -> 1 -> 2 and block hashes are {0} then {1, 1', 2} should be returned in that order.
let request = GetSuccessorsRequest {
anchor: genesis_hash,
processed_block_hashes: vec![],
};
let response = handler.get_successors(request).await.unwrap();
assert_eq!(response.blocks.len(), 3);
assert!(
matches!(response.blocks.get(0), Some(block) if block.block_hash() == main_block_1.block_hash())
);
assert!(
matches!(response.blocks.get(1), Some(block) if block.block_hash() == side_block_1.block_hash())
);
assert!(
matches!(response.blocks.get(2), Some(block) if block.block_hash() == main_block_2.block_hash())
);
}
/// This tests ensures that `get_successor` returns no more than MAX_BLOCKS_LENGTH blocks.
#[tokio::test]
async fn test_get_successors_max_num_blocks() {
let config = ConfigBuilder::new().with_network(Network::Regtest).build();
let blockchain_state = BlockchainState::new(&config, &MetricsRegistry::default());
let genesis = *blockchain_state.genesis();
let genesis_hash = genesis.block_hash();
let (blockchain_manager_tx, _blockchain_manager_rx) =
channel::<BlockchainManagerRequest>(10);
let handler = GetSuccessorsHandler::new(
&config,
Arc::new(Mutex::new(blockchain_state)),
blockchain_manager_tx,
&MetricsRegistry::default(),
);
let main_chain = generate_headers(genesis_hash, genesis.time, 120, &[]);
{
let mut blockchain = handler.state.lock().await;
blockchain.add_headers(&main_chain);
for header in main_chain {
let block = Block {
header,
txdata: vec![],
};
blockchain.add_block(block).expect("invalid block");
}
}
let request = GetSuccessorsRequest {
anchor: genesis_hash,
processed_block_hashes: vec![],
};
let response = handler.get_successors(request).await.unwrap();
assert_eq!(response.blocks.len(), MAX_BLOCKS_LENGTH);
}
/// This tests ensures that `BlockchainManager::get_successors(...)` returns multiple
/// blocks from the main chain and a fork. Order should be preserved.
#[tokio::test]
async fn test_get_successors_multiple_blocks_out_of_order() {
let config = ConfigBuilder::new().with_network(Network::Regtest).build();
let blockchain_state = BlockchainState::new(&config, &MetricsRegistry::default());
let genesis = *blockchain_state.genesis();
let genesis_hash = genesis.block_hash();
let (blockchain_manager_tx, _blockchain_manager_rx) =
channel::<BlockchainManagerRequest>(10);
let handler = GetSuccessorsHandler::new(
&config,
Arc::new(Mutex::new(blockchain_state)),
blockchain_manager_tx,
&MetricsRegistry::default(),
);
// Set up the following chain:
// |-> 1'
// 0 -> 1 -> 2
let main_chain = generate_headers(genesis_hash, genesis.time, 2, &[]);
let main_block_2 = Block {
header: main_chain[1],
txdata: vec![],
};
let side_chain = generate_headers(
genesis_hash,
genesis.time,
1,
&headers_to_hashes(&main_chain),
);
let side_1 = side_chain.get(0).cloned().expect("Should have 1 header");
let side_block_1 = Block {
header: side_1,
txdata: vec![],
};
{
let mut blockchain = handler.state.lock().await;
let (_, maybe_err) = blockchain.add_headers(&main_chain);
assert!(
maybe_err.is_none(),
"Error was found in main chain: {:#?}",
maybe_err
);
let (_, maybe_err) = blockchain.add_headers(&side_chain);
assert!(
maybe_err.is_none(),
"Error was found in side chain: {:#?}",
maybe_err
);
blockchain.add_block(main_block_2).expect("invalid block");
blockchain
.add_block(side_block_1.clone())
.expect("invalid block");
}
// |-> 1'
// If chain is 0 -> 1 -> 2 and block hashes are {0} then {1, 1', 2} would be the successor blocks.
// Block 1 is not in the cache yet. The Bitcoin virtual canister requires that the blocks
// are received in order.
let request = GetSuccessorsRequest {
anchor: genesis_hash,
processed_block_hashes: vec![],
};
let response = handler.get_successors(request).await.unwrap();
assert_eq!(
response.blocks.len(),
1,
"main_chain = {:#?}, side_chain = {:#?}, blocks = {:#?}",
headers_to_hashes(&main_chain),
headers_to_hashes(&side_chain),
response
.blocks
.iter()
.map(|b| b.block_hash())
.collect::<Vec<BlockHash>>()
);
assert!(
matches!(response.blocks.get(0), Some(block) if block.block_hash() == side_block_1.block_hash())
);
assert_eq!(
response.next.len(),
2,
"main_chain = {:#?}, side_chain = {:#?}, next = {:#?}",
headers_to_hashes(&main_chain),
headers_to_hashes(&side_chain),
headers_to_hashes(&response.next)
);
assert_eq!(response.next[0].block_hash(), main_chain[0].block_hash());
assert_eq!(response.next[1].block_hash(), main_chain[1].block_hash());
}
/// This test ensures that the 2MB limit is enforced by `BlockchainManager.get_successors(...)`.
#[tokio::test]
async fn test_get_successors_large_block() {
let config = ConfigBuilder::new().with_network(Network::Regtest).build();
let blockchain_state = BlockchainState::new(&config, &MetricsRegistry::default());
let genesis = *blockchain_state.genesis();
let genesis_hash = genesis.block_hash();
let (blockchain_manager_tx, _blockchain_manager_rx) =
channel::<BlockchainManagerRequest>(10);
let handler = GetSuccessorsHandler::new(
&config,
Arc::new(Mutex::new(blockchain_state)),
blockchain_manager_tx,
&MetricsRegistry::default(),
);
// Generate a blockchain with one large block.
let large_blocks = generate_large_block_blockchain(genesis_hash, genesis.time, 1);
let large_block = large_blocks.first().cloned().unwrap();
let headers: Vec<BlockHeader> = large_blocks.iter().map(|b| b.header).collect();
let previous_hashes = headers.iter().map(|h| h.block_hash()).collect::<Vec<_>>();
let additional_headers = generate_headers(
large_block.block_hash(),
large_block.header.time,
1,
&previous_hashes,
);
// Add an additional smaller block to the chain.
let small_block = Block {
header: additional_headers[0],
txdata: vec![],
};
{
let mut blockchain = handler.state.lock().await;
let (added_headers, _) = blockchain.add_headers(&headers);
assert_eq!(added_headers.len(), 1);
let (added_headers, _) = blockchain.add_headers(&additional_headers);
assert_eq!(added_headers.len(), 1);
blockchain
.add_block(large_block.clone())
.expect("invalid block");
blockchain.add_block(small_block).expect("invalid block");
};
let request = GetSuccessorsRequest {
anchor: genesis_hash,
processed_block_hashes: vec![],
};
let response = handler.get_successors(request).await.unwrap();
// There are 2 blocks in the chain: {large, small}.
// Only the large block should be returned in this response.
assert_eq!(response.blocks.len(), 1);
assert!(
matches!(response.blocks.first(), Some(block) if block.block_hash() == large_block.block_hash() && block.txdata.len() == large_block.txdata.len())
);
// The smaller block's header should be in the next field.
assert!(
matches!(response.next.first(), Some(header) if header.block_hash() == additional_headers[0].block_hash())
);
}
/// This test ensures that `BlockchainManager::get_successors(...)` sends blocks up to the cap limit.
#[tokio::test]
async fn test_get_successors_many_blocks_until_size_cap_is_met() {
let config = ConfigBuilder::new().with_network(Network::Regtest).build();
let blockchain_state = BlockchainState::new(&config, &MetricsRegistry::default());
let genesis = *blockchain_state.genesis();
let genesis_hash = genesis.block_hash();
let (blockchain_manager_tx, _blockchain_manager_rx) =
channel::<BlockchainManagerRequest>(10);
let handler = GetSuccessorsHandler::new(
&config,
Arc::new(Mutex::new(blockchain_state)),
blockchain_manager_tx,
&MetricsRegistry::default(),
);
let main_chain = generate_headers(genesis_hash, genesis.time, 5, &[]);
let large_blocks =
generate_large_block_blockchain(main_chain[4].block_hash(), main_chain[4].time, 1);
{
let mut blockchain = handler.state.lock().await;
let (added_headers, _) = blockchain.add_headers(&main_chain);
assert_eq!(added_headers.len(), 5);
let main_blocks = main_chain
.iter()
.map(|h| Block {
header: *h,
txdata: vec![],
})
.collect::<Vec<_>>();
for block in main_blocks {
blockchain.add_block(block).unwrap();
}
for block in &large_blocks {
blockchain.add_block(block.clone()).unwrap();
}
};
let request = GetSuccessorsRequest {
anchor: genesis_hash,
processed_block_hashes: vec![],
};
let response = handler.get_successors(request).await.unwrap();
// Six blocks in the chain. First 5 are small blocks and the last block is large.
// Should return the first 5 blocks as the total size is below the cap.
assert_eq!(response.blocks.len(), 5);
assert!(
matches!(response.blocks.last(), Some(block) if block.block_hash() == main_chain.last().unwrap().block_hash())
);
// The next field should contain the large block header as it is too large for the request.
assert_eq!(response.next.len(), 1);
assert!(
matches!(response.next.first(), Some(header) if large_blocks[0].block_hash() == header.block_hash())
);
}
#[test]
fn test_are_multiple_blocks_allowed() {
// Mainnet
assert!(
are_multiple_blocks_allowed(Network::Bitcoin, 100_500),
"Multiple blocks are allowed at 100_500"
);
assert!(
are_multiple_blocks_allowed(Network::Bitcoin, MAINNET_MAX_MULTI_BLOCK_ANCHOR_HEIGHT),
"Multiple blocks are allowed at {}",
MAINNET_MAX_MULTI_BLOCK_ANCHOR_HEIGHT
);
assert!(
!are_multiple_blocks_allowed(Network::Bitcoin, 900_000),
"Multiple blocks are not allowed at 900_000"
);
// Testnet
assert!(
are_multiple_blocks_allowed(Network::Testnet, 1_000_000),
"Multiple blocks are allowed at 1_000_000"
);
assert!(
are_multiple_blocks_allowed(Network::Testnet, u32::MAX),
"Multiple blocks are allowed at {}",
u32::MAX
);
// Regtest
assert!(
are_multiple_blocks_allowed(Network::Regtest, 1),
"Multiple blocks are allowed at 1"
);
assert!(
are_multiple_blocks_allowed(Network::Regtest, u32::MAX),
"Multiple blocks are allowed at {}",
u32::MAX
);
}
#[test]
fn response_size() {
assert_eq!(MAX_NEXT_BYTES + MAX_BLOCKS_BYTES, MAX_RESPONSE_SIZE);
}
}