Skip to content

Commit 595cff5

Browse files
Filip-Lfilip-neti
andauthored
[FIL-365] Update allowed SPs (#237)
* Update allowed SPs --------- Co-authored-by: Filip Lelek <[email protected]>
1 parent 67fcec6 commit 595cff5

File tree

8 files changed

+550
-22
lines changed

8 files changed

+550
-22
lines changed

fplus-http-server/src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ async fn main() -> std::io::Result<()> {
8989
.service(router::application::trigger_ssa)
9090
.service(router::application::request_kyc)
9191
.service(router::application::remove_pending_allocation)
92+
.service(router::application::propose_storage_providers)
93+
.service(router::application::approve_storage_providers)
9294
.service(router::application::allocation_failed),
9395
)
9496
.service(router::application::merged)

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

+82-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use actix_web::{get, post, web, HttpResponse, Responder};
22
use fplus_lib::core::{
3-
application::file::VerifierInput, ApplicationQueryParams, BranchDeleteInfo,
4-
CompleteGovernanceReviewInfo, CompleteNewApplicationApprovalInfo,
5-
CompleteNewApplicationProposalInfo, CreateApplicationInfo, DcReachedInfo, GithubQueryParams,
6-
LDNApplication, MoreInfoNeeded, NotifyRefillInfo, SubmitKYCInfo, TriggerSSAInfo,
7-
ValidationPullRequestData, VerifierActionsQueryParams,
3+
application::file::{StorageProviderChangeVerifier, VerifierInput},
4+
ApplicationQueryParams, BranchDeleteInfo, CompleteGovernanceReviewInfo,
5+
CompleteNewApplicationApprovalInfo, CompleteNewApplicationProposalInfo, CreateApplicationInfo,
6+
DcReachedInfo, GithubQueryParams, LDNApplication, MoreInfoNeeded, NotifyRefillInfo,
7+
StorageProvidersChangeApprovalInfo, StorageProvidersChangeProposalInfo, SubmitKYCInfo,
8+
TriggerSSAInfo, ValidationPullRequestData, VerifierActionsQueryParams,
89
};
910

1011
#[post("/application")]
@@ -132,6 +133,82 @@ pub async fn propose(
132133
}
133134
}
134135

136+
#[post("/application/propose_storage_providers")]
137+
pub async fn propose_storage_providers(
138+
info: web::Json<StorageProvidersChangeProposalInfo>,
139+
query: web::Query<VerifierActionsQueryParams>,
140+
) -> impl Responder {
141+
let StorageProvidersChangeProposalInfo {
142+
signer,
143+
allowed_sps,
144+
max_deviation,
145+
} = info.into_inner();
146+
let ldn_application =
147+
match LDNApplication::load(query.id.clone(), query.owner.clone(), query.repo.clone()).await
148+
{
149+
Ok(app) => app,
150+
Err(e) => {
151+
return HttpResponse::BadRequest().body(e.to_string());
152+
}
153+
};
154+
let verifier = StorageProviderChangeVerifier {
155+
github_username: query.github_username.clone(),
156+
signing_address: signer.signing_address.clone(),
157+
max_deviation_cid: signer.max_deviation_cid.clone(),
158+
add_allowed_sps_cids: signer.allowed_sps_cids.clone(),
159+
remove_allowed_sps_cids: signer.removed_allowed_sps_cids.clone(),
160+
};
161+
162+
match ldn_application
163+
.complete_sps_change_proposal(
164+
verifier,
165+
query.owner.clone(),
166+
query.repo.clone(),
167+
allowed_sps,
168+
max_deviation,
169+
)
170+
.await
171+
{
172+
Ok(_) => HttpResponse::Ok().body(serde_json::to_string_pretty("Success").unwrap()),
173+
Err(e) => HttpResponse::BadRequest().body(e.to_string()),
174+
}
175+
}
176+
177+
#[post("/application/approve_storage_providers")]
178+
pub async fn approve_storage_providers(
179+
info: web::Json<StorageProvidersChangeApprovalInfo>,
180+
query: web::Query<VerifierActionsQueryParams>,
181+
) -> impl Responder {
182+
let StorageProvidersChangeApprovalInfo { signer, request_id } = info.into_inner();
183+
let ldn_application =
184+
match LDNApplication::load(query.id.clone(), query.owner.clone(), query.repo.clone()).await
185+
{
186+
Ok(app) => app,
187+
Err(e) => {
188+
return HttpResponse::BadRequest().body(e.to_string());
189+
}
190+
};
191+
let verifier = StorageProviderChangeVerifier {
192+
github_username: query.github_username.clone(),
193+
signing_address: signer.signing_address.clone(),
194+
max_deviation_cid: signer.max_deviation_cid.clone(),
195+
add_allowed_sps_cids: signer.allowed_sps_cids.clone(),
196+
remove_allowed_sps_cids: signer.removed_allowed_sps_cids.clone(),
197+
};
198+
match ldn_application
199+
.complete_sps_change_approval(
200+
verifier,
201+
query.owner.clone(),
202+
query.repo.clone(),
203+
request_id,
204+
)
205+
.await
206+
{
207+
Ok(_) => HttpResponse::Ok().body(serde_json::to_string_pretty("Success").unwrap()),
208+
Err(e) => HttpResponse::BadRequest().body(e.to_string()),
209+
}
210+
}
211+
135212
#[post("/application/approve")]
136213
pub async fn approve(
137214
query: web::Query<VerifierActionsQueryParams>,

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

+61-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::str::FromStr;
1+
use std::{collections::HashMap, str::FromStr};
22

33
use serde::{Deserialize, Serialize};
44

@@ -48,7 +48,10 @@ pub struct ApplicationFile {
4848
pub id: String,
4949
#[serde(rename = "Issue Number")]
5050
pub issue_number: String,
51-
#[serde(rename = "Client Contract Address")]
51+
#[serde(
52+
rename = "Client Contract Address",
53+
skip_serializing_if = "Option::is_none"
54+
)]
5255
pub client_contract_address: Option<String>,
5356
#[serde(rename = "Client")]
5457
pub client: Client,
@@ -60,6 +63,11 @@ pub struct ApplicationFile {
6063
pub lifecycle: LifeCycle,
6164
#[serde(rename = "Allocation Requests")]
6265
pub allocation: Allocations,
66+
#[serde(
67+
rename = "Storage Providers Change Requests",
68+
skip_serializing_if = "Option::is_none"
69+
)]
70+
pub allowed_sps: Option<SpsChangeRequests>,
6371
}
6472

