{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "deddf281",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bitcoin_explorer as bex\n",
    "from tqdm.notebook import tqdm\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "edea286c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[14:04:30] INFO - Start loading block_index\n",
      "[14:04:32] INFO - Longest chain: 701178\n",
      "[14:04:35] INFO - Successfully opened tx_index DB!\n"
     ]
    }
   ],
   "source": [
    "db = bex.BitcoinDB(\"/Volumes/Extreme SSD/bitcoin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "06157a17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'txid': '97d1b00fcef1f19531a19bb1722635341a9f2ad261ecf6eed89eca2cbd3bb3ee',\n",
       " 'input': [{'txid': 'ac348c1a49389c4408ac8541b79dba83a25a817f199c0a5474fc647494cb07e5',\n",
       "   'vout': 0}],\n",
       " 'output': [{'value': 2020000,\n",
       "   'script_type': 'Pay2PublicKeyHash',\n",
       "   'addresses': ['1JwhvD6mwpRzAhVKihWcxXPJNeFKJGgHUh']},\n",
       "  {'value': 300000,\n",
       "   'script_type': 'Pay2PublicKeyHash',\n",
       "   'addresses': ['13vCrWerFRP2rULtNUpV5bTipDfubVtm7U']}]}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_transaction(\"97d1b00fcef1f19531a19bb1722635341a9f2ad261ecf6eed89eca2cbd3bb3ee\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5b51a065",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'txid': '97d1b00fcef1f19531a19bb1722635341a9f2ad261ecf6eed89eca2cbd3bb3ee',\n",
       " 'input': [{'value': 2323200,\n",
       "   'script_type': 'Pay2ScriptHash',\n",
       "   'addresses': ['3GA77qTGUyPRT17j6UdBPdZ8uLr2rsNUzU']}],\n",
       " 'output': [{'value': 2020000,\n",
       "   'script_type': 'Pay2PublicKeyHash',\n",
       "   'addresses': ['1JwhvD6mwpRzAhVKihWcxXPJNeFKJGgHUh']},\n",
       "  {'value': 300000,\n",
       "   'script_type': 'Pay2PublicKeyHash',\n",
       "   'addresses': ['13vCrWerFRP2rULtNUpV5bTipDfubVtm7U']}]}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_transaction(\"97d1b00fcef1f19531a19bb1722635341a9f2ad261ecf6eed89eca2cbd3bb3ee\", connected=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0faafc56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'value': 2323200,\n",
       " 'script_type': 'Pay2ScriptHash',\n",
       " 'addresses': ['3GA77qTGUyPRT17j6UdBPdZ8uLr2rsNUzU']}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_transaction(\"ac348c1a49389c4408ac8541b79dba83a25a817f199c0a5474fc647494cb07e5\")[\"output\"][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3f284d89",
   "metadata": {},
   "outputs": [],
   "source": [
    "big_blk = db.get_block(700000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5b3b710d",
   "metadata": {},
   "outputs": [],
   "source": [
    "big_blk_connected = db.get_block(100000, connected=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "11612390",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(big_blk_connected[\"txdata\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7deb3c21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "701178"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_max_height()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "14df2f2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'block_hash': '0000000000000000000590fc0f3eba193a278534220b2b37e9849e1a770ca959',\n",
       " 'n_version': 210100,\n",
       " 'n_height': 700000,\n",
       " 'n_status': 157,\n",
       " 'n_tx': 1276,\n",
       " 'n_file': 2721,\n",
       " 'n_data_pos': 28521169,\n",
       " 'n_undo_pos': 4290720,\n",
       " 'block_header': {'version': 1073733636,\n",
       "  'prev_blockhash': '0000000000000000000aa3ce000eb559f4143be419108134e0ce71042fc636eb',\n",
       "  'merkle_root': '1f8d213c864bfe9fb0098cecc3165cce407de88413741b0300d56ea0f4ec9c65',\n",
       "  'time': 1631333672,\n",
       "  'bits': 386877668,\n",
       "  'nonce': 2881644503}}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_block_header(700000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8723a97e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0000000099c744455f58e6c6e98b671e1bf7f37346bfd4cf5d0274ad8ee660cb'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_hash_from_height(10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b435aa89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_height_from_hash(\"0000000099c744455f58e6c6e98b671e1bf7f37346bfd4cf5d0274ad8ee660cb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cec3043a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f6ae6c428fdb4098b89f259576e61d7a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/700000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i in tqdm(range(700000)):\n",
    "    db.get_block_header(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "333f60ab",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7366ab292f7b4d22ac8f12c69f4ea160",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/700000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i in tqdm(range(700000)):\n",
    "    db.get_hash_from_height(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "96550388",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'addresses': ['1JoiKZz2QRd47ARtcYgvgxC9jhnre9aphv'],\n",
       " 'pattern': 'Pay2PublicKey'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.parse_script(\"41042f462d3245d2f3a015f7f9505f763ee1080cab36191d07ae9e6509f71bb68818719e6fb41c019bf48ae11c45b024d476e19b6963103ce8647fc15fee513b15c7ac\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "320673d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_speed = list(range(700000))\n",
    "random.shuffle(test_speed)\n",
    "test_speed = test_speed[:1000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b4bee151",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dcb63728b32642d8866adbbed7a74da0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for h in tqdm(test_speed, smoothing=0):\n",
    "    db.get_block(h)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "498fe3d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1ebe86b245054e1a8b12fcae047eeeb5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/1000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for b in tqdm(db.get_block_iter_array(test_speed), total=1000):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f8a5582d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1beed67c16c04800acefca3ef6c75358",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/10000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for b in tqdm(db.get_block_iter_range(stop=10000), total=10000):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1da4e1b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1be24fd7849c42838443c0a90ff0019f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/10000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for b in tqdm(db.get_block_iter_range(stop=10000, connected=True), total=10000):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34b8fce1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}