예제 #1
0
 def _collect_internal(self) -> None:
     nucypher_worker_token_actor = NucypherTokenActor(
         self.contract_registry, checksum_address=self.worker_address)
     self.metrics["worker_eth_balance_gauge"].set(
         nucypher_worker_token_actor.eth_balance)
     self.metrics["worker_token_balance_gauge"].set(
         int(nucypher_worker_token_actor.token_balance))
예제 #2
0
    def _collect_internal(self) -> None:
        staking_agent = ContractAgency.get_agent(
            StakingEscrowAgent, registry=self.contract_registry)

        # current period
        self.metrics["current_period_gauge"].set(
            staking_agent.get_current_period())

        # balances
        nucypher_token_actor = NucypherTokenActor(
            self.contract_registry, checksum_address=self.staker_address)
        self.metrics["eth_balance_gauge"].set(nucypher_token_actor.eth_balance)
        self.metrics["token_balance_gauge"].set(
            int(nucypher_token_actor.token_balance))

        # stake information
        self.metrics["substakes_count_gauge"].set(
            staking_agent.contract.functions.getSubStakesLength(
                self.staker_address).call())

        locked = staking_agent.get_locked_tokens(
            staker_address=self.staker_address, periods=1)
        self.metrics["active_stake_gauge"].set(locked)

        owned_tokens = staking_agent.owned_tokens(self.staker_address)
        unlocked_tokens = owned_tokens - locked
        self.metrics["unlocked_tokens_gauge"].set(unlocked_tokens)
        self.metrics["owned_tokens_gauge"].set(owned_tokens)

        # missed commitments
        missing_commitments = staking_agent.get_missing_commitments(
            checksum_address=self.staker_address)
        self.metrics["missing_commitments_gauge"].set(missing_commitments)
def test_adjudicator_slashes(
        agency,
        testerchain,
        #mock_ursula_reencrypts,
        token_economics,
        test_registry,
        mocker):

    staker_account = testerchain.staker_account(0)
    worker_account = testerchain.ursula_account(0)

    ##### STAKING ESCROW STUFF #####

    token_agent = ContractAgency.get_agent(NucypherTokenAgent,
                                           registry=test_registry)
    staking_agent = ContractAgency.get_agent(StakingEscrowAgent,
                                             registry=test_registry)

    locked_tokens = token_economics.minimum_allowed_locked * 5

    # The staker receives an initial amount of tokens
    tpower = TransactingPower(account=testerchain.etherbase_account,
                              signer=Web3Signer(testerchain.client))
    _txhash = token_agent.transfer(amount=locked_tokens,
                                   target_address=staker_account,
                                   transacting_power=tpower)

    # Deposit: The staker deposits tokens in the StakingEscrow contract.
    tpower = TransactingPower(account=staker_account,
                              signer=Web3Signer(testerchain.client))
    staker = Staker(domain=TEMPORARY_DOMAIN,
                    registry=test_registry,
                    transacting_power=tpower)

    staker.initialize_stake(
        amount=NU(locked_tokens, 'NuNit'),
        lock_periods=token_economics.minimum_locked_periods)
    assert staker.locked_tokens(periods=1) == locked_tokens

    # The staker hasn't bond a worker yet
    assert NULL_ADDRESS == staking_agent.get_worker_from_staker(
        staker_address=staker_account)

    _txhash = staking_agent.bond_worker(transacting_power=tpower,
                                        worker_address=worker_account)

    assert worker_account == staking_agent.get_worker_from_staker(
        staker_address=staker_account)
    assert staker_account == staking_agent.get_staker_from_worker(
        worker_address=worker_account)

    ###### END OF STAKING ESCROW STUFF ####

    adjudicator_agent = AdjudicatorAgent(registry=test_registry)
    bob_account = testerchain.bob_account
    bobby = NucypherTokenActor(checksum_address=bob_account,
                               domain=TEMPORARY_DOMAIN,
                               registry=test_registry)
    ursula = mock_ursula(testerchain, worker_account, mocker=mocker)

    # Let's create a bad cfrag
    evidence = mock_ursula_reencrypts(ursula, corrupt_cfrag=True)

    assert not adjudicator_agent.was_this_evidence_evaluated(evidence)
    bobby_old_balance = bobby.token_balance
    bob_tpower = TransactingPower(account=bob_account,
                                  signer=Web3Signer(testerchain.client))

    adjudicator_agent.evaluate_cfrag(evidence=evidence,
                                     transacting_power=bob_tpower)

    assert adjudicator_agent.was_this_evidence_evaluated(evidence)
    investigator_reward = bobby.token_balance - bobby_old_balance

    assert investigator_reward > 0
    assert investigator_reward == token_economics.base_penalty / token_economics.reward_coefficient
    assert staker.locked_tokens(periods=1) < locked_tokens
