Guest User

Untitled

a guest
Jun 17th, 2017
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.37 KB | None | 0 0
  1. # R script to simulate chain length during a UASF chain split
  2.  
  3. # 0. Variables
  4. t <- 60 # time in minutes when to calculate the likelihood of a reorg
  5. n <- 1000 # number of times the simulation runs
  6. p <- 20 # minority UASF chain in percentage points
  7.  
  8.  
  9. # 1. Create vectors
  10. chaina <- 1:n
  11. chainb <- 1:n
  12.  
  13. # 2.1 Minority chain length simulation
  14. for (i in 1:length(chaina)) {
  15.  
  16.   observationa <- rexp(n,1/(1000/p)) # exponential function to calculate block arrive intervals
  17.   cumulativea <- 1:n # create vector
  18.  
  19.   for (j in 1:length(observationa)){
  20.     cumulativea[j] <- sum(observationa[1:j])
  21.   } # sum up block arrival intervals
  22.  
  23.   chaina[i] <- min(which(cumulativea[] > t))
  24. } # check how long the chain is at time t
  25.  
  26. # 2.2 Majority chain length simulation
  27. for (i in 1:length(chainb)) {
  28.  
  29.   observationb <- rexp(n,1/(1000/(100-p)))
  30.   cumulativeb <- 1:n
  31.  
  32.   for (j in 1:length(observationb)){
  33.     cumulativeb[j] <- sum(observationb[1:j])
  34.   }
  35.  
  36.   chainb[i] <- min(which(cumulativeb[] > t))
  37. }
  38.  
  39. # 3. Compare the two chains
  40. all <- data.frame(chaina, chainb)
  41. all$x <- ifelse(chaina > chainb, TRUE, FALSE)
  42. sum(all$x == TRUE)
  43.  
  44. # Notes:
  45. # The script only compares the chain lengths.
  46. # There is also the chance that a chain re-organized well before time t
  47. # Therefore this script systematically UNDERESTIMATES the chance of a re-org
  48.  
  49. # @LeoAW https://liongrass.hk
Advertisement
Add Comment
Please, Sign In to add comment