Bitcoin Forum
July 05, 2022, 12:42:54 PM *
News: Latest Bitcoin Core release: 23.0 [Torrent]
  Home Help Search Login Register More  
  Show Posts
Pages: [1]
1  Other / Beginners & Help / Last Block Hash on: September 12, 2013, 10:50:28 PM
Recently I have started a deep dive into the Bitcoin Armory code base, and writing my own scripts to do some of the basics.

I often come here to get questions answered, so here is one script I created as a small token of my appreciation... and to get off the newbies list.

If you want to know if your block files are up to date just run the script below and compare the hash to

It uses a few things from the Bitcoin Armory so get the latest source and drop this into the same directory as

import sys
from armoryengine import BTC_HOME_DIR, binary_to_int, LITTLEENDIAN, sha256, \
   binary_to_hex, BIGENDIAN, ARMORY_HOME_DIR
import hashlib
import os


def getLastBlockFile():
   # the current last block file number
   i = 80
   blkFilePath = os.path.join(BTC_HOME_DIR, 'blocks', 'blk%05d.dat' % i)
   lastFile = None
   while True:
         with open(blkFilePath, 'rb') as f:
            lastFile = blkFilePath
      except IOError:
      i += 1
      blkFilePath = os.path.join(BTC_HOME_DIR, 'blocks', 'blk%05d.dat' % i)
   return lastFile

def getFileSize(f):
   pos = f.tell()
   # Go to the end to get the file length,2)
   result = f.tell()
   return result

def getNextBlockHash(f):
   fileOffset = f.tell(), 1)
   blkSize = binary_to_int(, LITTLEENDIAN)
   result = None
   if blkSize > 0:
      blkString =
      blkHdrBinary = blkString[:HEADER_LENGTH]
      result = sha256(sha256(blkHdrBinary))
   return result

def getLastBlockHash(blkFile):
   result = None
   with open(blkFile, 'rb') as f:
      fSize = getFileSize(f)
      while f.tell() < fSize:
         blockHash = getNextBlockHash(f)
         if blockHash != None:
            result = blockHash
   return result

lastHashPath = os.path.join(ARMORY_HOME_DIR, 'LastBlockHash.txt')
result = binary_to_hex(getLastBlockHash(getLastBlockFile()), BIGENDIAN)
with open(lastHashPath, 'w') as f:
print result

Thanks for all of the help I've received so far.
Pages: [1]
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!