@@ -52,14 +52,15 @@ use near_async::messaging::{noop, IntoMultiSender};
52
52
use near_async:: time:: { Clock , Duration , Instant } ;
53
53
use near_chain_configs:: { MutableConfigValue , MutableValidatorSigner } ;
54
54
use near_chain_primitives:: error:: { BlockKnownError , Error , LogTransientStorageError } ;
55
+ use near_epoch_manager:: shard_assignment:: shard_id_to_uid;
55
56
use near_epoch_manager:: shard_tracker:: ShardTracker ;
56
57
use near_epoch_manager:: EpochManagerAdapter ;
57
58
use near_primitives:: bandwidth_scheduler:: BandwidthRequests ;
58
59
use near_primitives:: block:: { genesis_chunks, Block , BlockValidityError , Chunks , MaybeNew , Tip } ;
59
60
use near_primitives:: block_header:: BlockHeader ;
60
61
use near_primitives:: challenge:: {
61
62
BlockDoubleSign , Challenge , ChallengeBody , ChallengesResult , ChunkProofs , ChunkState ,
62
- MaybeEncodedShardChunk , PartialState , SlashedValidator ,
63
+ MaybeEncodedShardChunk , SlashedValidator ,
63
64
} ;
64
65
use near_primitives:: checked_feature;
65
66
use near_primitives:: congestion_info:: CongestionInfo ;
@@ -74,6 +75,7 @@ use near_primitives::sharding::{
74
75
ChunkHash , ChunkHashHeight , EncodedShardChunk , ReceiptList , ReceiptProof , ShardChunk ,
75
76
ShardChunkHeader , ShardProof , StateSyncInfo ,
76
77
} ;
78
+ use near_primitives:: state:: PartialState ;
77
79
use near_primitives:: state_part:: PartId ;
78
80
use near_primitives:: state_sync:: {
79
81
get_num_state_parts, ReceiptProofResponse , RootProof , ShardStateSyncResponseHeader ,
@@ -365,12 +367,8 @@ impl Chain {
365
367
) -> Result < Chain , Error > {
366
368
let store = runtime_adapter. store ( ) ;
367
369
let transaction_validity_period = chain_genesis. transaction_validity_period ;
368
- let chain_store = ChainStore :: new (
369
- store. clone ( ) ,
370
- chain_genesis. height ,
371
- save_trie_changes,
372
- transaction_validity_period,
373
- ) ;
370
+ let chain_store =
371
+ ChainStore :: new ( store. clone ( ) , save_trie_changes, transaction_validity_period) ;
374
372
let state_roots = get_genesis_state_roots ( runtime_adapter. store ( ) ) ?
375
373
. expect ( "genesis should be initialized." ) ;
376
374
let ( genesis, _genesis_chunks) = Self :: make_genesis_block (
@@ -439,7 +437,6 @@ impl Chain {
439
437
// Check if we have a head in the store, otherwise pick genesis block.
440
438
let mut chain_store = ChainStore :: new (
441
439
runtime_adapter. store ( ) . clone ( ) ,
442
- chain_genesis. height ,
443
440
chain_config. save_trie_changes ,
444
441
transaction_validity_period,
445
442
) ;
@@ -710,7 +707,7 @@ impl Chain {
710
707
711
708
store_update. save_chunk_extra (
712
709
genesis. hash ( ) ,
713
- & epoch_manager . shard_id_to_uid ( chunk_header. shard_id ( ) , & EpochId :: default ( ) ) ?,
710
+ & shard_id_to_uid ( epoch_manager , chunk_header. shard_id ( ) , & EpochId :: default ( ) ) ?,
714
711
Self :: create_genesis_chunk_extra (
715
712
state_root,
716
713
chain_genesis. gas_limit ,
@@ -901,14 +898,8 @@ impl Chain {
901
898
fn partial_verify_orphan_header_signature ( & self , header : & BlockHeader ) -> Result < bool , Error > {
902
899
let block_producer =
903
900
self . epoch_manager . get_block_producer ( header. epoch_id ( ) , header. height ( ) ) ?;
904
- // DEVNOTE: we pass head which is not necessarily on block's chain, but it's only used for
905
- // slashing info which we will ignore
906
- let head = self . head ( ) ?;
907
- let ( block_producer, _slashed) = self . epoch_manager . get_validator_by_account_id (
908
- header. epoch_id ( ) ,
909
- & head. last_block_hash ,
910
- & block_producer,
911
- ) ?;
901
+ let block_producer =
902
+ self . epoch_manager . get_validator_by_account_id ( header. epoch_id ( ) , & block_producer) ?;
912
903
Ok ( header. signature ( ) . verify ( header. hash ( ) . as_ref ( ) , block_producer. public_key ( ) ) )
913
904
}
914
905
@@ -1206,7 +1197,6 @@ impl Chain {
1206
1197
& self ,
1207
1198
challenges : & [ Challenge ] ,
1208
1199
epoch_id : & EpochId ,
1209
- prev_block_hash : & CryptoHash ,
1210
1200
) -> Result < ( ChallengesResult , Vec < CryptoHash > ) , Error > {
1211
1201
let _span = tracing:: debug_span!(
1212
1202
target: "chain" ,
@@ -1220,7 +1210,6 @@ impl Chain {
1220
1210
self . epoch_manager . as_ref ( ) ,
1221
1211
self . runtime_adapter . as_ref ( ) ,
1222
1212
epoch_id,
1223
- prev_block_hash,
1224
1213
challenge,
1225
1214
) {
1226
1215
Ok ( ( hash, account_ids) ) => {
@@ -1557,7 +1546,7 @@ impl Chain {
1557
1546
/// soon as possible and allow next block producer to skip invalid blocks.
1558
1547
pub fn process_challenge ( & mut self , challenge : & Challenge ) {
1559
1548
let head = unwrap_or_return ! ( self . head( ) ) ;
1560
- match self . verify_challenges ( & [ challenge. clone ( ) ] , & head. epoch_id , & head . last_block_hash ) {
1549
+ match self . verify_challenges ( & [ challenge. clone ( ) ] , & head. epoch_id ) {
1561
1550
Ok ( ( _, challenged_blocks) ) => {
1562
1551
let mut chain_update = self . chain_update ( ) ;
1563
1552
for block_hash in challenged_blocks {
@@ -2010,7 +1999,7 @@ impl Chain {
2010
1999
true ,
2011
2000
) ;
2012
2001
let care_about_shard_this_or_next_epoch = care_about_shard || will_care_about_shard;
2013
- let shard_uid = self . epoch_manager . shard_id_to_uid ( shard_id, & epoch_id) . unwrap ( ) ;
2002
+ let shard_uid = shard_id_to_uid ( self . epoch_manager . as_ref ( ) , shard_id, epoch_id) ? ;
2014
2003
if care_about_shard_this_or_next_epoch {
2015
2004
shards_cares_this_or_next_epoch. push ( shard_uid) ;
2016
2005
}
@@ -2177,7 +2166,7 @@ impl Chain {
2177
2166
shard_id : ShardId ,
2178
2167
) -> Result < ( ) , Error > {
2179
2168
let epoch_id = block. header ( ) . epoch_id ( ) ;
2180
- let shard_uid = self . epoch_manager . shard_id_to_uid ( shard_id, epoch_id) ?;
2169
+ let shard_uid = shard_id_to_uid ( self . epoch_manager . as_ref ( ) , shard_id, epoch_id) ?;
2181
2170
2182
2171
// Update flat storage.
2183
2172
let flat_storage_manager = self . runtime_adapter . get_flat_storage_manager ( ) ;
@@ -2334,7 +2323,7 @@ impl Chain {
2334
2323
}
2335
2324
2336
2325
let ( challenges_result, challenged_blocks) =
2337
- self . verify_challenges ( block. challenges ( ) , header. epoch_id ( ) , header . prev_hash ( ) ) ?;
2326
+ self . verify_challenges ( block. challenges ( ) , header. epoch_id ( ) ) ?;
2338
2327
2339
2328
let prev_block = self . get_block ( & prev_hash) ?;
2340
2329
@@ -3297,7 +3286,7 @@ impl Chain {
3297
3286
shard_id,
3298
3287
true ,
3299
3288
) {
3300
- let shard_uid = self . epoch_manager . shard_id_to_uid ( shard_id, & epoch_id) ?;
3289
+ let shard_uid = shard_id_to_uid ( self . epoch_manager . as_ref ( ) , shard_id, epoch_id) ?;
3301
3290
self . resharding_manager . start_resharding (
3302
3291
self . chain_store . store_update ( ) ,
3303
3292
& block,
@@ -3742,7 +3731,7 @@ impl Chain {
3742
3731
cares_about_shard_next_epoch,
3743
3732
cared_about_shard_prev_epoch,
3744
3733
) ;
3745
- let shard_uid = self . epoch_manager . shard_id_to_uid ( shard_id, epoch_id) ?;
3734
+ let shard_uid = shard_id_to_uid ( self . epoch_manager . as_ref ( ) , shard_id, epoch_id) ?;
3746
3735
Ok ( ShardContext { shard_uid, should_apply_chunk } )
3747
3736
}
3748
3737
@@ -4483,9 +4472,16 @@ impl Chain {
4483
4472
) -> HashMap < ShardId , Vec < Receipt > > {
4484
4473
let mut result = HashMap :: new ( ) ;
4485
4474
for receipt in receipts {
4486
- let shard_id = shard_layout. account_id_to_shard_id ( receipt. receiver_id ( ) ) ;
4487
- let entry = result. entry ( shard_id) . or_insert_with ( Vec :: new) ;
4488
- entry. push ( receipt)
4475
+ if receipt. send_to_all_shards ( ) {
4476
+ for shard_id in shard_layout. shard_ids ( ) {
4477
+ let entry = result. entry ( shard_id) . or_insert_with ( Vec :: new) ;
4478
+ entry. push ( receipt. clone ( ) ) ;
4479
+ }
4480
+ } else {
4481
+ let shard_id = shard_layout. account_id_to_shard_id ( receipt. receiver_id ( ) ) ;
4482
+ let entry = result. entry ( shard_id) . or_insert_with ( Vec :: new) ;
4483
+ entry. push ( receipt) ;
4484
+ }
4489
4485
}
4490
4486
result
4491
4487
}
@@ -4506,13 +4502,22 @@ impl Chain {
4506
4502
}
4507
4503
let mut cache = HashMap :: new ( ) ;
4508
4504
for receipt in receipts {
4509
- let & mut shard_id = cache
4510
- . entry ( receipt. receiver_id ( ) )
4511
- . or_insert_with ( || shard_layout. account_id_to_shard_id ( receipt. receiver_id ( ) ) ) ;
4512
- // This unwrap should be safe as we pre-populated the map with all
4513
- // valid shard ids.
4514
- let shard_index = shard_layout. get_shard_index ( shard_id) . unwrap ( ) ;
4515
- result_map. get_mut ( & shard_index) . unwrap ( ) . 1 . push ( receipt) ;
4505
+ if receipt. send_to_all_shards ( ) {
4506
+ for shard_id in shard_layout. shard_ids ( ) {
4507
+ // This unwrap should be safe as we pre-populated the map with all
4508
+ // valid shard ids.
4509
+ let shard_index = shard_layout. get_shard_index ( shard_id) . unwrap ( ) ;
4510
+ result_map. get_mut ( & shard_index) . unwrap ( ) . 1 . push ( receipt) ;
4511
+ }
4512
+ } else {
4513
+ let & mut shard_id = cache
4514
+ . entry ( receipt. receiver_id ( ) )
4515
+ . or_insert_with ( || shard_layout. account_id_to_shard_id ( receipt. receiver_id ( ) ) ) ;
4516
+ // This unwrap should be safe as we pre-populated the map with all
4517
+ // valid shard ids.
4518
+ let shard_index = shard_layout. get_shard_index ( shard_id) . unwrap ( ) ;
4519
+ result_map. get_mut ( & shard_index) . unwrap ( ) . 1 . push ( receipt) ;
4520
+ }
4516
4521
}
4517
4522
4518
4523
let mut result_vec = vec ! [ ] ;
0 commit comments