Skip to content

Commit

Permalink
Move types to non ambiguous living situations
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Mar 14, 2023
1 parent 92935bf commit 6b1ccb7
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 53 deletions.
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/src/ui/state/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use tari_comms::{
net_address::{MultiaddressesWithStats, PeerAddressSource},
peer_manager::{NodeId, Peer, PeerFeatures, PeerFlags},
};
use tari_contacts::contacts_service::{handle::ContactsLivenessEvent, storage::database::Contact};
use tari_contacts::contacts_service::{handle::ContactsLivenessEvent, types::Contact};
use tari_core::transactions::{
tari_amount::{uT, MicroTari},
transaction_components::OutputFeatures,
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/src/ui/ui_contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: BSD-3-Clause

use chrono::{DateTime, Local};
use tari_contacts::contacts_service::storage::database::Contact;
use tari_contacts::contacts_service::types::Contact;

#[derive(Debug, Clone)]
pub struct UiContact {
Expand Down
8 changes: 1 addition & 7 deletions base_layer/contacts/src/contacts_service/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use tower::Service;
use crate::contacts_service::{
error::ContactsServiceError,
service::{ContactMessageType, ContactOnlineStatus},
storage::database::Contact,
types::{Contact, Message},
};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -116,12 +116,6 @@ impl Display for ContactsLivenessData {
}
}

#[derive(Debug)]
pub struct Message {
pub body: Vec<u8>,
pub address: TariAddress,
}

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum ContactsLivenessEvent {
Expand Down
1 change: 1 addition & 0 deletions base_layer/contacts/src/contacts_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod error;
pub mod handle;
pub mod service;
pub mod storage;
pub mod types;

use std::time::Duration;

Expand Down
3 changes: 2 additions & 1 deletion base_layer/contacts/src/contacts_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ use tokio::sync::broadcast;
use crate::contacts_service::{
error::ContactsServiceError,
handle::{ContactsLivenessData, ContactsLivenessEvent, ContactsServiceRequest, ContactsServiceResponse},
storage::database::{Contact, ContactsBackend, ContactsDatabase},
storage::database::{ContactsBackend, ContactsDatabase},
types::Contact,
};

const LOG_TARGET: &str = "contacts::contacts_service";
Expand Down
34 changes: 4 additions & 30 deletions base_layer/contacts/src/contacts_service/storage/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,13 @@ use log::*;
use tari_common_types::tari_address::TariAddress;
use tari_comms::peer_manager::NodeId;

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

const LOG_TARGET: &str = "contacts::contacts_service::database";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Contact {
pub alias: String,
pub address: TariAddress,
pub node_id: NodeId,
pub last_seen: Option<NaiveDateTime>,
pub latency: Option<u32>,
pub favourite: bool,
}

impl Contact {
pub fn new(
alias: String,
address: TariAddress,
last_seen: Option<NaiveDateTime>,
latency: Option<u32>,
favourite: bool,
) -> Self {
Self {
alias,
node_id: NodeId::from_key(address.public_key()),
address,
last_seen,
latency,
favourite,
}
}
}

/// This trait defines the functionality that a database backend need to provide for the Contacts Service
pub trait ContactsBackend: Send + Sync + Clone {
/// Retrieve the record associated with the provided DbKey
Expand Down
10 changes: 5 additions & 5 deletions base_layer/contacts/src/contacts_service/storage/sqlite_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ use tari_utilities::ByteArray;

use crate::contacts_service::{
error::ContactsServiceStorageError,
handle::Message,
storage::{
database::{Contact, ContactsBackend, DbKey, DbKeyValuePair, DbValue, WriteOperation},
database::{ContactsBackend, DbKey, DbKeyValuePair, DbValue, WriteOperation},
types::{
contacts::{ContactSql, UpdateContact},
messages::MessagesSql,
},
},
types::{Contact, Message},
};

const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
Expand Down Expand Up @@ -193,9 +193,9 @@ mod test {
use tari_test_utils::{paths::with_temp_dir, random::string};

use super::*;
use crate::contacts_service::storage::{
database::Contact,
types::contacts::{ContactSql, UpdateContact},
use crate::contacts_service::{
storage::types::contacts::{ContactSql, UpdateContact},
types::Contact,
};

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use tari_comms::peer_manager::NodeId;
use tari_utilities::ByteArray;

use crate::{
contacts_service::{error::ContactsServiceStorageError, storage::database::Contact},
contacts_service::{error::ContactsServiceStorageError, types::Contact},
schema::contacts,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@

use std::convert::TryFrom;

use chrono::NaiveDateTime;
use diesel::{prelude::*, SqliteConnection};
use tari_common_types::tari_address::TariAddress;
use tari_utilities::ByteArray;

use crate::{
contacts_service::{error::ContactsServiceStorageError, handle::Message},
contacts_service::{
error::ContactsServiceStorageError,
types::{Direction, Message},
},
schema::messages,
};

Expand Down
54 changes: 54 additions & 0 deletions base_layer/contacts/src/contacts_service/types/contact.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2023. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// 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 chrono::NaiveDateTime;
use tari_common_types::tari_address::TariAddress;
use tari_comms::peer_manager::NodeId;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Contact {
pub alias: String,
pub address: TariAddress,
pub node_id: NodeId,
pub last_seen: Option<NaiveDateTime>,
pub latency: Option<u32>,
pub favourite: bool,
}

impl Contact {
pub fn new(
alias: String,
address: TariAddress,
last_seen: Option<NaiveDateTime>,
latency: Option<u32>,
favourite: bool,
) -> Self {
Self {
alias,
node_id: NodeId::from_key(address.public_key()),
address,
last_seen,
latency,
favourite,
}
}
}
38 changes: 38 additions & 0 deletions base_layer/contacts/src/contacts_service/types/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// 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 tari_common_types::tari_address::TariAddress;

#[derive(Debug)]
pub struct Message {
pub body: Vec<u8>,
pub address: TariAddress,
pub direction: Direction,
pub logged_time: u64,
pub message_id: u64,
}

#[derive(Debug, Copy, Clone)]
pub enum Direction {
Inbound = 0,
Outbound = 1,
}
27 changes: 27 additions & 0 deletions base_layer/contacts/src/contacts_service/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2023. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// 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.

mod contact;
pub use contact::Contact;

mod message;
pub use message::{Direction, Message};
3 changes: 2 additions & 1 deletion base_layer/contacts/tests/contacts_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ use tari_contacts::contacts_service::{
error::{ContactsServiceError, ContactsServiceStorageError},
handle::ContactsServiceHandle,
storage::{
database::{Contact, ContactsBackend, DbKey},
database::{ContactsBackend, DbKey},
sqlite_db::ContactsServiceSqliteDatabase,
},
types::Contact,
ContactsServiceInitializer,
};
use tari_crypto::keys::PublicKey as PublicKeyTrait;
Expand Down
3 changes: 2 additions & 1 deletion base_layer/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ use tari_comms_dht::{store_forward::SafConfig, DhtConfig};
use tari_contacts::contacts_service::{
handle::ContactsLivenessEvent,
service::ContactMessageType,
storage::{database::Contact, sqlite_db::ContactsServiceSqliteDatabase},
storage::sqlite_db::ContactsServiceSqliteDatabase,
types::Contact,
};
use tari_core::{
consensus::ConsensusManager,
Expand Down
2 changes: 1 addition & 1 deletion base_layer/wallet_ffi/src/callback_handler_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod test {
use tari_contacts::contacts_service::{
handle::{ContactsLivenessData, ContactsLivenessEvent},
service::{ContactMessageType, ContactOnlineStatus},
storage::database::Contact,
types::Contact,
};
use tari_core::transactions::{
tari_amount::{uT, MicroTari},
Expand Down
4 changes: 2 additions & 2 deletions base_layer/wallet_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ use tari_comms::{
types::{CommsPublicKey, CommsSecretKey},
};
use tari_comms_dht::{store_forward::SafConfig, DbConnectionUrl, DhtConfig};
use tari_contacts::contacts_service::storage::database::Contact;
use tari_contacts::contacts_service::types::Contact;
use tari_core::{
borsh::FromBytes,
consensus::ConsensusManager,
Expand Down Expand Up @@ -208,7 +208,7 @@ pub struct TariUnblindedOutputs(Vec<DbUnblindedOutput>);

pub struct TariContacts(Vec<TariContact>);

pub type TariContact = tari_contacts::contacts_service::storage::database::Contact;
pub type TariContact = tari_contacts::contacts_service::types::Contact;
pub type TariCompletedTransaction = tari_wallet::transaction_service::storage::models::CompletedTransaction;
pub type TariTransactionSendStatus = tari_wallet::transaction_service::handle::TransactionSendStatus;
pub type TariFeePerGramStats = tari_wallet::transaction_service::handle::FeePerGramStatsResponse;
Expand Down

0 comments on commit 6b1ccb7

Please sign in to comment.