linera-execution 0.15.17

Persistent data and the corresponding logics used by the Linera protocol for runtime and execution of smart contracts / applications.
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Tests for how the runtime computes fees based on consumed resources.

use std::{collections::BTreeSet, sync::Arc, vec};

use linera_base::{
    crypto::AccountPublicKey,
    data_types::{Amount, BlockHeight, OracleResponse, Timestamp},
    http,
    identifiers::{Account, AccountOwner, StreamName},
    vm::VmRuntime,
};
use linera_execution::{
    test_utils::{
        blob_oracle_responses, dummy_chain_description, ExpectedCall, RegisterMockApplication,
        SystemExecutionState,
    },
    BaseRuntime, ContractRuntime, ExecutionError, ExecutionStateActor, Message, MessageContext,
    ResourceControlPolicy, ResourceController, ResourceTracker, TransactionTracker,
};
use test_case::test_case;

/// Tests if the chain balance is updated based on the fees spent for consuming resources.
// Chain account only.
#[test_case(vec![], Amount::ZERO, None, None; "without any costs")]
#[test_case(vec![FeeSpend::Fuel(100)], Amount::from_tokens(1_000), None, None; "with only execution costs")]
#[test_case(vec![FeeSpend::Read(vec![0, 1], None)], Amount::from_tokens(1_000), None, None; "with only an empty read")]
#[test_case(
    vec![
        FeeSpend::Read(vec![0, 1], None),
        FeeSpend::Fuel(207),
    ],
    Amount::from_tokens(1_000),
    None,
    None;
    "with execution and an empty read"
)]
// Chain account and small owner account.
#[test_case(
    vec![FeeSpend::Fuel(100)],
    Amount::from_tokens(1_000),
    Some(Amount::from_tokens(1)),
    None;
    "with only execution costs and with owner account"
)]
#[test_case(
    vec![FeeSpend::Read(vec![0, 1], None)],
    Amount::from_tokens(1_000),
    Some(Amount::from_tokens(1)),
    None;
    "with only an empty read and with owner account"
)]
#[test_case(
    vec![
        FeeSpend::Read(vec![0, 1], None),
        FeeSpend::Fuel(207),
    ],
    Amount::from_tokens(1_000),
    Some(Amount::from_tokens(1)),
    None;
    "with execution and an empty read and with owner account"
)]
// Small chain account and larger owner account.
#[test_case(
    vec![FeeSpend::Fuel(100)],
    Amount::from_tokens(1),
    Some(Amount::from_tokens(1_000)),
    None;
    "with only execution costs and with larger owner account"
)]
#[test_case(
    vec![FeeSpend::Read(vec![0, 1], None)],
    Amount::from_tokens(1),
    Some(Amount::from_tokens(1_000)),
    None;
    "with only an empty read and with larger owner account"
)]
#[test_case(
    vec![
        FeeSpend::Read(vec![0, 1], None),
        FeeSpend::Fuel(207),
    ],
    Amount::from_tokens(1),
    Some(Amount::from_tokens(1_000)),
    None;
    "with execution and an empty read and with larger owner account"
)]
// Small chain account, small owner account, large grant.
#[test_case(
    vec![FeeSpend::Fuel(100)],
    Amount::from_tokens(2),
    Some(Amount::from_tokens(1)),
    Some(Amount::from_tokens(1_000));
    "with only execution costs and with owner account and grant"
)]
#[test_case(
    vec![FeeSpend::Read(vec![0, 1], None)],
    Amount::from_tokens(2),
    Some(Amount::from_tokens(1)),
    Some(Amount::from_tokens(1_000));
    "with only an empty read and with owner account and grant"
)]
#[test_case(
    vec![
        FeeSpend::Read(vec![0, 1], None),
        FeeSpend::Fuel(207),
    ],
    Amount::from_tokens(2),
    Some(Amount::from_tokens(1)),
    Some(Amount::from_tokens(1_000));
    "with execution and an empty read and with owner account and grant"
)]
#[test_case(
    vec![
        FeeSpend::QueryServiceOracle,
        FeeSpend::Runtime(32),
    ],
    Amount::from_tokens(2),
    Some(Amount::from_tokens(1)),
    Some(Amount::from_tokens(1_000));
    "with only a service oracle call"
)]
#[test_case(
    vec![
        FeeSpend::QueryServiceOracle,
        FeeSpend::QueryServiceOracle,
        FeeSpend::QueryServiceOracle,
        FeeSpend::Runtime(96),
    ],
    Amount::from_tokens(2),
    Some(Amount::from_tokens(1)),
    Some(Amount::from_tokens(1_000));
    "with three service oracle calls"
)]
#[test_case(
    vec![
        FeeSpend::Fuel(91),
        FeeSpend::QueryServiceOracle,
        FeeSpend::Fuel(11),
        FeeSpend::Read(vec![0, 1, 2], None),
        FeeSpend::QueryServiceOracle,
        FeeSpend::Fuel(57),
        FeeSpend::QueryServiceOracle,
        FeeSpend::Runtime(96),
    ],
    Amount::from_tokens(2),
    Some(Amount::from_tokens(1_000)),
    None;
    "with service oracle calls, fuel consumption and a read operation"
)]
#[test_case(
    vec![FeeSpend::HttpRequest],
    Amount::from_tokens(2),
    Some(Amount::from_tokens(1)),
    Some(Amount::from_tokens(1_000));
    "with one HTTP request"
)]
#[test_case(
    vec![
        FeeSpend::HttpRequest,
        FeeSpend::HttpRequest,
        FeeSpend::HttpRequest,
    ],
    Amount::from_tokens(2),
    Some(Amount::from_tokens(1)),
    Some(Amount::from_tokens(1_000));
    "with three HTTP requests"
)]
#[test_case(
    vec![
        FeeSpend::Fuel(11),
        FeeSpend::HttpRequest,
        FeeSpend::Read(vec![0, 1], None),
        FeeSpend::Fuel(23),
        FeeSpend::HttpRequest,
    ],
    Amount::from_tokens(2),
    Some(Amount::from_tokens(1)),
    Some(Amount::from_tokens(1_000));
    "with all fee spend operations"
)]
// TODO(#1601): Add more test cases
#[tokio::test]
async fn test_fee_consumption(
    spends: Vec<FeeSpend>,
    chain_balance: Amount,
    owner_balance: Option<Amount>,
    initial_grant: Option<Amount>,
) -> anyhow::Result<()> {
    let chain_description = dummy_chain_description(0);
    let chain_id = chain_description.id();
    let mut state = SystemExecutionState {
        description: Some(chain_description.clone()),
        ..SystemExecutionState::default()
    };
    let (application_id, application, blobs) = state.register_mock_application(0).await?;
    let mut view = state.into_view().await;

    let mut oracle_responses = blob_oracle_responses(blobs.iter());

    let signer = AccountOwner::from(AccountPublicKey::test_key(0));
    view.system.balance.set(chain_balance);
    if let Some(owner_balance) = owner_balance {
        view.system.balances.insert(&signer, owner_balance)?;
    }

    let prices = ResourceControlPolicy {
        wasm_fuel_unit: Amount::from_tokens(3),
        evm_fuel_unit: Amount::from_tokens(2),
        read_operation: Amount::from_tokens(3),
        write_operation: Amount::from_tokens(5),
        byte_runtime: Amount::from_millis(1),
        byte_read: Amount::from_tokens(7),
        byte_written: Amount::from_tokens(11),
        byte_stored: Amount::from_tokens(13),
        operation: Amount::from_tokens(17),
        operation_byte: Amount::from_tokens(19),
        message: Amount::from_tokens(23),
        message_byte: Amount::from_tokens(29),
        service_as_oracle_query: Amount::from_millis(31),
        http_request: Amount::from_tokens(37),
        maximum_wasm_fuel_per_block: 4_868_145_137,
        maximum_evm_fuel_per_block: 4_868_145_137,
        maximum_block_size: 41,
        maximum_service_oracle_execution_ms: 43,
        maximum_blob_size: 47,
        maximum_published_blobs: 53,
        maximum_bytecode_size: 59,
        maximum_block_proposal_size: 61,
        maximum_bytes_read_per_block: 67,
        maximum_bytes_written_per_block: 71,
        maximum_oracle_response_bytes: 73,
        maximum_http_response_bytes: 79,
        http_request_timeout_ms: 83,
        blob_read: Amount::from_tokens(89),
        blob_published: Amount::from_tokens(97),
        blob_byte_read: Amount::from_tokens(101),
        blob_byte_published: Amount::from_tokens(103),
        http_request_allow_list: BTreeSet::new(),
    };

    let consumed_fees = spends
        .iter()
        .map(|spend| spend.amount(&prices))
        .fold(Amount::ZERO, |sum, spent_fees| {
            sum.saturating_add(spent_fees)
        });

    let authenticated_signer = if owner_balance.is_some() {
        Some(signer)
    } else {
        None
    };
    let mut controller = ResourceController::new(
        Arc::new(prices),
        ResourceTracker::default(),
        authenticated_signer,
    );

    for spend in &spends {
        oracle_responses.extend(spend.expected_oracle_responses());
    }

    application.expect_call(ExpectedCall::execute_message(move |runtime, _operation| {
        for spend in spends {
            spend.execute(runtime)?;
        }
        Ok(())
    }));
    application.expect_call(ExpectedCall::default_finalize());

    let refund_grant_to = authenticated_signer
        .map(|owner| Account { chain_id, owner })
        .or(None);
    let context = MessageContext {
        chain_id,
        origin: chain_id,
        is_bouncing: false,
        authenticated_signer,
        refund_grant_to,
        height: BlockHeight(0),
        round: Some(0),
        timestamp: Timestamp::default(),
    };
    let mut grant = initial_grant.unwrap_or_default();
    let mut txn_tracker = TransactionTracker::new_replaying(oracle_responses);
    ExecutionStateActor::new(&mut view, &mut txn_tracker, &mut controller)
        .execute_message(
            context,
            Message::User {
                application_id,
                bytes: vec![],
            },
            if initial_grant.is_some() {
                Some(&mut grant)
            } else {
                None
            },
        )
        .await?;

    let txn_outcome = txn_tracker.into_outcome()?;
    assert!(txn_outcome.outgoing_messages.is_empty());

    match initial_grant {
        None => {
            let (expected_chain_balance, expected_owner_balance) = if chain_balance >= consumed_fees
            {
                (chain_balance.saturating_sub(consumed_fees), owner_balance)
            } else {
                let Some(owner_balance) = owner_balance else {
                    panic!("execution should have failed earlier");
                };
                (
                    Amount::ZERO,
                    Some(
                        owner_balance
                            .saturating_add(chain_balance)
                            .saturating_sub(consumed_fees),
                    ),
                )
            };
            assert_eq!(*view.system.balance.get(), expected_chain_balance);
            assert_eq!(
                view.system.balances.get(&signer).await?,
                expected_owner_balance
            );
            assert_eq!(grant, Amount::ZERO);
        }
        Some(initial_grant) => {
            let (expected_grant, expected_owner_balance) = if initial_grant >= consumed_fees {
                (initial_grant.saturating_sub(consumed_fees), owner_balance)
            } else {
                let Some(owner_balance) = owner_balance else {
                    panic!("execution should have failed earlier");
                };
                (
                    Amount::ZERO,
                    Some(
                        owner_balance
                            .saturating_add(initial_grant)
                            .saturating_sub(consumed_fees),
                    ),
                )
            };
            assert_eq!(*view.system.balance.get(), chain_balance);
            assert_eq!(
                view.system.balances.get(&signer).await?,
                expected_owner_balance
            );
            assert_eq!(grant, expected_grant);
        }
    }

    Ok(())
}

