/
process_blocks.rs
137 lines (127 loc) · 5.95 KB
/
process_blocks.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
use crate::test_utils::TestEnv;
use assert_matches::assert_matches;
use near_chain::{test_utils, Provenance};
use near_crypto::vrf::Value;
use near_crypto::{KeyType, PublicKey, Signature};
use near_network::types::{NetworkRequests, PeerManagerMessageRequest};
use near_primitives::block::Block;
use near_primitives::congestion_info::CongestionInfo;
use near_primitives::network::PeerId;
use near_primitives::sharding::ShardChunkHeader;
use near_primitives::sharding::ShardChunkHeaderV3;
use near_primitives::test_utils::create_test_signer;
use near_primitives::types::validator_stake::ValidatorStake;
use near_primitives::utils::MaybeValidated;
use near_primitives::version::PROTOCOL_VERSION;
/// Only process one block per height
/// Test that if a node receives two blocks at the same height, it doesn't process the second one
/// if the second block is not requested
#[test]
fn test_not_process_height_twice() {
let mut env = TestEnv::default_builder().mock_epoch_managers().build();
let block = env.clients[0].produce_block(1).unwrap().unwrap();
// modify the block and resign it
let mut duplicate_block = block.clone();
env.process_block(0, block, Provenance::PRODUCED);
let validator_signer = create_test_signer("test0");
let proposals =
vec![ValidatorStake::new("test1".parse().unwrap(), PublicKey::empty(KeyType::ED25519), 0)];
duplicate_block.mut_header().get_mut().inner_rest.prev_validator_proposals = proposals;
duplicate_block.mut_header().resign(&validator_signer);
let dup_block_hash = *duplicate_block.hash();
// we should have dropped the block before we even tried to process it, so the result should be ok
env.clients[0]
.receive_block_impl(
duplicate_block,
PeerId::new(PublicKey::empty(KeyType::ED25519)),
false,
None,
)
.unwrap();
// check that the second block is not being processed
assert!(!test_utils::is_block_in_processing(&env.clients[0].chain, &dup_block_hash));
// check that we didn't rebroadcast the second block
while let Some(msg) = env.network_adapters[0].pop() {
assert!(!matches!(
msg,
PeerManagerMessageRequest::NetworkRequests(NetworkRequests::Block { .. })
));
}
}
/// Test that if a block contains chunks with invalid shard_ids, the client will return error.
#[test]
fn test_bad_shard_id() {
let mut env = TestEnv::default_builder().num_shards(4).mock_epoch_managers().build();
let prev_block = env.clients[0].produce_block(1).unwrap().unwrap();
env.process_block(0, prev_block, Provenance::PRODUCED);
let mut block = env.clients[0].produce_block(2).unwrap().unwrap(); // modify the block and resign it
let validator_signer = create_test_signer("test0");
let mut chunks: Vec<_> = block.chunks().iter().cloned().collect();
// modify chunk 0 to have shard_id 1
let chunk = chunks.get(0).unwrap();
let outgoing_receipts_root = chunks.get(1).unwrap().prev_outgoing_receipts_root();
let mut modified_chunk = ShardChunkHeaderV3::new(
PROTOCOL_VERSION,
*chunk.prev_block_hash(),
chunk.prev_state_root(),
chunk.prev_outcome_root(),
chunk.encoded_merkle_root(),
chunk.encoded_length(),
2,
1,
chunk.prev_gas_used(),
chunk.gas_limit(),
chunk.prev_balance_burnt(),
outgoing_receipts_root,
chunk.tx_root(),
chunk.prev_validator_proposals().collect(),
CongestionInfo::default(),
&validator_signer,
);
modified_chunk.height_included = 2;
chunks[0] = ShardChunkHeader::V3(modified_chunk);
block.mut_header().get_mut().inner_rest.chunk_headers_root =
Block::compute_chunk_headers_root(&chunks).0;
block.mut_header().get_mut().inner_rest.prev_chunk_outgoing_receipts_root =
Block::compute_chunk_prev_outgoing_receipts_root(&chunks);
block.set_chunks(chunks);
block.mut_header().get_mut().inner_rest.block_body_hash =
block.compute_block_body_hash().unwrap();
block.mut_header().resign(&validator_signer);
let err = env.clients[0]
.process_block_test(MaybeValidated::from(block), Provenance::NONE)
.unwrap_err();
assert_matches!(err, near_chain::Error::InvalidShardId(1));
}
/// Test that if a block's content (vrf_value) is corrupted, the invalid block will not affect the node's block processing
#[test]
fn test_bad_block_content_vrf() {
let mut env = TestEnv::default_builder().num_shards(4).mock_epoch_managers().build();
let prev_block = env.clients[0].produce_block(1).unwrap().unwrap();
env.process_block(0, prev_block, Provenance::PRODUCED);
let block = env.clients[0].produce_block(2).unwrap().unwrap();
let mut bad_block = block.clone();
bad_block.set_vrf_value(Value([0u8; 32]));
let err = env.clients[0]
.receive_block_impl(bad_block, PeerId::new(PublicKey::empty(KeyType::ED25519)), false, None)
.unwrap_err();
assert_matches!(err, near_chain::Error::InvalidSignature);
let _ =
env.clients[0].process_block_test(MaybeValidated::from(block), Provenance::NONE).unwrap();
}
/// Test that if a block's signature is corrupted, the invalid block will not affect the node's block processing
#[test]
fn test_bad_block_signature() {
let mut env = TestEnv::default_builder().num_shards(4).mock_epoch_managers().build();
let prev_block = env.clients[0].produce_block(1).unwrap().unwrap();
env.process_block(0, prev_block, Provenance::PRODUCED);
let block = env.clients[0].produce_block(2).unwrap().unwrap();
let mut bad_block = block.clone();
bad_block.mut_header().get_mut().signature = Signature::default();
let err = env.clients[0]
.receive_block_impl(bad_block, PeerId::new(PublicKey::empty(KeyType::ED25519)), false, None)
.unwrap_err();
assert_matches!(err, near_chain::Error::InvalidSignature);
let _ =
env.clients[0].process_block_test(MaybeValidated::from(block), Provenance::NONE).unwrap();
}