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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

//! Gas costs for common transactions.

use crate::{
    account::{Account, AccountData},
    common_transactions::{create_account_txn, peer_to_peer_txn, rotate_key_txn},
    executor::FakeExecutor,
};
use diem_crypto::{ed25519::Ed25519PrivateKey, PrivateKey, Uniform};
use diem_types::{
    account_config,
    transaction::{authenticator::AuthenticationKey, SignedTransaction},
};
use once_cell::sync::Lazy;

/// The gas each transaction is configured to reserve. If the gas available in the account,
/// converted to microdiem, falls below this threshold, transactions are expected to fail with
/// an insufficient balance.
pub const TXN_RESERVED: u64 = 500_000;

/// The gas cost of a first time create-account transaction.
///
/// This includes the cost of the event counter creation which makes the transaction more
/// expensive. All such transactions are expected to cost the same gas.
pub static CREATE_ACCOUNT_FIRST: Lazy<u64> = Lazy::new(|| {
    let mut executor = FakeExecutor::from_genesis_file();
    let sender = AccountData::new(1_000_000, 10);
    executor.add_account_data(&sender);
    let receiver = Account::new();

    let txn = create_account_txn(
        sender.account(),
        &receiver,
        10,
        20_000,
        account_config::xus_tag(),
    );
    compute_gas_used(txn, &mut executor)
});

/// The gas cost of a create-account transaction.
///
/// This is the cost after the event counter has been created.
/// All such transactions are expected to cost the same gas.
pub static CREATE_ACCOUNT_NEXT: Lazy<u64> = Lazy::new(|| {
    let mut executor = FakeExecutor::from_genesis_file();
    let sender = AccountData::new(1_000_000, 10);
    executor.add_account_data(&sender);

    let txns = vec![
        create_account_txn(
            sender.account(),
            &Account::new(),
            10,
            20_000,
            account_config::xus_tag(),
        ),
        create_account_txn(
            sender.account(),
            &Account::new(),
            11,
            20_000,
            account_config::xus_tag(),
        ),
    ];
    let output = &executor
        .execute_block(txns)
        .expect("The VM should not fail to startup");
    output[1].gas_used()
});

/// The gas cost of a create-account transaction where the sender has an insufficient balance.
///
/// This includes the cost of the event counter creation. As such the cost of the transaction
/// would be higher and the balance required must be higher.
/// All such transactions are expected to cost the same gas.
pub static CREATE_ACCOUNT_TOO_LOW_FIRST: Lazy<u64> = Lazy::new(|| {
    let mut executor = FakeExecutor::from_genesis_file();
    // The gas amount is the minimum that needs to be reserved, so use a value that's
    // clearly higher than that.
    let balance = TXN_RESERVED + 10_000;
    let sender = AccountData::new(balance, 10);
    executor.add_account_data(&sender);
    let receiver = Account::new();

    let txn = create_account_txn(
        sender.account(),
        &receiver,
        10,
        balance + 1,
        account_config::xus_tag(),
    );
    compute_gas_used(txn, &mut executor)
});

/// The gas cost of a create-account transaction where the sender has an insufficient balance.
///
/// This is the cost after the event counter has been created.
/// All such transactions are expected to cost the same gas.
pub static CREATE_ACCOUNT_TOO_LOW_NEXT: Lazy<u64> = Lazy::new(|| {
    let mut executor = FakeExecutor::from_genesis_file();
    // The gas amount is the minimum that needs to be reserved, so use a value that's
    // clearly higher than that.
    let balance = (2 * TXN_RESERVED) + 10_000;
    let sender = AccountData::new(balance, 10);
    executor.add_account_data(&sender);

    let txns = vec![
        create_account_txn(
            sender.account(),
            &Account::new(),
            10,
            10,
            account_config::xus_tag(),
        ),
        create_account_txn(
            sender.account(),
            &Account::new(),
            11,
            balance,
            account_config::xus_tag(),
        ),
    ];
    let output = &executor
        .execute_block(txns)
        .expect("The VM should not fail to startup");
    output[1].gas_used()
});

/// The gas cost of a create-account transaction where the receiver already exists.
///
/// This includes the cost of the event counter creation. As such the cost of the transaction
/// would be higher and the balance required must be higher.
/// All such transactions are expected to cost the same gas.
pub static CREATE_EXISTING_ACCOUNT_FIRST: Lazy<u64> = Lazy::new(|| {
    let mut executor = FakeExecutor::from_genesis_file();
    let sender = AccountData::new(1_000_000, 10);
    let receiver = AccountData::new(1_000_000, 10);
    executor.add_account_data(&sender);
    executor.add_account_data(&receiver);

    let txn = create_account_txn(
        sender.account(),
        receiver.account(),
        10,
        20_000,
        account_config::xus_tag(),
    );
    compute_gas_used(txn, &mut executor)
});

/// The gas cost of a create-account transaction where the receiver already exists.
///
/// This is the cost after the event counter has been created.
/// All such transactions are expected to cost the same gas.
pub static CREATE_EXISTING_ACCOUNT_NEXT: Lazy<u64> = Lazy::new(|| {
    let mut executor = FakeExecutor::from_genesis_file();
    let sender = AccountData::new(1_000_000, 10);
    let receiver = AccountData::new(1_000_000, 10);
    executor.add_account_data(&sender);
    executor.add_account_data(&receiver);

    let txns = vec![
        create_account_txn(
            sender.account(),
            &Account::new(),
            10,
            20_000,
            account_config::xus_tag(),
        ),
        create_account_txn(
            sender.account(),
            receiver.account(),
            11,
            20_000,
            account_config::xus_tag(),
        ),
    ];
    let output = &executor
        .execute_block(txns)
        .expect("The VM should not fail to startup");
    output[1].gas_used()
});

/// The gas cost of a peer-to-peer transaction.
///
/// All such transactions are expected to cost the same gas.
pub static PEER_TO_PEER: Lazy<u64> = Lazy::new(|| {
    // Compute gas used by running a placeholder transaction.
    let mut executor = FakeExecutor::from_genesis_file();
    let sender = AccountData::new(1_000_000, 10);
    let receiver = AccountData::new(1_000_000, 10);
    executor.add_account_data(&sender);
    executor.add_account_data(&receiver);

    let txn = peer_to_peer_txn(sender.account(), receiver.account(), 10, 20_000);
    compute_gas_used(txn, &mut executor)
});

/// The gas cost of a peer-to-peer transaction with an insufficient balance.
///
/// All such transactions are expected to cost the same gas.
pub static PEER_TO_PEER_TOO_LOW: Lazy<u64> = Lazy::new(|| {
    let mut executor = FakeExecutor::from_genesis_file();
    // The gas amount is the minimum that needs to be reserved, so use a value that's clearly
    // higher than that.
    let balance = TXN_RESERVED + 10_000;
    let sender = AccountData::new(balance, 10);
    let receiver = AccountData::new(1_000_000, 10);
    executor.add_account_data(&sender);
    executor.add_account_data(&receiver);

    let txn = peer_to_peer_txn(sender.account(), receiver.account(), 10, balance + 1);
    compute_gas_used(txn, &mut executor)
});

/// The gas cost of a peer-to-peer transaction that creates a new account.
///
/// This includes the cost of the event counter creation. As such the cost of the transaction
/// would be higher and the balance required must be higher.
/// All such transactions are expected to cost the same gas.
pub static PEER_TO_PEER_NEW_RECEIVER_FIRST: Lazy<u64> = Lazy::new(|| {
    // Compute gas used by running a placeholder transaction.
    let mut executor = FakeExecutor::from_genesis_file();
    let sender = AccountData::new(1_000_000, 10);
    executor.add_account_data(&sender);
    let receiver = Account::new();

    let txn = peer_to_peer_txn(sender.account(), &receiver, 10, 20_000);
    compute_gas_used(txn, &mut executor)
});

/// The gas cost of a peer-to-peer transaction that creates a new account.
///
/// This is the cost after the event counter has been created.
/// All such transactions are expected to cost the same gas.
pub static PEER_TO_PEER_NEW_RECEIVER_NEXT: Lazy<u64> = Lazy::new(|| {
    // Compute gas used by running a placeholder transaction.
    let mut executor = FakeExecutor::from_genesis_file();
    let sender = AccountData::new(1_000_000, 10);
    executor.add_account_data(&sender);

    let txns = vec![
        peer_to_peer_txn(sender.account(), &Account::new(), 10, 20_000),
        peer_to_peer_txn(sender.account(), &Account::new(), 11, 20_000),
    ];
    let output = &executor
        .execute_block(txns)
        .expect("The VM should not fail to startup");
    output[1].gas_used()
});

/// The gas cost of a peer-to-peer transaction that tries to create a new account, but fails
/// because of an insufficient balance.
///
/// This includes the cost of the event counter creation. As such the cost of the transaction
/// would be higher and the balance required must be higher.
/// All such transactions are expected to cost the same gas.
pub static PEER_TO_PEER_NEW_RECEIVER_TOO_LOW_FIRST: Lazy<u64> = Lazy::new(|| {
    let mut executor = FakeExecutor::from_genesis_file();
    // The gas amount is the minimum that needs to be reserved, so use a value that's
    // clearly higher than that.
    let balance = TXN_RESERVED + 10_000;
    let sender = AccountData::new(balance, 10);
    executor.add_account_data(&sender);
    let receiver = Account::new();

    let txn = peer_to_peer_txn(sender.account(), &receiver, 10, balance + 1);
    compute_gas_used(txn, &mut executor)
});

/// The gas cost of a peer-to-peer transaction that tries to create a new account, but fails
/// because of an insufficient balance.
///
/// This is the cost after the event counter has been created.
/// All such transactions are expected to cost the same gas.
pub static PEER_TO_PEER_NEW_RECEIVER_TOO_LOW_NEXT: Lazy<u64> = Lazy::new(|| {
    let mut executor = FakeExecutor::from_genesis_file();
    // The gas amount is the minimum that needs to be reserved, so use a value that's
    // clearly higher than that.
    let balance = (2 * TXN_RESERVED) + 20_000;
    let sender = AccountData::new(balance, 10);
    executor.add_account_data(&sender);

    let txns = vec![
        peer_to_peer_txn(sender.account(), &Account::new(), 10, 10_000),
        peer_to_peer_txn(sender.account(), &Account::new(), 11, balance),
    ];
    let output = &executor
        .execute_block(txns)
        .expect("The VM should not fail to startup");
    output[1].gas_used()
});

/// The gas cost of a rotate-key transaction.
///
/// All such transactions are expected to cost the same gas.
pub static ROTATE_KEY: Lazy<u64> = Lazy::new(|| {
    let mut executor = FakeExecutor::from_genesis_file();
    let sender = AccountData::new(1_000_000, 10);
    executor.add_account_data(&sender);
    let pubkey = Ed25519PrivateKey::generate_for_testing().public_key();
    let new_key_hash = AuthenticationKey::ed25519(&pubkey).to_vec();

    let txn = rotate_key_txn(sender.account(), new_key_hash, 10);
    compute_gas_used(txn, &mut executor)
});

fn compute_gas_used(txn: SignedTransaction, executor: &mut FakeExecutor) -> u64 {
    let output = &executor.execute_transaction(txn);
    output.gas_used()
}