Skip to content

Commit

Permalink
Merge pull request #19 from darthsiroftardis/bv-test
Browse files Browse the repository at this point in the history
Adjust tracking of block utilization/vacancy
  • Loading branch information
EdHastingsCasperAssociation authored Mar 28, 2024
2 parents b9e9d12 + b161b45 commit da35ffd
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 66 deletions.
3 changes: 2 additions & 1 deletion node/src/components/block_accumulator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tokio::time;
use casper_types::{
generate_ed25519_keypair, testing::TestRng, ActivationPoint, BlockV2, ChainNameDigest,
Chainspec, ChainspecRawBytes, FinalitySignature, FinalitySignatureV2, ProtocolVersion,
PublicKey, SecretKey, Signature, TestBlockBuilder, U512,
PublicKey, SecretKey, Signature, TestBlockBuilder, TransactionConfig, U512,
};
use reactor::ReactorEvent;

Expand Down Expand Up @@ -197,6 +197,7 @@ impl Reactor for MockReactor {
chainspec.core_config.recent_era_count(),
Some(registry),
false,
TransactionConfig::default(),
)
.unwrap();

Expand Down
8 changes: 4 additions & 4 deletions node/src/components/contract_runtime/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ pub fn execute_finalized_block(
);
match scratch_state.distribute_fees(fee_req) {
FeeResult::RootNotFound => {
return Err(BlockExecutionError::RootNotFound(state_root_hash))
return Err(BlockExecutionError::RootNotFound(state_root_hash));
}
FeeResult::Failure(fer) => return Err(BlockExecutionError::DistributeFees(fer)),
FeeResult::Success {
Expand Down Expand Up @@ -486,10 +486,10 @@ pub fn execute_finalized_block(
);
match scratch_state.distribute_block_rewards(rewards_req) {
BlockRewardsResult::RootNotFound => {
return Err(BlockExecutionError::RootNotFound(state_root_hash))
return Err(BlockExecutionError::RootNotFound(state_root_hash));
}
BlockRewardsResult::Failure(bre) => {
return Err(BlockExecutionError::DistributeBlockRewards(bre))
return Err(BlockExecutionError::DistributeBlockRewards(bre));
}
BlockRewardsResult::Success {
post_state_hash, ..
Expand All @@ -513,7 +513,7 @@ pub fn execute_finalized_block(
executable_block.era_id.successor(),
) {
StepResult::RootNotFound => {
return Err(BlockExecutionError::RootNotFound(state_root_hash))
return Err(BlockExecutionError::RootNotFound(state_root_hash));
}
StepResult::Failure(err) => return Err(BlockExecutionError::Step(err)),
StepResult::Success {
Expand Down
3 changes: 2 additions & 1 deletion node/src/components/contract_runtime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tempfile::TempDir;
use casper_types::{
bytesrepr::Bytes, runtime_args, BlockHash, Chainspec, ChainspecRawBytes, Deploy, Digest, EraId,
ExecutableDeployItem, PublicKey, SecretKey, TimeDiff, Timestamp, Transaction,
TransactionCategory, U512,
TransactionCategory, TransactionConfig, U512,
};

use super::*;
Expand Down Expand Up @@ -103,6 +103,7 @@ impl reactor::Reactor for Reactor {
RECENT_ERA_COUNT,
Some(registry),
false,
TransactionConfig::default(),
)
.unwrap();

Expand Down
43 changes: 27 additions & 16 deletions node/src/components/contract_runtime/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ pub(super) async fn exec_or_requeue<REv>(
{
debug!("ContractRuntime: execute_finalized_block_or_requeue");
let contract_runtime_metrics = metrics.clone();
let block_max_install_upgrade_count =
chainspec.transaction_config.block_max_install_upgrade_count;
let is_era_end = executable_block.era_report.is_some();
if is_era_end && executable_block.rewards.is_none() {
executable_block.rewards = Some(if chainspec.core_config.compute_rewards {
Expand Down Expand Up @@ -117,6 +115,8 @@ pub(super) async fn exec_or_requeue<REv>(
let block_max_standard_count = chainspec.transaction_config.block_max_standard_count;
let block_max_mint_count = chainspec.transaction_config.block_max_mint_count;
let block_max_auction_count = chainspec.transaction_config.block_max_auction_count;
let block_max_install_upgrade_count =
chainspec.transaction_config.block_max_install_upgrade_count;
let go_up = chainspec.vacancy_config.upper_threshold;
let go_down = chainspec.vacancy_config.lower_threshold;
let max = chainspec.vacancy_config.max_gas_price;
Expand All @@ -125,10 +125,32 @@ pub(super) async fn exec_or_requeue<REv>(
let era_id = executable_block.era_id;
let block_height = executable_block.height;

let switch_block_transaction_hashes = executable_block.transactions.len() as u64;
let per_block_capacity = {
block_max_install_upgrade_count
+ block_max_standard_count
+ block_max_mint_count
+ block_max_auction_count
} as u64;

let switch_block_utilization_score = {
let has_hit_slot_limt = {
(executable_block.mint.len() as u32 >= block_max_mint_count)
|| (executable_block.auction.len() as u32 >= block_max_auction_count)
|| (executable_block.standard.len() as u32 >= block_max_standard_count)
|| (executable_block.install_upgrade.len() as u32
>= block_max_install_upgrade_count)
};

if has_hit_slot_limt {
100u64
} else {
let num = executable_block.transactions.len() as u64;
Ratio::new(num * 100, per_block_capacity).to_integer()
}
};

let maybe_utilization = effect_builder
.get_block_utilization(era_id, block_height, switch_block_transaction_hashes)
.get_block_utilization(era_id, block_height, switch_block_utilization_score)
.await;

match maybe_utilization {
Expand All @@ -137,18 +159,7 @@ pub(super) async fn exec_or_requeue<REv>(
return fatal!(effect_builder, "{}", error).await;
}
Some((utilization, block_count)) => {
let per_block_capacity = {
block_max_install_upgrade_count
+ block_max_standard_count
+ block_max_mint_count
+ block_max_auction_count
} as u64;

let era_score = {
let numerator = utilization * 100;
let denominator = per_block_capacity * block_count;
Ratio::new(numerator, denominator).to_integer()
};
let era_score = { Ratio::new(utilization, block_count).to_integer() };

let new_gas_price = if era_score >= go_up {
let new_gas_price = current_gas_price + 1;
Expand Down
3 changes: 2 additions & 1 deletion node/src/components/fetcher/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use thiserror::Error;

use casper_types::{
testing::TestRng, BlockV2, Chainspec, ChainspecRawBytes, FinalitySignatureV2, Transaction,
TransactionHash, TransactionId,
TransactionConfig, TransactionHash, TransactionId,
};

use super::*;
Expand Down Expand Up @@ -295,6 +295,7 @@ impl ReactorTrait for Reactor {
chainspec.core_config.unbonding_delay,
Some(registry),
false,
TransactionConfig::default(),
)
.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion node/src/components/gossiper/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use tracing::debug;

use casper_types::{
testing::TestRng, BlockV2, Chainspec, ChainspecRawBytes, EraId, FinalitySignatureV2,
ProtocolVersion, TimeDiff, Transaction,
ProtocolVersion, TimeDiff, Transaction, TransactionConfig,
};

use super::*;
Expand Down Expand Up @@ -169,6 +169,7 @@ impl reactor::Reactor for Reactor {
RECENT_ERA_COUNT,
Some(registry),
false,
TransactionConfig::default(),
)
.unwrap();

Expand Down
36 changes: 21 additions & 15 deletions node/src/components/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use casper_types::{
Approval, ApprovalsHash, AvailableBlockRange, Block, BlockBody, BlockHash, BlockHeader,
BlockSignatures, BlockSignaturesV1, BlockSignaturesV2, BlockV2, ChainNameDigest, DeployHash,
EraId, ExecutionInfo, FinalitySignature, ProtocolVersion, SignedBlockHeader, Timestamp,
Transaction, TransactionHash, TransactionHeader, TransactionId, Transfer,
Transaction, TransactionConfig, TransactionHash, TransactionHeader, TransactionId, Transfer,
};
use datasize::DataSize;
use prometheus::Registry;
Expand Down Expand Up @@ -148,6 +148,8 @@ pub struct Storage {
max_ttl: MaxTtl,
/// The hash of the chain name.
chain_name_hash: ChainNameDigest,
/// The transaction config as specified by the chainspec.
transaction_config: TransactionConfig,
/// The utilization of blocks.
utilization_tracker: BTreeMap<EraId, BTreeMap<u64, u64>>,
}
Expand Down Expand Up @@ -245,6 +247,7 @@ impl Storage {
recent_era_count: u64,
registry: Option<&Registry>,
force_resync: bool,
transaction_config: TransactionConfig,
) -> Result<Self, FatalStorageError> {
let config = cfg.value();

Expand Down Expand Up @@ -289,6 +292,7 @@ impl Storage {
utilization_tracker: BTreeMap::new(),
metrics,
chain_name_hash: ChainNameDigest::from_chain_name(network_name),
transaction_config,
};

if force_resync {
Expand Down Expand Up @@ -960,8 +964,10 @@ impl Storage {
responder,
} => {
let block: Block = (*block).clone().into();
let transaction_config = self.transaction_config;
responder
.respond(self.put_executed_block(
transaction_config,
&block,
&approvals_hashes,
execution_results,
Expand Down Expand Up @@ -1000,11 +1006,14 @@ impl Storage {
StorageRequest::GetBlockUtilizationScore {
era_id,
block_height,
transaction_count,
switch_block_utilization,
responder,
} => {
let utilization =
self.get_block_utilization_score(era_id, block_height, transaction_count);
let utilization = self.get_block_utilization_score(
era_id,
block_height,
switch_block_utilization,
);

responder.respond(utilization).ignore()
}
Expand Down Expand Up @@ -1051,13 +1060,14 @@ impl Storage {

pub(crate) fn put_executed_block(
&mut self,
transaction_config: TransactionConfig,
block: &Block,
approvals_hashes: &ApprovalsHashes,
execution_results: HashMap<TransactionHash, ExecutionResult>,
) -> Result<bool, FatalStorageError> {
let mut txn = self.block_store.checkout_rw()?;
let transaction_hash_count = block.transaction_count();
let era_id = block.era_id();
let block_utilization_score = block.block_utilization(transaction_config);
let block_hash = txn.write(block)?;
let _ = txn.write(approvals_hashes)?;
let block_info = BlockHashHeightAndEra::new(block_hash, block.height(), block.era_id());
Expand All @@ -1068,17 +1078,13 @@ impl Storage {
})?;
txn.commit()?;

if let Some(block_score) = self.utilization_tracker.get_mut(&era_id) {
block_score.insert(block.height(), transaction_hash_count);
};

match self.utilization_tracker.get_mut(&era_id) {
Some(block_score) => {
block_score.insert(block.height(), transaction_hash_count);
block_score.insert(block.height(), block_utilization_score);
}
None => {
let mut block_score = BTreeMap::new();
block_score.insert(block.height(), transaction_hash_count);
block_score.insert(block.height(), block_utilization_score);
self.utilization_tracker.insert(era_id, block_score);
}
}
Expand Down Expand Up @@ -1967,11 +1973,11 @@ impl Storage {
&mut self,
era_id: EraId,
block_height: u64,
transaction_count: u64,
block_utilization: u64,
) -> Option<(u64, u64)> {
let ret = match self.utilization_tracker.get_mut(&era_id) {
Some(utilization) => {
utilization.entry(block_height).or_insert(transaction_count);
utilization.entry(block_height).or_insert(block_utilization);

let transaction_count = utilization.values().into_iter().sum();
let block_count = utilization.keys().len() as u64;
Expand All @@ -1980,12 +1986,12 @@ impl Storage {
}
None => {
let mut utilization = BTreeMap::new();
utilization.insert(block_height, transaction_count);
utilization.insert(block_height, block_utilization);

self.utilization_tracker.insert(era_id, utilization);

let block_count = 1u64;
Some((transaction_count, block_count))
Some((block_utilization, block_count))
}
};

Expand Down
7 changes: 6 additions & 1 deletion node/src/components/storage/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use casper_types::{
BlockSignaturesV2, BlockV2, ChainNameDigest, Chainspec, ChainspecRawBytes, Deploy, DeployHash,
Digest, EraId, ExecutionInfo, FinalitySignature, FinalitySignatureV2, Gas, InitiatorAddr,
ProtocolVersion, PublicKey, SecretKey, SignedBlockHeader, TestBlockBuilder, TestBlockV1Builder,
TimeDiff, Transaction, TransactionHash, TransactionV1Hash, Transfer, TransferV2, U512,
TimeDiff, Transaction, TransactionConfig, TransactionHash, TransactionV1Hash, Transfer,
TransferV2, U512,
};
use tempfile::tempdir;

Expand Down Expand Up @@ -193,6 +194,7 @@ fn storage_fixture(harness: &ComponentHarness<UnitTestEvent>) -> Storage {
RECENT_ERA_COUNT,
None,
false,
TransactionConfig::default(),
)
.expect("could not create storage component fixture")
}
Expand Down Expand Up @@ -223,6 +225,7 @@ fn storage_fixture_from_parts(
recent_era_count.unwrap_or(RECENT_ERA_COUNT),
None,
false,
TransactionConfig::default(),
)
.expect("could not create storage component fixture from parts")
}
Expand All @@ -245,6 +248,7 @@ fn storage_fixture_with_force_resync(cfg: &WithDir<Config>) -> Storage {
RECENT_ERA_COUNT,
None,
true,
TransactionConfig::default(),
)
.expect("could not create storage component fixture")
}
Expand Down Expand Up @@ -1782,6 +1786,7 @@ fn should_create_subdir_named_after_network() {
RECENT_ERA_COUNT,
None,
false,
TransactionConfig::default(),
)
.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion node/src/components/transaction_acceptor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use casper_types::{
Block, BlockV2, CLValue, Chainspec, ChainspecRawBytes, Contract, Deploy, EraId, HashAddr,
InvalidDeploy, InvalidTransaction, InvalidTransactionV1, Package, PricingMode, ProtocolVersion,
PublicKey, SecretKey, StoredValue, TestBlockBuilder, TimeDiff, Timestamp, Transaction,
TransactionSessionKind, TransactionV1, TransactionV1Builder, URef, U512,
TransactionConfig, TransactionSessionKind, TransactionV1, TransactionV1Builder, URef, U512,
};

use super::*;
Expand Down Expand Up @@ -910,6 +910,7 @@ impl reactor::Reactor for Reactor {
chainspec.core_config.recent_era_count(),
Some(registry),
false,
TransactionConfig::default(),
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion node/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ impl<REv> EffectBuilder<REv> {
|responder| StorageRequest::GetBlockUtilizationScore {
era_id,
block_height,
transaction_count,
switch_block_utilization: transaction_count,
responder,
},
QueueKind::FromStorage,
Expand Down
7 changes: 6 additions & 1 deletion node/src/effect/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,15 @@ pub(crate) enum StorageRequest {
},
/// Retrieve the height of the final block of the previous protocol version, if known.
GetKeyBlockHeightForActivationPoint { responder: Responder<Option<u64>> },
/// Retrieve the block utilization score.
GetBlockUtilizationScore {
/// The era id.
era_id: EraId,
/// The block height of the switch block
block_height: u64,
transaction_count: u64,
/// The utilization within the switch block.
switch_block_utilization: u64,
/// Responder, responded once the utilization for the era has been determined.
responder: Responder<Option<(u64, u64)>>,
},
}
Expand Down
1 change: 1 addition & 0 deletions node/src/reactor/main_reactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ impl reactor::Reactor for MainReactor {
chainspec.core_config.recent_era_count(),
Some(registry),
config.node.force_resync,
chainspec.transaction_config,
)?;

let contract_runtime = ContractRuntime::new(
Expand Down
Loading

0 comments on commit da35ffd

Please sign in to comment.