예제 #1
0
        public bool VerifyBlock(BlockMsg newBlock)
        {
            if (this.exists(newBlock.Header.Hash))
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BLOCK_HAS_BEEN_EXISTED);
            }

            if (newBlock.Header.Hash != newBlock.Header.GetHash())
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BLOCK_HASH_ERROR);
            }

            var blockComponent = new BlockComponent();
            var previousBlock  = this.GetBlockMsgByHash(newBlock.Header.PreviousBlockHash);

            if (newBlock.Header.Height > 0 && previousBlock == null)
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.PREV_BLOCK_NOT_EXISTED);
            }

            if ((newBlock.Header.Timestamp > Time.EpochTime && (newBlock.Header.Timestamp - Time.EpochTime) > 2 * 60 * 60 * 1000) ||
                (previousBlock != null && newBlock.Header.Timestamp <= previousBlock.Header.Timestamp))
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BLOCK_TIME_IS_ERROR);
            }

            if (newBlock.Serialize().Length > BlockSetting.MAX_BLOCK_SIZE)
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BLOCK_SIZE_LARGE_THAN_LIMIT);
            }

            var txComponent = new TransactionComponent();

            var newBlockReward = POC.GetNewBlockReward(newBlock.Header.Height);
            var prevStepBlock  = GetBlockMsgByHeight(newBlock.Header.Height - POC.DIFFIUCLTY_ADJUST_STEP - 1);
            var minerInfo      = string.Empty;

            if (newBlock.Transactions.Count > 0)
            {
                var coinbase = newBlock.Transactions[0];
                var totalFee = 0L;

                minerInfo = Encoding.UTF8.GetString(Base16.Decode(coinbase.Inputs[0].UnlockScript)).Split("`")[0];

                if (newBlock.Transactions.Count > 1)
                {
                    for (int i = 1; i < newBlock.Transactions.Count; i++)
                    {
                        long fee;
                        if (txComponent.VerifyTransaction(newBlock.Transactions[i], out fee))
                        {
                            totalFee += fee;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }

                if (coinbase.Inputs.Count != 1 || coinbase.Outputs.Count != 1 || coinbase.Inputs[0].OutputTransactionHash != Base16.Encode(HashHelper.EmptyHash()))
                {
                    throw new CommonException(ErrorCode.Engine.Transaction.Verify.COINBASE_FORMAT_ERROR);
                }
                else
                {
                    var output = coinbase.Outputs[0];

                    if (output.Amount > BlockSetting.OUTPUT_AMOUNT_MAX)
                    {
                        throw new CommonException(ErrorCode.Engine.Transaction.Verify.OUTPUT_EXCEEDED_THE_LIMIT);
                    }

                    if (!Script.VerifyLockScriptFormat(output.LockScript))
                    {
                        throw new CommonException(ErrorCode.Engine.Transaction.Verify.SCRIPT_FORMAT_ERROR);
                    }

                    if (output.Amount != (totalFee + newBlockReward))
                    {
                        throw new CommonException(ErrorCode.Engine.Transaction.Verify.COINBASE_OUTPUT_AMOUNT_ERROR);
                    }
                }
            }

            var pool = new MiningPoolComponent().GetMiningPoolByName(minerInfo);

            if (pool == null)
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.MINING_POOL_NOT_EXISTED);
            }

            if (!POC.VerifyBlockSignature(newBlock.Header.PayloadHash, newBlock.Header.BlockSignature, pool.PublicKey))
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BLOCK_SIGNATURE_IS_ERROR);
            }

            long previousBlockHeight = -1;
            long previousBlockBits   = -1;

            if (previousBlock != null)
            {
                previousBlockHeight = previousBlock.Header.Height;
                previousBlockBits   = previousBlock.Header.Bits;
            }

            if (POC.CalculateBaseTarget(newBlock.Header.Height, previousBlock, prevStepBlock) != newBlock.Header.Bits)
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BITS_IS_WRONG);
            }

            var targetResult = POC.CalculateTargetResult(newBlock);

            if (POC.Verify(newBlock.Header.Bits, targetResult))
            {
                return(true);
            }
            else
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.POC_VERIFY_FAIL);
            }
        }
