adding create_dht_record_with_owner

This commit is contained in:
eve.hobert 2024-04-06 13:18:24 -05:00 committed by eve.hobert
parent c198064b90
commit 3255352097
4 changed files with 56 additions and 4 deletions

View File

@ -235,6 +235,7 @@ impl StorageManager {
&self,
kind: CryptoKind,
schema: DHTSchema,
owner: Option<KeyPair>,
safety_selection: SafetySelection,
) -> VeilidAPIResult<DHTRecordDescriptor> {
let mut inner = self.lock().await?;
@ -242,7 +243,7 @@ impl StorageManager {
// Create a new owned local record from scratch
let (key, owner) = inner
.create_new_owned_local_record(kind, schema, safety_selection)
.create_new_owned_local_record(kind, schema, owner, safety_selection)
.await?;
// Now that the record is made we should always succeed to open the existing record

View File

@ -219,6 +219,7 @@ impl StorageManagerInner {
&mut self,
kind: CryptoKind,
schema: DHTSchema,
owner: Option<KeyPair>,
safety_selection: SafetySelection,
) -> VeilidAPIResult<(TypedKey, KeyPair)> {
// Get cryptosystem
@ -248,8 +249,8 @@ impl StorageManagerInner {
// Compile the dht schema
let schema_data = schema.compile();
// New values require a new owner key
let owner = vcrypto.generate_keypair();
// New values require a new owner key if not given
let owner = owner.unwrap_or_else(|| vcrypto.generate_keypair());
// Make a signed value descriptor for this dht value
let signed_value_descriptor = Arc::new(SignedValueDescriptor::make_signature(

View File

@ -71,6 +71,28 @@ pub async fn test_create_delete_dht_record_simple(api: VeilidAPI) {
rc.delete_dht_record(dht_key).await.unwrap();
}
pub async fn test_create_dht_record_with_owner(api: VeilidAPI) {
let rc = api
.routing_context()
.unwrap()
.with_safety(SafetySelection::Unsafe(Sequencing::EnsureOrdered))
.unwrap();
let cs = api.crypto().unwrap().get(CRYPTO_KIND_VLD0).unwrap();
let owner_keypair = cs.generate_keypair();
let rec = rc
.create_dht_record_with_owner(DHTSchema::dflt(1).unwrap(), owner_keypair, Some(CRYPTO_KIND_VLD0))
.await
.unwrap();
assert_eq!(rec.owner(), &owner_keypair.key);
let dht_key = *rec.key();
rc.close_dht_record(dht_key).await.unwrap();
rc.delete_dht_record(dht_key).await.unwrap();
}
pub async fn test_get_dht_value_nonexistent(api: VeilidAPI) {
let rc = api
.routing_context()
@ -340,6 +362,7 @@ pub async fn test_all() {
test_delete_dht_record_nonexistent(api.clone()).await;
test_get_dht_value_nonexistent(api.clone()).await;
test_create_delete_dht_record_simple(api.clone()).await;
test_create_dht_record_with_owner(api.clone()).await;
test_set_get_dht_value(api.clone()).await;
test_open_writer_dht_value(api.clone()).await;

View File

@ -245,7 +245,34 @@ impl RoutingContext {
Crypto::validate_crypto_kind(kind)?;
let storage_manager = self.api.storage_manager()?;
storage_manager
.create_record(kind, schema, self.unlocked_inner.safety_selection)
.create_record(kind, schema, None, self.unlocked_inner.safety_selection)
.await
}
/// Creates a new DHT record with a specified crypto kind, schema, and owner
/// This is intended to be used when you want to create a record with a given owner keypair rather than a randomly generated one.
///
/// Note: this is a deterministic way of creating a new record! This means that if you try to create a new record for a given owner and schema that already exists it *will* fail.
///
/// The record is considered 'open' after the create operation succeeds.
///
/// Returns the newly allocated DHT record's key if successful.
#[instrument(target = "veilid_api", level = "debug", ret, err)]
pub async fn create_dht_record_with_owner(
&self,
schema: DHTSchema,
owner: KeyPair,
kind: Option<CryptoKind>,
) -> VeilidAPIResult<DHTRecordDescriptor> {
event!(target: "veilid_api", Level::DEBUG,
"RoutingContext::create_dht_record_with_owner(self: {:?}, schema: {:?}, owner: {:?}, kind: {:?})", self, schema, owner, kind);
schema.validate()?;
let kind = kind.unwrap_or(best_crypto_kind());
Crypto::validate_crypto_kind(kind)?;
let storage_manager = self.api.storage_manager()?;
storage_manager
.create_record(kind, schema, Some(owner), self.unlocked_inner.safety_selection)
.await
}