Пример #1
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);
        }