From 09522f0d9571f61374f665fedd733a61224f3104 Mon Sep 17 00:00:00 2001
From: tbro <tbro@users.noreply.github.com>
Date: Thu, 16 May 2024 14:53:27 -0500
Subject: [PATCH] Add a wrapper `Auction` type w/ rudimentary state machine

I'm probably going off on a limb, but its easier for me to think about
the types if there is some logic tying them together. Ultimately I
think we will expose a function to an event handler that will make the
gears turn. I believe `ViewNumber` can be passed in, and I'm guessing
`BidTx` we be as well.
---
 sequencer/src/auction.rs | 43 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/sequencer/src/auction.rs b/sequencer/src/auction.rs
index 8d68251421..f523029f26 100644
--- a/sequencer/src/auction.rs
+++ b/sequencer/src/auction.rs
@@ -1,7 +1,37 @@
-use crate::{state::FeeAmount, NamespaceId};
+use crate::{state::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 if we we structure.
+    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
@@ -12,12 +42,15 @@ struct AuctionConfig {
 }
 
 /// 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,
@@ -26,6 +59,7 @@ struct AuctionPhase {
 }
 
 /// Describes the 3 kinds of phases an active auction can be in
+#[derive(Clone, Copy)]
 enum AuctionPhaseKind {
     Bid,
     Assign,
@@ -43,6 +77,13 @@ struct BidTx {
     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,