next_state.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. use crate::message::Header;
  2. use crate::message::Payload;
  3. use crate::neighbor::Neighborhood;
  4. use crate::CastID;
  5. use crate::Configuration;
  6. use crate::GnomeId;
  7. use crate::Message;
  8. use crate::Neighbor;
  9. use crate::SwarmTime;
  10. use crate::SyncData;
  11. #[derive(Debug)]
  12. pub enum ChangeConfig {
  13. None,
  14. InsertPubkey {
  15. id: GnomeId,
  16. key: Vec<u8>,
  17. turn_ended: bool,
  18. },
  19. AddBroadcast {
  20. id: CastID,
  21. origin: GnomeId,
  22. source: GnomeId,
  23. filtered_neighbors: Vec<GnomeId>,
  24. turn_ended: bool,
  25. },
  26. RemoveBroadcast {
  27. id: CastID,
  28. turn_ended: bool,
  29. },
  30. Custom {
  31. id: u8,
  32. s_data: SyncData,
  33. turn_ended: bool,
  34. },
  35. }
  36. impl ChangeConfig {
  37. pub fn end_turn(&mut self) {
  38. match *self {
  39. Self::None => {}
  40. Self::AddBroadcast {
  41. ref mut turn_ended, ..
  42. } => *turn_ended = true,
  43. Self::RemoveBroadcast {
  44. ref mut turn_ended, ..
  45. } => *turn_ended = true,
  46. Self::InsertPubkey {
  47. ref mut turn_ended, ..
  48. } => *turn_ended = true,
  49. Self::Custom {
  50. ref mut turn_ended, ..
  51. } => *turn_ended = true,
  52. }
  53. }
  54. pub fn add_filtered_neighbor(&mut self, gnome_id: GnomeId) {
  55. if let Self::AddBroadcast {
  56. ref mut filtered_neighbors,
  57. turn_ended,
  58. ..
  59. } = *self
  60. {
  61. if !turn_ended {
  62. filtered_neighbors.push(gnome_id);
  63. }
  64. }
  65. }
  66. }
  67. #[derive(Debug)]
  68. pub struct NextState {
  69. pub neighborhood: Neighborhood,
  70. pub swarm_time: SwarmTime,
  71. pub swarm_time_max: SwarmTime,
  72. pub change_config: ChangeConfig,
  73. // pub block_id: BlockID,
  74. // pub last_accepted_block: BlockID,
  75. // pub last_accepted_reconf: Option<Configuration>,
  76. // pub data: Data,
  77. pub header: Header,
  78. pub last_accepted_message: Message,
  79. pub payload: Payload,
  80. all_neighbors_same_header: bool,
  81. turn_update_with_a_message: bool,
  82. }
  83. impl NextState {
  84. pub fn new() -> Self {
  85. NextState {
  86. neighborhood: Neighborhood(0),
  87. swarm_time: SwarmTime(0),
  88. swarm_time_max: SwarmTime(u32::MAX),
  89. change_config: ChangeConfig::None,
  90. // block_id: BlockID(0),
  91. // last_accepted_block: BlockID(0),
  92. // last_accepted_reconf: None,
  93. // data: Data(0),
  94. header: Header::Sync,
  95. all_neighbors_same_header: true,
  96. last_accepted_message: Message::block(),
  97. payload: Payload::KeepAlive(1024),
  98. turn_update_with_a_message: false,
  99. }
  100. }
  101. // TODO: I have to redesign this barely functioning logic,
  102. // but I am not yet ready to do so - don't know how.
  103. //
  104. // We have a lot of stuff going on here:
  105. // - neighbors come and go
  106. // - there is local neighbor state
  107. // - there is our gnome's state
  108. // - and there are incoming messages from neighbors that can come out of order
  109. // or not at all
  110. //
  111. // We should only update from a neighbor if he has a new message: DONE
  112. // We should not increase Neighborhood if no message was received this turn: DONE?
  113. pub fn update(&mut self, neighbor: &mut Neighbor) {
  114. if !neighbor.new_message_recieved {
  115. return;
  116. }
  117. self.turn_update_with_a_message = true;
  118. neighbor.new_message_recieved = false;
  119. // println!("Update {:?}", neighbor);
  120. let neighbor_st = neighbor.swarm_time;
  121. if neighbor_st < self.last_accepted_message.swarm_time {
  122. return;
  123. }
  124. if neighbor_st < self.swarm_time_max && neighbor_st < self.swarm_time {
  125. self.swarm_time = neighbor_st;
  126. }
  127. // let block_id_received = match neighbor.header {
  128. // Header::Sync => BlockID(0),
  129. // Header::Reconfigure => BlockID(0),
  130. // Header::Block(b_id) => b_id,
  131. // };
  132. // let data_received = match neighbor.payload {
  133. // Payload::Block(_b_id, data) => data,
  134. // Payload::Reconfigure(config) => Data(config.as_u32()),
  135. // _ => Data(0),
  136. // };
  137. // println!("id, data: {} {}", block_id_received, data_received);
  138. // if self.block_id != block_id_received {
  139. // self.all_neighbors_same_header = false;
  140. // // println!("fols");
  141. // }
  142. if self.header != neighbor.header {
  143. self.all_neighbors_same_header = false;
  144. // println!("fols");
  145. }
  146. // println!("{} > {}", block_id_received, self.block_id);
  147. // if block_id_received > self.block_id
  148. // || (block_id_received == self.block_id && data_received.0 > self.data.0)
  149. if neighbor.header > self.header {
  150. // println!("N {} > {}", neighbor.header, self.header);
  151. // println!("P: {:?}", neighbor.payload);
  152. // self.block_id = block_id_received;
  153. self.header = neighbor.header;
  154. self.payload = neighbor.payload.clone();
  155. self.neighborhood = Neighborhood(0);
  156. // } else if block_id_received == self.block_id
  157. if self.header.is_reconfigure() {
  158. // TODO: make use of signature
  159. if let Payload::Reconfigure(ref _signature, ref config) = self.payload {
  160. match config {
  161. Configuration::StartBroadcast(origin, id) => {
  162. self.change_config = ChangeConfig::AddBroadcast {
  163. id: *id,
  164. origin: *origin,
  165. source: neighbor.id,
  166. filtered_neighbors: vec![],
  167. turn_ended: false,
  168. };
  169. }
  170. Configuration::InsertPubkey(id, key) => {
  171. self.change_config = ChangeConfig::InsertPubkey {
  172. id: *id,
  173. key: key.clone(),
  174. turn_ended: false,
  175. };
  176. }
  177. Configuration::UserDefined(id, s_data) => {
  178. self.change_config = ChangeConfig::Custom {
  179. id: *id,
  180. s_data: s_data.clone(),
  181. turn_ended: false,
  182. };
  183. }
  184. _ => {
  185. println!("Unhandled config");
  186. }
  187. }
  188. }
  189. }
  190. } else if neighbor.header == self.header && self.neighborhood.0 > neighbor.neighborhood.0 {
  191. // println!("nhood downgraded from {}", self.neighborhood);
  192. self.neighborhood = neighbor.neighborhood;
  193. // println!("nhood downgraded to {}", self.neighborhood);
  194. if neighbor.header.is_reconfigure() {
  195. if let Payload::Reconfigure(ref _signature, ref config) = neighbor.payload {
  196. match config {
  197. Configuration::StartBroadcast(g_id, _c_id) => {
  198. if neighbor.id.0 != g_id.0 {
  199. self.change_config.add_filtered_neighbor(neighbor.id);
  200. }
  201. }
  202. _ => {
  203. println!("Unhandled config update");
  204. }
  205. }
  206. }
  207. }
  208. // TODO: handle reconfig
  209. // } else {
  210. // println!("Unhandled next_state {:?} update: {:?}", self, neighbor);
  211. }
  212. // println!("Po wszystkim: {} {}", self.block_id, self.data);
  213. }
  214. // fn next_swarm_time(&mut self) {
  215. // let old_prev_st = self.prev_swarm_time;
  216. // self.prev_swarm_time = self.swarm_time;
  217. // self.swarm_time = if self.swarm_time.0 == u32::MAX {
  218. // SwarmTime(0)
  219. // } else if old_prev_st == self.prev_swarm_time {
  220. // self.swarm_time.inc()
  221. // } else {
  222. // self.swarm_time
  223. // };
  224. // self.swarm_time_max = self.swarm_time.inc();
  225. // }
  226. fn next_swarm_time(&mut self) {
  227. self.swarm_time = if self.swarm_time.0 == u32::MAX {
  228. SwarmTime(0)
  229. } else {
  230. self.swarm_time.inc()
  231. };
  232. self.swarm_time_max = self.swarm_time.inc();
  233. }
  234. // pub fn next_params(&self) -> (SwarmTime, Neighborhood, BlockID, Data) {
  235. pub fn next_params(&self) -> (SwarmTime, Neighborhood, Header, Payload) {
  236. // if self.all_neighbors_same_header {
  237. // println!("next_params 1 {}", self.swarm_time);
  238. (
  239. self.swarm_time.inc(),
  240. self.get_next_nhood(),
  241. self.header,
  242. self.payload.clone(),
  243. )
  244. // } else {
  245. // // println!("next_params 2 {}", self.swarm_time);
  246. // (
  247. // self.swarm_time.inc(),
  248. // self.neighborhood,
  249. // self.header,
  250. // self.payload.clone(),
  251. // )
  252. // }
  253. }
  254. fn get_next_nhood(&self) -> Neighborhood {
  255. if self.all_neighbors_same_header {
  256. if self.turn_update_with_a_message {
  257. // print!(">inc<");
  258. self.neighborhood.inc()
  259. } else {
  260. // print!(">no inc<");
  261. self.neighborhood
  262. }
  263. } else {
  264. self.neighborhood
  265. }
  266. }
  267. // pub fn reset_for_next_turn(&mut self, new_round: bool, block_id: BlockID, data: Data) {
  268. pub fn reset_for_next_turn(&mut self, new_round: bool, header: Header, payload: Payload) {
  269. // println!("reset {}", self.swarm_time);
  270. self.next_swarm_time();
  271. self.turn_update_with_a_message = false;
  272. // self.block_id = block_id;
  273. // self.data = data;
  274. self.header = header;
  275. self.payload = payload.clone();
  276. self.all_neighbors_same_header = true;
  277. self.change_config.end_turn();
  278. self.neighborhood = if new_round {
  279. self.change_config = if header.is_reconfigure() {
  280. if let Payload::Reconfigure(_signature, config) = payload {
  281. match config {
  282. Configuration::StartBroadcast(origin, id) => ChangeConfig::AddBroadcast {
  283. id,
  284. origin,
  285. source: origin,
  286. filtered_neighbors: vec![],
  287. turn_ended: false,
  288. },
  289. _ => {
  290. // TODO: handle other configs
  291. println!("Unhandled config in reset_for_next_turn");
  292. ChangeConfig::None
  293. }
  294. }
  295. } else {
  296. println!("Unexpected payload in reset_for_next_turn");
  297. ChangeConfig::None
  298. }
  299. } else {
  300. ChangeConfig::None
  301. };
  302. self.all_neighbors_same_header = false;
  303. Neighborhood(0)
  304. } else {
  305. self.neighborhood.inc()
  306. };
  307. // println!("bid: {}", self.block_id);
  308. }
  309. }