-
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: add stateless validator skelaton
- Loading branch information
1 parent
2e9a1d0
commit 2aaeabd
Showing
6 changed files
with
147 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pub mod errors; | ||
pub mod gateway; | ||
pub mod starknet_api_utils; | ||
pub mod transaction_validator; |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use starknet_api::external_transaction::{ | ||
DataAvailabilityMode, ExternalDeclareTransaction, ExternalDeclareTransactionV3, | ||
ExternalDeployAccountTransaction, ExternalDeployAccountTransactionV3, | ||
ExternalInvokeTransaction, ExternalInvokeTransactionV3, ExternalTransaction, | ||
}; | ||
use starknet_api::transaction::{ResourceBounds, ResourceBoundsMapping, TransactionVersion}; | ||
|
||
// Utils. | ||
pub fn create_external_declare_tx_for_testing() -> ExternalTransaction { | ||
ExternalTransaction::Declare(ExternalDeclareTransaction::V3( | ||
ExternalDeclareTransactionV3 { | ||
resource_bounds: zero_resource_bounds_mapping(), | ||
contract_class: Default::default(), | ||
tip: Default::default(), | ||
signature: Default::default(), | ||
nonce: Default::default(), | ||
compiled_class_hash: Default::default(), | ||
sender_address: Default::default(), | ||
nonce_data_availability_mode: DataAvailabilityMode::L1, | ||
fee_data_availability_mode: DataAvailabilityMode::L1, | ||
paymaster_data: Default::default(), | ||
account_deployment_data: Default::default(), | ||
version: TransactionVersion::THREE, | ||
}, | ||
)) | ||
} | ||
|
||
pub fn create_external_deploy_account_tx_for_testing() -> ExternalTransaction { | ||
ExternalTransaction::DeployAccount(ExternalDeployAccountTransaction::V3( | ||
ExternalDeployAccountTransactionV3 { | ||
resource_bounds: zero_resource_bounds_mapping(), | ||
tip: Default::default(), | ||
contract_address_salt: Default::default(), | ||
class_hash: Default::default(), | ||
constructor_calldata: Default::default(), | ||
nonce: Default::default(), | ||
signature: Default::default(), | ||
nonce_data_availability_mode: DataAvailabilityMode::L1, | ||
fee_data_availability_mode: DataAvailabilityMode::L1, | ||
paymaster_data: Default::default(), | ||
version: TransactionVersion::THREE, | ||
}, | ||
)) | ||
} | ||
|
||
pub fn create_external_invoke_tx_for_testing() -> ExternalTransaction { | ||
ExternalTransaction::Invoke(ExternalInvokeTransaction::V3(ExternalInvokeTransactionV3 { | ||
resource_bounds: zero_resource_bounds_mapping(), | ||
tip: Default::default(), | ||
signature: Default::default(), | ||
nonce: Default::default(), | ||
sender_address: Default::default(), | ||
calldata: Default::default(), | ||
nonce_data_availability_mode: DataAvailabilityMode::L1, | ||
fee_data_availability_mode: DataAvailabilityMode::L1, | ||
paymaster_data: Default::default(), | ||
account_deployment_data: Default::default(), | ||
version: TransactionVersion::THREE, | ||
})) | ||
} | ||
|
||
pub fn zero_resource_bounds_mapping() -> ResourceBoundsMapping { | ||
ResourceBoundsMapping::try_from(vec![ | ||
( | ||
starknet_api::transaction::Resource::L1Gas, | ||
ResourceBounds::default(), | ||
), | ||
( | ||
starknet_api::transaction::Resource::L2Gas, | ||
ResourceBounds::default(), | ||
), | ||
]) | ||
.expect("Resource bounds mapping has unexpected structure.") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use starknet_api::external_transaction::ExternalTransaction; | ||
|
||
use crate::errors::TransactionValidatorResult; | ||
|
||
#[cfg(test)] | ||
#[path = "transaction_validator_test.rs"] | ||
mod transaction_validator_test; | ||
|
||
pub struct TransactionValidatorConfig {} | ||
|
||
pub struct TransactionValidator { | ||
pub config: TransactionValidatorConfig, | ||
} | ||
|
||
impl TransactionValidator { | ||
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. | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use rstest::rstest; | ||
|
||
use starknet_api::external_transaction::ExternalTransaction; | ||
|
||
use crate::starknet_api_utils::{ | ||
create_external_declare_tx_for_testing, create_external_deploy_account_tx_for_testing, | ||
create_external_invoke_tx_for_testing, | ||
}; | ||
use crate::transaction_validator::{ | ||
TransactionValidator, TransactionValidatorConfig, TransactionValidatorResult, | ||
}; | ||
|
||
const VALIDATOR_CONFIG_FOR_TESTING: TransactionValidatorConfig = TransactionValidatorConfig {}; | ||
|
||
#[rstest] | ||
#[case::valid_declare_tx( | ||
VALIDATOR_CONFIG_FOR_TESTING, | ||
create_external_declare_tx_for_testing(), | ||
Ok(()) | ||
)] | ||
#[case::valid_deploy_account_tx( | ||
VALIDATOR_CONFIG_FOR_TESTING, | ||
create_external_deploy_account_tx_for_testing(), | ||
Ok(()) | ||
)] | ||
#[case::valid_invoke_tx( | ||
VALIDATOR_CONFIG_FOR_TESTING, | ||
create_external_invoke_tx_for_testing(), | ||
Ok(()) | ||
)] | ||
fn test_transaction_validator( | ||
#[case] config: TransactionValidatorConfig, | ||
#[case] tx: ExternalTransaction, | ||
#[case] expected_result: TransactionValidatorResult<()>, | ||
) { | ||
let tx_validator = TransactionValidator { config }; | ||
let result = tx_validator.validate(tx); | ||
|
||
assert_eq!(result, expected_result); | ||
} |