zebrad 6.2.3

The Zcash Foundation's independent, consensus-compatible implementation of a Zcash node
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
use std::{sync::Arc, time::Duration};

use color_eyre::eyre::{eyre, Result};
use tower::ServiceExt;

use zebra_chain::{
    block::{genesis::regtest_genesis_block, Height},
    parameters::{testnet::ConfiguredActivationHeights, Network},
    serialization::ZcashSerialize as _,
    transparent,
};
use zebra_node_services::rpc_client::RpcRequestClient;
use zebra_rpc::{
    client::{SubmitBlockErrorResponse, SubmitBlockResponse},
    config::mining::ExtraCoinbaseData,
    server::OPENED_RPC_ENDPOINT_MSG,
};
use zebra_test::{args, prelude::*};

use crate::common::{
    config::{
        default_test_config, os_assigned_rpc_port_config, read_listen_addr_from_logs, testdir,
    },
    launch::{ZebradTestDirExt, LAUNCH_DELAY},
    regtest::MiningRpcMethods,
};

/// Checks that the Regtest genesis block can be validated.
#[tokio::test]
async fn validate_regtest_genesis_block() {
    let _init_guard = zebra_test::init();

    let network = Network::new_regtest(Default::default());
    let state = zebra_state::init_test(&network).await;
    let (
        block_verifier_router,
        _transaction_verifier,
        _parameter_download_task_handle,
        _max_checkpoint_height,
    ) = zebra_consensus::router::init_test(zebra_consensus::Config::default(), &network, state)
        .await;

    let genesis_hash = block_verifier_router
        .oneshot(zebra_consensus::Request::Commit(regtest_genesis_block()))
        .await
        .expect("should validate Regtest genesis block");

    assert_eq!(
        genesis_hash,
        network.genesis_hash(),
        "validated block hash should match network genesis hash"
    )
}

/// Test successful `getblocktemplate` and `submitblock` RPC calls on Regtest on Canopy.
///
/// See [`crate::common::regtest::submit_blocks`] for more information.
#[tokio::test]
async fn regtest_block_templates_are_valid_block_submissions() -> Result<()> {
    crate::common::regtest::submit_blocks_test().await?;
    Ok(())
}

