Files @ 44a98be4e4b4
Branch filter:

Location: CSY/reowolf/src/runtime/communication.rs

44a98be4e4b4 31.0 KiB application/rls-services+xml Show Annotation Show as Raw Download as Raw
Christopher Esterhuyse
beginning large overhaul: moving to globally-unique ports & port -> endpoint route mappings
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
use crate::common::*;
use crate::runtime::{actors::*, endpoint::*, errors::*, *};

impl Controller {
    fn end_round_with_decision(&mut self, decision: Decision) -> Result<(), SyncErr> {
        log!(&mut self.inner.logger, "ENDING ROUND WITH DECISION! {:?}", &decision);
        let ret = match &decision {
            Decision::Success(predicate) => {
                // overwrite MonoN/P
                self.inner.mono_n = {
                    let poly_n = self.ephemeral.poly_n.take().unwrap();
                    poly_n.choose_mono(predicate).unwrap_or_else(|| {
                        panic!(
                            "Ending round with decision pred {:#?} but poly_n has branches {:#?}. My log is... {}",
                            &predicate, &poly_n.branches, &self.inner.logger
                        );
                    })
                };
                self.inner.mono_ps.clear();
                self.inner.mono_ps.extend(
                    self.ephemeral
                        .poly_ps
                        .drain(..)
                        .map(|poly_p| poly_p.choose_mono(predicate).unwrap()),
                );
                Ok(())
            }
            Decision::Failure => Err(SyncErr::Timeout),
        };
        let announcement = CommMsgContents::Announce { decision }.into_msg(self.inner.round_index);
        for &child_port in self.inner.family.children_ports.iter() {
            log!(
                &mut self.inner.logger,
                "Forwarding {:?} to child with port {:?}",
                &announcement,
                child_port
            );
            self.inner
                .endpoint_exts
                .get_mut(child_port)
                .expect("eefef")
                .endpoint
                .send(announcement.clone())?;
        }
        self.inner.round_index += 1;
        self.ephemeral.clear();
        ret
    }

    // Drain self.ephemeral.solution_storage and handle the new locals. Return decision if one is found
    fn handle_locals_maybe_decide(&mut self) -> Result<bool, SyncErr> {
        if let Some(parent_port) = self.inner.family.parent_port {
            // I have a parent -> I'm not the leader
            let parent_endpoint =
                &mut self.inner.endpoint_exts.get_mut(parent_port).expect("huu").endpoint;
            for partial_oracle in self.ephemeral.solution_storage.iter_new_local_make_old() {
                let msg =
                    CommMsgContents::Elaborate { partial_oracle }.into_msg(self.inner.round_index);
                log!(&mut self.inner.logger, "Sending {:?} to parent {:?}", &msg, parent_port);
                parent_endpoint.send(msg)?;
            }
            Ok(false)
        } else {
            // I have no parent -> I'm the leader
            assert!(self.inner.family.parent_port.is_none());
            let maybe_predicate = self.ephemeral.solution_storage.iter_new_local_make_old().next();
            Ok(if let Some(predicate) = maybe_predicate {
                let decision = Decision::Success(predicate);
                log!(&mut self.inner.logger, "DECIDE ON {:?} AS LEADER!", &decision);
                self.end_round_with_decision(decision)?;
                true
            } else {
                false
            })
        }
    }

