Skip to content

Commit

Permalink
feat(congestion) - Initialize congestion info in the genesis-populate…
Browse files Browse the repository at this point in the history
… tool (near#11596)

In forknet the genesis may contains some delayed / buffered receipts.
It's important to initialize the congestion info correctly otherwise
integer overflow may happen when updating the congestion info.

This is untested since I don't know how, looking for hints here :)
  • Loading branch information
wacban authored Jun 20, 2024
1 parent f98b2bd commit c2315ca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions genesis-tools/genesis-populate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ near-store.workspace = true
near-chain.workspace = true
near-test-contracts.workspace = true
near-vm-runner.workspace = true
node-runtime.workspace = true

[features]
nightly_protocol = [
Expand All @@ -38,6 +39,7 @@ nightly_protocol = [
"near-store/nightly_protocol",
"near-vm-runner/nightly_protocol",
"nearcore/nightly_protocol",
"node-runtime/nightly_protocol",
]
nightly = [
"near-async/nightly",
Expand All @@ -49,4 +51,5 @@ nightly = [
"near-vm-runner/nightly",
"nearcore/nightly",
"nightly_protocol",
"node-runtime/nightly",
]
39 changes: 26 additions & 13 deletions genesis-tools/genesis-populate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use near_store::{
use near_vm_runner::logic::ProtocolVersion;
use near_vm_runner::ContractCode;
use nearcore::{NearConfig, NightshadeRuntime, NightshadeRuntimeExt};
pub use node_runtime::bootstrap_congestion_info;
use std::collections::BTreeMap;
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -249,17 +250,20 @@ impl GenesisBuilder {
store_update.save_block(genesis.clone());

let protocol_version = self.genesis.config.protocol_version;
let congestion_info = Self::get_congestion_control(protocol_version);
for (chunk_header, state_root) in genesis.chunks().iter().zip(self.roots.values()) {
for (chunk_header, &state_root) in genesis.chunks().iter().zip(self.roots.values()) {
let shard_layout = &self.genesis.config.shard_layout;
let shard_id = chunk_header.shard_id();
let shard_uid = ShardUId::from_shard_id_and_layout(shard_id, &shard_layout);

let congestion_info =
self.get_congestion_info(protocol_version, &genesis, shard_id, state_root)?;

store_update.save_chunk_extra(
genesis.hash(),
&ShardUId::from_shard_id_and_layout(
chunk_header.shard_id(),
&self.genesis.config.shard_layout,
),
&shard_uid,
ChunkExtra::new(
self.genesis.config.protocol_version,
state_root,
&state_root,
CryptoHash::default(),
vec![],
0,
Expand All @@ -278,13 +282,22 @@ impl GenesisBuilder {
Ok(())
}

fn get_congestion_control(protocol_version: ProtocolVersion) -> Option<CongestionInfo> {
if ProtocolFeature::CongestionControl.enabled(protocol_version) {
// TODO(congestion_control) - properly initialize
Some(CongestionInfo::default())
} else {
None
fn get_congestion_info(
&self,
protocol_version: ProtocolVersion,
genesis: &Block,
shard_id: u64,
state_root: CryptoHash,
) -> Result<Option<CongestionInfo>> {
if !ProtocolFeature::CongestionControl.enabled(protocol_version) {
return Ok(None);
}
let prev_hash = genesis.header().prev_hash();
let trie = self.runtime.get_trie_for_shard(shard_id, prev_hash, state_root, true)?;
let protocol_config = self.runtime.get_protocol_config(genesis.header().epoch_id())?;
let runtime_config = protocol_config.runtime_config;
let congestion_info = bootstrap_congestion_info(&trie, &runtime_config, shard_id)?;
Ok(Some(congestion_info))
}

fn add_additional_account(&mut self, account_id: AccountId) -> Result<()> {
Expand Down

0 comments on commit c2315ca

Please sign in to comment.