/// A runtime operation that costs some amount of fees.
pub enum FeeSpend {
    /// Consume some execution fuel.
    Fuel(u64),
    /// Reads from storage.
    Read(Vec<u8>, Option<Vec<u8>>),
    /// Queries a service as an oracle.
    QueryServiceOracle,
    /// Performs an HTTP request.
    HttpRequest,
    /// Byte from runtime.
    Runtime(u32),
}

impl FeeSpend {
    /// Returns the [`OracleResponse`]s necessary for executing this runtime operation.
    pub fn expected_oracle_responses(&self) -> Vec<OracleResponse> {
        match self {
            FeeSpend::Fuel(_) | FeeSpend::Read(_, _) | FeeSpend::Runtime(_) => vec![],
            FeeSpend::QueryServiceOracle => {
                vec![OracleResponse::Service(vec![])]
            }
            FeeSpend::HttpRequest => vec![OracleResponse::Http(http::Response::ok([]))],
        }
    }

    /// The fee amount required for this runtime operation.
    pub fn amount(&self, policy: &ResourceControlPolicy) -> Amount {
        match self {
            FeeSpend::Fuel(units) => policy.wasm_fuel_unit.saturating_mul(*units as u128),
            FeeSpend::Read(_key, value) => {
                let value_read_fee = value
                    .as_ref()
                    .map_or(Amount::ZERO, |value| Amount::from(value.len() as u128));

                policy.read_operation.saturating_add(value_read_fee)
            }
            FeeSpend::QueryServiceOracle => policy.service_as_oracle_query,
            FeeSpend::HttpRequest => policy.http_request,
            FeeSpend::Runtime(bytes) => policy.byte_runtime.saturating_mul(*bytes as u128),
        }
    }

