Monday, 26 November 2018

Web simulator for my anti-reorg algorithm

I made a simple web front-end for my Toomim Time anti-reorg algorithm simulator. You can mess with it here:

http://ml.toom.im:8050/

It all runs server-side, so if too many people are trying to use it at any given time, it will get slow and buggy. If that happens, you can run a copy locally.

This algorithm penalizes chains heavily if the first few blocks after the fork were released with a delay after the honest chain, and penalizes the chain lightly if blocks long after the fork were delayed relative to the other chain.

If the defender chain is shorter than the other, we need to calculate what penalties the defender chain would receive from the blocks that the defender would need to generate in order to catch up. To do this, we add "hypothetical blocks" with the current time. These hypothetical blocks are ignored for calculating the PoW done, but they do contribute to the penalty. The increase in the penalty for the shorter chain over time is the main mechanism by which the algorithm eventually converges to the most-PoW chain.

Some simplified pseudocode for how the scores are calculated:

root = last_common_ancestor(chaintip, enemytip) blk = chaintip while blk.pow < opponent.pow: # need to add hypothetical blocks blk = Block(parent=blk, firstseen=opponent.firstseen, tag='-TBD') chain_with_hypotheticals = list_children_to_root(blk, root) for blk in chain_with_hypotheticals: blk.delay = max(0, block.time_received - cousin(block).time_received) blk.penalty = blk.delay / timeconstant / (blk.height - root.height) ** exponent chain_penalty = sum([block.penalty for block in chain_with_hypotheticals]) score = (chaintip.pow - root.pow) / chainpenalty 

Finalization is optional with this algorithm. The sim has finalization disabled by default, but you can put e.g. 10 into the finalization field if you want to see how that affects things. Finalization will only happen if (a) the defender chain is at least n blocks ahead of the attacker chain, and (b) the defender chain has at least 2x the score of the attacker chain at that time.

The attacker wins if the aqua "Defender hypothetical score" line crosses or touches the attacker's score line.

The default for the simulation is to emulate an attacker that has 2x as much hashrate as the defense (i.e. can mine blocks every 300 seconds on average), and waits 1 hour before publishing any of the blocks they have mined. This is intended to emulate the scenario in which a malicious actor tries to double-spend an exchange with a 67% hashrate majority. The attacker will give up if he doesn't win within 50 hours. With these settings, the attacker's chain is adopted in about 39.7% of attempts, and most of those attacker victories are fast. The defenders only suffer >=10 block reorgs in 3.3% of all scenarios, and 79.9% of the attacker's blocks are orphaned versus 18.5% of the defender's.

The attacker can do much better against this algorithm if they only wait 20 minutes before publishing their blocks, but if they do that, the exchanges and businesses that they're trying to double-spend can temporarily suspend deposits and prevent the guerrilla attack from being effective at defrauding the exchanges. Consequently, I decided to default to a 60 minute delay instead. Choosing a 20 minute delay with a 1.5 attacker hashrate also provides some interesting scenarios.

The full code for the algorithm is here. To install and run locally, use the following commands:

sudo apt-get install pypy # if you can't get pypy to work on your platform, regular python works too wget https://bootstrap.pypa.io/get-pip.py sudo pypy get-pip.py rm get-pip.py sudo pip install dash sudo pip install dash-html-components sudo pip install dash-core-components sudo pip install dash-table git clone https://github.com/jtoomim/antiReorgSim.git cd antiReorgSim pypy dashreorgsim.py 

You can then view your local instance by pointing a web browser to localhost:8050.

If you want to test what happens when 10,000 runs with the same configuration are done, edit the params in reorgsim.py and then run pypy reorgsim.py. This may take a while to execute.

submitted by /u/jtoomim
[link] [comments]

source https://www.reddit.com/r/btc/comments/a0fr5j/web_simulator_for_my_antireorg_algorithm/

No comments:

Post a Comment