示例#1
0
        private BlockMsg convertEntityToBlockMsg(Block entity)
        {
            var txDac     = new TransactionDac();
            var inputDac  = new InputDac();
            var outputDac = new OutputDac();

            var blockMsg  = new BlockMsg();
            var headerMsg = new BlockHeaderMsg();

            headerMsg.Version           = entity.Version;
            headerMsg.Hash              = entity.Hash;
            headerMsg.Height            = entity.Height;
            headerMsg.PreviousBlockHash = entity.PreviousBlockHash;
            headerMsg.Bits              = entity.Bits;
            headerMsg.Nonce             = entity.Nonce;
            headerMsg.Timestamp         = entity.Timestamp;

            var transactions = txDac.SelectByBlockHash(entity.Hash);

            headerMsg.TotalTransaction = transactions == null ? 0: transactions.Count;

            foreach (var tx in transactions)
            {
                var txMsg = new TransactionMsg();
                txMsg.Version   = tx.Version;
                txMsg.Hash      = tx.Hash;
                txMsg.Timestamp = tx.Timestamp;
                txMsg.Locktime  = tx.LockTime;

                var inputs  = inputDac.SelectByTransactionHash(tx.Hash);
                var outputs = outputDac.SelectByTransactionHash(tx.Hash);

                foreach (var input in inputs)
                {
                    txMsg.Inputs.Add(new InputMsg
                    {
                        OutputTransactionHash = input.OutputTransactionHash,
                        OutputIndex           = input.OutputIndex,
                        Size         = input.Size,
                        UnlockScript = input.UnlockScript
                    });
                }

                foreach (var output in outputs)
                {
                    txMsg.Outputs.Add(new OutputMsg
                    {
                        Index      = output.Index,
                        Amount     = output.Amount,
                        Size       = output.Size,
                        LockScript = output.LockScript
                    });
                }

                blockMsg.Transactions.Add(txMsg);
            }

            blockMsg.Header = headerMsg;
            return(blockMsg);
        }
        public TransactionMsg ConvertTxEntityToMsg(Transaction tx)
        {
            var inputDac  = new InputDac();
            var outputDac = new OutputDac();

            var txMsg = new TransactionMsg();

            txMsg.Version   = tx.Version;
            txMsg.Hash      = tx.Hash;
            txMsg.Timestamp = tx.Timestamp;
            txMsg.Locktime  = tx.LockTime;

            var inputs  = inputDac.SelectByTransactionHash(tx.Hash);
            var outputs = outputDac.SelectByTransactionHash(tx.Hash);

            foreach (var input in inputs)
            {
                txMsg.Inputs.Add(new InputMsg
                {
                    OutputTransactionHash = input.OutputTransactionHash,
                    OutputIndex           = input.OutputIndex,
                    Size         = input.Size,
                    UnlockScript = input.UnlockScript
                });
            }

            foreach (var output in outputs)
            {
                txMsg.Outputs.Add(new OutputMsg
                {
                    Index      = output.Index,
                    Amount     = output.Amount,
                    Size       = output.Size,
                    LockScript = output.LockScript
                });
            }

            return(txMsg);
        }
        public Transaction GetTransactionEntityByHash(string hash)
        {
            var inputDac  = new InputDac();
            var outputDac = new OutputDac();
            var item      = new TransactionDac().SelectByHash(hash);

            if (item != null)
            {
                item.Inputs  = inputDac.SelectByTransactionHash(item.Hash);
                item.Outputs = outputDac.SelectByTransactionHash(item.Hash);
            }
            else
            {
                var msg = TransactionPool.Instance.GetTransactionByHash(hash);

                if (msg != null)
                {
                    item = this.ConvertTxMsgToEntity(msg);
                }
            }

            return(item);
        }
        public List <Transaction> SearchTransactionEntities(string account, int count, int skip = 0, bool includeWatchOnly = true)
        {
            var inputDac  = new InputDac();
            var outputDac = new OutputDac();
            var items     = new TransactionDac().SelectTransactions(account, count, skip, includeWatchOnly);
            var accounts  = new AccountDac().SelectAll();

            foreach (var item in items)
            {
                item.Inputs  = inputDac.SelectByTransactionHash(item.Hash);
                item.Outputs = outputDac.SelectByTransactionHash(item.Hash);
            }

            if (skip == 0)
            {
                var  txList = TransactionPool.Instance.GetAllTransactions();
                bool containsUnconfirmedTx = false;

                foreach (var tx in txList)
                {
                    var  entity            = this.ConvertTxMsgToEntity(tx);
                    bool isSendTransaction = false;
                    if (entity != null)
                    {
                        foreach (var input in entity.Inputs)
                        {
                            if (accounts.Where(a => a.Id == input.AccountId).Count() > 0)
                            {
                                items.Add(entity);
                                isSendTransaction     = true;
                                containsUnconfirmedTx = true;
                                break;
                            }
                        }

                        if (!isSendTransaction)
                        {
                            foreach (var output in entity.Outputs)
                            {
                                if (accounts.Where(a => a.Id == output.ReceiverId).Count() > 0)
                                {
                                    items.Add(entity);
                                    containsUnconfirmedTx = true;
                                    break;
                                }
                            }
                        }
                    }
                }

                if (containsUnconfirmedTx)
                {
                    items = items.OrderByDescending(t => t.Timestamp).ToList();
                }
            }
            //var result = new List<TransactionMsg>();

            //foreach(var item in items)
            //{
            //    result.Add(convertTxEntityToMsg(item));
            //}

            //return result;
            return(items);
        }