-
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
ef8a66f
commit 36e8c06
Showing
6 changed files
with
172 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,77 @@ | ||
use starknet_api::external_transaction::ExternalTransaction; | ||
use starknet_api::external_transaction::{ | ||
ExternalDeclareTransaction, ExternalDeployAccountTransaction, ExternalInvokeTransaction, | ||
ExternalTransaction, | ||
}; | ||
use starknet_api::transaction::Resource; | ||
|
||
use crate::errors::TransactionValidatorResult; | ||
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, | ||
}, | ||
}; | ||
|
||
fn validate_reousrce_bounds( | ||
resource_bounds_mapping: &starknet_api::transaction::ResourceBoundsMapping, | ||
resource: Resource, | ||
) -> TransactionValidatorResult<()> { | ||
if let Some(resource_bounds) = resource_bounds_mapping.0.get(&resource) { | ||
if resource_bounds.max_amount == 0 || resource_bounds.max_price_per_unit == 0 { | ||
return Err(TransactionValidatorError::ZeroFee { | ||
resource, | ||
resource_bounds: *resource_bounds, | ||
}); | ||
} | ||
} else { | ||
return Err(TransactionValidatorError::MissingResource { resource }); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
if self.config.validate_non_zero_l1_gas_fee { | ||
validate_reousrce_bounds(resource_bounds_mapping, Resource::L1Gas)?; | ||
} | ||
if self.config.validate_non_zero_l2_gas_fee { | ||
validate_reousrce_bounds(resource_bounds_mapping, Resource::L2Gas)?; | ||
} | ||
|
||
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