def send_message(self, message): if verbose_sendmsg(message): print("send %s" % repr(message)) tmsg = message_to_str(self.node.netmagic, message) try: self.socket.sendall(tmsg) self.last_sent = time.time() except Exception, err: print "Exception: ", Exception, err self.handle_close()
def send_message(self, message): if verbose_sendmsg(message): self.logger.debug("send %s" % repr(message)) tmsg = message_to_str(self.node.netmagic, message) try: self.socket.sendall(tmsg) self.last_sent = time.time() except Exception as err: self.logger.error("Exception: %r" % err) self.handle_close()
def putoneblock(self, block): block.calc_sha256() if not block.is_valid(): self.log.write("Invalid block %064x" % (block.sha256, )) return False if not self.have_prevblock(block): self.orphans[block.sha256] = True self.orphan_deps[block.hashPrevBlock] = block self.log.write("Orphan block %064x (%d orphans)" % (block.sha256, len(self.orphan_deps))) return False top_height = self.getheight() top_work = long(self.db.Get('misc:total_work'), 16) # read metadata for previous block prevmeta = BlkMeta() if top_height >= 0: ser_prevhash = ser_uint256(block.hashPrevBlock) prevmeta.deserialize(self.db.Get('blkmeta:' + ser_prevhash)) else: ser_prevhash = '' batch = leveldb.WriteBatch() # build network "block" msg, as canonical disk storage form msg = msg_block() msg.block = block msg_data = message_to_str(self.netmagic, msg) # write "block" msg to storage fpos = self.blk_write.tell() if self.compress_on_write: msg_data = zlib.compress(msg_data, 1) msg_data = struct.pack('>4si%ds' % len(msg_data), 'ZLIB', len(msg_data), msg_data) self.blk_write.write(msg_data) self.blk_write.flush() # add index entry ser_hash = ser_uint256(block.sha256) batch.Put('blocks:' + ser_hash, str(fpos)) # store metadata related to this block blkmeta = BlkMeta() blkmeta.height = prevmeta.height + 1 blkmeta.work = (prevmeta.work + uint256_from_compact(block.nBits)) batch.Put('blkmeta:' + ser_hash, blkmeta.serialize()) # store list of blocks at this height heightidx = HeightIdx() heightstr = str(blkmeta.height) try: heightidx.deserialize(self.db.Get('height:' + heightstr)) except KeyError: pass heightidx.blocks.append(block.sha256) 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)) return True # update global chain pointers if not self.set_best_chain(ser_prevhash, ser_hash, block, blkmeta): return False return True
def putoneblock(self, block): block.calc_sha256() if not block.is_valid(): self.log.write("Invalid block %064x" % (block.sha256, )) return False if not self.have_prevblock(block): self.orphans[block.sha256] = True self.orphan_deps[block.hashPrevBlock] = block self.log.write("Orphan block %064x (%d orphans)" % (block.sha256, len(self.orphan_deps))) return False top_height = self.getheight() top_work = long(self.db.Get('misc:total_work'), 16) # read metadata for previous block prevmeta = BlkMeta() if top_height >= 0: ser_prevhash = ser_uint256(block.hashPrevBlock) prevmeta.deserialize(self.db.Get('blkmeta:'+ser_prevhash)) else: ser_prevhash = '' batch = leveldb.WriteBatch() # build network "block" msg, as canonical disk storage form msg = msg_block() msg.block = block msg_data = message_to_str(self.netmagic, msg) # write "block" msg to storage fpos = self.blk_write.tell() self.blk_write.write(msg_data) self.blk_write.flush() # add index entry ser_hash = ser_uint256(block.sha256) batch.Put('blocks:'+ser_hash, str(fpos)) # store metadata related to this block blkmeta = BlkMeta() blkmeta.height = prevmeta.height + 1 blkmeta.work = (prevmeta.work + uint256_from_compact(block.nBits)) batch.Put('blkmeta:'+ser_hash, blkmeta.serialize()) # store list of blocks at this height heightidx = HeightIdx() heightstr = str(blkmeta.height) try: heightidx.deserialize(self.db.Get('height:'+heightstr)) except KeyError: pass heightidx.blocks.append(block.sha256) 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)) return True # update global chain pointers if not self.set_best_chain(ser_prevhash, ser_hash, block, blkmeta): return False return True
def putoneblock(self, block): block.calc_sha256() if not block.is_valid(): self.logger.error("Invalid block %064x" % (block.sha256, )) return False if not self.have_prevblock(block): self.orphans[block.sha256] = True self.orphan_deps[block.hashPrevBlock] = block self.logger.warning("Orphan block %064x (%d orphans)" % \ (block.sha256, len(self.orphan_deps))) return False top_height = self.getheight() top_work = long(self.db.Get('misc:total_work'), 16) # read metadata for previous block prevmeta = BlkMeta() if top_height >= 0: ser_prevhash = ser_uint256(block.hashPrevBlock) prevmeta.deserialize(self.db.Get('blkmeta:' + ser_prevhash)) else: ser_prevhash = '' batch = leveldb.WriteBatch() # build network "block" msg, as canonical disk storage form msg = msg_block() msg.block = block msg_data = message_to_str(self.netmagic, msg) # write "block" msg to storage fpos = self.blk_write.tell() self.blk_write.write(msg_data) self.blk_write.flush() # add index entry ser_hash = ser_uint256(block.sha256) batch.Put('blocks:' + ser_hash, str(fpos)) # store metadata related to this block blkmeta = BlkMeta() blkmeta.height = prevmeta.height + 1 blkmeta.work = (prevmeta.work + uint256_from_compact(block.nBits)) batch.Put('blkmeta:' + ser_hash, blkmeta.serialize()) # store list of blocks at this height heightidx = HeightIdx() heightstr = str(blkmeta.height) try: heightidx.deserialize(self.db.Get('height:' + heightstr)) except KeyError: pass heightidx.blocks.append(block.sha256) 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.logger.warning("ChainDb: height %d (weak), block %064x" % (blkmeta.height, block.sha256)) return True # update global chain pointers if not self.set_best_chain(ser_prevhash, ser_hash, block, blkmeta): return False # If just inserted block is multiple of 2016, compute new difficulty if blkmeta.height % 2016 == 0 and blkmeta.height != 0: last_stamp = block.nTime old_block_hash = heightidx.blocks[len(heightidx.blocks) - 2016] old_block = self.getblock(old_block_hash) old_stamp = old_block.nTime self.logger.debug("Changing difficulty from %02x" % self.difficulty) self.difficulty = self.compute_difficulty(self.difficulty, last_stamp - old_stamp) self.logger.debug("New difficulty %02x" % self.difficulty) return True