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

feat: useable and tested scratchpads #2671

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

grumbach
Copy link
Member

@grumbach grumbach commented Jan 27, 2025

  • Now scratchpad have their own module clearly separated from Vault and Vaults use the generic Scratchpad API.
  • Exactly like Pointers, Scratchpads have a put/get/create/update API that allows for manual or user friendly use of the scratchpad.
  • E2E tests for Scratchpad to prove they work as intended. They could serve as usage examples as well.
  • GraphEntry, Pointer and Scratchpad use PaymentOption now, unifying the payment process

@grumbach grumbach enabled auto-merge January 27, 2025 05:36
@grumbach grumbach force-pushed the useable_scratchpads branch from e434114 to f4c263a Compare January 27, 2025 05:38
@grumbach grumbach force-pushed the useable_scratchpads branch from f4c263a to 28e8c26 Compare January 27, 2025 06:13
wallet,
.pay_for_content_addrs(
DataTypes::Pointer,
std::iter::once((xor_name, size_of::<Pointer>())),
Copy link
Contributor

Choose a reason for hiding this comment

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

size_of::<Pointer>() is the in-memory (Rust) size. Is that the same as the size of the data we store (serialized)? Or doesn't this need to be that accurately calculated/specified?

@@ -25,30 +25,79 @@ pub struct Scratchpad {
address: ScratchpadAddress,
/// Data encoding: custom apps using scratchpad should use this so they can identify the type of data they are storing
data_encoding: u64,
/// Contained data. This should be encrypted
/// Encrypted data stored in the scratchpad, it is encrypted automatically by the [`Scratchpad::new`] and [`Scratchpad::update_and_sign`] methods
Copy link
Member

Choose a reason for hiding this comment

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

any mut op (even create) will be signed, hence using consistent name of update shall be enough?

#[debug(skip)]
encrypted_data: Bytes,
/// Monotonically increasing counter to track the number of times this has been updated.
/// Monotonically increasing counter to track the number of times this has been updated. When pushed to the network, the scratchpad with the highest counter is kept.
Copy link
Member

Choose a reason for hiding this comment

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

better to have a new line for the new sentence (80/100 chars per line)

/// Create a new Scratchpad without provding the secret key
/// It is the caller's responsibility to ensure the signature is valid (signs [`Scratchpad::bytes_for_signature`]) and the data is encrypted
/// It is recommended to use the [`Scratchpad::new`] method instead when possible
pub fn new_with_signature(
Copy link
Member

Choose a reason for hiding this comment

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

to make the public API cleaner, this function shall not be exposed ?
also, this function means input_data is not encrypted, which is un-consistent with new function and claims made in other places.
also, this one doesn't call the is_valid self check ?

I'd suggest:

  • not use this in the public API
  • make new function use this (if retained as a private helper)

@@ -57,43 +106,32 @@ impl Scratchpad {
self.data_encoding
}

/// Increments the counter value.
pub fn increment(&mut self) -> u64 {
/// Updates the content, re-signs the scratchpad
Copy link
Member

Choose a reason for hiding this comment

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

better expand Updates the content to more detailed work done:

  • increment the counter
  • encrypt the input content and replace the existing encrypted data
  • sign the new fields and replace the existing signature


// pay for the scratchpad
let (receipt, _skipped_payments) = self
let xor_name = address.xorname();
let size = size_of::<Scratchpad>() + scratchpad.payload_size();
Copy link
Member

Choose a reason for hiding this comment

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

better make this as a utility function within the protocol ScratchPad definition mod.
to avoid future mismatched usage, for example, other places take scratchpad.bytes_for_sign().len() as the size of a scratchpad instance

let (receipt, _skipped_payments) = self
let xor_name = address.xorname();
let size = size_of::<Scratchpad>() + scratchpad.payload_size();
if size > MAX_SCRATCHPAD_SIZE {
Copy link
Member

Choose a reason for hiding this comment

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

make this as part of the ScratchPad::is_valid(), so that it can always be checked during create and update ?

counter,
signature,
};
debug_assert!(s.is_valid(), "Must be valid after being signed. This is a bug, please report it by opening an issue on our github");
Copy link
Member

Choose a reason for hiding this comment

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

not recommended to use assert in the production code.
better make this returns an error to report back detailed failures

DataTypes::Scratchpad.get_index(),
std::iter::once((scratch_xor, 256)),
DataTypes::Scratchpad,
std::iter::once((scratch_xor, size_of::<Scratchpad>())),
Copy link
Member

Choose a reason for hiding this comment

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

better make this as a const

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants