-
Notifications
You must be signed in to change notification settings - Fork 1
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
Multiple feats #186
Multiple feats #186
Changes from 3 commits
dea3838
cd05c50
9448568
0b74edf
8f58e4a
8d71307
b60ee70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ use serde_json::from_str; | |
|
||
use crate::{ | ||
base64, config::get_env_var_or_default, core::application::file::Allocations, error::LDNError, external_services::{ | ||
blockchain::BlockchainData, filecoin::get_multisig_threshold_for_actor, github::{ | ||
blockchain::{compare_allowance_and_allocation, BlockchainData}, filecoin::get_multisig_threshold_for_actor, github::{ | ||
github_async_new, CreateMergeRequestData, CreateRefillMergeRequestData, GithubWrapper, | ||
} | ||
}, parsers::ParsedIssue | ||
|
@@ -814,13 +814,27 @@ impl LDNApplication { | |
Ok(s) => match s { | ||
AppState::Submitted | AppState::AdditionalInfoRequired | AppState::AdditionalInfoSubmitted => { | ||
let app_file: ApplicationFile = self.file().await?; | ||
|
||
let db_allocator = match get_allocator(&owner, &repo).await { | ||
Ok(allocator) => allocator.unwrap(), | ||
Err(err) => { | ||
return Err(LDNError::New(format!("Database: get_allocator: {}", err))); | ||
} | ||
}; | ||
let db_multisig_address = db_allocator.multisig_address.unwrap(); | ||
Self::check_and_handle_allowance( | ||
&db_multisig_address.clone(), | ||
Some(allocation_amount.clone()), | ||
).await?; | ||
|
||
let uuid = uuidv4::uuid::v4(); | ||
let request = AllocationRequest::new( | ||
actor.clone(), | ||
uuid, | ||
AllocationRequestType::First, | ||
allocation_amount, | ||
); | ||
|
||
let app_file = app_file.complete_governance_review(actor.clone(), request); | ||
let file_content = serde_json::to_string_pretty(&app_file).unwrap(); | ||
let app_path = &self.file_name.clone(); | ||
|
@@ -967,6 +981,11 @@ impl LDNApplication { | |
app_lifecycle, | ||
); | ||
if new_allocation_amount.is_some() && app_file.allocation.0.len() > 1 { | ||
Self::check_and_handle_allowance( | ||
&db_multisig_address.clone(), | ||
new_allocation_amount.clone(), | ||
).await?; | ||
|
||
let _ = app_file.adjust_active_allocation_amount(new_allocation_amount.unwrap().clone()); | ||
} | ||
|
||
|
@@ -1107,7 +1126,16 @@ impl LDNApplication { | |
)); | ||
} | ||
|
||
// Check the allowance for the address | ||
|
||
if new_allocation_amount.is_some() && app_file.allocation.0.len() > 1 { | ||
let db_multisig_address = db_allocator.multisig_address.unwrap(); | ||
|
||
Self::check_and_handle_allowance( | ||
&db_multisig_address.clone(), | ||
new_allocation_amount.clone(), | ||
).await?; | ||
|
||
let _ = app_file.adjust_active_allocation_amount(new_allocation_amount.unwrap().clone()); | ||
} | ||
|
||
|
@@ -2521,6 +2549,40 @@ impl LDNApplication { | |
} | ||
} | ||
|
||
async fn check_and_handle_allowance( | ||
db_multisig_address: &str, | ||
new_allocation_amount: Option<String>, | ||
) -> Result<(), LDNError> { | ||
let blockchain = BlockchainData::new(); | ||
match blockchain.get_allowance_for_address("f24siazyti3akorqyvi33rvlq3i73j23rwohdamuy").await { | ||
Ok(allowance) if allowance != "0" => { | ||
log::info!("Allowance found and is not zero. Value is {}", allowance); | ||
match compare_allowance_and_allocation(&allowance, new_allocation_amount) { | ||
Some(result) => { | ||
if result { | ||
println!("Allowance is sufficient."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove prints on tagging |
||
Ok(()) | ||
} else { | ||
println!("Allowance is not sufficient."); | ||
Err(LDNError::New("Multisig address has less allowance than the new allocation amount".to_string())) | ||
} | ||
}, | ||
None => { | ||
println!("Error parsing sizes."); | ||
Err(LDNError::New("Error parsing sizes".to_string())) | ||
}, | ||
} | ||
}, | ||
Ok(_) => { | ||
Err(LDNError::New("Multisig address has no remaining allowance".to_string())) | ||
}, | ||
Err(e) => { | ||
log::error!("Failed to retrieve allowance: {:?}", e); | ||
Err(LDNError::New("Failed to retrieve allowance".to_string())) | ||
} | ||
} | ||
} | ||
|
||
pub async fn create_pr_from_issue_modification(parsed_ldn: ParsedIssue, application_model: ApplicationModel) -> Result<Self, LDNError> { | ||
let merged_application = ApplicationFile::from_str(&application_model.application.unwrap()) | ||
.map_err(|e| LDNError::Load(format!("Failed to parse application file from DB: {}", e))).unwrap(); | ||
|
@@ -3483,9 +3545,11 @@ impl LDNPullRequest { | |
)); | ||
})?; | ||
|
||
let issue_link = format!("https://github.com/{}/{}/issues/{}", owner, repo, application_id); | ||
|
||
let (_pr, file_sha) = gh | ||
.create_merge_request(CreateMergeRequestData { | ||
application_id: application_id.clone(), | ||
issue_link, | ||
branch_name: app_branch_name, | ||
file_name, | ||
owner_name, | ||
|
@@ -3526,9 +3590,12 @@ impl LDNPullRequest { | |
application_id, e | ||
)); | ||
})?; | ||
|
||
let issue_link = format!("https://github.com/{}/{}/issues/{}", owner, repo, application_id); | ||
|
||
let pr = match gh | ||
.create_refill_merge_request(CreateRefillMergeRequestData { | ||
application_id: application_id.clone(), | ||
issue_link, | ||
owner_name, | ||
file_name: file_name.clone(), | ||
file_sha: file_sha.clone(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
const BASE_URL: &str = "https://api.filplus.d.interplanetary.one/public/api"; | ||
extern crate regex; | ||
use regex::Regex; | ||
|
||
/// BlockchainData is a client for the Fil+ blockchain data API. | ||
pub struct BlockchainData { | ||
|
@@ -94,6 +96,10 @@ impl BlockchainData { | |
let allowance = json["allowance"].as_str().unwrap_or(""); | ||
Ok(allowance.to_string()) | ||
} | ||
Some("verifier") => { | ||
let allowance = json["allowance"].as_str().unwrap_or(""); | ||
Ok(allowance.to_string()) | ||
} | ||
Some("error") => { | ||
let message = json["message"].as_str().unwrap_or(""); | ||
Err(BlockchainDataError::Err(message.to_string())) | ||
|
@@ -110,3 +116,68 @@ impl BlockchainData { | |
format!("{}/{}", self.base_url, path) | ||
} | ||
} | ||
|
||
|
||
fn parse_size_to_bytes(size: &str) -> Option<u64> { | ||
let re = Regex::new(r"^(\d+)([a-zA-Z]+)$").unwrap(); | ||
let caps = re.captures(size.trim())?; | ||
|
||
let number = caps.get(1)?.as_str().parse::<u64>().ok()?; | ||
let unit = caps.get(2)?.as_str().to_uppercase(); | ||
|
||
// Normalize the unit by removing any trailing 'i', 's' and converting to upper case | ||
let normalized_unit = unit.trim_end_matches('S'); | ||
|
||
match normalized_unit { | ||
"KIB" => Some(number * 1024), // 2^10 | ||
"MIB" => Some(number * 1024 * 1024), // 2^20 | ||
"GIB" => Some(number * 1024 * 1024 * 1024), // 2^30 | ||
"TIB" => Some(number * 1024 * 1024 * 1024 * 1024), // 2^40 | ||
"PIB" => Some(number * 1024 * 1024 * 1024 * 1024 * 1024), // 2^50 | ||
"KB" => Some(number * 1000), // 10^3 | ||
"MB" => Some(number * 1000 * 1000), // 10^6 | ||
"GB" => Some(number * 1000 * 1000 * 1000), // 10^9 | ||
"TB" => Some(number * 1000 * 1000 * 1000 * 1000), // 10^12 | ||
"PB" => Some(number * 1000 * 1000 * 1000 * 1000 * 1000), // 10^15 | ||
_ => None, // Unsupported unit | ||
} | ||
} | ||
|
||
pub fn compare_allowance_and_allocation(allowance: &str, new_allocation_amount: Option<String>) -> Option<bool> { | ||
println!("Allowance: {} bytes, Allocation: {} bytes", allowance, new_allocation_amount.clone().unwrap()); | ||
|
||
let allowance_bytes: u64 = match allowance.parse::<u64>() { | ||
Ok(value) => { | ||
println!("Allowance value: {}", value); | ||
value | ||
} | ||
Err(_) => { | ||
println!("Error parsing allowance value"); | ||
return None | ||
} | ||
}; | ||
|
||
|
||
let allocation_bytes = match new_allocation_amount { | ||
Some(amount) => { | ||
println!("Allowance value: {}", amount); | ||
parse_size_to_bytes(&amount)? | ||
}, | ||
None => { | ||
println!("Error parsing allocation value"); | ||
return None | ||
}, | ||
}; | ||
|
||
println!("==============================="); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove prints on tagging |
||
println!("==============================="); | ||
println!("==============================="); | ||
println!("Allowance end: {} bytes, Allocation end: {} bytes", allowance_bytes, allocation_bytes); | ||
println!("==============================="); | ||
println!("==============================="); | ||
println!("==============================="); | ||
|
||
println!("COMPARISON: {}", allowance_bytes >= allocation_bytes); | ||
|
||
Some(allowance_bytes >= allocation_bytes) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave this as it is just for testing purposes. We need to remove it when tagging into prod