Skip to content

Commit b7b60b6

Browse files
authored
Merge pull request #155 from filecoin-project/update-threshold-from-blockchain
validation blockchain check
2 parents ccd05e4 + 2a43953 commit b7b60b6

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::{
@@ -162,8 +168,11 @@ pub struct ApplicationGithubInfo {
162168
}
163169

164170
impl LDNApplication {
165-
166-
pub async fn single_active(pr_number: u64, owner: String, repo: String) -> Result<ApplicationFile, LDNError> {
171+
pub async fn single_active(
172+
pr_number: u64,
173+
owner: String,
174+
repo: String,
175+
) -> Result<ApplicationFile, LDNError> {
167176
let gh = github_async_new(owner, repo).await;
168177
let (_, pull_request) = gh.get_pull_request_files(pr_number).await.unwrap();
169178
let pull_request = pull_request.get(0).unwrap();
@@ -268,8 +277,11 @@ impl LDNApplication {
268277
Ok(app)
269278
}
270279

271-
pub async fn load(application_id: String, owner: String, repo: String) -> Result<Self, LDNError> {
272-
280+
pub async fn load(
281+
application_id: String,
282+
owner: String,
283+
repo: String,
284+
) -> Result<Self, LDNError> {
273285
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
274286
let pull_requests = gh.list_pull_requests().await.unwrap();
275287
let pull_requests = future::try_join_all(
@@ -377,7 +389,11 @@ impl LDNApplication {
377389
Ok(apps)
378390
}
379391

380-
pub async fn active_apps_with_last_update(owner: String, repo: String, filter: Option<String>) -> Result<Vec<ApplicationFileWithDate>, LDNError> {
392+
pub async fn active_apps_with_last_update(
393+
owner: String,
394+
repo: String,
395+
filter: Option<String>,
396+
) -> Result<Vec<ApplicationFileWithDate>, LDNError> {
381397
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
382398
let mut apps: Vec<ApplicationFileWithDate> = Vec::new();
383399
let pull_requests = gh.list_pull_requests().await.unwrap();
@@ -411,9 +427,13 @@ impl LDNApplication {
411427
Ok(apps)
412428
}
413429

414-
pub async fn merged_apps_with_last_update(owner: String, repo: String, filter: Option<String>) -> Result<Vec<ApplicationFileWithDate>, LDNError> {
430+
pub async fn merged_apps_with_last_update(
431+
owner: String,
432+
repo: String,
433+
filter: Option<String>,
434+
) -> Result<Vec<ApplicationFileWithDate>, LDNError> {
415435
let gh = Arc::new(github_async_new(owner.to_string(), repo.to_string()).await);
416-
436+
417437
let applications_path = "applications";
418438
let mut all_files_result = gh.get_files(applications_path).await.map_err(|e| {
419439
LDNError::Load(format!(
@@ -680,7 +700,7 @@ impl LDNApplication {
680700
}
681701
};
682702
let db_threshold: u64 = db_allocator.unwrap().multisig_threshold.unwrap_or(2) as u64;
683-
703+
684704
// If blockchain threshold is available and different from DB, update DB (placeholder for update logic)
685705
if let Some(blockchain_threshold) = blockchain_threshold {
686706
if blockchain_threshold != db_threshold {
@@ -965,7 +985,8 @@ impl LDNApplication {
965985
if app.is_some() && app.unwrap().1.lifecycle.get_state() == AppState::Granted {
966986
let app = app.unwrap().1.reached_total_datacap();
967987
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
968-
let ldn_app = LDNApplication::load(application_id.clone(), owner.clone(), repo.clone()).await?;
988+
let ldn_app =
989+
LDNApplication::load(application_id.clone(), owner.clone(), repo.clone()).await?;
969990
let ContentItems { items } = gh.get_file(&ldn_app.file_name, "main").await.unwrap();
970991
Self::issue_full_dc(app.issue_number.clone(), owner.clone(), repo.clone()).await?;
971992
Self::update_issue_labels(
@@ -1235,7 +1256,11 @@ impl LDNApplication {
12351256
return Ok(false);
12361257
}
12371258

1238-
pub async fn merge_application(pr_number: u64, owner: String, repo: String) -> Result<bool, LDNError> {
1259+
pub async fn merge_application(
1260+
pr_number: u64,
1261+
owner: String,
1262+
repo: String,
1263+
) -> Result<bool, LDNError> {
12391264
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
12401265

12411266
gh.merge_pull_request(pr_number).await.map_err(|e| {
@@ -1428,13 +1453,19 @@ impl LDNApplication {
14281453
log::warn!("Val Trigger (RtS) - Active allocation has signers");
14291454
false
14301455
} else if validated_at.is_empty() {
1431-
log::warn!("Val Trigger (RtS) - Not ready to sign - validated_at is empty");
1456+
log::warn!(
1457+
"Val Trigger (RtS) - Not ready to sign - validated_at is empty"
1458+
);
14321459
false
14331460
} else if validated_by.is_empty() {
1434-
log::warn!("Val Trigger (RtS) - Not ready to sign - validated_by is empty");
1461+
log::warn!(
1462+
"Val Trigger (RtS) - Not ready to sign - validated_by is empty"
1463+
);
14351464
false
14361465
} else if actor != bot_user {
1437-
log::warn!("Val Trigger (RtS) - Not ready to sign - actor is not the bot user");
1466+
log::warn!(
1467+
"Val Trigger (RtS) - Not ready to sign - actor is not the bot user"
1468+
);
14381469
false
14391470
} else if !valid_verifier_list.is_valid(&validated_by) {
14401471
log::warn!("Val Trigger (RtS) - Not ready to sign - valid_verifier_list is not valid");
@@ -1543,7 +1574,10 @@ impl LDNApplication {
15431574
{
15441575
Some(()) => {
15451576
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
1546-
match gh.get_pull_request_by_head(&ldn_application.branch_name).await {
1577+
match gh
1578+
.get_pull_request_by_head(&ldn_application.branch_name)
1579+
.await
1580+
{
15471581
Ok(prs) => {
15481582
if let Some(pr) = prs.get(0) {
15491583
let number = pr.number;
@@ -1612,10 +1646,30 @@ impl LDNApplication {
16121646
}
16131647
};
16141648
let signers: application::file::Verifiers = active_request.signers.clone();
1615-
if signers.0.len() != 2 {
1616-
log::warn!("Val Approval (G) - Not enough signers");
1649+
1650+
// Try getting the multisig threshold from the blockchain
1651+
let blockchain_threshold =
1652+
get_multisig_threshold_for_actor("actor_address").await;
1653+
1654+
// Fallback to database value if blockchain query fails
1655+
let multisig_threshold = match blockchain_threshold {
1656+
Ok(threshold) => threshold as usize,
1657+
Err(_) => {
1658+
let db_allocator = get_allocator(&owner, &repo).await
1659+
.map_err(|e| LDNError::Load(format!("Database error: {}", e)))?;
1660+
db_allocator
1661+
.as_ref()
1662+
.and_then(|allocator| allocator.multisig_threshold)
1663+
.unwrap_or(2) as usize
1664+
}
1665+
};
1666+
1667+
// Check if the number of signers meets or exceeds the multisig threshold
1668+
if signers.0.len() < multisig_threshold {
1669+
log::warn!("Not enough signers for approval");
16171670
return Ok(false);
16181671
}
1672+
16191673
let signer = signers.0.get(1).unwrap();
16201674
let signer_gh_handle = signer.github_username.clone();
16211675
let valid_verifiers =
@@ -1732,7 +1786,11 @@ impl LDNApplication {
17321786
}
17331787
}
17341788

1735-
async fn issue_waiting_for_gov_review(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
1789+
async fn issue_waiting_for_gov_review(
1790+
issue_number: String,
1791+
owner: String,
1792+
repo: String,
1793+
) -> Result<bool, LDNError> {
17361794
let gh = github_async_new(owner, repo).await;
17371795
gh.add_comment_to_issue(
17381796
issue_number.parse().unwrap(),
@@ -1749,7 +1807,11 @@ impl LDNApplication {
17491807
Ok(true)
17501808
}
17511809

1752-
async fn issue_datacap_request_trigger(application_file: ApplicationFile, owner: String, repo: String) -> Result<bool, LDNError> {
1810+
async fn issue_datacap_request_trigger(
1811+
application_file: ApplicationFile,
1812+
owner: String,
1813+
repo: String,
1814+
) -> Result<bool, LDNError> {
17531815
let gh = github_async_new(owner, repo).await;
17541816

17551817
let client_address = application_file.lifecycle.client_on_chain_address.clone();
@@ -1929,7 +1991,11 @@ Your Datacap Allocation Request has been {} by the Notary
19291991
Ok(true)
19301992
}
19311993

1932-
async fn issue_start_sign_dc(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
1994+
async fn issue_start_sign_dc(
1995+
issue_number: String,
1996+
owner: String,
1997+
repo: String,
1998+
) -> Result<bool, LDNError> {
19331999
let gh = github_async_new(owner, repo).await;
19342000
gh.add_comment_to_issue(
19352001
issue_number.parse().unwrap(),
@@ -1945,7 +2011,11 @@ Your Datacap Allocation Request has been {} by the Notary
19452011
.unwrap();
19462012
Ok(true)
19472013
}
1948-
async fn issue_granted(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
2014+
async fn issue_granted(
2015+
issue_number: String,
2016+
owner: String,
2017+
repo: String,
2018+
) -> Result<bool, LDNError> {
19492019
let gh = github_async_new(owner, repo).await;
19502020
gh.add_comment_to_issue(issue_number.parse().unwrap(), "Application is Granted")
19512021
.await
@@ -1958,7 +2028,11 @@ Your Datacap Allocation Request has been {} by the Notary
19582028
.unwrap();
19592029
Ok(true)
19602030
}
1961-
async fn issue_refill(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
2031+
async fn issue_refill(
2032+
issue_number: String,
2033+
owner: String,
2034+
repo: String,
2035+
) -> Result<bool, LDNError> {
19622036
let gh = github_async_new(owner, repo).await;
19632037
gh.add_comment_to_issue(issue_number.parse().unwrap(), "Application is in Refill")
19642038
.await
@@ -1980,7 +2054,11 @@ Your Datacap Allocation Request has been {} by the Notary
19802054
.unwrap();
19812055
Ok(true)
19822056
}
1983-
async fn issue_full_dc(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
2057+
async fn issue_full_dc(
2058+
issue_number: String,
2059+
owner: String,
2060+
repo: String,
2061+
) -> Result<bool, LDNError> {
19842062
let gh = github_async_new(owner, repo).await;
19852063
gh.add_comment_to_issue(issue_number.parse().unwrap(), "Application is Completed")
19862064
.await
@@ -1994,7 +2072,12 @@ Your Datacap Allocation Request has been {} by the Notary
19942072
Ok(true)
19952073
}
19962074

1997-
async fn add_error_label(issue_number: String, comment: String, owner: String, repo: String) -> Result<(), LDNError> {
2075+
async fn add_error_label(
2076+
issue_number: String,
2077+
comment: String,
2078+
owner: String,
2079+
repo: String,
2080+
) -> Result<(), LDNError> {
19982081
let gh = github_async_new(owner, repo).await;
19992082
let num: u64 = issue_number.parse().expect("Not a valid integer");
20002083
gh.add_error_label(num, comment)
@@ -2010,8 +2093,13 @@ Your Datacap Allocation Request has been {} by the Notary
20102093
Ok(())
20112094
}
20122095

2013-
async fn update_issue_labels(issue_number: String, new_labels: &[&str], owner: String, repo: String) -> Result<(), LDNError> {
2014-
let gh = github_async_new(owner, repo).await;
2096+
async fn update_issue_labels(
2097+
issue_number: String,
2098+
new_labels: &[&str],
2099+
owner: String,
2100+
repo: String,
2101+
) -> Result<(), LDNError> {
2102+
let gh = github_async_new(owner, repo).await;
20152103
let num: u64 = issue_number.parse().expect("Not a valid integer");
20162104
gh.update_issue_labels(num, new_labels)
20172105
.await
@@ -2447,8 +2535,8 @@ pub fn get_file_sha(content: &ContentItems) -> Option<String> {
24472535
// }
24482536

24492537
// let ldn_application_after_trigger = match LDNApplication::load(
2450-
// application_id.clone(),
2451-
// OWNER.to_string(),
2538+
// application_id.clone(),
2539+
// OWNER.to_string(),
24522540
// REPO.to_string()
24532541
// ).await
24542542
// {

0 commit comments

Comments
 (0)