-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle (panic) on missing payment chosen platform (#2646)
Remove rinkeby as the default payment platform
- Loading branch information
1 parent
7c406b3
commit 5a68e2d
Showing
9 changed files
with
224 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod demand_validation; | ||
pub mod expiration; | ||
pub mod manifest; | ||
pub mod max_agreements; | ||
|
139 changes: 139 additions & 0 deletions
139
agent/provider/src/market/negotiator/builtin/demand_validation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
use ya_agreement_utils::OfferDefinition; | ||
|
||
use crate::market::negotiator::factory::DemandValidationNegotiatorConfig; | ||
use crate::market::negotiator::{ | ||
AgreementResult, NegotiationResult, NegotiatorComponent, ProposalView, | ||
}; | ||
|
||
/// Negotiator that verifies that all required fields are present in proposal. | ||
pub struct DemandValidation { | ||
required_fields: Vec<String>, | ||
} | ||
|
||
impl DemandValidation { | ||
pub fn new(config: &DemandValidationNegotiatorConfig) -> DemandValidation { | ||
let required_fields = config.required_fields.clone(); | ||
Self { required_fields } | ||
} | ||
} | ||
|
||
impl NegotiatorComponent for DemandValidation { | ||
fn negotiate_step( | ||
&mut self, | ||
demand: &ProposalView, | ||
offer: ProposalView, | ||
) -> anyhow::Result<NegotiationResult> { | ||
let missing_fields = self | ||
.required_fields | ||
.iter() | ||
.filter(|x| demand.pointer(x).is_none()) | ||
.cloned() | ||
.collect::<Vec<String>>(); | ||
if missing_fields.is_empty() { | ||
Ok(NegotiationResult::Ready { offer }) | ||
} else { | ||
log::info!( | ||
"'DemandValidation' negotiator: Reject proposal [{}] due to missing fields: {}", | ||
demand.id, | ||
missing_fields.join(",") | ||
); | ||
Ok(NegotiationResult::Reject { | ||
message: format!("Missing fields: {}", missing_fields.join(",")), | ||
is_final: false, | ||
}) | ||
} | ||
} | ||
|
||
fn fill_template( | ||
&mut self, | ||
offer_template: OfferDefinition, | ||
) -> anyhow::Result<OfferDefinition> { | ||
Ok(offer_template) | ||
} | ||
|
||
fn on_agreement_terminated( | ||
&mut self, | ||
_agreement_id: &str, | ||
_result: &AgreementResult, | ||
) -> anyhow::Result<()> { | ||
Ok(()) | ||
} | ||
|
||
fn on_agreement_approved(&mut self, _agreement_id: &str) -> anyhow::Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use chrono::Utc; | ||
use serde_json::json; | ||
use ya_agreement_utils::agreement::expand; | ||
use ya_agreement_utils::OfferTemplate; | ||
use ya_client_model::market::proposal::State; | ||
|
||
fn config() -> DemandValidationNegotiatorConfig { | ||
DemandValidationNegotiatorConfig { | ||
required_fields: vec![ | ||
"/golem/com/freebies".to_string(), | ||
"/golem/com/payment/address".to_string(), | ||
], | ||
} | ||
} | ||
|
||
fn properties_to_proposal(properties: serde_json::Value) -> ProposalView { | ||
ProposalView { | ||
content: OfferTemplate { | ||
properties: expand(properties), | ||
constraints: "()".to_string(), | ||
}, | ||
id: "proposalId".to_string(), | ||
issuer: Default::default(), | ||
state: State::Initial, | ||
timestamp: Utc::now(), | ||
} | ||
} | ||
|
||
/// Negotiator accepts demand if all of the required fields exist | ||
#[test] | ||
fn test_required_fields_exist() { | ||
let config = config(); | ||
let mut negotiator = DemandValidation::new(&config); | ||
|
||
let offer = properties_to_proposal(json!({})); | ||
let demand = properties_to_proposal(json!({ | ||
"golem.com.freebies": "mug", | ||
"golem.com.payment.address": "0x123", | ||
})); | ||
|
||
let expected_result = NegotiationResult::Ready { | ||
offer: offer.clone(), | ||
}; | ||
assert_eq!( | ||
negotiator.negotiate_step(&demand, offer).unwrap(), | ||
expected_result | ||
); | ||
} | ||
|
||
/// Negotiator rejects demand if some of the required fields are missing | ||
#[test] | ||
fn test_required_fields_missing() { | ||
let config = config(); | ||
let mut negotiator = DemandValidation::new(&config); | ||
|
||
let offer = properties_to_proposal(json!({})); | ||
let demand = properties_to_proposal(json!({ | ||
"golem.com.freebies": "mug", | ||
})); | ||
|
||
let expected_result = NegotiationResult::Reject { | ||
message: "Missing fields: /golem/com/payment/address".to_string(), | ||
is_final: false, | ||
}; | ||
assert_eq!( | ||
negotiator.negotiate_step(&demand, offer).unwrap(), | ||
expected_result | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters