def state(tmpdir):
    db = tmpdir.join("state.db")
    state = ChannelManagerState(db.strpath)
    state.setup_db(
        NETWORK_ID,
        CONTRACT_ADDRESS,
        RECEIVER_ADDRESS
    )
    return state
def main(
        rpc_provider: HTTPProvider,
        private_key: str,
        private_key_password_file: str,
        state_file: str,
        channel_manager_address: str,
        minimum_amount: int,
        gas_price: int,
):
    if minimum_amount <= 0:
        click.echo('Minimum amount need to be at least 1')
        sys.exit(1)
    private_key = utils.get_private_key(private_key, private_key_password_file)
    if private_key is None:
        sys.exit(1)

    receiver_address = utils.privkey_to_addr(private_key)

    web3 = Web3(HTTPProvider(rpc_provider, request_kwargs={'timeout': 60}))
    config.NETWORK_CFG.set_defaults(int(web3.version.network))
    web3.eth.defaultAccount = receiver_address
    channel_manager_address = (
        channel_manager_address or config.NETWORK_CFG.channel_manager_address
    )
    channel_manager_contract = make_channel_manager_contract(web3, channel_manager_address)

    if not state_file:
        state_file_name = "%s_%s.db" % (
            channel_manager_address[:10],
            receiver_address[:10]
        )
        app_dir = click.get_app_dir('microraiden')
        if not os.path.exists(app_dir):
            click.echo('No state file or directory found!')
            sys.exit(1)
        state_file = os.path.join(app_dir, state_file_name)

    try:
        click.echo('Loading state file from {}'.format(state_file))
        state = ChannelManagerState.load(state_file)
    except StateFileException:
        click.echo('Error reading state file')
        traceback.print_exc()
        sys.exit(1)
    if not is_same_address(state.receiver, receiver_address):
        click.echo('Private key does not match receiver address in state file')
        sys.exit(1)
    elif not is_same_address(state.contract_address, channel_manager_address):
        click.echo('Channel manager contract address mismatch')
        sys.exit(1)

    click.echo('Withdrawing all paid tokens with at least {} due from '
               'receiver {}'.format(minimum_amount, receiver_address))
    withdraw_from_channels(
        private_key,
        state,
        channel_manager_contract,
        minimum_amount,
        gas_price * denoms.gwei if gas_price else None,
    )
Exemple #3
0
def main(
    rpc_provider: HTTPProvider,
    private_key: str,
    private_key_password_file: str,
    state_file: str,
    channel_manager_address: str,
    minimum_amount: int,
    gas_price: int,
):
    if minimum_amount <= 0:
        click.echo('Minimum amount need to be at least 1')
        sys.exit(1)
    private_key = utils.get_private_key(private_key, private_key_password_file)
    if private_key is None:
        sys.exit(1)

    receiver_address = utils.privkey_to_addr(private_key)

    web3 = Web3(HTTPProvider(rpc_provider, request_kwargs={'timeout': 60}))
    config.NETWORK_CFG.set_defaults(int(web3.version.network))
    web3.eth.defaultAccount = receiver_address
    channel_manager_address = (channel_manager_address
                               or config.NETWORK_CFG.channel_manager_address)
    channel_manager_contract = make_channel_manager_contract(
        web3, channel_manager_address)

    if not state_file:
        state_file_name = "%s_%s.db" % (channel_manager_address[:10],
                                        receiver_address[:10])
        app_dir = click.get_app_dir('microraiden')
        if not os.path.exists(app_dir):
            click.echo('No state file or directory found!')
            sys.exit(1)
        state_file = os.path.join(app_dir, state_file_name)

    try:
        click.echo('Loading state file from {}'.format(state_file))
        state = ChannelManagerState.load(state_file)
    except StateFileException:
        click.echo('Error reading state file')
        traceback.print_exc()
        sys.exit(1)
    if not is_same_address(state.receiver, receiver_address):
        click.echo('Private key does not match receiver address in state file')
        sys.exit(1)
    elif not is_same_address(state.contract_address, channel_manager_address):
        click.echo('Channel manager contract address mismatch')
        sys.exit(1)

    click.echo('Withdrawing all paid tokens with at least {} due from '
               'receiver {}'.format(minimum_amount, receiver_address))
    withdraw_from_channels(
        private_key,
        state,
        channel_manager_contract,
        minimum_amount,
        gas_price * denoms.gwei if gas_price else None,
    )
def test_sync_state(state):
    state.update_sync_state(confirmed_head_hash=BLOCK_HASH)
    assert state.confirmed_head_hash == BLOCK_HASH
    state.update_sync_state(confirmed_head_number=123)
    assert state.confirmed_head_number == 123
    state.update_sync_state(unconfirmed_head_hash=BLOCK_HASH)
    assert state.unconfirmed_head_hash == BLOCK_HASH
    state.update_sync_state(unconfirmed_head_number=321)
    assert state.unconfirmed_head_number == 321

    state_loaded = ChannelManagerState.load(state.filename, check_permissions=False)
    assert state_loaded.confirmed_head_hash == BLOCK_HASH
    assert state_loaded.confirmed_head_number == 123
    assert state_loaded.unconfirmed_head_hash == BLOCK_HASH
    assert state_loaded.unconfirmed_head_number == 321
Exemple #5
0
def test_sync_state(state):
    state.update_sync_state(confirmed_head_hash=BLOCK_HASH)
    assert state.confirmed_head_hash == BLOCK_HASH
    state.update_sync_state(confirmed_head_number=123)
    assert state.confirmed_head_number == 123
    state.update_sync_state(unconfirmed_head_hash=BLOCK_HASH)
    assert state.unconfirmed_head_hash == BLOCK_HASH
    state.update_sync_state(unconfirmed_head_number=321)
    assert state.unconfirmed_head_number == 321

    state_loaded = ChannelManagerState.load(state.filename,
                                            check_permissions=False)
    assert state_loaded.confirmed_head_hash == BLOCK_HASH
    assert state_loaded.confirmed_head_number == 123
    assert state_loaded.unconfirmed_head_hash == BLOCK_HASH
    assert state_loaded.unconfirmed_head_number == 321
Exemple #6
0
def main(private_key, state_file, channel_manager_address):
    if private_key is None:
        log.fatal("No private key provided")
        sys.exit(1)
    if utils.check_permission_safety(private_key) is False:
        log.fatal("Private key file %s must be readable only by its owner." %
                  (private_key))
        sys.exit(1)
    with open(private_key) as keyfile:
        private_key = keyfile.readline()[:-1]
    print(len(decode_hex(private_key)), is_hex(private_key))
    if not is_hex(private_key) or len(decode_hex(private_key)) != 32:
        log.fatal("Private key must be specified as 32 hex encoded bytes")
        sys.exit(1)

    receiver_address = privkey_to_addr(private_key)
    channel_manager_address = channel_manager_address or config.CHANNEL_MANAGER_ADDRESS

    if not state_file:
        state_file_name = "%s_%s.json" % (channel_manager_address[:10],
                                          receiver_address[:10])
        app_dir = click.get_app_dir('microraiden')
        state_file = os.path.join(app_dir, state_file_name)

    web3 = Web3(config.WEB3_PROVIDER)
    web3.eth.defaultAccount = receiver_address
    contract_proxy = make_contract_proxy(web3, private_key,
                                         config.CHANNEL_MANAGER_ADDRESS)

    try:
        click.echo('Loading state file from {}'.format(state_file))
        state = ChannelManagerState.load(state_file)
    except StateFileException:
        click.echo('Error reading state file')
        traceback.print_exc()
        sys.exit(1)
    if not is_same_address(state.receiver, receiver_address):
        click.echo('Private key does not match receiver address in state file')
        sys.exit(1)
    elif not is_same_address(state.contract_address, channel_manager_address):
        click.echo('Channel manager contract address mismatch')
        sys.exit(1)

    click.echo('Closing all open channels with valid balance proofs for '
               'receiver {}'.format(receiver_address))
    close_open_channels(state, contract_proxy)
def test_creation(state):
    assert state.receiver == RECEIVER_ADDRESS
    assert state.contract_address == CONTRACT_ADDRESS
    assert state.network_id == NETWORK_ID

    assert state.confirmed_head_number is None
    assert state.confirmed_head_hash is None
    assert state.unconfirmed_head_number is None
    assert state.unconfirmed_head_hash is None
    assert state.n_channels == 0
    assert state.n_open_channels == 0

    state_loaded = ChannelManagerState.load(state.filename, check_permissions=False)
    assert state_loaded.confirmed_head_number is None
    assert state_loaded.confirmed_head_hash is None
    assert state_loaded.unconfirmed_head_number is None
    assert state_loaded.unconfirmed_head_hash is None
    assert state_loaded.n_channels == 0
    assert state_loaded.n_open_channels == 0
