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

Add pieces verification to farmers. #725

Merged
merged 19 commits into from Aug 4, 2022
Merged

Add pieces verification to farmers. #725

merged 19 commits into from Aug 4, 2022

Conversation

shamil-gadelshin
Copy link
Member

This PR aims to add pieces verification to the DSN sync on farmers. It introduces an additional RPC endpoint on the node side to get the records_root.

Original issue: #709

Changes

  • records_root endpoint for SubspaceRpcApi in the sc-consensus-subspace-rpc crate
  • new pieces_verification module in the subspace-farmer
  • RpcClient addition for the records_root in the subspace-farmer

Comments

  • This is a proof-of-concept, no optimizations like parallel requests, parameter batching were implemented. Please, add them in the PR-comments.
  • No manual testing was made because of the non-working --dsn-sync flag in the main branch.

Code contributor checklist:

  • I have reviewed my own changes one more time to spot typos, unintended changes, following project conventions, etc.
  • I have prepared clean readable history of commits before submitting this PR to make reviewer's life easier
  • I have tested my changes and/or added corresponding test cases (if relevant)
  • I understand that any changes to this PR going forward will notify multiple developers and will try to minimize them
  • I have added sufficient description of changes to make review process easier
  • This PR is ready for review by developers

@shamil-gadelshin shamil-gadelshin added farmer Farming library/app networking Subspace networking (DSN) node Node (service library/node app) labels Jul 27, 2022
@shamil-gadelshin shamil-gadelshin self-assigned this Jul 27, 2022
Copy link
Member

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

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

Generally in line with my expectations, no major issues

@@ -95,6 +97,9 @@ pub trait SubspaceRpcApi {

#[method(name = "subspace_acknowledgeArchivedSegment")]
async fn acknowledge_archived_segment(&self, segment_index: u64) -> RpcResult<()>;

#[method(name = "subspace_recordsRoot")]
async fn records_root(&self, segment_index: u64) -> RpcResult<Option<Sha256Hash>>;
Copy link
Member

Choose a reason for hiding this comment

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

I think this should take a vector of segment indexes and return a vector of roots to improve efficiency, otherwise there might be too many requests necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, sure. However, it seems vulnerable to a large indexes vector. It makes sense to introduce MAX_SEGMENT_INDEXES_PER_REQUEST const to limit both farmer and node sides.

Copy link
Member

Choose a reason for hiding this comment

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

Certainly. Requester needs to limit number of roots requested or else response will not fit into max size configured in RPC server and response will fail. For public RPC endpoints we also may want to limit this, so yeah, makes sense to have certain limits on both sides.

Comment on lines 38 to 39
let segment_index: u64 = piece_index / MERKLE_NUM_LEAVES as u64;
let position: u64 = piece_index % MERKLE_NUM_LEAVES as u64;
Copy link
Member

Choose a reason for hiding this comment

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

MERKLE_NUM_LEAVES and RECORD_SIZE should be derived from FarmerProtocolInfo instead, it allows us to have different values in tests and generally make implementation on the library level more flexible. This is the first time these constants are used on the farmer specifically.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. Please, verify the new algorithm.

crates/subspace-farmer/src/pieces_verification.rs Outdated Show resolved Hide resolved
crates/subspace-farmer/src/single_plot_farm.rs Outdated Show resolved Hide resolved
crates/subspace-farmer/src/single_plot_farm.rs Outdated Show resolved Hide resolved
crates/subspace-farmer/src/single_plot_farm.rs Outdated Show resolved Hide resolved
i1i1
i1i1 previously approved these changes Jul 28, 2022
Copy link
Contributor

@i1i1 i1i1 left a comment

Choose a reason for hiding this comment

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

LGTM

crates/subspace-farmer/src/dsn.rs Outdated Show resolved Hide resolved
crates/sc-consensus-subspace-rpc/src/lib.rs Show resolved Hide resolved
i1i1
i1i1 previously approved these changes Jul 28, 2022
crates/subspace-farmer/src/dsn/tests.rs Outdated Show resolved Hide resolved
Comment on lines 40 to 42
let records_per_segment =
farmer_protocol_info.recorded_history_segment_size as usize / PIECE_SIZE;
if farmer_protocol_info.record_size.is_zero() || records_per_segment.is_zero() {
Copy link
Member

Choose a reason for hiding this comment

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

It would be preferable to change record_size and recorded_history_segment_size to NonZeroU32, then you don't ever need to check it. At least in FarmerProtocolInfo 🙂

It is always better to make invalid invariants non-representable in the first place. Though I don't think these two were ever zero anywhere in our codebase anyway. And segment size was always bigger than piece size.

crates/subspace-farmer/src/single_plot_farm.rs Outdated Show resolved Hide resolved
crates/subspace-farmer/src/single_plot_farm.rs Outdated Show resolved Hide resolved
@@ -95,6 +97,9 @@ pub trait SubspaceRpcApi {

#[method(name = "subspace_acknowledgeArchivedSegment")]
async fn acknowledge_archived_segment(&self, segment_index: u64) -> RpcResult<()>;

#[method(name = "subspace_recordsRoot")]
async fn records_root(&self, segment_index: u64) -> RpcResult<Option<Sha256Hash>>;
Copy link
Member

Choose a reason for hiding this comment

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

Certainly. Requester needs to limit number of roots requested or else response will not fit into max size configured in RPC server and response will fail. For public RPC endpoints we also may want to limit this, so yeah, makes sense to have certain limits on both sides.

@shamil-gadelshin
Copy link
Member Author

The latest changes comment:

  • fixed review comments
  • I changed the pieces verification formula to the a recommended one (merkle_num_leaves)
  • added a minor refactoring for rpc_client_* files to reduce files in the farmer crates's root folder
  • I tested pieces verification locally with node and two farmers. DSN sync and verification worked. However, it became clear that one incorrect piece in a batch could break the sync process for an entire batch in the current simple implementation. It's probably a good idea to create a more complex sync-algorithm with more options like retry and partially successful verification.

# Conflicts:
#	crates/subspace-farmer/src/single_plot_farm.rs
@shamil-gadelshin shamil-gadelshin marked this pull request as ready for review July 29, 2022 16:19
Copy link
Member

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

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

Something broke DSN test reliably and needs to be fixed

crates/sc-consensus-subspace-rpc/src/lib.rs Outdated Show resolved Hide resolved
crates/subspace-farmer/src/rpc_client/mod.rs Outdated Show resolved Hide resolved
- refactor rpc_client module in the subspace-farmer
- fix error comment in sc-consensus-subspace-rpc
- fix broken test
@shamil-gadelshin
Copy link
Member Author

Something broke DSN test reliably and needs to be fixed

Fixed.

@shamil-gadelshin shamil-gadelshin merged commit dd02054 into main Aug 4, 2022
@shamil-gadelshin shamil-gadelshin deleted the verify-pieces branch August 4, 2022 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
farmer Farming library/app networking Subspace networking (DSN) node Node (service library/node app)
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants