Example #1
0
        public IPublicConsensusKeySet?GetValidators(long afterBlock)
        {
            Logger.LogTrace($"Getting validators after block {afterBlock}");
            try
            {
                var state = _snapshotIndexRepository.GetSnapshotForBlock((ulong)afterBlock).Validators
                            .GetConsensusState();
                var n = state.Validators.Length;
                var f = (n - 1) / 3;
                Logger.LogTrace($"Fetched {n} validators f={f}");
                return(new PublicConsensusKeySet(
                           n, f,
                           PublicKey.FromBytes(state.TpkePublicKey),
                           new PublicKeySet(
                               state.Validators.Select(v =>
                                                       Crypto.ThresholdSignature.PublicKey.FromBytes(v.ThresholdSignaturePublicKey)),
                               f
                               ),
                           state.Validators.Select(v => v.PublicKey)
                           ));
            }
            catch (Exception)
            {
                // ignored
            }

            return(null);
        }
Example #2
0
 public PublicConsensusKeySet(int n, int f,
                              PublicKey tpkePublicKey,
                              PublicKeySet thresholdSignaturePublicKeySet,
                              IEnumerable <ECDSAPublicKey> ecdsaPublicKeys
                              )
 {
     N             = n;
     F             = f;
     TpkePublicKey = tpkePublicKey;
     ThresholdSignaturePublicKeySet = thresholdSignaturePublicKeySet;
     _ecdsaPublicKeys = ecdsaPublicKeys.ToList();
 }
Example #3
0
        public ExecutionStatus KeyGenConfirm(UInt256 cycle, byte[] tpkePublicKey, byte[][] thresholdSignaturePublicKeys,
                                             SystemContractExecutionFrame frame)
        {
            Logger.LogDebug(
                $"KeyGenConfirm({tpkePublicKey.ToHex()}, [{string.Join(", ", thresholdSignaturePublicKeys.Select(s => s.ToHex()))}])");
            if (cycle.ToBigInteger() != GetConsensusGeneration(frame))
            {
                Logger.LogWarning($"Invalid cycle: {cycle}, now is {GetConsensusGeneration(frame)}");
                return(ExecutionStatus.Ok);
            }

            frame.ReturnValue = new byte[] { };
            frame.UseGas(GasMetering.KeygenConfirmCost);
            var players = thresholdSignaturePublicKeys.Length;
            var faulty  = (players - 1) / 3;

            UInt256      keyringHash;
            PublicKeySet tsKeys;

            try
            {
                tsKeys = new PublicKeySet(
                    thresholdSignaturePublicKeys.Select(x => Lachain.Crypto.ThresholdSignature.PublicKey.FromBytes(x)),
                    faulty
                    );
                var tpkeKey = PublicKey.FromBytes(tpkePublicKey);
                keyringHash = tpkeKey.ToBytes().Concat(tsKeys.ToBytes()).Keccak();
            }
            catch
            {
                Logger.LogError("GovernanceContract is halted in KeyGenConfirm");
                return(ExecutionStatus.ExecutionHalted);
            }

            var gen   = GetConsensusGeneration(frame);
            var votes = GetConfirmations(keyringHash.ToBytes(), gen);

            SetConfirmations(keyringHash.ToBytes(), gen, votes + 1);

            Logger.LogDebug($"KeygenConfirm: {votes + 1} collected for this keyset");
            if (votes + 1 != players - faulty)
            {
                return(ExecutionStatus.Ok);
            }
            Logger.LogDebug($"KeygenConfirm: succeeded since collected {votes + 1} votes");
            _lastSuccessfulKeygenBlock.Set(new BigInteger(frame.InvocationContext.Receipt.Block).ToUInt256().ToBytes());
            SetPlayersCount(players);
            SetTSKeys(tsKeys);
            SetTpkeKey(tpkePublicKey);

            Emit(GovernanceInterface.EventKeygenConfirm, tpkePublicKey, thresholdSignaturePublicKeys);
            return(ExecutionStatus.Ok);
        }
Example #4
0
        public void UpdateValidators(
            IEnumerable <ECDSAPublicKey> ecdsaKeys, PublicKeySet tsKeys, PublicKey tpkePublicKey
            )
        {
            var state = new ConsensusState(
                tpkePublicKey.ToBytes(),
                ecdsaKeys
                .Zip(tsKeys.Keys, (ecdsaKey, tsKey) => new ValidatorCredentials(ecdsaKey, tsKey.ToBytes()))
                .ToArray()
                );

            SetConsensusState(state);
        }
Example #5
0
        private PublicKey GetTpkeKey()
        {
            var tpkePublicKey = _tpkeKey.Get();

            return(PublicKey.FromBytes(tpkePublicKey));
        }