public static ChainedBlock GetLastBlockIndex(StakeChain stakeChain, ChainedBlock index, bool proofOfStake)
        {
            if (index == null)
            {
                throw new ArgumentNullException(nameof(index));
            }
            var blockStake = stakeChain.Get(index.HashBlock);

            while (index.Previous != null && (blockStake.IsProofOfStake() != proofOfStake))
            {
                index      = index.Previous;
                blockStake = stakeChain.Get(index.HashBlock);
            }

            return(index);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the last block in the chain that was generated using
        /// PoS if <paramref name="proofOfStake"/> is <c>true</c> or PoW if <paramref name="proofOfStake"/> is <c>false</c>.
        /// </summary>
        /// <param name="stakeChain">Database of stake related data for the current blockchain.</param>
        /// <param name="startChainedBlock">Block that we start from. Only blocks before that one will be checked.</param>
        /// <param name="proofOfStake">Specifies what kind of block we are looking for: <c>true</c> for PoS or <c>false</c> for PoW.</param>
        /// <returns>Last block in the chain that satisfies provided requirements.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="startChainedBlock"/> is <c>null</c>.</exception>
        public ChainedBlock GetLastPowPosChainedBlock(StakeChain stakeChain, ChainedBlock startChainedBlock, bool proofOfStake)
        {
            Guard.Assert(startChainedBlock != null);

            this.logger.LogTrace("({0}:'{1}',{2}:{3})", nameof(startChainedBlock), startChainedBlock, nameof(proofOfStake), proofOfStake);

            BlockStake blockStake = stakeChain.Get(startChainedBlock.HashBlock);

            while ((startChainedBlock.Previous != null) && (blockStake.IsProofOfStake() != proofOfStake))
            {
                startChainedBlock = startChainedBlock.Previous;
                blockStake        = stakeChain.Get(startChainedBlock.HashBlock);
            }

            this.logger.LogTrace("(-)':{0}'", startChainedBlock);
            return(startChainedBlock);
        }
Beispiel #3
0
        public static ChainedBlock GetLastBlockIndex(StakeChain stakeChain, ChainedBlock index, bool proofOfStake)
        {
            if (index == null)
            {
                throw new ArgumentNullException(nameof(index));
            }

            clogger.LogTrace("({0}:'{1}',{2}:{3})", nameof(index), index, nameof(proofOfStake), proofOfStake);

            BlockStake blockStake = stakeChain.Get(index.HashBlock);

            while ((index.Previous != null) && (blockStake.IsProofOfStake() != proofOfStake))
            {
                index      = index.Previous;
                blockStake = stakeChain.Get(index.HashBlock);
            }

            clogger.LogTrace("(-)':{0}'", index);
            return(index);
        }
Beispiel #4
0
        public void CheckKernel(ContextInformation context, ChainedBlock pindexPrev, uint nBits, long nTime, OutPoint prevout, ref long pBlockTime)
        {
            var coins = this.coinView.FetchCoinsAsync(new[] { prevout.Hash }).GetAwaiter().GetResult();

            if (coins == null || coins.UnspentOutputs.Length != 1)
            {
                ConsensusErrors.ReadTxPrevFailed.Throw();
            }

            var prevBlock = chain.GetBlock(coins.BlockHash);
            var prevUtxo  = coins.UnspentOutputs[0];

            //var txPrev = trasnactionStore.Get(prevout.Hash);
            //if (txPrev == null)
            //	return false;

            //// Read block header
            //var blockHashPrev = mapStore.GetBlockHash(prevout.Hash);
            //var block = blockHashPrev == null ? null : blockStore.GetBlock(blockHashPrev);
            //if (block == null)
            //	return false;

            if (IsProtocolV3((int)nTime))
            {
                if (IsConfirmedInNPrevBlocks(prevUtxo, pindexPrev, this.consensusOptions.StakeMinConfirmations - 1))
                {
                    ConsensusErrors.InvalidStakeDepth.Throw();
                }
            }
            else
            {
                var nTimeBlockFrom = prevBlock.Header.Time;
                if (nTimeBlockFrom + this.consensusOptions.StakeMinAge > nTime)
                {
                    ConsensusErrors.MinAgeViolation.Throw();
                }
            }

            var prevBlockStake = stakeChain.Get(pindexPrev.HashBlock);

            if (prevBlockStake == null)
            {
                ConsensusErrors.BadStakeBlock.Throw();
            }

            // todo: check this unclear logic
            //if (pBlockTime)
            //	pBlockTime = block.Header.Time;

            this.CheckStakeKernelHash(context, pindexPrev, nBits, prevBlock, prevUtxo, prevBlockStake, prevout, (uint)nTime);
        }