Example #1
0
 def genesis(pubkey, DB):
     out={'version':custom.version,
          'length':0,
          'time':time.time(),
          'target':blockchain.target(DB),
          'txs':[make_mint(pubkey, DB)]}
     out=tools.unpackage(tools.package(out))
     return out
Example #2
0
def mine(hashes_till_check, reward_address, DB):
    #tries to mine the next block hashes_till_check many times.
    def make_mint(pubkey, DB): return {'type':'mint', 'id':[pubkey], 'signature':['first_sig'],
                                       'count':blockchain.count(pubkey, DB)}
                                       
    def genesis(pubkey, DB):
        target=blockchain.target(DB)
        out={'version':custom.version,
             'length':0,
             'time':time.time(),
             'target':target,
             'diffLength':blockchain.hexInvert(target),
             'txs':[make_mint(pubkey, DB)]}
        out=tools.unpackage(tools.package(out))
        return out
        
    def make_block(prev_block, txs, pubkey, DB):
        leng=int(prev_block['length'])+1
        target=blockchain.target(DB, leng)
        diffLength=blockchain.hexSum(prev_block['diffLength'], 
                                     blockchain.hexInvert(target))
        out={'version':custom.version,
             'txs':txs+[make_mint(pubkey, DB)],
             'length':leng,
             'time':time.time(),
             'diffLength':diffLength,
             'target':target,
             'prevHash':tools.det_hash(prev_block)}
        out=tools.unpackage(tools.package(out))
        return out
        
    def POW(block, hashes, target):
        halfHash=tools.det_hash(block)
        block[u'nonce']=random.randint(0,100000000000000000)
        count=0
        while tools.det_hash({u'nonce':block['nonce'], 
                              u'halfHash':halfHash})>target:
            count+=1
            block[u'nonce']+=1
            if count>hashes:
                return {'error':False}
            ''' for testing sudden loss in hashpower from miners.
            if block[u'length']>150:
            else: time.sleep(0.01)
            '''
        return block
        
    length=copy.deepcopy(DB['length'])
    if length==-1:
        block=genesis(reward_address, DB)
        txs=[]
    else:
        prev_block=blockchain.db_get(length, DB)
        txs=DB['txs']
        block=make_block(prev_block, txs, reward_address, DB)
    block=POW(block, hashes_till_check, blockchain.target(DB, block['length']))
    DB['suggested_blocks'].append(block)
Example #3
0
 def make_block(prev_block, txs, pubkey, DB):
     leng=int(prev_block['length'])+1
     out={'version':custom.version,
          'txs':txs+[make_mint(pubkey, DB)],
          'length':leng,
          'time':time.time(),
          'target':blockchain.target(DB, leng),
          'prevHash':tools.det_hash(prev_block)}
     out=tools.unpackage(tools.package(out))
     return out
Example #4
0
 def genesis(pubkey, DB):
     target = blockchain.target(DB)
     out = {'version': custom.version,
            'length': 0,
            'time': time.time(),
            'target': target,
            'diffLength': blockchain.hexInvert(target),
            'txs': [make_mint(pubkey, DB)]}
     print('out: ' + str(out))
     out = tools.unpackage(tools.package(out))
     return out
Example #5
0
 def genesis(pubkey, DB):
     target = blockchain.target(DB)
     out = {'version': custom.version,
            'length': 0,
            'time': time.time(),
            'target': target,
            'diffLength': blockchain.hexInvert(target),
            'txs': [make_mint(pubkey, DB)]}
     print('out: ' + str(out))
     out = tools.unpackage(tools.package(out))
     return out
Example #6
0
 def make_block(prev_block, txs, pubkey, DB):
     leng=int(prev_block['length'])+1
     target=blockchain.target(DB, leng)
     diffLength=blockchain.hexSum(prev_block['diffLength'], blockchain.hexInvert(target))
     out={'version':custom.version,
          'txs':txs+[make_mint(pubkey, DB)],
          'length':leng,
          'time':time.time(),
          'diffLength':diffLength,
          'target':target,
          'prevHash':tools.det_hash(prev_block)}
     out=tools.unpackage(tools.package(out))
     return out
