예제 #1
0
        private async Task SaveTransactionToDb(GetBlockRpcModel block)
        {
            foreach (var itemTx in block.Tx)
            {
                try
                {
                    //Console.WriteLine("Transaction - " + itemTx);
                    var transaction = await _bitcoinRpc.GetRawTransactionAsync(itemTx);

                    var newTransaction = new Transaction
                    {
                        Time          = transaction.GetTime(),
                        Height        = block.Height,
                        Confirmations = transaction.Confirmations,
                        Txid          = transaction.Txid,
                        Blockhash     = transaction.Blockhash
                    };

                    await _transactionRepository.SaveAsync(newTransaction);

                    await SaveOutputsToDb(transaction);
                    await SaveInputsDb(transaction);
                }
                catch (Exception ex)
                {
                    /*var webException = ex as WebException;
                     *
                     *
                     * if (webException != null)
                     * {
                     *       using (var str = webException.Response.GetResponseStream())
                     *       {
                     *           using (var sr = new StreamReader(str))
                     *           {
                     *               var response = await sr.ReadToEndAsync();
                     *
                     *               await _log.WriteWarning("JobsBlockTransfer", "SaveTransactionToDbWebException", itemTx, response);
                     *           }
                     *       }
                     *   }*/

                    //Console.WriteLine("Error - SaveTransactionToDb");
                    await _log.WriteError("JobsBlockTransfer", "SaveTransactionToDb", itemTx, ex);
                }
            }
        }
예제 #2
0
        static int IndexBlock(ObsidianChainContext db, string blockHash)
        {
            Console.WriteLine($"Processing block at height {_currentBlockHeight}: {blockHash}");

            GetBlockRpcModel block = _txAdapter.RpcClient.GetBlockAsync(blockHash).GetAwaiter().GetResult();

            if (block == null)
            {
                Console.WriteLine($"Error - could not retrieve block not at height {_currentBlockHeight}: {blockHash}");
                return(-1);
            }

            var blockEntity = new BlockEntity {
                Id = _currentBlockNumber, Height = _currentBlockHeight, BlockHash = blockHash, BlockData = block.OriginalJson
            };

            db.BlockEntities.Add(blockEntity);
            if (_currentBlockHeight == 0)
            {
                // for the tx in the genesis block, we can't pull transaction data, so we make an exception here
                var genesisBlockTransaction = new TransactionEntity {
                    BlockEntity = blockEntity, Id = block.Tx[0]
                };
                db.TransactionEntities.Add(genesisBlockTransaction);
                _currentBlockHeight++;
                _currentBlockNumber++;
                db.SaveChanges();
                return(0);
            }

            // Get all transactions from the adapter, so that we can use the logic there
            string[] transactionIds = block.Tx;
            List <Core.Domain.Transaction> blockTransactions = new List <Core.Domain.Transaction>();

            foreach (var txid in transactionIds)
            {
                var transaction = _txAdapter.GetTransaction(txid).GetAwaiter().GetResult();
                transaction.Block = new Block {
                    Height = _currentBlockHeight
                };
                blockTransactions.Add(transaction);
            }

            // Queue all for insert to the db
            foreach (var blockTx in blockTransactions)
            {
                var transactionEntity = new TransactionEntity()
                {
                    Id = blockTx.TransactionId, BlockEntity = blockEntity, TransactionData = blockTx.OriginalJson
                };
                // db.TransactionEntities.Add(transactionEntity);
            }

            // Process all blockTransactions for accounting
            foreach (var blockTx in blockTransactions)
            {
                ProcessTransaction(blockTx, db);
            }

            db.SaveChanges();
            _currentBlockHeight++;
            _currentBlockNumber++;
            return(0);
        }
예제 #3
0
        public async Task <Block> GetBlock(string id)
        {
            if (id == null)
            {
                return(null);
            }

            string input = id.Trim();

            if (input.Length == 0 || input.Length > 64)
            {
                return(null);
            }

            try
            {
                string blockHash = null;
                uint   blocknumber;
                if (input.Length != 64 && uint.TryParse(input, out blocknumber))
                {
                    try
                    {
                        blockHash = await _client.GetBlockHashAsync(blocknumber);

                        if (blockHash == null)
                        {
                            return(null);
                        }
                    }
                    catch (Exception e)
                    {
                        return(null);
                    }
                }
                else
                {
                    blockHash = input;
                }



                GetBlockRpcModel b = await _client.GetBlockAsync(blockHash);

                if (b == null)
                {
                    return(null);
                }

                if (b.Tx == null)
                {
                    b.Tx = new string[0];
                }

                var block = new Block
                {
                    Hash              = b.Hash,
                    Height            = b.Height,
                    Time              = b.GetTime(),
                    Difficulty        = b.Difficulty,
                    MerkleRoot        = b.MerkleRoot,
                    Nonce             = b.Nonce,
                    PreviousBlock     = b.PreviousBlockHash,
                    NextBlock         = b.NextBlockHash,
                    Confirmations     = b.Confirmations,
                    TotalTransactions = b.Tx.Length,
                    Transactions      = new List <Transaction>(b.Tx.Length),
                    Chainwork         = b.Chainwork,
                    Bits              = b.Bits,
                    Size              = (int)b.Size,
                    StrippedSize      = (int)b.StrippedSize,
                    VersionHex        = b.VersionHex,
                    Version           = b.Version,
                    Weight            = b.Weight
                };

                foreach (var t in b.Tx)
                {
                    var transaction = new Transaction
                    {
                        TransactionId = t,
                        Blockhash     = block.Hash,
                        Block         = block,
                        Confirmations = block.Confirmations,
                        Time          = block.Time
                    };
                    block.Transactions.Add(transaction);
                }

                return(block);
            }
            catch (Exception e)
            {
                ;
                return(null);
            }
        }