|
| 1 | +use std::cmp::Ordering; |
| 2 | + |
| 3 | +use fuel_data_parser::DataEncoder; |
| 4 | +use fuel_streams_types::*; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +use super::{Message, MessageType, MessagesSubject}; |
| 8 | +use crate::infra::{ |
| 9 | + db::DbItem, |
| 10 | + record::{RecordEntity, RecordPacket, RecordPacketError, RecordPointer}, |
| 11 | + Cursor, |
| 12 | + DbError, |
| 13 | +}; |
| 14 | + |
| 15 | +#[derive( |
| 16 | + Debug, Clone, Serialize, Deserialize, PartialEq, Eq, sqlx::FromRow, Default, |
| 17 | +)] |
| 18 | +pub struct MessageDbItem { |
| 19 | + pub subject: String, |
| 20 | + pub value: Vec<u8>, |
| 21 | + pub block_height: BlockHeight, |
| 22 | + pub message_index: i32, |
| 23 | + pub cursor: String, |
| 24 | + // fields matching fuel-core |
| 25 | + pub r#type: MessageType, |
| 26 | + pub sender: String, |
| 27 | + pub recipient: String, |
| 28 | + pub nonce: String, |
| 29 | + pub amount: i64, |
| 30 | + pub data: String, |
| 31 | + pub da_height: DaBlockHeight, |
| 32 | + // timestamps |
| 33 | + pub block_time: BlockTimestamp, |
| 34 | + pub created_at: BlockTimestamp, |
| 35 | +} |
| 36 | + |
| 37 | +impl DataEncoder for MessageDbItem {} |
| 38 | + |
| 39 | +impl DbItem for MessageDbItem { |
| 40 | + fn cursor(&self) -> Cursor { |
| 41 | + Cursor::new(&[&self.block_height, &self.message_index]) |
| 42 | + } |
| 43 | + |
| 44 | + fn entity(&self) -> &RecordEntity { |
| 45 | + &RecordEntity::Message |
| 46 | + } |
| 47 | + |
| 48 | + fn encoded_value(&self) -> Result<Vec<u8>, DbError> { |
| 49 | + Ok(self.value.clone()) |
| 50 | + } |
| 51 | + |
| 52 | + fn subject_str(&self) -> String { |
| 53 | + self.subject.clone() |
| 54 | + } |
| 55 | + |
| 56 | + fn subject_id(&self) -> String { |
| 57 | + MessagesSubject::ID.to_string() |
| 58 | + } |
| 59 | + |
| 60 | + fn created_at(&self) -> BlockTimestamp { |
| 61 | + self.created_at |
| 62 | + } |
| 63 | + |
| 64 | + fn block_time(&self) -> BlockTimestamp { |
| 65 | + self.block_time |
| 66 | + } |
| 67 | + |
| 68 | + fn block_height(&self) -> BlockHeight { |
| 69 | + self.block_height |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl TryFrom<&RecordPacket> for MessageDbItem { |
| 74 | + type Error = RecordPacketError; |
| 75 | + fn try_from(packet: &RecordPacket) -> Result<Self, Self::Error> { |
| 76 | + let message = Message::decode_json(&packet.value)?; |
| 77 | + let block_height = packet.pointer.block_height; |
| 78 | + let msg_index = message.message_index as i32; |
| 79 | + Ok(MessageDbItem { |
| 80 | + subject: packet.subject_str(), |
| 81 | + value: packet.value.to_owned(), |
| 82 | + block_height: packet.pointer.block_height, |
| 83 | + message_index: msg_index, |
| 84 | + cursor: format!("{}-{}", block_height, msg_index), |
| 85 | + r#type: message.r#type, |
| 86 | + sender: message.sender.to_string(), |
| 87 | + recipient: message.recipient.to_string(), |
| 88 | + nonce: message.nonce.to_string(), |
| 89 | + amount: message.amount.into_inner() as i64, |
| 90 | + data: message.data.to_string(), |
| 91 | + da_height: message.da_height, |
| 92 | + block_time: packet.block_timestamp, |
| 93 | + created_at: packet.block_timestamp, |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl PartialOrd for MessageDbItem { |
| 99 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 100 | + Some(self.cmp(other)) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl Ord for MessageDbItem { |
| 105 | + fn cmp(&self, other: &Self) -> Ordering { |
| 106 | + self.block_height |
| 107 | + .cmp(&other.block_height) |
| 108 | + .then(self.message_index.cmp(&other.message_index)) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl From<MessageDbItem> for RecordPointer { |
| 113 | + fn from(val: MessageDbItem) -> Self { |
| 114 | + RecordPointer { |
| 115 | + block_height: val.block_height, |
| 116 | + ..Default::default() |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments