Exemplo n.º 1
0
        public override bool Verify()
        {
            if (Blockchain.Default.ContainsTransaction(Hash))
            {
                return(true);
            }
            if (!Blockchain.Default.Ability.HasFlag(BlockchainAbility.UnspentIndexes) || !Blockchain.Default.Ability.HasFlag(BlockchainAbility.TransactionIndexes))
            {
                return(false);
            }
            if (Blockchain.Default.IsDoubleSpend(this))
            {
                return(false);
            }
            foreach (UInt256 hash in Outputs.Select(p => p.AssetId).Distinct())
            {
                if (!Blockchain.Default.ContainsAsset(hash))
                {
                    return(false);
                }
            }
            TransactionResult[] results = GetTransactionResults()?.ToArray();
            if (results == null)
            {
                return(false);
            }
            TransactionResult[] results_destroy = results.Where(p => p.Amount > Fixed8.Zero).ToArray();
            if (results_destroy.Length > 1)
            {
                return(false);
            }
            if (results_destroy.Length == 1 && results_destroy[0].AssetId != Blockchain.AntCoin.Hash)
            {
                return(false);
            }
            if (SystemFee > Fixed8.Zero && (results_destroy.Length == 0 || results_destroy[0].Amount < SystemFee))
            {
                return(false);
            }
            TransactionResult[] results_issue = results.Where(p => p.Amount < Fixed8.Zero).ToArray();
            switch (Type)
            {
            case TransactionType.MinerTransaction:
            case TransactionType.ClaimTransaction:
                if (results_issue.Any(p => p.AssetId != Blockchain.AntCoin.Hash))
                {
                    return(false);
                }
                break;

            case TransactionType.IssueTransaction:
                if (results_issue.Any(p => p.AssetId == Blockchain.AntCoin.Hash))
                {
                    return(false);
                }
                break;

            default:
                if (results_issue.Length > 0)
                {
                    return(false);
                }
                break;
            }
            TransactionAttribute script = Attributes.FirstOrDefault(p => p.Usage == TransactionAttributeUsage.Script);

            if (script != null)
            {
                ScriptEngine engine = new ScriptEngine(new Script
                {
                    StackScript  = new byte[0],
                    RedeemScript = script.Data
                }, this);
                if (!engine.Execute())
                {
                    return(false);
                }
            }
            return(this.VerifySignature());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 验证交易
        /// </summary>
        /// <returns>返回验证的结果</returns>
        public override bool Verify()
        {
            if (Blockchain.Default.ContainsTransaction(Hash))
            {
                return(true);
            }
            if (!Blockchain.Default.Ability.HasFlag(BlockchainAbility.UnspentIndexes) || !Blockchain.Default.Ability.HasFlag(BlockchainAbility.TransactionIndexes))
            {
                return(false);
            }
            if (Blockchain.Default.IsDoubleSpend(this))
            {
                return(false);
            }
            foreach (var group in Outputs.GroupBy(p => p.AssetId))
            {
                RegisterTransaction asset = Blockchain.Default.GetTransaction(group.Key) as RegisterTransaction;
                if (asset == null)
                {
                    return(false);
                }
                foreach (TransactionOutput output in group)
                {
                    if (output.Value.GetData() % (long)Math.Pow(10, 8 - asset.Precision) != 0)
                    {
                        return(false);
                    }
                }
            }
            TransactionResult[] results = GetTransactionResults()?.ToArray();
            if (results == null)
            {
                return(false);
            }
            TransactionResult[] results_destroy = results.Where(p => p.Amount > Fixed8.Zero).ToArray();
            if (results_destroy.Length > 1)
            {
                return(false);
            }
            if (results_destroy.Length == 1 && results_destroy[0].AssetId != Blockchain.AntCoin.Hash)
            {
                return(false);
            }
            if (SystemFee > Fixed8.Zero && (results_destroy.Length == 0 || results_destroy[0].Amount < SystemFee))
            {
                return(false);
            }
            TransactionResult[] results_issue = results.Where(p => p.Amount < Fixed8.Zero).ToArray();
            switch (Type)
            {
            case TransactionType.MinerTransaction:
            case TransactionType.ClaimTransaction:
                if (results_issue.Any(p => p.AssetId != Blockchain.AntCoin.Hash))
                {
                    return(false);
                }
                break;

            case TransactionType.IssueTransaction:
                if (results_issue.Any(p => p.AssetId == Blockchain.AntCoin.Hash))
                {
                    return(false);
                }
                break;

            default:
                if (results_issue.Length > 0)
                {
                    return(false);
                }
                break;
            }
            TransactionAttribute script = Attributes.FirstOrDefault(p => p.Usage == TransactionAttributeUsage.Script);

            if (script != null)
            {
                ScriptEngine engine = new ScriptEngine(new Script
                {
                    StackScript  = new byte[0],
                    RedeemScript = script.Data
                }, this, InterfaceEngine.Default);
                if (!engine.Execute())
                {
                    return(false);
                }
            }
            return(this.VerifySignature());
        }