mirror of https://github.com/nucypher/nucypher.git
[KMS-ETH]- Miner class and methods, imagined as an Ursula mixin class.
parent
56bc161ad1
commit
1bbe6d0f8e
|
@ -1,3 +1,5 @@
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
from .blockchain import Blockchain
|
from .blockchain import Blockchain
|
||||||
from .escrow import Escrow
|
from .escrow import Escrow
|
||||||
from .token import NuCypherKMSToken
|
from .token import NuCypherKMSToken
|
||||||
|
@ -6,40 +8,61 @@ from .token import NuCypherKMSToken
|
||||||
class Miner:
|
class Miner:
|
||||||
"""Practically carrying a pickaxe"""
|
"""Practically carrying a pickaxe"""
|
||||||
|
|
||||||
def __init__(self, blockchain: Blockchain, token: NuCypherKMSToken, escrow: Escrow):
|
def __init__(self, blockchain: Blockchain, token: NuCypherKMSToken, escrow: Escrow, address=None):
|
||||||
self.blockchain = blockchain
|
self.blockchain = blockchain
|
||||||
self.escrow = escrow
|
|
||||||
self.token = token
|
self.token = token
|
||||||
|
self.address = address or self.token.creator
|
||||||
|
|
||||||
def lock(self, amount: int, locktime: int, address: str=None):
|
self.escrow = escrow
|
||||||
"""
|
if not escrow.contract:
|
||||||
Deposit and lock coins for mining.
|
raise Exception('Escrow contract not deployed')
|
||||||
Creating coins starts after it is done.
|
else:
|
||||||
|
escrow.miners.append(self)
|
||||||
|
|
||||||
:param amount: Amount of coins to lock (in smallest indivisible units)
|
def __del__(self):
|
||||||
:param locktime: Locktime in periods
|
self.escrow.miners.remove(self)
|
||||||
:param address: Optional address to get coins from (accounts[0] by default)
|
return None
|
||||||
"""
|
|
||||||
|
|
||||||
address = address or self.token.creator
|
def lock(self, amount: int, locktime: int) -> Tuple[str, str, str]:
|
||||||
|
"""Deposit and lock tokens for mining."""
|
||||||
|
|
||||||
tx = self.token.transact({'from': address}).approve(self.escrow.contract.address, amount)
|
approve_txhash = self.token.transact({'from': self.address}).approve(self.escrow.contract.address, amount)
|
||||||
self.blockchain.chain.wait.for_receipt(tx, timeout=self.blockchain.timeout)
|
self.blockchain.chain.wait.for_receipt(approve_txhash, timeout=self.blockchain.timeout)
|
||||||
|
|
||||||
tx = self.escrow.transact({'from': address}).deposit(amount, locktime)
|
deposit_txhash = self.escrow.transact({'from': self.address}).deposit(amount, locktime)
|
||||||
self.blockchain.chain.wait.for_receipt(tx, timeout=self.blockchain.timeout)
|
self.blockchain.chain.wait.for_receipt(deposit_txhash, timeout=self.blockchain.timeout)
|
||||||
|
|
||||||
tx = self.escrow.transact({'from': address}).switchLock()
|
lock_txhash = self.escrow.transact({'from': self.address}).switchLock()
|
||||||
self.blockchain.chain.wait.for_receipt(tx, timeout=self.blockchain.timeout)
|
self.blockchain.chain.wait.for_receipt(lock_txhash, timeout=self.blockchain.timeout)
|
||||||
|
|
||||||
def mine(self, address: str=None) -> str:
|
return approve_txhash, deposit_txhash, lock_txhash
|
||||||
address = address or self.token.creator
|
|
||||||
tx = self.escrow.transact({'from': address}).mint()
|
|
||||||
self.blockchain.chain.wait.for_receipt(tx, timeout=self.blockchain.timeout)
|
|
||||||
return tx
|
|
||||||
|
|
||||||
def withdraw(self, address: str=None) -> str:
|
def mint(self) -> str:
|
||||||
address = address or self.token.creator
|
"""Computes and transfers tokens to the miner's account"""
|
||||||
tx = self.escrow.transact({'from': address}).withdrawAll()
|
txhash = self.escrow.transact({'from': self.address}).mint()
|
||||||
self.blockchain.chain.wait.for_receipt(tx, timeout=self.blockchain.timeout)
|
self.blockchain.chain.wait.for_receipt(txhash, timeout=self.blockchain.timeout)
|
||||||
return tx
|
return txhash
|
||||||
|
|
||||||
|
def add_dht_key(self, dht_id) -> str:
|
||||||
|
"""Store a new DHT key"""
|
||||||
|
txhash = self.escrow.transact({'from': self.address}).publishDHTKey(dht_id)
|
||||||
|
self.blockchain.chain.wait.for_receipt(txhash)
|
||||||
|
return txhash
|
||||||
|
|
||||||
|
def get_dht_key(self) -> tuple:
|
||||||
|
"""Retrieve all stored DHT keys for this miner"""
|
||||||
|
count = self.escrow().getDHTKeysCount(self.address)
|
||||||
|
dht_keys = tuple(self.escrow().getDHTKey(self.address, index) for index in range(count))
|
||||||
|
return dht_keys
|
||||||
|
|
||||||
|
def confirm_activity(self) -> str:
|
||||||
|
"""Miner rewarded for every confirmed period"""
|
||||||
|
txhash = self.escrow.contract.transact({'from': self.address}).confirmActivity()
|
||||||
|
self.blockchain.chain.wait.for_receipt(txhash)
|
||||||
|
return txhash
|
||||||
|
|
||||||
|
def withdraw(self) -> str:
|
||||||
|
"""withdraw rewarded tokens"""
|
||||||
|
txhash = self.escrow.transact({'from': self.address}).withdrawAll()
|
||||||
|
self.blockchain.chain.wait.for_receipt(txhash, timeout=self.blockchain.timeout)
|
||||||
|
return txhash
|
||||||
|
|
Loading…
Reference in New Issue