    /// Executes the operation with the `runtime`
    pub fn execute(self, runtime: &mut impl ContractRuntime) -> Result<(), ExecutionError> {
        match self {
            FeeSpend::Fuel(units) => runtime.consume_fuel(units, VmRuntime::Wasm),
            FeeSpend::Runtime(_bytes) => Ok(()),
            FeeSpend::Read(key, value) => {
                let promise = runtime.read_value_bytes_new(key)?;
                let response = runtime.read_value_bytes_wait(&promise)?;
                assert_eq!(response, value);
                Ok(())
            }
            FeeSpend::QueryServiceOracle => {
                let application_id = runtime.application_id()?;
                runtime.query_service(application_id, vec![])?;
                Ok(())
            }
            FeeSpend::HttpRequest => {
                runtime.perform_http_request(http::Request::get("http://dummy.url"))?;
                Ok(())
            }
        }
    }
}

/// Tests that a free app has all message- and event-related fees waived when executing a message,
/// even with all fee categories set to non-zero values.
#[tokio::test]
async fn test_free_app_message_no_fees() -> anyhow::Result<()> {
    let chain_description = dummy_chain_description(0);
    let chain_id = chain_description.id();
    let mut state = SystemExecutionState {
        description: Some(chain_description.clone()),
        ..SystemExecutionState::default()
    };
    let (application_id, application, blobs) = state.register_mock_application(0).await?;

    let chain_balance = Amount::from_tokens(100);
    let owner_balance = Amount::from_tokens(50);
    let mut view = state.into_view().await;

    let mut oracle_responses = blob_oracle_responses(blobs.iter());

    let signer = AccountOwner::from(AccountPublicKey::test_key(0));
    view.system.balance.set(chain_balance);
    view.system.balances.insert(&signer, owner_balance)?;

    // Use all_categories() which sets non-zero prices for all fee types, then add
    // the application as a free app.
    let mut policy = ResourceControlPolicy::all_categories();
    policy
        .http_request_allow_list
        .insert(ResourceControlPolicy::free_app_flag(&application_id));

    let mut controller =
        ResourceController::new(Arc::new(policy), ResourceTracker::default(), Some(signer));

    // The mock application consumes fuel, does a read, queries a service oracle,
    // and emits an event.
    oracle_responses.push(OracleResponse::Service(vec![]));

    application.expect_call(ExpectedCall::execute_message(move |runtime, _message| {
        runtime.consume_fuel(500, VmRuntime::Wasm)?;
        let promise = runtime.read_value_bytes_new(vec![0, 1])?;
        let _response = runtime.read_value_bytes_wait(&promise)?;
        let app_id = BaseRuntime::application_id(runtime)?;
        runtime.query_service(app_id, vec![])?;
        runtime.emit(StreamName(b"test".to_vec()), b"event data".to_vec())?;
        Ok(())
    }));
    application.expect_call(ExpectedCall::default_finalize());

    let refund_grant_to = Some(Account {
        chain_id,
        owner: signer,
    });
    let context = MessageContext {
        chain_id,
        origin: chain_id,
        is_bouncing: false,
        authenticated_signer: Some(signer),
        refund_grant_to,
        height: BlockHeight(0),
        round: Some(0),
        timestamp: Timestamp::default(),
    };
    let mut txn_tracker = TransactionTracker::new_replaying(oracle_responses);
    ExecutionStateActor::new(&mut view, &mut txn_tracker, &mut controller)
        .execute_message(
            context,
            Message::User {
                application_id,
                bytes: vec![],
            },
            None,
        )
        .await?;

    // Verify no fees were deducted: balances should remain exactly as set.
    assert_eq!(*view.system.balance.get(), chain_balance);
    assert_eq!(
        view.system.balances.get(&signer).await?,
        Some(owner_balance)
    );

    Ok(())
}

