Example #1
0
def download_blocks(peer, peers_block_count, length):
    b = [
        max(0, length - 10),
        min(peers_block_count['length'] + 1, length + custom.download_many)
    ]

    # tell them our request's range
    blocks = network_cmd(peer, {'type': 'rangeRequest', 'range': b})

    if type(blocks) != list: return -1

    if not isinstance(blocks, list): return []

    length = tools.db_get('length')  # our blockcount
    block = tools.db_get(length)  # get the last block

    for i in range(
            10
    ):  # this part should be re-written so badly -> this was from Zack.... NO COMMENT
        if tools.fork_check(blocks, custom.queues, length,
                            block):  # if block is valid
            blockchain.delete_block(custom.queues)  # wtf does this do?
            length -= 1

    for block in blocks:
        custom.queues['suggested_blocks'].put([block, peer])

    return 0
Example #2
0
        def download_blocks(peer, DB, peers_block_count, length):

            def fork_check(newblocks, DB):
                length = copy.deepcopy(DB['length'])
                block = blockchain.db_get(length, DB)
                recent_hash = tools.det_hash(block)
                their_hashes = map(tools.det_hash, newblocks)
                return recent_hash not in map(tools.det_hash, newblocks)

            def bounds(length, peers_block_count, DB):
                if peers_block_count['length'] - length > custom.download_many:
                    end = length + custom.download_many - 1
                else:
                    end = peers_block_count['length']
                return [max(length - 2, 0), end]

            blocks = cmd({'type': 'rangeRequest',
                          'range': bounds(length, peers_block_count, DB)})
            if type(blocks) != type([1, 2]):
                return []
            for i in range(2):  # Only delete a max of 2 blocks, otherwise a
                # peer might trick us into deleting everything over and over.
                if fork_check(blocks, DB):
                    blockchain.delete_block(DB)
            for block in blocks:
                DB['suggested_blocks'].append(block)
            return
Example #3
0
        def download_blocks(peer, DB, peers_block_count, length):

            def fork_check(newblocks, DB):
                block = blockchain.db_get(DB['length'], DB)
                recent_hash = tools.det_hash(block)
                their_hashes = map(tools.det_hash, newblocks)
                return recent_hash not in their_hashes

            def bounds(length, peers_block_count):
                if peers_block_count['length'] - length > custom.download_many:
                    end = length + custom.download_many - 1
                else:
                    end = peers_block_count['length']
                return [max(length - 2, 0), end]

            blocks = cmd({'type': 'rangeRequest',
                          'range': bounds(length, peers_block_count)})
            if not isinstance(blocks, list):
                return []
            for i in range(2):  # Only delete a max of 2 blocks, otherwise a
                # peer might trick us into deleting everything over and over.
                if fork_check(blocks, DB):
                    blockchain.delete_block(DB)
            DB['suggested_blocks'].extend(blocks)
            return
Example #4
0
        def download_blocks(peer, DB, peers_block_count, length):
            def fork_check(newblocks, DB):
                length = copy.deepcopy(DB['length'])
                block = blockchain.db_get(length, DB)
                recent_hash = tools.det_hash(block)
                their_hashes = map(tools.det_hash, newblocks)
                return recent_hash not in map(tools.det_hash, newblocks)

            def bounds(length, peers_block_count, DB):
                if peers_block_count['length'] - length > custom.download_many:
                    end = length + custom.download_many - 1
                else:
                    end = peers_block_count['length']
                return [max(length - 2, 0), end]

            blocks = cmd({
                'type': 'rangeRequest',
                'range': bounds(length, peers_block_count, DB)
            })
            if type(blocks) != type([1, 2]):
                return []
            for i in range(10):  # Only delete a max of 20 blocks, otherwise a
                # peer might trick us into deleting everything over and over.
                if fork_check(blocks, DB):
                    blockchain.delete_block(DB)
            DB['suggested_blocks'] += blocks
            return
Example #5
0
def pushblock(dic, DB):
    if 'blocks' in dic:
        for i in range(20):
            if tools.fork_check(dic['blocks'], DB):
                blockchain.delete_block(DB)
        for block in dic['blocks']:
            DB['suggested_blocks'].put([block, dic['peer']])
    else:
        DB['suggested_blocks'].put([dic['block'], dic['peer']])
    return 'success'
Example #6
0
def pushblock(dic, DB):
    if 'blocks' in dic:
        for i in range(20):
            if tools.fork_check(dic['blocks'], DB):
                blockchain.delete_block(DB)
        for block in dic['blocks']:
            DB['suggested_blocks'].put([block, dic['peer']])
    else:
        DB['suggested_blocks'].put([dic['block'], dic['peer']])
    return 'success'
Example #7
0
def download_blocks(peer, DB, peers_block_count, length):
    blocks = cmd(peer, {'type': 'rangeRequest',
                        'range': bounds(length, peers_block_count['length'])})
    if not isinstance(blocks, list):
        return []
    for i in range(2):  # Only delete a max of 2 blocks, otherwise a
        # peer might trick us into deleting everything over and over.
        if fork_check(blocks, DB):
            blockchain.delete_block(DB)
    for block in blocks:
        DB['suggested_blocks'].put([block, peer])
    return 0
