public static void LoadGenesisBlock(this Blockchain blockchain, string path)
        {
            var json = File.ReadAllText(path);

            if (json != null)
            {
                var genesis = JsonSerializer.Deserialize <Block>(json, JsonDefaultSettings.Options);
                blockchain.AddBlock(genesis);
            }
        }
示例#2
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();
        }