Пример #1
0
        public int CompareTo(CoinSketch other)
        {
            var timeComparison = Time.CompareTo(other.Time);

            if (timeComparison != 0)
            {
                return(timeComparison);
            }
            return(CoinId.CompareTo(other.CoinId));
        }
Пример #2
0
 public Coin(CoinId id,
             BlockchainAsset asset,
             decimal value,
             string address,
             string redeem)
 {
     Id      = id;
     Asset   = asset;
     Value   = value;
     Address = address;
     Redeem  = redeem;
 }
Пример #3
0
 /// <summary>
 /// Coin to spend for the transaction.
 /// </summary>
 /// <param name="coinId">Reference to the coin which should be spend.</param>
 /// <param name="asset">Asset of the coin.</param>
 /// <param name="value">Coin value to spend.</param>
 /// <param name="address">Address that owns the coin.</param>
 /// <param name="addressContext">
 /// Optional.
 /// Address context associated with the owner address.
 /// </param>
 /// <param name="addressNonce">
 /// Optional.
 /// Nonce number of the transaction for the owner address.
 /// </param>
 public CoinToSpend(
     CoinId coinId,
     Asset asset,
     UMoney value,
     Address address,
     Base64String addressContext = null,
     long?addressNonce           = null)
 {
     Coin           = coinId ?? throw RequestValidationException.ShouldBeNotNull(nameof(coinId));
     Asset          = asset ?? throw RequestValidationException.ShouldBeNotNull(nameof(asset));
     Value          = value;
     Address        = address ?? throw RequestValidationException.ShouldBeNotNull(nameof(address));
     AddressContext = addressContext;
     AddressNonce   = addressNonce;
 }
Пример #4
0
        public void Dispatch(ConsensusMessage message, int from)
        {
            if (_terminated)
            {
                Logger.LogTrace($"Era {_era} is already finished, skipping Dispatch");
                return;
            }

            if (message.Validator.Era != _era)
            {
                throw new InvalidOperationException(
                          $"Message for era {message.Validator.Era} dispatched to era {_era}");
            }

            if (!ValidateMessage(message))
            {
                Logger.LogWarning(
                    $"Faulty behaviour: null value in required field of the consensus message from validator: {from}"
                    + $" ({GetPublicKeyById(from)!.ToHex()}). Discarding message as it could crash targeted protocol.");
                return;
            }

            switch (message.PayloadCase)
            {
            case ConsensusMessage.PayloadOneofCase.Bval:
                var idBval = new BinaryBroadcastId(message.Validator.Era, message.Bval.Agreement,
                                                   message.Bval.Epoch);
                EnsureProtocol(idBval)?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.Aux:
                var idAux = new BinaryBroadcastId(message.Validator.Era, message.Aux.Agreement, message.Aux.Epoch);
                EnsureProtocol(idAux)?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.Conf:
                var idConf = new BinaryBroadcastId(message.Validator.Era, message.Conf.Agreement,
                                                   message.Conf.Epoch);
                EnsureProtocol(idConf)?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.Coin:
                var idCoin = new CoinId(message.Validator.Era, message.Coin.Agreement, message.Coin.Epoch);
                EnsureProtocol(idCoin)?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.Decrypted:
                var hbbftId = new HoneyBadgerId((int)message.Validator.Era);
                EnsureProtocol(hbbftId)?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.ValMessage:
                var reliableBroadcastId = new ReliableBroadcastId(message.ValMessage.SenderId, (int)message.Validator.Era);
                EnsureProtocol(reliableBroadcastId)?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.EchoMessage:
                var rbIdEchoMsg = new ReliableBroadcastId(message.EchoMessage.SenderId, (int)message.Validator.Era);
                EnsureProtocol(rbIdEchoMsg)?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.ReadyMessage:
                var rbIdReadyMsg = new ReliableBroadcastId(message.ReadyMessage.SenderId, (int)message.Validator.Era);
                EnsureProtocol(rbIdReadyMsg)?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.SignedHeaderMessage:
                var rootId = new RootProtocolId(message.Validator.Era);
                EnsureProtocol(rootId)?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            default:
                throw new InvalidOperationException($"Unknown message type {message}");
            }
        }
Пример #5
0
 public override string ToString()
 {
     return($"{CoinId.ToString()}{Index.ToString()}{CoreClockDelta.ToString()}{MemoryClockDelta.ToString()}{CoreVoltage.ToString()}{MemoryVoltage.ToString()}{PowerCapacity.ToString()}{TempLimit.ToString()}{IsAutoFanSpeed.ToString()}{Cool.ToString()}");
 }
Пример #6
0
 public string GetId()
 {
     return($"{CoinId.ToString()}_{Index.ToString()}");
 }
