Skip to content

Commit

Permalink
feat(congestion_control) - Added more info to the InvalidCongestionIn…
Browse files Browse the repository at this point in the history
…fo error (#11461)
  • Loading branch information
wacban authored Jun 3, 2024
1 parent 118df5b commit f7679a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
8 changes: 4 additions & 4 deletions chain/chain-primitives/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ pub enum Error {
#[error("Invalid Balance Burnt")]
InvalidBalanceBurnt,
/// Invalid Congestion Info
#[error("Invalid Congestion Info")]
InvalidCongestionInfo,
#[error("Invalid Congestion Info: {0}")]
InvalidCongestionInfo(String),
/// Invalid shard id
#[error("Shard id {0} does not exist")]
InvalidShardId(ShardId),
Expand Down Expand Up @@ -302,7 +302,7 @@ impl Error {
| Error::InvalidGasPrice
| Error::InvalidGasUsed
| Error::InvalidBalanceBurnt
| Error::InvalidCongestionInfo
| Error::InvalidCongestionInfo(_)
| Error::InvalidShardId(_)
| Error::InvalidStateRequest(_)
| Error::InvalidRandomnessBeaconOutput
Expand Down Expand Up @@ -378,7 +378,7 @@ impl Error {
Error::InvalidGasPrice => "invalid_gas_price",
Error::InvalidGasUsed => "invalid_gas_used",
Error::InvalidBalanceBurnt => "invalid_balance_burnt",
Error::InvalidCongestionInfo => "invalid_congestion_info",
Error::InvalidCongestionInfo(_) => "invalid_congestion_info",
Error::InvalidShardId(_) => "invalid_shard_id",
Error::InvalidStateRequest(_) => "invalid_state_request",
Error::InvalidRandomnessBeaconOutput => "invalid_randomness_beacon_output",
Expand Down
21 changes: 15 additions & 6 deletions chain/chain/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,31 +204,40 @@ fn validate_congestion_info(
// The congestion info should be Some iff the congestion control features is enabled.
let enabled = ProtocolFeature::CongestionControl.enabled(header_protocol_version);
if header_congestion_info.is_some() != enabled {
return Err(Error::InvalidCongestionInfo);
return Err(Error::InvalidCongestionInfo("Congestion Information is missing.".to_string()));
}

match (extra_congestion_info, header_congestion_info) {
// If both are none then there is no congestion info to validate.
(None, None) => Ok(()),
// If the congestion control is enabled in the previous chunk then it should
// also be enabled in the current chunk.
(Some(_), None) => Err(Error::InvalidCongestionInfo),
(Some(info), None) => Err(Error::InvalidCongestionInfo(format!(
"Congestion Information disappeared. {:?}.",
info
))),
// At the epoch boundary where congestion control was enabled the chunk
// extra does not have the congestion control enabled and the header does
// have it enabled. The chunk extra of the previous chunk does not have
// congestion info so the congestion info in the current chunk header should
// be set to the default one.
(None, Some(_)) => {
if header_congestion_info == &Some(CongestionInfo::default()) {
(None, Some(info)) => {
if info == &CongestionInfo::default() {
Ok(())
} else {
Err(Error::InvalidCongestionInfo)
Err(Error::InvalidCongestionInfo(format!(
"Congestion Information invalid after protocol upgrade. {:?}",
info
)))
}
}
// Congestion Info is present in both the extra and the header. Validate it.
(Some(extra), Some(header)) => {
if !CongestionInfo::validate_extra_and_header(extra, header) {
Err(Error::InvalidCongestionInfo)
Err(Error::InvalidCongestionInfo(format!(
"Congestion Information mismatch. extra: {:?}, header: {:?}",
extra, header
)))
} else {
Ok(())
}
Expand Down

0 comments on commit f7679a7

Please sign in to comment.