[KMS-ETH]- Enhances the singleton pattern for blockchain, adding a way to retrieve the instance.

pull/195/head^2
Kieran Prasch 2018-03-01 19:03:32 -08:00
parent 2e74fc000a
commit e33b6e8154
2 changed files with 13 additions and 6 deletions

View File

@ -13,7 +13,7 @@ class Blockchain:
"""
_network = ''
_instance = False
_instance = None
class AlreadyRunning(Exception):
pass
@ -28,10 +28,10 @@ class Blockchain:
"""
# Singleton
if Blockchain._instance is True:
class_name = self.__class__.__name__
raise Blockchain.AlreadyRunning('{} is already running. Use .get() to retrieve'.format(class_name))
Blockchain._instance = True
if Blockchain._instance is not None:
message = 'Blockchain: is already running. Use .get() to retrieve'.format(self._network)
raise Blockchain.AlreadyRunning(message)
Blockchain.__instance = self
if populus_config is None:
populus_config = PopulusConfig()
@ -43,6 +43,13 @@ class Blockchain:
# Opens and preserves connection to a running populus blockchain
self._chain = self._project.get_chain(self._network).__enter__()
@classmethod
def get(cls):
if cls._instance is None:
class_name = cls.__name__
raise Exception('{} has not been created.'.format(class_name))
return cls._instance
def disconnect(self):
self._chain.__exit__(None, None, None)

View File

@ -10,7 +10,7 @@ def testerchain():
chain = TesterBlockchain()
yield chain
del chain
Blockchain._instance = False
TesterBlockchain._instance = None
@pytest.fixture(scope='function')