Skip to content

Commit f98fc0d

Browse files
authored
[FIL-865] Add endpoints to handle decrease allowance (#288)
* Add endpoints to handle decrease allowance
1 parent c75d099 commit f98fc0d

File tree

8 files changed

+367
-82
lines changed

8 files changed

+367
-82
lines changed

fplus-http-server/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ async fn main() -> std::io::Result<()> {
8787
.service(router::application::approve_storage_providers)
8888
.service(router::application::allocation_failed)
8989
.service(router::application::decline)
90-
.service(router::application::reopen_declined_application),
90+
.service(router::application::reopen_declined_application)
91+
.service(router::application::propose_decrease_allowance)
92+
.service(router::application::approve_decrease_allowance),
9193
)
9294
.service(router::application::merged)
9395
.service(router::application::active)

fplus-http-server/src/router/application.rs

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ use actix_web::{
44
};
55

66
use fplus_lib::core::{
7-
application::file::{StorageProviderChangeVerifier, VerifierInput},
7+
application::file::{
8+
DecreaseClientAllowanceVerifier, StorageProviderChangeVerifier, VerifierInput,
9+
},
810
ApplicationQueryParams, BranchDeleteInfo, CompleteGovernanceReviewInfo,
911
CompleteNewApplicationApprovalInfo, CompleteNewApplicationProposalInfo, CreateApplicationInfo,
10-
DcReachedInfo, GithubQueryParams, LDNApplication, MoreInfoNeeded, NotifyRefillInfo,
11-
StorageProvidersChangeApprovalInfo, StorageProvidersChangeProposalInfo, SubmitKYCInfo,
12-
TriggerSSAInfo, ValidationPullRequestData, VerifierActionsQueryParams,
12+
DcReachedInfo, DecreaseAllowanceApprovalInfo, DecreaseAllowanceProposalInfo, GithubQueryParams,
13+
LDNApplication, MoreInfoNeeded, NotifyRefillInfo, StorageProvidersChangeApprovalInfo,
14+
StorageProvidersChangeProposalInfo, SubmitKYCInfo, TriggerSSAInfo, ValidationPullRequestData,
15+
VerifierActionsQueryParams,
1316
};
1417

1518
#[post("/application")]
@@ -232,6 +235,62 @@ pub async fn approve_storage_providers(
232235
))
233236
}
234237

