Skip to content

Commit 2a43953

Browse files
committed
validation blockchain check
1 parent ab7f25e commit 2a43953

File tree

1 file changed

+119
-31
lines changed

1 file changed

+119
-31
lines changed

fplus-lib/src/core/mod.rs

Lines changed: 119 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ use crate::{
1616
base64,
1717
config::get_env_var_or_default,
1818
error::LDNError,
19-
external_services::{filecoin::get_multisig_threshold_for_actor, github::{
20-
github_async_new, CreateMergeRequestData, CreateRefillMergeRequestData, GithubWrapper,
21-
}},
19+
external_services::{
20+
filecoin::get_multisig_threshold_for_actor,
21+
github::{
22+
github_async_new, CreateMergeRequestData, CreateRefillMergeRequestData, GithubWrapper,
23+
},
24+
},
2225
parsers::ParsedIssue,
2326
};
24-
use fplus_database::database::{self, allocators::{get_allocator, update_allocator_threshold}};
27+
use fplus_database::database::{
28+
self,
29+
allocators::{get_allocator, update_allocator_threshold},
30+
};
2531
use fplus_database::models::applications::Model as ApplicationModel;
2632

2733
use self::application::file::{
@@ -155,8 +161,11 @@ pub struct ApplicationGithubInfo {
155161
}
156162

157163
impl LDNApplication {
158-
159-
pub async fn single_active(pr_number: u64, owner: String, repo: String) -> Result<ApplicationFile, LDNError> {
164+
pub async fn single_active(
165+
pr_number: u64,
166+
owner: String,
167+
repo: String,
168+
) -> Result<ApplicationFile, LDNError> {
160169
let gh = github_async_new(owner, repo).await;
161170
let (_, pull_request) = gh.get_pull_request_files(pr_number).await.unwrap();
162171
let pull_request = pull_request.get(0).unwrap();
@@ -261,8 +270,11 @@ impl LDNApplication {
261270
Ok(app)
262271
}
263272

264-
pub async fn load(application_id: String, owner: String, repo: String) -> Result<Self, LDNError> {
265-
273+
pub async fn load(
274+
application_id: String,
275+
owner: String,
276+
repo: String,
277+
) -> Result<Self, LDNError> {
266278
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
267279
let pull_requests = gh.list_pull_requests().await.unwrap();
268280
let pull_requests = future::try_join_all(
@@ -370,7 +382,11 @@ impl LDNApplication {
370382
Ok(apps)
371383
}
372384

373-
pub async fn active_apps_with_last_update(owner: String, repo: String, filter: Option<String>) -> Result<Vec<ApplicationFileWithDate>, LDNError> {
385+
pub async fn active_apps_with_last_update(
386+
owner: String,
387+
repo: String,
388+
filter: Option<String>,
389+
) -> Result<Vec<ApplicationFileWithDate>, LDNError> {
374390
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
375391
let mut apps: Vec<ApplicationFileWithDate> = Vec::new();
376392
let pull_requests = gh.list_pull_requests().await.unwrap();
@@ -404,9 +420,13 @@ impl LDNApplication {
404420
Ok(apps)
405421
}
406422

407-
pub async fn merged_apps_with_last_update(owner: String, repo: String, filter: Option<String>) -> Result<Vec<ApplicationFileWithDate>, LDNError> {
423+
pub async fn merged_apps_with_last_update(
424+
owner: String,
425+
repo: String,
426+
filter: Option<String>,
427+
) -> Result<Vec<ApplicationFileWithDate>, LDNError> {
408428
let gh = Arc::new(github_async_new(owner.to_string(), repo.to_string()).await);
409-
429+
410430
let applications_path = "applications";
411431
let mut all_files_result = gh.get_files(applications_path).await.map_err(|e| {
412432
LDNError::Load(format!(
@@ -673,7 +693,7 @@ impl LDNApplication {
673693
}
674694
};
675695
let db_threshold: u64 = db_allocator.unwrap().multisig_threshold.unwrap_or(2) as u64;
676-
696+
677697
// If blockchain threshold is available and different from DB, update DB (placeholder for update logic)
678698
if let Some(blockchain_threshold) = blockchain_threshold {
679699
if blockchain_threshold != db_threshold {
@@ -958,7 +978,8 @@ impl LDNApplication {
958978
if app.is_some() && app.unwrap().1.lifecycle.get_state() == AppState::Granted {
959979
let app = app.unwrap().1.reached_total_datacap();
960980
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
961-
let ldn_app = LDNApplication::load(application_id.clone(), owner.clone(), repo.clone()).await?;
981+
let ldn_app =
982+
LDNApplication::load(application_id.clone(), owner.clone(), repo.clone()).await?;
962983
let ContentItems { items } = gh.get_file(&ldn_app.file_name, "main").await.unwrap();
963984
Self::issue_full_dc(app.issue_number.clone(), owner.clone(), repo.clone()).await?;
964985
Self::update_issue_labels(
@@ -1228,7 +1249,11 @@ impl LDNApplication {
12281249
return Ok(false);
12291250
}
12301251

1231-
pub async fn merge_application(pr_number: u64, owner: String, repo: String) -> Result<bool, LDNError> {
1252+
pub async fn merge_application(
1253+
pr_number: u64,
1254+
owner: String,
1255+
repo: String,
1256+
) -> Result<bool, LDNError> {
12321257
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
12331258

12341259
gh.merge_pull_request(pr_number).await.map_err(|e| {
@@ -1421,13 +1446,19 @@ impl LDNApplication {
14211446
log::warn!("Val Trigger (RtS) - Active allocation has signers");
14221447
false
14231448
} else if validated_at.is_empty() {
1424-
log::warn!("Val Trigger (RtS) - Not ready to sign - validated_at is empty");
1449+
log::warn!(
1450+
"Val Trigger (RtS) - Not ready to sign - validated_at is empty"
1451+
);
14251452
false
14261453
} else if validated_by.is_empty() {
1427-
log::warn!("Val Trigger (RtS) - Not ready to sign - validated_by is empty");
1454+
log::warn!(
1455+
"Val Trigger (RtS) - Not ready to sign - validated_by is empty"
1456+
);
14281457
false
14291458
} else if actor != bot_user {
1430-
log::warn!("Val Trigger (RtS) - Not ready to sign - actor is not the bot user");
1459+
log::warn!(
1460+
"Val Trigger (RtS) - Not ready to sign - actor is not the bot user"
1461+
);
14311462
false
14321463
} else if !valid_verifier_list.is_valid(&validated_by) {
14331464
log::warn!("Val Trigger (RtS) - Not ready to sign - valid_verifier_list is not valid");
@@ -1536,7 +1567,10 @@ impl LDNApplication {
15361567
{
15371568
Some(()) => {
15381569
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
1539-
match gh.get_pull_request_by_head(&ldn_application.branch_name).await {
1570+
match gh
1571+
.get_pull_request_by_head(&ldn_application.branch_name)
1572+
.await
1573+
{
15401574
Ok(prs) => {
15411575
if let Some(pr) = prs.get(0) {
15421576
let number = pr.number;
@@ -1605,10 +1639,30 @@ impl LDNApplication {
16051639
}
16061640
};
16071641
let signers: application::file::Verifiers = active_request.signers.clone();
1608-
if signers.0.len() != 2 {
1609-
log::warn!("Val Approval (G) - Not enough signers");
1642+
1643+
// Try getting the multisig threshold from the blockchain
1644+
let blockchain_threshold =
1645+
get_multisig_threshold_for_actor("actor_address").await;
1646+
1647+
// Fallback to database value if blockchain query fails
1648+
let multisig_threshold = match blockchain_threshold {
1649+
Ok(threshold) => threshold as usize,
1650+
Err(_) => {
1651+
let db_allocator = get_allocator(&owner, &repo).await
1652+
.map_err(|e| LDNError::Load(format!("Database error: {}", e)))?;
1653+
db_allocator
1654+
.as_ref()
1655+
.and_then(|allocator| allocator.multisig_threshold)
1656+
.unwrap_or(2) as usize
1657+
}
1658+
};
1659+
1660+
// Check if the number of signers meets or exceeds the multisig threshold
1661+
if signers.0.len() < multisig_threshold {
1662+
log::warn!("Not enough signers for approval");
16101663
return Ok(false);
16111664
}
1665+
16121666
let signer = signers.0.get(1).unwrap();
16131667
let signer_gh_handle = signer.github_username.clone();
16141668
let valid_verifiers =
@@ -1725,7 +1779,11 @@ impl LDNApplication {
17251779
}
17261780
}
17271781

1728-
async fn issue_waiting_for_gov_review(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
1782+
async fn issue_waiting_for_gov_review(
1783+
issue_number: String,
1784+
owner: String,
1785+
repo: String,
1786+
) -> Result<bool, LDNError> {
17291787
let gh = github_async_new(owner, repo).await;
17301788
gh.add_comment_to_issue(
17311789
issue_number.parse().unwrap(),
@@ -1742,7 +1800,11 @@ impl LDNApplication {
17421800
Ok(true)
17431801
}
17441802

1745-
async fn issue_datacap_request_trigger(application_file: ApplicationFile, owner: String, repo: String) -> Result<bool, LDNError> {
1803+
async fn issue_datacap_request_trigger(
1804+
application_file: ApplicationFile,
1805+
owner: String,
1806+
repo: String,
1807+
) -> Result<bool, LDNError> {
17461808
let gh = github_async_new(owner, repo).await;
17471809

17481810
let client_address = application_file.lifecycle.client_on_chain_address.clone();
@@ -1922,7 +1984,11 @@ Your Datacap Allocation Request has been {} by the Notary
19221984
Ok(true)
19231985
}
19241986

1925-
async fn issue_start_sign_dc(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
1987+
async fn issue_start_sign_dc(
1988+
issue_number: String,
1989+
owner: String,
1990+
repo: String,
1991+
) -> Result<bool, LDNError> {
19261992
let gh = github_async_new(owner, repo).await;
19271993
gh.add_comment_to_issue(
19281994
issue_number.parse().unwrap(),
@@ -1938,7 +2004,11 @@ Your Datacap Allocation Request has been {} by the Notary
19382004
.unwrap();
19392005
Ok(true)
19402006
}
1941-
async fn issue_granted(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
2007+
async fn issue_granted(
2008+
issue_number: String,
2009+
owner: String,
2010+
repo: String,
2011+
) -> Result<bool, LDNError> {
19422012
let gh = github_async_new(owner, repo).await;
19432013
gh.add_comment_to_issue(issue_number.parse().unwrap(), "Application is Granted")
19442014
.await
@@ -1951,7 +2021,11 @@ Your Datacap Allocation Request has been {} by the Notary
19512021
.unwrap();
19522022
Ok(true)
19532023
}
1954-
async fn issue_refill(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
2024+
async fn issue_refill(
2025+
issue_number: String,
2026+
owner: String,
2027+
repo: String,
2028+
) -> Result<bool, LDNError> {
19552029
let gh = github_async_new(owner, repo).await;
19562030
gh.add_comment_to_issue(issue_number.parse().unwrap(), "Application is in Refill")
19572031
.await
@@ -1973,7 +2047,11 @@ Your Datacap Allocation Request has been {} by the Notary
19732047
.unwrap();
19742048
Ok(true)
19752049
}
1976-
async fn issue_full_dc(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
2050+
async fn issue_full_dc(
2051+
issue_number: String,
2052+
owner: String,
2053+
repo: String,
2054+
) -> Result<bool, LDNError> {
19772055
let gh = github_async_new(owner, repo).await;
19782056
gh.add_comment_to_issue(issue_number.parse().unwrap(), "Application is Completed")
19792057
.await
@@ -1987,7 +2065,12 @@ Your Datacap Allocation Request has been {} by the Notary
19872065
Ok(true)
19882066
}
19892067

1990-
async fn add_error_label(issue_number: String, comment: String, owner: String, repo: String) -> Result<(), LDNError> {
2068+
async fn add_error_label(
2069+
issue_number: String,
2070+
comment: String,
2071+
owner: String,
2072+
repo: String,
2073+
) -> Result<(), LDNError> {
19912074
let gh = github_async_new(owner, repo).await;
19922075
let num: u64 = issue_number.parse().expect("Not a valid integer");
19932076
gh.add_error_label(num, comment)
@@ -2003,8 +2086,13 @@ Your Datacap Allocation Request has been {} by the Notary
20032086
Ok(())
20042087
}
20052088

2006-
async fn update_issue_labels(issue_number: String, new_labels: &[&str], owner: String, repo: String) -> Result<(), LDNError> {
2007-
let gh = github_async_new(owner, repo).await;
2089+
async fn update_issue_labels(
2090+
issue_number: String,
2091+
new_labels: &[&str],
2092+
owner: String,
2093+
repo: String,
2094+
) -> Result<(), LDNError> {
2095+
let gh = github_async_new(owner, repo).await;
20082096
let num: u64 = issue_number.parse().expect("Not a valid integer");
20092097
gh.update_issue_labels(num, new_labels)
20102098
.await
@@ -2440,8 +2528,8 @@ pub fn get_file_sha(content: &ContentItems) -> Option<String> {
24402528
// }
24412529

24422530
// let ldn_application_after_trigger = match LDNApplication::load(
2443-
// application_id.clone(),
2444-
// OWNER.to_string(),
2531+
// application_id.clone(),
2532+
// OWNER.to_string(),
24452533
// REPO.to_string()
24462534
// ).await
24472535
// {

0 commit comments

Comments
 (0)