Exemple #8
0
def main(
        rpc_provider: HTTPProvider,
        private_key: str,
        private_key_password_file: str,
        state_file: str,
        channel_manager_address: str
):
    private_key = utils.get_private_key(private_key, private_key_password_file)
    if private_key is None:
        sys.exit(1)

    receiver_address = privkey_to_addr(private_key)

    if not state_file:
        state_file_name = "%s_%s.json" % (channel_manager_address[:10], receiver_address[:10])
        app_dir = click.get_app_dir('microraiden')
        state_file = os.path.join(app_dir, state_file_name)

    web3 = Web3(HTTPProvider(rpc_provider, request_kwargs={'timeout': 60}))
    web3.eth.defaultAccount = receiver_address
    channel_manager_address = (
        channel_manager_address or config.CHANNEL_MANAGER_ADDRESS[web3.version.network]
    )
    channel_manager_contract = make_channel_manager_contract(web3, channel_manager_address)

    try:
        click.echo('Loading state file from {}'.format(state_file))
        state = ChannelManagerState.load(state_file)
    except StateFileException:
        click.echo('Error reading state file')
        traceback.print_exc()
        sys.exit(1)
    if not is_same_address(state.receiver, receiver_address):
        click.echo('Private key does not match receiver address in state file')
        sys.exit(1)
    elif not is_same_address(state.contract_address, channel_manager_address):
        click.echo('Channel manager contract address mismatch')
        sys.exit(1)

    click.echo('Closing all open channels with valid balance proofs for '
               'receiver {}'.format(receiver_address))
    close_open_channels(private_key, state, channel_manager_contract)
Exemple #9
0
def test_creation(state):
    assert state.receiver == RECEIVER_ADDRESS
    assert state.contract_address == CONTRACT_ADDRESS
    assert state.network_id == NETWORK_ID

    assert state.confirmed_head_number is None
    assert state.confirmed_head_hash is None
    assert state.unconfirmed_head_number is None
    assert state.unconfirmed_head_hash is None
    assert state.n_channels == 0
    assert state.n_open_channels == 0

    state_loaded = ChannelManagerState.load(state.filename,
                                            check_permissions=False)
    assert state_loaded.confirmed_head_number is None
    assert state_loaded.confirmed_head_hash is None
    assert state_loaded.unconfirmed_head_number is None
    assert state_loaded.unconfirmed_head_hash is None
    assert state_loaded.n_channels == 0
    assert state_loaded.n_open_channels == 0
Exemple #10
0
def main(private_key, private_key_password_file, state_file,
         channel_manager_address):
    private_key = utils.get_private_key(private_key, private_key_password_file)
    if private_key is None:
        sys.exit(1)

    receiver_address = privkey_to_addr(private_key)
    channel_manager_address = channel_manager_address or config.CHANNEL_MANAGER_ADDRESS

    if not state_file:
        state_file_name = "%s_%s.json" % (channel_manager_address[:10],
                                          receiver_address[:10])
        app_dir = click.get_app_dir('microraiden')
        state_file = os.path.join(app_dir, state_file_name)

    web3 = Web3(config.WEB3_PROVIDER)
    web3.eth.defaultAccount = receiver_address
    contract_proxy = make_contract_proxy(web3, private_key,
                                         config.CHANNEL_MANAGER_ADDRESS)

    try:
        click.echo('Loading state file from {}'.format(state_file))
        state = ChannelManagerState.load(state_file)
    except StateFileException:
        click.echo('Error reading state file')
        traceback.print_exc()
        sys.exit(1)
    if not is_same_address(state.receiver, receiver_address):
        click.echo('Private key does not match receiver address in state file')
        sys.exit(1)
    elif not is_same_address(state.contract_address, channel_manager_address):
        click.echo('Channel manager contract address mismatch')
        sys.exit(1)

    click.echo('Closing all open channels with valid balance proofs for '
               'receiver {}'.format(receiver_address))
    close_open_channels(state, contract_proxy)
Exemple #11
0
def state(tmpdir):
    db = tmpdir.join("state.db")
    state = ChannelManagerState(db.strpath)
    state.setup_db(NETWORK_ID, CONTRACT_ADDRESS, RECEIVER_ADDRESS)
    return state