Example #1
0
        public bool ValidateParams(BlockHeader parent, BlockHeader header)
        {
            long number = header.Number;
            // Retrieve the snapshot needed to validate this header and cache it
            Snapshot snapshot = _snapshotManager.GetOrCreateSnapshot(number - 1, header.ParentHash);

            // Resolve the authorization key and check against signers
            header.Author ??= _snapshotManager.GetBlockSealer(header);
            Address signer = header.Author;

            if (!snapshot.Signers.ContainsKey(signer))
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block signer {signer} - not authorized to sign a block");
                }
                return(false);
            }

            if (_snapshotManager.HasSignedRecently(snapshot, number, signer))
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block signer {signer} - the signer is among recents");
                }
                return(false);
            }

            // Ensure that the difficulty corresponds to the turn-ness of the signer
            bool inTurn = _snapshotManager.IsInTurn(snapshot, header.Number, signer);

            if (inTurn && header.Difficulty != Clique.DifficultyInTurn)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block difficulty {header.Difficulty} - should be in-turn {Clique.DifficultyInTurn}");
                }
                return(false);
            }

            if (!inTurn && header.Difficulty != Clique.DifficultyNoTurn)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block difficulty {header.Difficulty} - should be no-turn {Clique.DifficultyNoTurn}");
                }
                return(false);
            }

            bool isEpochTransition = IsEpochTransition(header.Number);

            // Checkpoint blocks need to enforce zero beneficiary
            if (isEpochTransition && header.Beneficiary != Address.Zero)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block beneficiary ({header.Beneficiary}) - should be empty on checkpoint");
                }
                return(false);
            }

            if (isEpochTransition && header.Nonce != Clique.NonceDropVote)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block nonce ({header.Nonce}) - should be zeroes on checkpoints");
                }
                return(false);
            }

            // Ensure that the extra-data contains a signer list on checkpoint, but none otherwise
            int singersBytes = header.ExtraData.Length - Clique.ExtraVanityLength - Clique.ExtraSealLength;

            if (!isEpochTransition && singersBytes != 0)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block extra-data ({header.ExtraData}) - should be empty on non-checkpoints");
                }
                return(false);
            }

            if (isEpochTransition && singersBytes % Address.ByteLength != 0)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block nonce ({header.ExtraData}) - should contain a list of signers on checkpoints");
                }
                return(false);
            }

            // Nonce must be 0x00..0 or 0xff..f, zeroes enforced on checkpoints
            if (header.Nonce != Clique.NonceAuthVote && header.Nonce != Clique.NonceDropVote)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block nonce ({header.Nonce})");
                }
                return(false);
            }

            if (header.ExtraData.Length < Clique.ExtraVanityLength)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn("Invalid block extra data length - missing vanity");
                }
                return(false);
            }

            if (header.ExtraData.Length < Clique.ExtraVanityLength + Clique.ExtraSealLength)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn("Invalid block extra data length - missing seal");
                }
                return(false);
            }

            // Ensure that the mix digest is zero as we don't have fork protection currently
            if (header.MixHash != Keccak.Zero)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block mix hash ({header.MixHash}) - should be zeroes");
                }
                return(false);
            }

            // Ensure that the block doesn't contain any uncles which are meaningless in PoA
            if (header.UnclesHash != Keccak.OfAnEmptySequenceRlp)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block uncles hash ({header.UnclesHash}) - uncles are meaningless in Clique");
                }
                return(false);
            }

            if (header.Difficulty != Clique.DifficultyInTurn && header.Difficulty != Clique.DifficultyNoTurn)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block difficulty ({header.Difficulty}) - should be {Clique.DifficultyInTurn} or {Clique.DifficultyNoTurn}");
                }
                return(false);
            }

            return(ValidateCascadingFields(parent, header));
        }
