Example #1
0
        private void SetUpAllHonest(int n, int f)
        {
            _deliveryService    = new DeliveryService();
            _broadcasts         = new IConsensusProtocol[n];
            _broadcasters       = new IConsensusBroadcaster[n];
            _resultInterceptors = new ProtocolInvoker <BinaryAgreementId, bool> [n];
            _privateKeys        = new IPrivateConsensusKeySet[n];
            var keygen  = new TrustedKeyGen(n, f);
            var shares  = keygen.GetPrivateShares().ToArray();
            var pubKeys = new PublicKeySet(shares.Select(share => share.GetPublicKeyShare()), f);

            _publicKeys = new PublicConsensusKeySet(n, f, null !, pubKeys, Enumerable.Empty <ECDSAPublicKey>());
            for (var i = 0; i < n; ++i)
            {
                _resultInterceptors[i] = new ProtocolInvoker <BinaryAgreementId, bool>();
                _privateKeys[i]        = new PrivateConsensusKeySet(null !, null !, shares[i]);
                _broadcasters[i]       = new BroadcastSimulator(i, _publicKeys, _privateKeys[i], _deliveryService, true);
            }

            for (uint i = 0; i < n; ++i)
            {
                _broadcasts[i] = new BinaryAgreement(new BinaryAgreementId(0, 0), _publicKeys, _broadcasters[i]);
                _broadcasters[i].RegisterProtocols(new[] { _broadcasts[i], _resultInterceptors[i] });
            }
        }
Example #2
0
        private IConsensusProtocol?EnsureProtocol(IProtocolIdentifier id)
        {
            ValidateId(id);
            if (_registry.TryGetValue(id, out var existingProtocol))
            {
                return(existingProtocol);
            }
            Logger.LogTrace($"Creating protocol {id} on demand");
            if (_terminated)
            {
                Logger.LogTrace($"Protocol {id} not created since broadcaster is terminated");
                return(null);
            }

            switch (id)
            {
            case BinaryBroadcastId bbId:
                var bb = new BinaryBroadcast(bbId, _validators, this);
                RegisterProtocols(new[] { bb });
                return(bb);

            case CoinId coinId:
                var coin = new CommonCoin(
                    coinId, _validators,
                    _wallet.GetThresholdSignatureKeyForBlock((ulong)_era - 1) ??
                    throw new InvalidOperationException($"No TS keys present for era {_era}"),
                    this
                    );
                RegisterProtocols(new[] { coin });
                return(coin);

            case ReliableBroadcastId rbcId:
                var rbc = new ReliableBroadcast(rbcId, _validators, this);
                RegisterProtocols(new[] { rbc });
                return(rbc);

            case BinaryAgreementId baId:
                var ba = new BinaryAgreement(baId, _validators, this);
                RegisterProtocols(new[] { ba });
                return(ba);

            case CommonSubsetId acsId:
                var acs = new CommonSubset(acsId, _validators, this);
                RegisterProtocols(new[] { acs });
                return(acs);

            case HoneyBadgerId hbId:
                var hb = new HoneyBadger(
                    hbId, _validators,
                    _wallet.GetTpkePrivateKeyForBlock((ulong)_era - 1)
                    ?? throw new InvalidOperationException($"No TPKE keys present for era {_era}"),
                    this
                    );
                RegisterProtocols(new[] { hb });
                return(hb);

            case RootProtocolId rootId:
                var root = new RootProtocol(rootId, _validators, _wallet.EcdsaKeyPair.PrivateKey,
                                            this, _validatorAttendanceRepository, StakingContract.CycleDuration,
                                            HardforkHeights.IsHardfork_9Active((ulong)_era));
                RegisterProtocols(new[] { root });
                return(root);

            default:
                throw new Exception($"Unknown protocol type {id}");
            }
        }