Example #7
0
 def make_block(prev_block, txs, pubkey, DB):
     leng = int(prev_block['length']) + 1
     target = blockchain.target(DB, leng)
     diffLength = blockchain.hexSum(prev_block['diffLength'],
                                    blockchain.hexInvert(target))
     out = {'version': custom.version,
            'txs': txs + [make_mint(pubkey, DB)],
            'length': leng,
            'time': time.time(),
            'diffLength': diffLength,
            'target': target,
            'prevHash': tools.det_hash(prev_block)}
     out = tools.unpackage(tools.package(out))
     return out
Example #8
0
def mine(hashes_till_check, reward_address, DB):
    def make_mint(pubkey, DB): return {'type':'mint', 'id':pubkey, 'count':blockchain.count(pubkey, DB)}
    def genesis(pubkey, DB):
        out={'version':custom.version,
             'length':0,
             'time':time.time(),
             'target':blockchain.target(DB),
             'txs':[make_mint(pubkey, DB)]}
        out=tools.unpackage(tools.package(out))
        return out
    def make_block(prev_block, txs, pubkey, DB):
        leng=int(prev_block['length'])+1
        out={'version':custom.version,
             'txs':txs+[make_mint(pubkey, DB)],
             'length':leng,
             'time':time.time(),
             'target':blockchain.target(DB, leng),
             'prevHash':tools.det_hash(prev_block)}
        out=tools.unpackage(tools.package(out))
        return out
    def POW(block, hashes, target):
        halfHash=tools.det_hash(block)
        block[u'nonce']=random.randint(0,100000000000000000)
        count=0
        while tools.det_hash({u'nonce':block['nonce'], u'halfHash':halfHash})>target:
            count+=1
            block[u'nonce']+=1
            if count>hashes:
                return {'error':False}
            ''' for testing sudden loss in hashpower from miners.
            if block[u'length']>150:# and block[u'nonce']%10==0: time.sleep(0.1)
            else: time.sleep(0.01)
            '''
        print('found block: ' +str(block))
        return block
    length=copy.deepcopy(DB['length'])
    if length==-1:
        block=genesis(reward_address, DB)
        txs=[]
    else:
        prev_block=blockchain.db_get(length, DB)
        txs=DB['txs']
        block=make_block(prev_block, txs, reward_address, DB)
    block=POW(block, hashes_till_check, blockchain.target(DB, block['length']))
    DB['suggested_blocks'].append(block)
