Пример #1
0
 def onSuccess(self, callback=None):
     for inventory in self.inventory:
         if inventory.invType == InventoryType.BLOCK:
             message = MessageFactory.getInstance(MessageType.BLOCK)
             block = self.chain.getBlockByHash(inventory.getInvHash())
             message.addrRecv = self.addrFrom
             message.block = block.serialize()
             message.send()
         elif inventory.invType == InventoryType.TRANSACTION:
             message = MessageFactory.getInstance(MessageType.TX)
             txHash = inventory.getInvHash()
             transaction = self.memoryPool[txHash]
             message.addrRecv = self.addrFrom
             message.transaction = transaction.serialize()
             message.send()
Пример #2
0
 def onSuccess(self, callback=None):
     message = MessageFactory.getInstance(MessageType.ADDR)
     peers = Peers.getPeers()
     message.count = len(peers)
     for peer in peers.copy():
         message.addrList.append(peer.host)
     callback(message.serialize())
Пример #3
0
def inv(invType, invHash):
    message = MessageFactory.getInstance(MessageType.INV)
    inventory = []
    inventory.append(Inventory(invType, invHash))
    message.inventory.extend(inventory)
    message.count = len(inventory)
    broadcastMessage(message)
Пример #4
0
 def handlePayload(self, payload):
     message = MessageFactory.getInstance(payload)
     message.addrRecv = self.getAddrRecv()
     message.addrFrom = self.getAddrFrom()
     if message.deserialize(payload):
         message.onSuccess(self.writeResponse)
     else:
         message.onFailure(self.writeResponse)
Пример #5
0
 def send(self):
     payload = Network.sendData(self.addrRecv, self.serialize())
     message = MessageFactory.getInstance(MessageType.INV)
     message.addrRecv = self.addrRecv
     if message.deserialize(payload):
         message.onSuccess()
     else:
         message.onFailure(self.sendFailure)
Пример #6
0
 def onSuccess(self, callback=None):
     message = MessageFactory.getInstance(MessageType.INV)
     inventory = []
     for txId in self.memoryPool:
         unconfirmedTransaction = self.memoryPool[txId]
         inventory.append(
             Inventory(InventoryType.TRANSACTION,
                       unconfirmedTransaction.hash()))
     message.inventory.extend(inventory)
     callback(message.serialize())
Пример #7
0
 def send(self):
     payload = Network.sendData(self.addrRecv, self.serialize())
     message = MessageFactory.getInstance(MessageType.ADDR)
     if message.deserialize(payload):
         message.onSuccess()
     else:
         peer = Peer()
         peer.host = self.addrRecv
         Peers.removePeer(peer)
         message.onFailure(self.sendFailure)
Пример #8
0
    def send(self):
        peer = Peers.getPeerByHost(self.addrRecv)
        self.blockHashes.append(peer.chainHeadBlockHash)

        payload = Network.sendData(self.addrRecv, self.serialize())
        message = MessageFactory.getInstance(MessageType.INV)
        message.addrFrom = self.addrRecv
        if message.deserialize(payload):
            message.onSuccess()
        else:
            message.onFailure(self.sendFailure)
Пример #9
0
 def onSuccess(self, callback=None):
     message = MessageFactory.getInstance(MessageType.GETDATA)
     message.addrRecv = self.addrFrom
     for inventory in self.inventory:
         if inventory.invType == InventoryType.BLOCK:
             if self.chain.getBlockByHash(inventory.getInvHash()) == None:
                 message.inventory.append(inventory)
         elif inventory.invType == InventoryType.TRANSACTION:
             if MemoryPool.getTransactionById(
                     inventory.getInvHash()) == None:
                 message.inventory.append(inventory)
     if len(message.inventory) > 0:
         message.send()
