Skip to content

Commit d91b66b

Browse files
authored
Revert application to ReadyToSign (#233)
* Revert application to ReadyToSign --------- Co-authored-by: Filip Lelek <[email protected]>
1 parent c04aea3 commit d91b66b

File tree

7 files changed

+186
-56
lines changed

7 files changed

+186
-56
lines changed

fplus-http-server/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ async fn main() -> std::io::Result<()> {
8484
.service(router::application::additional_info_required)
8585
.service(router::application::trigger_ssa)
8686
.service(router::application::request_kyc)
87-
.service(router::application::remove_pending_allocation),
87+
.service(router::application::remove_pending_allocation)
88+
.service(router::application::allocation_failed),
8889
)
8990
.service(router::application::merged)
9091
.service(router::application::active)

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

+17
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,20 @@ pub async fn remove_pending_allocation(
505505
Err(e) => HttpResponse::BadRequest().body(e.to_string()),
506506
}
507507
}
508+
509+
#[post("application/allocation_failed")]
510+
pub async fn allocation_failed(query: web::Query<VerifierActionsQueryParams>) -> impl Responder {
511+
let ldn_application =
512+
match LDNApplication::load(query.id.clone(), query.owner.clone(), query.repo.clone()).await
513+
{
514+
Ok(app) => app,
515+
Err(e) => return HttpResponse::BadRequest().body(e.to_string()),
516+
};
517+
match ldn_application
518+
.revert_to_ready_to_sign(&query.id, &query.owner, &query.repo)
519+
.await
520+
{
521+
Ok(()) => HttpResponse::Ok().body(serde_json::to_string_pretty("Success").unwrap()),
522+
Err(e) => HttpResponse::BadRequest().body(e.to_string()),
523+
}
524+
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ impl Allocation {
4242
signers: Verifiers::default(),
4343
}
4444
}
45+
46+
pub fn remove_signers(&self) -> Self {
47+
Self {
48+
updated_at: Utc::now().to_string(),
49+
signers: Verifiers::default(),
50+
..self.clone()
51+
}
52+
}
4553
}
4654

