Skip to content

Commit

Permalink
Merge winners with participants (#3042)
Browse files Browse the repository at this point in the history
# Description
Extracted from https://github.com/cowprotocol/services/pull/2980/files

Merges participants and winners into a single vector, which is much
easier to work with and forward around. Also reduces quite a few
roundtrips when iterating over participants to determine winning and
non-winning orders etc.

Winner selection logic is just moved into `Runloop::competition`
function so that `Participant` could become a main struct we work with
and try to even remove `RawParticipant` with refactors in the future, or
at least make it local to competition only.

# Changes
<!-- List of detailed changes (how the change is accomplished) -->

- [ ] Moved `Participants` into `domain`
- [ ] `Participants` now contain `winner` to determine if it's a winner
and allowed to settle.

## How to test
Existing e2e tests.

<!--
## Related Issues

Fixes #
-->
  • Loading branch information
sunce86 authored Oct 10, 2024
1 parent 943d2ad commit 5dc5029
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 120 deletions.
9 changes: 5 additions & 4 deletions crates/autopilot/src/domain/competition/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use {
super::auction::order,
crate::{
domain,
domain::{auction, eth},
},
crate::domain::{self, auction, eth},
derive_more::Display,
std::collections::HashMap,
};

mod participant;

pub use participant::{Participant, Ranked, Unranked};

type SolutionId = u64;

#[derive(Debug, Clone)]
Expand Down
48 changes: 48 additions & 0 deletions crates/autopilot/src/domain/competition/participant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use {super::Solution, crate::infra, std::sync::Arc};

#[derive(Clone)]
pub struct Participant<State = Ranked> {
solution: Solution,
driver: Arc<infra::Driver>,
state: State,
}

#[derive(Clone)]
pub struct Unranked;
pub struct Ranked {
is_winner: bool,
}

impl<T> Participant<T> {
pub fn solution(&self) -> &Solution {
&self.solution
}

pub fn driver(&self) -> &Arc<infra::Driver> {
&self.driver
}
}

impl Participant<Unranked> {
pub fn new(solution: Solution, driver: Arc<infra::Driver>) -> Self {
Self {
solution,
driver,
state: Unranked,
}
}

pub fn rank(self, is_winner: bool) -> Participant<Ranked> {
Participant::<Ranked> {
state: Ranked { is_winner },
solution: self.solution,
driver: self.driver,
}
}
}

impl Participant<Ranked> {
pub fn is_winner(&self) -> bool {
self.state.is_winner
}
}
Loading

0 comments on commit 5dc5029

Please sign in to comment.