def send_node_request(self, peer, sha256, level, index, generations, complete=0):
     assert level < 253 and generations < 253 and index < 2**level and index < 2**30
     flags = 0
     if complete: flags |= 1
     msg = BTMessage.MSG_REQUEST_NODES + util.ser_uint256(sha256) + chr(level) + util.ser_varint(index) + chr(generations) + util.ser_varint(flags)
     #print "sending message %s to peer %s" % (msg.encode('hex'), str(peer))
     peer.send_message(msg)
 def send_txcount_proof(self, peer, sha256):
     txcount, hashes = self.merkles[sha256].maketxcountproof()
     if txcount and hashes:
         msg = BTMessage.MSG_TXCOUNT_PROOF + util.ser_uint256(sha256) + util.ser_varint(txcount) + ''.join(hashes)
         print "sending %i byte txcount proof to %s" %(len(msg), `peer`)
         peer.send_message(msg)
     else:
         "couldn't send txcount proof to ", peer
    def send_nodes(self, peer, sha256, level, index, generations, flags):
        if not sha256 in self.merkles:
            debuglog('btnet', 'peer %s wants a block that we don\'t know about: %s' % (str(peer), util.ser_uint256(sha256)[::-1].encode('hex')))
            peer.send_message(BTMessage.MSG_MISSING_BLOCK + util.ser_uint256(sha256) + chr(level) + util.ser_varint(index) + chr(generations))
            return
        if not self.merkles[sha256].state.hasdescendants(level, index, generations):
            debuglog('btnet', 'peer %s wants nodes that we don\'t know about: l=%i i=%i g=%i h=%s' % (str(peer), leve, index, generations, util.ser_uint256(sha256)[::-1].encode('hex')))
            peer.send_message(BTMessage.MSG_MISSING_NODES + util.ser_uint256(sha256) + chr(level) + util.ser_varint(index) + chr(generations))
            return
        run = self.merkles[sha256].getrun(level, index, generations)
        assert type(run[0]) == str and len(run[0]) == 32 # Just checking to make sure that merkles stores the serialized str version of the hash, since I forgot

        flags = 0
        if flags: raise NotImplementedError
        msg = BTMessage.MSG_RUN + util.ser_uint256(sha256) + chr(level) + util.ser_varint(index) + chr(generations) + util.ser_varint(len(run)) + util.ser_varint(flags) + ''.join(run)
        if len(msg) > peer.MTU:
            debuglog('btnet', 'MSG_RUN has length %i which exceeds peer %s\'s max MTU of %i' % (len(msg), str(peer), peer.MTU))
        peer.send_message(msg)
 def send_txcount_proof(self, peer, sha256):
     txcount, hashes = self.merkles[sha256].maketxcountproof()
     if txcount and hashes:
         msg = BTMessage.MSG_TXCOUNT_PROOF + util.ser_uint256(
             sha256) + util.ser_varint(txcount) + ''.join(hashes)
         print "sending %i byte txcount proof to %s" % (len(msg), ` peer `)
         peer.send_message(msg)
     else:
         "couldn't send txcount proof to ", peer
 def recv_node_request(self, data, peer):
     s = StringIO.StringIO(data.split(BTMessage.MSG_REQUEST_NODES)[1])
     sha256 = util.deser_uint256(s)
     level = ord(s.read(1))
     index = util.deser_varint(s)
     generations = ord(s.read(1))
     flags = util.deser_varint(s)
     debuglog('btnet', "peer %s wants h=%s l=%i i=%i g=%i f=%i" % (str(peer), util.ser_uint256(sha256)[::-1].encode('hex'), level, index, generations, flags))
     # fixme: maybe add choke/throttle checks here?
     self.send_nodes(peer, sha256, level, index, generations, flags)
 def recv_node_request(self, data, peer):
     s = StringIO.StringIO(data.split(BTMessage.MSG_REQUEST_NODES)[1])
     sha256 = util.deser_uint256(s)
     level = ord(s.read(1))
     index = util.deser_varint(s)
     generations = ord(s.read(1))
     flags = util.deser_varint(s)
     debuglog(
         'btnet', "peer %s wants h=%s l=%i i=%i g=%i f=%i" %
         (str(peer), util.ser_uint256(sha256)[::-1].encode('hex'), level,
          index, generations, flags))
     # fixme: maybe add choke/throttle checks here?
     self.send_nodes(peer, sha256, level, index, generations, flags)
    def send_nodes(self, peer, sha256, level, index, generations, flags):
        if not sha256 in self.merkles:
            debuglog(
                'btnet',
                'peer %s wants a block that we don\'t know about: %s' %
                (str(peer), util.ser_uint256(sha256)[::-1].encode('hex')))
            peer.send_message(BTMessage.MSG_MISSING_BLOCK +
                              util.ser_uint256(sha256) + chr(level) +
                              util.ser_varint(index) + chr(generations))
            return
        if not self.merkles[sha256].state.hasdescendants(
                level, index, generations):
            debuglog(
                'btnet',
                'peer %s wants nodes that we don\'t know about: l=%i i=%i g=%i h=%s'
                % (str(peer), leve, index, generations,
                   util.ser_uint256(sha256)[::-1].encode('hex')))
            peer.send_message(BTMessage.MSG_MISSING_NODES +
                              util.ser_uint256(sha256) + chr(level) +
                              util.ser_varint(index) + chr(generations))
            return
        run = self.merkles[sha256].getrun(level, index, generations)
        assert type(run[0]) == str and len(
            run[0]
        ) == 32  # Just checking to make sure that merkles stores the serialized str version of the hash, since I forgot

        flags = 0
        if flags: raise NotImplementedError
        msg = BTMessage.MSG_RUN + util.ser_uint256(sha256) + chr(
            level) + util.ser_varint(index) + chr(
                generations) + util.ser_varint(
                    len(run)) + util.ser_varint(flags) + ''.join(run)
        if len(msg) > peer.MTU:
            debuglog(
                'btnet',
                'MSG_RUN has length %i which exceeds peer %s\'s max MTU of %i'
                % (len(msg), str(peer), peer.MTU))
        peer.send_message(msg)
    def recv_nodes(self, data, peer):
        s = StringIO.StringIO(data.split(BTMessage.MSG_RUN)[1])
        sha256 = util.deser_uint256(s)
        level = ord(s.read(1))
        index = util.deser_varint(s)
        generations = ord(s.read(1))
        length = util.deser_varint(s)
        flags = util.deser_varint(s)
        if flags: raise NotImplementedError
        run = [s.read(32) for i in range(length)]
        result = self.merkles[sha256].checkaddrun(level, index, generations,
                                                  length, run)

        if not result:
            print "Failed to add from peer=%s: l=%i i=%i g=%i h=%s" % (
                str(peer), level, index, generations,
                util.ser_uint256(sha256)[::-1].encode('hex'))
            debuglog(
                'btnet', "Failed to add from peer=%s: l=%i i=%i g=%i h=%s" %
                (str(peer), level, index, generations,
                 util.ser_uint256(sha256)[::-1].encode('hex')))
        else:
            self.maybe_update_peers(sha256)
 def send_node_request(self,
                       peer,
                       sha256,
                       level,
                       index,
                       generations,
                       complete=0):
     assert level < 253 and generations < 253 and index < 2**level and index < 2**30
     flags = 0
     if complete: flags |= 1
     msg = BTMessage.MSG_REQUEST_NODES + util.ser_uint256(sha256) + chr(
         level) + util.ser_varint(index) + chr(
             generations) + util.ser_varint(flags)
     #print "sending message %s to peer %s" % (msg.encode('hex'), str(peer))
     peer.send_message(msg)
