예제 #1
0
        private void RunNSenders(
            int n, int f,
            DeliveryServiceMode mode = DeliveryServiceMode.TAKE_RANDOM,
            double repeatProbability = .0
            )
        {
            SetUpAllHonestNSenders(n, f);
            _deliveryService.RepeatProbability = repeatProbability;
            _deliveryService.Mode = mode;
            var rnd         = new Random();
            var mutePlayers = new List <int>();

            while (_deliveryService.MutedPlayers.Count < f)
            {
                var tmp = rnd.Next(0, n);
                _deliveryService.MutePlayer(tmp);
                mutePlayers.Add(tmp);
            }

            for (var i = 0; i < n; ++i)
            {
                _broadcasters[i].InternalRequest(new ProtocolRequest <ReliableBroadcastId, EncryptedShare?>
                                                     (_resultInterceptors[i].Id, new ReliableBroadcastId(i, 0), _testShare));

                for (var j = 0; j < n; ++j)
                {
                    if (j == i)
                    {
                        continue;
                    }
                    _broadcasters[i].InternalRequest(new ProtocolRequest <ReliableBroadcastId, EncryptedShare?>
                                                         (_resultInterceptors[i].Id, new ReliableBroadcastId(j, 0), null));
                }
            }


            for (var i = 0; i < n; ++i)
            {
                if (!mutePlayers.Contains(i))
                {
                    _broadcasts[i].WaitFinish();
                }
            }

            for (var i = 0; i < n; ++i)
            {
                if (!mutePlayers.Contains(i))
                {
                    Assert.AreEqual(_testShare, _resultInterceptors[i].Result);
                }
            }

            for (var i = 0; i < n; ++i)
            {
                if (!mutePlayers.Contains(i))
                {
                    Assert.IsTrue(_broadcasts[i].Terminated, $"protocol {i} did not terminate");
                }
            }
        }
예제 #2
0
        public void RunBinaryAgreementRandom(
            int n, int f, DeliveryServiceMode mode,
            int muteCnt = 0, double repeatProbability = .0
            )
        {
            SetUpAllHonest(n, f);
            _deliveryService.RepeatProbability = repeatProbability;
            _deliveryService.Mode = mode;
            while (_deliveryService.MutedPlayers.Count < muteCnt)
            {
                _deliveryService.MutePlayer(_rnd.Next(0, n));
            }

            var used = new BoolSet();

            for (var i = 0; i < n; ++i)
            {
                var cur = _rnd.Next() % 2 == 1;
                used = used.Add(cur);
                _broadcasters[i].InternalRequest(new ProtocolRequest <BinaryAgreementId, bool>(
                                                     _resultInterceptors[i].Id, (_broadcasts[i].Id as BinaryAgreementId) !, cur
                                                     ));
            }

            for (var i = 0; i < n; ++i)
            {
                if (_deliveryService.MutedPlayers.Contains(i))
                {
                    continue;
                }
                _broadcasts[i].WaitResult();
            }

            _deliveryService.WaitFinish();

            for (var i = 0; i < n; ++i)
            {
                if (_deliveryService.MutedPlayers.Contains(i))
                {
                    continue;
                }

                Assert.AreEqual(
                    _resultInterceptors[i].ResultSet, 1,
                    $"protocol has {i} emitted result not once but {_resultInterceptors[i].ResultSet}"
                    );
            }

            bool?res = null;

            for (var i = 0; i < n; ++i)
            {
                if (_deliveryService.MutedPlayers.Contains(i))
                {
                    continue;
                }
                var ans = _resultInterceptors[i].Result;
                res ??= ans;
                Assert.AreEqual(res, _resultInterceptors[i].Result);
            }

            Assert.IsNotNull(res);
            Assert.True(used.Contains(res !.Value));

            for (var i = 0; i < n; ++i)
            {
                _broadcasts[i].Terminate();
                _broadcasts[i].WaitFinish();
            }
        }