Skip to main content

ContextClient

Struct ContextClient 

Source
pub struct ContextClient { /* private fields */ }
Expand description

A client for interacting with the context management system.

This struct serves as the primary public API, providing methods to create, join, query, and manage contexts and their members. It orchestrates interactions between the datastore, background actors, and external networks.

Implementations§

Source§

impl ContextClient

Source

pub fn new_identity(&self) -> Result<PublicKey>

Creates a new cryptographic identity (key pair) and stores it in the datastore. The private key is randomly generated. The new identity doesn’t have any sender_key. If needed, the sender_key could be set via update_identity() method later.

§Note

This identity is not initially tied to a specific context (it is stored under a zeroed-out ContextId). It can be seen as a “global” identity within the node that can later be associated with one or more contexts.

§Returns

A Result containing the PublicKey of the newly created identity.

§Errors

Returns an error if there is an issue writing the new identity to the datastore.

Source

pub fn get_identity( &self, context_id: &ContextId, public_key: &PublicKey, ) -> Result<Option<ContextIdentity>>

Retrieves an identity from the datastore for a given context.

§Arguments
  • context_id - The context in which the identity is being retrieved.
  • public_key - The public key of the identity to fetch.
§Returns

An Option containing the ContextIdentity if found, otherwise None.

§Errors

Returns an error if there is an issue reading from the datastore.

Source

pub fn update_identity( &self, context_id: &ContextId, new_identity: &ContextIdentity, ) -> Result<()>

Updates an existing identity in the datastore.

This is typically used to add or change the sender_key or private_key for an identity that the node already knows about.

§Arguments
  • context_id - The context of the identity to update.
  • new_identity - The ContextIdentity object containing the updated fields.
§Errors

Returns an error if the identity does not exist or if there is a datastore issue.

Source

pub fn delete_identity( &self, context_id: &ContextId, public_key: &PublicKey, ) -> Result<()>

Deletes an identity from the datastore for a given context.

§Arguments
  • context_id - The context from which to delete the identity.
  • public_key - The public key of the identity to delete.
§Errors

Returns an error if there is an issue writing to the datastore.

Source§

impl ContextClient

Source

pub fn context_config( &self, context_id: &ContextId, ) -> Result<Option<ContextConfigParams<'static>>>

Source§

impl ContextClient

Source

pub const fn external_client<'a>( &'a self, context_id: &ContextId, config: &'a ContextConfigParams<'a>, ) -> Result<ExternalClient<'a>>

Source§

impl ContextClient

Source

