diff --git a/packages/rs-dapi/src/services/platform_service/broadcast_state_transition.rs b/packages/rs-dapi/src/services/platform_service/broadcast_state_transition.rs index b14c88d3790..e32d0db030a 100644 --- a/packages/rs-dapi/src/services/platform_service/broadcast_state_transition.rs +++ b/packages/rs-dapi/src/services/platform_service/broadcast_state_transition.rs @@ -15,9 +15,14 @@ use crate::services::platform_service::shielded_proof_failure_budget::{ INVALID_SHIELDED_PROOF_CODE, SourceKey, orchard_action_count, }; use base64::prelude::*; -use dapi_grpc::platform::v0::{BroadcastStateTransitionRequest, BroadcastStateTransitionResponse}; +use dapi_grpc::platform::v0::get_status_request::{self, GetStatusRequestV0}; +use dapi_grpc::platform::v0::get_status_response; +use dapi_grpc::platform::v0::{ + BroadcastStateTransitionRequest, BroadcastStateTransitionResponse, GetStatusRequest, + GetStatusResponse, +}; use dpp::state_transition::validate_state_transition_size; -use dpp::version::PlatformVersion; +use dpp::version::{PLATFORM_VERSIONS, PlatformVersion}; use sha2::{Digest, Sha256}; use std::time::Duration; use tonic::Request; @@ -51,7 +56,8 @@ impl PlatformServiceImpl { state_transition: tx, } = request.into_inner(); - validate_state_transition_bytes(&tx)?; + validate_state_transition_not_empty(&tx)?; + validate_state_transition_bytes_size(&tx, self.size_limit_version().await)?; let txid = Sha256::digest(&tx).to_vec(); let txid_hex = hex::encode(&txid); @@ -256,7 +262,79 @@ impl PlatformServiceImpl { } } -fn validate_state_transition_bytes(tx: &[u8]) -> Result<(), DapiError> { +/// How long a broadcast waits for Drive's status before it checks the size +/// against [`MOST_PERMISSIVE_SIZE_VERSION`] instead. +const ACTIVE_VERSION_LOOKUP_TIMEOUT: Duration = Duration::from_secs(2); + +impl PlatformServiceImpl { + /// The protocol version whose size limit a broadcast must meet: the one + /// the network runs now, from the cached `getStatus`. When the status + /// does not answer in [`ACTIVE_VERSION_LOOKUP_TIMEOUT`], or names a + /// version this build does not know, the most permissive known version, + /// so DAPI never refuses what the network could accept. Drive enforces + /// the active version's limit when it decodes. + async fn size_limit_version(&self) -> &'static PlatformVersion { + let request = Request::new(GetStatusRequest { + version: Some(get_status_request::Version::V0(GetStatusRequestV0 {})), + }); + let status = match tokio::time::timeout( + ACTIVE_VERSION_LOOKUP_TIMEOUT, + self.get_status_impl(request), + ) + .await + { + Ok(Ok(response)) => Some(response.into_inner()), + Ok(Err(error)) => { + debug!(%error, "Status unavailable; checking the size against the largest known limit"); + None + } + Err(_) => { + debug!("Status timed out; checking the size against the largest known limit"); + None + } + }; + active_platform_version(status.as_ref()).unwrap_or(MOST_PERMISSIVE_SIZE_VERSION) + } +} + +/// The protocol version Drive reports as current in `status`, when this +/// build knows it. +fn active_platform_version(status: Option<&GetStatusResponse>) -> Option<&'static PlatformVersion> { + let get_status_response::Version::V0(status) = status?.version.as_ref()?; + let current = status + .version + .as_ref()? + .protocol + .as_ref()? + .drive + .as_ref()? + .current; + PlatformVersion::get(current).ok() +} + +/// The known protocol version with the largest `max_state_transition_size`. +const MOST_PERMISSIVE_SIZE_VERSION: &PlatformVersion = + &PLATFORM_VERSIONS[most_permissive_size_index(PLATFORM_VERSIONS)]; + +/// The index in `versions` of the first version with the largest +/// `max_state_transition_size`. +const fn most_permissive_size_index(versions: &[PlatformVersion]) -> usize { + let mut most_permissive = 0; + let mut index = 1; + while index < versions.len() { + if versions[index].system_limits.max_state_transition_size + > versions[most_permissive] + .system_limits + .max_state_transition_size + { + most_permissive = index; + } + index += 1; + } + most_permissive +} + +fn validate_state_transition_not_empty(tx: &[u8]) -> Result<(), DapiError> { if tx.is_empty() { debug!("State transition is empty"); return Err(DapiError::InvalidArgument( @@ -264,11 +342,14 @@ fn validate_state_transition_bytes(tx: &[u8]) -> Result<(), DapiError> { )); } - // `latest()` is a static upper bound, not the network's active version: - // every released version so far shares the same limit, and if a future - // version raises it, latest ≥ active keeps this a pure pre-filter — - // Drive still enforces the active version's limit authoritatively. - if let Err(e) = validate_state_transition_size(tx.len() as u64, PlatformVersion::latest()) { + Ok(()) +} + +fn validate_state_transition_bytes_size( + tx: &[u8], + platform_version: &PlatformVersion, +) -> Result<(), DapiError> { + if let Err(e) = validate_state_transition_size(tx.len() as u64, platform_version) { let max_size = e.max_size_bytes(); debug!(actual = tx.len(), max_size, "State transition is too large"); return Err(DapiError::InvalidArgument(format!( @@ -324,22 +405,87 @@ mod tests { use super::*; #[test] - fn state_transition_size_is_checked_before_broadcast_work() { - let max_size = PlatformVersion::latest() - .system_limits - .max_state_transition_size as usize; + fn should_pick_the_version_with_the_largest_size_limit() { + let with_limit = |limit| { + let mut version = PlatformVersion::latest().clone(); + version.system_limits.max_state_transition_size = limit; + version + }; - assert!(validate_state_transition_bytes(&vec![1; max_size]).is_ok()); + assert_eq!(most_permissive_size_index(&[with_limit(100)]), 0); + assert_eq!( + most_permissive_size_index(&[with_limit(100), with_limit(300), with_limit(200)]), + 1 + ); + assert_eq!( + most_permissive_size_index(&[with_limit(300), with_limit(100), with_limit(300)]), + 0 + ); + } + + #[test] + fn should_check_the_size_against_the_given_version_limit() { + let mut version = PlatformVersion::latest().clone(); + version.system_limits.max_state_transition_size = 100; + + assert!(validate_state_transition_bytes_size(&[1; 100], &version).is_ok()); assert!(matches!( - validate_state_transition_bytes(&vec![1; max_size + 1]), - Err(DapiError::InvalidArgument(message)) if message.contains("maximum size") + validate_state_transition_bytes_size(&[1; 101], &version), + Err(DapiError::InvalidArgument(message)) if message.contains("maximum size of 100 bytes") )); + } + + #[test] + fn should_refuse_an_empty_state_transition() { + assert!(validate_state_transition_not_empty(&[1]).is_ok()); assert!(matches!( - validate_state_transition_bytes(&[]), + validate_state_transition_not_empty(&[]), Err(DapiError::InvalidArgument(message)) if message.contains("not specified") )); } + fn status_with_current_version(current: u32) -> GetStatusResponse { + use get_status_response::GetStatusResponseV0; + use get_status_response::get_status_response_v0::{ + Version, version::Protocol, version::protocol::Drive, + }; + + GetStatusResponse { + version: Some(get_status_response::Version::V0(GetStatusResponseV0 { + version: Some(Version { + protocol: Some(Protocol { + drive: Some(Drive { + current, + ..Default::default() + }), + ..Default::default() + }), + ..Default::default() + }), + ..Default::default() + })), + } + } + + #[test] + fn should_take_the_current_drive_version_from_the_status() { + let first = PLATFORM_VERSIONS[0].protocol_version; + let status = status_with_current_version(first); + + assert_eq!( + active_platform_version(Some(&status)).map(|version| version.protocol_version), + Some(first) + ); + } + + #[test] + fn should_have_no_active_version_without_a_known_current_version() { + assert!(active_platform_version(None).is_none()); + assert!(active_platform_version(Some(&GetStatusResponse::default())).is_none()); + assert!(active_platform_version(Some(&status_with_current_version(0))).is_none()); + assert!(active_platform_version(Some(&status_with_current_version(u32::MAX))).is_none()); + } + #[test] fn should_refuse_a_spent_budget_as_a_rate_limit_with_whole_seconds() { let status = proof_failure_budget_refusal(Duration::from_millis(2_500)).to_status(); diff --git a/packages/rs-dapi/src/services/platform_service/shielded_proof_failure_budget.rs b/packages/rs-dapi/src/services/platform_service/shielded_proof_failure_budget.rs index 35928262747..3098cf8c1f0 100644 --- a/packages/rs-dapi/src/services/platform_service/shielded_proof_failure_budget.rs +++ b/packages/rs-dapi/src/services/platform_service/shielded_proof_failure_budget.rs @@ -38,7 +38,7 @@ use dpp::state_transition::token_shielded_transfer_with_shielded_fee_transition: use dpp::state_transition::token_unshield_with_shielded_fee_transition::TokenUnshieldWithShieldedFeeTransition; use dpp::state_transition::unshield_transition::UnshieldTransition; use dpp::tokens::token_payment_info::v1::v1_accessors::TokenPaymentInfoAccessorsV1; -use dpp::version::PlatformVersion; +use dpp::version::{PLATFORM_VERSIONS, PlatformVersion}; use tonic::Request; /// Consensus code of `InvalidShieldedProofError`, the CheckTx answer for a @@ -54,6 +54,11 @@ const FAILED_ACTIONS_ALLOWED: u32 = 4; /// two actions verified a minute. const ACTION_DRAIN_INTERVAL: Duration = Duration::from_secs(30); +/// The most Orchard actions one bundle can carry under any protocol version +/// this build knows: the largest bundle a source can have in flight. +const MAX_SHIELDED_ACTIONS: u16 = largest_action_limit(PLATFORM_VERSIONS); +const _: () = assert!(MAX_SHIELDED_ACTIONS > 0, "a zero bound would meter nothing"); + /// Sources tracked at once. When full, a source with nothing in flight and the /// least debt makes room. const MAX_TRACKED_SOURCES: usize = 16_384; @@ -131,42 +136,44 @@ pub(super) fn orchard_action_count(state_transition_bytes: &[u8]) -> usize { return 0; }; match state_transition { - StateTransition::Shield(ShieldTransition::V0(v0)) => v0.actions.len(), - StateTransition::ShieldedTransfer(ShieldedTransferTransition::V0(v0)) => v0.actions.len(), - StateTransition::Unshield(UnshieldTransition::V0(v0)) => v0.actions.len(), + StateTransition::Shield(ShieldTransition::V0(v0)) => bundle(v0.actions.len()), + StateTransition::ShieldedTransfer(ShieldedTransferTransition::V0(v0)) => { + bundle(v0.actions.len()) + } + StateTransition::Unshield(UnshieldTransition::V0(v0)) => bundle(v0.actions.len()), StateTransition::ShieldFromAssetLock(ShieldFromAssetLockTransition::V0(v0)) => { - v0.actions.len() + bundle(v0.actions.len()) } StateTransition::ShieldFromAssetLock(ShieldFromAssetLockTransition::V1(v1)) => { - v1.actions.len() + bundle(v1.actions.len()) } StateTransition::ShieldedWithdrawal(ShieldedWithdrawalTransition::V0(v0)) => { - v0.actions.len() + bundle(v0.actions.len()) } StateTransition::IdentityCreateFromShieldedPool( IdentityCreateFromShieldedPoolTransition::V0(v0), - ) => v0.actions.len(), + ) => bundle(v0.actions.len()), StateTransition::ShieldFromIdentity(ShieldFromIdentityTransition::V0(v0)) => { - v0.actions.len() + bundle(v0.actions.len()) } StateTransition::IdentityTopUpFromShieldedPool( IdentityTopUpFromShieldedPoolTransition::V0(v0), - ) => v0.actions.len(), + ) => bundle(v0.actions.len()), StateTransition::PrivateDocumentCreateFromShieldedPool( PrivateDocumentCreateFromShieldedPoolTransition::V0(v0), - ) => v0.actions.len(), + ) => bundle(v0.actions.len()), // A token pool transition carries two bundles, one against the token's // pool and one paying the fee from the credit shielded pool; Drive // verifies both. StateTransition::TokenShieldedTransferWithShieldedFee( TokenShieldedTransferWithShieldedFeeTransition::V0(v0), - ) => v0.token_actions.len() + v0.fee_actions.len(), + ) => bundle(v0.token_actions.len()) + bundle(v0.fee_actions.len()), StateTransition::TokenUnshieldWithShieldedFee( TokenUnshieldWithShieldedFeeTransition::V0(v0), - ) => v0.token_actions.len() + v0.fee_actions.len(), + ) => bundle(v0.token_actions.len()) + bundle(v0.fee_actions.len()), StateTransition::TokenPurchaseFromShieldedPool( TokenPurchaseFromShieldedPoolTransition::V0(v0), - ) => v0.token_actions.len() + v0.fee_actions.len(), + ) => bundle(v0.token_actions.len()) + bundle(v0.fee_actions.len()), // A batch carries a bundle for each token transition against a token's // shielded pool and each document whose token price is paid from one. StateTransition::Batch(batch) => batch @@ -174,13 +181,13 @@ pub(super) fn orchard_action_count(state_transition_bytes: &[u8]) -> usize { .map(|transition| match transition { BatchedTransitionRef::Token(token_transition) => token_transition .shielded_pool_actions() - .map_or(0, |actions| actions.len()), + .map_or(0, |actions| bundle(actions.len())), BatchedTransitionRef::Document(document_transition) => document_transition .base() .token_payment_info_ref() .as_ref() .and_then(|info| info.shielded_payment()) - .map_or(0, |payment| payment.actions.len()), + .map_or(0, |payment| bundle(payment.actions.len())), }) .sum(), StateTransition::DataContractCreate(_) @@ -203,6 +210,13 @@ pub(super) fn orchard_action_count(state_transition_bytes: &[u8]) -> usize { } } +/// The actions Drive can verify for one bundle: it refuses a bundle over the +/// system limit before any proof work, so a larger one costs no more than the +/// largest valid one. Each bundle of a transition is bounded on its own. +fn bundle(actions: usize) -> usize { + actions.min(usize::from(MAX_SHIELDED_ACTIONS)) +} + #[derive(Debug)] struct SourceBudget { /// When this source's failed actions have fully drained. @@ -220,7 +234,8 @@ impl SourceBudget { /// Per source, failed Orchard actions still draining and the actions of /// broadcasts still waiting for Drive. A source with no failures may have one /// largest bundle's worth in flight, which is all the concurrency an honest -/// sender needs. A source that failed recently gets one broadcast at a time, +/// sender needs, or one broadcast of any size whose bundles together carry +/// more. A source that failed recently gets one broadcast at a time, /// and only while its failures plus that broadcast stay within the allowance, /// so it cannot burst past the drain rate. pub struct ShieldedProofFailureBudget { @@ -233,21 +248,38 @@ pub struct ShieldedProofFailureBudget { impl Default for ShieldedProofFailureBudget { fn default() -> Self { - // `latest()` is a static upper bound, as for the size pre-filter: - // Drive refuses a bundle over the active limit before any proof work. - let max_actions = PlatformVersion::latest() - .system_limits - .max_shielded_transition_actions; Self { sources: Mutex::new(HashMap::new()), failed_actions_allowed: FAILED_ACTIONS_ALLOWED, - max_in_flight_actions: u32::from(max_actions), + max_in_flight_actions: u32::from(MAX_SHIELDED_ACTIONS), drain_interval: ACTION_DRAIN_INTERVAL, max_sources: MAX_TRACKED_SOURCES, } } } +/// The largest `max_shielded_transition_actions` in `versions`. +/// +/// DAPI does not track which version the network runs, so the budget clamps a +/// bundle to the most actions any version this build knows admits: a smaller +/// bound would charge a bundle the active version still verifies for fewer +/// actions than Drive checks. Drive refuses a bundle over the active limit +/// before any proof work. +const fn largest_action_limit(versions: &[PlatformVersion]) -> u16 { + let mut largest = 0; + let mut index = 0; + while index < versions.len() { + let limit = versions[index] + .system_limits + .max_shielded_transition_actions; + if limit > largest { + largest = limit; + } + index += 1; + } + largest +} + impl ShieldedProofFailureBudget { /// Reserve `actions` from `source`'s budget for one broadcast. `Ok(None)` /// for a broadcast without Orchard actions; `Err` carries how long the @@ -269,11 +301,7 @@ impl ShieldedProofFailureBudget { if actions == 0 { return Ok(None); } - // Drive refuses more actions than the system limit before any proof - // work, so a larger bundle costs no more than the largest valid one. - let actions = u32::try_from(actions) - .unwrap_or(u32::MAX) - .min(self.max_in_flight_actions); + let actions = u32::try_from(actions).unwrap_or(u32::MAX); let mut sources = self .sources @@ -283,7 +311,9 @@ impl ShieldedProofFailureBudget { if let Some(entry) = sources.get(&source) { let debt = entry.debt(now); if debt.is_zero() { - if entry.in_flight + actions > self.max_in_flight_actions { + if entry.in_flight != 0 + && entry.in_flight.saturating_add(actions) > self.max_in_flight_actions + { return Err(Duration::from_secs(1)); } } else { @@ -552,7 +582,18 @@ mod tests { fn should_charge_an_oversized_bundle_as_the_largest_valid_one() { let budget = budget(); let now = Instant::now(); - broadcast(&budget, SOURCE, 1_000, true, now).expect("a clean source is admitted"); + let oversized = StateTransition::ShieldedTransfer(ShieldedTransferTransition::V0( + ShieldedTransferTransitionV0 { + actions: actions(usize::from(MAX_SHIELDED_ACTIONS) + 1), + value_balance: 1, + anchor: [7; 32], + proof: vec![0; 100], + binding_signature: [0; 64], + }, + )); + let count = orchard_action_count(&oversized.serialize_to_bytes().unwrap()); + assert_eq!(count, 16); + broadcast(&budget, SOURCE, count, true, now).expect("a clean source is admitted"); assert_eq!( broadcast(&budget, SOURCE, 2, true, now), Err(ACTION_DRAIN_INTERVAL * 14), @@ -560,6 +601,69 @@ mod tests { ); } + #[test] + fn should_clamp_to_the_largest_action_limit_of_any_known_version() { + let with_limit = |limit| { + let mut version = PlatformVersion::latest().clone(); + version.system_limits.max_shielded_transition_actions = limit; + version + }; + + assert_eq!(largest_action_limit(&[with_limit(16)]), 16); + assert_eq!( + largest_action_limit(&[with_limit(32), with_limit(16)]), + 32, + "an older version that admits more actions still bounds the charge" + ); + assert_eq!( + largest_action_limit(&[with_limit(8), with_limit(24), with_limit(16)]), + 24 + ); + } + + /// Drive bounds each bundle of a token pool transition on its own, so two + /// full bundles are charged twice the largest bundle, even to a source + /// that already has a settled broadcast behind it. + #[test] + fn should_charge_each_bundle_of_a_token_transition_up_to_the_limit() { + let largest = usize::from(MAX_SHIELDED_ACTIONS); + let transition = StateTransition::TokenShieldedTransferWithShieldedFee( + TokenShieldedTransferWithShieldedFeeTransition::V0( + TokenShieldedTransferWithShieldedFeeTransitionV0 { + data_contract_id: Identifier::from([9; 32]), + token_contract_position: 0, + token_id: Identifier::from([8; 32]), + token_actions: actions(largest + 1), + token_anchor: [7; 32], + token_proof: vec![0; 100], + token_binding_signature: [0; 64], + fee_actions: actions(largest + 1), + fee_anchor: [7; 32], + fee_proof: vec![0; 100], + fee_binding_signature: [0; 64], + credit_amount: 1, + }, + ), + ); + let count = orchard_action_count(&transition.serialize_to_bytes().unwrap()); + assert_eq!(count, 2 * largest); + + let budget = budget(); + let now = Instant::now(); + broadcast(&budget, SOURCE, 1, false, now).expect("a clean source is admitted"); + let reservation = budget + .try_reserve_at(SOURCE, count, now) + .expect("one broadcast fits when nothing else is in flight") + .unwrap(); + assert_eq!(reservation.actions as usize, 2 * largest); + reservation.settle_at(true, now); + assert_eq!( + broadcast(&budget, SOURCE, 1, false, now), + Err(ACTION_DRAIN_INTERVAL * (2 * largest as u32 - FAILED_ACTIONS_ALLOWED + 1)), + "both bundles' failed actions drain before the next broadcast" + ); + } + #[test] fn should_evict_the_least_indebted_idle_source_when_full() { let mut budget = budget();