Example #2
0
        private Snapshot Apply(Snapshot original, List <BlockHeader> headers, ulong epoch)
        {
            // Allow passing in no headers for cleaner code
            if (headers.Count == 0)
            {
                return(original);
            }

            // Sanity check that the headers can be applied
            for (int i = 0; i < headers.Count - 1; i++)
            {
                if (headers[i].Number != original.Number + i + 1)
                {
                    throw new InvalidOperationException("Invalid voting chain");
                }
            }

            // Iterate through the headers and create a new snapshot
            Snapshot snapshot = (Snapshot)original.Clone();

            foreach (BlockHeader header in headers)
            {
                // Remove any votes on checkpoint blocks
                long number = header.Number;
                if ((ulong)number % epoch == 0)
                {
                    snapshot.Votes.Clear();
                    snapshot.Tally.Clear();
                }

                // Resolve the authorization key and check against signers
                Address signer = header.Author;
                if (!snapshot.Signers.ContainsKey(signer))
                {
                    throw new InvalidOperationException("Unauthorized signer");
                }
                if (HasSignedRecently(snapshot, number, signer))
                {
                    throw new InvalidOperationException($"Recently signed (trying to sign {number} when last signed {snapshot.Signers[signer]} with {snapshot.Signers.Count} signers)");
                }

                snapshot.Signers[signer] = number;

                // Header authorized, discard any previous votes for the signer
                for (int i = 0; i < snapshot.Votes.Count; i++)
                {
                    Vote vote = snapshot.Votes[i];
                    if (vote.Signer == signer && vote.Address == header.Beneficiary)
                    {
                        // Uncast the vote from the cached tally
                        Uncast(snapshot, vote.Address, vote.Authorize);
                        // Uncast the vote from the chronological list
                        snapshot.Votes.RemoveAt(i);
                        break;
                    }
                }

                // Tally up the new vote from the signer
                bool authorize = header.Nonce == Clique.NonceAuthVote;
                if (Cast(snapshot, header.Beneficiary, authorize))
                {
                    Vote vote = new Vote(signer, number, header.Beneficiary, authorize);
                    snapshot.Votes.Add(vote);
                }

                // If the vote passed, update the list of signers
                Tally tally = snapshot.Tally[header.Beneficiary];
                if (tally.Votes > snapshot.Signers.Count / 2)
                {
                    if (tally.Authorize)
                    {
                        snapshot.Signers.Add(header.Beneficiary, 0);
                    }
                    else
                    {
                        snapshot.Signers.Remove(header.Beneficiary);
                    }

                    // Discard any previous votes the deauthorized signer cast
                    for (int i = 0; i < snapshot.Votes.Count; i++)
                    {
                        if (snapshot.Votes[i].Signer == header.Beneficiary)
                        {
                            // Uncast the vote from the cached tally
                            if (Uncast(snapshot, snapshot.Votes[i].Address, snapshot.Votes[i].Authorize))
                            {
                                // Uncast the vote from the chronological list
                                snapshot.Votes.RemoveAt(i);
                                i--;
                            }
                        }
                    }

                    // Discard any previous votes around the just changed account
                    for (int i = 0; i < snapshot.Votes.Count; i++)
                    {
                        if (snapshot.Votes[i].Address == header.Beneficiary)
                        {
                            snapshot.Votes.RemoveAt(i);
                            i--;
                        }
                    }

                    snapshot.Tally.Remove(header.Beneficiary);
                }
            }

            snapshot.Number += headers.Count;

            // was this needed?
//            snapshot.Hash = headers[headers.Count - 1].CalculateHash();
            snapshot.Hash = headers[^ 1].Hash;
Example #3
0
 public bool IsInTurn(Snapshot snapshot, long number, Address signer)
 {
     return((long)number % snapshot.Signers.Count == snapshot.Signers.IndexOfKey(signer));
 }
Example #4
0
        public bool IsValidVote(Snapshot snapshot, Address address, bool authorize)
        {
            bool signer = snapshot.Signers.ContainsKey(address);

            return(signer && !authorize || !signer && authorize);
        }
Example #5
0
        public Snapshot GetOrCreateSnapshot(long number, Keccak hash)
        {
            Snapshot?snapshot = GetSnapshot(number, hash);

            if (!(snapshot is null))
            {
                return(snapshot);
            }

            var headers = new List <BlockHeader>();

            lock (_snapshotCreationLock)
            {
                BlockHeader?header = null;
                // Search for a snapshot in memory or on disk for checkpoints
                while (true)
                {
                    snapshot = GetSnapshot(number, hash);
                    if (snapshot != null)
                    {
                        break;
                    }

                    // If we're at an checkpoint block, make a snapshot if it's known
                    BlockHeader?previousHeader = header;
                    header = _blockTree.FindHeader(hash, BlockTreeLookupOptions.TotalDifficultyNotNeeded);
                    if (header == null)
                    {
                        throw new InvalidOperationException($"Unknown ancestor ({hash}) of {previousHeader?.ToString(BlockHeader.Format.Short)}");
                    }

                    if (header.Hash == null)
                    {
                        throw new InvalidOperationException("Block tree block without hash set");
                    }

                    Keccak parentHash = header.ParentHash;
                    if (IsEpochTransition(number))
                    {
                        Snapshot?parentSnapshot = GetSnapshot(number - 1, parentHash);

                        if (_logger.IsInfo)
                        {
                            _logger.Info($"Creating epoch snapshot at block {number}");
                        }
                        int     signersCount = CalculateSignersCount(header);
                        var     signers      = new SortedList <Address, long>(signersCount, AddressComparer.Instance);
                        Address epochSigner  = GetBlockSealer(header);
                        for (int i = 0; i < signersCount; i++)
                        {
                            Address signer = new Address(header.ExtraData.Slice(Clique.ExtraVanityLength + i * Address.ByteLength, Address.ByteLength));
                            signers.Add(signer, signer == epochSigner ? number : parentSnapshot == null ? 0L : parentSnapshot.Signers.ContainsKey(signer) ? parentSnapshot.Signers[signer] : 0L);
                        }

                        snapshot = new Snapshot(number, header.Hash, signers);
                        Store(snapshot);
                        break;
                    }

                    // No snapshot for this header, gather the header and move backward
                    headers.Add(header);
                    number = number - 1;
                    hash   = header.ParentHash;
                }

                if (headers.Count > 0)
                {
                    // Previous snapshot found, apply any pending headers on top of it
                    headers.Reverse();

                    for (int i = 0; i < headers.Count; i++)
                    {
                        headers[i].Author ??= GetBlockSealer(headers[i]);
                    }

                    int countBefore = snapshot.Signers.Count;
                    snapshot = Apply(snapshot, headers, _cliqueConfig.Epoch);

                    int countAfter = snapshot.Signers.Count;
                    if (countAfter != countBefore && _logger.IsInfo)
                    {
                        int    signerIndex = 0;
                        string word        = countAfter > countBefore ? "added to" : "removed from";
                        _logger.Info($"At block {number } a signer has been {word} the signer list:{Environment.NewLine}{string.Join(Environment.NewLine, snapshot.Signers.OrderBy(s => s.Key, AddressComparer.Instance).Select(s => $"  Signer {signerIndex++}: " + (KnownAddresses.GoerliValidators.ContainsKey(s.Key) ? KnownAddresses.GoerliValidators[s.Key] : s.Key.ToString())))}");
                    }
                }

                _snapshotCache.Set(snapshot.Hash, snapshot);
                // If we've generated a new checkpoint snapshot, save to disk
            }

            if ((ulong)snapshot.Number % Clique.CheckpointInterval == 0 && headers.Count > 0)
            {
                Store(snapshot);
            }

            return(snapshot);
        }
        private Block?PrepareBlock(Block parentBlock)
        {
            BlockHeader parentHeader = parentBlock.Header;

            if (parentHeader == null)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Preparing new block on top of {parentBlock.ToString(Block.Format.Short)} - parent header is null");
                }
                return(null);
            }

            if (_recentNotAllowedParent == parentBlock.Hash)
            {
                return(null);
            }

            if (!_sealer.CanSeal(parentHeader.Number + 1, parentHeader.Hash))
            {
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Not allowed to sign block ({parentBlock.Number + 1})");
                }
                _recentNotAllowedParent = parentHeader.Hash;
                return(null);
            }

            if (_logger.IsInfo)
            {
                _logger.Info($"Preparing new block on top of {parentBlock.ToString(Block.Format.Short)}");
            }

            UInt256 timestamp = _timestamper.EpochSeconds;

            BlockHeader header = new BlockHeader(
                parentBlock.Hash,
                Keccak.OfAnEmptySequenceRlp,
                Address.Zero,
                1,
                parentBlock.Number + 1,
                parentBlock.GasLimit,
                timestamp > parentBlock.Timestamp ? timestamp : parentBlock.Timestamp + 1,
                new byte[0]);

            // If the block isn't a checkpoint, cast a random vote (good enough for now)
            long number = header.Number;
            // Assemble the voting snapshot to check which votes make sense
            Snapshot snapshot     = _snapshotManager.GetOrCreateSnapshot(number - 1, header.ParentHash);
            bool     isEpochBlock = (ulong)number % 30000 == 0;

            if (!isEpochBlock && _proposals.Any())
            {
                // Gather all the proposals that make sense voting on
                List <Address> addresses = new List <Address>();
                foreach (var proposal in _proposals)
                {
                    Address address   = proposal.Key;
                    bool    authorize = proposal.Value;
                    if (_snapshotManager.IsValidVote(snapshot, address, authorize))
                    {
                        addresses.Add(address);
                    }
                }

                // If there's pending proposals, cast a vote on them
                if (addresses.Count > 0)
                {
                    header.Beneficiary = addresses[_cryptoRandom.NextInt(addresses.Count)];
                    header.Nonce       = _proposals[header.Beneficiary] ? Clique.NonceAuthVote : Clique.NonceDropVote;
                }
            }

            // Set the correct difficulty
            header.Difficulty      = CalculateDifficulty(snapshot, _address);
            header.TotalDifficulty = parentBlock.TotalDifficulty + header.Difficulty;
            if (_logger.IsDebug)
            {
                _logger.Debug($"Setting total difficulty to {parentBlock.TotalDifficulty} + {header.Difficulty}.");
            }

            // Set extra data
            int mainBytesLength   = Clique.ExtraVanityLength + Clique.ExtraSealLength;
            int signerBytesLength = isEpochBlock ? 20 * snapshot.Signers.Count : 0;
            int extraDataLength   = mainBytesLength + signerBytesLength;

            header.ExtraData = new byte[extraDataLength];

            byte[] clientName = Encoding.UTF8.GetBytes("Nethermind " + ClientVersion.Version);
            Array.Copy(clientName, header.ExtraData, clientName.Length);

            if (isEpochBlock)
            {
                for (int i = 0; i < snapshot.Signers.Keys.Count; i++)
                {
                    Address signer = snapshot.Signers.Keys[i];
                    int     index  = Clique.ExtraVanityLength + 20 * i;
                    Array.Copy(signer.Bytes, 0, header.ExtraData, index, signer.Bytes.Length);
                }
            }

            // Mix digest is reserved for now, set to empty
            header.MixHash = Keccak.Zero;
            // Ensure the timestamp has the correct delay
            header.Timestamp = parentBlock.Timestamp + _config.BlockPeriod;
            if (header.Timestamp < _timestamper.EpochSeconds)
            {
                header.Timestamp = new UInt256(_timestamper.EpochSeconds);
            }

            _stateProvider.StateRoot = parentHeader.StateRoot;

            var   selectedTxs = _pendingTxSelector.SelectTransactions(header.GasLimit);
            Block block       = new Block(header, selectedTxs, new BlockHeader[0]);

            header.TxRoot       = new TxTrie(block.Transactions).RootHash;
            block.Header.Author = _address;
            return(block);
        }