-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: stateless validator add non zero fee check
- Loading branch information
1 parent
9e36a57
commit f38447a
Showing
6 changed files
with
175 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,76 @@ | ||
use starknet_api::external_transaction::ExternalTransaction; | ||
use strum::IntoEnumIterator; | ||
|
||
use crate::errors::TransactionValidatorResult; | ||
use starknet_api::external_transaction::{ | ||
ExternalDeclareTransaction, ExternalDeployAccountTransaction, ExternalInvokeTransaction, | ||
ExternalTransaction, | ||
}; | ||
use starknet_api::transaction::Resource; | ||
|
||
use crate::errors::{TransactionValidatorError, TransactionValidatorResult}; | ||
|
||
#[cfg(test)] | ||
#[path = "transaction_validator_test.rs"] | ||
mod transaction_validator_test; | ||
|
||
pub struct TransactionValidatorConfig {} | ||
#[derive(Default)] | ||
pub struct TransactionValidatorConfig { | ||
// TODO(Arni, 1/5/2024): Consider squashing this config into a single field. Is it possible to | ||
// use more than one gas for fee in a single flow? | ||
// If true, validates that the reousrce bounds are not zero. | ||
pub validate_non_zero_l1_gas_fee: bool, | ||
pub validate_non_zero_l2_gas_fee: bool, | ||
} | ||
|
||
pub struct TransactionValidator { | ||
pub config: TransactionValidatorConfig, | ||
} | ||
|
||
impl TransactionValidator { | ||
pub fn validate(&self, _tx: ExternalTransaction) -> TransactionValidatorResult<()> { | ||
pub fn validate(&self, tx: ExternalTransaction) -> TransactionValidatorResult<()> { | ||
// TODO(Arni, 1/5/2024): Add a mechanism that validate the sender address is not blocked. | ||
// TODO(Arni, 1/5/2024): Validate transaction version. | ||
// TODO(Arni, 4/4/2024): Validate fee non zero. | ||
// TODO(Arni, 4/4/2024): Validate tx signature and calldata are not too long. | ||
|
||
self.validate_fee(&tx)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn validate_fee(&self, tx: &ExternalTransaction) -> TransactionValidatorResult<()> { | ||
let resource_bounds_mapping = match tx { | ||
ExternalTransaction::Declare(tx) => match tx { | ||
ExternalDeclareTransaction::V3(tx) => &tx.resource_bounds, | ||
}, | ||
ExternalTransaction::DeployAccount(tx) => match tx { | ||
ExternalDeployAccountTransaction::V3(tx) => &tx.resource_bounds, | ||
}, | ||
ExternalTransaction::Invoke(tx) => match tx { | ||
ExternalInvokeTransaction::V3(tx) => &tx.resource_bounds, | ||
}, | ||
}; | ||
|
||
for resource in Resource::iter() { | ||
if (resource == Resource::L1Gas && !self.config.validate_non_zero_l1_gas_fee) | ||
|| (resource == Resource::L2Gas && !self.config.validate_non_zero_l2_gas_fee) | ||
{ | ||
continue; | ||
} | ||
let resource_bounds = resource_bounds_mapping.0.get(&resource); | ||
match resource_bounds { | ||
None => { | ||
return Err(TransactionValidatorError::MissingResource { resource }); | ||
} | ||
Some(resource_bounds) => { | ||
if resource_bounds.max_amount == 0 || resource_bounds.max_price_per_unit == 0 { | ||
return Err(TransactionValidatorError::ZeroFee { | ||
resource, | ||
resource_bounds: *resource_bounds, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters