示例#1
0
def post_move():
    new_move = request.get_json()
    move = Move.query.get(new_move['id'])

    if move:
        return jsonify(result='success')

    if not move:
        move = Move(
            id=new_move['id'],
            user=new_move['user'],
            name=new_move['name'],
            signature=new_move['signature'],
            tax=new_move['tax'],
            details=new_move['details'],
            created_at=datetime.datetime.strptime(new_move['created_at'],
                                                  '%Y-%m-%d %H:%M:%S.%f'),
        )

    if not move.valid:
        return jsonify(result='failed',
                       message=f"move {move.id} isn't valid."), 400

    db.session.add(move)
    db.session.commit()

    sent_node = Node()
    if 'sent_node' in new_move:
        sent_node.url = new_move['sent_node']

    move_broadcast.delay(move.id,
                         sent_node_url=sent_node.url,
                         my_node_url=f'{request.scheme}://{request.host}')
    return jsonify(result='success')
示例#2
0
def sync():
    Client(os.environ.get('SENTRY_DSN'))
    public_url = get_my_public_url()
    if public_url:
        click.echo(f"You have a public node url. ({public_url})")
        Node.broadcast(Node.post_node_endpoint, {'url': public_url})
    Node.update()
    engine = db.engine
    if not engine.dialect.has_table(engine.connect(), Block.__tablename__):
        click.echo("You need to initialize. try `nekoyume init`.")
        return False
    while True:
        try:
            prev_id = Block.query.order_by(Block.id.desc()).first().id
        except AttributeError:
            prev_id = 0
        Block.sync(click=click)
        try:
            if prev_id == Block.query.order_by(Block.id.desc()).first().id:
                click.echo("The blockchain is up to date.")
                time.sleep(15)
        except AttributeError:
            click.echo(("There is no well-connected node. "
                        "please check you network."))
            break
示例#3
0
文件: cli.py 项目: doodoori2/nekoyume
def init(seed):
    click.echo('Creating database...')
    db.create_all()
    click.echo(f'Updating node... (seed: {seed})')
    Node.update(Node(url=seed))
    click.echo('Syncing blocks...')
    Block.sync(click=click)
示例#4
0
def test_sync(fx_user, fx_session, fx_other_user, fx_other_session, fx_server,
              fx_novice_status):
    assert fx_other_session.query(Block).count() == 0
    assert fx_session.query(Block).count() == 0

    Block.sync(Node(url=fx_server.url), fx_other_session)
    assert fx_other_session.query(Block).count() == 0
    assert fx_session.query(Block).count() == 0

    fx_other_user.create_block([])
    Block.sync(Node(url=fx_server.url), fx_other_session)
    assert fx_other_session.query(Block).count() == 1
    assert fx_session.query(Block).count() == 0

    move = fx_user.create_novice(fx_novice_status)
    fx_user.create_block([move])
    fx_user.create_block([])
    fx_user.create_block([])

    assert fx_other_session.query(Block).count() == 1
    assert fx_other_session.query(Move).count() == 0
    assert fx_session.query(Block).count() == 3
    assert fx_session.query(Move).count() == 1

    Block.sync(Node(url=fx_server.url), fx_other_session)
    assert fx_other_session.query(Block).count() == 3
    assert fx_other_session.query(Move).count() == 1
    assert fx_session.query(Block).count() == 3
    assert fx_session.query(Move).count() == 1
示例#5
0
文件: api.py 项目: ziwon/nekoyume
def post_move():
    new_move = request.get_json()
    move = Move.query.get(new_move['id'])

    if move:
        return jsonify(result='success')

    if not move:
        move = Move.deserialize(new_move)

    if not move.valid:
        return jsonify(result='failed',
                       message=f"move {move.id} isn't valid."), 400

    db.session.add(move)
    try:
        db.session.commit()
    except IntegrityError:
        return jsonify(result='failed',
                       message="This node already has this move."), 400
    sent_node = Node()
    if 'sent_node' in new_move:
        sent_node.url = new_move['sent_node']

    move_broadcast.delay(move.id,
                         sent_node_url=sent_node.url,
                         my_node_url=f'{request.scheme}://{request.host}')
    return jsonify(result='success')
