def _connect(self, provider: Web3Providers = None, provider_uri: str = None): self._attach_provider(provider=provider, provider_uri=provider_uri) self.log.info("Connecting to {}".format(self.provider_uri)) if self.__provider is NO_BLOCKCHAIN_CONNECTION: raise self.NoProvider( "There are no configured blockchain providers") # Connect if not connected try: w3 = self.Web3(provider=self.__provider) self.client = Web3Client.from_w3(w3=w3) except requests.ConnectionError: # RPC raise self.ConnectionFailed( f'Connection Failed - {str(self.provider_uri)} - is RPC enabled?' ) except FileNotFoundError: # IPC File Protocol raise self.ConnectionFailed( f'Connection Failed - {str(self.provider_uri)} - is IPC enabled?' ) # Check connection return self.is_connected
def connect(self): # Spawn child process if self._provider_process: self._provider_process.start() provider_uri = self._provider_process.provider_uri(scheme='file') else: provider_uri = self.provider_uri self.log.info( f"Using external Web3 Provider '{self.provider_uri}'") # Attach Provider self._attach_provider(provider=self._provider, provider_uri=provider_uri) self.log.info("Connecting to {}".format(self.provider_uri)) if self._provider is NO_BLOCKCHAIN_CONNECTION: raise self.NoProvider( "There are no configured blockchain providers") # Connect if not connected try: self.w3 = self.Web3(provider=self._provider) self.client = Web3Client.from_w3(w3=self.w3) except requests.ConnectionError: # RPC raise self.ConnectionFailed( f'Connection Failed - {str(self.provider_uri)} - is RPC enabled?' ) except FileNotFoundError: # IPC File Protocol raise self.ConnectionFailed( f'Connection Failed - {str(self.provider_uri)} - is IPC enabled?' ) else: self.attach_middleware() return self.is_connected
def connect(self, provider: Web3Providers = None, provider_uri: str = None, fetch_registry: bool = True, sync_now: bool = True): # Spawn child process if self._provider_process: self._provider_process.start() provider_uri = self._provider_process.provider_uri(scheme='file') else: self.log.info(f"Using external Web3 Provider '{provider_uri}'") # Attach Provider self._attach_provider(provider=provider, provider_uri=provider_uri) self.log.info("Connecting to {}".format(self.provider_uri)) if self._provider is NO_BLOCKCHAIN_CONNECTION: raise self.NoProvider( "There are no configured blockchain providers") # Connect Web3 Instance try: self.w3 = self.Web3(provider=self._provider) self.client = Web3Client.from_w3(w3=self.w3) except requests.ConnectionError: # RPC raise self.ConnectionFailed( f'Connection Failed - {str(self.provider_uri)} - is RPC enabled?' ) except FileNotFoundError: # IPC File Protocol raise self.ConnectionFailed( f'Connection Failed - {str(self.provider_uri)} - is IPC enabled?' ) else: self.attach_middleware() # Establish contact with NuCypher contracts if not self.registry: self._configure_registry(fetch_registry=fetch_registry) # Wait for chaindata sync if sync_now: self.client.sync() return self.is_connected
def connect(self, *args, **kwargs): self._attach_provider(testerchain.provider) self.w3 = self.Web3(provider=self._provider) self.client = Web3Client.from_w3(w3=self.w3)
def connect(self, fetch_registry: bool = True, sync_now: bool = False, emitter: StdoutEmitter = None): # Spawn child process if self._provider_process: self._provider_process.start() provider_uri = self._provider_process.provider_uri(scheme='file') else: provider_uri = self.provider_uri self.log.info( f"Using external Web3 Provider '{self.provider_uri}'") # Attach Provider self._attach_provider(provider=self._provider, provider_uri=provider_uri) self.log.info("Connecting to {}".format(self.provider_uri)) if self._provider is NO_BLOCKCHAIN_CONNECTION: raise self.NoProvider( "There are no configured blockchain providers") # Connect if not connected try: self.w3 = self.Web3(provider=self._provider) self.client = Web3Client.from_w3(w3=self.w3) except requests.ConnectionError: # RPC raise self.ConnectionFailed( f'Connection Failed - {str(self.provider_uri)} - is RPC enabled?' ) except FileNotFoundError: # IPC File Protocol raise self.ConnectionFailed( f'Connection Failed - {str(self.provider_uri)} - is IPC enabled?' ) else: self.attach_middleware() # Establish contact with NuCypher contracts if not self.registry: self._configure_registry(fetch_registry=fetch_registry) # Wait for chaindata sync if sync_now: sync_state = self.client.sync() if emitter: import click emitter.echo( f"Syncing: {self.client.chain_name.capitalize()}. Waiting for sync to begin." ) while not len(self.client.peers): emitter.echo("waiting for peers...") time.sleep(5) peer_count = len(self.client.peers) emitter.echo( f"Found {'an' if peer_count == 1 else peer_count} Ethereum peer{('s' if peer_count>1 else '')}." ) try: emitter.echo("Beginning sync...") initial_state = next(sync_state) except StopIteration: # will occur if no syncing needs to happen emitter.echo("Local blockchain data is already synced.") return True prior_state = initial_state total_blocks_to_sync = int(initial_state.get( 'highestBlock', 0)) - int( initial_state.get('currentBlock', 0)) with click.progressbar(length=total_blocks_to_sync, label="sync progress") as bar: for syncdata in sync_state: if syncdata: blocks_accomplished = int( syncdata['currentBlock']) - int( prior_state.get('currentBlock', 0)) bar.update(blocks_accomplished) prior_state = syncdata else: try: for syncdata in sync_state: self.client.log.info( f"Syncing {syncdata['currentBlock']}/{syncdata['highestBlock']}" ) except TypeError: # it's already synced return True return self.is_connected