private void HandleBValMessage(int sender, BValMessage bval) { if (bval.Epoch != _broadcastId.Epoch || bval.Agreement != _broadcastId.Agreement) { throw new ArgumentException("era, agreement or epoch of message mismatched"); } var b = bval.Value ? 1 : 0; if (_receivedValues[sender].Contains(b == 1)) { Logger.LogTrace($"{_broadcastId}: double receive message {bval} from {sender}"); return; } _receivedValues[sender].Add(b == 1); ++_receivedCount[b]; if (!_wasBvalBroadcasted[b] && _receivedCount[b] >= F + 1) { BroadcastBVal(bval.Value); } if (_receivedCount[b] < 2 * F + 1) { return; } if (_binValues.Contains(b == 1)) { return; } _binValues = _binValues.Add(b == 1); if (_binValues.Count() == 1) { Broadcaster.Broadcast(CreateAuxMessage(b)); } RevisitAuxMessages(); RevisitConfMessages(); }
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(); } }