Beispiel #10
0
	def hash_merkletree(self, transactions):
		txhashes = [None] + [ util.ser_uint256(int(t['hash'], 16)) for t in transactions ]
		self.merkletree = MerkleTree(txhashes)
    def recv_nodes(self, data, peer):
        s = StringIO.StringIO(data.split(BTMessage.MSG_RUN)[1])
        sha256 = util.deser_uint256(s)
        level = ord(s.read(1))
        index = util.deser_varint(s)
        generations = ord(s.read(1))
        length = util.deser_varint(s)
        flags = util.deser_varint(s)
        if flags: raise NotImplementedError
        run = [s.read(32) for i in range(length)]
        result = self.merkles[sha256].checkaddrun(level, index, generations, length, run)

        if not result:
            print "Failed to add from peer=%s: l=%i i=%i g=%i h=%s" % (str(peer), level, index, generations, util.ser_uint256(sha256)[::-1].encode('hex'))
            debuglog('btnet', "Failed to add from peer=%s: l=%i i=%i g=%i h=%s" % (str(peer), level, index, generations, util.ser_uint256(sha256)[::-1].encode('hex')))
        else:
            self.maybe_update_peers(sha256)
 def req_txcount_proof(self, peer, sha256):
     msg = BTMessage.MSG_REQ_TXCOUNT + util.ser_uint256(sha256)
     print "sending txcount proof req to ", peer
     peer.send_message(msg)
 def send_blockstate(self, state, sha256, peer, level=0, index=0):
     assert peer in self.peers.values()
     peer.lastupdates[sha256] = (self.merkles[sha256].runs, time.time())
     msg = BTMessage.MSG_BLOCKSTATE + util.ser_uint256(sha256) + state.serialize(level, index)
     peer.send_message(msg)
Beispiel #14
0
 def hash_merkletree(self, transactions):
     txhashes = [None] + [
         util.ser_uint256(int(t['hash'], 16)) for t in transactions
     ]
     self.merkletree = MerkleTree(txhashes)
 def req_txcount_proof(self, peer, sha256):
     msg = BTMessage.MSG_REQ_TXCOUNT + util.ser_uint256(sha256)
     print "sending txcount proof req to ", peer
     peer.send_message(msg)
 def send_blockstate(self, state, hash, peer, level=0, index=0):
     assert peer in self.peers.values()
     msg = BTMessage.MSG_BLOCKSTATE + util.ser_uint256(
         hash) + state.serialize(level, index)
     peer.send_message(msg)
 def send_blockstate(self, state, sha256, peer, level=0, index=0):
     assert peer in self.peers.values()
     peer.lastupdates[sha256] = (self.merkles[sha256].runs, time.time())
     msg = BTMessage.MSG_BLOCKSTATE + util.ser_uint256(
         sha256) + state.serialize(level, index)
     peer.send_message(msg)