Пример #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);
        }
Пример #2
0
        private IBlockchainSnapshot?GetSnapshotByTag(string tag)
        {
            switch (tag)
            {
            case "latest":
                return(_stateManager.LastApprovedSnapshot);

            case "earliest":
                return(_snapshotIndexer.GetSnapshotForBlock(0));

            case "pending":
                return(_stateManager.PendingSnapshot);

            default:
            {
                var blockNum = tag.HexToUlong();
                try
                {
                    return(_snapshotIndexer.GetSnapshotForBlock(blockNum));
                }
                catch (Exception e)
                {
                    throw new Exception("Block with " + tag + " doesn't exist.");
                }
            }
            }
        }
Пример #3
0
        private void CheckSnapshots(ulong depth, ulong totalBlocks)
        {
            var fromBlock = StartingBlockToKeep(depth, totalBlocks);

            Logger.LogTrace($"Checking snapshots for blocks in range [{fromBlock} , {totalBlocks}]");
            for (var block = fromBlock; block <= totalBlocks; block++)
            {
                try
                {
                    var blockchainSnapshot = _snapshotIndexRepository.GetSnapshotForBlock(block);
                    var snapshots          = blockchainSnapshot.GetAllSnapshot();
                    foreach (var snapshot in snapshots)
                    {
                        if (!snapshot.IsTrieNodeHashesOk())
                        {
                            throw new Exception($"Consistency check failed for {snapshot} of block {block}");
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception($"Got exception trying to get snapshot for block {block}, "
                                        + $"exception:\n{exception}");
                }
            }
        }
Пример #4
0
        public JObject?GetStateByNumber(string blockTag)
        {
            var blockNumber = GetBlockNumberByTag(blockTag);

            if (blockNumber == null)
            {
                return(null);
            }
            IBlockchainSnapshot blockchainSnapshot = _snapshotIndexer.GetSnapshotForBlock((ulong)blockNumber);
            var state = new JObject {
            };

            string[]    trieNames = new string[] { "Balances", "Contracts", "Storage", "Transactions", "Blocks", "Events", "Validators" };
            ISnapshot[] snapshots = blockchainSnapshot.GetAllSnapshot();
            for (var i = 0; i < trieNames.Length; i++)
            {
                state[trieNames[i]]          = Web3DataFormatUtils.Web3Trie(snapshots[i].GetState());
                state[trieNames[i] + "Root"] = Web3DataFormatUtils.Web3Number(snapshots[i].Version);
            }

            return(state);
        }
Пример #5
0
 private bool VerifyValidatorSet(IReadOnlyCollection <ECDSAPublicKey> keys, ulong height)
 {
     try
     {
         IReadOnlyCollection <ECDSAPublicKey> validators = _snapshotIndexRepository.GetSnapshotForBlock(height).Validators
                                                           .GetValidatorsPublicKeys().ToArray();
         var result = validators.All(v => keys.Contains(v)) && keys.All(k => validators.Contains(k));
         if (!result)
         {
             Logger.LogDebug("Validator set from peer block does not match with validator set from snapshot");
         }
         return(result);
     }
     catch (Exception exception)
     {
         Logger.LogTrace($"Got exception while matching validator set: {exception}");
         return(false);
     }
 }