示例#1
0
        public void Add(Block block)
        {
            logger.LogInformation($"Recibiendo bloque {block.BlockNumber} con hash {block.Hash.ByteArrayToHexString()}");

            var other = Blockchain.GetBlock(block.Hash);

            if (other != null)
            {
                logger.LogInformation("El bloque ya existe");
                return;
            }

            if (!Blockchain.Last.Hash.SequenceEqual(block.PreviousHash))
            {
                var hashAsHex = block.PreviousHash.ByteArrayToHexString();
                if (!searchingBlocks.Contains(hashAsHex))
                {
                    logger.LogInformation("El bloque no existe ni es el siguiente al último, buscando más");
                    Blockchain.AddBlock(block);

                    searchingBlocks.Add(hashAsHex);
                    Peers.GetBlock(block.PreviousHash);
                    searchingBlocks.Remove(hashAsHex);
                }
                return;
            }

            lock (semaphore)
            {
                //TODO: stop miner
                Blockchain.StopMine();

                var transactions = block.GetTransactions();
                logger.LogInformation($"Quitando {transactions.Length} transacciones pendientes");

                foreach (var item in transactions)
                {
                    int count = 0;
                    if (pendings.ContainsKey(item.GetKey()))
                    {
                        count++;
                        pendings.Remove(item.GetKey());
                    }

                    logger.LogInformation($"Se quitaron {count} transacciones de la lista de pendientes");
                }

                logger.LogInformation($"Agregando bloque #{block.BlockNumber} a la cadena");
                Blockchain.AddBlock(block);
            }

            Peers.GetLastBlock();
        }
示例#2
0
        public void Add(Recount recount)
        {
            lock (semaphore)
            {
                if (pendings.ContainsKey(recount.GetKey()))
                {
                    return;
                }

                pendings.Add(recount.GetKey(), recount);
            }

            Peers.Broadcast(recount);
        }
示例#3
0
        public void Add(Document document)
        {
            lock (semaphore)
            {
                if (pendings.ContainsKey(document.GetKey()))
                {
                    return;
                }

                pendings.Add(document.GetKey(), document);
            }

            Peers.Broadcast(document);
        }
示例#4
0
        public void Add(Fiscal fiscal)
        {
            lock (semaphore)
            {
                if (pendings.ContainsKey(fiscal.GetKey()))
                {
                    return;
                }

                pendings.Add(fiscal.GetKey(), fiscal);
            }

            Peers.Broadcast(fiscal);
        }
示例#5
0
        public void Add(Urn urn)
        {
            lock (semaphore)
            {
                if (pendings.ContainsKey(urn.GetKey()))
                {
                    return;
                }

                pendings.Add(urn.GetKey(), urn);
            }

            Peers.Broadcast(urn);
        }
示例#6
0
        public void Add(Member member)
        {
            lock (semaphore)
            {
                if (pendings.ContainsKey(member.GetKey()))
                {
                    return;
                }

                pendings.Add(member.GetKey(), member);
            }

            Peers.Broadcast(member);
        }
示例#7
0
        public void Add(Vote vote)
        {
            lock (semaphore)
            {
                if (pendings.ContainsKey(vote.GetKey()))
                {
                    return;
                }

                pendings.Add(vote.GetKey(), vote);
            }

            Peers.Broadcast(vote);
        }
示例#8
0
        public void Add(Community community)
        {
            lock (semaphore)
            {
                if (pendings.ContainsKey(community.GetKey()))
                {
                    return;
                }

                pendings.Add(community.GetKey(), community);
            }

            Peers.Broadcast(community);
        }
示例#9
0
        public void Add(Question question)
        {
            lock (semaphore)
            {
                if (pendings.ContainsKey(question.GetKey()))
                {
                    return;
                }

                pendings.Add(question.GetKey(), question);
            }

            Peers.Broadcast(question);
        }
示例#10
0
        private void CheckPeers()
        {
            logger.LogInformation("Verificando conexiones con pares");

            if (!string.IsNullOrWhiteSpace(options.PeerUrl))
            {
                if (!Peers.Contains(options.PeerUrl))
                {
                    Connect(options.PeerUrl);
                }
            }

            Discovery();
        }
示例#11
0
        public void Connect(string url)
        {
            logger.LogInformation($"Conectando con el peer: {url}");

            var peer = Peers.GetNodeInfo(url);

            if (peer != null)
            {
                Register(peer);
            }
            else
            {
                logger.LogWarning($"No se pudo establecer la conexión con {url}");
            }
        }
示例#12
0
 public void Register(Peer peer)
 {
     Peers.Add(peer);
 }
示例#13
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");
            }
        }
示例#14
0
        public void Discovery()
        {
            logger.LogInformation("Descubriendo nuevos pares");

            Peers.Discovery();
        }
示例#15
0
 public void Syncronize()
 {
     logger.LogInformation("Sincronizando blockchain con pares");
     Peers.GetLastBlock();
 }