示例#1
0
    def get(signer,
            channel_identifier,
            reward_amount,
            token_network_address,
            nonce=0,
            v=27):
        private_key = get_private_key(signer)

        signature = sign_reward_proof(
            private_key,
            channel_identifier,
            reward_amount,
            token_network_address,
            int(token_network.functions.chain_id().call()),
            nonce,
            v,
        )
        return (
            channel_identifier,
            reward_amount,
            token_network_address,
            int(token_network.functions.chain_id().call()),
            nonce,
            signature,
        )
    def get(
        signer: HexAddress,
        channel_identifier: int,
        reward_amount: int,
        token_network_address: HexAddress,
        monitoring_service_contract_address: HexAddress,
        nonce: int = 0,
        v: int = 27,
    ) -> Tuple[int, int, HexAddress, int, int, bytes]:
        private_key = get_private_key(signer)

        signature = sign_reward_proof(
            private_key,
            channel_identifier,
            monitoring_service_contract_address,
            reward_amount,
            token_network_address,
            int(token_network.functions.chain_id().call()),
            nonce,
            v,
        )
        return (
            channel_identifier,
            reward_amount,
            token_network_address,
            int(token_network.functions.chain_id().call()),
            nonce,
            signature,
        )
    def f(monitoring_service_contract: Contract) -> Dict:
        # Create two parties and a channel between them
        (A, B) = get_accounts(2, privkeys=["0x" + "1" * 64, "0x" + "2" * 64])
        deposit_to_udc(B, REWARD_AMOUNT)
        channel_identifier = create_channel(A, B)[0]

        # Create balance proofs
        balance_proof_A = create_balance_proof(channel_identifier,
                                               B,
                                               transferred_amount=10,
                                               nonce=1)
        balance_proof_B = create_balance_proof(channel_identifier,
                                               A,
                                               transferred_amount=20,
                                               nonce=2)

        # Add signatures by non_closing_participant
        closing_signature_A = create_balance_proof_countersignature(
            A, channel_identifier, MessageTypeId.BALANCE_PROOF,
            *balance_proof_A)
        non_closing_signature_B = create_balance_proof_countersignature(
            B, channel_identifier, MessageTypeId.BALANCE_PROOF_UPDATE,
            *balance_proof_B)
        reward_proof_signature = sign_reward_proof(
            privatekey=get_private_key(B),
            monitoring_service_contract_address=monitoring_service_contract.
            address,
            chain_id=token_network.functions.chain_id().call(),
            non_closing_signature=non_closing_signature_B,
            reward_amount=REWARD_AMOUNT,
        )

        # close channel
        token_network.functions.closeChannel(
            channel_identifier, B, A, *balance_proof_A,
            closing_signature_A).call_and_transact({"from": A})

        # calculate when this MS is allowed to monitor
        (settle_block_number,
         _) = token_network.functions.getChannelInfo(channel_identifier, A,
                                                     B).call()
        first_allowed = monitoring_service_contract.functions.firstBlockAllowedToMonitor(
            closed_at_block=settle_block_number - TEST_SETTLE_TIMEOUT_MIN,
            settle_timeout=TEST_SETTLE_TIMEOUT_MIN,
            participant1=A,
            participant2=B,
            monitoring_service_address=ms_address,
        ).call()

        # return args for `monitor` function
        return {
            "participants": (A, B),
            "balance_proof_A": balance_proof_A,
            "balance_proof_B": balance_proof_B,
            "non_closing_signature": non_closing_signature_B,
            "reward_proof_signature": reward_proof_signature,
            "channel_identifier": channel_identifier,
            "first_allowed": first_allowed,
        }
