Просмотр исходного кода

[blockchain/epoch] remove EpochItem from EpochConsensus

mohab metwally 3 лет назад
Родитель
Сommit
df690f54b8
5 измененных файлов с 80 добавлено и 367 удалено
  1. 4 5
      example/lead.rs
  2. 61 343
      script/research/ec/valuation.ipynb
  3. 13 17
      src/blockchain/epoch.rs
  4. 1 1
      src/blockchain/mod.rs
  5. 1 1
      src/stakeholder/stakeholder.rs

+ 4 - 5
example/lead.rs

@@ -5,7 +5,7 @@ use url::Url;
 
 use darkfi::{
     blockchain::{
-        epoch::{Epoch, EpochItem},
+        epoch::{Epoch},
         EpochConsensus,
     },
     crypto::leadcoin::{LeadCoin, LEAD_PUBLIC_INPUT_LEN},
@@ -19,9 +19,8 @@ fn main() {
 
     //
     const LEN: usize = 10;
-    let epoch_item = EpochItem {
-        value: 332233,  //static stake value
-    };
+    let value = 33223;  //static stake value
+
     //
     let settings = Settings {
         inbound: vec![Url::parse("tls://127.0.0.1:12002").unwrap()],
@@ -46,7 +45,7 @@ fn main() {
         block_on(Stakeholder::new(consensus, settings, "db", 0, Some(k))).unwrap();
 
     let eta: pallas::Base = stakeholder.get_eta();
-    let mut epoch = Epoch { len: Some(LEN), item: Some(epoch_item), eta, coins: vec![] };
+    let mut epoch = Epoch { len: Some(LEN), value: Some(value), eta, coins: vec![] };
     // sigma is nubmer of slots * reward (assuming reward is 1 for simplicity)
     let sigma = pallas::Base::from(10);
     let coins: Vec<LeadCoin> = epoch.create_coins(sigma);

+ 61 - 343
script/research/ec/valuation.ipynb

@@ -16,21 +16,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": null,
    "id": "5d554e0c",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "Fraction Field of Quotient of Multivariate Polynomial Ring in x, y over Finite Field of size 11 by the ideal (-x^3 + y^2 - 4*x)"
-      ]
-     },
-     "execution_count": 5,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "# Define our base polynomial ring over Z_11\n",
     "K.<x, y> = GF(11)[]\n",
@@ -55,21 +44,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 11,
+   "execution_count": null,
    "id": "c430ae30",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "True"
-      ]
-     },
-     "execution_count": 11,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "X, Y = S(x), S(y)\n",
     "(Y - 4)*(Y + 4) == (X - 2)^3 - 5*(X - 2)^2 - 6*(X - 2)"
@@ -91,21 +69,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 10,
+   "execution_count": null,
    "id": "bd415ef9",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "-2*x + y"
-      ]
-     },
-     "execution_count": 10,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "# Components for f\n",
     "a0, a1, a2 = -2, 1, 0\n",
@@ -133,21 +100,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
+   "execution_count": null,
    "id": "9e2be279",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "True"
-      ]
-     },
-     "execution_count": 13,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "# and lets double check this\n",
     "B0 = X - 2\n",
@@ -169,42 +125,20 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": null,
    "id": "249b0ed0",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "-2*y - 3"
-      ]
-     },
-     "execution_count": 14,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "-6 - 2*(y + 4)"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": null,
    "id": "5894ffc4",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "-2*y - 3"
-      ]
-     },
-     "execution_count": 15,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "-2*(y - 4)"
    ]
@@ -226,42 +160,20 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 16,
+   "execution_count": null,
    "id": "7b343008",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "-5*y + 3"
-      ]
-     },
-     "execution_count": 16,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "-5*(y + 4) - 2*(-6)"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 17,
+   "execution_count": null,
    "id": "595834a3",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "-5*y - 2"
-      ]
-     },
-     "execution_count": 17,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "-5*(y - 4)"
    ]
@@ -290,42 +202,20 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 18,
+   "execution_count": null,
    "id": "a0a83e30",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "(x - 2, y - 4, 1)"
-      ]
-     },
-     "execution_count": 18,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "b0, b1, b2"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 24,
+   "execution_count": null,
    "id": "d042e184",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "(-2, 1, 0)"
-      ]
-     },
-     "execution_count": 24,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "# Decompose polynomial function into basis components\n",
     "def decomp(f):\n",
@@ -340,21 +230,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 23,
+   "execution_count": null,
    "id": "c804233f",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "-x^3 + y^2 - 4*x"
-      ]
-     },
-     "execution_count": 23,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "Px, Py = (2, 4)\n",
     "EC_A = 4\n",
