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 List <BlockHeaderMsg> GetBlockHeaderMsgByHeights(List <long> heights) { var blockDac = new BlockDac(); var txDac = new TransactionDac(); var headers = new List <BlockHeaderMsg>(); foreach (var height in heights) { var items = blockDac.SelectByHeight(height); foreach (var entity in items) { var header = new BlockHeaderMsg(); header.Version = entity.Version; header.Hash = entity.Hash; header.Height = entity.Height; header.PreviousBlockHash = entity.PreviousBlockHash; header.Bits = entity.Bits; header.Nonce = entity.Nonce; header.Timestamp = entity.Timestamp; var transactions = txDac.SelectByBlockHash(entity.Hash); header.TotalTransaction = entity.Transactions == null ? 0 : entity.Transactions.Count; headers.Add(header); } } return(headers); }
public List <BlockHeaderMsg> GetBlockHeaderMsgByHeights(List <long> heights) { var blockDac = new BlockDac(); var txDac = new TransactionDac(); var headers = new List <BlockHeaderMsg>(); foreach (var height in heights) { var items = blockDac.SelectByHeight(height); foreach (var entity in items) { var header = new BlockHeaderMsg(); header.Version = entity.Version; header.Hash = entity.Hash; header.Height = entity.Height; header.PreviousBlockHash = entity.PreviousBlockHash; header.Bits = entity.Bits; header.Nonce = entity.Nonce; header.Timestamp = entity.Timestamp; header.GeneratorId = entity.GeneratorId; //header.GenerationSignature = entity.GenerationSignature; //header.BlockSignature = entity.BlockSignature; //header.CumulativeDifficulty = entity.CumulativeDifficulty; header.PayloadHash = entity.PayloadHash; header.BlockSignature = entity.BlockSignature; header.BlockSigSize = entity.BlockSignature.Length; var transactions = txDac.SelectByBlockHash(entity.Hash); header.TotalTransaction = entity.Transactions == null ? 0 : entity.Transactions.Count; headers.Add(header); } } return(headers); }
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(); } } } }