Пример #10
0
 def onSuccess(self, callback=None):
     message = MessageFactory.getInstance(MessageType.INV)
     inventory = []
     for blockHash in self.blockHashes:
         previousHash = blockHash
         while previousHash != None:
             block = self.chain.getBlockByHash(previousHash)
             if block != None:
                 inventory.append(
                     Inventory(InventoryType.BLOCK, block.hash()))
                 previousHash = block.previousHash
             else:
                 previousHash = None
     message.inventory.extend(inventory)
     callback(message.serialize())
Пример #11
0
 def send(self):
     payload = Network.sendData(self.addrRecv, self.serialize())
     isValid = True
     message = MessageFactory.getInstance(MessageType.PONG)
     if message.deserialize(payload):
         if message.nonce == self.nonce:
             message.onSuccess()
         else:
             isValid = False 
     else:
         isValid = False 
     if isValid == False:
         peer = Peer()
         peer.host = self.addrRecv
         Peers.removePeer(peer)
         message.onFailure(self.sendFailure)
Пример #12
0
 def onSuccess(self, callback=None):
     peer = Peers.getPeerByHost(self.addrFrom)
     syncVersion = False
     if peer == None:
         peer = Peer()
     else:
         syncVersion = peer.lastVersionNonce == None
     if peer.lastVersionNonce == None or peer.lastVersionNonce == self.nonce:
         peer.host = self.addrFrom
         peer.version = self.version
         peer.lastVersionNonce = self.nonce
         peer.chainHeadBlockHash = self.chainHeadBlockHash
         Peers.addPeer(peer)
         if syncVersion:
             Sync.synchronizeMessage(MessageType.VERSION, peer.host)
     message = MessageFactory.getInstance(MessageType.VERACK)
     callback(message.serialize())
Пример #13
0
    def send(self):
        peer = Peers.getPeerByHost(self.addrRecv)
        if peer.lastVersionNonce == None:
            peer.lastVersionNonce = self.nonce
            Peers.addPeer(peer)
        else:
            self.nonce = peer.lastVersionNonce

        chainHeadBlock = Chain.getChain().getChainHeadBlock()
        self.chainHeadBlockHash = chainHeadBlock.hash()

        payload = Network.sendData(self.addrRecv, self.serialize())
        message = MessageFactory.getInstance(MessageType.VERACK)
        message.addrFrom = self.addrRecv
        if message.deserialize(payload):
            message.onSuccess()
        else:
            peer = Peer()
            peer.host = self.addrRecv
            Peers.removePeer(peer)
            message.onFailure(self.sendFailure)
Пример #14
0
def getdata(inventoryType, inventoryHash):
    message = MessageFactory.getInstance(MessageType.GETDATA)
    message.inventory.append(Inventory(inventoryType, inventoryHash))
    broadcastMessage(message)
Пример #15
0
def mempool():
    message = MessageFactory.getInstance(MessageType.MEMPOOL)
    broadcastMessage(message)
Пример #16
0
def ping():
    message = MessageFactory.getInstance(MessageType.PING)
    while True:
        broadcastMessage(message)
        time.sleep(Config.getIntValue("PING_BROADCAST_SECONDS"))
Пример #17
0
 def onSuccess(self, callback = None):
     message = MessageFactory.getInstance(MessageType.PONG)
     message.nonce = self.nonce
     callback(message.serialize())        
Пример #18
0
def getblocks():
    message = MessageFactory.getInstance(MessageType.GETBLOCKS)
    broadcastMessage(message)
Пример #19
0
def version():
    message = MessageFactory.getInstance(MessageType.VERSION)
    broadcastMessage(message)
Пример #20
0
def synchronizeMessage(messageType, host):
    message = MessageFactory.getInstance(messageType)
    processMessage(message, host)
Пример #21
0
def getaddr():
    message = MessageFactory.getInstance(MessageType.GETADDR)
    broadcastMessage(message)
Пример #22
0
def addr(host):
    message = MessageFactory.getInstance(MessageType.ADDR)
    message.addrList.append(host)
    broadcastMessage(message)