Skip to content

Commit

Permalink
Support for upcoming smart-contract-based allocators.
Browse files Browse the repository at this point in the history
What: this PR stores "address" and "tooling" fields from allocator JSON
files in database and exposes to clients via API.

Why: smart-contract allocators will need to make a different call
on-chain to grant datacap to a client. Instead of calling verifreg
directly, they'll need to call smart contract which will do it on their
behalf. Address of this smart contract is stored in the "address" field
and we need the "tooling" field to know when to use the new approach.
  • Loading branch information
kacperzuk-neti committed Jun 6, 2024
1 parent 7ef514a commit 81747a5
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 10 deletions.
38 changes: 35 additions & 3 deletions fplus-database/src/database/allocators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub async fn get_allocators() ->Result<Vec<AllocatorModel>, sea_orm::DbErr> {
* @param multisig_address: Option<String> - The multisig address
* @param verifiers_gh_handles: Option<String> - The GitHub handles of the verifiers
* @param multisig_threshold: Option<i32> - The multisig threshold
* @param address: Option<String> - Address of the Allocator
* @param tooling: Option<String> - Supported tooling
*
* # Returns
* @return Result<AllocatorModel, sea_orm::DbErr> - The result of the operation
Expand All @@ -33,7 +35,9 @@ pub async fn update_allocator(
installation_id: Option<i64>,
multisig_address: Option<String>,
verifiers_gh_handles: Option<String>,
multisig_threshold: Option<i32>
multisig_threshold: Option<i32>,
address: Option<String>,
tooling: Option<String>,
) -> Result<AllocatorModel, sea_orm::DbErr> {
let conn = get_database_connection().await?;

Expand All @@ -45,7 +49,7 @@ pub async fn update_allocator(
if Some(installation_id) != None {
allocator_active_model.installation_id = Set(installation_id);
}

if Some(multisig_address.clone()) != None {
allocator_active_model.multisig_address = Set(multisig_address);
}
Expand All @@ -58,6 +62,14 @@ pub async fn update_allocator(
allocator_active_model.multisig_threshold = Set(multisig_threshold);
}

if address.is_some() {
allocator_active_model.address = Set(address);
}

if tooling.is_some() {
allocator_active_model.tooling = Set(tooling);
}

let updated_model = allocator_active_model.update(&conn).await?;

Ok(updated_model)
Expand Down Expand Up @@ -97,6 +109,8 @@ pub async fn get_allocator(
* @param installation_id: Option<i64> - The installation ID
* @param multisig_address: Option<String> - The multisig address
* @param verifiers_gh_handles: Option<String> - The GitHub handles of the verifiers
* @param address: Option<String> - Address of the Allocator
* @param tooling: Option<String> - Supported tooling
*
* # Returns
* @return Result<AllocatorModel, sea_orm::DbErr> - The result of the operation
Expand All @@ -108,7 +122,9 @@ pub async fn create_or_update_allocator(
multisig_address: Option<String>,
verifiers_gh_handles: Option<String>,
multisig_threshold: Option<i32>,
allocation_amount_type: Option<String>
allocation_amount_type: Option<String>,
address: Option<String>,
tooling: Option<String>,
) -> Result<AllocatorModel, sea_orm::DbErr> {

let existing_allocator = get_allocator(&owner, &repo).await?;
Expand Down Expand Up @@ -138,6 +154,14 @@ pub async fn create_or_update_allocator(
allocator_active_model.allocation_amount_type = Set(None);
}

if address.is_some() {
allocator_active_model.address = Set(address);
}

if tooling.is_some() {
allocator_active_model.tooling = Set(tooling);
}

let updated_model = allocator_active_model.update(&conn).await?;

Ok(updated_model)
Expand Down Expand Up @@ -170,6 +194,14 @@ pub async fn create_or_update_allocator(
new_allocator.allocation_amount_type = Set(None);
}

if address.is_some() {
new_allocator.address = Set(address);
}

if tooling.is_some() {
new_allocator.tooling = Set(tooling);
}

let conn = get_database_connection().await.expect("Failed to get DB connection");
let insert_result = new_allocator.insert(&conn).await;
println!("Allocator inserted: {:?}", insert_result);
Expand Down
24 changes: 20 additions & 4 deletions fplus-database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ mod tests {
let verifiers_gh_handles = Some("test_verifier_1, test_verifier_2".to_string());
let multisig_threshold = Some(2);
let amount_type = Some("Fixed".to_string());
let address = Some("0x1234567890".to_string());
let tooling = Some("common_ui, smart_contract_allocator".to_string());

let result = database::allocators::create_or_update_allocator(
owner,
Expand All @@ -113,7 +115,9 @@ mod tests {
multisig_address,
verifiers_gh_handles,
multisig_threshold,
amount_type
amount_type,
address,
tooling
).await;
assert!(result.is_ok());
}
Expand Down Expand Up @@ -156,6 +160,8 @@ mod tests {
let verifiers_gh_handles = Some("test_verifier_1, test_verifier_2".to_string());
let multisig_threshold = Some(2);
let amount_type = Some("Fixed".to_string());
let address = Some("0x1234567890".to_string());
let tooling = Some("common_ui, smart_contract_allocator".to_string());

let result = database::allocators::create_or_update_allocator(
owner.clone(),
Expand All @@ -164,7 +170,9 @@ mod tests {
multisig_address,
verifiers_gh_handles,
multisig_threshold,
amount_type
amount_type,
address,
tooling,
).await;
assert!(result.is_ok());
}
Expand All @@ -175,14 +183,18 @@ mod tests {
let multisig_address = Some("0x0987654321".to_string());
let verifiers_gh_handles = Some("test_verifier_3, test_verifier_4".to_string());
let multisig_threshold = Some(1);
let address = Some("0xababababa".to_string());
let tooling = Some("common_ui".to_string());

let result = database::allocators::update_allocator(
&owner,
&repo,
installation_id,
multisig_address,
verifiers_gh_handles,
multisig_threshold
multisig_threshold,
address,
tooling
).await;
assert!(result.is_ok());
}
Expand Down Expand Up @@ -229,6 +241,8 @@ mod tests {
let verifiers_gh_handles = Some("test_verifier_1, test_verifier_2".to_string());
let multisig_threshold = Some(2);
let amount_type = Some("Fixed".to_string());
let address = Some("0x1234567890".to_string());
let tooling = Some("common_ui, smart_contract_allocator".to_string());

let result = database::allocators::create_or_update_allocator(
owner.clone(),
Expand All @@ -237,7 +251,9 @@ mod tests {
multisig_address,
verifiers_gh_handles,
multisig_threshold,
amount_type
amount_type,
address,
tooling,
).await;

assert!(result.is_ok());
Expand Down
2 changes: 2 additions & 0 deletions fplus-database/src/models/allocators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct Model {
pub verifiers_gh_handles: Option<String>,
pub multisig_threshold: Option<i32>,
pub allocation_amount_type: Option<String>,
pub address: Option<String>,
pub tooling: Option<String>,
}

#[derive(Copy, Clone, Debug, EnumIter)]
Expand Down
13 changes: 11 additions & 2 deletions fplus-http-server/src/router/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ pub async fn create_from_json(files: web::Json<ChangedAllocators>) -> actix_web:
} else {
Some(model.application.verifiers_gh_handles.join(", ")) // Join verifiers in a string if exists
};
let tooling = if model.application.tooling.is_empty() {
None
} else {
Some(model.application.tooling.join(", "))
};
let owner = model.owner.clone().unwrap_or_default().to_string();
let repo = model.repo.clone().unwrap_or_default().to_string();

Expand All @@ -84,7 +89,9 @@ pub async fn create_from_json(files: web::Json<ChangedAllocators>) -> actix_web:
Some(model.pathway_addresses.msig),
verifiers_gh_handles,
model.multisig_threshold,
model.application.allocation_amount.clone().map(|a| a.amount_type.clone()).flatten() // Flattens Option<Option<String>> to Option<String>
model.application.allocation_amount.clone().map(|a| a.amount_type.clone()).flatten(), // Flattens Option<Option<String>> to Option<String>
model.address,
tooling,
).await;

match allocator_creation_result {
Expand Down Expand Up @@ -175,7 +182,9 @@ pub async fn update(
None,
info.multisig_address.clone(),
info.verifiers_gh_handles.clone(),
info.multisig_threshold
info.multisig_threshold,
info.address.clone(),
info.tooling.clone(),
).await {
Ok(allocator_model) => HttpResponse::Ok().json(allocator_model),
Err(e) => {
Expand Down
2 changes: 2 additions & 0 deletions fplus-lib/src/core/allocator/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct AllocatorModel {
pub pathway_addresses: AllocatorModelPathwayAddresses,
pub owner: Option<String>,
pub repo: Option<String>,
pub address: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -21,6 +22,7 @@ pub struct Application {
pub verifiers_gh_handles: Vec<String>,
pub allocation_bookkeeping: String,
pub allocation_amount: Option<AllocationAmount>,
pub tooling: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion fplus-lib/src/core/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ pub async fn update_installation_ids_in_db(installation: InstallationRepositorie
for repo in installation.repositories.iter() {
let owner = repo.owner.clone();
let repo = repo.slug.clone();
let _ = create_or_update_allocator(owner, repo, Some(installation_id.try_into().unwrap()), None, None, None, None).await;
let _ = create_or_update_allocator(owner, repo, Some(installation_id.try_into().unwrap()), None, None, None, None, None, None).await;
}
}

Expand Down
2 changes: 2 additions & 0 deletions fplus-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ pub struct AllocatorUpdateInfo {
pub multisig_address: Option<String>,
pub verifiers_gh_handles: Option<String>,
pub multisig_threshold: Option<i32>,
pub address: Option<String>,
pub tooling: Option<String>,
}

#[derive(Deserialize)]
Expand Down

0 comments on commit 81747a5

Please sign in to comment.