pub async fn sync_context_config( &self, context_id: ContextId, config: Option<ContextConfigParams<'_>>, ) -> Result<Context>

Source§

impl ContextClient

Source

pub const fn new( datastore: Store, node_client: NodeClient, external_client: ExternalClient<AnyTransport>, context_manager: LazyRecipient<ContextMessage>, ) -> Self

Source

pub fn datastore_handle(&self) -> Handle<Store>

Returns a handle to the datastore for direct access. Used by node components that need to read stored data.

Source

pub async fn create_context( &self, protocol: String, application_id: &ApplicationId, identity_secret: Option<PrivateKey>, init_params: Vec<u8>, seed: Option<[u8; 32]>, ) -> Result<CreateContextResponse>

Sends a request to create a new context.

This operation is asynchronous and is handled by the ContextManager actor.

§Arguments
  • protocol - The name of the protocol that will be used for the new context.
  • application_id - The ID of the application that will run in the context.
  • identity_secret - An optional private key to use for the initial identity. If not provided, a new identity will be generated.
  • init_params - Raw byte parameters for initializing the application state.
  • seed - An optional 32-byte seed for deterministic context ID and identity creation.
§Returns

A Result containing the CreateContextResponse from the actor upon completion.

Source

pub async fn invite_member( &self, context_id: &ContextId, inviter_id: &PublicKey, invitee_id: &PublicKey, ) -> Result<Option<ContextInvitationPayload>>

Invites a new member to an existing context.

This involves an external call to the on-chain contract to register the new member.

§Arguments
  • context_id - The context to invite the member to.
  • inviter_id - The public key of an existing member who is performing the invitation.
  • invitee_id - The public key of the identity being invited.
§Returns
  • A Result containing an Option with the shareable ContextInvitationPayload.
  • Returns Ok(None) if the context configuration cannot be found locally.
Source

pub async fn invite_member_by_open_invitation( &self, context_id: &ContextId, inviter_id: &PublicKey, valid_for_blocks: BlockHeight, _secret_salt: [u8; 32], ) -> Result<Option<SignedOpenInvitation>>

Creates and signs a one-time, expiring open invitation for a new member.

This method allows an existing member of a context (the inviter) to generate a shareable invitation. The method fetches the inviter’s private key managed by the local node, signs the invitation details, and returns the resulting payload and signature.

§Arguments
  • context_id - The context to invite the new member to.
  • inviter_id - The public key of the existing member creating the invitation. This node must have the corresponding private key for this identity.
  • valid_for_blocks - A number of blocks from the current block height for which the invitation is considered to be valid.
  • secret_salt - A 32-byte random value to ensure the invitation is unique.
§Returns
  • A Result containing the SignedOpenInvitation if successful, or an error if the inviter’s private key is not found or signing fails.
  • Returns Ok(None) if the context configuration cannot be found locally.
Source

pub async fn join_context( &self, invitation_payload: ContextInvitationPayload, ) -> Result<JoinContextResponse>

Sends a request to join a context using an invitation payload.

This is an asynchronous operation handled by the ContextManager actor. The actor will parse the payload, validate the information, and configure the local node to participate in the specified context.

§Arguments
  • invitation_payload - The opaque ContextInvitationPayload received from an inviter.
§Returns

A Result containing the JoinContextResponse from the actor upon completion.

Source

pub async fn join_context_by_open_invitation( &self, signed_invitation: SignedOpenInvitation, new_member_public_key: &PublicKey, ) -> Result<Option<JoinContextResponse>>

Sends a request to join a context using the new commit-reveal open invitation flow.

This is an asynchronous operation handled by the ContextManager actor. The actor will parse the payload, validate the information, and configure the local node to participate in the specified context.

§Arguments
  • invitation_payload - The opaque ContextInvitationPayload received from an inviter.
§Returns
  • A Result containing the JoinContextResponse from the actor upon completion.
  • Returns Ok(None) if the context configuration cannot be found locally.
Source

pub fn has_context(&self, context_id: &ContextId) -> Result<bool>

Checks if a context’s metadata exists in the local datastore.

§Arguments
  • context_id - The ID of the context to check for.
§Returns

A Result containing true if the context exists locally, false otherwise.

Source

pub fn get_context(&self, context_id: &ContextId) -> Result<Option<Context>>

Retrieves a context metadata from the local datastore.

§Arguments
  • context_id - The ID of the context to retrieve.
§Returns

A Result containing Some(Context) if the context is found, or None if it is not.

Source

pub fn update_dag_heads( &self, context_id: &ContextId, dag_heads: Vec<[u8; 32]>, ) -> Result<()>

Updates the DAG heads for a context after applying a delta.

§Arguments
  • context_id - The ID of the context to update.
  • dag_heads - The new DAG heads (typically the delta ID that was just applied).
§Returns

A Result indicating success or failure.

Source

pub fn update_context_application_id( &self, context_id: &ContextId, application_id: ApplicationId, ) -> Result<()>

Updates the ApplicationId for a context.

§Arguments
  • context_id - The ID of the context to update.
  • application_id - The new ApplicationId.
§Returns

A Result indicating success or failure.

Source

pub fn compute_root_hash(&self, context_id: &ContextId) -> Result<[u8; 32]>

Computes the actual root hash from storage by reading the root Index entry.

This reads the EntityIndex for Id::root() from RocksDB and extracts the Merkle full_hash. This is the authoritative hash computed from the actual state, not a claimed value.

§Arguments
  • context_id - The ID of the context to compute the root hash for.
§Returns

The computed root hash, or [0; 32] if no root index exists (empty state).

Source

pub fn force_root_hash( &self, context_id: &ContextId, root_hash: Hash, ) -> Result<()>

Forces the root hash for a context to a specific value.

WARNING: This bypasses verification and should only be used when the hash has already been verified or during controlled operations. Prefer compute_root_hash + set_root_hash for safety.

§Arguments
  • context_id - The ID of the context to update.
  • root_hash - The root hash to set.
§Returns

A Result indicating success or failure.

Source

pub fn verify_root_hash( &self, context_id: &ContextId, claimed_hash: [u8; 32], ) -> Result<()>

Verifies that the stored root hash matches the actual state.

Computes the root hash from storage and compares with the claimed hash. Returns Ok(()) if they match, or an error describing the mismatch.

§Arguments
  • context_id - The ID of the context to verify.
  • claimed_hash - The hash to verify against.
§Returns

A Result indicating success if hashes match, or an error if they don’t.

Source

pub fn get_context_ids( &self, start: Option<ContextId>, ) -> impl Stream<Item = Result<ContextId>>

Returns a stream of all context IDs stored locally.

§Arguments
  • start - An optional ContextId from which to begin the stream. If None, the stream starts from the beginning.
§Returns

An implementation of Stream that yields Result<ContextId>.

Source

pub fn has_member( &self, context_id: &ContextId, public_key: &PublicKey, ) -> Result<bool>

Checks if a given public key is a member of a context in the local datastore.

§Arguments
  • context_id - The context to check within.
  • public_key - The public key of the potential member.
§Returns

A Result containing true if the identity is a known member, false otherwise.

Source

pub fn get_context_members( &self, context_id: &ContextId, owned: Option<bool>, ) -> impl Stream<Item = Result<(PublicKey, bool)>>

Retrieves and returns a stream of all members of a given context.

§Arguments
  • context_id - The context to query for members.
  • owned - If Some(true), the stream returns only members for which this node holds the private key. If Some(false) or None, it returns all members.
§Returns

A stream of tuples (PublicKey, bool), where the boolean indicates if the identity is owned.

Source

pub async fn execute( &self, context_id: &ContextId, executor: &PublicKey, method: String, payload: Vec<u8>, aliases: Vec<Alias<PublicKey>>, atomic: Option<ContextAtomic>, ) -> Result<ExecuteResponse, ExecuteError>

Sends a request to execute a method within a context.

This is the primary way to interact with the application running inside a context. The request is handled asynchronously by the ContextManager actor.

§Arguments
  • context_id - The ID of the context where the execution should occur.
  • executor - The public key of the identity performing the execution. The executor must be a member of the context.
  • method - The string name of the application method to call.
  • payload - The input data (e.g., serialized JSON) for the method.
  • aliases - A list of public key aliases to use for this specific execution.
  • atomic - An optional handle for batching multiple executions into an atomic transaction.
§Returns

A Result containing the ExecuteResponse on success, or an ExecuteError on failure.

Source

pub async fn update_application( &self, context_id: &ContextId, application_id: &ApplicationId, identity: &PublicKey, migrate_method: Option<String>, ) -> Result<()>

Sends a request to update the application for a given context. This is an asynchronous operation handled by the ContextManager actor.

§Arguments
  • context_id - The ID of the context where to update the application.
  • application_id - The ID of the new application to switch to.
  • identity - The public key of the member authorizing the update.
  • migrate_method - Optional name of the migration function to execute.
§Returns

An empty Result indicating the outcome of the application update request.

Source

pub async fn delete_context( &self, context_id: &ContextId, ) -> Result<DeleteContextResponse>

Sends a request to delete a context from the local node. This is an asynchronous operation handled by the ContextManager actor. It will remove all associated data for the context from the local datastore.

§Arguments
  • context_id - The ID of the context to delete.
§Returns

A Result containing the DeleteContextResponse from the actor.

Trait Implementations§

Source§

impl Clone for ContextClient

Source§

fn clone(&self) -> ContextClient

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ContextClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoResult<T> for T

Source§

impl<T> Pipe for T
where T: ?Sized,

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
Source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
Source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Reflect for T

Source§

fn as_dyn_ref<'a>(&self) -> &(dyn Reflect + 'a)
where T: 'a,

