def save_state(self) -> None: # If Redis is enabled, save the current time, indicating that the node # monitor was alive at this time, the current session index, # and the last height checked. if self.redis_enabled: key_si = Keys.get_node_monitor_session_index(self.monitor_name) key_lh = Keys.get_node_monitor_last_height_checked( self.monitor_name) key_alive = Keys.get_node_monitor_alive(self.monitor_name) self.logger.debug('Saving node monitor state: %s=%s, %s=%s', self._monitor_name, key_si, self._session_index, key_lh, self._last_height_checked) # Set session index key self.redis.set(key_si, self._session_index) # Set last height checked key until = timedelta(seconds=self._redis_last_height_key_timeout) self.redis.set_for(key_lh, self._last_height_checked, until) # Set alive key (to be able to query latest update from Telegram) until = timedelta(seconds=self._redis_alive_key_timeout) self.redis.set_for(key_alive, str(datetime.now().timestamp()), until)
def load_state(self) -> None: # If Redis is enabled, load any previously stored state if self.redis_enabled: key_lh = Keys.get_node_monitor_last_height_checked( self.monitor_name) self._last_height_checked = self.redis.get_int(key_lh, NONE) self.logger.debug('Restored %s state: %s=%s', self._monitor_name, key_lh, self._last_height_checked)
def test_load_state_sets_values_to_saved_values(self) -> None: # Set Redis values manually key_lh = Keys.get_node_monitor_last_height_checked(self.monitor_name) self.redis.set_unsafe(key_lh, self.dummy_last_height_checked) # Load the values from Redis self.monitor.load_state() # Assert self.assertEqual(self.dummy_last_height_checked, self.monitor.last_height_checked)
def load_state(self) -> None: # If Redis is enabled, load the session index, and last height checked # for slashing if any. if self.redis_enabled: key_si = Keys.get_node_monitor_session_index(self.monitor_name) key_lh = Keys.get_node_monitor_last_height_checked( self.monitor_name) self._session_index = self.redis.get_int(key_si, NONE) self._last_height_checked = self.redis.get_int(key_lh, NONE) self.logger.debug('Restored %s state: %s=%s, %s=%s', self._monitor_name, key_si, self._session_index, key_lh, self._last_height_checked)
def test_save_state_sets_values_to_current_values_and_stores_alive_key_temp( self) -> None: # Set monitor values manually self.monitor._last_height_checked = self.dummy_last_height_checked # Save the values to Redis self.monitor.save_state() key_lh = Keys.get_node_monitor_last_height_checked(self.monitor_name) # Get last update, and its timeout in Redis last_update = self.redis.get(key_lh) timeout = self.redis.time_to_live(key_lh) # Assert self.assertEqual(self.dummy_last_height_checked, self.redis.get_int(key_lh)) self.assertIsNotNone(last_update) self.assertEqual(timeout, self.redis_alive_key_timeout)