[KMS-ETH]- Rename Escrow to MinerEscrow.

pull/195/head^2
Kieran Prasch 2018-03-06 14:30:07 -08:00
parent 9bacbab86c
commit 1e2d277d59
7 changed files with 32 additions and 34 deletions

View File

@ -10,7 +10,7 @@ from .blockchain import Blockchain
addr = str addr = str
class Escrow: class MinerEscrow:
""" """
Wraps NuCypher's Escrow solidity smart contract, and manages a PopulusContract. 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.""" """Gateway to contract function calls without state change."""
return self._contract.call() 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.""" """If two deployed escrows have the same contract address, they are equal."""
return self._contract.address == other._contract.address return self._contract.address == other._contract.address
@ -117,7 +117,7 @@ class Escrow:
return deploy_txhash, reward_txhash, init_txhash return deploy_txhash, reward_txhash, init_txhash
@classmethod @classmethod
def get(cls, blockchain, token) -> 'Escrow': def get(cls, blockchain, token) -> 'MinerEscrow':
""" """
Returns the Escrow object, Returns the Escrow object,
or raises UnknownContract if the contract has not been deployed. or raises UnknownContract if the contract has not been deployed.

View File

@ -1,6 +1,6 @@
from typing import Tuple from typing import Tuple
from .escrow import Escrow from .escrow import MinerEscrow
class Miner: 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 self.escrow = escrow
if not escrow._contract: 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: else:
escrow.miners.append(self) escrow.miners.append(self)

View File

@ -1,14 +1,14 @@
from collections import OrderedDict from collections import OrderedDict
from typing import Tuple, List 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.miner import Miner
from nkms_eth.token import NuCypherKMSToken from nkms_eth.token import NuCypherKMSToken
class PolicyArrangement: class PolicyArrangement:
def __init__(self, author: 'PolicyAuthor', miner: 'Miner', value: int=None, def __init__(self, author: 'PolicyAuthor', miner: 'Miner', value: int,
periods: int=None, rate: int=None, arrangement_id: bytes=None): periods: int, arrangement_id: bytes=None):
if arrangement_id is None: if arrangement_id is None:
self.id = self.__class__._generate_arrangement_id() # TODO: Generate policy ID self.id = self.__class__._generate_arrangement_id() # TODO: Generate policy ID
@ -20,16 +20,14 @@ class PolicyArrangement:
self.miner = miner self.miner = miner
# Arrangement value, rate, and duration # Arrangement value, rate, and duration
if (value and periods) and (not rate): rate = value // periods
rate = value // periods
self._rate = rate self._rate = rate
self.value = value self.value = value
self.periods = periods # TODO: datetime -> duration in blocks self.periods = periods # TODO: datetime -> duration in blocks
self.is_published = False self.is_published = False
self._elapsed_periods = None
self.publish_transaction = None # TX hashes set when published to network
self.revoke_transaction = None
@staticmethod @staticmethod
def _generate_arrangement_id(policy_hrac: bytes) -> bytes: def _generate_arrangement_id(policy_hrac: bytes) -> bytes:
@ -75,7 +73,7 @@ class PolicyManager:
class ContractDeploymentError(Exception): class ContractDeploymentError(Exception):
pass pass
def __init__(self, escrow: Escrow): def __init__(self, escrow: MinerEscrow):
self.escrow = escrow self.escrow = escrow
self.token = escrow.token self.token = escrow.token
self.blockchain = self.token.blockchain self.blockchain = self.token.blockchain
@ -96,7 +94,7 @@ class PolicyManager:
if self.is_deployed is True: if self.is_deployed is True:
raise PolicyManager.ContractDeploymentError('PolicyManager contract already deployed') raise PolicyManager.ContractDeploymentError('PolicyManager contract already deployed')
if self.escrow._contract is None: 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: if self.token.contract is None:
raise NuCypherKMSToken.ContractDeploymentError('Token contract must be deployed before') raise NuCypherKMSToken.ContractDeploymentError('Token contract must be deployed before')
@ -117,7 +115,7 @@ class PolicyManager:
return self._contract.call() return self._contract.call()
@classmethod @classmethod
def get(cls, escrow: Escrow) -> 'PolicyManager': def get(cls, escrow: MinerEscrow) -> 'PolicyManager':
contract = escrow.blockchain._chain.provider.get_contract(cls.__contract_name) contract = escrow.blockchain._chain.provider.get_contract(cls.__contract_name)
instance = cls(escrow) instance = cls(escrow)
instance._contract = contract instance._contract = contract
@ -133,8 +131,7 @@ class PolicyManager:
def revoke_arrangement(self, arrangement_id: bytes, author: 'PolicyAuthor', gas_price: int): def revoke_arrangement(self, arrangement_id: bytes, author: 'PolicyAuthor', gas_price: int):
""" """
Revoke by arrangement ID. Revoke by arrangement ID; Only the policy author can revoke the policy
Only the policy author can revoke the policy
""" """
txhash = self.transact({'from': author.address, 'gas_price': gas_price}).revokePolicy(arrangement_id) txhash = self.transact({'from': author.address, 'gas_price': gas_price}).revokePolicy(arrangement_id)
self.blockchain._chain.wait.for_receipt(txhash) self.blockchain._chain.wait.for_receipt(txhash)
@ -172,12 +169,13 @@ class PolicyAuthor:
"""Fetch a published arrangement from the blockchain""" """Fetch a published arrangement from the blockchain"""
blockchain_record = self.policy_manager().policies(arrangement_id) 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) duration = end_block - start_block
arrangement = PolicyArrangement(author=self, miner=miner, rate=rate)
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 arrangement.is_published = True
return arrangement return arrangement

View File

@ -2,7 +2,7 @@ import pytest
from nkms_eth.blockchain import Blockchain from nkms_eth.blockchain import Blockchain
from nkms_eth.token import NuCypherKMSToken from nkms_eth.token import NuCypherKMSToken
from tests.utilities import TesterBlockchain, MockEscrow from tests.utilities import TesterBlockchain, MockMinerEscrow
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@ -23,7 +23,7 @@ def token(testerchain):
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def escrow(testerchain, token): def escrow(testerchain, token):
escrow = MockEscrow(blockchain=testerchain, token=token) escrow = MockMinerEscrow(blockchain=testerchain, token=token)
escrow.arm() escrow.arm()
escrow.deploy() escrow.deploy()
yield escrow yield escrow

View File

@ -4,7 +4,7 @@ import pytest
from populus.contracts.exceptions import NoKnownAddress from populus.contracts.exceptions import NoKnownAddress
from pytest import raises 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.miner import Miner
from nkms_eth.token import NuCypherKMSToken from nkms_eth.token import NuCypherKMSToken
@ -28,14 +28,14 @@ def test_create_escrow(testerchain):
assert token._contract.address == same_token._contract.address assert token._contract.address == same_token._contract.address
with raises(NoKnownAddress): 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.arm()
escrow.deploy() escrow.deploy()
same_escrow = Escrow.get(blockchain=testerchain, token=token) same_escrow = MinerEscrow.get(blockchain=testerchain, token=token)
with raises(Escrow.ContractDeploymentError): with raises(MinerEscrow.ContractDeploymentError):
same_escrow.arm() same_escrow.arm()
same_escrow.deploy() same_escrow.deploy()

View File

@ -3,7 +3,7 @@ import random
import os import os
import pytest import pytest
from nkms_eth.escrow import Escrow from nkms_eth.escrow import MinerEscrow
from nkms_eth.miner import Miner from nkms_eth.miner import Miner
from nkms_eth.token import NuCypherKMSToken from nkms_eth.token import NuCypherKMSToken
@ -89,5 +89,5 @@ def test_select_ursulas(testerchain, token, escrow):
assert len(miners) == 3 assert len(miners) == 3
assert len(set(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 escrow.sample(quantity=100) # Waay more than we have deployed

View File

@ -1,5 +1,5 @@
from nkms_eth.blockchain import Blockchain from nkms_eth.blockchain import Blockchain
from nkms_eth.escrow import Escrow from nkms_eth.escrow import MinerEscrow
class TesterBlockchain(Blockchain): class TesterBlockchain(Blockchain):
@ -7,7 +7,7 @@ class TesterBlockchain(Blockchain):
_network = 'tester' _network = 'tester'
class MockEscrow(Escrow): class MockMinerEscrow(MinerEscrow):
"""Speed things up a bit""" """Speed things up a bit"""
hours_per_period = 1 hours_per_period = 1
min_release_periods = 1 min_release_periods = 1