forked from ethereum/research
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vub
authored and
vub
committed
Sep 14, 2015
1 parent
533442d
commit a2433df
Showing
3 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import math | ||
BLKTIME = 17 | ||
X = 0.28 | ||
|
||
faclog = [1] | ||
for i in range(5000): | ||
faclog.append(faclog[-1] * len(faclog)) | ||
|
||
def fac(x): | ||
return faclog[x] | ||
|
||
def poisson(expected, actual): | ||
if expected == 0: | ||
return 1 if actual == 0 else 0 | ||
return 2.718281828 ** (-expected + actual * math.log(expected) - math.log(fac(actual))) | ||
|
||
def p_we_win(k, x): | ||
return 1 - (x / (1.0 - x)) ** k | ||
|
||
def p_we_win_after(s): | ||
p = 0 | ||
for i in range(4000): | ||
p += poisson(s * 1.0 / BLKTIME, i) * p_we_win(i, X) | ||
return p | ||
|
||
for i in range(0, 7200, 12): | ||
print i, p_we_win_after(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import random | ||
BLKTIME = 600 | ||
LATENCY = 10 | ||
TEST_MAX = 1200 | ||
TEST_INTERVAL = 6 | ||
|
||
class Block(): | ||
def __init__(self, parent, txstate): | ||
self.parent = parent | ||
self.score = 1 if parent is None else parent.score + 1 | ||
if parent is None or parent.txstate is None: | ||
self.txstate = txstate | ||
else: | ||
self.txstate = parent.txstate | ||
|
||
|
||
results = {} | ||
|
||
|
||
for double_spend_delay in range(0, TEST_MAX, TEST_INTERVAL): | ||
results[double_spend_delay] = 0 | ||
for _ in range(1000): | ||
a_head = None | ||
b_head = None | ||
recvqueue = {} | ||
for time_elapsed in range(5000): | ||
txstate = 1 if time_elapsed < double_spend_delay else 2 | ||
# Miner A mines and sends (has 50% network share) | ||
if random.random() * BLKTIME < 0.5: | ||
a_head = Block(a_head, txstate) | ||
if time_elapsed + LATENCY not in recvqueue: | ||
recvqueue[time_elapsed + LATENCY] = [] | ||
recvqueue[time_elapsed + LATENCY].append(a_head) | ||
# Miner B mines and sends (has 50% network share) | ||
if random.random() * BLKTIME < 0.5: | ||
b_head = Block(b_head, txstate) | ||
if time_elapsed + LATENCY not in recvqueue: | ||
recvqueue[time_elapsed + LATENCY] = [] | ||
recvqueue[time_elapsed + LATENCY].append(b_head) | ||
# Receive blocks | ||
if time_elapsed in recvqueue: | ||
for b in recvqueue[time_elapsed]: | ||
if not a_head or b.score > a_head.score or (b.score == a_head.score and random.random() < 0.5): | ||
a_head = b | ||
if not b_head or b.score > b_head.score or (b.score == b_head.score and random.random() < 0.5): | ||
b_head = b | ||
# Check which transaction "made it" | ||
if a_head and a_head.txstate == 1: | ||
results[double_spend_delay] += 0.001 | ||
print (double_spend_delay, results[double_spend_delay]) | ||
|
||
print(results) |