@@ -367,21 +246,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 27,
+   "execution_count": null,
    "id": "cbbb63c7",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "(x^2 + 2*x - 2*y, y + 4)"
-      ]
-     },
-     "execution_count": 27,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "a0, a1, a2 = decomp(f)\n",
     "g = 1\n",
@@ -404,21 +272,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 28,
+   "execution_count": null,
    "id": "d5810612",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "(x + 4, -2, 0)"
-      ]
-     },
-     "execution_count": 28,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "a0, a1, a2 = decomp(a0)\n",
     "a0, a1, a2"
@@ -434,21 +291,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 31,
+   "execution_count": null,
    "id": "22671821",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "True"
-      ]
-     },
-     "execution_count": 31,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "U = (X - 2)\n",
     "F = (X + 4)*(X - 2) - 2*(Y - 4)\n",
@@ -458,21 +304,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 32,
+   "execution_count": null,
    "id": "411c7216",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "(-2*x^2 + x*y + 4*y, y^2 - 3*y + 5)"
-      ]
-     },
-     "execution_count": 32,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "# Second reduction\n",
     "a0 = Eg*a0 + Ef*a1\n",
@@ -492,21 +327,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 33,
+   "execution_count": null,
    "id": "eceb52f7",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "(-2*x + y - 4, -5, 5)"
-      ]
-     },
-     "execution_count": 33,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "a0, a1, a2 = decomp(a0)\n",
     "a0, a1, a2"
@@ -522,7 +346,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 44,
+   "execution_count": null,
    "id": "1c654c46",
    "metadata": {},
    "outputs": [],
@@ -551,21 +375,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
+   "execution_count": null,
    "id": "ce94255a",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "(True, False)"
-      ]
-     },
-     "execution_count": 13,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "R.<x> = FunctionField(GF(11)); _.<Y> = R[]\n",
     "K.<y> = R.extension(Y^2 - x^3 - 4*x)\n",
@@ -587,56 +400,29 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": null,
    "id": "1a218ce1",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "True"
-      ]
-     },
-     "execution_count": 8,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "M.is_prime()"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": null,
    "id": "e737ac7f",
    "metadata": {
     "scrolled": true
    },
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "[(Ideal (x + 9, y + 4) of Maximal order of Function field in y defined by y^2 + 10*x^3 + 7*x,\n",
-       "  1,\n",
-       "  1),\n",
-       " (Ideal (x + 9, y + 7) of Maximal order of Function field in y defined by y^2 + 10*x^3 + 7*x,\n",
-       "  1,\n",
-       "  1)]"
-      ]
-     },
-     "execution_count": 14,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "O.decomposition(M)"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 19,
+   "execution_count": null,
    "id": "867a9f68",
    "metadata": {},
    "outputs": [],
@@ -648,21 +434,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 20,
+   "execution_count": null,
    "id": "2a10bcd6",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "(True, True, False)"
-      ]
-     },
-     "execution_count": 20,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "f = y - 2*x\n",
     "f in M, f in M^2, f in M^3"
@@ -670,21 +445,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 21,
+   "execution_count": null,
    "id": "73406fd6",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "2"
-      ]
-     },
-     "execution_count": 21,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "d = [i for i in range(1, 10) if f in M^i]\n",
     "max(d)"
@@ -702,21 +466,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 1,
+   "execution_count": null,
    "id": "02ae75dc",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "10*t^3 + 9*t^2"
-      ]
-     },
-     "execution_count": 1,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "K.<t> = GF(11)[]\n",
     "x = t + 2\n",
@@ -747,31 +500,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 2,
+   "execution_count": null,
    "id": "3da404fc",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "[Place (x, y),\n",
-       " Place (x + 3, y + 4),\n",
-       " Place (x + 3, y + 7),\n",
-       " Place (x + 5, y + 3),\n",
-       " Place (x + 5, y + 8),\n",
-       " Place (x + 7, y + 5),\n",
-       " Place (x + 7, y + 6),\n",
-       " Place (x + 9, y + 4),\n",
-       " Place (x + 9, y + 7),\n",
-       " Place (x + 10, y + 4),\n",
-       " Place (x + 10, y + 7)]"
-      ]
-     },
-     "execution_count": 2,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "K.<x> = FunctionField(GF(11))\n",
     "_.<Y> = K[]\n",
@@ -789,21 +521,10 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": null,
    "id": "f87c8b23",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "Place (x + 9, y + 7)"
-      ]
-     },
-     "execution_count": 3,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "# I don't know how to actually construct this yet\n",
     "p = L.places_finite()[-3]\n",
