Skip to content

Commit af00970

Browse files
committed
determining signers count and then threshold based on signers.
1 parent 48ca91c commit af00970

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

fplus-lib/src/core/mod.rs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,11 @@ pub struct AllocatorUpdateInfo {
131131
pub multisig_threshold: Option<i32>,
132132
}
133133

134-
135134
#[derive(Deserialize)]
136135
pub struct InstallationIdUpdateInfo {
137136
pub installation_id: i64,
138137
}
139138

140-
141139
#[derive(Deserialize)]
142140
pub struct GithubQueryParams {
143141
pub owner: String,
@@ -694,10 +692,11 @@ impl LDNApplication {
694692
repo: String,
695693
) -> Result<ApplicationFile, LDNError> {
696694
// Get multisig threshold from blockchain
697-
let blockchain_threshold = match get_multisig_threshold_for_actor(&signer.signing_address).await {
698-
Ok(threshold) => Some(threshold),
699-
Err(_) => None,
700-
};
695+
let blockchain_threshold =
696+
match get_multisig_threshold_for_actor(&signer.signing_address).await {
697+
Ok(threshold) => Some(threshold),
698+
Err(_) => None,
699+
};
701700
// TODO: Convert DB errors to LDN Error
702701
// Get multisig threshold from the database
703702
let db_allocator = match get_allocator(&owner, &repo).await {
@@ -810,10 +809,11 @@ impl LDNApplication {
810809
repo: String,
811810
) -> Result<ApplicationFile, LDNError> {
812811
// Get multisig threshold from blockchain
813-
let blockchain_threshold = match get_multisig_threshold_for_actor(&signer.signing_address).await {
814-
Ok(threshold) => Some(threshold),
815-
Err(_) => None,
816-
};
812+
let blockchain_threshold =
813+
match get_multisig_threshold_for_actor(&signer.signing_address).await {
814+
Ok(threshold) => Some(threshold),
815+
Err(_) => None,
816+
};
817817

818818
// Get multisig threshold from the database
819819
let db_allocator = match get_allocator(&owner, &repo).await {
@@ -1653,7 +1653,32 @@ impl LDNApplication {
16531653
}
16541654
};
16551655
let signers: application::file::Verifiers = active_request.signers.clone();
1656-
let signer = signers.0.get(1).unwrap();
1656+
let num_signers = signers.0.len();
1657+
// Default to first signer for validation
1658+
let mut signer_index = 0;
1659+
1660+
// If there are multiple signers, fetch the multisig threshold to determine which signer to validate
1661+
if num_signers > 1 {
1662+
let blockchain_threshold =
1663+
get_multisig_threshold_for_actor(&signers.0[0].signing_address).await;
1664+
let multisig_threshold = match blockchain_threshold {
1665+
Ok(threshold) => threshold as usize,
1666+
Err(_) => {
1667+
let db_allocator =
1668+
get_allocator(&owner, &repo).await.map_err(|e| {
1669+
LDNError::Load(format!("Database error: {}", e))
1670+
})?;
1671+
db_allocator
1672+
.as_ref()
1673+
.and_then(|allocator| allocator.multisig_threshold)
1674+
.unwrap_or(2) as usize
1675+
}
1676+
};
1677+
// Adjust signer index based on multisig threshold
1678+
signer_index = if multisig_threshold <= 1 { 0 } else { 1 };
1679+
}
1680+
1681+
let signer = signers.0.get(signer_index).unwrap();
16571682

16581683
// Try getting the multisig threshold from the blockchain
16591684
let blockchain_threshold =
@@ -1663,21 +1688,22 @@ impl LDNApplication {
16631688
let multisig_threshold = match blockchain_threshold {
16641689
Ok(threshold) => threshold as usize,
16651690
Err(_) => {
1666-
let db_allocator = get_allocator(&owner, &repo).await
1667-
.map_err(|e| LDNError::Load(format!("Database error: {}", e)))?;
1691+
let db_allocator = get_allocator(&owner, &repo)
1692+
.await
1693+
.map_err(|e| LDNError::Load(format!("Database error: {}", e)))?;
16681694
db_allocator
16691695
.as_ref()
16701696
.and_then(|allocator| allocator.multisig_threshold)
16711697
.unwrap_or(2) as usize
16721698
}
16731699
};
1674-
1700+
16751701
// Check if the number of signers meets or exceeds the multisig threshold
16761702
if signers.0.len() < multisig_threshold {
16771703
log::warn!("Not enough signers for approval");
16781704
return Ok(false);
16791705
}
1680-
let signer_index = if multisig_threshold <= 1 { 0 } else { 1 };
1706+
let signer_index = if multisig_threshold <= 1 { 0 } else { 1 };
16811707

16821708
let signer = signers.0.get(signer_index).unwrap();
16831709
let signer_gh_handle = signer.github_username.clone();
@@ -1795,17 +1821,16 @@ impl LDNApplication {
17951821
}
17961822
}
17971823

1798-
pub async fn delete_merged_branch(owner: String, repo: String, branch_name: String) -> Result<bool, LDNError> {
1824+
pub async fn delete_merged_branch(
1825+
owner: String,
1826+
repo: String,
1827+
branch_name: String,
1828+
) -> Result<bool, LDNError> {
17991829
let gh = github_async_new(owner, repo).await;
18001830
let request = gh.build_remove_ref_request(branch_name.clone()).unwrap();
18011831

1802-
gh.remove_branch(request)
1803-
.await
1804-
.map_err(|e| {
1805-
return LDNError::New(format!(
1806-
"Error deleting branch {} /// {}",
1807-
branch_name, e
1808-
));
1832+
gh.remove_branch(request).await.map_err(|e| {
1833+
return LDNError::New(format!("Error deleting branch {} /// {}", branch_name, e));
18091834
})?;
18101835

18111836
Ok(true)

0 commit comments

Comments
 (0)