예제 #1
0
        public void MinePendingTransactions()
        {
            logger.LogInformation($"Buscando transacciones que minar.\n" +
                                  $"\tÚltimo bloque #{Blockchain.Last.BlockNumber}.\n" +
                                  $"\tCadenas secundarias #{Blockchain.BranchesCount}\n" +
                                  $"\tTransacciones pendientes #{pendings.Count}\n" +
                                  $"\tPares registrados #{Peers.Count}: {string.Join(", ",Peers.List())}"
                                  );

            BlockItem[] pendingsToMine;
            lock (semaphore)
            {
                pendingsToMine = Pendings.ToArray();
            }

            if (pendingsToMine.Length > 0)
            {
                logger.LogInformation($"Minando {pendingsToMine.Length} transacciones");
                try
                {
                    var block = Blockchain.MineNextBlock(pendingsToMine);

                    if (block != null)
                    {
                        lock (semaphore)
                        {
                            logger.LogInformation("Hemos minado un bloque!!!");
                            Peers.Broadcast(block);
                            var transactions = block.GetTransactions();
                            foreach (var item in transactions)
                            {
                                pendings.Remove(item.GetKey());
                            }
                        }
                    }
                    else
                    {
                        logger.LogInformation("No hemos logrado minado el bloque antes que otro :(");
                    }

                    var chain = Blockchain.Trunk.ToArray();
                    KeyValuePair <string, BlockItem>[] invalids;
                    lock (semaphore)
                    {
                        invalids = pendings.Where(p => !p.Value.IsValid(chain)).ToArray();
                    }

                    foreach (var invalid in invalids)
                    {
                        logger.LogWarning($"Item {invalid.Value.GetType().Name} inválido: {string.Join("\n\r\t", invalid.Value.Messages)}");
                    }
                }
                catch (Exception ex)
                {
                    logger.LogCritical($"Error minando transacciones pendientes: {ex}");
                }
            }
            else
            {
                logger.LogDebug("No se encontraron transacciones que minar");
            }
        }
예제 #2
0
 public Counter(Blockchain blockchain)
 {
     this.blockchain = blockchain;
 }