cfxcore/pos/mempool/shared_mempool/
transaction_validator.rs

1use diem_types::{
2    term_state::PosState,
3    transaction::{
4        authenticator::TransactionAuthenticator, GovernanceRole,
5        SignedTransaction, TransactionPayload, VMValidatorResult,
6    },
7};
8use move_core_types::vm_status::DiscardedVMStatus;
9use std::sync::Arc;
10
11pub struct TransactionValidator {}
12
13impl TransactionValidator {
14    pub fn new() -> Self { Self {} }
15
16    // TODO: `score` and `governance_role` in `VMValidatorResult` are not
17    // needed now.
18    pub fn validate_transaction(
19        &self, tx: &SignedTransaction, pos_state: Arc<PosState>,
20    ) -> Option<VMValidatorResult> {
21        // This check is cheaper than signature verification, so we do not
22        // need to verify signatures for old transactions.
23        let result = match tx.payload() {
24            TransactionPayload::Election(election_payload) => {
25                pos_state.validate_election_simple(election_payload)
26            }
27            TransactionPayload::PivotDecision(pivot_decision) => {
28                pos_state.validate_pivot_decision_simple(pivot_decision)
29            }
30            _ => None,
31        };
32        if result.is_some() {
33            return Some(VMValidatorResult::new(
34                result,
35                0,
36                GovernanceRole::Validator,
37            ));
38        }
39
40        match tx.authenticator() {
41            TransactionAuthenticator::BLS { .. } => {}
42            _ => {
43                return Some(VMValidatorResult::new(
44                    Some(DiscardedVMStatus::INVALID_SIGNATURE),
45                    0,
46                    GovernanceRole::Validator,
47                ));
48            }
49        }
50
51        // check signature
52        if tx.clone().check_signature().is_err() {
53            return Some(VMValidatorResult::new(
54                Some(DiscardedVMStatus::INVALID_SIGNATURE),
55                0,
56                GovernanceRole::Validator,
57            ));
58        }
59
60        Some(VMValidatorResult::new(result, 0, GovernanceRole::Validator))
61    }
62}