Python testing attack probability with 5000 masternodes


The Byz variable occurs twice you can adjust it to different values. The first calculation is involving control of a ChainLock quorum with 5000 total masternodes. The second calculation is withholding a ChainLock.

thresholdwith5000dekayear.py (Source)

#!/usr/bin/python
#This script was written by Darren Tapp and optimized by thephez

from decimal import Decimal
from math import log
from math import factorial as fac
from math import log1p
from math import exp



def binom(x, y):
    try:
        binom = fac(x) // fac(y) // fac(x - y)
    except ValueError:
        binom = 0
    return binom


###This function takes inputs and outputs the probability
#of sucess in one trial
#pcalc is short for probability calculation
def pcalc(masternodes,quorumsize,attacksuccess,Byznodes):
    SampleSpace = binom(masternodes,quorumsize)
    pctemp=0
    for x in range(attacksuccess, quorumsize+1):
        pctemp = pctemp + binom(Byznodes,x)*binom(masternodes-Byznodes,quorumsize-x)
    #at this junctiure the answer is pctemp/SampleSpace
    #but that will produce an overflow error.  We use logarithims to
    #calculate this value
    return 10 ** (log(pctemp,10)- log(SampleSpace,10))


def DekaYear(probability):
        trials = 2*365*10
        return 1-exp(trials * log1p(-probability))


#Control of Quorum

Byz = 2473


print pcalc(5000,400,240,Byz)

print DekaYear(pcalc(5000,400,240,Byz))

#Withold Chainlock

Byz = 1515

print pcalc(5000,400,161,Byz)

print DekaYear(pcalc(5000,400,161,Byz))