Skip to content

fix ensures approval validation returns False when total DataCap is reached and validates DataCap Lifecycle properties in JSON #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions fplus-lib/src/core/application/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,31 @@ impl LifeCycle {

/// Change Application state to Proposal from Governance Review
/// Actor input is the actor who is changing the state
pub fn finish_governance_review(&self, actor: String, current_allocation_id: String) -> Self {
LifeCycle {
pub fn finish_governance_review(&self, actor: String, current_allocation_id: String) -> Result<Self, String> {
let new_lifecycle = LifeCycle {
state: AppState::ReadyToSign,
validated_by: actor,
validated_at: Utc::now().to_string(),
updated_at: Utc::now().to_string(),
active_request: Some(current_allocation_id),
..self.clone()
}
};

new_lifecycle.validate()?;
Ok(new_lifecycle)
}

pub fn sign_grant_datacap_proposal(&self, validated_by: &str) -> Self {
LifeCycle {
pub fn sign_grant_datacap_proposal(&self, validated_by: &str) -> Result<Self, String> {
let new_lifecycle = LifeCycle {
state: AppState::StartSignDatacap,
updated_at: Utc::now().to_string(),
validated_by: validated_by.into(),
validated_at: Utc::now().to_string(),
..self.clone()
}
};

new_lifecycle.validate()?;
Ok(new_lifecycle)
}

pub fn update_lifecycle_after_sign(
Expand Down Expand Up @@ -154,4 +160,38 @@ impl LifeCycle {
..self.clone()
}
}

pub fn validate(&self) -> Result<(), String> {
if self.client_on_chain_address.is_empty() {
return Err("Client on-chain address is required".to_string());
}
if self.multisig_address.is_empty() {
return Err("Multisig address is required".to_string());
}

match self.state {
AppState::Granted | AppState::StartSignDatacap => {
if self.validated_by.is_empty() {
return Err("Validated by is required for Granted/StartSignDatacap state".to_string());
}
if self.validated_at.is_empty() {
return Err("Validated at is required for Granted/StartSignDatacap state".to_string());
}
}
AppState::ReadyToSign => {
if self.validated_by.is_empty() {
return Err("Validated by is required for ReadyToSign state".to_string());
}
if self.validated_at.is_empty() {
return Err("Validated at is required for ReadyToSign state".to_string());
}
if self.active_request.is_none() || self.active_request.as_ref().unwrap().is_empty() {
return Err("Active request ID is required for ReadyToSign state".to_string());
}
}
_ => {}
}

Ok(())
}
}
32 changes: 25 additions & 7 deletions fplus-lib/src/core/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,25 @@ impl file::ApplicationFile {
sps_change_request: &SpsChangeRequest,
app_state: &AppState,
request_id: &String,
) -> Self {
let new_life_cycle =
self.lifecycle
.clone()
.update_lifecycle_after_sign(app_state, validated_by, request_id);
) -> Result<Self, String> {
let new_life_cycle = self.lifecycle
.clone()
.update_lifecycle_after_sign(app_state, validated_by, request_id)?;

let sps_change_requests = self
.allowed_sps
.clone()
.unwrap_or_default()
.add_change_request(sps_change_request);
Self {

let new_app = Self {
lifecycle: new_life_cycle,
allowed_sps: Some(sps_change_requests),
..self.clone()
}
};

new_app.validate()?;
Ok(new_app)
}

pub fn update_changing_sps_request(
Expand Down Expand Up @@ -212,6 +216,20 @@ impl file::ApplicationFile {
..self.clone()
}
}
pub fn validate(&self) -> Result<(), String> {
self.lifecycle.validate()?;

match self.lifecycle.state {
AppState::ReadyToSign | AppState::StartSignDatacap | AppState::Granted => {
if self.allocation.0.is_empty() {
return Err("Allocations are required for this state".to_string());
}
}
_ => {}
}

Ok(())
}
}

impl std::str::FromStr for file::ApplicationFile {
Expand Down
11 changes: 11 additions & 0 deletions fplus-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4463,6 +4463,17 @@ _The initial issue can be edited in order to solve the request of the verifier.

Ok(application_file)
}

pub fn reached_total_datacap(self) -> Self {
let empty = "".to_string();

LifeCycle {
is_active: false,
updated_at: Utc::now().to_string(),
active_request: Some(empty),
..self
}
}
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down