    fn kick_off_native(
        &mut self,
        sync_batches: impl Iterator<Item = SyncBatch>,
    ) -> Result<PolyN, EndpointErr> {
        let MonoN { ports, .. } = self.inner.mono_n.clone();
        let Self { inner: ControllerInner { endpoint_exts, round_index, .. }, .. } = self;
        let mut branches = HashMap::<_, _>::default();
        for (sync_batch_index, SyncBatch { puts, gets }) in sync_batches.enumerate() {
            let port_to_channel_id = |port| endpoint_exts.get(port).unwrap().info.channel_id;
            let all_ports = ports.iter().copied();
            let all_channel_ids = all_ports.map(port_to_channel_id);

            let mut predicate = Predicate::new_trivial();

            // assign TRUE for puts and gets
            let true_ports = puts.keys().chain(gets.iter()).copied();
            let true_channel_ids = true_ports.clone().map(port_to_channel_id);
            predicate.batch_assign_nones(true_channel_ids, true);

            // assign FALSE for all in interface not assigned true
            predicate.batch_assign_nones(all_channel_ids.clone(), false);

            if branches.contains_key(&predicate) {
                // TODO what do I do with redundant predicates?
                unimplemented!(
                    "Duplicate predicate {:#?}!\nHaving multiple batches with the same
                    predicate requires the support of oracle boolean variables",
                    &predicate,
                )
            }
            let branch = BranchN { to_get: gets, gotten: Default::default(), sync_batch_index };
            for (port, payload) in puts {
                log!(
                    &mut self.inner.logger,
                    "... ... Initial native put msg {:?} pred {:?} batch {:?}",
                    &payload,
                    &predicate,
                    sync_batch_index,
                );
                let msg =
                    CommMsgContents::SendPayload { payload_predicate: predicate.clone(), payload }
                        .into_msg(*round_index);
                endpoint_exts.get_mut(port).unwrap().endpoint.send(msg)?;
            }
            log!(
                &mut self.inner.logger,
                "... Initial native branch batch index={} with pred {:?}",
                sync_batch_index,
                &predicate
            );
            if branch.to_get.is_empty() {
                self.ephemeral.solution_storage.submit_and_digest_subtree_solution(
                    &mut self.inner.logger,
                    SubtreeId::PolyN,
                    predicate.clone(),
                );
            }
            branches.insert(predicate, branch);
        }
        Ok(PolyN { ports, branches })
    }
    pub fn sync_round(
        &mut self,
        deadline: Option<Instant>,
        sync_batches: Option<impl Iterator<Item = SyncBatch>>,
    ) -> Result<(), SyncErr> {
        if let Some(e) = self.unrecoverable_error {
            return Err(e.clone());
        }
        self.sync_round_inner(deadline, sync_batches).map_err(move |e| match e {
            SyncErr::Timeout => e, // this isn't unrecoverable
            _ => {
                // Must set unrecoverable error! and tear down our net channels
                self.unrecoverable_error = Some(e);
                self.ephemeral.clear();
                self.inner.endpoint_exts = Default::default();
                e
            }
        })
    }

