forked from tari-project/tari-dan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
286 changed files
with
5,359 additions
and
15,381 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
File renamed without changes.
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
101 changes: 101 additions & 0 deletions
101
applications/tari_dan_app_utilities/src/transaction_executor.rs
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,101 @@ | ||
// Copyright 2023 The Tari Project | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
use std::sync::Arc; | ||
|
||
use tari_common_types::types::PublicKey; | ||
use tari_crypto::tari_utilities::ByteArray; | ||
use tari_dan_common_types::services::template_provider::TemplateProvider; | ||
use tari_dan_engine::{ | ||
fees::{FeeModule, FeeTable}, | ||
packager::LoadedTemplate, | ||
runtime::{AuthParams, ConsensusContext, RuntimeModule}, | ||
state_store::{memory::MemoryStateStore, StateStoreError}, | ||
transaction::{TransactionError, TransactionProcessor}, | ||
}; | ||
use tari_dan_storage::consensus_models::ExecutedTransaction; | ||
use tari_engine_types::commit_result::{ExecuteResult, FinalizeResult, RejectReason}; | ||
use tari_template_lib::{crypto::RistrettoPublicKeyBytes, prelude::NonFungibleAddress}; | ||
use tari_transaction::Transaction; | ||
|
||
pub trait TransactionExecutor { | ||
type Error: Send + Sync + 'static; | ||
|
||
fn execute( | ||
&self, | ||
transaction: Transaction, | ||
state_store: MemoryStateStore, | ||
consensus_context: ConsensusContext, | ||
) -> Result<ExecutedTransaction, Self::Error>; | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct TariDanTransactionProcessor<TTemplateProvider> { | ||
template_provider: Arc<TTemplateProvider>, | ||
fee_table: FeeTable, | ||
} | ||
|
||
impl<TTemplateProvider> TariDanTransactionProcessor<TTemplateProvider> { | ||
pub fn new(template_provider: TTemplateProvider, fee_table: FeeTable) -> Self { | ||
Self { | ||
template_provider: Arc::new(template_provider), | ||
fee_table, | ||
} | ||
} | ||
} | ||
|
||
impl<TTemplateProvider> TransactionExecutor for TariDanTransactionProcessor<TTemplateProvider> | ||
where TTemplateProvider: TemplateProvider<Template = LoadedTemplate> | ||
{ | ||
type Error = TransactionProcessorError; | ||
|
||
fn execute( | ||
&self, | ||
transaction: Transaction, | ||
state_store: MemoryStateStore, | ||
consensus_context: ConsensusContext, | ||
) -> Result<ExecutedTransaction, Self::Error> { | ||
// Include ownership token for the signers of this in the auth scope | ||
let owner_token = get_auth_token(transaction.signer_public_key()); | ||
let auth_params = AuthParams { | ||
initial_ownership_proofs: vec![owner_token], | ||
}; | ||
|
||
let initial_cost = 0; | ||
let modules: Vec<Arc<dyn RuntimeModule<TTemplateProvider>>> = | ||
vec![Arc::new(FeeModule::new(initial_cost, self.fee_table.clone()))]; | ||
|
||
let processor = TransactionProcessor::new( | ||
self.template_provider.clone(), | ||
state_store, | ||
auth_params, | ||
consensus_context, | ||
modules, | ||
); | ||
let tx_id = transaction.hash(); | ||
let result = match processor.execute(transaction.clone()) { | ||
Ok(result) => result, | ||
Err(err) => ExecuteResult { | ||
finalize: FinalizeResult::reject(tx_id, RejectReason::ExecutionFailure(err.to_string())), | ||
transaction_failure: None, | ||
fee_receipt: None, | ||
}, | ||
}; | ||
|
||
Ok(ExecutedTransaction::new(transaction, result)) | ||
} | ||
} | ||
|
||
fn get_auth_token(public_key: &PublicKey) -> NonFungibleAddress { | ||
let public_key = | ||
RistrettoPublicKeyBytes::from_bytes(public_key.as_bytes()).expect("Expected public key to be 32 bytes"); | ||
NonFungibleAddress::from_public_key(public_key) | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum TransactionProcessorError { | ||
#[error(transparent)] | ||
TransactionError(#[from] TransactionError), | ||
#[error(transparent)] | ||
StateStoreError(#[from] StateStoreError), | ||
} |
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
Oops, something went wrong.