예제 #4
0
def test_adjudicator_slashes(agency, testerchain, mock_ursula_reencrypts,
                             token_economics, test_registry, mocker):

    staker_account = testerchain.staker_account(0)
    worker_account = testerchain.ursula_account(0)

    ##### STAKING ESCROW STUFF #####

    token_agent, staking_agent, _policy_agent = agency

    locked_tokens = token_economics.minimum_allowed_locked * 5

    # Mock Powerup consumption (Deployer)
    testerchain.transacting_power = TransactingPower(
        password=INSECURE_DEVELOPMENT_PASSWORD,
        account=testerchain.etherbase_account)
    testerchain.transacting_power.activate()

    # The staker receives an initial amount of tokens
    _txhash = token_agent.transfer(
        amount=locked_tokens,
        target_address=staker_account,
        sender_address=testerchain.etherbase_account)

    # Mock Powerup consumption (Staker)
    testerchain.transacting_power = TransactingPower(
        password=INSECURE_DEVELOPMENT_PASSWORD, account=staker_account)
    testerchain.transacting_power.activate()

    # Deposit: The staker deposits tokens in the StakingEscrow contract.
    staker = Staker(checksum_address=staker_account,
                    is_me=True,
                    registry=test_registry)
    staker.initialize_stake(
        amount=NU(locked_tokens, 'NuNit'),
        lock_periods=token_economics.minimum_locked_periods)
    assert staker.locked_tokens(periods=1) == locked_tokens

    # The staker hasn't set a worker yet
    assert BlockchainInterface.NULL_ADDRESS == staking_agent.get_worker_from_staker(
        staker_address=staker_account)

    _txhash = staking_agent.set_worker(staker_address=staker_account,
                                       worker_address=worker_account)

    assert worker_account == staking_agent.get_worker_from_staker(
        staker_address=staker_account)
    assert staker_account == staking_agent.get_staker_from_worker(
        worker_address=worker_account)

    ###### END OF STAKING ESCROW STUFF ####

    adjudicator_agent = AdjudicatorAgent(registry=test_registry)
    bob_account = testerchain.bob_account
    bobby = NucypherTokenActor(checksum_address=bob_account,
                               registry=test_registry)
    ursula = mock_ursula(testerchain, worker_account, mocker=mocker)

    # Let's create a bad cfrag
    evidence = mock_ursula_reencrypts(ursula, corrupt_cfrag=True)

    assert not adjudicator_agent.was_this_evidence_evaluated(evidence)
    bobby_old_balance = bobby.token_balance

    # Mock Powerup consumption (Bob)
    testerchain.transacting_power = TransactingPower(
        password=INSECURE_DEVELOPMENT_PASSWORD, account=bob_account)
    testerchain.transacting_power.activate()

    adjudicator_agent.evaluate_cfrag(evidence=evidence,
                                     sender_address=bob_account)

    assert adjudicator_agent.was_this_evidence_evaluated(evidence)
    investigator_reward = bobby.token_balance - bobby_old_balance

    assert investigator_reward > 0
    assert investigator_reward == token_economics.base_penalty / token_economics.reward_coefficient
    assert staker.locked_tokens(periods=1) < locked_tokens