    // Runs a synchronous round until all the actors are in decided state OR 1+ are inconsistent.
    // If a native requires setting up, arg `sync_batches` is Some, and those are used as the sync batches.
    fn sync_round_inner(
        &mut self,
        mut deadline: Option<Instant>,
        sync_batches: Option<impl Iterator<Item = SyncBatch>>,
    ) -> Result<(), SyncErr> {
        log!(
            &mut self.inner.logger,
            "~~~~~~~~ SYNC ROUND STARTS! ROUND={} ~~~~~~~~~",
            self.inner.round_index
        );
        assert!(self.ephemeral.is_clear());
        assert!(self.unrecoverable_error.is_none());

        // 1. Run the Mono for each Mono actor (stored in `self.mono_ps`).
        //    Some actors are dropped. some new actors are created.
        //    Ultimately, we have 0 Mono actors and a list of unnamed sync_actors
        self.ephemeral.mono_ps.extend(self.inner.mono_ps.iter().cloned());
        log!(&mut self.inner.logger, "Got {} MonoP's to run!", self.ephemeral.mono_ps.len());
        while let Some(mut mono_p) = self.ephemeral.mono_ps.pop() {
            let mut m_ctx = MonoPContext {
                ports: &mut mono_p.ports,
                mono_ps: &mut self.ephemeral.mono_ps,
                inner: &mut self.inner,
            };
            // cross boundary into crate::protocol
            let blocker = mono_p.state.pre_sync_run(&mut m_ctx, &self.protocol_description);
            log!(&mut self.inner.logger, "... MonoP's pre_sync_run got blocker {:?}", &blocker);
            match blocker {
                MonoBlocker::Inconsistent => return Err(SyncErr::Inconsistent),
                MonoBlocker::ComponentExit => drop(mono_p),
                MonoBlocker::SyncBlockStart => self.ephemeral.poly_ps.push(mono_p.into()),
            }
        }
        log!(
            &mut self.inner.logger,
            "Finished running all MonoPs! Have {} PolyPs waiting",
            self.ephemeral.poly_ps.len()
        );

        // 3. define the mapping from port -> actor
        //    this is needed during the event loop to determine which actor
        //    should receive the incoming message.
        //    TODO: store and update this mapping rather than rebuilding it each round.
        let port_to_holder: HashMap<PortId, PolyId> = {
            use PolyId::*;
            let n = self.inner.mono_n.ports.iter().map(move |&e| (e, N));
            let p = self
                .ephemeral
                .poly_ps
                .iter()
                .enumerate()
                .flat_map(|(index, m)| m.ports.iter().map(move |&e| (e, P { index })));
            n.chain(p).collect()
        };
        log!(
            &mut self.inner.logger,
            "SET OF PolyPs and MonoPs final! port lookup map is {:?}",
            &port_to_holder
        );

        // 4. Create the solution storage. it tracks the solutions of "subtrees"
        //    of the controller in the overlay tree.
        self.ephemeral.solution_storage.reset({
            let n = std::iter::once(SubtreeId::PolyN);
            let m = (0..self.ephemeral.poly_ps.len()).map(|index| SubtreeId::PolyP { index });
            let c = self
                .inner
                .family
                .children_ports
                .iter()
                .map(|&port| SubtreeId::ChildController { port });
            let subtree_id_iter = n.chain(m).chain(c);
            log!(
                &mut self.inner.logger,
                "Solution Storage has subtree Ids: {:?}",
                &subtree_id_iter.clone().collect::<Vec<_>>()
            );
            subtree_id_iter
        });

        // 5. kick off the synchronous round of the native actor if it exists

        log!(&mut self.inner.logger, "Kicking off native's synchronous round...");
        self.ephemeral.poly_n = if let Some(sync_batches) = sync_batches {
            // using if let because of nested ? operator
            // TODO check that there are 1+ branches or NO SOLUTION
            let poly_n = self.kick_off_native(sync_batches)?;
            log!(
                &mut self.inner.logger,
                "PolyN kicked off, and has branches with predicates... {:?}",
                poly_n.branches.keys().collect::<Vec<_>>()
            );
            Some(poly_n)
        } else {
            log!(&mut self.inner.logger, "NO NATIVE COMPONENT");
            None
        };

        // 6. Kick off the synchronous round of each protocol actor
        //    If just one actor becomes inconsistent now, there can be no solution!
        //    TODO distinguish between completed and not completed poly_p's?
        log!(&mut self.inner.logger, "Kicking off {} PolyP's.", self.ephemeral.poly_ps.len());
        for (index, poly_p) in self.ephemeral.poly_ps.iter_mut().enumerate() {
            let my_subtree_id = SubtreeId::PolyP { index };
            let m_ctx = PolyPContext {
                my_subtree_id,
                inner: &mut self.inner,
                solution_storage: &mut self.ephemeral.solution_storage,
            };
            use SyncRunResult as Srr;
            let blocker = poly_p.poly_run(m_ctx, &self.protocol_description)?;
            log!(&mut self.inner.logger, "... PolyP's poly_run got blocker {:?}", &blocker);
            match blocker {
                Srr::NoBranches => return Err(SyncErr::Inconsistent),
                Srr::AllBranchesComplete | Srr::BlockingForRecv => (),
            }
        }
        log!(&mut self.inner.logger, "All Poly machines have been kicked off!");

        // 7. `solution_storage` may have new solutions for this controller
        //    handle their discovery. LEADER => announce, otherwise => send to parent
        {
            let peeked = self.ephemeral.solution_storage.peek_new_locals().collect::<Vec<_>>();
            log!(
                &mut self.inner.logger,
                "Got {} controller-local solutions before a single RECV: {:?}",
                peeked.len(),
                peeked
            );
        }
        if self.handle_locals_maybe_decide()? {
            return Ok(());
        }

        // 4. Receive incoming messages until the DECISION is made OR some unrecoverable error
        log!(&mut self.inner.logger, "`No decision yet`. Time to recv messages");
        self.undelay_all();
        'recv_loop: loop {
            log!(&mut self.inner.logger, "`POLLING` with deadline {:?}...", deadline);
            let received = match deadline {
                None => {
                    // we have personally timed out. perform a "long" poll.
                    self.recv(Instant::now() + Duration::from_secs(10))?.expect("DRIED UP")
                }
                Some(d) => match self.recv(d)? {
                    // we have not yet timed out. performed a time-limited poll
                    Some(received) => received,
                    None => {
                        // timed out! send a FAILURE message to the sink,
                        // and henceforth don't time out on polling.
                        deadline = None;
                        match self.inner.family.parent_port {
                            None => {
                                // I am the sink! announce failure and return.
                                return self.end_round_with_decision(Decision::Failure);
                            }
                            Some(parent_port) => {
                                // I am not the sink! send a failure message.
                                let announcement = Msg::CommMsg(CommMsg {
                                    round_index: self.inner.round_index,
                                    contents: CommMsgContents::Failure,
                                });
                                log!(
                                    &mut self.inner.logger,
                                    "Forwarding {:?} to parent with port {:?}",
                                    &announcement,
                                    parent_port
                                );
                                self.inner
                                    .endpoint_exts
                                    .get_mut(parent_port)
                                    .expect("ss")
                                    .endpoint
                                    .send(announcement.clone())?;
                                continue; // poll some more
                            }
                        }
                    }
                },
            };
            log!(&mut self.inner.logger, "::: message {:?}...", &received);
            let current_content = match received.msg {
                Msg::SetupMsg(s) => {
                    // This occurs in the event the connector was malformed during connect()
                    println!("WASNT EXPECTING {:?}", s);
                    return Err(SyncErr::UnexpectedSetupMsg);
                }
                Msg::CommMsg(CommMsg { round_index, .. })
                    if round_index < self.inner.round_index =>
                {
                    // Old message! Can safely discard
                    log!(&mut self.inner.logger, "...and its OLD! :(");
                    drop(received);
                    continue 'recv_loop;
                }
                Msg::CommMsg(CommMsg { round_index, .. })
                    if round_index > self.inner.round_index =>
                {
                    // Message from a next round. Keep for later!
                    log!(&mut self.inner.logger, "... DELAY! :(");
                    self.delay(received);
                    continue 'recv_loop;
                }
                Msg::CommMsg(CommMsg { contents, round_index }) => {
                    log!(
                        &mut self.inner.logger,
                        "... its a round-appropriate CommMsg with port {:?}",
                        received.recipient
                    );
                    assert_eq!(round_index, self.inner.round_index);
                    contents
                }
            };
            match current_content {
                CommMsgContents::Failure => match self.inner.family.parent_port {
                    Some(parent_port) => {
                        let announcement = Msg::CommMsg(CommMsg {
                            round_index: self.inner.round_index,
                            contents: CommMsgContents::Failure,
                        });
                        log!(
                            &mut self.inner.logger,
                            "Forwarding {:?} to parent with port {:?}",
                            &announcement,
                            parent_port
                        );
                        self.inner
                            .endpoint_exts
                            .get_mut(parent_port)
                            .expect("ss")
                            .endpoint
                            .send(announcement.clone())?;
                    }
                    None => return self.end_round_with_decision(Decision::Failure),
                },
                CommMsgContents::Elaborate { partial_oracle } => {
                    // Child controller submitted a subtree solution.
                    if !self.inner.family.children_ports.contains(&received.recipient) {
                        return Err(SyncErr::ElaborateFromNonChild);
                    }
                    let subtree_id = SubtreeId::ChildController { port: received.recipient };
                    log!(
                        &mut self.inner.logger,
                        "Received elaboration from child for subtree {:?}: {:?}",
                        subtree_id,
                        &partial_oracle
                    );
                    self.ephemeral.solution_storage.submit_and_digest_subtree_solution(
                        &mut self.inner.logger,
                        subtree_id,
                        partial_oracle,
                    );
                    if self.handle_locals_maybe_decide()? {
                        return Ok(());
                    }
                }
                CommMsgContents::Announce { decision } => {
                    if self.inner.family.parent_port != Some(received.recipient) {
                        return Err(SyncErr::AnnounceFromNonParent);
                    }
                    log!(
                        &mut self.inner.logger,
                        "Received ANNOUNCEMENT from from parent {:?}: {:?}",
                        received.recipient,
                        &decision
                    );
                    return self.end_round_with_decision(decision);
                }
                CommMsgContents::SendPayload { payload_predicate, payload } => {
                    // check that we expect to be able to receive payloads from this sender
                    assert_eq!(
                        Getter,
                        self.inner.endpoint_exts.get(received.recipient).unwrap().info.polarity
                    );

                    // message for some actor. Feed it to the appropriate actor
                    // and then give them another chance to run.
                    let subtree_id = port_to_holder.get(&received.recipient);
                    log!(
                        &mut self.inner.logger,
                        "Received SendPayload for subtree {:?} with pred {:?} and payload {:?}",
                        subtree_id,
                        &payload_predicate,
                        &payload
                    );
                    let channel_id = self
                        .inner
                        .endpoint_exts
                        .get(received.recipient)
                        .expect("UEHFU")
                        .info
                        .channel_id;
                    if payload_predicate.query(channel_id) != Some(true) {
                        // sender didn't preserve the invariant
                        return Err(SyncErr::PayloadPremiseExcludesTheChannel(channel_id));
                    }
                    match subtree_id {
                        None => {
                            // this happens when a message is sent to a component that has exited.
                            // It's safe to drop this message;
                            // The sender branch will certainly not be part of the solution
                        }
                        Some(PolyId::N) => {
                            // Message for NativeMachine
                            self.ephemeral.poly_n.as_mut().unwrap().sync_recv(
                                received.recipient,
                                &mut self.inner.logger,
                                payload,
                                payload_predicate,
                                &mut self.ephemeral.solution_storage,
                            );
                            if self.handle_locals_maybe_decide()? {
                                return Ok(());
                            }
                        }
                        Some(PolyId::P { index }) => {
                            // Message for protocol actor
                            let poly_p = &mut self.ephemeral.poly_ps[*index];

                            let m_ctx = PolyPContext {
                                my_subtree_id: SubtreeId::PolyP { index: *index },
                                inner: &mut self.inner,
                                solution_storage: &mut self.ephemeral.solution_storage,
                            };
                            use SyncRunResult as Srr;
                            let blocker = poly_p.poly_recv_run(
                                m_ctx,
                                &self.protocol_description,
                                received.recipient,
                                payload_predicate,
                                payload,
                            )?;
                            log!(
                                &mut self.inner.logger,
                                "... Fed the msg to PolyP {:?} and ran it to blocker {:?}",
                                subtree_id,
                                blocker
                            );
                            match blocker {
                                Srr::NoBranches => return Err(SyncErr::Inconsistent),
                                Srr::BlockingForRecv | Srr::AllBranchesComplete => {
                                    {
                                        let peeked = self
                                            .ephemeral
                                            .solution_storage
                                            .peek_new_locals()
                                            .collect::<Vec<_>>();
                                        log!(
                                            &mut self.inner.logger,
                                            "Got {} new controller-local solutions from RECV: {:?}",
                                            peeked.len(),
                                            peeked
                                        );
                                    }
                                    if self.handle_locals_maybe_decide()? {
                                        return Ok(());
                                    }
                                }
                            }
                        }
                    };
                }
            }
        }
    }
}
impl ControllerEphemeral {
    fn is_clear(&self) -> bool {
        self.solution_storage.is_clear()
            && self.poly_n.is_none()
            && self.poly_ps.is_empty()
            && self.mono_ps.is_empty()
            && self.port_to_holder.is_empty()
    }
    fn clear(&mut self) {
        self.solution_storage.clear();
        self.poly_n.take();
        self.poly_ps.clear();
        self.port_to_holder.clear();
    }
}
impl Into<PolyP> for MonoP {
    fn into(self) -> PolyP {
        PolyP {
            complete: Default::default(),
            incomplete: hashmap! {
                Predicate::new_trivial() =>
                BranchP {
                    state: self.state,
                    inbox: Default::default(),
                    outbox: Default::default(),
                    blocking_on: None,
                }
            },
            ports: self.ports,
        }
    }
}

