Skip to content

Commit

Permalink
Tidying up (starkware-libs#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
giladchase authored Jan 22, 2023
1 parent 6b5a9e9 commit 27fe165
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 86 deletions.
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/state/state_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait StateReader {
}

/// A class defining the API for writing to StarkNet global state.
///
/// Reader functionality should be delegated to the associated type; which is passed in by
/// dependency-injection.
pub trait State {
Expand All @@ -57,8 +57,6 @@ pub trait State {

fn get_class_hash_at(&mut self, contract_address: ContractAddress) -> StateResult<&ClassHash>;

fn get_contract_class(&mut self, class_hash: &ClassHash) -> StateResult<&ContractClass>;

/// Allocates the given address to the given class hash.
/// Raises an exception if the address is already assigned;
/// meaning: this is a write once action.
Expand All @@ -67,4 +65,6 @@ pub trait State {
contract_address: ContractAddress,
class_hash: ClassHash,
) -> StateResult<()>;

fn get_contract_class(&mut self, class_hash: &ClassHash) -> StateResult<&ContractClass>;
}
137 changes: 68 additions & 69 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,49 @@ pub enum AccountTransaction {
}

impl AccountTransaction {
pub fn execute(
self,
state: &mut dyn State,
block_context: &BlockContext,
) -> TransactionExecutionResult<TransactionExecutionInfo> {
let account_tx_context = self.get_account_transaction_context();
Self::verify_tx_version(account_tx_context.version)?;
Self::handle_nonce(&account_tx_context, state)?;

// Handle transaction-type specific execution.
let validate_call_info;
let execute_call_info;
match self {
Self::Invoke(tx) => {
// Validate invoke transaction.
validate_call_info = Self::validate_tx(
state,
block_context,
&account_tx_context,
selector_from_name(VALIDATE_ENTRY_POINT_NAME),
// The validation calldata for invoke transaction is the same calldata as for
// the execution itself.
Calldata(Arc::clone(&tx.calldata.0)),
)?;

// Execute invoke transaction.
execute_call_info = tx.execute_tx(state, block_context, &account_tx_context)?;
}
};

// Charge fee.
let actual_resources = ResourcesMapping::default();
let (actual_fee, fee_transfer_call_info) =
Self::charge_fee(state, block_context, &account_tx_context)?;

Ok(TransactionExecutionInfo {
validate_call_info,
execute_call_info: Some(execute_call_info),
fee_transfer_call_info,
actual_fee,
actual_resources,
})
}
fn get_account_transaction_context(&self) -> AccountTransactionContext {
match self {
Self::Invoke(tx) => AccountTransactionContext {
Expand All @@ -39,6 +82,19 @@ impl AccountTransaction {
}
}

fn verify_tx_version(version: TransactionVersion) -> TransactionExecutionResult<()> {
static ALLOWED_VERSIONS: Lazy<Vec<TransactionVersion>> =
Lazy::new(|| vec![TransactionVersion(StarkFelt::from(1))]);
if ALLOWED_VERSIONS.contains(&version) {
Ok(())
} else {
Err(TransactionExecutionError::InvalidVersion {
version,
allowed_versions: &ALLOWED_VERSIONS,
})
}
}

fn handle_nonce(
account_tx_context: &AccountTransactionContext,
state: &mut dyn State,
Expand All @@ -55,19 +111,6 @@ impl AccountTransaction {
Ok(state.increment_nonce(account_tx_context.sender_address)?)
}

fn verify_tx_version(version: TransactionVersion) -> TransactionExecutionResult<()> {
static ALLOWED_VERSIONS: Lazy<Vec<TransactionVersion>> =
Lazy::new(|| vec![TransactionVersion(StarkFelt::from(1))]);
if ALLOWED_VERSIONS.contains(&version) {
Ok(())
} else {
Err(TransactionExecutionError::InvalidVersion {
version,
allowed_versions: &ALLOWED_VERSIONS,
})
}
}

fn validate_tx(
state: &mut dyn State,
block_context: &BlockContext,
Expand All @@ -87,6 +130,18 @@ impl AccountTransaction {
Ok(validate_call.execute(state, block_context, account_tx_context)?)
}

fn charge_fee(
state: &mut dyn State,
block_context: &BlockContext,
account_tx_context: &AccountTransactionContext,
) -> TransactionExecutionResult<(Fee, CallInfo)> {
let actual_fee = calculate_tx_fee(block_context);
let fee_transfer_call_info =
Self::execute_fee_transfer(state, block_context, account_tx_context, actual_fee)?;

Ok((actual_fee, fee_transfer_call_info))
}

fn execute_fee_transfer(
state: &mut dyn State,
block_context: &BlockContext,
Expand Down Expand Up @@ -118,60 +173,4 @@ impl AccountTransaction {

Ok(fee_transfer_call.execute(state, block_context, account_tx_context)?)
}

fn charge_fee(
state: &mut dyn State,
block_context: &BlockContext,
account_tx_context: &AccountTransactionContext,
) -> TransactionExecutionResult<(Fee, CallInfo)> {
let actual_fee = calculate_tx_fee(block_context);
let fee_transfer_call_info =
Self::execute_fee_transfer(state, block_context, account_tx_context, actual_fee)?;

Ok((actual_fee, fee_transfer_call_info))
}

pub fn execute(
self,
state: &mut dyn State,
block_context: &BlockContext,
) -> TransactionExecutionResult<TransactionExecutionInfo> {
let account_tx_context = self.get_account_transaction_context();
Self::verify_tx_version(account_tx_context.version)?;
Self::handle_nonce(&account_tx_context, state)?;

// Handle transaction-type specific execution.
let validate_call_info;
let execute_call_info;
match self {
Self::Invoke(tx) => {
// Validate invoke transaction.
validate_call_info = Self::validate_tx(
state,
block_context,
&account_tx_context,
selector_from_name(VALIDATE_ENTRY_POINT_NAME),
// The validation calldata for invoke transaction is the same calldata as for
// the execution itself.
Calldata(Arc::clone(&tx.calldata.0)),
)?;

// Execute invoke transaction.
execute_call_info = tx.execute_tx(state, block_context, &account_tx_context)?;
}
};

// Charge fee.
let actual_resources = ResourcesMapping::default();
let (actual_fee, fee_transfer_call_info) =
Self::charge_fee(state, block_context, &account_tx_context)?;

Ok(TransactionExecutionInfo {
validate_call_info,
execute_call_info: Some(execute_call_info),
fee_transfer_call_info,
actual_fee,
actual_resources,
})
}
}
13 changes: 0 additions & 13 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,3 @@ group_imports = "StdExternalCrate"
normalize_comments = true
normalize_doc_attributes = true
wrap_comments = true

# To use these settings in vscode, add the following line to your settings:
# "rust-analyzer.rustfmt.overrideCommand": [
# "rustup",
# "run",
# "nightly-2022-07-27",
# "--",
# "rustfmt",
# "--edition",
# "2018",
# "--"
# ]
# and run "rustup toolchain install nightly-2022-07-27".

0 comments on commit 27fe165

Please sign in to comment.