@@ -812,29 +533,26 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": null,
    "id": "3d007b0b",
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "2"
-      ]
-     },
-     "execution_count": 4,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
     "(y - 2*x).valuation(p)"
    ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "021f9110",
+   "metadata": {},
+   "outputs": [],
+   "source": []
   }
  ],
  "metadata": {
   "kernelspec": {
-   "display_name": "SageMath 9.5",
+   "display_name": "SageMath 9.7",
    "language": "sage",
    "name": "sagemath"
   },

+ 13 - 17
src/blockchain/epoch.rs

@@ -23,24 +23,18 @@ use crate::crypto::{
 };
 
 const PRF_NULLIFIER_PREFIX: u64 = 0;
-
 const MERKLE_DEPTH: u8 = MERKLE_DEPTH_ORCHARD as u8;
 
-#[derive(Copy, Debug, Default, Clone)]
-pub struct EpochItem {
-    pub value: u64, // the stake value is static during the epoch.
-}
-
 /// epoch configuration
 /// this struct need be a singleton,
-/// should be populated from configuration file.
+/// TODO should be populated from configuration file.
 #[derive(Copy, Debug, Default, Clone)]
 pub struct EpochConsensus {
-    pub sl_len: u64,
+    pub sl_len: u64, // length of slot in terms of ticks
     /// number of slots per epoch
-    pub e_len: u64,
-    pub tick_len: u64,
-    pub reward: u64,
+    pub e_len: u64, // length of epoch in terms of slots
+    pub tick_len: u64, // length of tick in terms of seconds
+    pub reward: u64, // constant reward value for the slot leader
 }
 
 impl EpochConsensus {
@@ -58,19 +52,23 @@ impl EpochConsensus {
         }
     }
 
-    /// TODO how is the reward derived?
+    /// getter for constant stakeholder reward
+    /// used for configuring the stakeholder reward value
     pub fn get_reward(&self) -> u64 {
         self.reward
     }
 
+    /// getter for the slot length in terms of ticks
     pub fn get_slot_len(&self) -> u64 {
         self.sl_len
     }
 
+    /// getter for the epoch length in terms of slots
     pub fn get_epoch_len(&self) -> u64 {
         self.e_len
     }
 
+    /// getter for the
     pub fn get_tick_len(&self) -> u64 {
         self.tick_len
     }
@@ -78,12 +76,10 @@ impl EpochConsensus {
 
 #[derive(Debug, Default, Clone)]
 pub struct Epoch {
-    // TODO this need to emulate epoch
     // should have ep, slot, current block, etc.
     //epoch metadata
     pub len: Option<usize>, // number of slots in the epoch
-    //epoch item
-    pub item: Option<EpochItem>,
+    pub value: Option<u64>, // the stake value is static during the epoch.
     pub eta: pallas::Base,    // CRS for the leader selection.
     pub coins: Vec<LeadCoin>, // competing coins
 }
@@ -92,7 +88,7 @@ impl Epoch {
     pub fn new(consensus: EpochConsensus, true_random: pallas::Base) -> Self {
         Self {
             len: Some(consensus.get_slot_len() as usize),
-            item: Some(EpochItem { value: consensus.reward }),
+            value: Some(consensus.reward),
             eta: true_random,
             coins: vec![],
         }
@@ -171,7 +167,7 @@ impl Epoch {
         let mut tree_cm = BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(self.len.unwrap() as usize);
         let mut coins: Vec<LeadCoin> = vec![];
         for i in 0..self.len.unwrap() {
-            let c_v = pallas::Base::from(self.item.unwrap().value);
+            let c_v = pallas::Base::from(self.value.unwrap());
             //random sampling of the same size of prf,
             //pseudo random sampling that is the size of pederson commitment
             // coin slot number

+ 1 - 1
src/blockchain/mod.rs

@@ -7,7 +7,7 @@ use crate::{
 };
 
 pub mod epoch;
-pub use epoch::{Epoch, EpochConsensus, EpochItem};
+pub use epoch::{Epoch, EpochConsensus};
 
 pub mod blockstore;
 pub use blockstore::{BlockOrderStore, BlockStore, HeaderStore};

+ 1 - 1
src/stakeholder/stakeholder.rs

@@ -383,7 +383,7 @@ impl Stakeholder {
     /// * `e` - epoch index
     /// * `sl` - slot relative index
     fn new_slot(&mut self, e: u64, sl: u64) {
-        info!(target: LOG_T, "[new slot] e:{}, rel sl:{}", e, sl);
+        info!(target: LOG_T, "[new slot] {}, e:{}, rel sl:{}", self, e, sl);
         let st: blake3::Hash = if e > 0 || (e == 0 && sl > 0) {
             self.workspace.block.blockhash()
         } else {