示例#6
0
def post_node():
    if 'url' in request.values:
        url = request.values['url']
    elif 'url' in request.get_json():
        url = request.get_json()['url']
    else:
        return jsonify(
            result='failed',
            message='Invalid parameter.'
        ), 400
    node = Node.query.get(url)
    if node:
        return jsonify(result='success')
    else:
        node = Node(url=url)
        db.session.add(node)
        try:
            response = requests.get(f'{node.url}/ping')
        except requests.exceptions.ConnectionError:
            return jsonify(
                result='failed',
                message=f'Connection to node {node.url} was failed.'
            ), 403
        if response.status_code == 200:
            node.last_connected_at = datetime.datetime.now()
            db.session.commit()
            return jsonify(result='success')
        else:
            return jsonify(
                result='failed',
                message=f'Connection to node {node.url} was failed.'
            ), 403
示例#7
0
文件: api.py 项目: longfin/nekoyume
def post_block():
    new_block = request.get_json()
    last_block = Block.query.order_by(Block.id.desc()).first()

    if not new_block:
        return jsonify(result='failed', message="empty block."), 400

    if not last_block and new_block['id'] != 1:
        Block.sync(Node.query.order_by(Node.last_connected_at.desc()).first())
        return jsonify(result='failed',
                       message="new block isn't our next block."), 403

    if (new_block['id'] > 1
            and (new_block['id'] != last_block.id + 1
                 or new_block['prev_hash'] != last_block.hash)):
        if new_block['id'] > last_block.id + 1:
            Block.sync(
                Node.query.order_by(Node.last_connected_at.desc()).first())
        return jsonify(result='failed',
                       message="new block isn't our next block."), 403

    block = Block.deserialize(new_block)

    for new_move in new_block['moves']:
        move = Move.query.get(new_move['id'])
        if not move:
            move = Move(
                id=new_move['id'],
                user=new_move['user'],
                name=new_move['name'],
                signature=new_move['signature'],
                tax=new_move['tax'],
                details=new_move['details'],
                created_at=datetime.datetime.strptime(new_move['created_at'],
                                                      '%Y-%m-%d %H:%M:%S.%f'),
                block_id=block.id,
            )
        if not move.valid:
            return jsonify(result='failed',
                           message=f"move {move.id} isn't valid."), 400
        block.moves.append(move)
    if not block.valid:
        return jsonify(result='failed', message="new block isn't valid."), 400

    db.session.add(block)
    try:
        db.session.commit()
    except IntegrityError:
        return jsonify(result='failed',
                       message="This node already has this block."), 400
    sent_node = Node()
    if 'sent_node' in new_block:
        sent_node.url = new_block['sent_node']
    block_broadcast.delay(block.id,
                          sent_node_url=sent_node.url,
                          my_node_url=f'{request.scheme}://{request.host}')
    return jsonify(result='success')
示例#8
0
def init(seed, sync):
    click.echo('Creating database...')
    db.create_all()
    click.echo(f'Updating node... (seed: {seed})')
    if seed:
        Node.update(Node.get(url=seed))
    else:
        Node.update()
    if sync:
        click.echo('Syncing blocks...')
        Block.sync(click=click)
示例#9
0
def init(seed, sync):
    echo('Creating database...')
    db.create_all()
    echo(f'Updating node... (seed: {seed})')
    if sync:
        Node.update(Node.get(url=seed))
        echo('Syncing blocks...')
        Block.sync(echo=echo)

    echo('Compiling translations...')
    dir_path = os.path.abspath(os.path.dirname(__file__))
    compile_command = compile_catalog()
    compile_command.directory = dir_path + '/translations'
    compile_command.finalize_options()
    compile_command.run()
示例#10
0
def post_move():
    unconfirmed_move = get_unconfirmed_move(g.user.address)

    if unconfirmed_move:
        return redirect(url_for('.get_dashboard'))

    if request.values.get('name') == 'hack_and_slash':
        move = g.user.hack_and_slash(
            request.values.get('weapon'),
            request.values.get('armor'),
            request.values.get('food'),
        )
    if request.values.get('name') == 'sleep':
        move = g.user.sleep()
    if request.values.get('name') == 'level_up':
        move = g.user.level_up(request.values.get('new_status'))
    if request.values.get('name') == 'say':
        move = g.user.say(request.values.get('content'))
    if request.values.get('name') == 'send':
        move = g.user.send(request.values.get('item'),
                           request.values.get('amount'),
                           request.values.get('receiver'))
    if request.values.get('name') == 'combine':
        move = g.user.combine(request.values.get('item1'),
                              request.values.get('item2'),
                              request.values.get('item3'))

    if move:
        move.broadcast(my_node=Node(url=f'{request.scheme}://{request.host}'))
    return redirect(url_for('.get_dashboard'))