6573
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
@@ -254,6 +262,7 @@ pub enum AppState {
254262
StartSignDatacap,
255263
Granted,
256264
TotalDatacapReached,
265+
ChangingSP,
257266
Error,
258267
}
259268

@@ -281,6 +290,9 @@ pub struct LifeCycle {
281290
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
282291
pub struct Allocations(pub Vec<Allocation>);
283292

293+
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
294+
pub struct SpsChangeRequests(pub Vec<SpsChangeRequest>);
295+
284296
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
285297
pub enum AllocationRequestType {
286298
First,
@@ -316,6 +328,27 @@ pub struct Allocation {
316328
pub signers: Verifiers,
317329
}
318330

331+
#[derive(Serialize, Deserialize, Debug, Clone)]
332+
pub struct SpsChangeRequest {
333+
#[serde(rename = "ID")]
334+
pub id: String,
335+
#[serde(rename = "Created At")]
336+
pub created_at: String,
337+
#[serde(rename = "Updated At")]
338+
pub updated_at: String,
339+
#[serde(rename = "Active")]
340+
pub is_active: bool,
341+
#[serde(
342+
rename = "Allowed Storage Providers",
343+
skip_serializing_if = "Option::is_none"
344+
)]
345+
pub allowed_sps: Option<Vec<u64>>,
346+
#[serde(rename = "Max Deviation", skip_serializing_if = "Option::is_none")]
347+
pub max_deviation: Option<String>,
348+
#[serde(rename = "Signers")]
349+
pub signers: StorageProviderChangeVerifiers,
350+
}
351+
319352
impl ApplicationFile {
320353
pub fn remove_active_allocation(&mut self) {
321354
self.allocation.0.retain(|alloc| !alloc.is_active);
@@ -365,6 +398,9 @@ impl ApplicationFile {
365398
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
366399
pub struct Verifiers(pub Vec<Verifier>);
367400

401+
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
402+
pub struct StorageProviderChangeVerifiers(pub Vec<StorageProviderChangeVerifier>);
403+
368404
#[derive(Serialize, Deserialize, Debug, Clone)]
369405
pub struct VerifierInput {
370406
pub github_username: String,
@@ -396,6 +432,29 @@ pub struct Verifier {
396432
pub message_cid: String,
397433
}
398434

435+
#[derive(Serialize, Deserialize, Debug, Clone)]
436+
pub struct StorageProviderChangeVerifier {
437+
#[serde(rename = "Github Username")]
438+
pub github_username: String,
439+
#[serde(rename = "Signing Address")]
440+
pub signing_address: String,
441+
#[serde(
442+
rename = "Set Max Deviation CID",
443+
skip_serializing_if = "Option::is_none"
444+
)]
445+
pub max_deviation_cid: Option<String>,
446+
#[serde(
447+
rename = "Add Allowed Storage Providers CID",
448+
skip_serializing_if = "Option::is_none"
449+
)]
450+
pub add_allowed_sps_cids: Option<HashMap<String, Vec<String>>>,
451+
#[serde(
452+
rename = "Remove Allowed Storage Providers CID",
453+
skip_serializing_if = "Option::is_none"
454+
)]
455+
pub remove_allowed_sps_cids: Option<HashMap<String, Vec<String>>>,
456+
}
457+
399458
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
400459
pub struct AllocationRequest {
401460
pub actor: String,

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

+17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ impl AppState {
1414
AppState::StartSignDatacap => "start sign datacap",
1515
AppState::Granted => "granted",
1616
AppState::TotalDatacapReached => "total datacap reached",
17+
AppState::ChangingSP => "changing SPs",
1718
AppState::Error => "error",
1819
}
1920
}
@@ -64,6 +65,22 @@ impl LifeCycle {
6465
}
6566
}
6667