示例#4
0
def print_gas_monitoring_service(
    token_network: Contract,
    monitoring_service_external: Contract,
    get_accounts: Callable,
    create_channel: Callable,
    create_balance_proof: Callable,
    create_balance_proof_countersignature: Callable,
    service_registry: Contract,
    custom_token: Contract,
    deposit_to_udc: Callable,
    print_gas: Callable,
    get_private_key: Callable,
    create_service_account: Callable,
    web3: Web3,
) -> None:
    """Abusing pytest to print gas cost of MonitoringService functions"""
    # setup: two parties + MS
    (A, MS) = get_accounts(2)
    B = create_service_account()
    reward_amount = TokenAmount(10)
    deposit_to_udc(B, reward_amount)

    # register MS in the ServiceRegistry contract
    call_and_transact(custom_token.functions.mint(SERVICE_DEPOSIT * 2),
                      {"from": MS})
    call_and_transact(
        custom_token.functions.approve(service_registry.address,
                                       SERVICE_DEPOSIT),
        {"from": MS},
    )
    call_and_transact(service_registry.functions.deposit(SERVICE_DEPOSIT),
                      {"from": MS})

    # open a channel (c1, c2)
    channel_identifier = create_channel(A, B)[0]

    # create balance and reward proofs
    balance_proof_A = create_balance_proof(channel_identifier,
                                           B,
                                           transferred_amount=10,
                                           nonce=1)
    closing_sig_A = create_balance_proof_countersignature(
        participant=A,
        channel_identifier=channel_identifier,
        msg_type=MessageTypeId.BALANCE_PROOF,
        **balance_proof_A._asdict(),
    )
    balance_proof_B = create_balance_proof(channel_identifier,
                                           A,
                                           transferred_amount=20,
                                           nonce=2)
    non_closing_signature_B = create_balance_proof_countersignature(
        participant=B,
        channel_identifier=channel_identifier,
        msg_type=MessageTypeId.BALANCE_PROOF_UPDATE,
        **balance_proof_B._asdict(),
    )
    reward_proof_signature = sign_reward_proof(
        privatekey=get_private_key(B),
        monitoring_service_contract_address=monitoring_service_external.
        address,
        chain_id=token_network.functions.chain_id().call(),
        token_network_address=token_network.address,
        non_closing_participant=B,
        reward_amount=reward_amount,
        non_closing_signature=non_closing_signature_B,
    )

    # c1 closes channel
    call_and_transact(
        token_network.functions.closeChannel(
            channel_identifier, B, A,
            *balance_proof_A._asdict().values(), closing_sig_A),
        {"from": A},
    )
    mine_blocks(web3, 4)

    # MS calls `MSC::monitor()` using c1's BP and reward proof
    txn_hash = call_and_transact(
        monitoring_service_external.functions.monitor(
            A,
            B,
            balance_proof_B.balance_hash,
            balance_proof_B.nonce,
            balance_proof_B.additional_hash,
            balance_proof_B.original_signature,
            non_closing_signature_B,  # non-closing signature
            reward_amount,
            token_network.address,  # token network address
            reward_proof_signature,
        ),
        {"from": MS},
    )
    print_gas(txn_hash, CONTRACT_MONITORING_SERVICE + ".monitor")

    mine_blocks(web3, 1)

    # MS claims the reward
    txn_hash = call_and_transact(
        monitoring_service_external.functions.claimReward(
            channel_identifier, token_network.address, A, B),
        {"from": MS},
    )
    print_gas(txn_hash, CONTRACT_MONITORING_SERVICE + ".claimReward")
示例#5
0
    def f(monitoring_service_contract: Contract) -> Dict:
        # Create two parties and a channel between them
        (A, B) = get_accounts(2, privkeys=[bytes([1] * 32), bytes([2] * 32)])
        deposit_to_udc(B, REWARD_AMOUNT)
        channel_identifier = create_channel(A, B)[0]

        # Create balance proofs
        balance_proof_A = create_balance_proof(
            channel_identifier, B, transferred_amount=10, nonce=1
        )
        balance_proof_B = create_balance_proof(
            channel_identifier, A, transferred_amount=20, nonce=2
        )

        # Add signatures by non_closing_participant
        closing_signature_A = create_balance_proof_countersignature(
            participant=A,
            channel_identifier=channel_identifier,
            msg_type=MessageTypeId.BALANCE_PROOF,
            **balance_proof_A._asdict(),
        )
        non_closing_signature_B = create_balance_proof_countersignature(
            participant=B,
            channel_identifier=channel_identifier,
            msg_type=MessageTypeId.BALANCE_PROOF_UPDATE,
            **balance_proof_B._asdict(),
        )
        reward_proof_signature = sign_reward_proof(
            privatekey=get_private_key(B),
            monitoring_service_contract_address=monitoring_service_contract.address,
            chain_id=token_network.functions.chain_id().call(),
            token_network_address=token_network.address,
            non_closing_participant=B,
            non_closing_signature=non_closing_signature_B,
            reward_amount=REWARD_AMOUNT,
        )

        # close channel
        call_and_transact(
            token_network.functions.closeChannel(
                channel_identifier,
                B,
                A,
                *balance_proof_A._asdict().values(),
                closing_signature_A,
            ),
            {"from": A},
        )

        # calculate when this MS is allowed to monitor
        first_allowed = monitoring_service_contract.functions.firstBlockAllowedToMonitorChannel(
            token_network=token_network.address,
            channel_identifier=channel_identifier,
            closing_participant=A,
            non_closing_participant=B,
            monitoring_service_address=ms_address,
        ).call()

        # return args for `monitor` function
        return {
            "participants": (A, B),
            "balance_proof_A": balance_proof_A,
            "balance_proof_B": balance_proof_B,
            "non_closing_signature": non_closing_signature_B,
            "reward_proof_signature": reward_proof_signature,
            "channel_identifier": channel_identifier,
            "first_allowed": first_allowed,
        }
