Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions examples/amm/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ impl Contract for AmmContract {
}
}

async fn execute_message(&mut self, message: Self::Message) {
async fn execute_message(
&mut self,
_is_bouncing: bool,
origin: ChainId,
message: Self::Message,
) {
assert_eq!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this assert would be wrong on bouncing messages. So to be consistent, we might also add assert!(!is_bouncing, "This application doesn't use bouncing messages.")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, here is the problem: Shouldn't the assert statement be assert!(!is_bouncing, "Bouncing messages are not yet supported")?

Not processing bouncing messages is fine for Message::RemoveLiquidity and Message::RemoveAllAddedLiquidity since no operation of consequence is done before that needs to be reverted.

However, for Message::AddLiquidity and Message::Swap, there is a transfer operation before the message emission. So, what about that operation?

In all transfers, the transfers are done to the creator chain of the AMM. So, the creator chain of the AMM receives two kinds of messages:

  • The ones of transfer
  • The ones of the AMM.

I think the only reasonable design is that all messages are bounced or none of them (is that the case @afck?). Therefore, the transfers are reverted by the bouncing messages, and so it should be ok. And so the correct handling of bouncing messages for the AMM should be, I think

if is_bouncing {
    return;
}

Then the assert statement would work fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An app cannot receive "bouncing messages" if it doesn't create "tracked messages".

If is_bouncing is confusing, then that's an argument in favor of hiding it until people need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for correcting me.
I would argue that if it forces clarification on the is_bouncing then it is a desirable outcome.
But of course, we can abort the PR as well.

self.runtime.chain_id(),
self.runtime.application_creator_chain_id(),
Expand Down Expand Up @@ -93,7 +98,10 @@ impl Contract for AmmContract {
self.transfer(owner, input_amount, amm_account, input_token_idx);

let amm_app_owner = self.get_amm_app_owner();
let message_origin_account = self.get_message_origin_account(owner);
let message_origin_account = Account {
chain_id: origin,
owner,
};
self.transfer(
amm_app_owner,
output_amount,
Expand Down Expand Up @@ -180,7 +188,10 @@ impl Contract for AmmContract {
}

let amm_account = self.get_amm_account();
let message_origin_account = self.get_message_origin_account(owner);
let message_origin_account = Account {
chain_id: origin,
owner,
};
// See if we'll need to send refunds
if token0_amount < max_token0_amount {
self.transfer(
Expand Down Expand Up @@ -272,7 +283,10 @@ impl Contract for AmmContract {
self.get_shares(other_amount, token_to_remove_amount, &balance0_bigint)
};

let message_origin_account = self.get_message_origin_account(owner);
let message_origin_account = Account {
chain_id: origin,
owner,
};
let current_shares = self
.current_shares_or_default(&message_origin_account)
.await;
Expand All @@ -295,7 +309,10 @@ impl Contract for AmmContract {
.check_account_permission(owner)
.expect("Permission for RemoveAllAddedLiquidity message");

let message_origin_account = self.get_message_origin_account(owner);
let message_origin_account = Account {
chain_id: origin,
owner,
};
let current_shares = self
.current_shares_or_default(&message_origin_account)
.await;
Expand Down Expand Up @@ -444,16 +461,6 @@ impl AmmContract {
}
}

fn get_message_origin_account(&mut self, owner: AccountOwner) -> Account {
Account {
chain_id: self
.runtime
.message_origin_chain_id()
.expect("Getting message origin chain ID should not fail"),
owner,
}
}

fn get_account_on_amm_chain(&mut self, owner: AccountOwner) -> Account {
Account {
chain_id: self.get_amm_chain_id(),
Expand Down
4 changes: 2 additions & 2 deletions examples/call-evm-counter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use call_evm_counter::{CallCounterAbi, CallCounterOperation};
use linera_sdk::{
abis::evm::EvmAbi,
linera_base_types::{ApplicationId, WithContractAbi},
Contract, ContractRuntime,
ChainId, Contract, ContractRuntime,
};

pub struct CallCounterContract {
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Contract for CallCounterContract {
}
}

async fn execute_message(&mut self, _message: ()) {
async fn execute_message(&mut self, _is_bouncing: bool, _origin: ChainId, _message: ()) {
panic!("Counter application doesn't support any cross-chain messages");
}

Expand Down
7 changes: 4 additions & 3 deletions examples/counter-no-graphql/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use counter_no_graphql::{CounterNoGraphQlAbi, CounterOperation};
use linera_sdk::{
linera_base_types::WithContractAbi,
views::{RootView, View},
Contract, ContractRuntime,
ChainId, Contract, ContractRuntime,
};

use self::state::CounterState;
Expand Down Expand Up @@ -53,7 +53,7 @@ impl Contract for CounterContract {
new_value
}

async fn execute_message(&mut self, _message: ()) {
async fn execute_message(&mut self, _is_bouncing: bool, _chain_id: ChainId, _message: ()) {
panic!("Counter application doesn't support any cross-chain messages");
}

Expand Down Expand Up @@ -94,8 +94,9 @@ mod tests {
let initial_value = 72_u64;
let mut counter = create_and_instantiate_counter(initial_value);

use linera_sdk::linera_base_types::ChainId;
counter
.execute_message(())
.execute_message(false, ChainId::default(), ())
.now_or_never()
.expect("Execution of counter operation should not await anything");
}
Expand Down
8 changes: 4 additions & 4 deletions examples/counter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use counter::CounterAbi;
use linera_sdk::{
linera_base_types::WithContractAbi,
views::{RootView, View},
Contract, ContractRuntime,
ChainId, Contract, ContractRuntime,
};

use self::state::CounterState;
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Contract for CounterContract {
new_value
}

async fn execute_message(&mut self, _message: ()) {
async fn execute_message(&mut self, _is_bouncing: bool, _origin: ChainId, _message: ()) {
panic!("Counter application doesn't support any cross-chain messages");
}

Expand All @@ -63,7 +63,7 @@ impl Contract for CounterContract {
#[cfg(test)]
mod tests {
use futures::FutureExt as _;
use linera_sdk::{util::BlockingWait, views::View, Contract, ContractRuntime};
use linera_sdk::{util::BlockingWait, views::View, ChainId, Contract, ContractRuntime};

use super::{CounterContract, CounterState};

Expand Down Expand Up @@ -92,7 +92,7 @@ mod tests {
let mut counter = create_and_instantiate_counter(initial_value);

counter
.execute_message(())
.execute_message(false, ChainId::default(), ())
.now_or_never()
.expect("Execution of counter operation should not await anything");
}
Expand Down
4 changes: 2 additions & 2 deletions examples/create-and-call/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use create_and_call::{CreateAndCallAbi, CreateAndCallOperation};
use linera_sdk::{
linera_base_types::{Bytecode, VmRuntime, WithContractAbi},
views::{RootView, View},
Contract, ContractRuntime,
ChainId, Contract, ContractRuntime,
};

use self::state::CreateAndCallState;
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Contract for CreateAndCallContract {
.call_application(true, application_id, &counter_operation)
}

async fn execute_message(&mut self, _message: ()) {
async fn execute_message(&mut self, _is_bouncing: bool, _origin: ChainId, _message: ()) {
panic!("Create and call application doesn't support any cross-chain messages");
}

Expand Down
4 changes: 2 additions & 2 deletions examples/crowd-funding/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use linera_sdk::{
abis::fungible::FungibleOperation,
linera_base_types::{AccountOwner, Amount, ApplicationId, WithContractAbi},
views::{RootView, View},
Contract, ContractRuntime,
ChainId, Contract, ContractRuntime,
};
use state::{CrowdFundingState, Status};

Expand Down Expand Up @@ -66,7 +66,7 @@ impl Contract for CrowdFundingContract {
}
}

async fn execute_message(&mut self, message: Message) {
async fn execute_message(&mut self, _is_bouncing: bool, _origin: ChainId, message: Message) {
match message {
Message::PledgeWithAccount { owner, amount } => {
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions examples/ethereum-tracker/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ethereum_tracker::{EthereumTrackerAbi, InstantiationArgument};
use linera_sdk::{
linera_base_types::WithContractAbi,
views::{RootView, View},
Contract, ContractRuntime,
ChainId, Contract, ContractRuntime,
};

use self::state::EthereumTrackerState;
Expand Down Expand Up @@ -66,7 +66,7 @@ impl Contract for EthereumTrackerContract {
}
}

async fn execute_message(&mut self, _message: ()) {
async fn execute_message(&mut self, _is_bouncing: bool, _origin: ChainId, _message: ()) {
panic!("Messages not supported");
}

Expand Down
8 changes: 2 additions & 6 deletions examples/fungible/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use fungible::{
use linera_sdk::{
linera_base_types::{AccountOwner, Amount, WithContractAbi},
views::{RootView, View},
Contract, ContractRuntime,
ChainId, Contract, ContractRuntime,
};

use self::state::FungibleTokenState;
Expand Down Expand Up @@ -124,17 +124,13 @@ impl Contract for FungibleTokenContract {
}
}

async fn execute_message(&mut self, message: Message) {
async fn execute_message(&mut self, is_bouncing: bool, _chain_id: ChainId, message: Message) {
match message {
Message::Credit {
amount,
target,
source,
} => {
let is_bouncing = self
.runtime
.message_is_bouncing()
.expect("Message delivery status has to be available when executing a message");
let receiver = if is_bouncing { source } else { target };
self.state.credit(receiver, amount).await;
}
Expand Down
8 changes: 2 additions & 6 deletions examples/gen-nft/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use gen_nft::{GenNftAbi, Message, Nft, Operation, TokenId};
use linera_sdk::{
linera_base_types::{AccountOwner, WithContractAbi},
views::{RootView, View},
Contract, ContractRuntime,
ChainId, Contract, ContractRuntime,
};

use self::state::GenNftState;
Expand Down Expand Up @@ -91,16 +91,12 @@ impl Contract for GenNftContract {
}
}

async fn execute_message(&mut self, message: Message) {
async fn execute_message(&mut self, is_bouncing: bool, _origin: ChainId, message: Message) {
match message {
Message::Transfer {
mut nft,
target_account,
} => {
let is_bouncing = self
.runtime
.message_is_bouncing()
.expect("Message delivery status has to be available when executing a message");
if !is_bouncing {
nft.owner = target_account.owner;
}
Expand Down
5 changes: 2 additions & 3 deletions examples/hex-game/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Contract for HexContract {
self.handle_winner(outcome)
}

async fn execute_message(&mut self, message: Message) {
async fn execute_message(&mut self, _is_bouncing: bool, origin: ChainId, message: Message) {
log::trace!("Handling message {:?}", message);
match message {
Message::Start {
Expand All @@ -80,15 +80,14 @@ impl Contract for HexContract {
self.state.board.set(Board::new(board_size));
}
Message::End { winner, loser } => {
let origin_chain_id = self.runtime.message_origin_chain_id().unwrap();
for owner in [&winner, &loser] {
let chain_set = self
.state
.game_chains
.get_mut_or_default(owner)
.await
.unwrap();
chain_set.retain(|game_chain| game_chain.chain_id != origin_chain_id);
chain_set.retain(|game_chain| game_chain.chain_id != origin);
if chain_set.is_empty() {
self.state.game_chains.remove(owner).unwrap();
}
Expand Down
6 changes: 4 additions & 2 deletions examples/how-to/perform-http-requests/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#![cfg_attr(target_arch = "wasm32", no_main)]

use how_to_perform_http_requests::{Abi, Operation};
use linera_sdk::{http, linera_base_types::WithContractAbi, Contract as _, ContractRuntime};
use linera_sdk::{
http, linera_base_types::WithContractAbi, ChainId, Contract as _, ContractRuntime,
};

pub struct Contract {
runtime: ContractRuntime<Self>,
Expand Down Expand Up @@ -41,7 +43,7 @@ impl linera_sdk::Contract for Contract {
}
}

async fn execute_message(&mut self, (): Self::Message) {
async fn execute_message(&mut self, _is_bouncing: bool, _origin: ChainId, (): Self::Message) {
panic!("This application doesn't support any cross-chain messages");
}

Expand Down
4 changes: 2 additions & 2 deletions examples/llm/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#![cfg_attr(target_arch = "wasm32", no_main)]

use linera_sdk::{linera_base_types::WithContractAbi, Contract, ContractRuntime};
use linera_sdk::{linera_base_types::WithContractAbi, ChainId, Contract, ContractRuntime};

pub struct LlmContract;

Expand All @@ -27,7 +27,7 @@ impl Contract for LlmContract {

async fn execute_operation(&mut self, _operation: ()) -> Self::Response {}

async fn execute_message(&mut self, _message: ()) {
async fn execute_message(&mut self, _is_bouncing: bool, _chain_id: ChainId, _message: ()) {
panic!("Llm application doesn't support any cross-chain messages");
}

Expand Down
7 changes: 2 additions & 5 deletions examples/matching-engine/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Contract for MatchingEngineContract {
}

/// Execution of the order on the creation chain
async fn execute_message(&mut self, message: Message) {
async fn execute_message(&mut self, _is_bouncing: bool, origin: ChainId, message: Message) {
assert_eq!(
self.runtime.chain_id(),
self.runtime.application_creator_chain_id(),
Expand All @@ -118,13 +118,10 @@ impl Contract for MatchingEngineContract {
match message {
Message::ExecuteOrder { order } => {
let owner = Self::get_owner(&order);
let origin_chain_id = self.runtime.message_origin_chain_id().expect(
"Incoming message origin chain ID has to be available when executing a message",
);
self.runtime
.check_account_permission(owner)
.expect("Permission for ExecuteOrder message");
self.execute_order_local(order, origin_chain_id).await;
self.execute_order_local(order, origin).await;
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions examples/meta-counter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use linera_sdk::{
linera_base_types::{ApplicationId, StreamName, WithContractAbi},
Contract, ContractRuntime, Resources,
ChainId, Contract, ContractRuntime, Resources,
};
use meta_counter::{Message, MetaCounterAbi, Operation};

Expand Down Expand Up @@ -79,11 +79,7 @@ impl Contract for MetaCounterContract {
message.send_to(recipient_id);
}

async fn execute_message(&mut self, message: Message) {
let is_bouncing = self
.runtime
.message_is_bouncing()
.expect("Message delivery status has to be available when executing a message");
async fn execute_message(&mut self, is_bouncing: bool, _chain_id: ChainId, message: Message) {
if is_bouncing {
log::trace!("receiving a bouncing message {message:?}");
return;
Expand Down
7 changes: 6 additions & 1 deletion examples/native-fungible/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ impl Contract for NativeFungibleTokenContract {
}
}

async fn execute_message(&mut self, message: Self::Message) {
async fn execute_message(
&mut self,
_is_bouncing: bool,
_origin: ChainId,
message: Self::Message,
) {
// Messages for now don't do anything, just pass messages around
match message {
Message::Notify => (),
Expand Down
Loading
Loading