Example #8
0
def download_blocks(peer, DB, peers_block_count, length):
    b=[max(0, length-10), min(peers_block_count, length+custom.download_many)]
    blocks = cmd(peer, {'type': 'rangeRequest', 'range': b})
    if type(blocks)!=list: return -1
    if not isinstance(blocks, list): return []
    length=tools.db_get('length')
    block=tools.db_get(length)
    for i in range(10):#this part should be re-written so badly
        if tools.fork_check(blocks, DB, length, block):
            blockchain.delete_block(DB)
            length-=1
    for block in blocks:
        DB['suggested_blocks'].put([block, peer])
    return 0
def pushblock(dic, DB):
    length=tools.db_get('length')
    block = tools.db_get(length, DB)    
    if 'peer' in dic: peer=dic['peer']
    else: peer=False
    if 'blocks' in dic:
        for i in range(20):
            if tools.fork_check(dic['blocks'], DB, length, block):
                blockchain.delete_block(DB)
                length-=1
        for block in dic['blocks']:
            DB['suggested_blocks'].put([block, peer])
    else:
        DB['suggested_blocks'].put([dic['block'], peer])
    return 'success'
def pushblock(dic, DB):
    length = tools.db_get('length')
    block = tools.db_get(length, DB)
    if 'peer' in dic: peer = dic['peer']
    else: peer = False
    if 'blocks' in dic:
        for i in range(20):
            if tools.fork_check(dic['blocks'], DB, length, block):
                blockchain.delete_block(DB)
                length -= 1
        for block in dic['blocks']:
            DB['suggested_blocks'].put([block, peer])
    else:
        DB['suggested_blocks'].put([dic['block'], peer])
    return 'success'
Example #11
0
def download_blocks(peer, DB, peers_block_count, length):
    blocks = cmd(
        peer, {
            'type': 'rangeRequest',
            'range': bounds(length, peers_block_count['length'])
        })
    if not isinstance(blocks, list):
        return []
    for i in range(2):  # Only delete a max of 2 blocks, otherwise a
        # peer might trick us into deleting everything over and over.
        if fork_check(blocks, DB):
            blockchain.delete_block(DB)
    for block in blocks:
        DB['suggested_blocks'].put([block, peer])
    return 0
Example #12
0
 def peer_check(peer, DB):
     cmd=(lambda x: networking.send_command(peer, x))
     block_count=cmd({'type':'blockCount'})
     if type(block_count)!=type({'a':1}):
         return 
     if 'error' in block_count.keys():
         return         
     length=copy.deepcopy(DB['length'])
     us=copy.deepcopy(DB['diffLength'])
     them=block_count['diffLength']
     ahead=length-block_count['length']
     if them < us and ahead>0:#if we are ahead of them
         cmd({'type':'pushblock', 
              'block':blockchain.db_get(block_count['length']+1, DB)})
         return []
     if length<0: return []
     if us == them:#if we are on the same block, ask for any new txs
         block=blockchain.db_get(length, DB)
         if 'recent_hash' in block_count:
             if tools.det_hash(block)!=block_count['recent_hash']:
                 blockchain.delete_block()
                 #print('WE WERE ON A FORK. time to back up.')
                 return []
         my_txs=DB['txs']
         txs=cmd({'type':'txs'})
         for tx in txs:
             DB['suggested_txs'].append(tx)
         pushers=[x for x in my_txs if x not in txs]
         for push in pushers:
             cmd({'type':'pushtx', 'tx':push})
         return []
     start=length-2
     if start<0:
         start=0
     if ahead>custom.download_many:
         end=length+custom.doanload_many-1
     else:
         end=block_count['length']
     blocks= cmd({'type':'rangeRequest', 
                  'range':[start, end]})
     if type(blocks)!=type([1,2]):
         return []
     times=2
     while fork_check(blocks, DB) and times>0:
         times-=1
         blockchain.delete_block(DB)
     for block in blocks:
         DB['suggested_blocks'].append(block)
Example #13
0
def download_blocks(peer, DB, peers_block_count, length):
    b = [
        max(0, length - 10),
        min(peers_block_count, length + custom.download_many)
    ]
    blocks = cmd(peer, {'type': 'rangeRequest', 'range': b})
    if type(blocks) != list: return -1
    if not isinstance(blocks, list): return []
    length = tools.db_get('length')
    block = tools.db_get(length)
    for i in range(10):  #this part should be re-written so badly
        if tools.fork_check(blocks, DB, length, block):
            blockchain.delete_block(DB)
            length -= 1
    for block in blocks:
        DB['suggested_blocks'].put([block, peer])
    return 0
Example #14
0
def download_blocks(peer, DB, peers_block_count, length):
    b = [
        max(0, length - 10),
        min(peers_block_count['length'], length + custom.download_many)
    ]
    #tools.log('asked for: ' +str(b))
    blocks = cmd(peer, {'type': 'rangeRequest', 'range': b})
    if type(blocks) != list:
        #tools.log('unable to download blocks that time')
        return -1
    if not isinstance(blocks, list):
        return []
    for i in range(20):  # Only delete a max of 20 blocks, otherwise a
        # peer might trick us into deleting everything over and over.
        if tools.fork_check(blocks, DB):
            blockchain.delete_block(DB)
    for block in blocks:
        DB['suggested_blocks'].put([block, peer])
    return 0