Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: optimize cluster identification #3032

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions crates/subspace-farmer/src/farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use thiserror::Error;
use ulid::Ulid;

pub mod plotted_pieces;
#[cfg(test)]
mod tests;

/// Erased error type
pub type FarmError = Box<dyn std::error::Error + Send + Sync + 'static>;
Expand Down Expand Up @@ -499,6 +501,19 @@ impl FarmId {
pub fn new() -> Self {
Self::Ulid(Ulid::new())
}

/// Derive sub IDs
#[inline]
pub fn derive_sub_ids(&self, n: usize) -> Vec<Self> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes no sense to me, can you explain what this is supposed to mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a child id derived from a farmer or cache avoids the need to transfer farm ids, which would make it redundant to randomize new ids for each farm when the farmer or cache is already randomly generating ids.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still makes no sense to me, sorry.

This is farm ID, just one of the many farms the farmer is managing. Other farms have different IDs. I don't understand what this derivation has to do with any of that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove him if you don't think it's necessary, it doesn't affect the overall logic here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to understand why it was added in the first place and so far it doesn't really make sense to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does make it confusing, maybe I should change the type to FarmerId.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes no sense to me either way. You need to transfer the same information that was transferred before these changes anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way we need to know what farms are inside the farmer, recorded or derived, so you prefer to record the farms_id inside the farmer in the controller.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are already farm_ids of individual farms in the controller. What needs to be is to group them by farmer ID somehow and only transfer details once during initialization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what you mean. I'll try it later.

match self {
FarmId::Ulid(ulid) => {
let ulid = ulid.0;
(0..n as u128)
.map(|i| FarmId::Ulid(Ulid(ulid + i)))
.collect()
}
}
}
}

/// Abstract farm implementation
Expand Down
19 changes: 19 additions & 0 deletions crates/subspace-farmer/src/farm/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::farm::FarmId;

#[test]
fn derive_sub_farm_ids_test() {
let id = FarmId::new();
let sub_ids = id.derive_sub_ids(128);
assert_eq!(sub_ids.len(), 128);

match id {
FarmId::Ulid(id) => {
let id: u128 = id.into();
sub_ids.into_iter().zip(0..128u128).for_each(|(sub_id, i)| {
let FarmId::Ulid(sub_id) = sub_id;
let sub_id: u128 = sub_id.into();
assert_eq!(sub_id, id + i);
});
}
};
}