Skip to content

Commit

Permalink
congestion: genesis congestion info (near#11362)
Browse files Browse the repository at this point in the history
A small PR to initialize the congestion info for genesis.

Please check if this is correct. But I think we can just take the
genesis block and read the congestion info from there. I assume it's
usually empty, but if genesis somehow contains delayed or buffered
receipts, I assume it should be included in the chunk header.
  • Loading branch information
jakmeier authored May 21, 2024
1 parent 141e7e5 commit 48ecde9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
32 changes: 21 additions & 11 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,17 @@ impl Chain {
.save_block_extra(genesis.hash(), BlockExtra { challenges_result: vec![] });

for (chunk_header, state_root) in genesis.chunks().iter().zip(state_roots.iter()) {
let congestion_info = if ProtocolFeature::CongestionControl
.enabled(chain_genesis.protocol_version)
{
genesis
.shards_congestion_info()
.get(&chunk_header.shard_id())
.map(|info| info.congestion_info)
} else {
None
};

store_update.save_chunk_extra(
genesis.hash(),
&epoch_manager
Expand All @@ -480,6 +491,7 @@ impl Chain {
state_root,
chain_genesis.gas_limit,
chain_genesis.protocol_version,
congestion_info,
),
);
}
Expand Down Expand Up @@ -610,6 +622,7 @@ impl Chain {
state_root: &StateRoot,
gas_limit: Gas,
genesis_protocol_version: ProtocolVersion,
congestion_info: Option<CongestionInfo>,
) -> ChunkExtra {
ChunkExtra::new(
genesis_protocol_version,
Expand All @@ -619,23 +632,15 @@ impl Chain {
0,
gas_limit,
0,
Self::get_genesis_congestion_info(genesis_protocol_version),
congestion_info,
)
}

fn get_genesis_congestion_info(protocol_version: ProtocolVersion) -> Option<CongestionInfo> {
if ProtocolFeature::CongestionControl.enabled(protocol_version) {
// TODO(congestion_control) - properly initialize
Some(CongestionInfo::default())
} else {
None
}
}

pub fn genesis_chunk_extra(
&self,
shard_id: ShardId,
genesis_protocol_version: ProtocolVersion,
congestion_info: Option<CongestionInfo>,
) -> Result<ChunkExtra, Error> {
let shard_index = shard_id as usize;
let state_root = *get_genesis_state_roots(self.chain_store.store())?
Expand All @@ -652,7 +657,12 @@ impl Chain {
Error::Other(format!("genesis chunk does not exist for shard {shard_index}"))
})?
.gas_limit();
Ok(Self::create_genesis_chunk_extra(&state_root, gas_limit, genesis_protocol_version))
Ok(Self::create_genesis_chunk_extra(
&state_root,
gas_limit,
genesis_protocol_version,
congestion_info,
))
}

/// Creates a light client block for the last final block from perspective of some other block
Expand Down
12 changes: 7 additions & 5 deletions chain/client/src/stateless_validation/chunk_validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,14 @@ pub(crate) fn pre_validate_chunk_state_witness(

let main_transition_params = if last_chunk_block.header().is_genesis() {
let epoch_id = last_chunk_block.header().epoch_id();
let congestion_info = last_chunk_block
.shards_congestion_info()
.get(&shard_id)
.map(|info| info.congestion_info);
let genesis_protocol_version = epoch_manager.get_epoch_protocol_version(&epoch_id)?;
MainTransition::Genesis {
chunk_extra: chain.genesis_chunk_extra(shard_id, genesis_protocol_version)?,
block_hash: *last_chunk_block.hash(),
shard_id,
}
let chunk_extra =
chain.genesis_chunk_extra(shard_id, genesis_protocol_version, congestion_info)?;
MainTransition::Genesis { chunk_extra, block_hash: *last_chunk_block.hash(), shard_id }
} else {
MainTransition::NewChunk(NewChunkData {
chunk_header: last_chunk_block.chunks().get(shard_id as usize).unwrap().clone(),
Expand Down

0 comments on commit 48ecde9

Please sign in to comment.