예제 #1
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
예제 #2
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)
예제 #3
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')
예제 #4
0
파일: cli.py 프로젝트: doodoori2/nekoyume
def neko(private_key):
    app.app_context().push()
    while True:
        Block.sync()
        block = User(private_key).create_block(
            Move.query.filter_by(block=None).limit(20).all(),
            click=click,
        )
        if block:
            block.broadcast()
            click.echo(block)
예제 #5
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)
예제 #6
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()
예제 #7
0
def neko(private_key):
    app.app_context().push()
    Client(os.environ.get('SENTRY_DSN'))

    while True:
        Block.sync()
        block = User(private_key).create_block(
            [
                m for m in Move.query.filter_by(block=None).limit(20).all()
                if m.valid
            ],
            click=click,
        )
        if block:
            block.broadcast()
            click.echo(block)
예제 #8
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)
예제 #9
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