impl From<EndpointErr> for SyncErr {
    fn from(e: EndpointErr) -> SyncErr {
        SyncErr::EndpointErr(e)
    }
}

impl MonoContext for MonoPContext<'_> {
    type D = ProtocolD;
    type S = ProtocolS;
    fn new_component(&mut self, moved_ports: HashSet<PortId>, init_state: Self::S) {
        log!(
            &mut self.inner.logger,
            "!! MonoContext callback to new_component with ports {:?}!",
            &moved_ports,
        );
        if moved_ports.is_subset(self.ports) {
            self.ports.retain(|x| !moved_ports.contains(x));
            self.mono_ps.push(MonoP { state: init_state, ports: moved_ports });
        } else {
            panic!("MachineP attempting to move alien port!");
        }
    }
    fn new_channel(&mut self) -> [PortId; 2] {
        let [a, b] = Endpoint::new_memory_pair();
        let channel_id = self.inner.channel_id_stream.next();

        let mut clos = |endpoint, polarity| {
            let endpoint_ext =
                EndpointExt { info: EndpointInfo { polarity, channel_id }, endpoint };
            let port = self.inner.endpoint_exts.alloc(endpoint_ext);
            let endpoint = &self.inner.endpoint_exts.get(port).unwrap().endpoint;
            let token = PortId::to_token(port);
            self.inner
                .messenger_state
                .poll
                .register(endpoint, token, Ready::readable(), PollOpt::edge())
                .expect("AAGAGGGGG");
            self.ports.insert(port);
            port
        };
        let [kp, kg] = [clos(a, Putter), clos(b, Getter)];
        log!(
            &mut self.inner.logger,
            "!! MonoContext callback to new_channel. returning ports {:?}!",
            [kp, kg],
        );
        [kp, kg]
    }
    fn new_random(&mut self) -> u64 {
        type Bytes8 = [u8; std::mem::size_of::<u64>()];
        let mut bytes = Bytes8::default();
        getrandom::getrandom(&mut bytes).unwrap();
        let val = unsafe { std::mem::transmute::<Bytes8, _>(bytes) };
        log!(
            &mut self.inner.logger,
            "!! MonoContext callback to new_random. returning val {:?}!",
            val,
        );
        val
    }
}