68+
pub fn update_lifecycle_after_sign(
69+
&self,
70+
state: &AppState,
71+
validated_by: &String,
72+
request_id: &String,
73+
) -> Self {
74+
LifeCycle {
75+
state: state.clone(),
76+
updated_at: Utc::now().to_string(),
77+
validated_by: validated_by.into(),
78+
validated_at: Utc::now().to_string(),
79+
active_request: Some(request_id.into()),
80+
..self.clone()
81+
}
82+
}
83+
6784
pub fn finish_approval(&self) -> Self {
6885
LifeCycle {
6986
state: AppState::Granted,

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

+47
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use file::{AppState, SpsChangeRequest, SpsChangeRequests};
2+
13
use self::file::{AllocationRequest, Allocations, LifeCycle, Verifier, Version};
24

35
pub mod allocation;
46
pub mod file;
57
pub mod gitcoin_interaction;
68
pub mod lifecycle;
9+
pub mod sps_change;
710

811
impl file::ApplicationFile {
912
pub async fn new(
@@ -27,6 +30,7 @@ impl file::ApplicationFile {
2730
lifecycle,
2831
allocation,
2932
client_contract_address: None,
33+
allowed_sps: None,
3034
}
3135
}
3236

@@ -41,6 +45,7 @@ impl file::ApplicationFile {
4145
allocation: file::Allocations,
4246
lifecycle: file::LifeCycle,
4347
client_contract_address: Option<String>,
48+
allowed_sps: Option<file::SpsChangeRequests>,
4449
) -> Self {
4550
//set lifecycle.edited = true
4651
let lifecycle = LifeCycle {
@@ -57,6 +62,7 @@ impl file::ApplicationFile {
5762
lifecycle,
5863
allocation,
5964
client_contract_address,
65+
allowed_sps,
6066
}
6167
}
6268

@@ -111,6 +117,47 @@ impl file::ApplicationFile {
111117
}
112118
}
113119

120+
pub fn handle_changing_sps_request(
121+
&mut self,
122+
validated_by: &String,
123+
sps_change_request: &SpsChangeRequest,
124+
app_state: &AppState,
125+
request_id: &String,
126+
) -> Self {
127+
let new_life_cycle =
128+
self.lifecycle
129+
.clone()
130+
.update_lifecycle_after_sign(app_state, validated_by, request_id);
131+
let sps_change_requests = self
132+
.allowed_sps
133+
.clone()
134+
.unwrap_or_default()
135+
.add_change_request(sps_change_request);
136+
Self {
137+
lifecycle: new_life_cycle,
138+
allowed_sps: Some(sps_change_requests),
139+
..self.clone()
140+
}
141+
}
142+
143+
pub fn update_changing_sps_request(
144+
&mut self,
145+
validated_by: &String,
146+
sps_change_requests: &SpsChangeRequests,
147+
app_state: &AppState,
148+
request_id: &String,
149+
) -> Self {
150+
let new_life_cycle =
151+
self.lifecycle
152+
.clone()
153+
.update_lifecycle_after_sign(app_state, validated_by, request_id);
154+
Self {
155+
lifecycle: new_life_cycle,
156+
allowed_sps: Some(sps_change_requests.clone()),
157+
..self.clone()
158+
}
159+
}
160+
114161
pub fn add_signer_to_allocation(
115162
&self,
116163
signer: Verifier,

0 commit comments

Comments
 (0)