1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::vm_validator::TransactionValidation;
use anyhow::Result;
use diem_state_view::StateView;
use diem_types::{
    account_address::AccountAddress,
    on_chain_config::OnChainConfigPayload,
    transaction::{GovernanceRole, SignedTransaction, VMValidatorResult},
    vm_status::StatusCode,
};
use diem_vm::VMValidator;

#[derive(Clone)]
pub struct MockVMValidator;

impl VMValidator for MockVMValidator {
    fn validate_transaction(
        &self,
        _transaction: SignedTransaction,
        _state_view: &dyn StateView,
    ) -> VMValidatorResult {
        VMValidatorResult::new(None, 0, GovernanceRole::NonGovernanceRole)
    }
}

impl TransactionValidation for MockVMValidator {
    type ValidationInstance = MockVMValidator;
    fn validate_transaction(&self, txn: SignedTransaction) -> Result<VMValidatorResult> {
        let txn = match txn.check_signature() {
            Ok(txn) => txn,
            Err(_) => {
                return Ok(VMValidatorResult::new(
                    Some(StatusCode::INVALID_SIGNATURE),
                    0,
                    GovernanceRole::NonGovernanceRole,
                ))
            }
        };

        let sender = txn.sender();
        let account_dne_test_add = AccountAddress::new([0_u8; AccountAddress::LENGTH]);
        let invalid_sig_test_add = AccountAddress::new([1_u8; AccountAddress::LENGTH]);
        let insufficient_balance_test_add = AccountAddress::new([2_u8; AccountAddress::LENGTH]);
        let seq_number_too_new_test_add = AccountAddress::new([3_u8; AccountAddress::LENGTH]);
        let seq_number_too_old_test_add = AccountAddress::new([4_u8; AccountAddress::LENGTH]);
        let txn_expiration_time_test_add = AccountAddress::new([5_u8; AccountAddress::LENGTH]);
        let invalid_auth_key_test_add = AccountAddress::new([6_u8; AccountAddress::LENGTH]);
        let ret = if sender == account_dne_test_add {
            Some(StatusCode::SENDING_ACCOUNT_DOES_NOT_EXIST)
        } else if sender == invalid_sig_test_add {
            Some(StatusCode::INVALID_SIGNATURE)
        } else if sender == insufficient_balance_test_add {
            Some(StatusCode::INSUFFICIENT_BALANCE_FOR_TRANSACTION_FEE)
        } else if sender == seq_number_too_new_test_add {
            Some(StatusCode::SEQUENCE_NUMBER_TOO_NEW)
        } else if sender == seq_number_too_old_test_add {
            Some(StatusCode::SEQUENCE_NUMBER_TOO_OLD)
        } else if sender == txn_expiration_time_test_add {
            Some(StatusCode::TRANSACTION_EXPIRED)
        } else if sender == invalid_auth_key_test_add {
            Some(StatusCode::INVALID_AUTH_KEY)
        } else {
            None
        };
        Ok(VMValidatorResult::new(
            ret,
            0,
            GovernanceRole::NonGovernanceRole,
        ))
    }

    fn restart(&mut self, _config: OnChainConfigPayload) -> Result<()> {
        unimplemented!();
    }
}