Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate locktime confidence across chains #35

Open
prestwich opened this issue Oct 16, 2017 · 5 comments
Open

Calculate locktime confidence across chains #35

prestwich opened this issue Oct 16, 2017 · 5 comments

Comments

@prestwich
Copy link
Contributor

Eventually we need reliable estimates of how often X Bitcoin blocks will occur before Y Zcash blocks, so that we can set safe locktimes on each chain. With the help of a statistician friend, I wrote a script to calculate that.

import operator as op
import math


# StackExchange ncr magic
def ncr(n, r):
    r = min(r, n - r)
    if r == 0:
        return 1
    numer = reduce(op.mul, xrange(n, n - r, -1))
    denom = reduce(op.mul, xrange(1, r + 1))
    return numer // denom


# Number of Bitcoin blocks for estimation
c1b = 100.0
# Bitcoin's rate (1 block per 600 seconds)
c1r = 1/600.0

# Number of Zcash blocks for estimation
c2b = 300.0
# Zcash's rate (1 block per 150 seconds)
c2r = 1/150.0

sum = 0.0

for k in range(int(c1b), int(c1b + c2b)):
    term_one = ncr(int(c1b + c2b - 1), int(k))
    term_two = math.pow(c1r / (c1r + c2r), k)
    term_three = math.pow(c2r / (c1r + c2r), c1b + c2b - 1 - k)

    sum += term_one * term_two * term_three

print sum
@leto
Copy link

leto commented Oct 16, 2017

I am seeing 0.00799453086688 as a result of this script on Python 2.7.13 .

To clarify, what are the units of the result?

@prestwich
Copy link
Contributor Author

The result is the probability that 100 bitcoin blocks elapse before 300 zcash blocks. In this case, 7.99%.

You can swap out c1b and c2b to calculate probability for different numbers of blocks on each chain.

For real-life applications, we should probably target something like 0.0000001 (99.99999% confidence that the locktimes will expire in the expected order).

@leto
Copy link

leto commented Oct 16, 2017

@frdwrd thanks, I appreciate the answer. This code will be useful for various coins doing XCAT, thanks.

@prestwich
Copy link
Contributor Author

Worth noting that this is less reliable if the coins have strongly correlated hashrates (like bitcoin cash and bitcoin).

@leto
Copy link

leto commented Oct 17, 2017

@frdwrd thanks for pointing that out. For myself, I would be interested in KMD<->HUSH and ZEC<->HUSH because we are performing XCATs between those coins with barterDEX. Perhaps a brave soul will improve this into a script that prints out all the useful data for lots of coins 😺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants