Browse Source

[research/lotterysim] randomize staked tokens

police 3 years ago
parent
commit
b756277359

+ 18 - 7
script/research/lotterysim/darkie.py

@@ -2,7 +2,7 @@ from utils import *
 from threading import Thread
 
 class Darkie(Thread):
-    def __init__(self, airdrop, vesting=[], hp=False):
+    def __init__(self, airdrop, vesting=[], hp=False, commit=True, epoch_len=100):
         Thread.__init__(self)
         self.vesting = [0] + vesting
         self.stake = (Num(airdrop) if hp else airdrop)
@@ -11,14 +11,25 @@ class Darkie(Thread):
         self.feedback = None
         self.f = None
         self.won=False
+        self.commit = commit # commit to staked tokens
+        self.epoch_len=epoch_len # epoch length during which the stake is static
+        self.staked_tokens_ratio = 1 # ratio of staked tokens, if commit is true then it's 100%
 
     def clone(self):
         return Darkie(self.finalized_stake)
 
-    def set_sigma_feedback(self, sigma, feedback, f, hp=False):
+    def set_sigma_feedback(self, sigma, feedback, f, count, hp=False):
         self.Sigma = (Num(sigma) if hp else sigma)
         self.feedback = (Num(feedback) if hp else feedback)
         self.f = (Num(f) if hp else f)
+        self.slot = count
+
+    def randomized_finalized_stake(self):
+        if self.commit:
+            return self.finalized_stake
+        if self.slot%self.epoch_len==0:
+            self.staked_tokens_ratio = random.random()
+        return self.staked_tokens_ratio*self.finalized_stake
 
     def run(self, hp=False):
         k=N_TERM
@@ -28,14 +39,14 @@ class Darkie(Thread):
             sigmas = [   c/((self.Sigma+EPSILON)**i) * ( ((L_HP if hp else L)/fact(i)) ) for i in range(1, k+1) ]
             scaled_target = approx_target_in_zk(sigmas, stake) #+ (BASE_L_HP if hp else BASE_L)
             return scaled_target
-        T = target(self.f, self.finalized_stake)
+        T = target(self.f, self.randomized_finalized_stake())
         self.won = lottery(T, hp)
 
-    def update_vesting(self, slot):
-        if slot >= len(self.vesting):
+    def update_vesting(self):
+        if self.slot >= len(self.vesting):
             return 0
-        slot2vest_index = int(slot/28800.0)
-        slot2vest_prev_index = int((slot-1)/28800.0)
+        slot2vest_index = int(self.slot/28800.0)
+        slot2vest_prev_index = int((self.slot-1)/28800.0)
         slot2vest_index_shifted = slot2vest_index - 1 # by end of month
         slot2vest_prev_index_shifted = slot2vest_prev_index - 1 # by end of month
         vesting_value = float(self.vesting[slot2vest_index_shifted]) - self.vesting[slot2vest_prev_index_shifted]

File diff suppressed because it is too large
+ 0 - 0
script/research/lotterysim/f.hist


+ 2 - 1
script/research/lotterysim/instance.py

@@ -8,7 +8,8 @@ RUNNING_TIME = int(input("running time:"))
 
 if __name__ == "__main__":
     darkies = []
-    darkies += [ Darkie(random.gauss(20,20)*50) for id in range(1000000) ]
+    #darkies += [ Darkie(abs(random.gauss(20,20))*50, commit=False) for id in range(1000) ]
+    darkies += [ Darkie(1, commit=False) for id in range(1000) ]
     airdrop = 0
     for darkie in darkies:
         airdrop+=darkie.stake

File diff suppressed because it is too large
+ 0 - 0
script/research/lotterysim/leads.hist


+ 2 - 2
script/research/lotterysim/lottery.py

@@ -37,9 +37,9 @@ class DarkfiTable:
             f = self.pid.pid_clipped(feedback, self.controller_type, debug)
             #note! thread overhead is 10X slower than sequential node execution!
             for i in range(len(self.darkies)):
-                self.darkies[i].set_sigma_feedback(self.Sigma, feedback, f, hp)
+                self.darkies[i].set_sigma_feedback(self.Sigma, feedback, f, count, hp)
                 self.darkies[i].run(hp)
-                total_vesting_stake+=self.darkies[i].update_vesting(count)
+                total_vesting_stake+=self.darkies[i].update_vesting()
             self.Sigma+=total_vesting_stake
             for i in range(len(self.darkies)):
                 winners += self.darkies[i].won

+ 37 - 1
script/research/lotterysim/playground.ipynb

@@ -218,6 +218,42 @@
     "draw()"
    ]
   },
+  {
+   "cell_type": "markdown",
+   "id": "3aa2e8fc",
+   "metadata": {},
+   "source": [
+    "# randomize token in stake"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "01b062b3",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def vesting_instance(kp, ki, kd, initial_distribution):\n",
+    "    os.system(\"rm f.hist; rm leads.hist\")\n",
+    "    RUNNING_TIME = len(next(iter(vesting.values())))*28800\n",
+    "\n",
+    "    if __name__ == \"__main__\":\n",
+    "        darkies = []\n",
+    "        id = 0\n",
+    "        for name, distrib in vesting.items():\n",
+    "            darkies += [Darkie(initial_distribution[id], vesting=distrib)]\n",
+    "            id+=1\n",
+    "        airdrop = 0\n",
+    "        for darkie in darkies:\n",
+    "            airdrop+=darkie.stake\n",
+    "        print(\"network airdrop: {} on {} nodes\".format(airdrop, len(darkies)))\n",
+    "        dt = DarkfiTable(airdrop, RUNNING_TIME)\n",
+    "        for darkie in darkies:\n",
+    "            dt.add_darkie(darkie)\n",
+    "        dt.background(rand_running_time=False)\n",
+    "        dt.write()"
+   ]
+  },
   {
    "cell_type": "markdown",
    "id": "571219cb",
@@ -243,7 +279,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.10.9"
+   "version": "3.10.6"
   }
  },
  "nbformat": 4,

+ 1 - 1
script/research/lotterysim/utils.py

@@ -24,7 +24,7 @@ def approx_target_in_zk(sigmas, stake):
     # this dictates that tuning need to be hardcoded,
     # secondly the reward, or at least the total stake in the network,
     # can't be anonymous, should be public.
-    T = [sigma*stake**(i+1) for i, sigma in enumerate(sigmas)]
+    T = [sigma*(stake+1)**(i+1) for i, sigma in enumerate(sigmas)]
     return -1*sum(T)
 
 def rnd(hp=False):

Some files were not shown because too many files changed in this diff