|
| 1 | +// Copyright (c) Zefchain Labs, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#![cfg_attr(target_arch = "wasm32", no_main)] |
| 5 | + |
| 6 | +mod state; |
| 7 | + |
| 8 | +use counter_no_graphql::{CounterNoGraphQlAbi, CounterOperation}; |
| 9 | +use linera_sdk::{ |
| 10 | + linera_base_types::WithContractAbi, |
| 11 | + views::{RootView, View}, |
| 12 | + Contract, ContractRuntime, |
| 13 | +}; |
| 14 | + |
| 15 | +use self::state::CounterState; |
| 16 | + |
| 17 | +pub struct CounterContract { |
| 18 | + state: CounterState, |
| 19 | + runtime: ContractRuntime<Self>, |
| 20 | +} |
| 21 | + |
| 22 | +linera_sdk::contract!(CounterContract); |
| 23 | + |
| 24 | +impl WithContractAbi for CounterContract { |
| 25 | + type Abi = CounterNoGraphQlAbi; |
| 26 | +} |
| 27 | + |
| 28 | +impl Contract for CounterContract { |
| 29 | + type Message = (); |
| 30 | + type InstantiationArgument = u64; |
| 31 | + type Parameters = (); |
| 32 | + |
| 33 | + async fn load(runtime: ContractRuntime<Self>) -> Self { |
| 34 | + let state = CounterState::load(runtime.root_view_storage_context()) |
| 35 | + .await |
| 36 | + .expect("Failed to load state"); |
| 37 | + CounterContract { state, runtime } |
| 38 | + } |
| 39 | + |
| 40 | + async fn instantiate(&mut self, value: u64) { |
| 41 | + // Validate that the application parameters were configured correctly. |
| 42 | + self.runtime.application_parameters(); |
| 43 | + |
| 44 | + self.state.value.set(value); |
| 45 | + } |
| 46 | + |
| 47 | + async fn execute_operation(&mut self, operation: CounterOperation) -> u64 { |
| 48 | + let previous_value = self.state.value.get(); |
| 49 | + let CounterOperation::Increment(value) = operation; |
| 50 | + let new_value = previous_value + value; |
| 51 | + self.state.value.set(new_value); |
| 52 | + new_value |
| 53 | + } |
| 54 | + |
| 55 | + async fn execute_message(&mut self, _message: ()) { |
| 56 | + panic!("Counter application doesn't support any cross-chain messages"); |
| 57 | + } |
| 58 | + |
| 59 | + async fn store(mut self) { |
| 60 | + self.state.save().await.expect("Failed to save state"); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use futures::FutureExt as _; |
| 67 | + use linera_sdk::{util::BlockingWait, views::View, Contract, ContractRuntime}; |
| 68 | + |
| 69 | + use super::{CounterContract, CounterOperation, CounterState}; |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn operation() { |
| 73 | + let initial_value = 72_u64; |
| 74 | + let mut counter = create_and_instantiate_counter(initial_value); |
| 75 | + |
| 76 | + let increment = 42_308_u64; |
| 77 | + let operation = CounterOperation::Increment(increment); |
| 78 | + |
| 79 | + let response = counter |
| 80 | + .execute_operation(operation) |
| 81 | + .now_or_never() |
| 82 | + .expect("Execution of counter operation should not await anything"); |
| 83 | + |
| 84 | + let expected_value = initial_value + increment; |
| 85 | + |
| 86 | + assert_eq!(response, expected_value); |
| 87 | + assert_eq!(*counter.state.value.get(), initial_value + increment); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + #[should_panic(expected = "Counter application doesn't support any cross-chain messages")] |
| 92 | + fn message() { |
| 93 | + let initial_value = 72_u64; |
| 94 | + let mut counter = create_and_instantiate_counter(initial_value); |
| 95 | + |
| 96 | + counter |
| 97 | + .execute_message(()) |
| 98 | + .now_or_never() |
| 99 | + .expect("Execution of counter operation should not await anything"); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn cross_application_call() { |
| 104 | + let initial_value = 2_845_u64; |
| 105 | + let mut counter = create_and_instantiate_counter(initial_value); |
| 106 | + |
| 107 | + let increment = 8_u64; |
| 108 | + let operation = CounterOperation::Increment(increment); |
| 109 | + |
| 110 | + let response = counter |
| 111 | + .execute_operation(operation) |
| 112 | + .now_or_never() |
| 113 | + .expect("Execution of counter operation should not await anything"); |
| 114 | + |
| 115 | + let expected_value = initial_value + increment; |
| 116 | + |
| 117 | + assert_eq!(response, expected_value); |
| 118 | + assert_eq!(*counter.state.value.get(), expected_value); |
| 119 | + } |
| 120 | + |
| 121 | + fn create_and_instantiate_counter(initial_value: u64) -> CounterContract { |
| 122 | + let runtime = ContractRuntime::new().with_application_parameters(()); |
| 123 | + let mut contract = CounterContract { |
| 124 | + state: CounterState::load(runtime.root_view_storage_context()) |
| 125 | + .blocking_wait() |
| 126 | + .expect("Failed to read from mock key value store"), |
| 127 | + runtime, |
| 128 | + }; |
| 129 | + |
| 130 | + contract |
| 131 | + .instantiate(initial_value) |
| 132 | + .now_or_never() |
| 133 | + .expect("Initialization of counter state should not await anything"); |
| 134 | + |
| 135 | + assert_eq!(*contract.state.value.get(), initial_value); |
| 136 | + |
| 137 | + contract |
| 138 | + } |
| 139 | +} |
0 commit comments