Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hansieodendaal committed Aug 13, 2024
1 parent d06bc59 commit fa7e558
Show file tree
Hide file tree
Showing 25 changed files with 174 additions and 105 deletions.
7 changes: 4 additions & 3 deletions applications/minotari_console_wallet/src/ui/state/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@ use rand::{random, rngs::OsRng};
use tari_common_types::{
tari_address::TariAddress,
types::{PublicKey, Signature},
MaxSizeBytes,
};
use tari_core::{
consensus::{DomainSeparatedConsensusHasher, MaxSizeString},
transactions::{
tari_amount::MicroMinotari,
transaction_components::{BuildInfo, encrypted_data::PaymentId, OutputFeatures, TemplateType},
transaction_components::{encrypted_data::PaymentId, BuildInfo, OutputFeatures, TemplateType},
},
};
use tari_crypto::{keys::PublicKey as PublicKeyTrait, ristretto::RistrettoPublicKey};
use tari_hashing::TransactionHashDomain;
use tari_key_manager::key_manager::KeyManager;
use tari_utilities::{ByteArray, hex::Hex};
use tari_utilities::{hex::Hex, ByteArray};
use tokio::sync::{broadcast, watch};
use tari_common_types::MaxSizeBytes;

use crate::ui::{
state::{BurntProofBase64, CommitmentSignatureBase64, UiTransactionBurnStatus, UiTransactionSendStatus},
ui_error::UiError,
Expand Down
4 changes: 2 additions & 2 deletions base_layer/chat_ffi/src/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub unsafe extern "C" fn destroy_confirmation(ptr: *mut Confirmation) {
#[cfg(test)]
mod test {
use tari_contacts::contacts_service::types::{Confirmation, MessageBuilder};
use tari_utilities::epoch_time::EpochTime;
use tari_utilities::{epoch_time::EpochTime, ByteArray};

use crate::{
byte_vector::{chat_byte_vector_get_at, chat_byte_vector_get_length},
Expand Down Expand Up @@ -190,7 +190,7 @@ mod test {
read_id.push(chat_byte_vector_get_at(id_byte_vec, i, error_out));
}

assert_eq!(message_id, read_id)
assert_eq!(message_id.to_vec(), read_id)
}

unsafe {
Expand Down
14 changes: 11 additions & 3 deletions base_layer/chat_ffi/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::{convert::TryFrom, ffi::CStr, ptr};
use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong};
use tari_chat_client::ChatClient as ChatClientTrait;
use tari_common_types::tari_address::TariAddress;
use tari_contacts::contacts_service::types::{Message, MessageBuilder, MessageMetadata};
use tari_contacts::contacts_service::types::{Message, MessageBuilder, MessageId, MessageMetadata};
use tari_utilities::ByteArray;

use crate::{
Expand Down Expand Up @@ -135,8 +135,16 @@ pub unsafe extern "C" fn get_chat_message(
}

let id = process_vector(message_id, error_out);
let message_id = match MessageId::try_from(id) {
Ok(val) => val,
Err(e) => {
error = LibChatError::from(InterfaceError::ConversionError(format!("message_id ({})", e))).code;
ptr::swap(error_out, &mut error as *mut c_int);
return ptr::null_mut();
},
};

let result = (*client).runtime.block_on((*client).client.get_message(&id));
let result = (*client).runtime.block_on((*client).client.get_message(&message_id));

match result {
Ok(message) => Box::into_raw(Box::new(message)),
Expand Down Expand Up @@ -563,7 +571,7 @@ mod test {
message_id.push(chat_byte_vector_get_at(message_byte_vector, i, error_out));
}

assert_eq!(message.message_id, message_id);
assert_eq!(message.message_id.to_vec(), message_id);

destroy_chat_message(message_ptr);
chat_byte_vector_destroy(message_byte_vector);
Expand Down
2 changes: 1 addition & 1 deletion base_layer/chat_ffi/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ mod test {
message_id.push(chat_byte_vector_get_at(message_byte_vector, i, error_out));
}

assert_eq!(m.message_id, message_id);
assert_eq!(m.message_id.to_vec(), message_id);

destroy_message_vector(message_vector_ptr);
destroy_chat_message(message_ptr);
Expand Down
20 changes: 13 additions & 7 deletions base_layer/common_types/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@
// Portions of this file were originally copyrighted (c) 2018 The Grin Developers, issued under the Apache License,
// Version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0.

use std::convert::TryFrom;
use serde::{Deserialize, Serialize};
use std::ops::{Deref, DerefMut};
use std::cmp;
use std::{
cmp,
convert::TryFrom,
ops::{Deref, DerefMut},
};

use borsh::{BorshDeserialize, BorshSerialize};
use tari_utilities::{ByteArray, ByteArrayError};
use tari_utilities::hex::{from_hex, HexError};
use serde::{Deserialize, Serialize};
use tari_utilities::{
hex::{from_hex, HexError},
ByteArray,
ByteArrayError,
};

#[derive(
Debug,
Expand Down Expand Up @@ -156,4 +162,4 @@ impl From<HexError> for MaxSizeBytesError {
fn from(err: HexError) -> Self {
MaxSizeBytesError::HexError(err.to_string())
}
}
}
4 changes: 2 additions & 2 deletions base_layer/common_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
pub const WALLET_COMMS_AND_SPEND_KEY_BRANCH: &str = "comms";

pub mod burnt_proof;
mod bytes;
pub mod chain_metadata;
pub mod dammsum;
pub mod emoji;
Expand All @@ -37,5 +38,4 @@ pub mod transaction;
mod tx_id;
pub mod types;
pub mod wallet_types;
mod bytes;
pub use bytes::{MaxSizeBytes};
pub use bytes::{MaxSizeBytes, MaxSizeBytesError};
6 changes: 3 additions & 3 deletions base_layer/contacts/src/chat_client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use tari_comms::{peer_manager::PeerFeatures, CommsNode, NodeIdentity};
use tari_contacts::contacts_service::{
handle::ContactsServiceHandle,
service::ContactOnlineStatus,
types::{Message, MessageBuilder, MessageMetadata},
types::{Message, MessageBuilder, MessageId, MessageMetadata},
};
use tari_shutdown::Shutdown;

Expand All @@ -49,7 +49,7 @@ pub trait ChatClient {
async fn check_online_status(&self, address: &TariAddress) -> Result<ContactOnlineStatus, Error>;
fn create_message(&self, receiver: &TariAddress, message: String) -> Message;
async fn get_messages(&self, sender: &TariAddress, limit: u64, page: u64) -> Result<Vec<Message>, Error>;
async fn get_message(&self, id: &[u8]) -> Result<Message, Error>;
async fn get_message(&self, id: &MessageId) -> Result<Message, Error>;
async fn send_message(&self, message: Message) -> Result<(), Error>;
async fn send_read_receipt(&self, message: Message) -> Result<(), Error>;
async fn get_conversationalists(&self) -> Result<Vec<TariAddress>, Error>;
Expand Down Expand Up @@ -208,7 +208,7 @@ impl ChatClient for Client {
Ok(messages)
}

async fn get_message(&self, message_id: &[u8]) -> Result<Message, Error> {
async fn get_message(&self, message_id: &MessageId) -> Result<Message, Error> {
match self.contacts.clone() {
Some(mut contacts_service) => contacts_service.get_message(message_id).await.map_err(|e| e.into()),
None => Err(Error::InitializationError(
Expand Down
5 changes: 5 additions & 0 deletions base_layer/contacts/src/contacts_service/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use diesel::result::Error as DieselError;
use tari_common_sqlite::error::SqliteStorageError;
use tari_common_types::MaxSizeBytesError;
use tari_comms::connectivity::ConnectivityError;
use tari_comms_dht::outbound::DhtOutboundError;
use tari_p2p::services::liveness::error::LivenessError;
Expand Down Expand Up @@ -53,6 +54,8 @@ pub enum ContactsServiceError {
MalformedMessageError(#[from] prost::DecodeError),
#[error("Message source does not match authenticated origin")]
MessageSourceDoesNotMatchOrigin,
#[error("Byte size conversion error: `{0}`")]
MaxSizeBytesError(#[from] MaxSizeBytesError),
}

#[derive(Debug, Error)]
Expand All @@ -79,4 +82,6 @@ pub enum ContactsServiceStorageError {
BlockingTaskSpawnError(String),
#[error("We got an error")]
UnknownError,
#[error("Byte size conversion error: `{0}`")]
MaxSizeBytesError(#[from] MaxSizeBytesError),
}
10 changes: 5 additions & 5 deletions base_layer/contacts/src/contacts_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use tower::Service;
use crate::contacts_service::{
error::ContactsServiceError,
service::{ContactMessageType, ContactOnlineStatus},
types::{Confirmation, Contact, Message, MessageDispatch},
types::{Confirmation, Contact, Message, MessageDispatch, MessageId},
};

pub static DEFAULT_MESSAGE_LIMIT: u64 = 35;
Expand Down Expand Up @@ -138,7 +138,7 @@ pub enum ContactsServiceRequest {
GetContactOnlineStatus(Contact),
SendMessage(TariAddress, Message),
GetMessages(TariAddress, i64, i64),
GetMessage(Vec<u8>),
GetMessage(MessageId),
SendReadConfirmation(TariAddress, Confirmation),
GetConversationalists,
}
Expand Down Expand Up @@ -279,10 +279,10 @@ impl ContactsServiceHandle {
}
}

pub async fn get_message(&mut self, message_id: &[u8]) -> Result<Message, ContactsServiceError> {
pub async fn get_message(&mut self, message_id: &MessageId) -> Result<Message, ContactsServiceError> {
match self
.request_response_service
.call(ContactsServiceRequest::GetMessage(message_id.to_vec()))
.call(ContactsServiceRequest::GetMessage(message_id.clone()))
.await??
{
ContactsServiceResponse::Message(message) => Ok(message),
Expand All @@ -307,7 +307,7 @@ impl ContactsServiceHandle {
pub async fn send_read_confirmation(
&mut self,
address: TariAddress,
message_id: Vec<u8>,
message_id: MessageId,
) -> Result<(), ContactsServiceError> {
match self
.request_response_service
Expand Down
13 changes: 8 additions & 5 deletions base_layer/contacts/src/contacts_service/storage/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ use chrono::NaiveDateTime;
use log::*;
use tari_common_types::tari_address::TariAddress;
use tari_comms::peer_manager::NodeId;
use tari_utilities::ByteArray;

use crate::contacts_service::{
error::ContactsServiceStorageError,
types::{Contact, Message},
types::{Contact, Message, MessageId},
};

const LOG_TARGET: &str = "contacts::contacts_service::database";
Expand Down Expand Up @@ -187,9 +188,9 @@ where T: ContactsBackend + 'static
}
}

pub fn get_message(&self, message_id: Vec<u8>) -> Result<Message, ContactsServiceStorageError> {
pub fn get_message(&self, message_id: MessageId) -> Result<Message, ContactsServiceStorageError> {
let db_clone = self.db.clone();
fetch!(db_clone, message_id, Message)
fetch!(db_clone, message_id.to_vec(), Message)
}

pub fn save_message(&self, message: Message) -> Result<(), ContactsServiceStorageError> {
Expand All @@ -201,7 +202,7 @@ where T: ContactsBackend + 'static

pub fn confirm_message(
&self,
message_id: Vec<u8>,
message_id: MessageId,
delivery_confirmation: Option<u64>,
read_confirmation: Option<u64>,
) -> Result<(), ContactsServiceStorageError> {
Expand All @@ -225,7 +226,9 @@ where T: ContactsBackend + 'static

self.db
.write(WriteOperation::Upsert(Box::new(DbKeyValuePair::MessageConfirmations(
message_id, delivery, read,
message_id.to_vec(),
delivery,
read,
))))?;

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ use std::convert::TryFrom;

use chrono::NaiveDateTime;
use diesel::prelude::*;
use tari_utilities::ByteArray;
use tari_common_sqlite::util::diesel_ext::ExpectedRowsExtension;
use tari_common_types::tari_address::TariAddress;

use crate::{
contacts_service::{
error::ContactsServiceStorageError,
types::{Direction, Message, MessageMetadata},
types::{Direction, Message, MessageId, MessageMetadata},
},
schema::messages,
};
use crate::contacts_service::types::ChatBody;

/// A Sql version of the Contact struct
#[derive(Clone, Debug, Insertable, PartialEq, Eq)]
Expand Down Expand Up @@ -183,7 +185,7 @@ impl TryFrom<MessagesSql> for Message {

Ok(Self {
metadata,
body: o.body,
body: ChatBody::try_from(o.body)?,
receiver_address,
sender_address,
direction: Direction::from_byte(
Expand All @@ -194,7 +196,7 @@ impl TryFrom<MessagesSql> for Message {
stored_at: o.stored_at.timestamp() as u64,
delivery_confirmation_at: Some(o.stored_at.timestamp() as u64),
read_confirmation_at: Some(o.stored_at.timestamp() as u64),
message_id: o.message_id,
message_id: MessageId::try_from(o.message_id)?,
})
}
}
Expand All @@ -209,8 +211,8 @@ impl TryFrom<Message> for MessagesSqlInsert {
Ok(Self {
receiver_address: o.receiver_address.to_vec(),
sender_address: o.sender_address.to_vec(),
message_id: o.message_id,
body: o.body,
message_id: o.message_id.to_vec(),
body: o.body.to_vec(),
metadata: metadata.into_bytes().to_vec(),
stored_at: NaiveDateTime::from_timestamp_opt(o.stored_at as i64, 0)
.ok_or(ContactsServiceStorageError::ConversionError)?,
Expand Down
23 changes: 15 additions & 8 deletions base_layer/contacts/src/contacts_service/types/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,34 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::contacts_service::proto;
use std::convert::TryFrom;

use tari_common_types::MaxSizeBytes;
use tari_utilities::ByteArray;

use crate::contacts_service::{error::ContactsServiceError, proto, types::MessageId};

#[derive(Clone, Debug, Default)]
pub struct Confirmation {
pub message_id: Vec<u8>,
pub message_id: MessageId,
pub timestamp: u64,
}

impl From<proto::Confirmation> for Confirmation {
fn from(confirmation: proto::Confirmation) -> Self {
Self {
message_id: confirmation.message_id,
impl TryFrom<proto::Confirmation> for Confirmation {
type Error = ContactsServiceError;

fn try_from(confirmation: proto::Confirmation) -> Result<Self, Self::Error> {
Ok(Self {
message_id: MaxSizeBytes::try_from(confirmation.message_id)?,
timestamp: confirmation.timestamp,
}
})
}
}

impl From<Confirmation> for proto::Confirmation {
fn from(confirmation: Confirmation) -> Self {
Self {
message_id: confirmation.message_id,
message_id: confirmation.message_id.to_vec(),
timestamp: confirmation.timestamp,
}
}
Expand Down
Loading

0 comments on commit fa7e558

Please sign in to comment.