示例#11
0
文件: cli.py 项目: doodoori2/nekoyume
def sync():
    public_url = get_my_public_url()
    if public_url:
        click.echo(f"You have a public node url. ({public_url})")
        Node.broadcast(Node.post_node_endpoint, {'url': public_url})
    while True:
        try:
            prev_id = Block.query.order_by(Block.id.desc()).first().id
        except AttributeError:
            click.echo("You need to initialize. try `nekoyume init`.")
            break
        if not prev_id:
            click.echo("You need to initialize. try `nekoyume init`.")
            break
        Block.sync(click=click)
        if prev_id == Block.query.order_by(Block.id.desc()).first().id:
            click.echo("The blockchain is up to date.")
            time.sleep(15)
示例#12
0
def get_nodes():
    nodes = Node.query.filter(
        Node.last_connected_at >= datetime.datetime.now() -
        datetime.timedelta(60 * 3)
    ).order_by(Node.last_connected_at.desc()).limit(2500).all()

    public_url = get_my_public_url()
    if public_url:
        nodes.append(Node(url=public_url,
                          last_connected_at=datetime.datetime.now()))

    return jsonify(nodes=[n.url for n in nodes])
示例#13
0
def test_block_broadcast(fx_user, fx_session, fx_other_user, fx_other_session,
                         fx_server):
    assert fx_other_session.query(Block).count() == 0
    assert fx_session.query(Block).count() == 0

    fx_other_session.add(
        Node(url=fx_server.url, last_connected_at=datetime.datetime.now()))
    fx_other_session.commit()

    block = fx_other_user.create_block([])
    block.broadcast(session=fx_other_session)
    assert fx_other_session.query(Block).count() == 1
    assert fx_session.query(Block).count() == 1
示例#14
0
def test_move_broadcast(fx_user, fx_session, fx_other_user, fx_other_session,
                        fx_server, fx_novice_status):
    assert fx_other_session.query(Move).count() == 0
    assert fx_session.query(Move).count() == 0

    fx_other_session.add(
        Node(url=fx_server.url, last_connected_at=datetime.datetime.now()))
    fx_other_session.commit()

    move = fx_other_user.create_novice(fx_novice_status)
    assert not fx_session.query(Move).get(move.id)

    move.broadcast(session=fx_other_session)
    assert fx_session.query(Move).get(move.id)
示例#15
0
def get_new_novice():
    if not g.user.avatar():
        move = Move.query.filter_by(
            user_address=g.user.address,
            name='create_novice',
        ).first()
        if not move:
            move = g.user.create_novice({
                'strength': '12',
                'dexterity': '12',
                'constitution': '9',
                'intelligence': '10',
                'wisdom': '8',
                'charisma': '13'
            })
            db.session.add(move)
            db.session.commit()
            move.broadcast(my_node=Node(
                url=f'{request.scheme}://{request.host}'))
        return render_template('new.html', move=move)
    return redirect(url_for('.get_dashboard'))
示例#16
0
def block_broadcast(block_id, sent_node_url, my_node_url, session=db.session):
    try:
        session.query(Block).get(block_id).broadcast(Node(url=sent_node_url),
                                                     Node(url=my_node_url))
    except AttributeError:
        pass
示例#17
0
def move_broadcast(move_id, sent_node_url, my_node_url, session=db.session):
    try:
        session.query(Move).get(move_id).broadcast(Node(url=sent_node_url),
                                                   Node(url=my_node_url))
    except AttributeError:
        pass
示例#18
0
def test_node(fx_server, fx_session):
    assert fx_server.url
    assert Node.get(fx_server.url, session=fx_session)
    assert Node.get(fx_server.url, session=fx_session).url == fx_server.url
    assert Node.get(fx_server.url, session=fx_session).last_connected_at