diff --git a/sequencer/src/auction.rs b/sequencer/src/auction.rs new file mode 100644 index 000000000..b536f0cb0 --- /dev/null +++ b/sequencer/src/auction.rs @@ -0,0 +1,107 @@ +use crate::{ + state::{FeeAccount, FeeAmount}, + NamespaceId, Transaction, +}; +use hotshot_types::data::ViewNumber; +use std::num::NonZeroU64; + +/// State Machine for Auction +#[derive(Clone, Copy)] +struct Auction { + /// current phase. + phase: AuctionPhase, + /// A structure to find which Phase should be the current one. + // This could probably be an enum with some restructuring. + possible_phases: [AuctionPhase; 3], +} + +impl Auction { + fn update(mut self, view: ViewNumber) { + self.phase = self + .possible_phases + .into_iter() + .find(|phase| (view > phase.start && view < phase.end)) + .unwrap(); + } + fn recv_bid(&self, view: ViewNumber, bid: BidTx) { + self.update(view); + match self.phase.kind { + AuctionPhaseKind::Bid => self.send_txn(bid.as_txn()), + _ => unimplemented!(), + } + } + fn send_txn(&self, txn: Transaction) { + unimplemented!(); + } +} + +// - needs to be configured in genesis block +// - needs to be updatable +/// Configuration for the auction system +struct AuctionConfig { + bid_phase_num_views: NonZeroU64, + auction_phase_num_views: NonZeroU64, + sequencing_phase_num_views: NonZeroU64, +} + +/// Uniquely identifies an auction for sequencing rights of namespaces in the network +#[derive(Clone, Copy)] +struct AuctionId(u64); + +/// Uniquely identifies one auction phase for a specific auction +#[derive(Clone, Copy)] +struct AuctionPhaseId(AuctionId, AuctionPhaseKind); + +/// Describes one auction phase for a specific auction +#[derive(Clone, Copy)] +struct AuctionPhase { + id: AuctionPhaseId, + kind: AuctionPhaseKind, + start: ViewNumber, + end: ViewNumber, +} + +/// Describes the 3 kinds of phases an active auction can be in +#[derive(Clone, Copy)] +enum AuctionPhaseKind { + Bid, + Assign, + Sequence, +} + +struct Signature; + +/// A transaction to bid for the sequencing rights of a namespace +struct BidTx { + auction: AuctionId, + amount: FeeAmount, + account: FeeAccount, + namespace: NamespaceId, + nonce: Nonce, + signature: Signature, +} + +impl BidTx { + // maybe better implemented as a From on Transaction + fn as_txn(&self) -> Transaction { + unimplemented!(); + } +} + +/// A solution to one auction of sequencing rights for namespaces +struct AuctionResultTx { + auction: AuctionId, + nonce: Nonce, + winners: Vec, + signature: Signature, +} + +// Not sure if needed +struct BidRefundTx { + nonce: Nonce, + txns_to_refund: Vec, + signature: Signature, +} + +/// Nonce for special (auction) transactions +struct Nonce(u64); diff --git a/sequencer/src/lib.rs b/sequencer/src/lib.rs index 6bdc69bdc..5ec687e9a 100644 --- a/sequencer/src/lib.rs +++ b/sequencer/src/lib.rs @@ -1,4 +1,5 @@ pub mod api; +pub mod auction; pub mod block; pub mod catchup; mod chain_config; diff --git a/sequencer/src/state.rs b/sequencer/src/state.rs index fac937ae1..4f4153623 100644 --- a/sequencer/src/state.rs +++ b/sequencer/src/state.rs @@ -629,6 +629,19 @@ impl ValidatedState { let mut validated_state = apply_proposal(&validated_state, &mut delta, parent_leaf, l1_deposits); + // TODO we will need something similar to `charge_fee` but + // that charges `BidTx` to the account. We will also validate + // against bid phase time constraints. + // + // According to discussion we may receive multiple bids per + // header (up to configurable maximum), so this will likely be + // represented as `Vec` where `BidTx` holds account and + // amount. + // + // Note that different from `charge_fee` fee for the bid (sent + // to fee_recipient) whilch the bid itself will be held in an + // escrow account. + // charge_bids(&mut validated_state, bids_vec, fee_recipient, escrow_account); charge_fee( &mut validated_state, &mut delta,