mirror of https://github.com/nucypher/nucypher.git
[KMS-ETH]- Enhances the singleton pattern for blockchain, adding a way to retrieve the instance.
parent
2e74fc000a
commit
e33b6e8154
|
@ -13,7 +13,7 @@ class Blockchain:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_network = ''
|
_network = ''
|
||||||
_instance = False
|
_instance = None
|
||||||
|
|
||||||
class AlreadyRunning(Exception):
|
class AlreadyRunning(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -28,10 +28,10 @@ class Blockchain:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Singleton
|
# Singleton
|
||||||
if Blockchain._instance is True:
|
if Blockchain._instance is not None:
|
||||||
class_name = self.__class__.__name__
|
message = 'Blockchain: is already running. Use .get() to retrieve'.format(self._network)
|
||||||
raise Blockchain.AlreadyRunning('{} is already running. Use .get() to retrieve'.format(class_name))
|
raise Blockchain.AlreadyRunning(message)
|
||||||
Blockchain._instance = True
|
Blockchain.__instance = self
|
||||||
|
|
||||||
if populus_config is None:
|
if populus_config is None:
|
||||||
populus_config = PopulusConfig()
|
populus_config = PopulusConfig()
|
||||||
|
@ -43,6 +43,13 @@ class Blockchain:
|
||||||
# Opens and preserves connection to a running populus blockchain
|
# Opens and preserves connection to a running populus blockchain
|
||||||
self._chain = self._project.get_chain(self._network).__enter__()
|
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):
|
def disconnect(self):
|
||||||
self._chain.__exit__(None, None, None)
|
self._chain.__exit__(None, None, None)
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ def testerchain():
|
||||||
chain = TesterBlockchain()
|
chain = TesterBlockchain()
|
||||||
yield chain
|
yield chain
|
||||||
del chain
|
del chain
|
||||||
Blockchain._instance = False
|
TesterBlockchain._instance = None
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture(scope='function')
|
||||||
|
|
Loading…
Reference in New Issue