research/casper/voting_strategy.py

32 lines
1.1 KiB
Python
Raw Normal View History

2015-08-27 13:38:48 +00:00
import random
2015-07-19 19:01:20 +00:00
2015-08-27 13:38:48 +00:00
# The voting strategy. Validators see what every other validator votes,
# and return their vote.
#
# Votes are log odds, ie. ln(p / (1-p))
#
# Remember, 0 and 1 are not probabilities!
2015-08-27 13:38:48 +00:00
# http://lesswrong.com/lw/mp/0_and_1_are_not_probabilities/ !)
2015-07-19 19:01:20 +00:00
def default_vote(scheduled_time, received_time, now, **kwargs):
if received_time is None:
time_delta = now - scheduled_time
my_opinion_prob = 1 if time_delta < kwargs["blktime"] * 4 else 4.0 / (4 + time_delta * 1.0 / kwargs["blktime"])
return 0 if random.random() < my_opinion_prob else -1
else:
time_delta = received_time * 0.9 + now * 0.1 - scheduled_time
my_opinion_prob = 1 if abs(time_delta) < kwargs["blktime"] * 4 else 4.0 / (4 + abs(time_delta) * 1.0 / kwargs["blktime"])
return 1 if random.random() < my_opinion_prob else -1
2015-08-27 13:38:48 +00:00
2015-07-19 19:01:20 +00:00
2015-08-27 13:38:48 +00:00
def vote(probs):
if len(probs) == 0:
return 0
2015-08-27 13:38:48 +00:00
probs = sorted(probs)
if probs[len(probs)/3] >= 1:
return probs[len(probs)/3] + 1
elif probs[len(probs)*2/3] <= -1:
return probs[len(probs)*2/3] - 1
else:
return probs[len(probs)/2]