Bitcoin Greedy Miner


SUBMITTED BY: Guest

DATE: Nov. 10, 2013, 5:02 p.m.

FORMAT: Text only

SIZE: 4.0 kB

HITS: 778

  1. package bitcoinattackmodel;
  2. /**
  3. *
  4. * @author Kooooooj
  5. */
  6. public class BitcoinAttackModel {
  7. public static void main(String[] args) {
  8. int honestBlocks = 0, attackBlocks = 0;//counts profit of honest and attack
  9. int currentHonestChain = 0, currentAttackChain = 0;//number of blocks not yet accepted
  10. double attackPercent = .3333, honestPercent = 1-attackPercent;//relative percentages of the network
  11. double raceOdds = .0;//odds that an honest miner builds off of the attacker's block. Typical range 0 to .5. Can be as high as 1.
  12. double oddsPerStep = 0.00001;//small number to discourage simultaneous mining of blocks by attacker and honest. Will run faster with larger values but could be less accurate
  13. long maxSteps = 1000000000L;//exit condition. Program will exit eariler with smaller values but will use a smaller data set
  14. long currentStep = 0;//counter
  15. while(currentStep < maxSteps)
  16. {
  17. currentStep++;
  18. if(Math.random() < oddsPerStep * attackPercent) {//attackers found a block
  19. if(currentAttackChain>0 && currentAttackChain == currentHonestChain){
  20. //attackers found the block. They *always* build off of the attack chain
  21. attackBlocks+=currentAttackChain + 1;//they get the old chain plus the added block
  22. currentAttackChain = 0;
  23. currentHonestChain = 0;
  24. }
  25. else{
  26. currentAttackChain++;//only get to start a new chain if we didn't just resolve a race
  27. }
  28. // System.out.println("Attack found a block. Current Attack Chain of " + currentAttackChain + " blocks.");
  29. }
  30. if(Math.random() < oddsPerStep * honestPercent) {//honest found a block
  31. if(currentAttackChain>0 && currentAttackChain==currentHonestChain){//there's a race condition in place
  32. System.out.println("Race!!");
  33. if(Math.random() < raceOdds){//honest miners mined on top of the attack chain
  34. attackBlocks+=currentAttackChain;//accept the attack block
  35. }
  36. else{//honest miners mined on top of the honest chain; attackers lost the race
  37. honestBlocks += currentHonestChain;
  38. }
  39. currentAttackChain = 0;
  40. currentHonestChain = 0;
  41. }
  42. currentHonestChain++;//race (if any) has been resolved. Note the new block
  43. System.out.println("Honest found a block. Current Honest Chain of " + currentHonestChain + " blocks\t\tCurrent Stats: " + attackBlocks + " : " + honestBlocks + ", " + attackBlocks/((double)honestBlocks+attackBlocks));
  44. }
  45. if(currentHonestChain>0 && currentAttackChain ==0){
  46. honestBlocks+=currentHonestChain;
  47. currentHonestChain = 0;
  48. currentAttackChain = 0;
  49. // System.out.println("Honest block accepted. " + honestBlocks + " honest blocks found");
  50. }
  51. if(currentAttackChain > 1 && currentAttackChain - currentHonestChain == 1){
  52. attackBlocks += currentAttackChain;
  53. // System.out.println("Attacker orphaned " + currentHonestChain + " blocks");
  54. currentAttackChain = 0;
  55. currentHonestChain = 0;
  56. }
  57. }
  58. System.out.println("Attacker mined " + attackBlocks + " blocks while honest mined " + honestBlocks);
  59. System.out.println("Attacker's percentage is " + ((double)attackBlocks/(attackBlocks + honestBlocks)) + " while expected was " + attackPercent);
  60. }
  61. }

comments powered by Disqus