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 = ''
|
||||
_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)
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ def testerchain():
|
|||
chain = TesterBlockchain()
|
||||
yield chain
|
||||
del chain
|
||||
Blockchain._instance = False
|
||||
TesterBlockchain._instance = None
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
|
|
Loading…
Reference in New Issue