/// A rejected block body must not poison the children of a later valid block with the same header
/// hash.
///
/// This is a regression test for [GHSA-8gxx-hc65-vv82][ghsa-8gxx]. Under the transaction digest
/// scheme defined by [ZIP-244][zip-244], two different block bodies can share the same header hash.
/// `zebra-state` previously retained the contextual validation error from the poisoned body and
/// incorrectly propagated it to children of the later valid block, causing them to be incorrectly
/// rejected.
///
/// [ghsa-8gxx]: https://github.com/ZcashFoundation/zebra/security/advisories/GHSA-8gxx-hc65-vv82
/// [zip-244]: https://zips.z.cash/zip-0244
#[tokio::test]
async fn rejected_block_does_not_reject_same_hash_block_children() -> Result<()> {
    const EXTRA_COINBASE_DATA: &str = "zebra-chain-stall-poc";

    let _init_guard = zebra_test::init();

    let network = Network::new_regtest(
        ConfiguredActivationHeights {
            nu5: Some(1),
            ..Default::default()
        }
        .into(),
    );
    let mut config = os_assigned_rpc_port_config(false, &network)?;
    config.mempool.debug_enable_at_height = Some(0);
    config.mining.extra_coinbase_data =
        Some(ExtraCoinbaseData::try_from(EXTRA_COINBASE_DATA.to_owned())?);

    let mut block_builder = testdir()?
        .with_config(&mut config)?
        .spawn_child(args!["start"])?;
    let rpc_address = read_listen_addr_from_logs(&mut block_builder, OPENED_RPC_ENDPOINT_MSG)?;

    tokio::time::sleep(LAUNCH_DELAY).await;

    let client = RpcRequestClient::new(rpc_address);
    let mut blocks = Vec::new();
    for expected_height in 1..=4 {
        let (block, height) = client.block_from_template(&network).await?;
        assert_eq!(height.0, expected_height);
        client.submit_block(block.clone()).await?;
        blocks.push(block);
    }

    block_builder.kill(false)?;
    let output = block_builder.wait_with_output()?;
    output.assert_failure()?.assert_was_killed()?;

    let mut zebrad = testdir()?
        .with_config(&mut config)?
        .spawn_child(args!["start"])?;
    let rpc_address = read_listen_addr_from_logs(&mut zebrad, OPENED_RPC_ENDPOINT_MSG)?;

    tokio::time::sleep(LAUNCH_DELAY).await;

    let client = RpcRequestClient::new(rpc_address);
    client.submit_block(blocks[0].clone()).await?;
    client.submit_block(blocks[1].clone()).await?;

    let valid_block = blocks[2].clone();

    let mut poisoned_block = valid_block.clone();
    let coinbase = Arc::make_mut(
        poisoned_block
            .transactions
            .first_mut()
            .expect("block templates contain a coinbase transaction"),
    );
    let transparent::Input::Coinbase { data, .. } = coinbase
        .inputs_mut()
        .first_mut()
        .expect("coinbase transactions contain a transparent input")
    else {
        panic!("the first coinbase transaction input must be a coinbase input");
    };
    assert!(
        data.ends_with(EXTRA_COINBASE_DATA.as_bytes()),
        "the coinbase transaction must contain the configured extra data"
    );
    let last_data_byte = data
        .last_mut()
        .expect("configured extra coinbase data is non-empty");
    *last_data_byte = b'a';

    assert_eq!(
        poisoned_block.hash(),
        valid_block.hash(),
        "changing a NU5 coinbase scriptSig must not change the block header hash"
    );

    let poisoned_block_data = hex::encode(poisoned_block.zcash_serialize_to_vec()?);
    let poisoned_response: SubmitBlockResponse = client
        .json_result_from_call("submitblock", format!(r#"["{poisoned_block_data}"]"#))
        .await
        .map_err(|err| eyre!(err))?;
    assert!(
        matches!(
            poisoned_response,
            SubmitBlockResponse::ErrorResponse(SubmitBlockErrorResponse::Rejected)
        ),
        "the poisoned block body must be rejected"
    );

    let valid_block_data = hex::encode(valid_block.zcash_serialize_to_vec()?);
    let valid_block_response: SubmitBlockResponse = client
        .json_result_from_call("submitblock", format!(r#"["{valid_block_data}"]"#))
        .await
        .map_err(|err| eyre!(err))?;
    assert_eq!(
        valid_block_response,
        SubmitBlockResponse::Accepted,
        "KnownBlock must drain rejected hashes before checking sent hashes"
    );

    let valid_child = blocks[3].clone();
    let valid_child_data = hex::encode(valid_child.zcash_serialize_to_vec()?);
    let valid_child_response: SubmitBlockResponse = client
        .json_result_from_call("submitblock", format!(r#"["{valid_child_data}"]"#))
        .await
        .map_err(|err| eyre!(err))?;
    assert_eq!(
        valid_child_response,
        SubmitBlockResponse::Accepted,
        "the valid child must not inherit the rejected block body's contextual error"
    );
    assert_eq!(
        client.blockchain_info().await?.blocks(),
        Height(4),
        "the valid child must not inherit the rejected block body error"
    );

    zebrad.kill(false)?;
    let output = zebrad.wait_with_output()?;
    output.assert_failure()?.assert_was_killed()?;

    Ok(())
}

/// A contextually rejected block must not remain known as sent.
///
/// Sync checks [`zebra_state::Request::KnownBlock`] before downloading a block body. If a rejected
/// block remains in the state's sent hashes, an honest block body with the same header hash is
/// incorrectly reported as a duplicate and never reaches contextual verification.
#[tokio::test]
async fn rejected_block_is_not_known_as_sent() -> Result<()> {
    const EXTRA_COINBASE_DATA: &str = "zebra-chain-stall-poc";

    let _init_guard = zebra_test::init();

    let network = Network::new_regtest(
        ConfiguredActivationHeights {
            nu5: Some(1),
            ..Default::default()
        }
        .into(),
    );
    let mut config = os_assigned_rpc_port_config(false, &network)?;
    config.mempool.debug_enable_at_height = Some(0);
    config.mining.extra_coinbase_data =
        Some(ExtraCoinbaseData::try_from(EXTRA_COINBASE_DATA.to_owned())?);

    let mut block_builder = testdir()?
        .with_config(&mut config)?
        .spawn_child(args!["start"])?;
    let rpc_address = read_listen_addr_from_logs(&mut block_builder, OPENED_RPC_ENDPOINT_MSG)?;

    tokio::time::sleep(LAUNCH_DELAY).await;

    let client = RpcRequestClient::new(rpc_address);
    let mut blocks = Vec::new();
    for expected_height in 1..=3 {
        let (block, height) = client.block_from_template(&network).await?;
        assert_eq!(height.0, expected_height);
        client.submit_block(block.clone()).await?;
        blocks.push(block);
    }

    block_builder.kill(false)?;
    let output = block_builder.wait_with_output()?;
    output.assert_failure()?.assert_was_killed()?;

    let mut zebrad = testdir()?
        .with_config(&mut config)?
        .spawn_child(args!["start"])?;
    let rpc_address = read_listen_addr_from_logs(&mut zebrad, OPENED_RPC_ENDPOINT_MSG)?;

    tokio::time::sleep(LAUNCH_DELAY).await;

    let client = RpcRequestClient::new(rpc_address);
    client.submit_block(blocks[0].clone()).await?;
    client.submit_block(blocks[1].clone()).await?;

    let valid_block = blocks[2].clone();
    let mut poisoned_block = valid_block.clone();
    let coinbase = Arc::make_mut(
        poisoned_block
            .transactions
            .first_mut()
            .expect("block templates contain a coinbase transaction"),
    );
    let transparent::Input::Coinbase { data, .. } = coinbase
        .inputs_mut()
        .first_mut()
        .expect("coinbase transactions contain a transparent input")
    else {
        panic!("the first coinbase transaction input must be a coinbase input");
    };
    assert!(
        data.ends_with(EXTRA_COINBASE_DATA.as_bytes()),
        "the coinbase transaction must contain the configured extra data"
    );
    *data
        .last_mut()
        .expect("configured extra coinbase data is non-empty") = b'a';

    assert_eq!(
        poisoned_block.hash(),
        valid_block.hash(),
        "changing a NU5 coinbase scriptSig must not change the block header hash"
    );

    let poisoned_block_data = hex::encode(poisoned_block.zcash_serialize_to_vec()?);
    let poisoned_response: SubmitBlockResponse = client
        .json_result_from_call("submitblock", format!(r#"["{poisoned_block_data}"]"#))
        .await
        .map_err(|err| eyre!(err))?;
    assert_eq!(
        poisoned_response,
        SubmitBlockResponse::ErrorResponse(SubmitBlockErrorResponse::Rejected),
        "the poisoned block body must be rejected"
    );

    let valid_block_data = hex::encode(valid_block.zcash_serialize_to_vec()?);
    let valid_response: SubmitBlockResponse = client
        .json_result_from_call("submitblock", format!(r#"["{valid_block_data}"]"#))
        .await
        .map_err(|err| eyre!(err))?;
    assert_eq!(
        valid_response,
        SubmitBlockResponse::Accepted,
        "KnownBlock must drain rejected hashes before checking sent hashes"
    );

    zebrad.kill(false)?;
    let output = zebrad.wait_with_output()?;
    output.assert_failure()?.assert_was_killed()?;

    Ok(())
}

/// Regression test for <https://github.com/ZcashFoundation/zebra/issues/10470>.
///
/// `getrawtransaction` must count confirmations against the full best-chain tip
/// (including non-finalized blocks), not just the finalized-database tip.
#[tokio::test]
async fn getrawtransaction_confirmations_include_non_finalized_blocks() -> Result<()> {
    use serde_json::Value;
    use zebra_state::constants::MAX_BLOCK_REORG_HEIGHT;

    let _init_guard = zebra_test::init();

    let network = Network::new_regtest(
        ConfiguredActivationHeights {
            nu5: Some(100),
            ..Default::default()
        }
        .into(),
    );
    let mut config = os_assigned_rpc_port_config(false, &network)?;
    config.mempool.debug_enable_at_height = Some(0);

    let mut zebrad = testdir()?
        .with_config(&mut config)?
        .spawn_child(args!["start"])?;
    let rpc_address = read_listen_addr_from_logs(&mut zebrad, OPENED_RPC_ENDPOINT_MSG)?;

    tokio::time::sleep(LAUNCH_DELAY).await;

    // Use a longer timeout because generating MAX_BLOCK_REORG_HEIGHT + 10 blocks
    // in a single RPC call takes ~400s at ~400ms per block.
    let client =
        RpcRequestClient::new_with_timeout(rpc_address, std::time::Duration::from_secs(15 * 60));

    // Mine enough blocks to push the first few blocks into the finalized state.
    // Block at height 2 is finalized once tip > 2 + MAX_BLOCK_REORG_HEIGHT (= 1002).
    let blocks_to_mine = MAX_BLOCK_REORG_HEIGHT + 10;
    client.generate(blocks_to_mine).await?;

    // Get the coinbase txid from block 2 (it will be in the finalized DB).
    let block2 = client
        .get_block(2)
        .await
        .map_err(|err| eyre!(err))?
        .expect("block at height 2 should exist");
    let txid = block2.transactions[0].hash();

    // Confirm the tip height and compute expected confirmations.
    let info = client.blockchain_info().await?;
    let tip_height = info.blocks().0;
    let expected_confirmations = 1 + tip_height - 2;

    // getrawtransaction verbose=1 returns a JSON object that includes `confirmations`.
    let response: Value = client
        .json_result_from_call("getrawtransaction", format!(r#"["{txid}", 1]"#))
        .await
        .map_err(|err| eyre!(err))?;

    let confirmations: u32 = response["confirmations"]
        .as_u64()
        .expect("confirmations should be a positive integer")
        .try_into()
        .expect("confirmations should fit in u32 because regtest block heights fit in u32");

    assert_eq!(
        confirmations, expected_confirmations,
        "getrawtransaction must count confirmations against the full best-chain tip \
         (including non-finalized blocks), not just the finalized-DB tip"
    );

    zebrad.kill(false)?;
    let output = zebrad.wait_with_output()?;
    output.assert_failure()?.assert_was_killed()?;

    Ok(())
}

#[tokio::test]
async fn regtest_coinbase() -> Result<()> {
    crate::common::coinbase::regtest_coinbase().await
}

/// Test successful block template submission as a block proposal or submission on a custom Testnet.
///
/// This test can be run locally with:
/// `cargo test --package zebrad --test zebrad-tests -- nu6_funding_streams_and_coinbase_balance --exact --show-output`
#[tokio::test(flavor = "multi_thread")]
async fn nu6_funding_streams_and_coinbase_balance() -> Result<()> {
    use zebra_chain::{
        amount::Amount,
        chain_sync_status::MockSyncStatus,
        parameters::{
            subsidy::FundingStreamReceiver,
            testnet::{
                self, ConfiguredActivationHeights, ConfiguredFundingStreamRecipient,
                ConfiguredFundingStreams,
            },
        },
        work::difficulty::U256,
    };
    use zebra_network::address_book_peers::MockAddressBookPeers;
    use zebra_node_services::mempool;
    use zebra_rpc::client::HexData;
    use zebra_test::mock_service::MockService;

    use zebra_rpc::{
        client::{
            BlockTemplateResponse, DefaultRoots, GetBlockTemplateParameters,
            GetBlockTemplateRequestMode, GetBlockTemplateResponse, TransactionTemplate,
        },
        fetch_chain_info,
        methods::{RpcImpl, RpcServer},
        proposal_block_from_template, MinerParams, SubmitBlockChannel,
    };

    let _init_guard = zebra_test::init();

    tracing::info!("running nu6_funding_streams_and_coinbase_balance test");

    let base_network_params = testnet::Parameters::build()
        // Regtest genesis hash
        .with_genesis_hash("029f11d80ef9765602235e1bc9727e3eb6ba20839319f761fee920d63401e327")
        .expect("failed to set genesis hash")
        .with_checkpoints(false)
        .expect("failed to verify checkpoints")
        .with_target_difficulty_limit(U256::from_big_endian(&[0x0f; 32]))
        .expect("failed to set target difficulty limit")
        .with_disable_pow(true)
        .with_slow_start_interval(Height::MIN)
        .with_activation_heights(ConfiguredActivationHeights {
            nu6: Some(1),
            ..Default::default()
        })
        .expect("failed to set activation heights");

    let network = base_network_params
        .clone()
        .with_funding_streams(vec![ConfiguredFundingStreams {
            // Start checking funding streams from block height 1
            height_range: Some(Height(1)..Height(100)),
            // Use default post-NU6 recipients
            recipients: None,
        }])
        .to_network()
        .expect("failed to build configured network");

    tracing::info!("built configured Testnet, starting state service and block verifier");

    let default_test_config = default_test_config(&network);
    let mining_config = default_test_config.mining;
    let miner_params = MinerParams::new(&network, mining_config.clone())?;

    let (state, read_state, latest_chain_tip, _chain_tip_change) =
        zebra_state::init_test_services(&network).await;

    let (
        block_verifier_router,
        _transaction_verifier,
        _parameter_download_task_handle,
        _max_checkpoint_height,
    ) = zebra_consensus::router::init_test(
        zebra_consensus::Config::default(),
        &network,
        state.clone(),
    )
    .await;

    tracing::info!("started state service and block verifier, committing Regtest genesis block");

    let genesis_hash = block_verifier_router
        .clone()
        .oneshot(zebra_consensus::Request::Commit(regtest_genesis_block()))
        .await
        .expect("should validate Regtest genesis block");

    let mut mempool = MockService::build()
        .with_max_request_delay(Duration::from_secs(5))
        .for_unit_tests();
    let mut mock_sync_status = MockSyncStatus::default();
    mock_sync_status.set_is_close_to_tip(true);

    let submitblock_channel = SubmitBlockChannel::new();

    let (_tx, rx) = tokio::sync::watch::channel(None);

    let (rpc, _) = RpcImpl::new(
        network.clone(),
        mining_config,
        false,
        "0.0.1",
        "Zebra tests",
        mempool.clone(),
        state.clone(),
        read_state.clone(),
        block_verifier_router,
        mock_sync_status,
        latest_chain_tip,
        MockAddressBookPeers::default(),
        rx,
        Some(submitblock_channel.sender()),
    );

    let make_mock_mempool_request_handler = || async move {
        mempool
            .expect_request(mempool::Request::FullTransactions)
            .await
            .respond(mempool::Response::FullTransactions {
                transactions: vec![],
                transaction_dependencies: Default::default(),
                // tip hash needs to match chain info for long poll requests
                last_seen_tip_hash: genesis_hash,
            });
    };

    let block_template_fut = rpc.get_block_template(None);
    let mock_mempool_request_handler = make_mock_mempool_request_handler.clone()();
    let (block_template, _) = tokio::join!(block_template_fut, mock_mempool_request_handler);
    let GetBlockTemplateResponse::TemplateMode(block_template) =
        block_template.expect("unexpected error in getblocktemplate RPC call")
    else {
        panic!(
            "this getblocktemplate call without parameters should return the `TemplateMode` variant of the response"
        )
    };

    let proposal_block = proposal_block_from_template(&block_template, None, &network)?;
    let hex_proposal_block = HexData(proposal_block.zcash_serialize_to_vec()?);

    // Check that the block template is a valid block proposal
    let GetBlockTemplateResponse::ProposalMode(block_proposal_result) = rpc
        .get_block_template(Some(GetBlockTemplateParameters::new(
            GetBlockTemplateRequestMode::Proposal,
            Some(hex_proposal_block),
            Default::default(),
            Default::default(),
            Default::default(),
        )))
        .await?
    else {
        panic!(
            "this getblocktemplate call should return the `ProposalMode` variant of the response"
        )
    };

    assert!(
        block_proposal_result.is_valid(),
        "block proposal should succeed"
    );

    // Submit the same block
    let submit_block_response = rpc
        .submit_block(HexData(proposal_block.zcash_serialize_to_vec()?), None)
        .await?;

    assert_eq!(
        submit_block_response,
        SubmitBlockResponse::Accepted,
        "valid block should be accepted"
    );

    // Check that the submitblock channel received the submitted block
    let mut submit_block_receiver = submitblock_channel.receiver();
    let submit_block_channel_data = submit_block_receiver.recv().await.expect("channel is open");
    assert_eq!(
        submit_block_channel_data,
        (
            proposal_block.hash(),
            proposal_block.coinbase_height().unwrap()
        ),
        "submitblock channel should receive the submitted block"
    );

    // Use an invalid coinbase transaction (with an output value greater than the `block_subsidy + miner_fees - expected_lockbox_funding_stream`)

    let make_configured_recipients_with_lockbox_numerator = |numerator| {
        Some(vec![
            ConfiguredFundingStreamRecipient {
                receiver: FundingStreamReceiver::Deferred,
                numerator,
                addresses: None,
            },
            ConfiguredFundingStreamRecipient::new_for(FundingStreamReceiver::MajorGrants),
        ])
    };

    // Gets the next block template
    let block_template_fut = rpc.get_block_template(None);
    let mock_mempool_request_handler = make_mock_mempool_request_handler.clone()();
    let (block_template, _) = tokio::join!(block_template_fut, mock_mempool_request_handler);
    let GetBlockTemplateResponse::TemplateMode(block_template) =
        block_template.expect("unexpected error in getblocktemplate RPC call")
    else {
        panic!(
            "this getblocktemplate call without parameters should return the `TemplateMode` variant of the response"
        )
    };

    let valid_original_block_template = block_template.clone();

    let zebra_state::GetBlockTemplateChainInfo {
        chain_history_root, ..
    } = fetch_chain_info(read_state.clone()).await?;

    let net = base_network_params
        .clone()
        .with_funding_streams(vec![ConfiguredFundingStreams {
            height_range: Some(Height(1)..Height(100)),
            recipients: make_configured_recipients_with_lockbox_numerator(0),
        }])
        .to_network()
        .expect("failed to build configured network");

    let coinbase_txn = TransactionTemplate::new_coinbase(
        &net,
        Height(block_template.height()),
        &miner_params,
        Amount::zero(),
    )
    .expect("coinbase transaction should be valid under the given parameters");

    let default_roots = DefaultRoots::from_coinbase(
        &net,
        Height(block_template.height()),
        &coinbase_txn,
        chain_history_root,
        &[],
    );

    let block_template = BlockTemplateResponse::new(
        block_template.capabilities().clone(),
        block_template.version(),
        block_template.previous_block_hash(),
        default_roots.block_commitments_hash(),
        default_roots.block_commitments_hash(),
        default_roots.block_commitments_hash(),
        default_roots,
        block_template.transactions().clone(),
        coinbase_txn,
        block_template.long_poll_id(),
        block_template.target(),
        block_template.min_time(),
        block_template.mutable().clone(),
        block_template.nonce_range().clone(),
        block_template.sigop_limit(),
        block_template.size_limit(),
        block_template.cur_time(),
        block_template.bits(),
        block_template.height(),
        block_template.max_time(),
        block_template.submit_old(),
    );

    let proposal_block = proposal_block_from_template(&block_template, None, &net)?;

    // Submit the invalid block with an excessive coinbase output value
    let submit_block_response = rpc
        .submit_block(HexData(proposal_block.zcash_serialize_to_vec()?), None)
        .await?;

    tracing::info!(?submit_block_response, "submitted invalid block");

    assert_eq!(
        submit_block_response,
        SubmitBlockResponse::ErrorResponse(SubmitBlockErrorResponse::Rejected),
        "invalid block with excessive coinbase output value should be rejected"
    );

    // Use an invalid coinbase transaction (with an output value less than the `block_subsidy + miner_fees - expected_lockbox_funding_stream`)
    let net = base_network_params
        .clone()
        .with_funding_streams(vec![ConfiguredFundingStreams {
            height_range: Some(Height(1)..Height(100)),
            recipients: make_configured_recipients_with_lockbox_numerator(20),
        }])
        .to_network()
        .expect("failed to build configured network");

    let coinbase_txn = TransactionTemplate::new_coinbase(
        &net,
        Height(block_template.height()),
        &miner_params,
        Amount::zero(),
    )
    .expect("coinbase transaction should be valid under the given parameters");

    let default_roots = DefaultRoots::from_coinbase(
        &net,
        Height(block_template.height()),
        &coinbase_txn,
        chain_history_root,
        &[],
    );

    let block_template = BlockTemplateResponse::new(
        block_template.capabilities().clone(),
        block_template.version(),
        block_template.previous_block_hash(),
        default_roots.block_commitments_hash(),
        default_roots.block_commitments_hash(),
        default_roots.block_commitments_hash(),
        default_roots,
        block_template.transactions().clone(),
        coinbase_txn,
        block_template.long_poll_id(),
        block_template.target(),
        block_template.min_time(),
        block_template.mutable().clone(),
        block_template.nonce_range().clone(),
        block_template.sigop_limit(),
        block_template.size_limit(),
        block_template.cur_time(),
        block_template.bits(),
        block_template.height(),
        block_template.max_time(),
        block_template.submit_old(),
    );

    let proposal_block = proposal_block_from_template(&block_template, None, &net)?;

    // Submit the invalid block with an excessive coinbase input value
    let submit_block_response = rpc
        .submit_block(HexData(proposal_block.zcash_serialize_to_vec()?), None)
        .await?;

    tracing::info!(?submit_block_response, "submitted invalid block");

    assert_eq!(
        submit_block_response,
        SubmitBlockResponse::ErrorResponse(SubmitBlockErrorResponse::Rejected),
        "invalid block with insufficient coinbase output value should be rejected"
    );

    // Check that the original block template can be submitted successfully
    let proposal_block = proposal_block_from_template(&valid_original_block_template, None, &net)?;

    let submit_block_response = rpc
        .submit_block(HexData(proposal_block.zcash_serialize_to_vec()?), None)
        .await?;

    assert_eq!(
        submit_block_response,
        SubmitBlockResponse::Accepted,
        "valid block should be accepted"
    );

    Ok(())
}

/// Test successful block template submission as a block proposal.
///
/// This test can be run locally with:
/// `cargo test --package zebrad --test zebrad-tests -- nu6_3_block_template_proposal --exact --show-output`
#[tokio::test(flavor = "multi_thread")]
async fn nu6_3_block_template_proposal() -> Result<()> {
    use zebra_chain::{
        chain_sync_status::MockSyncStatus,
        parameters::testnet::{self, ConfiguredActivationHeights, ConfiguredFundingStreams},
        work::difficulty::U256,
    };
    use zebra_network::address_book_peers::MockAddressBookPeers;
    use zebra_node_services::mempool;
    use zebra_rpc::client::HexData;
    use zebra_test::mock_service::MockService;

    use zebra_rpc::{
        client::{
            GetBlockTemplateParameters, GetBlockTemplateRequestMode, GetBlockTemplateResponse,
        },
        methods::{RpcImpl, RpcServer},
        proposal_block_from_template, SubmitBlockChannel,
    };

    let _init_guard = zebra_test::init();

    tracing::info!("running nu6_3_block_template_proposal test");

    let base_network_params = testnet::Parameters::build()
        // Regtest genesis hash
        .with_genesis_hash("029f11d80ef9765602235e1bc9727e3eb6ba20839319f761fee920d63401e327")
        .unwrap()
        .with_checkpoints(false)
        .unwrap()
        .with_target_difficulty_limit(U256::from_big_endian(&[0x0f; 32]))
        .unwrap()
        .with_disable_pow(true)
        .with_slow_start_interval(Height::MIN)
        .with_lockbox_disbursements(vec![])
        .with_activation_heights(ConfiguredActivationHeights {
            nu6_3: Some(1),
            ..Default::default()
        });

    let network = base_network_params
        .clone()
        .unwrap()
        .with_funding_streams(vec![ConfiguredFundingStreams {
            // Start checking funding streams from block height 1
            height_range: Some(Height(1)..Height(100)),
            // Use default post-NU6 recipients
            recipients: None,
        }])
        .to_network()
        .unwrap();

    tracing::info!("built configured Testnet, starting state service and block verifier");

    let default_test_config = default_test_config(&network);
    let mining_config = default_test_config.mining;

    let (state, read_state, latest_chain_tip, _chain_tip_change) =
        zebra_state::init_test_services(&network).await;

    let (
        block_verifier_router,
        _transaction_verifier,
        _parameter_download_task_handle,
        _max_checkpoint_height,
    ) = zebra_consensus::router::init_test(
        zebra_consensus::Config::default(),
        &network,
        state.clone(),
    )
    .await;

    tracing::info!("started state service and block verifier, committing Regtest genesis block");

    let genesis_hash = block_verifier_router
        .clone()
        .oneshot(zebra_consensus::Request::Commit(regtest_genesis_block()))
        .await
        .expect("should validate Regtest genesis block");

    let mut mempool = MockService::build()
        .with_max_request_delay(Duration::from_secs(5))
        .for_unit_tests();
    let mut mock_sync_status = MockSyncStatus::default();
    mock_sync_status.set_is_close_to_tip(true);

    let submitblock_channel = SubmitBlockChannel::new();

    let (_tx, rx) = tokio::sync::watch::channel(None);

    let (rpc, _) = RpcImpl::new(
        network.clone(),
        mining_config,
        false,
        "0.0.1",
        "Zebra tests",
        mempool.clone(),
        state.clone(),
        read_state.clone(),
        block_verifier_router,
        mock_sync_status,
        latest_chain_tip,
        MockAddressBookPeers::default(),
        rx,
        Some(submitblock_channel.sender()),
    );

    let make_mock_mempool_request_handler = || async move {
        mempool
            .expect_request(mempool::Request::FullTransactions)
            .await
            .respond(mempool::Response::FullTransactions {
                transactions: vec![],
                transaction_dependencies: Default::default(),
                // tip hash needs to match chain info for long poll requests
                last_seen_tip_hash: genesis_hash,
            });
    };

    let block_template_fut = rpc.get_block_template(None);
    let mock_mempool_request_handler = make_mock_mempool_request_handler.clone()();
    let (block_template, _) = tokio::join!(block_template_fut, mock_mempool_request_handler);
    let GetBlockTemplateResponse::TemplateMode(block_template) =
        block_template.expect("unexpected error in getblocktemplate RPC call")
    else {
        panic!("this getblocktemplate call without parameters should return the `TemplateMode` variant of the response")
    };

    let proposal_block = proposal_block_from_template(&block_template, None, &network)?;
    let hex_proposal_block = HexData(proposal_block.zcash_serialize_to_vec()?);

    // Check that the block template is a valid block proposal
    let GetBlockTemplateResponse::ProposalMode(block_proposal_result) = rpc
        .get_block_template(Some(GetBlockTemplateParameters::new(
            GetBlockTemplateRequestMode::Proposal,
            Some(hex_proposal_block),
            Default::default(),
            Default::default(),
            Default::default(),
        )))
        .await?
    else {
        panic!(
            "this getblocktemplate call should return the `ProposalMode` variant of the response"
        )
    };

    assert!(
        block_proposal_result.is_valid(),
        "block proposal should succeed"
    );

    // Submit the same block
    let submit_block_response = rpc
        .submit_block(HexData(proposal_block.zcash_serialize_to_vec()?), None)
        .await?;

    assert_eq!(
        submit_block_response,
        SubmitBlockResponse::Accepted,
        "valid block should be accepted"
    );

    Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn invalidate_and_reconsider_block() -> Result<()> {
    use zebra_chain::block;

    let _init_guard = zebra_test::init();
    let net = Network::new_regtest(
        ConfiguredActivationHeights {
            nu7: Some(100),
            ..Default::default()
        }
        .into(),
    );
    let mut config = os_assigned_rpc_port_config(false, &net)?;
    config.state.ephemeral = false;

    let test_dir = testdir()?.with_config(&mut config)?;

    let mut child = test_dir.spawn_child(args!["start"])?;
    let rpc_address = read_listen_addr_from_logs(&mut child, OPENED_RPC_ENDPOINT_MSG)?;

    tracing::info!("waiting for Zebra state cache to be opened");

    tokio::time::sleep(LAUNCH_DELAY).await;

    let rpc_client = RpcRequestClient::new(rpc_address);
    let mut blocks = Vec::new();
    for _ in 0..50 {
        let (block, _) = rpc_client.block_from_template(&net).await?;

        rpc_client.submit_block(block.clone()).await?;
        blocks.push(block);
    }

    tracing::info!("checking that read state has the new non-finalized best chain blocks");
    for expected_block in blocks.clone() {
        let height = expected_block.coinbase_height().unwrap();
        let zebra_block = rpc_client
            .get_block(height.0 as i32)
            .await
            .map_err(|err| eyre!(err))?
            .expect("Zebra test child should have the expected block");

        assert_eq!(
            zebra_block,
            Arc::new(expected_block),
            "Zebra should have the same block"
        );
    }

    tracing::info!("invalidating blocks");

    // Note: This is the block at height 7, it's the 6th generated block.
    let block_6_hash = blocks
        .get(5)
        .expect("should have 50 blocks")
        .hash()
        .to_string();
    let params = serde_json::to_string(&vec![block_6_hash]).expect("should serialize successfully");

    let _: () = rpc_client
        .json_result_from_call("invalidateblock", &params)
        .await
        .map_err(|err| eyre!(err))?;

    let expected_reconsidered_hashes = blocks
        .iter()
        .skip(5)
        .map(|block| block.hash())
        .collect::<Vec<_>>();

    tracing::info!("reconsidering blocks");

    let reconsidered_hashes: Vec<block::Hash> = rpc_client
        .json_result_from_call("reconsiderblock", &params)
        .await
        .map_err(|err| eyre!(err))?;

    assert_eq!(
        reconsidered_hashes, expected_reconsidered_hashes,
        "reconsidered hashes should match expected hashes"
    );

    child.kill(false)?;
    let output = child.wait_with_output()?;

    // Make sure the command was killed
    output.assert_was_killed()?;

    output.assert_failure()?;

    Ok(())
}