238+
#[post("/application/propose_decrease_allowance")]
239+
pub async fn propose_decrease_allowance(
240+
info: web::Json<DecreaseAllowanceProposalInfo>,
241+
query: web::Query<VerifierActionsQueryParams>,
242+
) -> actix_web::Result<impl Responder> {
243+
let ldn_application =
244+
LDNApplication::load(query.id.clone(), query.owner.clone(), query.repo.clone())
245+
.await
246+
.map_err(ErrorNotFound)?;
247+
let verifier = DecreaseClientAllowanceVerifier {
248+
github_username: query.github_username.clone(),
249+
signing_address: info.signer.signing_address.clone(),
250+
decrease_allowance_cid: info.signer.decrease_allowance_cid.clone(),
251+
};
252+
253+
ldn_application
254+
.propose_decrease_allowance(
255+
&verifier,
256+
&query.owner,
257+
&query.repo,
258+
&info.amount_to_decrease,
259+
&info.reason_for_decrease,
260+
)
261+
.await
262+
.map_err(ErrorInternalServerError)?;
263+
Ok(HttpResponse::Ok().body(
264+
serde_json::to_string_pretty("Success")
265+
.expect("Serialization of static string should succeed"),
266+
))
267+
}
268+
269+
#[post("/application/approve_decrease_allowance")]
270+
pub async fn approve_decrease_allowance(
271+
info: web::Json<DecreaseAllowanceApprovalInfo>,
272+
query: web::Query<VerifierActionsQueryParams>,
273+
) -> actix_web::Result<impl Responder> {
274+
let ldn_application =
275+
LDNApplication::load(query.id.clone(), query.owner.clone(), query.repo.clone())
276+
.await
277+
.map_err(ErrorNotFound)?;
278+
let verifier = DecreaseClientAllowanceVerifier {
279+
github_username: query.github_username.clone(),
280+
signing_address: info.signer.signing_address.clone(),
281+
decrease_allowance_cid: info.signer.decrease_allowance_cid.clone(),
282+
};
283+
284+
ldn_application
285+
.approve_decrease_allowance(&verifier, &query.owner, &query.repo, &info.request_id)
286+
.await
287+
.map_err(ErrorInternalServerError)?;
288+
Ok(HttpResponse::Ok().body(
289+
serde_json::to_string_pretty("Success")
290+
.expect("Serialization of static string should succeed"),
291+
))
292+
}
293+
235294
#[post("/application/approve")]
236295
pub async fn approve(
237296
query: web::Query<VerifierActionsQueryParams>,

fplus-lib/src/core/application/allocation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ impl Allocations {
143143
self.clone()
144144
}
145145

146-
pub fn total_requested(&self) -> u64 {
147-
let mut total_amount: u64 = 0;
146+
pub fn total_requested(&self) -> i64 {
147+
let mut total_amount: i64 = 0;
148148
for allocation in self.0.iter() {
149-
if let Some(amount) = parse_size_to_bytes(&allocation.amount) {
149+
if let Ok(amount) = parse_size_to_bytes(&allocation.amount) {
150150
total_amount += amount;
151151
}
152152
}

fplus-lib/src/core/application/file.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{collections::HashMap, str::FromStr};
22

3+
use chrono::Utc;
34
use serde::{Deserialize, Serialize};
45

56
use crate::error::LDNError;
@@ -274,6 +275,7 @@ pub enum AppState {
274275
TotalDatacapReached,
275276
Declined,
276277
ChangingSP,
278+
DecreasingDataCap,
277279
Error,
278280
}
279281

@@ -309,6 +311,7 @@ pub enum AllocationRequestType {
309311
First,
310312
Removal,
311313
Refill(u8),
314+
Decrease,
312315
}
313316

314317
impl std::fmt::Display for AllocationRequestType {
@@ -317,6 +320,7 @@ impl std::fmt::Display for AllocationRequestType {
317320
AllocationRequestType::First => write!(f, "First"),
318321
AllocationRequestType::Removal => write!(f, "Removal"),
319322
AllocationRequestType::Refill(_) => write!(f, "Refill"),
323+
AllocationRequestType::Decrease => write!(f, "Decrease"),
320324
}
321325
}
322326
}
@@ -436,6 +440,13 @@ pub struct VerifierInput {
436440
pub increase_allowance_cid: Option<String>,
437441
}
438442

443+
#[derive(Serialize, Deserialize, Debug, Clone)]
444+
pub struct DecreaseClientAllowanceVerifier {
445+
pub github_username: String,
446+
pub signing_address: String,
447+
pub decrease_allowance_cid: String,
448+
}
449+
439450
impl From<VerifierInput> for Verifier {
440451
fn from(input: VerifierInput) -> Self {
441452
Self {
@@ -448,6 +459,18 @@ impl From<VerifierInput> for Verifier {
448459
}
449460
}
450461

462+
impl From<&DecreaseClientAllowanceVerifier> for Verifier {
463+
fn from(input: &DecreaseClientAllowanceVerifier) -> Self {
464+
Self {
465+
github_username: input.github_username.clone(),
466+
signing_address: input.signing_address.clone(),
467+
created_at: Utc::now().to_string(),
468+
message_cid: input.decrease_allowance_cid.clone(),
469+
increase_allowance_cid: None,
470+
}
471+
}
472+
}
473+
451474
#[derive(Serialize, Deserialize, Debug, Clone)]
452475
pub struct Verifier {
453476
#[serde(rename = "Github Username")]

fplus-lib/src/core/application/lifecycle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ impl AppState {
1616
AppState::TotalDatacapReached => "total datacap reached",
1717
AppState::Declined => "declined",
1818
AppState::ChangingSP => "changing SPs",
19+
AppState::DecreasingDataCap => "decreasing datacap",
1920
AppState::Error => "error",
2021
}
2122
}

fplus-lib/src/core/application/mod.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::core::application::file::DecreaseClientAllowanceVerifier;
12
use file::{AppState, SpsChangeRequest, SpsChangeRequests};
23

34
use self::file::{AllocationRequest, Allocations, LifeCycle, Verifier, Version};
@@ -137,6 +138,32 @@ impl file::ApplicationFile {
137138
}
138139
}
139140

141+
pub fn start_decrease_request(
142+
&self,
143+
allocation_request: &AllocationRequest,
144+
verifier: &DecreaseClientAllowanceVerifier,
145+
request_id: &str,
146+
app_state: &AppState,
147+
) -> Self {
148+
let allocations = self.allocation.clone().push(allocation_request.clone());
149+
150+
let allocations_after_sign = if *app_state == AppState::Granted {
151+
allocations.add_signer_and_complete(request_id.into(), verifier.into())
152+
} else {
153+
allocations.add_signer(&allocation_request.id, verifier.into())
154+
};
155+
let updated_lifecycle = self.lifecycle.update_lifecycle_after_sign(
156+
app_state,
157+
&verifier.github_username,
158+
&request_id.to_string(),
159+
);
160+
Self {
161+
allocation: allocations_after_sign,
162+
lifecycle: updated_lifecycle,
163+
..self.clone()
164+
}
165+
}
166+
140167
pub fn handle_changing_sps_request(
141168
&mut self,
142169
validated_by: &String,
@@ -208,10 +235,12 @@ impl file::ApplicationFile {
208235
let new_allocation = self
209236
.allocation
210237
.clone()
211-
.add_signer_and_complete(request_id, signer);
238+
.add_signer_and_complete(request_id, signer.clone());
239+
let lifecycle_after_complete =
240+
app_lifecycle.finish_grant_datacap_approval(&signer.github_username);
212241
Self {
213242
allocation: new_allocation,
214-
lifecycle: app_lifecycle,
243+
lifecycle: lifecycle_after_complete,
215244
..self.clone()
216245
}
217246
}

0 commit comments

Comments
 (0)