예제 #1
0
 private void ValidateId(IProtocolIdentifier id)
 {
     if (id.Era != _era)
     {
         throw new InvalidOperationException($"Era mismatched, expected {_era} got message with {id.Era}");
     }
 }
예제 #2
0
 protected AbstractProtocol(
     IPublicConsensusKeySet wallet,
     IProtocolIdentifier id,
     IConsensusBroadcaster broadcaster
     )
 {
     _thread = new Thread(Start)
     {
         IsBackground = true
     };
     _thread.Start();
     Broadcaster = broadcaster;
     Id          = id;
     Wallet      = wallet;
 }
예제 #3
0
        private void CheckRequest(IProtocolIdentifier id)
        {
            if (Registry.ContainsKey(id))
            {
                return;
            }
            if (Terminated)
            {
                return;
            }

            switch (id)
            {
            case BinaryBroadcastId bbId:
                RegisterProtocols(new[] { new BinaryBroadcast(bbId, _wallet, this) });
                break;

            case CoinId coinId:
                RegisterProtocols(new[]
                                  { new CommonCoin(coinId, _wallet, _privateKeys.ThresholdSignaturePrivateKeyShare, this) });
                break;

            case ReliableBroadcastId rbcId:
                RegisterProtocols(new[] { new ReliableBroadcast(rbcId, _wallet, this) });
                break;

            case BinaryAgreementId baId:
                RegisterProtocols(new[] { new BinaryAgreement(baId, _wallet, this) });
                break;

            case CommonSubsetId acsId:
                RegisterProtocols(new[] { new CommonSubset(acsId, _wallet, this) });
                break;

            case RootProtocolId _:
                throw new Exception("cannot instantiate root protocol in BroadcastSimulator");

            default:
                throw new Exception($"Unknown protocol type {id}");
            }
        }
예제 #4
0
 public bool Equals(IProtocolIdentifier other)
 {
     return(Equals((object)other));
 }
예제 #5
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}");
            }
        }
예제 #6
0
 public IConsensusProtocol?GetProtocolById(IProtocolIdentifier id)
 {
     return(_registry.TryGetValue(id, out var value) ? value : null);
 }
예제 #7
0
 public IConsensusProtocol GetProtocolById(IProtocolIdentifier id)
 {
     return(Registry[id]);
 }
예제 #8
0
 public ProtocolRequest(IProtocolIdentifier from, TIdType id, TInputType input)
 {
     From  = from;
     To    = id;
     Input = input;
 }