示例#6
0
def print_gas_monitoring_service(
    token_network: Contract,
    monitoring_service_external: Contract,
    get_accounts: Callable,
    create_channel: Callable,
    create_balance_proof: Callable,
    create_balance_proof_countersignature: Callable,
    service_registry: Contract,
    custom_token: Contract,
    deposit_to_udc: Callable,
    print_gas: Callable,
    get_private_key: Callable,
    create_service_account: Callable,
) -> None:
    """ Abusing pytest to print gas cost of MonitoringService functions """
    # setup: two parties + MS
    (A, MS) = get_accounts(2)
    B = create_service_account()
    reward_amount = 10
    deposit_to_udc(B, reward_amount)

    # register MS in the ServiceRegistry contract
    custom_token.functions.mint(SERVICE_DEPOSIT * 2).call_and_transact({"from": MS})
    custom_token.functions.approve(service_registry.address, SERVICE_DEPOSIT).call_and_transact(
        {"from": MS}
    )
    service_registry.functions.deposit(SERVICE_DEPOSIT).call_and_transact({"from": MS})

    # open a channel (c1, c2)
    channel_identifier = create_channel(A, B)[0]

    # create balance and reward proofs
    balance_proof_A = create_balance_proof(channel_identifier, B, transferred_amount=10, nonce=1)
    closing_sig_A = create_balance_proof_countersignature(
        A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_A
    )
    balance_proof_B = create_balance_proof(channel_identifier, A, transferred_amount=20, nonce=2)
    non_closing_signature_B = create_balance_proof_countersignature(
        B, channel_identifier, MessageTypeId.BALANCE_PROOF_UPDATE, *balance_proof_B
    )
    reward_proof_signature = sign_reward_proof(
        privatekey=get_private_key(B),
        monitoring_service_contract_address=monitoring_service_external.address,
        chain_id=token_network.functions.chain_id().call(),
        reward_amount=reward_amount,
        non_closing_signature=non_closing_signature_B,
    )

    # c1 closes channel
    txn_hash = token_network.functions.closeChannel(
        channel_identifier, B, A, *balance_proof_A, closing_sig_A
    ).call_and_transact({"from": A})
    token_network.web3.testing.mine(4)

    # MS calls `MSC::monitor()` using c1's BP and reward proof
    txn_hash = monitoring_service_external.functions.monitor(
        A,
        B,
        balance_proof_B[0],  # balance_hash
        balance_proof_B[1],  # nonce
        balance_proof_B[2],  # additional_hash
        balance_proof_B[3],  # closing signature
        non_closing_signature_B,  # non-closing signature
        reward_amount,
        token_network.address,  # token network address
        reward_proof_signature,
    ).call_and_transact({"from": MS})
    print_gas(txn_hash, CONTRACT_MONITORING_SERVICE + ".monitor")

    token_network.web3.testing.mine(1)

    # MS claims the reward
    txn_hash = monitoring_service_external.functions.claimReward(
        channel_identifier, token_network.address, A, B
    ).call_and_transact({"from": MS})
    print_gas(txn_hash, CONTRACT_MONITORING_SERVICE + ".claimReward")