Example #9
0
def miner_controller(reward_address, peers, hashes_till_check, DB):
    """ Spawns worker CPU mining processes and coordinates the effort."""
    def make_mint(pubkey, DB):
        address = tools.make_address([reward_address], 1)
        return {'type': 'mint',
                'pubkeys': [pubkey],
                'signatures': ['first_sig'],
                'count': blockchain.count(address, DB)}

    def genesis(pubkey, DB):
        target = blockchain.target(DB)
        out = {'version': custom.version,
               'length': 0,
               'time': time.time(),
               'target': target,
               'diffLength': blockchain.hexInvert(target),
               'txs': [make_mint(pubkey, DB)]}
        print('out: ' + str(out))
        out = tools.unpackage(tools.package(out))
        return out

    def make_block(prev_block, txs, pubkey, DB):
        leng = int(prev_block['length']) + 1
        target = blockchain.target(DB, leng)
        diffLength = blockchain.hexSum(prev_block['diffLength'],
                                       blockchain.hexInvert(target))
        out = {'version': custom.version,
               'txs': txs + [make_mint(pubkey, DB)],
               'length': leng,
               'time': time.time(),
               'diffLength': diffLength,
               'target': target,
               'prevHash': tools.det_hash(prev_block)}
        out = tools.unpackage(tools.package(out))
        return out
    def restart_workers():
        print("Possible solution found, restarting mining workers.")
        for worker_mailbox in worker_mailboxes:
            worker_mailbox['restart'].set()

    def spawn_worker():
        print("Spawning worker")
        restart_signal = multiprocessing.Event()
        work_queue = multiprocessing.Queue()
        worker_proc = multiprocessing.Process(target=miner,
                                              args=(submitted_blocks, work_queue,
                                                    restart_signal))
        worker_proc.daemon = True
        worker_proc.start()
        return {'restart': restart_signal, 'worker': worker_proc,
                'work_queue': work_queue}

    submitted_blocks = multiprocessing.Queue()
    num_cores = multiprocessing.cpu_count()
    print("Creating %d mining workers." % num_cores)
    worker_mailboxes = [spawn_worker() for _ in range(num_cores)]
    candidate_block = None
    length = None
    while True:
        length = DB['length']
        if length == -1:
            candidate_block = genesis(reward_address, DB)
            txs = []
        else:
            prev_block = blockchain.db_get(length, DB)
            txs = DB['txs']
            candidate_block = make_block(prev_block, txs, reward_address, DB)

        work = (candidate_block, hashes_till_check,
                blockchain.target(DB, candidate_block['length']))

        for worker_mailbox in worker_mailboxes:
            worker_mailbox['work_queue'].put(copy.copy(work))

        # When block found, add to suggested blocks.
        solved_block = submitted_blocks.get()  # TODO(roasbeef): size=1?
        if solved_block['length'] != length + 1:
            continue
        DB['suggested_blocks'].append(solved_block)
        restart_workers()
Example #10
0
def mine(dic):
    hashes_till_check = dic['hashes']
    reward_address = dic['reward_address']
    DB = dic['DB']

    # Tries to mine the next block hashes_till_check many times.
    def make_mint(pubkey, DB):
        address = tools.make_address([reward_address], 1)
        return {
            'type': 'mint',
            'pubkeys': [pubkey],
            'signatures': ['first_sig'],
            'count': blockchain.count(address, DB)
        }

    def genesis(pubkey, DB):
        target = blockchain.target(DB)
        out = {
            'version': custom.version,
            'length': 0,
            'time': time.time(),
            'target': target,
            'diffLength': blockchain.hexInvert(target),
            'txs': [make_mint(pubkey, DB)]
        }
        out = tools.unpackage(tools.package(out))
        return out

    def make_block(prev_block, txs, pubkey, DB):
        try:
            leng = int(prev_block['length']) + 1
        except:
            print('prev: ' + str(prev_block))
            error('here')
        target = blockchain.target(DB, leng)
        diffLength = blockchain.hexSum(prev_block['diffLength'],
                                       blockchain.hexInvert(target))
        out = {
            'version': custom.version,
            'txs': txs + [make_mint(pubkey, DB)],
            'length': leng,
            'time': time.time(),
            'diffLength': diffLength,
            'target': target,
            'prevHash': tools.det_hash(prev_block)
        }
        out = tools.unpackage(tools.package(out))
        return out

    def POW(block, hashes, target):
        halfHash = tools.det_hash(block)
        block[u'nonce'] = random.randint(0, 100000000000000000)
        count = 0
        while tools.det_hash({
                u'nonce': block['nonce'],
                u'halfHash': halfHash
        }) > target:
            count += 1
            block[u'nonce'] += 1
            if count > hashes:
                return {'error': False}
            ''' for testing sudden loss in hashpower from miners.
            if block[u'length']>150:
            else: time.sleep(0.01)
            '''
        return block

    length = DB['length']
    if length == -1:
        block = genesis(reward_address, DB)
        txs = []
    else:
        prev_block = blockchain.db_get(length, DB)
        txs = DB['txs']
        block = make_block(prev_block, txs, reward_address, DB)
    block = POW(block, hashes_till_check,
                blockchain.target(DB, block['length']))
    DB['suggested_blocks'].append(block)
Example #11
0
def difficulty(DB): return(str(blockchain.target(DB)))
def my_balance(DB, address='default'): 
Example #12
0
def difficulty(DB):
    return (str(blockchain.target(DB)))