Skip to content

Instantly share code, notes, and snippets.

@christianb93
Created April 3, 2018 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianb93/a1ae4e1f5251879b87f08af4ae2951fe to your computer and use it in GitHub Desktop.
Save christianb93/a1ae4e1f5251879b87f08af4ae2951fe to your computer and use it in GitHub Desktop.
Determine the initial difficulty for a bitcoind in regtest mode - once using the RPC call getinfo and once from the genesis block
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#\n",
"# The bits hardcoded in the genesis block - see the call to CreateGenesisBlock in chainparams.cpp\n",
"#\n",
"nBits = int('0x207fffff', 16)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"545259519"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nBits"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.6565423739069247e-10"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"difficulty = 256**(29 - (nBits >> 24))*(65535.0 / ((float)(nBits & 0xFFFFFF)))\n",
"difficulty"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.656542373906925e-10\n"
]
}
],
"source": [
"#\n",
"# Now do an RPC call to the local regtest server and see what we get. This assumes that you have a\n",
"# bitcoind listening on port 18332 in regtest mode\n",
"#\n",
"import requests\n",
"headers = {'content-type': 'application/json'}\n",
"url = \"http://localhost:18332\"\n",
"payload = {\"method\": \"getinfo\", \"params\": [], \"jsonrpc\": \"2.0\", \"id\": 1} \n",
"r = requests.post(url, json=payload, headers=headers, auth=(\"user\", \"password\"))\n",
"json = r.json()['result']\n",
"print(json['difficulty'])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment