MCPcopy Create free account
hub / github.com/ddimaria/rust-blockchain-tutorial / process_transaction

Method process_transaction

chain/src/blockchain.rs:208–265  ·  view source on GitHub ↗

TODO(ddimaria): handle TransactionKind::ContractExecution

(
        &mut self,
        transaction: &'a mut Transaction,
    )

Source from the content-addressed store, hash-verified

206
207 // TODO(ddimaria): handle TransactionKind::ContractExecution
208 pub(crate) fn process_transaction<'a>(
209 &mut self,
210 transaction: &'a mut Transaction,
211 ) -> Result<(&'a mut Transaction, TransactionReceipt)> {
212 let mut contract_address: Option<Account> = None;
213 let transaction_hash = transaction.transaction_hash()?;
214
215 // ignore transactions without a nonce
216 if let Some(nonce) = transaction.nonce {
217 tracing::info!("Processing Transaction {:?}", transaction_hash);
218
219 // create the `to` account if it doesn't exist
220 if let Some(to) = transaction.to {
221 self.accounts.add_empty_account(&to)?;
222 }
223
224 // TODO(ddimaria): remove this copy
225 let kind = transaction.to_owned().kind()?;
226
227 match kind {
228 TransactionKind::Regular(from, to, value) => {
229 self.accounts.transfer(&from, &to, value)
230 }
231 TransactionKind::ContractDeployment(from, data) => {
232 contract_address = self.accounts.add_contract_account(&from, data).ok();
233 Ok(())
234 }
235 TransactionKind::ContractExecution(_from, to, data) => {
236 let code = self
237 .accounts
238 .get_account(&to)?
239 .code_hash
240 .ok_or_else(|| ChainError::NotAContractAccount(to.to_string()))?;
241 let (function, params): (&str, Vec<&str>) = bincode::deserialize(&data)?;
242
243 // call the function in the contract
244 runtime::contract::call_function(&code, function, &params)
245 .map_err(|e| ChainError::RuntimeError(to.to_string(), e.to_string()))
246 }
247 }?;
248
249 // update the nonce
250 self.accounts.update_nonce(&transaction.from, nonce)?;
251
252 let transaction_receipt = TransactionReceipt {
253 block_hash: None,
254 block_number: None,
255 contract_address,
256 transaction_hash,
257 };
258
259 return Ok((transaction, transaction_receipt));
260 }
261
262 Err(ChainError::MissingTransactionNonce(
263 transaction_hash.to_string(),
264 ))
265 }

Callers 1

process_transactionsMethod · 0.80

Calls 10

deserializeFunction · 0.85
call_functionFunction · 0.85
RuntimeErrorEnum · 0.85
transaction_hashMethod · 0.80
add_empty_accountMethod · 0.80
kindMethod · 0.80
add_contract_accountMethod · 0.80
get_accountMethod · 0.80
update_nonceMethod · 0.80
transferMethod · 0.45

Tested by

no test coverage detected