Skip to content

Commit

Permalink
feat(db)!: add cone white flag order (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandcoats authored and grtlr committed May 31, 2022
1 parent 26a86ba commit 6b936b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
15 changes: 11 additions & 4 deletions bin/inx-chronicle/src/stardust_inx/cone_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ impl ConeStream {

#[async_trait]
impl Actor for ConeStream {
type State = ();
type State = u32;
type Error = InxError;

async fn init(&mut self, _cx: &mut ActorContext<Self>) -> Result<Self::State, Self::Error> {
Ok(())
Ok(0)
}

fn name(&self) -> std::borrow::Cow<'static, str> {
Expand Down Expand Up @@ -58,16 +58,23 @@ impl HandleEvent<Result<inx::proto::BlockWithMetadata, Status>> for ConeStream {
&mut self,
_cx: &mut ActorContext<Self>,
block_metadata_result: Result<inx::proto::BlockWithMetadata, Status>,
_state: &mut Self::State,
white_flag_index: &mut Self::State,
) -> Result<(), Self::Error> {
log::trace!("Received Stardust Block Event");

let inx_block_with_metadata: inx::BlockWithMetadata = block_metadata_result?.try_into()?;
let BlockWithMetadata { metadata, block, raw } = inx_block_with_metadata;

self.db
.insert_block_with_metadata(metadata.block_id.into(), block.into(), raw, metadata.into())
.insert_block_with_metadata(
metadata.block_id.into(),
block.into(),
raw,
metadata.into(),
*white_flag_index,
)
.await?;
*white_flag_index += 1;

log::trace!("Inserted block into database.");

Expand Down
4 changes: 4 additions & 0 deletions src/db/collections/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct BlockDocument {
raw: Vec<u8>,
/// The block's metadata.
metadata: BlockMetadata,
/// The index of this block in white flag order.
white_flag_index: u32,
}

impl BlockDocument {
Expand Down Expand Up @@ -120,12 +122,14 @@ impl MongoDb {
block: Block,
raw: Vec<u8>,
metadata: BlockMetadata,
white_flag_index: u32,
) -> Result<(), Error> {
let block_document = BlockDocument {
block_id: block_id.clone(),
block,
raw,
metadata,
white_flag_index,
};

self.0
Expand Down
2 changes: 1 addition & 1 deletion tests/message_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn test_test() -> Result<(), mongodb::error::Error> {

db.clear().await?;

db.insert_block_with_metadata(block_id.clone(), block, raw, metadata)
db.insert_block_with_metadata(block_id.clone(), block, raw, metadata, 0)
.await?;

let result = db.get_block(&block_id).await?.unwrap();
Expand Down

0 comments on commit 6b936b5

Please sign in to comment.