Exemplo n.º 1
0
        public void Add(Konan konan, int nonce)
        {
            konan.Nonce = nonce.ToString();
            konan.Hash  = konan.GetHash();

            this.List.Add(konan);
        }
Exemplo n.º 2
0
 public void addTransaction(Konan block)
 {
     if (this.Transactions.Count > 0)
     {
         block.Transaction = this.Transactions.Dequeue();
     }
 }
Exemplo n.º 3
0
        public Konan Create(string data, string prevhash, int diff)
        {
            Konan konan = new Konan()
            {
                Id          = this.List.Count + 1,
                PrevHash    = prevhash,
                TimeStamp   = DateTime.Now.Ticks.ToString(),
                Nonce       = "0",
                Difficulty  = diff,
                Transaction = new KonanTrans()
            };

            return(konan);
        }
Exemplo n.º 4
0
        public int mining(Konan konan)
        {
            int n = 0;

            string target = new string('0', this.Difficulty);

            while (!BlockChain.Current.IsValid(konan.GetHash(), target))
            {
                n++;

                konan.Nonce = n.ToString();
            }

            return(n);
        }
Exemplo n.º 5
0
        public void start()
        {
            Konan block = BlockChain.Current.Create("The first block: Hello world!", "0", this.Difficulty);

            //start mining
            Task.Run(() =>
            {
                for (int i = 0; i < BlockChain.Current.Total; i++)
                {
                    if (i % 10 == 0 && i >= 10)
                    {
                        this.Difficulty++;
                    }

                    int nonce = this.mining(block);

                    this.addTransaction(block);

                    BlockChain.Current.Add(block, nonce);

                    if (OnMiningSuccess != null)
                    {
                        OnMiningSuccess(this, new MiningEventArgs()
                        {
                            Diff = this.Difficulty, Hash = block.Hash, Date = DateTime.Now
                        });
                    }

                    block = BlockChain.Current.Create("block no." + i, BlockChain.Current.List[i].Hash, this.Difficulty);

                    Task.Delay(200);
                }

                if (OnMiningEnd != null)
                {
                    OnMiningEnd(this, new ServerEventArgs()
                    {
                        Message = "Server mining end."
                    });
                }
            });
        }