Skip to content

Commit

Permalink
Add test for blocks produced on optimistic block
Browse files Browse the repository at this point in the history
  • Loading branch information
VanBarbascu committed Jan 23, 2025
1 parent b81214b commit 276b198
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions chain/client/src/tests/process_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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::optimistic_block::OptimisticBlock;
use near_primitives::sharding::ShardChunkHeader;
use near_primitives::sharding::ShardChunkHeaderV3;
use near_primitives::test_utils::create_test_signer;
Expand Down Expand Up @@ -287,3 +288,33 @@ fn test_bad_congestion_info_corrupt_allowed_shard() {
fn test_bad_congestion_info_none() {
test_bad_congestion_info_impl(BadCongestionInfoMode::None);
}

// Helper function to check that a block was produced from an optimistic block
fn check_block_produced_from_optimistic_block(block: &Block, optimistic_block: &OptimisticBlock) {
assert_eq!(block.header().height(), optimistic_block.inner.block_height, "height");
assert_eq!(
block.header().prev_hash(),
&optimistic_block.inner.prev_block_hash,
"previous hash"
);
assert_eq!(block.header().raw_timestamp(), optimistic_block.inner.block_timestamp, "timestamp");
assert_eq!(block.header().random_value(), &optimistic_block.inner.random_value, "random value");
}

// Testing the production and application of optimistic blocks
#[test]
fn test_process_optimistic_block() {
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);
assert!(!env.clients[0].is_optimistic_block_done(2), "Optimistic block should not be ready");
let optimistic_block = env.clients[0].produce_optimistic_block_on_head(2).unwrap().unwrap();
// Store optimistic block to be used at block production.
env.clients[0].save_optimistic_block(&optimistic_block);
assert!(env.clients[0].is_optimistic_block_done(2), "Optimistic block should be ready");

// TODO(#10584): Process chunks with optimistic block

let block = env.clients[0].produce_block(2).unwrap().unwrap();
check_block_produced_from_optimistic_block(&block, &optimistic_block);
}

0 comments on commit 276b198

Please sign in to comment.