From a147bcc39fb3f6aa7833e0fe9236aa25a3b217b2 Mon Sep 17 00:00:00 2001 From: Kieran R Prasch Date: Wed, 7 Feb 2018 14:41:31 -0800 Subject: [PATCH] [KMS-ETH]- Adds wait_time and unlock methods to blockchain, modifies tests to follow. --- nkms_eth/blockchain.py | 10 +++++--- nkms_eth/miner.py | 10 ++++---- tests/conftest.py | 2 -- tests/contracts/test_escrow_contract.py | 33 +++++++++++-------------- tests/test_miner.py | 5 +--- 5 files changed, 26 insertions(+), 34 deletions(-) diff --git a/nkms_eth/blockchain.py b/nkms_eth/blockchain.py index 52a22f14a..705484adb 100644 --- a/nkms_eth/blockchain.py +++ b/nkms_eth/blockchain.py @@ -47,11 +47,8 @@ class Blockchain: class_name = self.__class__.__name__ return "{} {}:{}".format(class_name, self.network, self.project_name) - def __del__(self): + def disconnect(self): self._project.chain.__exit__(None, None, None) - for attr in ('project', 'chain', 'w3'): - if hasattr(self._project, attr): - delattr(self._project, attr) @property def chain(self): @@ -65,6 +62,11 @@ class Blockchain: """ Gets an existing contract or returns an error """ return self._project.chain.provider.get_contract(name) + def wait_time(self, wait_hours, step=50): + end_timestamp = self.web3.eth.getBlock(self.web3.eth.blockNumber).timestamp + wait_hours * 60 * 60 + while self.web3.eth.getBlock(self.web3.eth.blockNumber).timestamp < end_timestamp: + self.chain.wait.for_block(self.web3.eth.blockNumber + step) + class TesterBlockchain(Blockchain): network = 'tester' diff --git a/nkms_eth/miner.py b/nkms_eth/miner.py index b9f118394..4fab09c42 100644 --- a/nkms_eth/miner.py +++ b/nkms_eth/miner.py @@ -25,11 +25,11 @@ class Miner: tx = self.escrow.contract.transact({'from': address}).deposit(amount, locktime) self.blockchain.chain.wait.for_receipt(tx, timeout=self.blockchain.timeout) - # def unlock(self, address: str=None): - # if not address: - # address = chain.web3.eth.accounts[0] - # tx = self.escrow.contract.transact({'from': address}).switchLock() - # chain.wait.for_receipt(tx, timeout=chain.timeout) + def unlock(self, address: str=None): + if not address: + address = self.blockchain.chain.web3.eth.accounts[0] + tx = self.escrow.contract.transact({'from': address}).switchLock() + self.blockchain.chain.wait.for_receipt(tx, timeout=self.blockchain.chain.timeout) def mine(self, address: str=None): if not address: diff --git a/tests/conftest.py b/tests/conftest.py index 10a8e2906..493fe5bec 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,8 +9,6 @@ from nkms_eth.miner import Miner def testerchain(): chain = TesterBlockchain() yield chain - del chain - @pytest.fixture() def token(testerchain): diff --git a/tests/contracts/test_escrow_contract.py b/tests/contracts/test_escrow_contract.py index 665ff4c04..b9d136124 100644 --- a/tests/contracts/test_escrow_contract.py +++ b/tests/contracts/test_escrow_contract.py @@ -2,11 +2,7 @@ import pytest from ethereum.tester import TransactionFailed -# TODO extract method -def wait_time(testerchain, wait_hours, step=50): - end_timestamp = testerchain.web3.eth.getBlock(testerchain.web3.eth.blockNumber).timestamp + wait_hours * 60 * 60 - while testerchain.web3.eth.getBlock(testerchain.web3.eth.blockNumber).timestamp 0 @@ -317,7 +312,7 @@ def test_mining(testerchain, token, escrow): chain.wait.for_receipt(tx) # Only Ursula confirm next period - wait_time(chain, 1) + testerchain.wait_time(1) assert 1500 == escrow().getAllLockedTokens() tx = escrow.transact({'from': ursula}).confirmActivity() chain.wait.for_receipt(tx) @@ -327,7 +322,7 @@ def test_mining(testerchain, token, escrow): chain.wait.for_receipt(tx) # Ursula and Alice mint tokens for last periods - wait_time(chain, 1) + testerchain.wait_time(1) assert 1000 == escrow().getAllLockedTokens() tx = escrow.transact({'from': ursula}).mint() chain.wait.for_receipt(tx) @@ -349,7 +344,7 @@ def test_mining(testerchain, token, escrow): chain.wait.for_receipt(tx) # Ursula can't confirm next period because end of locking - wait_time(chain, 1) + testerchain.wait_time(1) assert 500 == escrow().getAllLockedTokens() with pytest.raises(TransactionFailed): tx = escrow.transact({'from': ursula}).confirmActivity() @@ -360,7 +355,7 @@ def test_mining(testerchain, token, escrow): chain.wait.for_receipt(tx) # Ursula mint tokens for next period - wait_time(chain, 1) + testerchain.wait_time(1) assert 500 == escrow().getAllLockedTokens() tx = escrow.transact({'from': ursula}).mint() chain.wait.for_receipt(tx) @@ -381,7 +376,7 @@ def test_mining(testerchain, token, escrow): chain.wait.for_receipt(tx) tx = escrow.transact({'from': alice}).confirmActivity() chain.wait.for_receipt(tx) - wait_time(chain, 2) + testerchain.wait_time(2) assert 0 == escrow().getAllLockedTokens() tx = escrow.transact({'from': alice}).mint() chain.wait.for_receipt(tx) diff --git a/tests/test_miner.py b/tests/test_miner.py index 57087685e..019484cad 100644 --- a/tests/test_miner.py +++ b/tests/test_miner.py @@ -13,10 +13,7 @@ def test_deposit(testerchain, miner, token): miner.lock(amount=1000*M, locktime=100, address=testerchain.web3.eth.accounts[1]) -def test_select_ursulas(testerchain): - token = NuCypherKMSToken(blockchain=testerchain) - escrow = Escrow(blockchain=testerchain, token=token) - miner = Miner(blockchain=testerchain) +def test_select_ursulas(testerchain, token, escrow, miner): token.airdrop() # Create a random set of miners (we have 9 in total)