4755
impl Allocations {
@@ -100,6 +108,13 @@ impl Allocations {
100108
Self(res)
101109
}
102110

111+
pub fn remove_signers_in_active_allocation(&mut self) -> Self {
112+
if let Some(alloc) = self.0.iter_mut().find(|alloc| alloc.is_active) {
113+
*alloc = alloc.remove_signers();
114+
}
115+
self.clone()
116+
}
117+
103118
pub fn add_signer_and_complete(&self, request_id: String, signer: Verifier) -> Self {
104119
let mut res: Vec<Allocation> = self.0.clone();
105120
for allocation in res.iter_mut() {

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

+11
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,17 @@ impl ApplicationFile {
331331
self.allocation.0.iter().find(|alloc| alloc.is_active)
332332
}
333333

334+
pub fn get_active_allocation_request_type(&self) -> Result<String, LDNError> {
335+
self.allocation
336+
.0
337+
.iter()
338+
.find(|alloc| alloc.is_active)
339+
.map(|alloc| alloc.request_type.clone())
340+
.ok_or(LDNError::Load(
341+
"Request type not found for an active allocation.".to_string(),
342+
))
343+
}
344+
334345
pub fn adjust_active_allocation_amount(&mut self, new_amount: String) -> Result<(), LDNError> {
335346
// Find the first active allocation
336347
if let Some(allocation) = self.allocation.0.iter_mut().find(|alloc| alloc.is_active) {

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

+8
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,12 @@ impl LifeCycle {
125125
..self.clone()
126126
}
127127
}
128+
129+
pub fn move_back_to_ready_to_sign(self) -> Self {
130+
LifeCycle {
131+
state: AppState::ReadyToSign,
132+
updated_at: Utc::now().to_string(),
133+
..self.clone()
134+
}
135+
}
128136
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ impl file::ApplicationFile {
115115
}
116116
}
117117

118+
pub fn move_back_to_ready_to_sign(&self) -> Self {
119+
let updated_allocation = self
120+
.clone()
121+
.allocation
122+
.remove_signers_in_active_allocation();
123+
let updated_lifecycle = self.clone().lifecycle.move_back_to_ready_to_sign();
124+
Self {
125+
lifecycle: updated_lifecycle,
126+
allocation: updated_allocation,
127+
..self.clone()
128+
}
129+
}
130+
118131
pub fn add_signer_to_allocation_and_complete(
119132
&self,
120133
signer: Verifier,

fplus-lib/src/core/mod.rs

+120-55
Original file line numberDiff line numberDiff line change
@@ -4044,85 +4044,71 @@ _The initial issue can be edited in order to solve the request of the verifier.
40444044
let app_model =
40454045
Self::get_application_model(client_id.into(), owner.into(), repo.into()).await?;
40464046

4047-
if app_model.pr_number == 0 {
4048-
return Err(LDNError::Load("Active pull request not found".to_string()));
4049-
}
4050-
4051-
let app_str = &app_model.application.ok_or_else(|| {
4052-
LDNError::Load(format!(
4053-
"Application {} does not have an application field",
4054-
client_id
4055-
))
4056-
})?;
4057-
let application_file = serde_json::from_str::<ApplicationFile>(app_str).unwrap();
4047+
let application_file =
4048+
Self::get_application_file_with_active_allocation(&app_model).await?;
40584049

40594050
if application_file.lifecycle.state != AppState::ReadyToSign {
40604051
return Err(LDNError::Load(format!(
40614052
"Application state is {:?}. Expected ReadyToSign",
40624053
application_file.lifecycle.state
40634054
)));
40644055
}
4065-
let last_allocation = application_file.get_active_allocation().ok_or_else(|| {
4066-
LDNError::Load(format!(
4067-
"Application {} does not have an active allocation",
4068-
client_id
4069-
))
4070-
})?;
4071-
4072-
let is_first = last_allocation.request_type == "First";
4056+
let is_first = application_file.get_active_allocation_request_type()? == "First";
40734057
if is_first {
40744058
self.remove_first_pending_allocation(&application_file)
40754059
.await?;
40764060
} else {
40774061
self.remove_pending_refill(&app_model.pr_number).await?;
40784062
}
4079-
self.issue_updates_after_removing_pending_allocation(
4080-
&application_file.issue_number,
4081-
is_first,
4082-
)
4083-
.await?;
4084-
Ok(())
4085-
}
40864063

4087-
async fn issue_updates_after_removing_pending_allocation(
4088-
&self,
4089-
issue_number: &str,
4090-
is_first: bool,
4091-
) -> Result<(), LDNError> {
40924064
let comment = format!(
40934065
"Last pending allocation reverted for an application `{}`.",
40944066
&self.application_id
40954067
);
4096-
let issue_number = issue_number.parse::<u64>().map_err(|e| {
4097-
LDNError::New(format!(
4098-
"Parse issue number: {} to u64 failed. {}",
4099-
issue_number, e
4100-
))
4101-
})?;
4102-
self.github
4103-
.add_comment_to_issue(issue_number, &comment)
4104-
.await
4105-
.map_err(|e| {
4106-
LDNError::New(format!(
4107-
"Error adding comment to issue {} /// {}",
4108-
issue_number, e
4109-
))
4110-
})?;
41114068

41124069
let app_state = if is_first {
41134070
AppState::Submitted.as_str()
41144071
} else {
41154072
AppState::Granted.as_str()
41164073
};
4117-
self.github
4118-
.replace_issue_labels(issue_number, &[app_state.into()])
4119-
.await
4120-
.map_err(|e| {
4121-
LDNError::New(format!(
4122-
"Error adding labels to issue {} /// {}",
4123-
issue_number, e
4124-
))
4125-
})?;
4074+
4075+
self.issue_updates(&application_file.issue_number, &comment, app_state)
4076+
.await?;
4077+
Ok(())
4078+
}
4079+
4080+
pub async fn revert_to_ready_to_sign(
4081+
&self,
4082+
client_id: &str,
4083+
owner: &str,
4084+
repo: &str,
4085+
) -> Result<(), LDNError> {
4086+
let app_model =
4087+
Self::get_application_model(client_id.into(), owner.into(), repo.into()).await?;
4088+
4089+
let application_file =
4090+
Self::get_application_file_with_active_allocation(&app_model).await?;
4091+
4092+
if application_file.lifecycle.state != AppState::StartSignDatacap {
4093+
return Err(LDNError::Load(format!(
4094+
"Application state is {:?}. Expected StartSignDatacap",
4095+
application_file.lifecycle.state
4096+
)));
4097+
}
4098+
4099+
self.remove_signers_from_active_request(&application_file)
4100+
.await?;
4101+
4102+
let comment = format!(
4103+
"Allocation transaction failed on chain, application {:?} reverted to ReadyToSign state. Please try again.",
4104+
&self.application_id
4105+
);
4106+
self.issue_updates(
4107+
&application_file.issue_number,
4108+
&comment,
4109+
AppState::ReadyToSign.as_str(),
4110+
)
4111+
.await?;
41264112
Ok(())
41274113
}
41284114

@@ -4166,6 +4152,85 @@ _The initial issue can be edited in order to solve the request of the verifier.
41664152
})?;
41674153
Ok(())
41684154
}
4155+
4156+
async fn remove_signers_from_active_request(
4157+
&self,
4158+
application_file: &ApplicationFile,
4159+
) -> Result<(), LDNError> {
4160+
let updated_application = application_file.clone().move_back_to_ready_to_sign();
4161+
self.update_and_commit_application_state(
4162+
updated_application.clone(),
4163+
self.github.owner.clone(),
4164+
self.github.repo.clone(),
4165+
self.file_sha.clone(),
4166+
self.branch_name.clone(),
4167+
self.file_name.clone(),
4168+
"Revert pending allocation to ReadyToSign".to_string(),
4169+
)
4170+
.await?;
4171+
Ok(())
4172+
}
4173+
4174+
async fn issue_updates(
4175+
&self,
4176+
issue_number: &str,
4177+
comment: &str,
4178+
label: &str,
4179+
) -> Result<(), LDNError> {
4180+
let issue_number = issue_number.parse::<u64>().map_err(|e| {
4181+
LDNError::New(format!(
4182+
"Parse issue number: {} to u64 failed. {}",
4183+
issue_number, e
4184+
))
4185+
})?;
4186+
self.github
4187+
.add_comment_to_issue(issue_number, comment)
4188+
.await
4189+
.map_err(|e| {
4190+
LDNError::New(format!(
4191+
"Error adding comment to issue {} /// {}",
4192+
issue_number, e
4193+
))
4194+
})?;
4195+
self.github
4196+
.replace_issue_labels(issue_number, &[label.into()])
4197+
.await
4198+
.map_err(|e| {
4199+
LDNError::New(format!(
4200+
"Error adding labels to issue {} /// {}",
4201+
issue_number, e
4202+
))
4203+
})?;
4204+
Ok(())
4205+
}
4206+
4207+
async fn get_application_file_with_active_allocation(
4208+
app_model: &ApplicationModel,
4209+
) -> Result<ApplicationFile, LDNError> {
4210+
if app_model.pr_number == 0 {
4211+
return Err(LDNError::Load("Active pull request not found".to_string()));
4212+
}
4213+
4214+
let app_str = app_model.application.as_ref().ok_or_else(|| {
4215+
LDNError::New(format!(
4216+
"Application {} does not have an application field",
4217+
app_model.id
4218+
))
4219+
})?;
4220+
4221+
let application_file = serde_json::from_str::<ApplicationFile>(app_str).map_err(|e| {
4222+
LDNError::New(format!("Failed to parse string to ApplicationFile: {}", e))
4223+
})?;
4224+
4225+
application_file.get_active_allocation().ok_or_else(|| {
4226+
LDNError::Load(format!(
4227+
"Application {} does not have an active allocation",
4228+
app_model.id
4229+
))
4230+
})?;
4231+
4232+
Ok(application_file)
4233+
}
41694234
}
41704235

41714236
#[derive(Serialize, Deserialize, Debug)]

0 commit comments

Comments
 (0)