Skip to content
Merged
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
17 changes: 10 additions & 7 deletions rust-macros/src/delegate_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl ImplStruct {
quote! {
#[no_mangle]
#[cfg(feature = "freenet-main-delegate")]
pub extern "C" fn process(parameters: i64, attested: i64, inbound: i64) -> #ret {
pub extern "C" fn process(parameters: i64, origin: i64, inbound: i64) -> #ret {
#set_logger
let parameters = unsafe {
let param_buf = &*(parameters as *const ::freenet_stdlib::memory::buf::BufferBuilder);
Expand All @@ -38,16 +38,19 @@ impl ImplStruct {
);
Parameters::from(bytes)
};
let attested = unsafe {
let attested_buf = &*(attested as *const ::freenet_stdlib::memory::buf::BufferBuilder);
let origin: Option<::freenet_stdlib::prelude::MessageOrigin> = unsafe {
let origin_buf = &*(origin as *const ::freenet_stdlib::memory::buf::BufferBuilder);
let bytes = &*std::ptr::slice_from_raw_parts(
attested_buf.start(),
attested_buf.bytes_written(),
origin_buf.start(),
origin_buf.bytes_written(),
);
if bytes.is_empty() {
None
} else {
Some(bytes)
match ::freenet_stdlib::prelude::bincode::deserialize(bytes) {
Ok(v) => Some(v),
Err(_) => None,
}
}
};
let inbound = unsafe {
Expand All @@ -70,7 +73,7 @@ impl ImplStruct {
let result = <#type_name as ::freenet_stdlib::prelude::DelegateInterface>::process(
&mut ctx,
parameters,
attested,
origin,
inbound
);
::freenet_stdlib::prelude::DelegateInterfaceResult::from(result).into_raw()
Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "freenet-stdlib"
version = "0.2.2"
version = "0.3.0"
edition = "2021"
rust-version = "1.80"
publish = true
Expand Down
8 changes: 0 additions & 8 deletions rust/src/client_api/client_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,19 +1443,11 @@ impl HostResponse {
let mut messages: Vec<WIPOffset<FbsOutboundDelegateMsg>> = Vec::new();
values.iter().for_each(|msg| match msg {
OutboundDelegateMsg::ApplicationMessage(app) => {
let instance_data = builder.create_vector(key.bytes());
let instance_offset = FbsContractInstanceId::create(
&mut builder,
&ContractInstanceIdArgs {
data: Some(instance_data),
},
);
let payload_data = builder.create_vector(&app.payload);
let delegate_context_data = builder.create_vector(app.context.as_ref());
let app_offset = FbsApplicationMessage::create(
&mut builder,
&ApplicationMessageArgs {
app: Some(instance_offset),
payload: Some(payload_data),
context: Some(delegate_context_data),
processed: app.processed,
Expand Down
30 changes: 18 additions & 12 deletions rust/src/delegate_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::common_generated::common::SecretsId as FbsSecretsId;

use crate::client_api::{TryFromFbs, WsApiError};
use crate::contract_interface::{RelatedContracts, UpdateData};
use crate::prelude::{ContractInstanceId, WrappedState, CONTRACT_KEY_SIZE};
use crate::prelude::{ContractInstanceId, WrappedState};
use crate::versioning::ContractContainer;
use crate::{code_hash::CodeHash, prelude::Parameters};

Expand Down Expand Up @@ -363,6 +363,18 @@ impl<'a> TryFromFbs<&FbsSecretsId<'a>> for SecretsId {
}
}

/// Identifies where an inbound application message originated from.
///
/// When a web app sends a message to a delegate through the WebSocket API with
/// an authentication token, the runtime resolves the token to the originating
/// contract and wraps it in `MessageOrigin::WebApp`. Delegates receive this as
/// the `origin` parameter of [`DelegateInterface::process`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum MessageOrigin {
/// The message was sent by a web application backed by the given contract.
WebApp(ContractInstanceId),
}

/// A Delegate is a webassembly code designed to act as an agent for the user on
/// Freenet. Delegates can:
///
Expand Down Expand Up @@ -391,7 +403,7 @@ impl<'a> TryFromFbs<&FbsSecretsId<'a>> for SecretsId {
/// fn process(
/// ctx: &mut DelegateCtx,
/// _params: Parameters<'static>,
/// _attested: Option<&'static [u8]>,
/// _origin: Option<MessageOrigin>,
/// message: InboundDelegateMsg,
/// ) -> Result<Vec<OutboundDelegateMsg>, DelegateError> {
/// // Access secrets synchronously - no round-trip needed!
Expand All @@ -415,13 +427,13 @@ pub trait DelegateInterface {
/// - **Context** (temporary): `read()`, `write()`, `len()`, `clear()` - state within a batch
/// - **Secrets** (persistent): `get_secret()`, `set_secret()`, `has_secret()`, `remove_secret()`
/// - `parameters`: The delegate's initialization parameters.
/// - `attested`: An optional identifier for the client of this function. Usually
/// will be a [`ContractInstanceId`].
/// - `origin`: An optional [`MessageOrigin`] identifying where the message came from.
/// For messages sent by web applications, this is `MessageOrigin::WebApp(contract_id)`.
/// - `message`: The inbound message to process.
fn process(
ctx: &mut crate::delegate_host::DelegateCtx,
parameters: Parameters<'static>,
attested: Option<&'static [u8]>,
origin: Option<MessageOrigin>,
message: InboundDelegateMsg,
) -> Result<Vec<OutboundDelegateMsg>, DelegateError>;
}
Expand Down Expand Up @@ -555,11 +567,7 @@ impl<'a> TryFromFbs<&FbsInboundDelegateMsg<'a>> for InboundDelegateMsg<'a> {
match msg.inbound_type() {
InboundDelegateMsgType::common_ApplicationMessage => {
let app_msg = msg.inbound_as_common_application_message().unwrap();
let mut instance_key_bytes = [0; CONTRACT_KEY_SIZE];
instance_key_bytes
.copy_from_slice(app_msg.app().data().bytes().to_vec().as_slice());
let app_msg = ApplicationMessage {
app: ContractInstanceId::new(instance_key_bytes),
payload: app_msg.payload().bytes().to_vec(),
context: DelegateContext::new(app_msg.context().bytes().to_vec()),
processed: app_msg.processed(),
Expand All @@ -585,16 +593,14 @@ impl<'a> TryFromFbs<&FbsInboundDelegateMsg<'a>> for InboundDelegateMsg<'a> {
#[non_exhaustive]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ApplicationMessage {
pub app: ContractInstanceId,
pub payload: Vec<u8>,
pub context: DelegateContext,
pub processed: bool,
}

impl ApplicationMessage {
pub fn new(app: ContractInstanceId, payload: Vec<u8>) -> Self {
pub fn new(payload: Vec<u8>) -> Self {
Self {
app,
payload,
context: DelegateContext::default(),
processed: false,
Expand Down
Loading
Loading