oasis_core_runtime/consensus/
scheduler.rs1use anyhow::{anyhow, Result};
3
4use crate::{
5 common::{crypto::signature::PublicKey, namespace::Namespace},
6 consensus::beacon::EpochTime,
7};
8
9#[derive(
11 Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, cbor::Encode, cbor::Decode,
12)]
13#[repr(u8)]
14pub enum Role {
15 #[default]
17 Invalid = 0,
18 Worker = 1,
20 BackupWorker = 2,
22}
23
24#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
26pub struct CommitteeNode {
27 pub role: Role,
29
30 pub public_key: PublicKey,
32}
33
34#[derive(
36 Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, cbor::Encode, cbor::Decode,
37)]
38#[repr(u8)]
39pub enum CommitteeKind {
40 #[default]
42 Invalid = 0,
43 ComputeExecutor = 1,
45}
46
47#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
49pub struct Committee {
50 pub kind: CommitteeKind,
52
53 pub members: Vec<CommitteeNode>,
55
56 pub runtime_id: Namespace,
58
59 pub valid_for: EpochTime,
61}
62
63impl Committee {
64 pub fn workers(&self) -> Vec<&CommitteeNode> {
66 self.members
67 .iter()
68 .filter(|&member| member.role == Role::Worker)
69 .collect()
70 }
71
72 pub fn transaction_scheduler(&self, round: u64) -> Result<&CommitteeNode> {
74 let workers = self.workers();
75 if workers.is_empty() {
76 return Err(anyhow!("GetTransactionScheduler: no workers in committee"));
77 }
78 let scheduler_idx = round as usize % workers.len();
79 let scheduler = workers[scheduler_idx];
80
81 Ok(scheduler)
82 }
83}