def createPyBlock(prevBlkHeader, txlist, useMinDiff=True): print 'Creating block (%d tx): Computing nonce...' % len(txlist) extraNonce = random.randrange(2**32) txlist[0].inputs[0].binScript = int_to_binary(extraNonce, widthBytes=4) aGoodNonce = False numTries = 0 newbh = CppBlockHeader() # Keep searching for a good nonce 'til we find one. See # http://bitcoin.stackexchange.com/questions/5048/what-is-the-extranonce for # more info on why we mod the timestamp instead of the coinbase script # (extraNonce). nonceVal = -1 while not aGoodNonce: blk = PyBlock(prevBlkHeader, txlist) blk.blockHeader.timestamp += numTries # Set up the difficulty-related vars. Assume min diff (0x1d00ffff) unless # indicated otherwise, then use values for next-highest diff (0x1d00fffe). diffHex = 'FFFF0000000000000000000000000000000000000000000000000000h' if not useMinDiff: blk.blockHeader.diffBits = hex_to_binary('1d00fffe', BIGENDIAN) diffHex = 'FFFE0000000000000000000000000000000000000000000000000000h' newbh = CppBlockHeader() newbh.unserialize_1_(blk.blockHeader.serialize()) if fakeBlocks: nonceVal = 1414 aGoodNonce = True else: # C++ returns -1 if a nonce isn't found. nonceVal = newbh.findNonce(diffHex) if nonceVal != -1: aGoodNonce = True numTries += 1 blk.blockHeader.nonce = nonceVal blk.blockHeader.timestamp = newbh.getTimestamp() print 'Done!' return blk
def createPyBlock(prevBlkHeader, txlist): print 'Creating block (%d tx): Computing nonce...' % len(txlist), extraNonce = random.randrange(2**32) txlist[0].inputs[0].binScript = int_to_binary(extraNonce, widthBytes=4) aGoodNonce = 0 numTries = 0 # Instead of changing the txin script as a nonce, we slightly modify # the timestamp newbh = CppBlockHeader() while aGoodNonce == 0: blk = PyBlock(prevBlkHeader, txlist) newbh = CppBlockHeader() newbh.unserialize_1_(blk.blockHeader.serialize()) tstamp = newbh.getTimestamp() newbh.setTimestamp(tstamp+numTries) aGoodNonce = newbh.findNonce() numTries += 1 blk.blockHeader.nonce = aGoodNonce blk.blockHeader.timestamp = newbh.getTimestamp() print 'Done!' return blk