Skip to content

Commit

Permalink
feat: add stateless validator skelaton
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Apr 4, 2024
1 parent 2e9a1d0 commit 2aaeabd
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ workspace = true
assert_matches.workspace = true
axum.workspace = true
hyper.workspace = true
rstest.workspace = true
serde.workspace = true
serde_json.workspace = true
starknet_api.workspace = true
Expand Down
6 changes: 6 additions & 0 deletions crates/gateway/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ pub enum GatewayConfigError {
#[error("Server address is not an bind IP address: {0}")]
InvalidServerBindAddress(String),
}

#[derive(Debug, Error)]
#[cfg_attr(test, derive(PartialEq))]
pub enum TransactionValidatorError {}

pub type TransactionValidatorResult<T> = Result<T, TransactionValidatorError>;
2 changes: 2 additions & 0 deletions crates/gateway/src/lib.rs
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;
74 changes: 74 additions & 0 deletions crates/gateway/src/starknet_api_utils.rs
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.")
}
24 changes: 24 additions & 0 deletions crates/gateway/src/transaction_validator.rs
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(())
}
}
40 changes: 40 additions & 0 deletions crates/gateway/src/transaction_validator_test.rs
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);
}

0 comments on commit 2aaeabd

Please sign in to comment.