コード例 #1
0
        public byte[] SendBlock(long time)
        {
            DataMine dataMine = new DataMine(this.difficulty, this.BlockToMine, this.Worker.Address[0], time);

            byte[] datasend = Encoding.Default.GetBytes(Serialyze.Serialize(dataMine));
            return(datasend);
        }
コード例 #2
0
        public static string SendMinedBlock(DataMine mine)
        {
            Protocol reqProtocol = new Protocol(MessageType.MinedBlock)
            {
                Mine = mine
            };

            byte[] buffer = Formatter.ToByteArray(reqProtocol);
            DataClient.Client.Client.Send(buffer, SocketFlags.None);
            Protocol receiveMessage = ReceiveMessage();

            return(receiveMessage.Type != MessageType.Response ? receiveMessage.Message : receiveMessage.Message);
        }
コード例 #3
0
        private byte[] GenData()
        {
            DataMine dataMine;

            byte[] datasend;
            if (this.Coin.BlockToMines.Count != 0)
            {
                dataMine = new DataMine(this.Coin.Difficulty, this.Coin.BlockToMines[0], null);
                datasend = Encoding.Default.GetBytes(Serialyze.Serialize(dataMine));
            }
            else
            {
                dataMine = new DataMine(this.Coin.Difficulty, null, null);
                datasend = Encoding.Default.GetBytes(Serialyze.Serialize(dataMine));
            }

            return(datasend);
        }
コード例 #4
0
        public void Mine(string workerAddress)
        {
            Epicoin.Log = new Logger();
            while (DataClient.Continue)
            {
                Thread.Sleep(1000);
                try
                {
                    var data = Network.AskBlockToMine();
                    if (data == null)
                    {
                        Epicoin.Log.Write("[CM] No blocks to mine");
                        Console.WriteLine("[CM] No blocks to mine");
                        Thread.Sleep(1000);
                    }
                    else
                    {
                        int   difficulty = data.Difficulty;
                        Block block      = data.Block;
                        Epicoin.Log.Write("[CM] Mining ...");
                        Console.WriteLine("[CM] Mining ...");
                        long start = DateTime.Now.Ticks;
                        block.MineBlock(difficulty);
                        long miningtime = DateTime.Now.Ticks - start;
                        Epicoin.Log.Write("[CM] Creating Block " + block.Index + " : " + block.Hashblock
                                          + " : difficulty " + difficulty);
                        Console.WriteLine("[CM] Creating Block " + block.Index + " : " + block.Hashblock
                                          + " : difficulty " + difficulty);
                        Epicoin.Log.Write("[CM] Sending block mined ...");
                        Console.WriteLine("[CM] Sending block mined ...");
                        DataMine send = new DataMine(difficulty, block, workerAddress, miningtime);
                        string   resp = Network.SendMinedBlock(send);
                        Console.WriteLine("[M] " + resp);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("[M] " + e.Message);
                    continue;
                }
            }

            return;
        }
コード例 #5
0
        public static Protocol MinedBlock(Protocol prot)
        {
            if (prot.Mine == null)
            {
                return(new Protocol(MessageType.Error)
                {
                    Message = "Empty block"
                });
            }

            DataMine dataMine = prot.Mine;

            if (dataMine.block == null)
            {
                return(new Protocol(MessageType.Error)
                {
                    Message = "Empty block"
                });
            }

            if (dataMine.block.Data == null)
            {
                return(new Protocol(MessageType.Error)
                {
                    Message = "Block invalid"
                });
            }
            if (DataServer.Chain.BlockToMines[0].Index != dataMine.block.Index)
            {
                return(new Protocol(MessageType.Error)
                {
                    Message = "Block invalid"
                });
            }

            if (DataServer.Chain.BlockToMines[0].Timestamp != dataMine.block.Timestamp)
            {
                return(new Protocol(MessageType.Error)
                {
                    Message = "Block invalid"
                });
            }

            try
            {
                for (int i = 0; i < Block.nb_trans; i++)
                {
                    if (DataServer.Chain.BlockToMines[0].Data[i].Amount != dataMine.block.Data[i].Amount)
                    {
                        return(new Protocol(MessageType.Error)
                        {
                            Message = "Block invalid"
                        });
                    }

                    if (DataServer.Chain.BlockToMines[0].Data[i].FromAddress != dataMine.block.Data[i].FromAddress)
                    {
                        return(new Protocol(MessageType.Error)
                        {
                            Message = "Block invalid"
                        });
                    }

                    if (DataServer.Chain.BlockToMines[0].Data[i].ToAddress != dataMine.block.Data[i].ToAddress)
                    {
                        return(new Protocol(MessageType.Error)
                        {
                            Message = "Block invalid"
                        });
                    }

                    if (DataServer.Chain.BlockToMines[0].Data[i].Timestamp != dataMine.block.Data[i].Timestamp)
                    {
                        return(new Protocol(MessageType.Error)
                        {
                            Message = "Block invalid"
                        });
                    }
                }
            }
            catch (Exception e)
            {
                return(new Protocol(MessageType.Error)
                {
                    Message = "Block invalid"
                });
            }


            if (DataServer.Chain.BlockToMines[0].PreviousHash != dataMine.block.PreviousHash)
            {
                return(new Protocol(MessageType.Error)
                {
                    Message = "Block invalid"
                });
            }

            bool succes = DataServer.Chain.NetworkMinePendingTransaction(dataMine.address, dataMine.block, dataMine.timemining, dataMine.difficulty);

            if (succes)
            {
                return(new Protocol(MessageType.Response)
                {
                    Message = "Sucess"
                });
            }
            else
            {
                return(new Protocol(MessageType.Error)
                {
                    Message = "failed"
                });
            }
        }