Exemplo n.º 1
0
def sync_block():
    length = 0
    block_chain_new = None
    for column, i in enumerate(BaseUtils.node):
        message = BaseUtils.parse_node_valid(url='http://%s/block/sync/get' % i.addr, column=column)
        if not message:
            continue
        block_chain = BaseUtils.load_all_block(json.loads(message))
        if len(block_chain) > length:
            length = len(block_chain)
            block_chain_new = block_chain[:]
    if block_chain_new:
        for i in block_chain_new:
            i.have_sync = True
        for i in BaseUtils.node:
            data = json.dumps(BaseUtils.parse_all_block(block=block_chain_new[:]))
            BaseUtils.parse_node_valid(url='http://%s/block/add/new' % i.addr, data=data, column=column, method='post')
        if getsizeof(json.dumps(BaseUtils.parse_all_block(block=BaseUtils.block_chain_have_sync)))\
                / 1024 / 1024 > base_block_cap:
            block_size = len(BaseUtils.block_chain_have_sync) - 7
            BaseUtils.parse_block_to_file(new_block=BaseUtils.block_chain_have_sync,
                                          filename=str(BaseUtils.block_chain_have_sync[block_size - 1].index),
                                          block_size=block_size)
    if BaseUtils.block_chain_not_sync:
        BaseUtils.block_chain_not_sync[0].index = BaseUtils.block_chain_have_sync[-1].index + 1
        BaseUtils.block_chain_not_sync[0].previous_hash = BaseUtils.block_chain_have_sync[-1].hash
    else:
        BaseUtils.block_new.index = BaseUtils.block_chain_have_sync[-1].index + 1
        BaseUtils.block_new.previous_hash = BaseUtils.block_chain_have_sync[-1].hash
    return 'sync block success, and the sync block_link\'s length is %s' % length
Exemplo n.º 2
0
def get_transaction():
    values = request.values
    if 'hash' not in values:
        return 'Error: you must offer a hash of this request!!'
    hash = values['hash']
    return json.dumps(BaseUtils.get_transaction(
        hash, block=BaseUtils.block_chain_have_sync[:]), default=BaseUtils.parse_transaction)
Exemplo n.º 3
0
def register_node():
    values = request.values
    if 'addr' not in values or 'data' not in values or 'first' not in values:
        return 'Error: you must offer address and data in this request !!'
    addr = values['addr']
    data = values['data']
    first = values['first']
    for i in BaseUtils.node:
        if i.addr == addr:
            return 'you have register!!'
    BaseUtils.register_node(addr=addr, data=data)
    httpRequest.request('GET', url='http://%s/node/register?addr=%s&data=%s&first=0'
                                   % (addr, BaseUtils.node[0].addr, BaseUtils.node[0].data))
    for i in BaseUtils.node:
        httpRequest.request('GET', url='http://%s/node/register?data=%s&addr=%s&first=0' % (i.addr, data, addr))
    result = json.loads(requests.get('http://%s/block/sync/first' % addr).text)
    if first == '1':
        threading.Thread(target=BaseUtils.get_data_to_nodeByHttp, args=(result['ip'],)).start()
        BaseUtils.block_chain_have_sync = BaseUtils.load_all_block(result['block_chain_have_sync'])
        BaseUtils.block_chain_not_sync = BaseUtils.load_all_block(result['block_chain_not_sync'])
    return 'Success: you have added in the network-link !!'
Exemplo n.º 4
0
def add_new_sync():
    block_chain_new_str = json.loads(request.data.decode())
    block_chain_new = BaseUtils.load_all_block(block_chain_new_str)
    if block_chain_new[0].previous_hash == BaseUtils.block_chain_have_sync[-1].hash:
        BaseUtils.block_chain_have_sync.extend(block_chain_new)
        block_new_hash = [i.hash for i in block_chain_new]
        while BaseUtils.block_chain_not_sync:
            if BaseUtils.block_chain_not_sync[0].hash not in block_new_hash:
                break
            else:
                BaseUtils.block_chain_not_sync.pop(0)
    if BaseUtils.block_chain_not_sync:
        BaseUtils.block_chain_not_sync[0].previous_hash = BaseUtils.block_chain_have_sync[-1].hash
    return 'Success: sync finish!'
Exemplo n.º 5
0
def submit_transaction(random):
    data = request.data.decode()
    hash = BaseUtils.submit_transaction(data=data, gas_price=0)
    return json.dumps({"Success": {'hash': hash,
                                   },
                       })
Exemplo n.º 6
0
def get_sync_block_chain():
    return json.dumps(BaseUtils.parse_all_block(block=BaseUtils.block_chain_not_sync[:]))
Exemplo n.º 7
0
def get_block_chain():
    return json.dumps(BaseUtils.parse_all_block(block=BaseUtils.block_chain_have_sync))
Exemplo n.º 8
0
def sync_block_all():
    return BaseUtils.block_get_all()