Skip to content

Commit

Permalink
Tangle simplification (#764)
Browse files Browse the repository at this point in the history
* Nits

* Remove metadata template

* Fix clippy

* Remove hooks

* Update changelog and version
  • Loading branch information
thibault-martinez authored Oct 8, 2021
1 parent 2a8b199 commit c5d7800
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 209 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion bee-ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bee-common = { version = "0.5.0", path = "../bee-common/bee-common" }
bee-message = { version = "0.1.5", path = "../bee-message" }
bee-runtime = { version = "0.1.1-alpha", path = "../bee-runtime", optional = true }
bee-storage = { version = "0.9.0", path = "../bee-storage/bee-storage", optional = true }
bee-tangle = { version = "0.1.2", path = "../bee-tangle", optional = true }
bee-tangle = { version = "0.2.0", path = "../bee-tangle", optional = true }

async-trait = { version = "0.1", optional = true }
chrono = { version = "0.4", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion bee-storage/bee-storage-rocksdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bee-common = { version = "0.5.0", path = "../../bee-common/bee-common" }
bee-ledger = { version = "0.5.0", path = "../../bee-ledger" }
bee-message = { version = "0.1.5", path = "../../bee-message" }
bee-storage = { version = "0.9.0", path = "../bee-storage" }
bee-tangle = { version = "0.1.2", path = "../../bee-tangle" }
bee-tangle = { version = "0.2.0", path = "../../bee-tangle" }

num_cpus = "1.13"
rocksdb = { version = "0.17", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion bee-storage/bee-storage-sled/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bee-common = { version = "0.5.0", path = "../../bee-common/bee-common" }
bee-ledger = { version = "0.5.0", path = "../../bee-ledger" }
bee-message = { version = "0.1.5", path = "../../bee-message" }
bee-storage = { version = "0.9.0", path = "../bee-storage" }
bee-tangle = { version = "0.1.2", path = "../../bee-tangle" }
bee-tangle = { version = "0.2.0", path = "../../bee-tangle" }

num_cpus = "1.13"
serde = { version = "1.0", features = [ "derive" ] }
Expand Down
2 changes: 1 addition & 1 deletion bee-storage/bee-storage-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ bee-common = { version = "0.5.0", path = "../../bee-common/bee-common" }
bee-ledger = { version = "0.5.0", path = "../../bee-ledger" }
bee-message = { version = "0.1.5", path = "../../bee-message" }
bee-storage = { version = "0.9.0", path = "../bee-storage" }
bee-tangle = { version = "0.1.2", path = "../../bee-tangle" }
bee-tangle = { version = "0.2.0", path = "../../bee-tangle" }
bee-test = { version = "0.1.0", path = "../../bee-test" }
9 changes: 7 additions & 2 deletions bee-tangle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 0.2.0 - 2021-XX-XX

### Removed

- Tangle genericity over metadata type;
- Storage hooks;

## 0.1.2 - 2021-05-10

### Added

- Moved `ConflictReason` from bee-ledger;

### Added

## 0.1.1 - 2021-05-07

### Added
Expand Down
2 changes: 1 addition & 1 deletion bee-tangle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bee-tangle"
version = "0.1.2"
version = "0.2.0"
authors = ["IOTA Stiftung"]
edition = "2018"
description = "A distributed, directed, acyclic graph that underpins the DLT for IOTA's Bee node"
Expand Down
2 changes: 1 addition & 1 deletion bee-tangle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod vertex;

pub use conflict::ConflictReason;
pub use ms_tangle::MsTangle;
pub use tangle::{Hooks, Tangle};
pub use tangle::Tangle;
pub use tangle_worker::TangleWorker;

use tip_pool_cleaner_worker::TipPoolCleanerWorker;
Expand Down
82 changes: 19 additions & 63 deletions bee-tangle/src/ms_tangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
metadata::{IndexId, MessageMetadata},
solid_entry_point::SolidEntryPoint,
storage::StorageBackend,
tangle::{Hooks, Tangle, DEFAULT_CACHE_LEN},
tangle::{Tangle, DEFAULT_CACHE_LEN},
urts::UrtsTipPool,
MessageRef,
};
Expand All @@ -30,65 +30,10 @@ use std::{
const SYNCED_THRESHOLD: u32 = 2;
const CONFIRMED_THRESHOLD: u32 = 2;

/// Tangle hooks that interoperate with Bee's storage layer.
pub struct StorageHooks<B> {
#[allow(dead_code)]
storage: ResourceHandle<B>,
}

impl<B: StorageBackend> Hooks<MessageMetadata> for StorageHooks<B> {
type Error = B::Error;

fn get(&self, id: &MessageId) -> Result<Option<(Message, MessageMetadata)>, Self::Error> {
trace!("Attempted to fetch message {:?}", id);
let msg = self.storage.fetch(id)?;
let meta = self.storage.fetch(id)?;
Ok(msg.zip(meta))
}

fn insert(&self, id: MessageId, tx: Message, metadata: MessageMetadata) -> Result<(), Self::Error> {
trace!("Attempted to insert message {:?}", id);
self.storage.insert(&id, &tx)?;
self.storage.insert(&id, &metadata)?;
Ok(())
}

fn fetch_approvers(&self, id: &MessageId) -> Result<Option<Vec<MessageId>>, Self::Error> {
trace!("Attempted to fetch approvers for message {:?}", id);
self.storage.fetch(id)
}

fn insert_approver(&self, id: MessageId, approver: MessageId) -> Result<(), Self::Error> {
trace!("Attempted to insert approver for message {:?}", id);
self.storage.insert(&(id, approver), &())
}

fn update_approvers(&self, id: MessageId, approvers: &[MessageId]) -> Result<(), Self::Error> {
trace!("Attempted to update approvers for message {:?}", id);
for approver in approvers {
self.storage.insert(&(id, *approver), &())?;
}
Ok(())
}
}

impl<B: StorageBackend> StorageHooks<B> {
fn get_milestone(&self, idx: &MilestoneIndex) -> Result<Option<Milestone>, B::Error> {
trace!("Attempted to fetch milestone {:?}", idx);
self.storage.fetch(idx)
}

fn insert_milestone(&self, idx: MilestoneIndex, milestone: &Milestone) -> Result<(), B::Error> {
trace!("Attempted to insert milestone {:?}", idx);
self.storage.insert(&idx, milestone)?;
Ok(())
}
}

/// A Tangle wrapper designed to encapsulate milestone state.
pub struct MsTangle<B> {
config: TangleConfig,
inner: Tangle<MessageMetadata, StorageHooks<B>>,
inner: Tangle<B>,
milestones: Mutex<HashMap<MilestoneIndex, Milestone>>,
solid_entry_points: Mutex<HashMap<SolidEntryPoint, MilestoneIndex>>,
latest_milestone_index: AtomicU32,
Expand All @@ -101,7 +46,7 @@ pub struct MsTangle<B> {
}

impl<B> Deref for MsTangle<B> {
type Target = Tangle<MessageMetadata, StorageHooks<B>>;
type Target = Tangle<B>;

fn deref(&self) -> &Self::Target {
&self.inner
Expand All @@ -112,7 +57,7 @@ impl<B: StorageBackend> MsTangle<B> {
/// Create a new `MsTangle` instance with the given configuration and storage handle.
pub fn new(config: TangleConfig, storage: ResourceHandle<B>) -> Self {
Self {
inner: Tangle::new(StorageHooks { storage }),
inner: Tangle::new(storage),
milestones: Default::default(),
solid_entry_points: Default::default(),
latest_milestone_index: Default::default(),
Expand Down Expand Up @@ -152,9 +97,7 @@ impl<B: StorageBackend> MsTangle<B> {
metadata.set_ymrsi(IndexId::new(idx, *milestone.message_id()));
})
.await;
self.inner
.hooks()
.insert_milestone(idx, &milestone)
self.storage_insert_milestone(idx, &milestone)
.unwrap_or_else(|e| info!("Failed to insert message {:?}", e));
self.milestones.lock().await.insert(idx, milestone);
}
Expand All @@ -165,7 +108,7 @@ impl<B: StorageBackend> MsTangle<B> {
}

async fn pull_milestone(&self, idx: MilestoneIndex) -> Option<MessageId> {
if let Some(milestone) = self.inner.hooks().get_milestone(&idx).unwrap_or_else(|e| {
if let Some(milestone) = self.storage_get_milestone(&idx).unwrap_or_else(|e| {
info!("Failed to insert message {:?}", e);
None
}) {
Expand Down Expand Up @@ -418,3 +361,16 @@ impl<B: StorageBackend> MsTangle<B> {
self.tip_pool.lock().await.non_lazy_tips().len()
}
}

impl<B: StorageBackend> MsTangle<B> {
fn storage_get_milestone(&self, idx: &MilestoneIndex) -> Result<Option<Milestone>, B::Error> {
trace!("Attempted to fetch milestone {:?}", idx);
self.inner.storage().fetch(idx)
}

fn storage_insert_milestone(&self, idx: MilestoneIndex, milestone: &Milestone) -> Result<(), B::Error> {
trace!("Attempted to insert milestone {:?}", idx);
self.inner.storage().insert(&idx, milestone)?;
Ok(())
}
}
Loading

0 comments on commit c5d7800

Please sign in to comment.