Skip to content

Commit

Permalink
WIP - Fix integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Mar 14, 2023
1 parent 6b1ccb7 commit 300fa2d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions base_layer/contacts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@ edition = "2018"

[dependencies]
tari_common = { path = "../../common" }
tari_common_sqlite = { path = "../../common_sqlite" }
tari_common_types = { path = "../../base_layer/common_types" }
tari_comms = { path = "../../comms/core" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.16.8" }
tari_p2p = { path = "../p2p", features = ["auto-update"] }
tari_service_framework = { path = "../service_framework" }
tari_shutdown = { path = "../../infrastructure/shutdown" }
tari_common_sqlite = { path = "../../common_sqlite" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag="v0.4.10" }

tokio = { version = "1.23", features = ["sync", "macros"] }
chrono = { version = "0.4.19", default-features = false, features = ["serde"] }
diesel = { version = "2.0.3", features = ["sqlite", "serde_json", "chrono", "64-column-tables"] }
diesel_migrations = "2.0.0"
futures = { version = "^0.3.1", features = ["compat", "std"] }
libsqlite3-sys = { version = "0.25.1", features = ["bundled"], optional = true }
log = "0.4.6"
num-traits = "0.2.15"
num-derive = "0.3.3"
rand = "0.7.3"
thiserror = "1.0.26"
tokio = { version = "1.23", features = ["sync", "macros"] }
tower = "0.4"

[dev-dependencies]
tari_test_utils = { path = "../../infrastructure/test_utils" }
tari_comms_dht = { path = "../../comms/dht", features = ["test-mocks"] }
tari_test_utils = { path = "../../infrastructure/test_utils" }
tempfile = "3.1.0"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE messages (
address BLOB PRIMARY KEY NOT NULL,
message_id INTEGER NOT NULL,
message_id BIGINT NOT NULL,
body BLOB NOT NULL,
stored_at TIMESTAMP NOT NULL,
direction INTEGER NOT NULL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
#[diesel(table_name = messages)]
pub struct MessagesSql {
pub address: Vec<u8>,
pub message_id: i32,
pub message_id: i64,
pub body: Vec<u8>,
pub stored_at: NaiveDateTime,
pub direction: i32,
Expand Down Expand Up @@ -72,8 +72,9 @@ impl TryFrom<MessagesSql> for Message {
let address = TariAddress::from_bytes(&o.address).map_err(|_| ContactsServiceStorageError::ConversionError)?;
Ok(Self {
address,
direction: Direction::Inbound,
logged_time: 0,
direction: Direction::from_byte(o.direction as u8)
.unwrap_or_else(|| panic!("Direction from byte {}", o.direction)),
stored_at: 0,
body: o.body,
message_id: 0,
})
Expand All @@ -86,10 +87,11 @@ impl From<Message> for MessagesSql {
fn from(o: Message) -> Self {
Self {
address: o.address.to_bytes().to_vec(),
direction: 0,
message_id: 0,
direction: o.direction.as_byte() as i32,
message_id: o.message_id as i64,
body: o.body,
stored_at: Default::default(),
stored_at: NaiveDateTime::from_timestamp_opt(o.stored_at as i64, 0)
.expect("i64 to NaiveDateTime conversion"),
}
}
}
23 changes: 21 additions & 2 deletions base_layer/contacts/src/contacts_service/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,38 @@
// 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 num_derive::FromPrimitive;
use num_traits::FromPrimitive;
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 stored_at: u64,
pub message_id: u64,
}

#[derive(Debug, Copy, Clone)]
#[repr(u8)]
#[derive(FromPrimitive, Debug, Copy, Clone)]
pub enum Direction {
Inbound = 0,
Outbound = 1,
}

impl Direction {
pub fn as_byte(self) -> u8 {
self as u8
}

pub fn from_byte(value: u8) -> Option<Self> {
FromPrimitive::from_u8(value)
}
}

impl Default for Direction {
fn default() -> Self {
Self::Inbound
}
}
2 changes: 1 addition & 1 deletion base_layer/contacts/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ diesel::table! {
diesel::table! {
messages (address) {
address -> Binary,
message_id -> Integer,
message_id -> BigInt,
body -> Binary,
stored_at -> Timestamp,
direction -> Integer,
Expand Down

0 comments on commit 300fa2d

Please sign in to comment.