From 1e2d277d59941dfe628efc10488f81b3927ff7b7 Mon Sep 17 00:00:00 2001 From: Kieran Prasch Date: Tue, 6 Mar 2018 14:30:07 -0800 Subject: [PATCH] [KMS-ETH]- Rename Escrow to MinerEscrow. --- nkms_eth/escrow.py | 6 +++--- nkms_eth/miner.py | 6 +++--- nkms_eth/policies.py | 32 +++++++++++++++----------------- tests/conftest.py | 4 ++-- tests/entities/test_escrow.py | 10 +++++----- tests/entities/test_miner.py | 4 ++-- tests/utilities.py | 4 ++-- 7 files changed, 32 insertions(+), 34 deletions(-) diff --git a/nkms_eth/escrow.py b/nkms_eth/escrow.py index 95a467612..2a4c9bd03 100644 --- a/nkms_eth/escrow.py +++ b/nkms_eth/escrow.py @@ -10,7 +10,7 @@ from .blockchain import Blockchain addr = str -class Escrow: +class MinerEscrow: """ Wraps NuCypher's Escrow solidity smart contract, and manages a PopulusContract. @@ -75,7 +75,7 @@ class Escrow: """Gateway to contract function calls without state change.""" return self._contract.call() - def __eq__(self, other: 'Escrow'): + def __eq__(self, other: 'MinerEscrow'): """If two deployed escrows have the same contract address, they are equal.""" return self._contract.address == other._contract.address @@ -117,7 +117,7 @@ class Escrow: return deploy_txhash, reward_txhash, init_txhash @classmethod - def get(cls, blockchain, token) -> 'Escrow': + def get(cls, blockchain, token) -> 'MinerEscrow': """ Returns the Escrow object, or raises UnknownContract if the contract has not been deployed. diff --git a/nkms_eth/miner.py b/nkms_eth/miner.py index 7d1271bce..243b58c31 100644 --- a/nkms_eth/miner.py +++ b/nkms_eth/miner.py @@ -1,6 +1,6 @@ from typing import Tuple -from .escrow import Escrow +from .escrow import MinerEscrow class Miner: @@ -14,11 +14,11 @@ class Miner: """ - def __init__(self, escrow: Escrow, address=None): + def __init__(self, escrow: MinerEscrow, address=None): self.escrow = escrow if not escrow._contract: - raise Escrow.ContractDeploymentError('Escrow contract not deployed. Arm then deploy.') + raise MinerEscrow.ContractDeploymentError('Escrow contract not deployed. Arm then deploy.') else: escrow.miners.append(self) diff --git a/nkms_eth/policies.py b/nkms_eth/policies.py index 82177c505..f14c44824 100644 --- a/nkms_eth/policies.py +++ b/nkms_eth/policies.py @@ -1,14 +1,14 @@ from collections import OrderedDict from typing import Tuple, List -from nkms_eth.escrow import Escrow +from nkms_eth.escrow import MinerEscrow from nkms_eth.miner import Miner from nkms_eth.token import NuCypherKMSToken class PolicyArrangement: - def __init__(self, author: 'PolicyAuthor', miner: 'Miner', value: int=None, - periods: int=None, rate: int=None, arrangement_id: bytes=None): + def __init__(self, author: 'PolicyAuthor', miner: 'Miner', value: int, + periods: int, arrangement_id: bytes=None): if arrangement_id is None: self.id = self.__class__._generate_arrangement_id() # TODO: Generate policy ID @@ -20,16 +20,14 @@ class PolicyArrangement: self.miner = miner # Arrangement value, rate, and duration - if (value and periods) and (not rate): - rate = value // periods + rate = value // periods self._rate = rate + self.value = value self.periods = periods # TODO: datetime -> duration in blocks self.is_published = False - self._elapsed_periods = None - self.publish_transaction = None # TX hashes set when published to network - self.revoke_transaction = None + @staticmethod def _generate_arrangement_id(policy_hrac: bytes) -> bytes: @@ -75,7 +73,7 @@ class PolicyManager: class ContractDeploymentError(Exception): pass - def __init__(self, escrow: Escrow): + def __init__(self, escrow: MinerEscrow): self.escrow = escrow self.token = escrow.token self.blockchain = self.token.blockchain @@ -96,7 +94,7 @@ class PolicyManager: if self.is_deployed is True: raise PolicyManager.ContractDeploymentError('PolicyManager contract already deployed') if self.escrow._contract is None: - raise Escrow.ContractDeploymentError('Escrow contract must be deployed before') + raise MinerEscrow.ContractDeploymentError('Escrow contract must be deployed before') if self.token.contract is None: raise NuCypherKMSToken.ContractDeploymentError('Token contract must be deployed before') @@ -117,7 +115,7 @@ class PolicyManager: return self._contract.call() @classmethod - def get(cls, escrow: Escrow) -> 'PolicyManager': + def get(cls, escrow: MinerEscrow) -> 'PolicyManager': contract = escrow.blockchain._chain.provider.get_contract(cls.__contract_name) instance = cls(escrow) instance._contract = contract @@ -133,8 +131,7 @@ class PolicyManager: def revoke_arrangement(self, arrangement_id: bytes, author: 'PolicyAuthor', gas_price: int): """ - Revoke by arrangement ID. - Only the policy author can revoke the policy + Revoke by arrangement ID; Only the policy author can revoke the policy """ txhash = self.transact({'from': author.address, 'gas_price': gas_price}).revokePolicy(arrangement_id) self.blockchain._chain.wait.for_receipt(txhash) @@ -172,12 +169,13 @@ class PolicyAuthor: """Fetch a published arrangement from the blockchain""" blockchain_record = self.policy_manager().policies(arrangement_id) - author_address, miner_address, rate, *periods = blockchain_record + author_address, miner_address, rate, start_block, end_block, downtime_index = blockchain_record - miner = Miner.from_address(miner_address) - arrangement = PolicyArrangement(author=self, miner=miner, rate=rate) + duration = end_block - start_block + + miner = Miner(address=miner_address, escrow=self.policy_manager.escrow) + arrangement = PolicyArrangement(author=self, miner=miner, periods=duration, rate=rate) - arrangement._elapsed_periods = periods arrangement.is_published = True return arrangement diff --git a/tests/conftest.py b/tests/conftest.py index 777654876..02ab5ad6a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,7 +2,7 @@ import pytest from nkms_eth.blockchain import Blockchain from nkms_eth.token import NuCypherKMSToken -from tests.utilities import TesterBlockchain, MockEscrow +from tests.utilities import TesterBlockchain, MockMinerEscrow @pytest.fixture(scope='function') @@ -23,7 +23,7 @@ def token(testerchain): @pytest.fixture(scope='function') def escrow(testerchain, token): - escrow = MockEscrow(blockchain=testerchain, token=token) + escrow = MockMinerEscrow(blockchain=testerchain, token=token) escrow.arm() escrow.deploy() yield escrow \ No newline at end of file diff --git a/tests/entities/test_escrow.py b/tests/entities/test_escrow.py index 9b7b4867b..d939edb31 100644 --- a/tests/entities/test_escrow.py +++ b/tests/entities/test_escrow.py @@ -4,7 +4,7 @@ import pytest from populus.contracts.exceptions import NoKnownAddress from pytest import raises -from nkms_eth.escrow import Escrow +from nkms_eth.escrow import MinerEscrow from nkms_eth.miner import Miner from nkms_eth.token import NuCypherKMSToken @@ -28,14 +28,14 @@ def test_create_escrow(testerchain): assert token._contract.address == same_token._contract.address with raises(NoKnownAddress): - Escrow.get(blockchain=testerchain, token=token) + MinerEscrow.get(blockchain=testerchain, token=token) - escrow = Escrow(blockchain=testerchain, token=token) + escrow = MinerEscrow(blockchain=testerchain, token=token) escrow.arm() escrow.deploy() - same_escrow = Escrow.get(blockchain=testerchain, token=token) - with raises(Escrow.ContractDeploymentError): + same_escrow = MinerEscrow.get(blockchain=testerchain, token=token) + with raises(MinerEscrow.ContractDeploymentError): same_escrow.arm() same_escrow.deploy() diff --git a/tests/entities/test_miner.py b/tests/entities/test_miner.py index 2a5a45305..25ba07a57 100644 --- a/tests/entities/test_miner.py +++ b/tests/entities/test_miner.py @@ -3,7 +3,7 @@ import random import os import pytest -from nkms_eth.escrow import Escrow +from nkms_eth.escrow import MinerEscrow from nkms_eth.miner import Miner from nkms_eth.token import NuCypherKMSToken @@ -89,5 +89,5 @@ def test_select_ursulas(testerchain, token, escrow): assert len(miners) == 3 assert len(set(miners)) == 3 - with pytest.raises(Escrow.NotEnoughUrsulas): + with pytest.raises(MinerEscrow.NotEnoughUrsulas): escrow.sample(quantity=100) # Waay more than we have deployed diff --git a/tests/utilities.py b/tests/utilities.py index e7201058f..45efc06bd 100644 --- a/tests/utilities.py +++ b/tests/utilities.py @@ -1,5 +1,5 @@ from nkms_eth.blockchain import Blockchain -from nkms_eth.escrow import Escrow +from nkms_eth.escrow import MinerEscrow class TesterBlockchain(Blockchain): @@ -7,7 +7,7 @@ class TesterBlockchain(Blockchain): _network = 'tester' -class MockEscrow(Escrow): +class MockMinerEscrow(MinerEscrow): """Speed things up a bit""" hours_per_period = 1 min_release_periods = 1