예제 #5
0
def collect_prometheus_metrics(
        ursula, event_metrics_collectors: List[BaseEventMetricsCollector],
        node_metrics: dict) -> None:
    base_payload = {
        'app_version': nucypher.__version__,
        'teacher_version': str(ursula.TEACHER_VERSION),
        'host': str(ursula.rest_interface),
        'domains': str(', '.join(ursula.learning_domains)),
        'fleet_state': str(ursula.known_nodes.checksum),
        'known_nodes': str(len(ursula.known_nodes))
    }

    node_metrics["learning_status"].state(
        'running' if ursula._learning_task.running else 'stopped')
    node_metrics["known_nodes_gauge"].set(len(ursula.known_nodes))
    node_metrics["work_orders_gauge"].set(len(ursula.work_orders()))

    if not ursula.federated_only:

        blockchain = BlockchainInterfaceFactory.get_or_create_interface(
            provider_uri=ursula.provider_uri)

        node_metrics["current_eth_block_number"].set(
            blockchain.client.block_number)

        nucypher_token_actor = NucypherTokenActor(
            ursula.registry, checksum_address=ursula.checksum_address)
        node_metrics["eth_balance_gauge"].set(nucypher_token_actor.eth_balance)
        node_metrics["token_balance_gauge"].set(
            int(nucypher_token_actor.token_balance))

        nucypher_worker_token_actor = NucypherTokenActor(
            ursula.registry, checksum_address=ursula.worker_address)
        node_metrics["worker_eth_balance_gauge"].set(
            nucypher_worker_token_actor.eth_balance)
        node_metrics["worker_token_balance_gauge"].set(
            int(nucypher_worker_token_actor.token_balance))

        for event_metrics_collector in event_metrics_collectors:
            event_metrics_collector.collect(node_metrics)

        staking_agent = ContractAgency.get_agent(StakingEscrowAgent,
                                                 registry=ursula.registry)
        worklock_agent = ContractAgency.get_agent(WorkLockAgent,
                                                  registry=ursula.registry)

        node_metrics["substakes_count_gauge"].set(
            staking_agent.contract.functions.getSubStakesLength(
                ursula.checksum_address).call())

        locked = staking_agent.get_locked_tokens(
            staker_address=ursula.checksum_address, periods=1)

        node_metrics["active_stake_gauge"].set(locked)

        owned_tokens = staking_agent.owned_tokens(ursula.checksum_address)

        unlocked_tokens = owned_tokens - locked

        node_metrics["unlocked_tokens_gauge"].set(unlocked_tokens)

        node_metrics["owned_tokens_gauge"].set(owned_tokens)

        node_metrics["available_refund_gauge"].set(
            worklock_agent.get_available_refund(
                checksum_address=ursula.checksum_address))

        node_metrics["worklock_remaining_work_gauge"].set(
            worklock_agent.get_remaining_work(
                checksum_address=ursula.checksum_address))

        node_metrics["worklock_refund_completed_work_gauge"].set(
            staking_agent.get_completed_work(
                bidder_address=ursula.checksum_address) -
            worklock_agent.get_refunded_work(
                checksum_address=ursula.checksum_address))

        node_metrics["policies_held_gauge"].set(
            len(ursula.datastore.get_all_policy_arrangements()))

        node_metrics["current_period_gauge"].set(
            staking_agent.get_current_period())

        missing_commitments = staking_agent.get_missing_commitments(
            staker_address=ursula.checksum_address)  # TODO: lol
        node_metrics["missing_commitments_gauge"].set(missing_commitments)

        decentralized_payload = {
            'provider': str(ursula.provider_uri),
            'active_stake': str(locked),
            'missing_commitments': str(missing_commitments)
        }

        base_payload.update(decentralized_payload)

    node_metrics["host_info"].info(base_payload)