-
+ 388113D46790BA9828275D8F1B20CEF6DCA63976D43192EFA8D4332B3360FA8CB93A50A026EB35A66BA129BEA7E61FC6973491072F6B5385D4EC83C11CCD2DC7
blatta/lib/order_buffer.py
(0 . 0)(1 . 57)
1873 import time
1874 from broadcast import Broadcast
1875 from direct import Direct
1876 from commands import BROADCAST
1877 from commands import DIRECT
1878
1879
1880 class OrderBuffer(object):
1881 def __init__(self, state):
1882 self.buffer = {}
1883 self.state = state
1884
1885 def add(self, message):
1886 ts = time.time()
1887 if message['command'] == BROADCAST:
1888 m = Broadcast(message, self.state)
1889 elif message['command'] == DIRECT:
1890 m = Direct(message, self.state)
1891 else:
1892 return
1893
1894 if self.buffer.get(ts) is None:
1895 self.buffer[ts] = [m]
1896 else:
1897 self.buffer[ts].append(m)
1898
1899 def expects(self, message_hash):
1900 for value in self.buffer.values():
1901 for message in value:
1902 if message_hash == message.self_chain:
1903 return True
1904 elif message_hash == message.net_chain:
1905 return True
1906 return False
1907
1908 def has(self, message_hash):
1909 for value in self.buffer.values():
1910 for message in value:
1911 if message_hash == message.message_hash:
1912 return True
1913 return False
1914
1915 def dequeue_and_order_mature_messages(self):
1916 current_time = time.time()
1917 sorted_messages = sorted(self.buffer.keys())
1918 mature_messages = []
1919 for timestamp in sorted_messages:
1920 if timestamp < current_time - int(self.state.get_knob('order_buffer_expiration_seconds')):
1921 if isinstance(self.buffer[timestamp], list):
1922 if len(self.buffer[timestamp]) > 0:
1923 for message in self.buffer[timestamp]:
1924 mature_messages.append(message)
1925 del self.buffer[timestamp]
1926 else:
1927 mature_messages.append(self.buffer[timestamp])
1928 del self.buffer[timestamp]
1929 return sorted(mature_messages, key=lambda m: m.timestamp)