I opened this issue due to Ben's request. We should have a record of the fact it needs fixing and we collate comments over here.
This description is still approximate and not accurate, we need to define an approach and agree on checks.
Draft TransactionVerifier
I suggest adding a structure that will contain:
pub struct TransactionVerifier<'a, T: frame_system::Config> {
// Pointer to a tx that we have to check
tx: &'a TransactionFor<T>,
// All inputs, to avoid repeated search in the loop
all_inputs_map: BTreeMap<TokenId, TransactionOutputFor<T>>,
// All outputs, to avoid repeated search in the loop
all_outputs_map: BTreeMap<TokenId, TransactionOutputFor<T>>,
// Using TokenId, you can get the entire amount of this token in all inputs
total_value_of_input_tokens: BTreeMap<TokenId, TransactionOutputFor<T>>,
// Using TokenId, you can get the entire amount of this token in all outputs
total_value_of_output_tokens: BTreeMap<TokenId, TransactionOutputFor<T>>,
// A set of transaction verification functions, this approach will allow you to remove unnecessary cycles, which will speed up the function
set_of_checks: Vec<&'a mut FnMut(...)>,
// ...
// I may add a priority field to the set of checks. I'm still thinking here.
}
This struct we will use this way in the pallet utxo:
pub fn validate_transaction<T: Config>(
tx: &TransactionFor<T>,
) -> Result<ValidTransaction, &'static str> {
TransactionVerifier::<'_, T>::new(tx)
.checking_inputs()
.checking_outputs()
.checking_utxos_exists()
.checking_signatures()
.checking_tokens_transferring()
.checking_tokens_issued()
.checking_nft_mint()
.checking_assets_burn()
.calculating_reward()
.collect_result()?
}
When creating a new instance of this structure, we must initialize the fields.
Each subsequent check adds a new instance of the function to set_of_checks, which will be called in collect_result.
At the moment we can split the verification function for these parts:
Questions
- Do we need other checks?
- What is we need for checking Bitcoin Script?
- What is we need for checking contracts?
- If we can check an output address here, and add a possibility to find in the UtxoStore by any address format, then we can remove
fn pick_utxo and fn send_to_address. Isn't that?
I'm glad to see any suggestions or critics.
To continue the discussion from the PR #80
I opened this issue due to Ben's request. We should have a record of the fact it needs fixing and we collate comments over here.
This description is still approximate and not accurate, we need to define an approach and agree on checks.
Draft TransactionVerifier
I suggest adding a structure that will contain:
This struct we will use this way in the pallet utxo:
When creating a new instance of this structure, we must initialize the fields.
Each subsequent check adds a new instance of the function to
set_of_checks, which will be called incollect_result.At the moment we can split the verification function for these parts:
checking_inputsu32::MAXchecking_outputsu32::MAXchecking_utxos_existschecking_signatureschecking_tokens_transferringchecking_tokens_issuedmetadata_uriandtickervalueanddecimalchecking_nft_mintmetadata_urichecking_assets_burncalculating_rewardcollect_resultQuestions
fn pick_utxoandfn send_to_address. Isn't that?I'm glad to see any suggestions or critics.
To continue the discussion from the PR #80