Skip to content

Commit

Permalink
Construct chatroom message
Browse files Browse the repository at this point in the history
  • Loading branch information
iduartgomez committed Oct 6, 2023
1 parent 16fd365 commit 66ba406
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 92 deletions.
59 changes: 59 additions & 0 deletions rust/src/composers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,65 @@ impl<T: ComposableContract> From<(Option<T>, Option<T::Delta>)> for TypedUpdateD
}
}

#[allow(unused)]
impl<T: ComposableContract> ComposableContract for Vec<T> {
type Context = T::Context;
type Parameters = T::Parameters;
type Delta = T::Delta;
type Summary = T::Summary;

fn verify<Child, Ctx>(
&self,
parameters: &Self::Parameters,
context: &Ctx,
related: &RelatedContractsContainer,
) -> Result<ValidateResult, ContractError>
where
Child: ComposableContract,
Self::Context: for<'x> From<&'x Ctx>,
{
todo!()
}

fn verify_delta<Child>(
parameters: &Self::Parameters,
delta: &Self::Delta,
) -> Result<bool, ContractError>
where
Child: ComposableContract,
{
todo!()
}

fn merge(
&mut self,
parameters: &Self::Parameters,
update: &TypedUpdateData<Self>,
related: &RelatedContractsContainer,
) -> MergeResult {
todo!()
}

fn summarize<ParentSummary>(
&self,
parameters: &Self::Parameters,
summary: &mut ParentSummary,
) -> Result<(), ContractError>
where
ParentSummary: ComposableSummary<<Self as ComposableContract>::Summary>,
{
todo!()
}

fn delta(
&self,
parameters: &Self::Parameters,
summary: &Self::Summary,
) -> Result<Self::Delta, ContractError> {
todo!()
}
}

pub struct NoContext;