Source§

fn as_dyn_mut<'a>(&mut self) -> &mut (dyn Reflect + 'a)
where T: 'a,

Source§

fn as_dyn_box<'a>(self: Box<T>) -> Box<dyn Reflect + 'a>
where T: 'a,

Source§

fn as_dyn_rc<'a>(self: Rc<T>) -> Rc<dyn Reflect + 'a>
where T: 'a,

Source§

fn as_dyn_arc<'a>(self: Arc<T>) -> Arc<dyn Reflect + 'a>
where T: 'a,

Source§

fn type_id(&self) -> TypeId

Source§

fn type_name(&self) -> &'static str

Source§

impl<T> ReflectExt for T
where T: Reflect + ?Sized,

Source§

fn is<T>(&self) -> bool
where T: Reflect + ?Sized,

Source§

fn type_id() -> TypeId

Source§

fn downcast_ref<T>(&self) -> Option<&T>
where T: Reflect,

Source§

fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: Reflect,

Source§

fn downcast_box<T>(self: Box<Self>) -> Result<Box<T>, Box<Self>>
where T: Reflect,

Source§

fn downcast_rc<T>(self: Rc<Self>) -> Result<Rc<T>, Rc<Self>>
where T: Reflect,

Source§

fn downcast_arc<T>(self: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
where T: Reflect,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more