Пример #7
0
        private void TryProgressEpoch()
        {
            CheckResult();
            while (_result == null || !_wasRepeat)
            {
                if (_currentEpoch % 2 == 0)
                {
                    // epoch mod 2 = 0 -> we have not yet initiated BB
                    if (_currentEpoch != 0 && !_coins.ContainsKey(_currentEpoch - 1))
                    {
                        /*    Logger.LogTrace(
                         *      $"{_agreementId}: can't progress epoch, blocked, coin (Ep={_currentEpoch - 1}) not present"); */
                        return; // we cannot progress since coin is not tossed and estimate is not correct
                    }

                    /*    Logger.LogTrace(
                     *      $"Epoch progressed, coin (Ep={_currentEpoch - 1}) is present " +
                     *      $"with value {_currentEpoch > 0 && _coins[_currentEpoch - 1]}"
                     *  ); */
                    // we have right to calculate new estimate and proceed
                    if (_currentEpoch != 0)
                    {
                        var s = _coins[_currentEpoch - 1];
                        _estimate = _currentValues.Values().First();

                        if (_currentValues.Count() == 1 && _result == null)
                        {
                            if (_estimate == s)
                            {
                                // we are winners!
                                _resultEpoch = _currentEpoch;
                                _result      = _estimate;
                                CheckResult();
                                Logger.LogTrace($"{_agreementId}: result = {_result} achieved at Ep={_currentEpoch}");
                            }
                        }
                        else if (_result == s)
                        {
                            if (_currentEpoch > _resultEpoch)
                            {
                                Logger.LogTrace(
                                    $"{_agreementId}: value repeated at Ep={_currentEpoch}, result is already obtained: {_result}. Terminating protocol"
                                    );
                                _wasRepeat = true;
                                Terminate();
                            }
                        }
                        else
                        {
                            _estimate = s;
                        }
                    }

                    if (_result != null)
                    {
                        _estimate = _result.Value;
                    }

                    // here we start new BB assuming that current estimate is correct
                    var broadcastId = new BinaryBroadcastId(_agreementId.Era, _agreementId.AssociatedValidatorId,
                                                            _currentEpoch);
                    Broadcaster.InternalRequest(
                        new ProtocolRequest <BinaryBroadcastId, bool>(Id, broadcastId, _estimate)
                        );
                    _currentEpoch += 1;
                }
                else
                {
                    // epoch mod 2 = 1 -> we have not yet tossed coin
                    if (!_binaryBroadcastsResults.ContainsKey(_currentEpoch - 1))
                    {
                        /*    Logger.LogTrace(
                         *      $"{_agreementId}: can't progress epoch, blocked, BB (Ep={_currentEpoch - 1}) not present"
                         *  ); */
                        return; // we cannot progress since BB is not completed
                    }

                    //    Logger.LogTrace($"{_agreementId}: epoch progressed, BB (Ep={_currentEpoch - 1}) is present");

                    _currentValues = _binaryBroadcastsResults[_currentEpoch - 1];
                    var coinId = new CoinId(_agreementId.Era, _agreementId.AssociatedValidatorId, _currentEpoch);
                    if ((_currentEpoch / 2) % 3 == 2)
                    {
                        Broadcaster.InternalRequest(new ProtocolRequest <CoinId, object?>(Id, coinId, null));
                    }
                    else
                    {
                        _coins[_currentEpoch] = ((_currentEpoch / 2) % 3) != 0;
                    }

                    _currentEpoch += 1;
                }
            }
        }
Пример #8
0
        public void Dispatch(ConsensusMessage message, int from)
        {
            if (_silenced.Contains(from))
            {
                return;
            }
            if (_silenced.Contains(GetMyId()))
            {
                return;
            }

            switch (message.PayloadCase)
            {
            case ConsensusMessage.PayloadOneofCase.Bval:
                var idBval = new BinaryBroadcastId(message.Validator.Era, message.Bval.Agreement,
                                                   message.Bval.Epoch);
                CheckRequest(idBval);
                Registry[idBval]?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.Aux:
                var idAux = new BinaryBroadcastId(message.Validator.Era, message.Aux.Agreement, message.Aux.Epoch);
                CheckRequest(idAux);
                Registry[idAux]?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.Conf:
                var idConf = new BinaryBroadcastId(message.Validator.Era, message.Conf.Agreement,
                                                   message.Conf.Epoch);
                CheckRequest(idConf);
                Registry[idConf]?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.Coin:
                var idCoin = new CoinId(message.Validator.Era, message.Coin.Agreement, message.Coin.Epoch);
                CheckRequest(idCoin);
                Registry[idCoin]?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.Decrypted:
                var hbbftId = new HoneyBadgerId((int)message.Validator.Era);
                CheckRequest(hbbftId);
                Registry[hbbftId]?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.ReadyMessage:
                var rbIdReadyMsg =
                    new ReliableBroadcastId(message.ReadyMessage.SenderId, (int)message.Validator.Era);
                CheckRequest(rbIdReadyMsg);
                Registry[rbIdReadyMsg]?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.ValMessage:
                var reliableBroadcastId =
                    new ReliableBroadcastId(message.ValMessage.SenderId, (int)message.Validator.Era);
                CheckRequest(reliableBroadcastId);
                Registry[reliableBroadcastId]?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.EchoMessage:
                var rbIdEchoMsg =
                    new ReliableBroadcastId(message.EchoMessage.SenderId, (int)message.Validator.Era);
                CheckRequest(rbIdEchoMsg);
                Registry[rbIdEchoMsg]?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            case ConsensusMessage.PayloadOneofCase.SignedHeaderMessage:
                var idRoot = new RootProtocolId(message.Validator.Era);
                CheckRequest(idRoot);
                Registry[idRoot]?.ReceiveMessage(new MessageEnvelope(message, from));
                break;

            default:
                throw new InvalidOperationException($"Unknown message type {message.PayloadCase}");
            }
        }