Skip to content

Commit 19e2710

Browse files
committed
Make the composed operation work in an example.
1 parent 11326dc commit 19e2710

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

examples/counter/src/contract.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ impl Contract for CounterContract {
4545
self.state.value.set(value);
4646
}
4747

48-
async fn execute_operation(&mut self, operation: CounterOperation) -> u64 {
49-
let CounterOperation::Increment(operation) = operation;
50-
let new_value = self.state.value.get() + operation;
48+
async fn execute_operation(&mut self, operation: CounterOperation) -> CounterOperation {
49+
let CounterOperation::Increment(increment) = operation;
50+
let new_value = self.state.value.get() + increment;
5151
self.state.value.set(new_value);
52-
new_value
52+
operation
5353
}
5454

5555
async fn execute_message(&mut self, _message: ()) {
@@ -84,8 +84,8 @@ mod tests {
8484

8585
let expected_value = initial_value + increment;
8686

87-
assert_eq!(response, expected_value);
88-
assert_eq!(*counter.state.value.get(), initial_value + increment);
87+
assert_eq!(response, CounterOperation::Increment(increment));
88+
assert_eq!(*counter.state.value.get(), expected_value);
8989
}
9090

9191
#[test]
@@ -115,7 +115,7 @@ mod tests {
115115

116116
let expected_value = initial_value + increment;
117117

118-
assert_eq!(response, expected_value);
118+
assert_eq!(response, CounterOperation::Increment(increment));
119119
assert_eq!(*counter.state.value.get(), expected_value);
120120
}
121121

examples/counter/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ use serde::{Deserialize, Serialize};
1212

1313
pub struct CounterAbi;
1414

15-
#[derive(Debug, Deserialize, Serialize, GraphQLMutationRoot)]
15+
#[derive(Debug, Deserialize, PartialEq, Eq, Serialize, GraphQLMutationRoot)]
1616
pub enum CounterOperation {
1717
/// Increment the counter by the given value
1818
Increment(u64),
1919
}
2020

2121
impl ContractAbi for CounterAbi {
2222
type Operation = CounterOperation;
23-
type Response = u64;
23+
type Response = CounterOperation;
2424
}
2525

2626
impl ServiceAbi for CounterAbi {

linera-core/src/unit_tests/wasm_client_tests.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ where
164164

165165
let increment = 5_u64;
166166
let counter_operation = counter::CounterOperation::Increment(increment);
167+
let operation1 = Operation::user(application_id, &counter_operation)?;
168+
let operation2 = Operation::user_composed(application_id.forget_abi());
167169
creator
168-
.execute_operation(Operation::user(application_id, &counter_operation)?)
170+
.execute_operations(vec![operation1, operation2], vec![])
169171
.await
170172
.unwrap();
171173

@@ -175,9 +177,10 @@ where
175177
.await
176178
.unwrap();
177179

180+
// We do an increment by 5 and then immediately reiterate it.
178181
let expected = QueryOutcome {
179182
response: async_graphql::Response::new(
180-
async_graphql::Value::from_json(json!({"value": 15})).unwrap(),
183+
async_graphql::Value::from_json(json!({"value": 20})).unwrap(),
181184
),
182185
operations: vec![],
183186
};

linera-execution/src/wasm/runtime_api.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,20 @@ where
683683
Runtime: ServiceRuntime + 'static,
684684
{
685685
/// Schedules an operation to be included in the block being built by this query.
686-
fn schedule_operation(caller: &mut Caller, input: OperationInput) -> Result<(), RuntimeError> {
686+
fn schedule_operation(caller: &mut Caller, operation: Vec<u8>) -> Result<(), RuntimeError> {
687687
caller
688688
.user_data_mut()
689689
.runtime
690-
.schedule_operation(input)
690+
.schedule_operation(OperationInput::Direct(operation))
691+
.map_err(|error| RuntimeError::Custom(error.into()))
692+
}
693+
694+
/// Schedules a composed operation to be included in the block being built by this query.
695+
fn schedule_composed_operation(caller: &mut Caller) -> Result<(), RuntimeError> {
696+
caller
697+
.user_data_mut()
698+
.runtime
699+
.schedule_operation(OperationInput::Composed)
691700
.map_err(|error| RuntimeError::Custom(error.into()))
692701
}
693702

linera-sdk/wit/service-runtime-api.wit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package linera:app;
22

33
interface service-runtime-api {
44
schedule-operation: func(operation: list<u8>);
5+
schedule-composed-operation: func();
56
try-query-application: func(application: application-id, argument: list<u8>) -> list<u8>;
67
check-execution-time: func(fuel-consumed: u64);
78

0 commit comments

Comments
 (0)