From 3f770f0ce78474bc81282d4136c6783666fb4c06 Mon Sep 17 00:00:00 2001 From: vub Date: Sun, 30 Oct 2016 20:52:12 -0400 Subject: [PATCH] Added casper selfish mining --- casper_sm/.test.py.swp | Bin 0 -> 12288 bytes casper_sm/test.py | 97 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 casper_sm/.test.py.swp create mode 100644 casper_sm/test.py diff --git a/casper_sm/.test.py.swp b/casper_sm/.test.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..19aed5347c26b487cb4540ff0308326a9050382c GIT binary patch literal 12288 zcmeHNOKclO7@n4AX-g5HBDnCk6{_>_de?1)6p~0Zq$Db70!g7#6j>X494}t)I=kzn zk$7CWw3Gt~kl=z4mEeTLfdg;=!HEl3D5zXG69g9!;+uVVch*kQR^m`;R{65_&dmS# z{%;;myrMrpGfR(aCmF8$82e)D(D`Sl53`X68C&nHh^M<==vQ29SWa`^tQF6fNAzt* zyn>FU7hJDd;z5IZywfUq+~=lOZIr5}-{xM$Z2U>e`8yo7eVn8u)vkV+$C(1@R zJ&zqdLJ!=3?q&_Caf$)OfMP%~pcqgLCA+QcS3H*avhc%MZ33^36J@s_S|+ z%P|AXb*OGu12@-IcA7Kuv}f_U<*ddd!%*-=5zEU*Q=)>A4~Y-pi7|kH`-EST!x#A& z11#rWP=MS*3M2-y28T$BsK>~nh?ipaLDr_Y1hnvS3J|HnQkFXisCEdxi~K32(?0kq-!HxBS~jDC=)CZu>k9vcFk`5&79El1$hLqZ{`1#1PRP8K00` zLDE7HQPa2_2KXaEqTAA4E>ic-&4VQ0E}-DIUeL#azlvocG%C-?qy zM6{-HZ)cDtMv^Z4`a?~f5z1RlEsH}@Cs#(aS@D`ei4`_?{TC6x5CHaW6Me;i8C^L-lS~xya z+VEL7z9yBVL}MLkX1mRuTA@jgQ8`+N>)7)ughL*ZUFC(z=_%}3Pfg7)PRILNQ_LPQ zzGBrv`$=SYzN9ZRVIXNw?UV-xWmD`JwL2@e5x7g{%D#8g_sDSI6)9e(rcJR z1;k<$7PjiP+C0EMQpVGo>l_bYW+6@sJ576(oK9#dfPrpH{f-I;(=%z{T;^ zwmUe#u9D-{I0B7`C0w~OTM!ML_t|^%t3HXzAFoD&1}n{9$Wyi}j<4n6P`9~;%mG4f zSqekRKNEXKq_UjP{?>(Cc&PC@moPSZAwiz_Y z^2va*BI#kcnb*a(zNn8gLl1r7^UC(VAag^+4ZShxa~2m-Y9~h+>w9Vz8pHv-dpHsA aS!PQSZ! if validator path is 1, 0, 1 then +# a colluding node will make the next block. +# randao_results[10011] = 0 -> if validator path is 1, 1, 0, 0 +# then a non-colluding node will make the next block. +randao_results = [None, None] +for i in range(2, 2**CHECK_DEPTH): + randao_results.append(1 if random.random() < attacker_share else 0) + +def update_randao(): + for i in range(2, len(randao_results) / 2): + randao_results[i] = randao_results[i * 2] + for i in range(len(randao_results) / 2, len(randao_results)): + randao_results[i] = 1 if random.random() < attacker_share else 0 + +# Strategy map: number of blocks belonging to colluding node in +# the 0, 0, 0... chain -> 0 = publish, 1 = wait and compete, 2 = don't +# publish + +for strat_id in range(2**CHECK_DEPTH): + strategy = [0] + k = strat_id + for _ in range(CHECK_DEPTH): + strategy.append(k % 2) + k /= 2 + + my_revenue = 0. + their_revenue = 0. + + print 'Testing strategy:', strategy + + time = 0 + while time < 200000: + child_0 = randao_results[2] + child_1 = randao_results[3] + situation = 0 + q = 2 + while situation < len(strategy) - 2 and randao_results[q] == 1: + situation += 1 + q *= 2 + if child_0 == 0: + their_revenue += 1 + time += BLOCK_TIME + elif strategy[situation] == 0: + my_revenue += 1 + time += BLOCK_TIME + elif strategy[situation] == 2: + their_revenue += 1 + time += BLOCK_TIME + SKIP_TIME + else: + assert situation >= 1 + my_score = situation + my_time = situation + 1 + their_time = 0 + their_score = 0 + update_randao() + while their_time <= situation: + if randao_results[2] == 0: + wait_time = BLOCK_TIME + elif randao_results[3] == 0: + wait_time = BLOCK_TIME + SKIP_TIME + else: + wait_time = BLOCK_TIME + SKIP_TIME * 2 + while random.random() < attacker_share: + wait_time += SKIP_TIME + their_score += 1 + their_time += wait_time + update_randao() + time += max(my_time, their_time) + while my_score > their_score + 1: + if random.random() < attacker_share: + my_score += 1 + if random.random() > attacker_share: + their_score += 1 + time += BLOCK_TIME + if my_score > their_score or (my_score == their_score and random.random() < 0.5): + my_revenue += my_score - FORK_PUNISHMENT_COEFF + their_revenue -= their_score * 0.5 + else: + their_revenue += their_score - FORK_PUNISHMENT_COEFF + my_revenue -= my_score * 0.5 + update_randao() + + if my_revenue * 1.0 / (my_revenue + their_revenue) > attacker_share - 0.01: + print 'My revenue:', my_revenue / time + print 'Their revenue:', their_revenue / time + print 'My share:', my_revenue / (my_revenue + their_revenue) + print 'Griefing factor:', (their_revenue / time / (1 - attacker_share) - 1) / (my_revenue / time / attacker_share - 1)