impl SolutionStorage {
    fn is_clear(&self) -> bool {
        self.subtree_id_to_index.is_empty()
            && self.subtree_solutions.is_empty()
            && self.old_local.is_empty()
            && self.new_local.is_empty()
    }
    fn clear(&mut self) {
        self.subtree_id_to_index.clear();
        self.subtree_solutions.clear();
        self.old_local.clear();
        self.new_local.clear();
    }
    pub(crate) fn reset(&mut self, subtree_ids: impl Iterator<Item = SubtreeId>) {
        self.subtree_id_to_index.clear();
        self.subtree_solutions.clear();
        self.old_local.clear();
        self.new_local.clear();
        for key in subtree_ids {
            self.subtree_id_to_index.insert(key, self.subtree_solutions.len());
            self.subtree_solutions.push(Default::default())
        }
    }

    pub(crate) fn peek_new_locals(&self) -> impl Iterator<Item = &Predicate> + '_ {
        self.new_local.iter()
    }

    pub(crate) fn iter_new_local_make_old(&mut self) -> impl Iterator<Item = Predicate> + '_ {
        let Self { old_local, new_local, .. } = self;
        new_local.drain().map(move |local| {
            old_local.insert(local.clone());
            local
        })
    }

    pub(crate) fn submit_and_digest_subtree_solution(
        &mut self,
        logger: &mut String,
        subtree_id: SubtreeId,
        predicate: Predicate,
    ) {
        log!(logger, "NEW COMPONENT SOLUTION {:?} {:?}", subtree_id, &predicate);
        let index = self.subtree_id_to_index[&subtree_id];
        let left = 0..index;
        let right = (index + 1)..self.subtree_solutions.len();

        let Self { subtree_solutions, new_local, old_local, .. } = self;
        let was_new = subtree_solutions[index].insert(predicate.clone());
        if was_new {
            let set_visitor = left.chain(right).map(|index| &subtree_solutions[index]);
            Self::elaborate_into_new_local_rec(
                logger,
                predicate,
                set_visitor,
                old_local,
                new_local,
            );
        }
    }

    fn elaborate_into_new_local_rec<'a, 'b>(
        logger: &mut String,
        partial: Predicate,
        mut set_visitor: impl Iterator<Item = &'b HashSet<Predicate>> + Clone,
        old_local: &'b HashSet<Predicate>,
        new_local: &'a mut HashSet<Predicate>,
    ) {
        if let Some(set) = set_visitor.next() {
            // incomplete solution. keep traversing
            for pred in set.iter() {
                if let Some(elaborated) = pred.union_with(&partial) {
                    Self::elaborate_into_new_local_rec(
                        logger,
                        elaborated,
                        set_visitor.clone(),
                        old_local,
                        new_local,
                    )
                }
            }
        } else {
            // recursive stop condition. `partial` is a local subtree solution
            if !old_local.contains(&partial) {
                // ... and it hasn't been found before
                log!(logger, "... storing NEW LOCAL SOLUTION {:?}", &partial);
                new_local.insert(partial);
            }
        }
    }
}
impl PolyContext for BranchPContext<'_, '_> {
    type D = ProtocolD;

    fn is_firing(&mut self, port: PortId) -> Option<bool> {
        assert!(self.ports.contains(&port));
        let channel_id = self.m_ctx.inner.endpoint_exts.get(port).unwrap().info.channel_id;
        let val = self.predicate.query(channel_id);
        log!(
            &mut self.m_ctx.inner.logger,
            "!! PolyContext callback to is_firing by {:?}! returning {:?}",
            self.m_ctx.my_subtree_id,
            val,
        );
        val
    }
    fn read_msg(&mut self, port: PortId) -> Option<&Payload> {
        assert!(self.ports.contains(&port));
        let val = self.inbox.get(&port);
        log!(
            &mut self.m_ctx.inner.logger,
            "!! PolyContext callback to read_msg by {:?}! returning {:?}",
            self.m_ctx.my_subtree_id,
            val,
        );
        val
    }
}