예제 #2
0
        public void ProcessUncsonfirmedBlocks()
        {
            var blockDac      = new BlockDac();
            var txDac         = new TransactionDac();
            var txComponent   = new TransactionComponent();
            var utxoComponent = new UtxoComponent();

            long lastHeight      = this.GetLatestHeight();
            long confirmedHeight = -1;
            var  confirmedBlock  = blockDac.SelectLastConfirmed();
            var  confirmedHash   = Base16.Encode(HashHelper.EmptyHash());

            if (confirmedBlock != null)
            {
                confirmedHeight = confirmedBlock.Height;
                confirmedHash   = confirmedBlock.Hash;
            }

            if (lastHeight - confirmedHeight >= 6)
            {
                var blocks = blockDac.SelectByPreviousHash(confirmedHash);

                if (blocks.Count == 1)
                {
                    blockDac.UpdateBlockStatusToConfirmed(blocks[0].Hash);
                    this.ProcessUncsonfirmedBlocks();
                }
                else if (blocks.Count > 1)
                {
                    var countOfDescendants = new Dictionary <string, long>();
                    foreach (var block in blocks)
                    {
                        var count = blockDac.CountOfDescendants(block.Hash);

                        if (!countOfDescendants.ContainsKey(block.Hash))
                        {
                            countOfDescendants.Add(block.Hash, count);
                        }
                    }

                    var dicSort = countOfDescendants.OrderBy(d => d.Value).ToList();

                    var lastItem = blocks.Where(b => b.Hash == dicSort[dicSort.Count - 1].Key).First();
                    var index    = 0;

                    while (index < dicSort.Count - 1)
                    {
                        var currentItem = blocks.Where(b => b.Hash == dicSort[index].Key).First();

                        if (lastHeight - currentItem.Height >= 6)
                        {
                            var txList = txDac.SelectByBlockHash(currentItem.Hash);

                            //Skip coinbase tx
                            for (int i = 1; i < txList.Count; i++)
                            {
                                var tx    = txList[i];
                                var txMsg = txComponent.ConvertTxEntityToMsg(tx);
                                txComponent.AddTransactionToPool(txMsg);
                            }

                            //remove coinbase from utxo
                            UtxoSet.Instance.RemoveUtxoRecord(txList[0].Hash, 0);

                            blockDac.UpdateBlockStatusToDiscarded(currentItem.Hash);
                            index++;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (dicSort.Count - index <= 1)
                    {
                        blockDac.UpdateBlockStatusToConfirmed(lastItem.Hash);
                        this.ProcessUncsonfirmedBlocks();
                    }
                }
            }
        }
예제 #3
0
        public bool VerifyBlock(BlockMsg newBlock)
        {
            if (this.exists(newBlock.Header.Hash))
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BLOCK_HAS_BEEN_EXISTED);
            }

            if (newBlock.Header.Hash != newBlock.Header.GetHash())
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BLOCK_HASH_ERROR);
            }

            var blockComponent = new BlockComponent();
            var previousBlock  = this.GetBlockMsgByHash(newBlock.Header.PreviousBlockHash);

            if (newBlock.Header.Height > 0 && previousBlock == null)
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.PREV_BLOCK_NOT_EXISTED);
            }

            if ((newBlock.Header.Timestamp > Time.EpochTime && (newBlock.Header.Timestamp - Time.EpochTime) > 2 * 60 * 60 * 1000) ||
                (previousBlock != null && newBlock.Header.Timestamp <= previousBlock.Header.Timestamp))
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BLOCK_TIME_IS_ERROR);
            }

            if (newBlock.Serialize().Length > BlockSetting.MAX_BLOCK_SIZE)
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BLOCK_SIZE_LARGE_THAN_LIMIT);
            }

            var work        = new POW(newBlock.Header.Height);
            var txComponent = new TransactionComponent();

            if (newBlock.Transactions.Count > 0)
            {
                var coinbase = newBlock.Transactions[0];
                var reward   = work.GetNewBlockReward();
                var totalFee = 0L;

                if (newBlock.Transactions.Count > 1)
                {
                    for (int i = 1; i < newBlock.Transactions.Count; i++)
                    {
                        long fee;
                        if (txComponent.VerifyTransaction(newBlock.Transactions[i], out fee))
                        {
                            totalFee += fee;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }

                if (coinbase.Inputs.Count != 1 || coinbase.Outputs.Count != 1 || coinbase.Inputs[0].OutputTransactionHash != Base16.Encode(HashHelper.EmptyHash()))
                {
                    throw new CommonException(ErrorCode.Engine.Transaction.Verify.COINBASE_FORMAT_ERROR);
                }
                else
                {
                    var output = coinbase.Outputs[0];

                    if (output.Amount > BlockSetting.OUTPUT_AMOUNT_MAX)
                    {
                        throw new CommonException(ErrorCode.Engine.Transaction.Verify.OUTPUT_EXCEEDED_THE_LIMIT);
                    }

                    if (!Script.VerifyLockScriptFormat(output.LockScript))
                    {
                        throw new CommonException(ErrorCode.Engine.Transaction.Verify.SCRIPT_FORMAT_ERROR);
                    }

                    if (output.Amount != (totalFee + reward))
                    {
                        throw new CommonException(ErrorCode.Engine.Transaction.Verify.COINBASE_OUTPUT_AMOUNT_ERROR);
                    }
                }
            }


            var prev4032Block = this.GetBlockMsgByHeight(newBlock.Header.Height - POW.DiffiucltyAdjustStep);

            long previousBlockHeight = -1;
            long previousBlockBits   = -1;

            if (previousBlock != null)
            {
                previousBlockHeight = previousBlock.Header.Height;
                previousBlockBits   = previousBlock.Header.Bits;
            }

            if (work.CalculateNextWorkTarget(previousBlockHeight, previousBlockBits, prev4032Block) != newBlock.Header.Bits)
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.BITS_IS_WRONG);
            }

            var hashResult = this.GetMiningWorkResult(newBlock);

            if (work.Verify(newBlock.Header.Bits, hashResult))
            {
                return(true);
            }
            else
            {
                throw new CommonException(ErrorCode.Engine.Block.Verify.POW_VERIFY_FAIL);
            }
        }