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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::{
account_universe::{AUTransactionGen, AccountPair, AccountPairGen, AccountUniverse},
common_transactions::peer_to_peer_txn,
};
use diem_types::{
transaction::{SignedTransaction, TransactionStatus},
vm_status::{known_locations, KeptVMStatus, StatusCode},
};
use proptest::prelude::*;
use proptest_derive::Arbitrary;
use std::sync::Arc;
#[derive(Arbitrary, Clone, Debug)]
#[proptest(params = "(u64, u64)")]
pub struct P2PTransferGen {
sender_receiver: AccountPairGen,
#[proptest(strategy = "params.0 ..= params.1")]
amount: u64,
}
impl AUTransactionGen for P2PTransferGen {
fn apply(
&self,
universe: &mut AccountUniverse,
) -> (SignedTransaction, (TransactionStatus, u64)) {
let AccountPair {
account_1: sender,
account_2: receiver,
..
} = self.sender_receiver.pick(universe);
let txn = peer_to_peer_txn(
sender.account(),
receiver.account(),
sender.sequence_number,
self.amount,
);
let enough_to_transfer = sender.balance >= self.amount;
let gas_amount = sender.peer_to_peer_gas_cost() * txn.gas_unit_price();
let to_deduct = self.amount + gas_amount;
let enough_max_gas = sender.balance >= gas_amount;
let mut gas_used = 0;
let enough_to_succeed = sender.balance >= to_deduct;
let status;
match (enough_max_gas, enough_to_transfer, enough_to_succeed) {
(true, true, true) => {
sender.sequence_number += 1;
sender.sent_events_count += 1;
sender.balance -= to_deduct;
receiver.balance += self.amount;
receiver.received_events_count += 1;
status = TransactionStatus::Keep(KeptVMStatus::Executed);
gas_used = sender.peer_to_peer_gas_cost();
}
(true, true, false) => {
sender.sequence_number += 1;
gas_used = sender.peer_to_peer_gas_cost();
sender.balance -= gas_used * txn.gas_unit_price();
status = TransactionStatus::Keep(KeptVMStatus::MoveAbort(
known_locations::account_module_abort(),
6,
));
}
(true, false, _) => {
sender.sequence_number += 1;
gas_used = sender.peer_to_peer_too_low_gas_cost();
sender.balance -= gas_used * txn.gas_unit_price();
status = TransactionStatus::Keep(KeptVMStatus::MoveAbort(
known_locations::account_module_abort(),
1288,
));
}
(false, _, _) => {
status = TransactionStatus::Discard(
StatusCode::INSUFFICIENT_BALANCE_FOR_TRANSACTION_FEE,
);
}
}
(txn, (status, gas_used))
}
}
pub fn p2p_strategy(
min: u64,
max: u64,
) -> impl Strategy<Value = Arc<dyn AUTransactionGen + 'static>> {
prop_oneof![
3 => any_with::<P2PTransferGen>((min, max)).prop_map(P2PTransferGen::arced),
]
}