Hello, I just started taking baby steps reading the code, beginning with sn_api
, in particular, this function in sn_api/src/api/app/safe_client.rs
puzzles me, so I wonder if someone could offer some hint?
pub async fn store_sequence(
&mut self,
data: &[u8],
name: Option<XorName>,
tag: u64,
_permissions: Option<String>,
private: bool,
) -> Result<XorName> {
debug!(
"Storing {} Sequence data with tag type: {:?}, xorname: {:?}",
if private { "Private" } else { "Public" },
tag,
name
);
let mut safe_client = self.get_safe_client()?;
let xorname = name.unwrap_or_else(rand::random);
info!("Xorname for storage: {:?}", &xorname);
let app_public_key = get_public_bls_key(&safe_client).await?;
// The Sequence's owner will be the user
let user_acc_owner = safe_client.public_key().await;
// Store the Sequence on the network
let address = if private {
// Set permissions for append, delete, and manage perms to this application
let mut perms = BTreeMap::default();
let _ = perms.insert(
SafeNdPublicKey::Bls(app_public_key),
SequencePrivatePermissions::new(true, true, true),
);
safe_client
.store_private_sequence(
Some(vec![data.to_vec()]),
xorname,
tag,
user_acc_owner,
perms,
)
.await
.map_err(|e| {
Error::NetDataError(format!("Failed to store Private Sequence data: {:?}", e))
})?
} else {
// Set permissions for append and manage perms to this application
let user_app = SequenceUser::Key(SafeNdPublicKey::Bls(app_public_key));
let mut perms = BTreeMap::default();
let _ = perms.insert(user_app, SequencePublicPermissions::new(true, true));
safe_client
.store_public_sequence(
Some(vec![data.to_vec()]),
xorname,
tag,
user_acc_owner,
perms,
)
.await
.map_err(|e| {
Error::NetDataError(format!("Failed to store Public Sequence data: {:?}", e))
})?
};
let _op = safe_client
.append_to_sequence(address, data.to_vec())
.await
.map_err(|e| {
Error::NetDataError(format!("Failed to append data to the Sequence: {:?}", e))
})?;
Ok(xorname)
}
The question is, why does the sequence data have to be stored twice? Namely, at the end there’s this append_to_sequence
call, but isn’t the data already stored before that?
My first guess was that, perhaps the initial “store” doesn’t actually store the data but rather creates a “placeholder”, and via its address, the subsequent “append” stores the actual data. But then when I looked into the store_private_sequence
function, it did seem to add the actual data… so by far, I still can’t understand it.
So I thought, maybe I should ask a question? And I hope this is the right place to do so?
Thanks for your attention.