Skip to content

Commit

Permalink
Test that events get added.
Browse files Browse the repository at this point in the history
  • Loading branch information
afck committed Jul 15, 2024
1 parent 48bc1a7 commit d87aa47
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 11 deletions.
4 changes: 3 additions & 1 deletion examples/meta-counter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![cfg_attr(target_arch = "wasm32", no_main)]

use linera_sdk::{
base::{ApplicationId, WithContractAbi},
base::{ApplicationId, StreamName, WithContractAbi},
Contract, ContractRuntime, Resources,
};
use meta_counter::{Message, MetaCounterAbi, Operation};
Expand Down Expand Up @@ -40,6 +40,8 @@ impl Contract for MetaCounterContract {
// Send a no-op message to ourselves. This is only for testing contracts that send messages
// on initialization. Since the value is 0 it does not change the counter value.
let this_chain = self.runtime.chain_id();
self.runtime
.emit(StreamName(b"announcements".to_vec()), b"instantiated");
self.runtime.send_message(this_chain, Message::Increment(0));
}

Expand Down
2 changes: 1 addition & 1 deletion linera-base/src/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ pub struct ChannelName(#[serde(with = "serde_bytes")] Vec<u8>);
WitStore,
WitType,
)]
pub struct StreamName(#[serde(with = "serde_bytes")] Vec<u8>);
pub struct StreamName(#[serde(with = "serde_bytes")] pub Vec<u8>);

/// The destination of a message, relative to a particular application.
#[derive(
Expand Down
19 changes: 16 additions & 3 deletions linera-core/src/unit_tests/wasm_client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ use async_graphql::Request;
use counter::CounterAbi;
use linera_base::{
data_types::{Amount, HashedBlob, OracleResponse},
identifiers::{AccountOwner, ApplicationId, ChainDescription, ChainId, Destination, Owner},
identifiers::{
AccountOwner, ApplicationId, ChainDescription, ChainId, Destination, Owner, StreamName,
},
ownership::{ChainOwnership, TimeoutConfig},
};
use linera_chain::data_types::{CertificateValue, MessageAction, OutgoingMessage};
use linera_chain::data_types::{CertificateValue, EventRecord, MessageAction, OutgoingMessage};
use linera_execution::{
Bytecode, Message, MessageKind, Operation, ResourceControlPolicy, SystemMessage,
UserApplicationDescription, WasmRuntime,
Expand Down Expand Up @@ -367,7 +369,7 @@ where
.await
.unwrap()
.unwrap();
let (application_id2, _) = creator
let (application_id2, certificate) = creator
.create_application(
bytecode_id2,
&application_id1,
Expand All @@ -377,6 +379,17 @@ where
.await
.unwrap()
.unwrap();
assert_eq!(
certificate.value().executed_block().unwrap().outcome.events,
vec![
Vec::new(),
vec![EventRecord {
application_id: application_id2.forget_abi().into(),
stream_name: StreamName(b"announcements".to_vec()),
payload: b"instantiated".to_vec(),
}]
]
);

let mut operation = meta_counter::Operation::increment(receiver_id, 5);
operation.fuel_grant = 1000000;
Expand Down
2 changes: 1 addition & 1 deletion linera-core/src/unit_tests/wasm_worker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ where
message: Message::System(SystemMessage::ApplicationCreated),
}],
],
events: vec![Vec::new()],
events: vec![Vec::new(); 2],
state_hash: creator_state.crypto_hash().await?,
oracle_responses: vec![Vec::new(); 2],
}
Expand Down
4 changes: 2 additions & 2 deletions linera-core/src/unit_tests/worker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ where
Amount::from_tokens(2),
)],
],
events: vec![Vec::new(); 3],
events: vec![Vec::new(); 2],
state_hash: SystemExecutionState {
committees: [(epoch, committee.clone())].into_iter().collect(),
ownership: ChainOwnership::single(sender_key_pair.public()),
Expand Down Expand Up @@ -2331,7 +2331,7 @@ where
},
),
]],
events: vec![Vec::new(); 2],
events: vec![Vec::new()],
state_hash: SystemExecutionState {
committees: committees.clone(),
ownership: ChainOwnership::single(key_pair.public()),
Expand Down
1 change: 1 addition & 0 deletions linera-explorer/src/components/Block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ test('Block mounting', () => {
}
}
}]],
events: [[]],
stateHash: "5bcd40995283e74798c60e8dc7a93e8c61059440534070673dfb973b2b66f61a",
oracleResponses: []
}
Expand Down
1 change: 1 addition & 0 deletions linera-explorer/src/components/Blocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ test('Blocks mounting', () => {
}
}
}]],
events: [[]],
stateHash: "5bcd40995283e74798c60e8dc7a93e8c61059440534070673dfb973b2b66f61a",
oracleResponses: []
}
Expand Down
8 changes: 8 additions & 0 deletions linera-sdk/src/contract/test_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use linera_base::{
data_types::{Amount, BlockHeight, HashedBlob, Resources, SendMessageRequest, Timestamp},
identifiers::{
Account, ApplicationId, BlobId, ChainId, ChannelName, Destination, MessageId, Owner,
StreamName,
},
ownership::{ChainOwnership, CloseChainError},
};
Expand Down Expand Up @@ -43,6 +44,7 @@ where
subscribe_requests: Vec<(ChainId, ChannelName)>,
unsubscribe_requests: Vec<(ChainId, ChannelName)>,
outgoing_transfers: HashMap<Account, Amount>,
events: Vec<(StreamName, Vec<u8>)>,
claim_requests: Vec<ClaimRequest>,
expected_service_queries: VecDeque<(ApplicationId, String, String)>,
expected_post_requests: VecDeque<(String, Vec<u8>, Vec<u8>)>,
Expand Down Expand Up @@ -84,6 +86,7 @@ where
subscribe_requests: Vec::new(),
unsubscribe_requests: Vec::new(),
outgoing_transfers: HashMap::new(),
events: Vec::new(),
claim_requests: Vec::new(),
expected_service_queries: VecDeque::new(),
expected_post_requests: VecDeque::new(),
Expand Down Expand Up @@ -586,6 +589,11 @@ where
.expect("Failed to deserialize `Response` type from cross-application call")
}