/// Tests that a free app is still charged fees for operations (not messages).
#[tokio::test]
async fn test_free_app_operation_still_charged() -> anyhow::Result<()> {
    let chain_description = dummy_chain_description(0);
    let chain_id = chain_description.id();
    let mut state = SystemExecutionState {
        description: Some(chain_description.clone()),
        ..SystemExecutionState::default()
    };
    let (application_id, application, blobs) = state.register_mock_application(0).await?;

    let chain_balance = Amount::from_tokens(1_000);
    let mut view = state.into_view().await;

    let oracle_responses = blob_oracle_responses(blobs.iter());

    view.system.balance.set(chain_balance);

    let mut policy = ResourceControlPolicy::all_categories();
    policy
        .http_request_allow_list
        .insert(ResourceControlPolicy::free_app_flag(&application_id));

    let mut controller = ResourceController::new(
        Arc::new(policy),
        ResourceTracker::default(),
        None::<AccountOwner>,
    );

    application.expect_call(ExpectedCall::execute_operation(
        move |runtime, _operation| {
            runtime.consume_fuel(100, VmRuntime::Wasm)?;
            Ok(vec![])
        },
    ));
    application.expect_call(ExpectedCall::default_finalize());

    let context = linera_execution::OperationContext {
        chain_id,
        height: BlockHeight(0),
        round: Some(0),
        authenticated_signer: None,
        timestamp: Timestamp::default(),
    };
    let mut txn_tracker = TransactionTracker::new_replaying(oracle_responses);
    ExecutionStateActor::new(&mut view, &mut txn_tracker, &mut controller)
        .execute_operation(
            context,
            linera_execution::Operation::User {
                application_id,
                bytes: vec![],
            },
        )
        .await?;

    // Verify that fees WERE deducted (operations are not free).
    // At minimum, 100 fuel units * 1 nano per unit = 100 nanos should have been charged.
    let min_expected_fees = Amount::from_nanos(100);
    let final_balance = *view.system.balance.get();
    assert!(
        chain_balance.saturating_sub(final_balance) >= min_expected_fees,
        "Expected at least {min_expected_fees} in fees, but balance only dropped from \
         {chain_balance} to {final_balance}"
    );

    Ok(())
}