impl<'x, T> From<&'x T> for NoContext {
Expand Down
239 changes: 149 additions & 90 deletions rust/tests/chatroom.rs
Original file line number Diff line number Diff line change
@@ -1,131 +1,190 @@
/*
mod example_2 {
use crate::parameters;
use freenet_stdlib::composers::{ComposableContract, ComposableParameters};

use super::*;
pub struct ChatRoom {
pub members: Vec<dependency_2::ChatRoomMember>,
pub messages: Vec<dependency_1::SignedComposable<dependency_2::ChatRoomMessage>>,
}

pub struct PublicKey {}
pub struct Signature {}
pub struct ChatRoomParameters {
pub owner_public_key: dependency_2::PublicKey,
}

impl<T: ComposableContract> ComposableContract for Vec<T>
impl ComposableParameters for ChatRoomParameters {
fn contract_id(&self) -> Option<freenet_stdlib::prelude::ContractInstanceId> {
unimplemented!()
}
}

impl<'x> From<&'x ChatRoomParameters> for dependency_2::ChatRoomMsgParameters {
fn from(_: &'x ChatRoomParameters) -> Self {
unimplemented!()
}
}

impl<'x> From<&'x ChatRoom> for dependency_2::PublicKey {
fn from(_: &'x ChatRoom) -> Self {
unimplemented!()
}
}

impl ComposableContract for ChatRoom {
type Context = ();
type Parameters = ChatRoomParameters;
type Delta = ();
type Summary = ();

fn verify<Child, Ctx>(
&self,
parameters: &Self::Parameters,
_: &Ctx,
related: &freenet_stdlib::prelude::RelatedContractsContainer,
) -> Result<freenet_stdlib::prelude::ValidateResult, freenet_stdlib::prelude::ContractError>
where
T: ComposableContract,
Child: ComposableContract,
Self::Context: for<'x> From<&'x Ctx>,
{
type Context = T;
type Parameters = T::Parameters;
type Summary = T::Summary;
type Delta = T::Delta;
fn verify(
&self,
parameters: &Self::Parameters,
context: &Self::Context,
related: &RelatedContractsContainer,
) -> Result<(), Box<dyn Error>> {
unimplemented!()
}
self.messages
.verify::<Vec<dependency_1::SignedComposable<dependency_2::ChatRoomMessage>>, _>(
&parameters.into(),
self,
related,
)?;
unimplemented!()
}

pub struct ChatRoomParameters {
pub owner_public_key: PublicKey,
fn verify_delta<Child>(
_: &Self::Parameters,
_: &Self::Delta,
) -> Result<bool, freenet_stdlib::prelude::ContractError>
where
Child: ComposableContract,
{
unimplemented!()
}

impl ComposableParameters for ChatRoomParameters {}
fn merge(
&mut self,
_: &Self::Parameters,
_: &freenet_stdlib::composers::TypedUpdateData<Self>,
_: &freenet_stdlib::prelude::RelatedContractsContainer,
) -> freenet_stdlib::composers::MergeResult {
unimplemented!()
}

/// Contract 0
pub struct ChatRoom {
pub members: Vec<ChatRoomMember>,
pub messages: Vec<SignedSyncable<ChatRoomMessage>>,
fn summarize<ParentSummary>(
&self,
_: &Self::Parameters,
_: &mut ParentSummary,
) -> Result<(), freenet_stdlib::prelude::ContractError>
where
ParentSummary:
freenet_stdlib::composers::ComposableSummary<<Self as ComposableContract>::Summary>,
{
unimplemented!()
}

impl ComposableContract for ChatRoom {
type Context = ();
type Parameters = ChatRoomParameters;
type Summary = ();
type Delta = ();
fn delta(
&self,
_: &Self::Parameters,
_: &Self::Summary,
) -> Result<Self::Delta, freenet_stdlib::prelude::ContractError> {
unimplemented!()
}
}

fn verify(
&self,
parameters: &Self::Parameters,
context: &Self::Context,
related: &RelatedContractsContainer,
) -> Result<(), Box<dyn Error>> {
// automatically generated
{
self.members.verify(&parameters, self, related)?;
}
{
self.messages.verify(&parameters, self, related)?;
}
Ok(())
}
pub mod dependency_1 {
pub struct Signature {}
pub struct SignedComposable<S> {
pub value: S,
pub signature: Signature,
}
}

pub mod dependency_2 {
use freenet_stdlib::composers::{ComposableContract, ComposableParameters};

#[derive(Clone, Copy)]
pub struct PublicKey {}

pub struct ChatRoomMember {
pub name: String,
pub public_key: PublicKey,
}

impl ComposableContract for ChatRoomMember {
type Context = ();
type Parameters = ();
type Summary = ();
type Delta = ();
}
pub struct ChatRoomMessage {
pub message: String,
pub author: String,
pub signature: String,
}

impl ComposableContract for ChatRoomMessage {
type Context = ();
type Parameters = ();
type Summary = ();
type Delta = ();
}
pub struct SignedSyncable<S> {
pub value: S,
pub signature: Signature,
// extractor: EXTRACTOR,
pub struct ChatRoomMsgParameters {
pub owner_public_key: PublicKey,
}

impl<S: ComposableContract> SignedSyncable<S> {}
trait ParametersWithPublicKey: ComposableParameters {
fn public_key(&self) -> PublicKey;
impl ComposableParameters for ChatRoomMsgParameters {
fn contract_id(&self) -> Option<freenet_stdlib::prelude::ContractInstanceId> {
unimplemented!()
}
}

// manually implemented
impl<S: ComposableContract> ComposableContract for SignedSyncable<S> {
type Context = ();
// type Parameters<T: ParametersWithPublicKey>;
type Summary = ();
impl ComposableContract for super::dependency_1::SignedComposable<ChatRoomMessage> {
type Context = PublicKey;
type Parameters = ChatRoomMsgParameters;
type Delta = ();
type Summary = ();

fn verify(
fn verify<Child, Ctx>(
&self,
parameters: &Self::Parameters,
context: &Self::Context,
related: &RelatedContractsContainer,
) -> Result<(), Box<dyn Error>> {
// Extracts a public key either from the context or the parameters using a
// function passed as a generic type or associated type
let public_key = parameters.public_key();
context.extract::<PublicKey>();
todo!()
context: &Ctx,
_: &freenet_stdlib::prelude::RelatedContractsContainer,
) -> Result<freenet_stdlib::prelude::ValidateResult, freenet_stdlib::prelude::ContractError>
where
Child: ComposableContract,
Self::Context: for<'x> From<&'x Ctx>,
{
let _public_key = parameters.owner_public_key;
let _public_key = PublicKey::from(context);
// do stuff with pub key
unimplemented!()
}

fn verify_delta<Child>(
_: &Self::Parameters,
_: &Self::Delta,
) -> Result<bool, freenet_stdlib::prelude::ContractError>
where
Child: ComposableContract,
{
unimplemented!()
}

fn merge(
&mut self,
parameters: &Self::Parameters,
delta: &Self::Delta,
related: &RelatedContractsContainer,
) -> MergeResult {
_: &Self::Parameters,
_: &freenet_stdlib::composers::TypedUpdateData<Self>,
_: &freenet_stdlib::prelude::RelatedContractsContainer,
) -> freenet_stdlib::composers::MergeResult {
unimplemented!()
}

fn summarize<ParentSummary>(
&self,
_: &Self::Parameters,
_: &mut ParentSummary,
) -> Result<(), freenet_stdlib::prelude::ContractError>
where
ParentSummary:
freenet_stdlib::composers::ComposableSummary<<Self as ComposableContract>::Summary>,
{
unimplemented!()
}

fn delta(
&self,
_: &Self::Parameters,
_: &Self::Summary,
) -> Result<Self::Delta, freenet_stdlib::prelude::ContractError> {
unimplemented!()
}
}
}
*/
2 changes: 1 addition & 1 deletion rust/tests/composable_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod parent {
Child: ComposableContract,
Self::Context: for<'x> From<&'x Ctx>,
{
<ChildContract as ComposableContract>::verify::<ChildContract, Self>(
<ChildContract as ComposableContract>::verify::<ChildContract, _>(
&self.contract_b_0,
&<ChildContract as ComposableContract>::Parameters::from(parameters),
self,
Expand Down
2 changes: 1 addition & 1 deletion rust/tests/typed_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl TypedContract for Contract {
_: Self::Parameters,
_: Self::Delta,
) -> Result<bool, freenet_stdlib::prelude::ContractError> {
unimplemented!()
Ok(true)
}

fn merge(
Expand Down

0 comments on commit 66ba406

Please sign in to comment.