當前位置: 首頁>>代碼示例>>Python>>正文


Python DB.getBlock方法代碼示例

本文整理匯總了Python中db.DB.getBlock方法的典型用法代碼示例。如果您正苦於以下問題:Python DB.getBlock方法的具體用法?Python DB.getBlock怎麽用?Python DB.getBlock使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在db.DB的用法示例。


在下文中一共展示了DB.getBlock方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: verify_block_chain

# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import getBlock [as 別名]
 def verify_block_chain(self, debug=False):
   from db import DB
   d = DB()
   log.info('Verifying blockchain...')
   self.client.broadcast_info('Verifying blockchain')
   latest_block_hash = d.getLatestBlockHash()
   if not latest_block_hash:
     return True
   latest_block = d.getBlock(latest_block_hash)
   return latest_block.verify(debug=debug)
開發者ID:plutokid,項目名稱:PyCoin,代碼行數:12,代碼來源:miningmanager.py

示例2: __init__

# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import getBlock [as 別名]

#.........這裏部分代碼省略.........
      raise Exception('Not a Transaction object!')
    for t in self.transactionList:
      if t.hash_transaction() == trans.hash_transaction():
        return
    log.debug('Adding trans to block: %s', trans.hash_transaction())
    self.transactionList.append(trans)
    
  def computeMerkleRoot(self):
    self.HashMerkleRoot = Utils.buildMerkleTree(self.transactionList)
    
  def getPreviousBlockHash(self):
    """ retrieves the previous block hash from the database """
    return self.db.getLatestBlock()[0]
  
  def pack(self, withoutReward=False):
    """ Serializes the block into a byte array """
    b = bytearray()
    b.extend(self.HashPrevBlock) #32
    b.extend(struct.pack('I', self.nonce)) #4
    b.extend(struct.pack('B', self.target)) #1
    if withoutReward:
      num = len(self.transactionList) - 1
    else:
      num = len(self.transactionList)
    b.extend(struct.pack('B', num)) #1
    for i in range(num):
      b.extend(self.transactionList[i].hash_transaction()) # 32
    return b
  
  def unpack(self, buf):
    """ Deserializes a byte array into this block object """
    offset = 0
    self.HashPrevBlock = buf[offset:offset+32]
    offset += 32
    self.nonce = struct.unpack_from('I', buf, offset)[0]
    offset += 4
    self.target = struct.unpack_from('B', buf, offset)[0]
    offset += 1
    num_trans = struct.unpack_from('B', buf, offset)[0]
    offset += 1
    for i in range(num_trans):
      h = buf[offset:offset+32]
      log.debug('Unpacking: ', h)
      trans = self.db.getTransactionByHash(h)
      self.transactionList.append(trans)
      offset += 32
    log.debug('Block hash: %s', self.hash_block(hex=True, withoutReward=False))
    log.debug('Block nonce: &d', self.nonce)
    log.info('Block unpacked')
    
  def store_block(self):
    self.db.insertBlock(self)
    
  def hash_block(self, hex=False, withoutReward=False):
    """ hashes the block """
    b = bytearray()
    b.extend(self.pack(withoutReward=withoutReward))
    self.hash = SHA256.new(b)
    if hex:
      return self.hash.hexdigest()
    return self.hash.digest()
    
  def finish_block(self):
    """ verifies the block, then broadcasts it """
    v = self.verify()
    if v:
      self.client.broadcast_block(self, ignore=True)
      log.debug('Block verified')
      self.store_block()
    
  def verify(self, debug=False):
    """ verifies the block """
    log.debug('Finishing block')
    verified = False
    if debug:
      log.info('Block hash: %s', self.hash_block(hex=True))
    for t in self.transactionList:
      if not t.verify(debug=debug):
        log.warn('Invalid transaction in block')
        return False
    if not self.test_hash(self.hash_block(withoutReward=False), self.target):
      log.warn('Block has doesn\'t match target!')
      return False
    prevBlock = self.db.getBlock(self.HashPrevBlock)
    if prevBlock:
      log.debug('Verifying previous block')
      return prevBlock.verify(debug=debug)
    return True
    
  def test_hash(self, hash, target):
    """ check if the last 'target' bits are zeros """
    int_hash = int(struct.unpack_from('I', hash[-4:])[0])
    low = (int_hash & -int_hash)
    lowBit = -1
    while (low):
      low >>= 1
      lowBit += 1
    if lowBit == target:
      log.info(bin(int_hash))
    return lowBit == target
開發者ID:plutokid,項目名稱:PyCoin,代碼行數:104,代碼來源:block.py


注:本文中的db.DB.getBlock方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。