Guest User

blocktimes

a guest
Dec 8th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
  2. import time
  3. from decimal import *
  4. import json
  5. import pprint
  6. import plotly.plotly as py
  7. from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
  8. from plotly.graph_objs import *
  9.  
  10. pp = pprint.PrettyPrinter(indent=2)
  11.  
  12. class Node:
  13.     def __init__(self, uri):
  14.         self.conn = AuthServiceProxy(uri);
  15.  
  16. class Chain:
  17.     def __init__(self, short, name, node):
  18.         self.short = short
  19.         self.name = name
  20.         self.node = node
  21.  
  22.         self.best_block_hash = self.node.conn.getbestblockhash()
  23.         self.best_block = self.node.conn.getblockheader(self.best_block_hash)
  24.         self.height = self.best_block['height']
  25.  
  26.     def __str__(self):
  27.         return "Chain %s (%s): @%i" % (self.short, self.name, self.height)
  28.    
  29.     def getblockheader(self, h):
  30.         block_hash = self.node.conn.getblockhash(h)
  31.         block_header = self.node.conn.getblockheader(block_hash)
  32.         return block_header
  33.    
  34.     def blocktime_histogram(self, delta_h, hist_size=60, hist_seconds_per_bin=60, height=-1):
  35.         if height < 0:
  36.             height = self.height
  37.  
  38.         hist = [0 for i in range(hist_size)]
  39.        
  40.         block = self.getblockheader(self.height - delta_h )
  41.         for h in range( height - delta_h + 1, height ):
  42.             old_block = block
  43.             block = self.getblockheader( h )
  44.             block_time = block['time'] - old_block['time']
  45.             hist_index = block_time // hist_seconds_per_bin;
  46.             hist_index = max(min(hist_size-1, hist_index), 0)
  47.             #print ("h=%u #%u: block_time: %u hist_index: %i" % ( h, block['height'], block_time, hist_index ))
  48.             hist[hist_index] += 1
  49.            
  50.         fig = Figure(
  51.             data = [
  52.                 Bar(
  53.                     x = [(i*hist_seconds_per_bin/60) for i in range(hist_size)],#.append("%i+" % (hist_size*hist_seconds_per_bin/60),
  54.                     y = hist
  55.                 )
  56.             ],
  57.             layout = Layout(
  58.                 title = "%s block time histogram (blocks #%i - #%i, %i blocks)" % ( self.name, height - delta_h, height, delta_h ),
  59.                 xaxis = dict(title='block time (in minutes, based on block header times)'),
  60.                 yaxis = dict(title='number of blocks')
  61.             )
  62.         )
  63.         return fig
  64.  
  65.  
  66. xbc = Chain( 'BCC', 'BitcoinCash', Node('http://joebitcoin:<password>@localhost:8332'))
  67. xbt = Chain( 'BSW', 'BitcoinSegWit', Node('http://joebitcoin:<password>@localhost:8342'))
  68.  
  69. delta_h = 2016
  70. hist_size = 60
  71. hist_seconds_per_bin = 60
  72.  
  73. init_notebook_mode(connected=True)        
  74. iplot(xbc.blocktime_histogram( delta_h, hist_size, hist_seconds_per_bin ))
  75. iplot(xbt.blocktime_histogram( delta_h, hist_size, hist_seconds_per_bin ))
Advertisement
Add Comment
Please, Sign In to add comment