/// Adds a new item to an event stream.
pub fn emit(&mut self, name: StreamName, payload: &[u8]) {
self.events.push((name, payload.to_vec()));
}

/// Adds an expected `query_service` call`, and the response it should return in the test.
pub fn add_expected_service_query<A: ServiceAbi + Send>(
&mut self,
Expand Down
10 changes: 10 additions & 0 deletions linera-service-graphql-client/gql/service_requests.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ query Block($hash: CryptoHash, $chainId: ChainId!) {
}
stateHash
oracleResponses
events {
applicationId
streamName
payload
}
}
}
}
Expand Down Expand Up @@ -219,6 +224,11 @@ query Blocks($from: CryptoHash, $chainId: ChainId!, $limit: Int) {
}
stateHash
oracleResponses
events {
applicationId
streamName
payload
}
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions linera-service-graphql-client/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use graphql_client::GraphQLQuery;
use linera_base::{
crypto::CryptoHash,
data_types::{Amount, BlockHeight, OracleResponse, Timestamp},
identifiers::{Account, ChainDescription, ChainId, ChannelName, Destination, Owner},
identifiers::{
Account, ChainDescription, ChainId, ChannelName, Destination, GenericApplicationId, Owner,
StreamName,
},
};

pub type JSONObject = serde_json::Value;
Expand Down Expand Up @@ -130,7 +133,7 @@ pub struct Transfer;
#[cfg(not(target_arch = "wasm32"))]
mod from {
use linera_chain::data_types::{
BlockExecutionOutcome, ExecutedBlock, HashedCertificateValue, IncomingMessage,
BlockExecutionOutcome, EventRecord, ExecutedBlock, HashedCertificateValue, IncomingMessage,
OutgoingMessage,
};

Expand Down Expand Up @@ -210,6 +213,7 @@ mod from {
messages,
state_hash,
oracle_responses,
events,
},
} = val;
let messages = messages
Expand All @@ -222,12 +226,25 @@ mod from {
messages,
state_hash,
oracle_responses: oracle_responses.into_iter().map(Into::into).collect(),
events: vec![], // events.into_iter().map(Into::into).collect(),
events: events
.into_iter()
.map(|events| events.into_iter().map(Into::into).collect())
.collect(),
},
}
}
}

impl From<block::BlockBlockValueExecutedBlockOutcomeEvents> for EventRecord {
fn from(event: block::BlockBlockValueExecutedBlockOutcomeEvents) -> Self {
EventRecord {
application_id: event.application_id,
stream_name: event.stream_name,
payload: event.payload.into_iter().map(|byte| byte as u8).collect(),
}
}
}

impl TryFrom<block::BlockBlock> for HashedCertificateValue {
type Error = String;
fn try_from(val: block::BlockBlock) -> Result<Self, Self::Error> {
Expand Down

0 comments on commit d87aa47

Please sign in to comment.