research/multi_uncle_ghost.py

157 lines
4.8 KiB
Python
Raw Normal View History

2014-07-08 08:05:52 +00:00
# Time between successful PoW solutions
POW_SOLUTION_TIME = 10
2014-07-08 06:51:02 +00:00
# Time for a block to traverse the network
TRANSIT_TIME = 50
# Number of required uncles
UNCLES = 4
# Uncle block reward (normal block reward = 1)
UNCLE_REWARD_COEFF = 0.875
2014-07-08 08:05:52 +00:00
# Reward for including uncles
NEPHEW_REWARD_COEFF = 0.01
# Rounds to test
ROUNDS = 80000
2014-07-08 06:51:02 +00:00
import random
import copy
class Miner():
def __init__(self, p):
self.hashpower = p
2014-07-08 08:05:52 +00:00
self.id = random.randrange(10000000)
2014-07-08 06:51:02 +00:00
# Set up a few genesis blocks (since the algo is grandpa-dependent,
# we need two genesis blocks plus some genesis uncles)
self.blocks = {
0: {"parent": -1, "uncles": [], "miner": -1, "height": 0,
"score": 0, "id": 0, "children": {1: 1}},
1: {"parent": 0, "uncles": [], "miner": -1, "height": 1,
"score": 0, "id": 1, "children": {}}
}
2014-07-08 08:05:52 +00:00
# ID of "latest block"
self.head = 1
2014-07-08 06:51:02 +00:00
# Hear about a block
def recv(self, block):
# Add the block to the set if it's valid
addme = True
if block["id"] in self.blocks:
addme = False
if block["parent"] not in self.blocks:
addme = False
for u in block["uncles"]:
2014-07-08 08:05:52 +00:00
if u not in self.blocks:
2014-07-08 06:51:02 +00:00
addme = False
p = self.blocks[block["parent"]]
if addme:
self.blocks[block["id"]] = copy.deepcopy(block)
2014-07-08 08:05:52 +00:00
# Each parent keeps track of its children, to help
# facilitate the rule that a block must have N+ siblings
# to be valid
2014-07-08 06:51:02 +00:00
if block["id"] not in p["children"]:
p["children"][block["id"]] = block["id"]
2014-07-08 08:05:52 +00:00
# Check if the new block deserves to be the new head
2014-07-08 06:51:02 +00:00
if len(p["children"]) >= 1 + UNCLES:
2014-07-08 17:50:11 +00:00
for c in p["children"]:
newblock = self.blocks[c]
if newblock["score"] > self.blocks[self.head]["score"]:
self.head = newblock["id"]
2014-07-08 06:51:02 +00:00
# Mine a block
def mine(self):
2014-07-08 08:05:52 +00:00
h = self.blocks[self.blocks[self.head]["parent"]]
b = sorted(list(h["children"]), key=lambda x: -self.blocks[x]["score"])
2014-07-08 06:51:02 +00:00
p = self.blocks[b[0]]
block = {"parent": b[0], "uncles": b[1:], "miner": self.id,
"height": h["height"] + 2, "score": p["score"] + len(b),
2014-07-08 08:05:52 +00:00
"id": random.randrange(1000000000000), "children": {}}
2014-07-08 06:51:02 +00:00
self.recv(block)
return block
2014-07-08 08:05:52 +00:00
def cousin_degree(miner, b1, b2):
while miner.blocks[b1]["height"] > miner.blocks[b2]["height"]:
b1 = miner.blocks[b1]["parent"]
while miner.blocks[b2]["height"] > miner.blocks[b1]["height"]:
b2 = miner.blocks[b2]["parent"]
t = 0
while b1 != b2:
b1 = miner.blocks[b1]["parent"]
b2 = miner.blocks[b2]["parent"]
t += 1
return t
2014-07-08 06:51:02 +00:00
percentages = [1]*25 + [5, 5, 5, 5, 5, 10, 15, 25]
miners = []
for p in percentages:
miners.append(Miner(p))
miner_dict = {}
for m in miners:
miner_dict[m.id] = m
listen_queue = []
2014-07-08 08:05:52 +00:00
for t in range(ROUNDS):
2014-07-08 06:51:02 +00:00
if t % 5000 == 0:
print t
for m in miners:
2014-07-08 08:05:52 +00:00
R = random.randrange(POW_SOLUTION_TIME * sum(percentages))
if R < m.hashpower and t < ROUNDS - TRANSIT_TIME * 3:
2014-07-08 06:51:02 +00:00
b = m.mine()
listen_queue.append([t + TRANSIT_TIME, b])
while len(listen_queue) and listen_queue[0][0] <= t:
t, b = listen_queue.pop(0)
for m in miners:
m.recv(b)
h = miners[0].blocks[miners[0].head]
profit = {}
2014-07-08 08:05:52 +00:00
total_blocks_in_chain = 0
length_of_chain = 0
ZORO = {}
2014-07-08 06:51:02 +00:00
print "### PRINTING BLOCKCHAIN ###"
while h["id"] > 1:
print h["miner"], h["height"], h["score"]
2014-07-08 08:05:52 +00:00
total_blocks_in_chain += 1 + len(h["uncles"])
ZORO[h["id"]] = True
length_of_chain += 1
profit[h["miner"]] = profit.get(h["miner"], 0) + \
1 + NEPHEW_REWARD_COEFF * len(h["uncles"])
2014-07-08 06:51:02 +00:00
for u in h["uncles"]:
2014-07-08 08:05:52 +00:00
ZORO[u] = True
2014-07-08 06:51:02 +00:00
u2 = miners[0].blocks[u]
profit[u2["miner"]] = profit.get(u2["miner"], 0) + UNCLE_REWARD_COEFF
h = miners[0].blocks[h["parent"]]
2014-07-08 08:05:52 +00:00
print "### PRINTING HEADS ###"
for m in miners:
print m.head
2014-07-08 06:51:02 +00:00
print "### PRINTING PROFITS ###"
for p in profit:
print miner_dict[p].hashpower, profit[p]
2014-07-08 08:05:52 +00:00
print "### PRINTING RESULTS ###"
2014-07-08 06:51:02 +00:00
groupings = {}
counts = {}
for p in profit:
h = miner_dict[p].hashpower
counts[h] = counts.get(h, 0) + 1
groupings[h] = groupings.get(h, 0) + profit[p]
for c in counts:
print c, groupings[c] / counts[c] / (groupings[1] / counts[1])
2014-07-08 08:05:52 +00:00
print " "
print "Total blocks produced: ", len(miners[0].blocks) - 2
print "Total blocks in chain: ", total_blocks_in_chain
print "Efficiency: ", total_blocks_in_chain * 1.0 / (len(miners[0].blocks) - 2)
print "Average uncles: ", total_blocks_in_chain * 1.0 / length_of_chain
print "Length of chain: ", length_of_chain
print "Block time: ", ROUNDS * 1.0 / length_of_chain