diff --git a/ChainDb.py b/ChainDb.py index bf3df00..af86fe7 100644 --- a/ChainDb.py +++ b/ChainDb.py @@ -14,11 +14,11 @@ import time from decimal import Decimal from Cache import Cache -from bitcoin.serialize import * +import bitcoin from bitcoin.core import * -from bitcoin.messages import msg_block, message_to_str, message_read -from bitcoin.coredefs import COIN -from bitcoin.scripteval import VerifySignature +from bitcoin.core.scripteval import VerifySignature +from bitcoin.core.serialize import * +from bitcoin.messages import MsgSerializable, msg_block @@ -33,7 +33,7 @@ def block_value(height, fees): return subsidy + fees class TxIdx(object): - def __init__(self, blkhash=0L, spentmask=0L): + def __init__(self, blkhash=b'\x00'*32, spentmask=0L): self.blkhash = blkhash self.spentmask = spentmask @@ -66,13 +66,12 @@ def deserialize(self, s): self.blocks = [] l = s.split() for hashstr in l: - hash = long(hashstr, 16) - self.blocks.append(hash) + self.blocks.append(lx(hashstr)) def serialize(self): l = [] for blkhash in self.blocks: - l.append(hex(blkhash)) + l.append(b2lx(blkhash)) return ' '.join(l) def __repr__(self): @@ -80,13 +79,12 @@ def __repr__(self): class ChainDb(object): - def __init__(self, settings, datadir, log, mempool, netmagic, + def __init__(self, settings, datadir, log, mempool, readonly=False, fast_dbm=False): self.settings = settings self.log = log self.mempool = mempool self.readonly = readonly - self.netmagic = netmagic self.fast_dbm = fast_dbm self.blk_cache = Cache(500) self.orphans = {} @@ -108,36 +106,36 @@ def __init__(self, settings, datadir, log, mempool, netmagic, self.log.write("INITIALIZING EMPTY BLOCKCHAIN DATABASE") batch = leveldb.WriteBatch() batch.Put('misc:height', str(-1)) - batch.Put('misc:msg_start', self.netmagic.msg_start) - batch.Put('misc:tophash', ser_uint256(0L)) + batch.Put('misc:msg_start', bitcoin.params.MESSAGE_START) + batch.Put('misc:tophash', b2lx(b'\x00'*32)) batch.Put('misc:total_work', hex(0L)) self.db.Write(batch) try: start = self.db.Get('misc:msg_start') - if start != self.netmagic.msg_start: raise KeyError + if start != bitcoin.params.MESSAGE_START: raise KeyError except KeyError: self.log.write("Database magic number mismatch. Data corruption or incorrect network?") raise RuntimeError def puttxidx(self, txhash, txidx, batch=None): - ser_txhash = ser_uint256(txhash) + ser_txhash = b2lx(txhash) try: self.db.Get('tx:'+ser_txhash) old_txidx = self.gettxidx(txhash) - self.log.write("WARNING: overwriting duplicate TX %064x, height %d, oldblk %064x, oldspent %x, newblk %064x" % (txhash, self.getheight(), old_txidx.blkhash, old_txidx.spentmask, txidx.blkhash)) + self.log.write("WARNING: overwriting duplicate TX %s, height %d, oldblk %s, oldspent %x, newblk %s" % (b2lx(txhash), self.getheight(), b2lx(old_txidx.blkhash), old_txidx.spentmask, b2lx(txidx.blkhash))) except KeyError: pass batch = self.db if batch is not None else batch - batch.Put('tx:'+ser_txhash, hex(txidx.blkhash) + ' ' + + batch.Put('tx:'+ser_txhash, b2lx(txidx.blkhash) + ' ' + hex(txidx.spentmask)) return True def gettxidx(self, txhash): - ser_txhash = ser_uint256(txhash) + ser_txhash = b2lx(txhash) try: ser_value = self.db.Get('tx:'+ser_txhash) except KeyError: @@ -146,7 +144,7 @@ def gettxidx(self, txhash): pos = string.find(ser_value, ' ') txidx = TxIdx() - txidx.blkhash = long(ser_value[:pos], 16) + txidx.blkhash = lx(ser_value[:pos]) txidx.spentmask = long(ser_value[pos+1:], 16) return txidx @@ -158,11 +156,10 @@ def gettx(self, txhash): block = self.getblock(txidx.blkhash) for tx in block.vtx: - tx.calc_sha256() - if tx.sha256 == txhash: + if tx.GetHash() == txhash: return tx - self.log.write("ERROR: Missing TX %064x in block %064x" % (txhash, txidx.blkhash)) + self.log.write("ERROR: Missing TX %s in block %s" % (b2lx(txhash), b2lx(txidx.blkhash))) return None def haveblock(self, blkhash, checkorphans): @@ -170,7 +167,7 @@ def haveblock(self, blkhash, checkorphans): return True if checkorphans and blkhash in self.orphans: return True - ser_hash = ser_uint256(blkhash) + ser_hash = b2lx(blkhash) try: self.db.Get('blocks:'+ser_hash) return True @@ -178,7 +175,7 @@ def haveblock(self, blkhash, checkorphans): return False def have_prevblock(self, block): - if self.getheight() < 0 and block.sha256 == self.netmagic.block0: + if self.getheight() < 0 and block.GetHash() == bitcoin.params.GENESIS_BLOCK.GetHash(): return True if self.haveblock(block.hashPrevBlock, False): return True @@ -189,14 +186,14 @@ def getblock(self, blkhash): if block is not None: return block - ser_hash = ser_uint256(blkhash) + ser_hash = b2lx(blkhash) try: # Lookup the block index, seek in the file fpos = long(self.db.Get('blocks:'+ser_hash)) self.blk_read.seek(fpos) # read and decode "block" msg - msg = message_read(self.netmagic, self.blk_read) + msg = MsgSerializable.stream_deserialize(self.blk_read) if msg is None: return None block = msg.block @@ -233,7 +230,7 @@ def unique_outpts(self, block): for tx in block.vtx: if tx.is_coinbase: continue - txmap[tx.sha256] = tx + txmap[tx.GetHash()] = tx for txin in tx.vin: v = (txin.prevout.hash, txin.prevout.n) if v in outs: @@ -295,8 +292,6 @@ def spent_outpts(self, block): return outpts.keys() def tx_signed(self, tx, block, check_mempool): - tx.calc_sha256() - for i in xrange(len(tx.vin)): txin = tx.vin[i] @@ -306,8 +301,7 @@ def tx_signed(self, tx, block, check_mempool): # search block for dependent TX if txfrom is None and block is not None: for blktx in block.vtx: - blktx.calc_sha256() - if blktx.sha256 == txin.prevout.hash: + if blktx.GetHash() == txin.prevout.hash: txfrom = blktx break @@ -316,25 +310,27 @@ def tx_signed(self, tx, block, check_mempool): try: txfrom = self.mempool.pool[txin.prevout.hash] except: - self.log.write("TX %064x/%d no-dep %064x" % - (tx.sha256, i, - txin.prevout.hash)) + self.log.write("TX %s/%d no-dep %s" % + (b2lx(tx.GetHash()), i, + b2lx(txin.prevout.hash))) return False if txfrom is None: - self.log.write("TX %064x/%d no-dep %064x" % - (tx.sha256, i, - txin.prevout.hash)) + self.log.write("TX %s/%d no-dep %s" % + (b2lx(tx.GetHash()), i, + b2lx(txin.prevout.hash))) return False - if not VerifySignature(txfrom, tx, i, 0): - self.log.write("TX %064x/%d sigfail" % - (tx.sha256, i)) + if not VerifySignature(txfrom, tx, i): + self.log.write("TX %s/%d sigfail" % + (b2lx(tx.GetHash()), i)) return False return True def tx_is_orphan(self, tx): - if not tx.is_valid(): + try: + CheckTransaction(tx) + except CheckTransactionError: return None for txin in tx.vin: @@ -352,34 +348,20 @@ def tx_is_orphan(self, tx): return False def connect_block(self, ser_hash, block, blkmeta): - # verify against checkpoint list - try: - chk_hash = self.netmagic.checkpoints[blkmeta.height] - if chk_hash != block.sha256: - self.log.write("Block %064x does not match checkpoint hash %064x, height %d" % ( - block.sha256, chk_hash, blkmeta.height)) - return False - except KeyError: - pass - # check TX connectivity outpts = self.spent_outpts(block) if outpts is None: - self.log.write("Unconnectable block %064x" % (block.sha256, )) + self.log.write("Unconnectable block %s" % (b2lx(block.GetHash()), )) return False # verify script signatures - if ('nosig' not in self.settings and - ('forcesig' in self.settings or - blkmeta.height > self.netmagic.checkpoint_max)): + if ('nosig' not in self.settings): for tx in block.vtx: - tx.calc_sha256() - if tx.is_coinbase(): continue if not self.tx_signed(tx, block, False): - self.log.write("Invalid signature in block %064x" % (block.sha256, )) + self.log.write("Invalid signature in block %s" % (b2lx(block.GetHash()), )) return False # update database pointers for best chain @@ -388,20 +370,18 @@ def connect_block(self, ser_hash, block, blkmeta): batch.Put('misc:height', str(blkmeta.height)) batch.Put('misc:tophash', ser_hash) - self.log.write("ChainDb: height %d, block %064x" % ( - blkmeta.height, block.sha256)) + self.log.write("ChainDb: height %d, block %s" % ( + blkmeta.height, b2lx(block.GetHash()))) # all TX's in block are connectable; index neverseen = 0 for tx in block.vtx: - tx.calc_sha256() - - if not self.mempool.remove(tx.sha256): + if not self.mempool.remove(tx.GetHash()): neverseen += 1 - txidx = TxIdx(block.sha256) - if not self.puttxidx(tx.sha256, txidx, batch): - self.log.write("TxIndex failed %064x" % (tx.sha256,)) + txidx = TxIdx(block.GetHash()) + if not self.puttxidx(tx.GetHash(), txidx, batch): + self.log.write("TxIndex failed %s" % (b2lx(tx.GetHash()),)) return False self.log.write("MemPool: blk.vtx.sz %d, neverseen %d, poolsz %d" % (len(block.vtx), neverseen, self.mempool.size())) @@ -414,7 +394,7 @@ def connect_block(self, ser_hash, block, blkmeta): return True def disconnect_block(self, block): - ser_prevhash = ser_uint256(block.hashPrevBlock) + ser_prevhash = b2lx(block.hashPrevBlock) prevmeta = BlkMeta() prevmeta.deserialize(self.db.Get('blkmeta:'+ser_prevhash)) @@ -431,8 +411,7 @@ def disconnect_block(self, block): # update tx index and memory pool for tx in block.vtx: - tx.calc_sha256() - ser_hash = ser_uint256(tx.sha256) + ser_hash = b2lx(tx.GetHash()) try: batch.Delete('tx:'+ser_hash) except KeyError: @@ -447,13 +426,22 @@ def disconnect_block(self, block): batch.Put('misc:tophash', ser_prevhash) self.db.Write(batch) - self.log.write("ChainDb(disconn): height %d, block %064x" % ( - prevmeta.height, block.hashPrevBlock)) + self.log.write("ChainDb(disconn): height %d, block %s" % ( + prevmeta.height, b2lx(block.hashPrevBlock))) return True + def getblockhash(self, height): + heightidx = HeightIdx() + heightstr = str(height) + try: + heightidx.deserialize(self.db.Get('height:'+heightstr)) + except KeyError: + pass + return heightidx.blocks[0] + def getblockmeta(self, blkhash): - ser_hash = ser_uint256(blkhash) + ser_hash = b2lx(blkhash) try: meta = BlkMeta() meta.deserialize(self.db.Get('blkmeta:'+ser_hash)) @@ -482,27 +470,25 @@ def reorganize(self, new_best_blkhash): while (self.getblockheight(longer) > self.getblockheight(fork)): block = self.getblock(longer) - block.calc_sha256() conn.append(block) longer = block.hashPrevBlock - if longer == 0: + if longer == b'\x00'*32: return False if fork == longer: break block = self.getblock(fork) - block.calc_sha256() disconn.append(block) fork = block.hashPrevBlock - if fork == 0: + if fork == b'\x00'*32: return False - self.log.write("REORG disconnecting top hash %064x" % (old_best_blkhash,)) - self.log.write("REORG connecting new top hash %064x" % (new_best_blkhash,)) - self.log.write("REORG chain union point %064x" % (fork,)) + self.log.write("REORG disconnecting top hash %s" % (b2lx(old_best_blkhash),)) + self.log.write("REORG connecting new top hash %s" % (b2lx(new_best_blkhash),)) + self.log.write("REORG chain union point %s" % (b2lx(fork),)) self.log.write("REORG disconnecting %d blocks, connecting %d blocks" % (len(disconn), len(conn))) for block in disconn: @@ -510,8 +496,8 @@ def reorganize(self, new_best_blkhash): return False for block in conn: - if not self.connect_block(ser_uint256(block.sha256), - block, self.getblockmeta(block.sha256)): + if not self.connect_block(b2lx(block.GetHash()), + block, self.getblockmeta(block.GetHash())): return False self.log.write("REORGANIZE DONE") @@ -524,19 +510,19 @@ def set_best_chain(self, ser_prevhash, ser_hash, block, blkmeta): return self.connect_block(ser_hash, block, blkmeta) # switching from current chain to another, stronger chain - return self.reorganize(block.sha256) + return self.reorganize(block.GetHash()) def putoneblock(self, block): - block.calc_sha256() - - if not block.is_valid(): - self.log.write("Invalid block %064x" % (block.sha256, )) + try: + CheckBlock(block) + except CheckBlockError: + self.log.write("Invalid block %s" % (b2lx(block.GetHash()), )) return False if not self.have_prevblock(block): - self.orphans[block.sha256] = True + self.orphans[block.GetHash()] = True self.orphan_deps[block.hashPrevBlock] = block - self.log.write("Orphan block %064x (%d orphans)" % (block.sha256, len(self.orphan_deps))) + self.log.write("Orphan block %s (%d orphans)" % (b2lx(block.GetHash()), len(self.orphan_deps))) return False top_height = self.getheight() @@ -545,7 +531,7 @@ def putoneblock(self, block): # read metadata for previous block prevmeta = BlkMeta() if top_height >= 0: - ser_prevhash = ser_uint256(block.hashPrevBlock) + ser_prevhash = b2lx(block.hashPrevBlock) prevmeta.deserialize(self.db.Get('blkmeta:'+ser_prevhash)) else: ser_prevhash = '' @@ -555,7 +541,7 @@ def putoneblock(self, block): # build network "block" msg, as canonical disk storage form msg = msg_block() msg.block = block - msg_data = message_to_str(self.netmagic, msg) + msg_data = msg.to_bytes() # write "block" msg to storage fpos = self.blk_write.tell() @@ -563,7 +549,7 @@ def putoneblock(self, block): self.blk_write.flush() # add index entry - ser_hash = ser_uint256(block.sha256) + ser_hash = b2lx(block.GetHash()) batch.Put('blocks:'+ser_hash, str(fpos)) # store metadata related to this block @@ -580,14 +566,14 @@ def putoneblock(self, block): heightidx.deserialize(self.db.Get('height:'+heightstr)) except KeyError: pass - heightidx.blocks.append(block.sha256) + heightidx.blocks.append(block.GetHash()) batch.Put('height:'+heightstr, heightidx.serialize()) self.db.Write(batch) # if chain is not best chain, proceed no further if (blkmeta.work <= top_work): - self.log.write("ChainDb: height %d (weak), block %064x" % (blkmeta.height, block.sha256)) + self.log.write("ChainDb: height %d (weak), block %s" % (blkmeta.height, b2lx(block.GetHash()))) return True # update global chain pointers @@ -598,41 +584,44 @@ def putoneblock(self, block): return True def putblock(self, block): - block.calc_sha256() - if self.haveblock(block.sha256, True): - self.log.write("Duplicate block %064x submitted" % (block.sha256, )) + if self.haveblock(block.GetHash(), True): + self.log.write("Duplicate block %s submitted" % (b2lx(block.GetHash()), )) return False if not self.putoneblock(block): return False - blkhash = block.sha256 + blkhash = block.GetHash() while blkhash in self.orphan_deps: block = self.orphan_deps[blkhash] if not self.putoneblock(block): return True del self.orphan_deps[blkhash] - del self.orphans[block.sha256] + del self.orphans[block.GetHash()] - blkhash = block.sha256 + blkhash = block.GetHash() return True def locate(self, locator): for hash in locator.vHave: - ser_hash = ser_uint256(hash) - if ser_hash in self.blkmeta: + ser_hash = b2lx(hash) + try: blkmeta = BlkMeta() blkmeta.deserialize(self.db.Get('blkmeta:'+ser_hash)) return blkmeta - return 0 + except KeyError: + pass + blkmeta = BlkMeta() + blkmeta.height = 0 + return blkmeta def getheight(self): return int(self.db.Get('misc:height')) def gettophash(self): - return uint256_from_str(self.db.Get('misc:tophash')) + return lx(self.db.Get('misc:tophash')) def loadfile(self, filename): fd = os.open(filename, os.O_RDONLY) @@ -651,7 +640,7 @@ def loadfile(self, filename): wanted = 0 buflen = len(buf) - startpos = string.find(buf, self.netmagic.msg_start) + startpos = string.find(buf, bitcoin.params.MESSAGE_START) if startpos < 0: wanted = 8 continue diff --git a/MemPool.py b/MemPool.py index a7cdff7..79a6d3a 100644 --- a/MemPool.py +++ b/MemPool.py @@ -6,7 +6,7 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. # -from bitcoin.serialize import uint256_to_shortstr +from bitcoin.core import CheckTransaction, CheckTransactionError, b2lx class MemPool(object): @@ -15,14 +15,15 @@ def __init__(self, log): self.log = log def add(self, tx): - tx.calc_sha256() - hash = tx.sha256 - hashstr = uint256_to_shortstr(hash) + hash = tx.GetHash() + hashstr = b2lx(hash) if hash in self.pool: self.log.write("MemPool.add(%s): already known" % (hashstr,)) return False - if not tx.is_valid(): + try: + CheckTransaction(tx) + except CheckTransactionError: self.log.write("MemPool.add(%s): invalid TX" % (hashstr, )) return False diff --git a/dbck.py b/dbck.py index 113445f..e86c8b3 100755 --- a/dbck.py +++ b/dbck.py @@ -50,8 +50,10 @@ block = CBlock() block.deserialize(f) - if not block.is_valid(): - log.write("block %064x failed" % (blkhash,)) + try: + CheckBlock(block) + except CheckBlockError: + log.write("block %s failed" % (b2lx(blkhash),)) failures += 1 scanned += 1 diff --git a/mkbootstrap.py b/mkbootstrap.py index 7fd9b00..0770fcb 100755 --- a/mkbootstrap.py +++ b/mkbootstrap.py @@ -15,7 +15,7 @@ import struct import argparse -from bitcoin.coredefs import NETWORKS +import bitcoin from bitcoin.core import CBlock from bitcoin.scripteval import * @@ -31,6 +31,7 @@ } MY_NETWORK = 'mainnet' +bitcoin.SelectParams(MY_NETWORK) SETTINGS = NET_SETTINGS[MY_NETWORK] @@ -46,9 +47,7 @@ log = Log.Log() mempool = MemPool.MemPool(log) -netmagic = NETWORKS[MY_NETWORK] -chaindb = ChainDb.ChainDb(SETTINGS, SETTINGS['db'], log, mempool, netmagic, - True) +chaindb = ChainDb.ChainDb(SETTINGS, SETTINGS['db'], log, mempool, True) if args.latest: scan_height = chaindb.getheight() @@ -78,7 +77,7 @@ ser_block = block.serialize() - outhdr = netmagic.msg_start + outhdr = bitcoin.params.MESSAGE_START outhdr += struct.pack("= NOBLKS_VERSION_START and - self.ver_send <= NOBLKS_VERSION_END): - self.getblocks_ok = False - self.remote_height = message.nStartingHeight self.send_message(msg_verack(self.ver_send)) if self.ver_send >= CADDR_TIME_VERSION: @@ -215,8 +205,7 @@ def got_message(self, message): # self.send_message(msg_mempool()) elif message.command == "ping": - if self.ver_send > BIP0031_VERSION: - self.send_message(msg_pong(self.ver_send)) + self.send_message(msg_pong(self.ver_send)) elif message.command == "addr": peermgr.new_addrs(message.addrs) @@ -241,9 +230,9 @@ def got_message(self, message): elif message.command == "tx": if self.chaindb.tx_is_orphan(message.tx): - self.log.write("MemPool: Ignoring orphan TX %064x" % (message.tx.sha256,)) + self.log.write("MemPool: Ignoring orphan TX %s" % (b2lx(message.tx.GetHash()),)) elif not self.chaindb.tx_signed(message.tx, None, True): - self.log.write("MemPool: Ignoring failed-sig TX %064x" % (message.tx.sha256,)) + self.log.write("MemPool: Ignoring failed-sig TX %s" % (b2lx(message.tx.GetHash()),)) else: self.mempool.add(message.tx) @@ -333,14 +322,14 @@ def getdata(self, message): def getblocks(self, message): blkmeta = self.chaindb.locate(message.locator) height = blkmeta.height - top_height = self.getheight() + top_height = self.chaindb.getheight() end_height = height + 500 if end_height > top_height: end_height = top_height msg = msg_inv() while height <= end_height: - hash = long(self.chaindb.height[str(height)]) + hash = self.chaindb.getblockhash(height) if hash == message.hashstop: break @@ -359,22 +348,20 @@ def getblocks(self, message): def getheaders(self, message): blkmeta = self.chaindb.locate(message.locator) height = blkmeta.height - top_height = self.getheight() + top_height = self.chaindb.getheight() end_height = height + 2000 if end_height > top_height: end_height = top_height msg = msg_headers() while height <= end_height: - blkhash = long(self.chaindb.height[str(height)]) + blkhash = self.chaindb.getblockhash(height) if blkhash == message.hashstop: break - db_block = self.chaindb.getblock(blkhash) - block = copy.copy(db_block) - block.vtx = [] + block = self.chaindb.getblock(blkhash) - msg.headers.append(block) + msg.headers.append(block.get_header()) height += 1 @@ -382,11 +369,10 @@ def getheaders(self, message): class PeerManager(object): - def __init__(self, log, mempool, chaindb, netmagic): + def __init__(self, log, mempool, chaindb): self.log = log self.mempool = mempool self.chaindb = chaindb - self.netmagic = netmagic self.peers = [] self.addrs = {} self.tried = {} @@ -396,7 +382,7 @@ def add(self, host, port): (host, port)) self.tried[host] = True c = NodeConn(host, port, self.log, self, self.mempool, - self.chaindb, self.netmagic) + self.chaindb) self.peers.append(c) return c @@ -467,16 +453,12 @@ def closeall(self): log.write("\n\n\n\n") - if chain not in NETWORKS: - log.write("invalid network") - sys.exit(1) - - netmagic = NETWORKS[chain] + bitcoin.SelectParams(chain) mempool = MemPool.MemPool(log) chaindb = ChainDb.ChainDb(settings, settings['db'], log, mempool, - netmagic, False, False) - peermgr = PeerManager(log, mempool, chaindb, netmagic) + False, False) + peermgr = PeerManager(log, mempool, chaindb) if 'loadblock' in settings: chaindb.loadfile(settings['loadblock']) diff --git a/q_avg_size.py b/q_avg_size.py index b04c87c..996384f 100755 --- a/q_avg_size.py +++ b/q_avg_size.py @@ -14,7 +14,7 @@ import cStringIO import struct -from bitcoin.coredefs import NETWORKS +import bitcoin from bitcoin.core import CBlock from bitcoin.scripteval import * @@ -30,14 +30,13 @@ } MY_NETWORK = 'mainnet' +bitcoin.SelectParams(MY_NETWORK) SETTINGS = NET_SETTINGS[MY_NETWORK] log = Log.Log(SETTINGS['log']) mempool = MemPool.MemPool(log) -netmagic = NETWORKS[MY_NETWORK] -chaindb = ChainDb.ChainDb(SETTINGS, SETTINGS['db'], log, mempool, netmagic, - True) +chaindb = ChainDb.ChainDb(SETTINGS, SETTINGS['db'], log, mempool, True) scanned = 0 failures = 0 diff --git a/rpc.py b/rpc.py index 3ec70c1..963f11e 100644 --- a/rpc.py +++ b/rpc.py @@ -15,8 +15,8 @@ import itertools import ChainDb -import bitcoin.coredefs -from bitcoin.serialize import uint256_from_compact +import bitcoin.core +from bitcoin.core.serialize import uint256_from_compact VALID_RPCS = { "getblockcount", @@ -52,24 +52,22 @@ def bufreverse(in_buf): return ''.join(out_words) def blockToJSON(block, blkmeta, cur_height): - block.calc_sha256() res = {} - res['hash'] = "%064x" % (block.sha256,) + res['hash'] = b2lx(block.GetHash()) res['confirmations'] = cur_height - blkmeta.height + 1 res['size'] = len(block.serialize()) res['height'] = blkmeta.height res['version'] = block.nVersion - res['merkleroot'] = "%064x" % (block.hashMerkleRoot,) + res['merkleroot'] = b2lx(block.hashMerkleRoot) res['time'] = block.nTime res['nonce'] = block.nNonce res['bits'] = "%x" % (block.nBits,) - res['previousblockhash'] = "%064x" % (block.hashPrevBlock,) + res['previousblockhash'] = b2lx(block.hashPrevBlock) txs = [] for tx in block.vtx: - tx.calc_sha256() - txs.append("%064x" % (tx.sha256,)) + txs.append("%s" % (b2lx(tx.GetHash()),)) res['tx'] = txs @@ -139,16 +137,16 @@ def getblockhash(self, params): heightidx = ChainDb.HeightIdx() heightidx.deserialize(self.chaindb.height[str(index)]) - return ("%064x" % (heightidx.blocks[0],), None) + return (b2lx(heightidx.blocks[0]), None) def getconnectioncount(self, params): return (len(self.peermgr.peers), None) def getinfo(self, params): d = {} - d['protocolversion'] = bitcoin.coredefs.PROTO_VERSION + d['protocolversion'] = bitcoin.core.PROTO_VERSION d['blocks'] = self.chaindb.getheight() - if self.chaindb.netmagic.block0 == 0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26fL: + if bitcoin.params.GENESIS_BLOCK.GetHash() == bitcoin.core.lx('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'): d['testnet'] = False else: d['testnet'] = True @@ -157,7 +155,7 @@ def getinfo(self, params): def getrawmempool(self, params): l = [] for k in self.mempool.pool.iterkeys(): - l.append("%064x" % (k,)) + l.append(b2lx(k)) return (l, None) def getrawtransaction(self, params): @@ -195,7 +193,7 @@ def getwork_new(self): res = {} target = uint256_from_compact(block.nBits) - res['target'] = "%064x" % (target,) + res['target'] = b2lx(target) data = block.serialize() data = data[:80] diff --git a/testscript.py b/testscript.py index 238ce2e..7982248 100755 --- a/testscript.py +++ b/testscript.py @@ -62,20 +62,18 @@ def scan_tx(tx): - tx.calc_sha256() - - if tx.sha256 in SKIP_TX: + if tx.GetHash() in SKIP_TX: return True -# log.write("...Scanning TX %064x" % (tx.sha256,)) +# log.write("...Scanning TX %s" % (b2lx(tx.GetHash()),)) for i in xrange(len(tx.vin)): txin = tx.vin[i] txfrom = chaindb.gettx(txin.prevout.hash) if not VerifySignature(txfrom, tx, i, 0): - log.write("TX %064x/%d failed" % (tx.sha256, i)) - log.write("FROMTX %064x" % (txfrom.sha256,)) + log.write("TX %s/%d failed" % (b2lx(tx.GetHash()), i)) + log.write("FROMTX %s" % (b2lx(txfrom.GetHash()),)) log.write(txfrom.__repr__()) - log.write("TOTX %064x" % (tx.sha256,)) + log.write("TOTX %s" % (b2lx(tx.GetHash()),)) log.write(tx.__repr__()) return False return True