-
Notifications
You must be signed in to change notification settings - Fork 9
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
submit solution only if solution is better than the solution in the chain #269
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -41,14 +41,21 @@ impl<'a> StableXDriver<'a> { | |
solution | ||
}; | ||
|
||
let submitted = if solution.executed_sell_amounts.iter().sum::<u128>() > 0 { | ||
self.contract.submit_solution(batch, orders, solution)?; | ||
info!("Successfully applied solution to batch {}", batch); | ||
true | ||
let current_objective_value = self.contract.get_current_objective_value()?; | ||
let submitted = if let Some(objective_value) = solution.objective_value { | ||
if current_objective_value < objective_value { | ||
self.contract.submit_solution(batch, orders, solution)?; | ||
info!("Successfully applied solution to batch {}", batch); | ||
true | ||
} else { | ||
info!("Not submitting trivial solution for batch {}", batch); | ||
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. This log message seems off. 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. I agree. Also, this wouldn't be possible anyway. |
||
false | ||
} | ||
} else { | ||
info!("Not submitting trivial solution for batch {}", batch); | ||
false | ||
}; | ||
josojo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self.past_auctions.insert(batch); | ||
Ok(submitted) | ||
} | ||
|
@@ -83,14 +90,18 @@ mod tests { | |
.get_auction_data | ||
.given(batch - 1) | ||
.will_return(Ok((state.clone(), orders.clone()))); | ||
contract | ||
.get_current_objective_value | ||
.given(()) | ||
.will_return(Ok(U256::zero())); | ||
|
||
contract | ||
.submit_solution | ||
.given((batch - 1, Val(orders.clone()), Any)) | ||
.will_return(Ok(())); | ||
|
||
let solution = Solution { | ||
objective_value: Some(U256::zero()), | ||
objective_value: Some(U256::one()), | ||
prices: vec![1, 2], | ||
executed_sell_amounts: vec![0, 2], | ||
executed_buy_amounts: vec![0, 2], | ||
|
@@ -122,13 +133,18 @@ mod tests { | |
.given(batch - 1) | ||
.will_return(Ok((state.clone(), orders.clone()))); | ||
|
||
contract | ||
.get_current_objective_value | ||
.given(()) | ||
.will_return(Ok(U256::zero())); | ||
|
||
contract | ||
.submit_solution | ||
.given((batch - 1, Val(orders.clone()), Any)) | ||
.will_return(Ok(())); | ||
|
||
let solution = Solution { | ||
objective_value: Some(U256::zero()), | ||
objective_value: Some(U256::one()), | ||
prices: vec![1, 2], | ||
executed_sell_amounts: vec![0, 2], | ||
executed_buy_amounts: vec![0, 2], | ||
|
@@ -145,6 +161,45 @@ mod tests { | |
//Second auction | ||
assert_eq!(driver.run().unwrap(), false); | ||
} | ||
#[test] | ||
|
||
fn does_not_process_solution_if_better_one_exists() { | ||
let contract = StableXContractMock::default(); | ||
let mut pf = PriceFindingMock::default(); | ||
|
||
let orders = vec![create_order_for_test(), create_order_for_test()]; | ||
let state = create_account_state_with_balance_for(&orders); | ||
|
||
let batch = U256::from(42); | ||
contract | ||
.get_current_auction_index | ||
.given(()) | ||
.will_return(Ok(batch)); | ||
|
||
contract | ||
.get_auction_data | ||
.given(batch - 1) | ||
.will_return(Ok((state.clone(), orders.clone()))); | ||
|
||
contract | ||
.get_current_objective_value | ||
.given(()) | ||
.will_return(Ok(U256::from(100))); | ||
|
||
let solution = Solution { | ||
objective_value: Some(U256::one()), | ||
prices: vec![1, 2], | ||
executed_sell_amounts: vec![0, 2], | ||
executed_buy_amounts: vec![0, 2], | ||
}; | ||
pf.find_prices | ||
.given((orders, state)) | ||
.will_return(Ok(solution)); | ||
|
||
let mut driver = StableXDriver::new(&contract, &mut pf); | ||
|
||
assert_eq!(driver.run().unwrap(), false); | ||
} | ||
|
||
#[test] | ||
fn test_errors_on_failing_contract() { | ||
|
@@ -199,14 +254,19 @@ mod tests { | |
.given(()) | ||
.will_return(Ok(batch)); | ||
|
||
contract | ||
.get_current_objective_value | ||
.given(()) | ||
.will_return(Ok(U256::zero())); | ||
|
||
contract | ||
.get_auction_data | ||
.given(batch - 1) | ||
.will_return(Ok((state.clone(), orders.clone()))); | ||
|
||
let mut driver = StableXDriver::new(&contract, &mut pf); | ||
|
||
assert!(driver.run().is_ok()); | ||
assert_eq!(driver.run().unwrap(), false); | ||
assert!(!mock_it::verify( | ||
pf.find_prices.was_called_with((orders, state)) | ||
)); | ||
|
@@ -226,6 +286,11 @@ mod tests { | |
.given(()) | ||
.will_return(Ok(batch)); | ||
|
||
contract | ||
.get_current_objective_value | ||
.given(()) | ||
.will_return(Ok(U256::zero())); | ||
|
||
contract | ||
.get_auction_data | ||
.given(batch - 1) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
How about an objective value of 0? Would that ever get submitted?
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.
At the moment, the contract does not allow for trivial solution submission. Furthermore, it does not accept solutions with zero objective evaluation either.