Esempio n. 1
0
        public override string ToString()
        {
            var sb = new StringBuilder();

            Txs.ForEach(x => sb.Append(x.ToString()));

            sb.Append(" PrevHash:").Append(PrevHash)
            .Append(" Dificulty:").Append(Difficulty)
            .Append(" Nonce:").Append(Nonce)
            .Append(" Hash:").Append(Hash);

            return(sb.ToString());
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public byte[] ToStream()
        {
            if (Validate().Any())
            {
                return(null);
            }

            using var ts = new Helper.TangramStream();
            ts.Append(Height);
            if (Size != 0)
            {
                ts.Append(Size);
            }

            ts.Append(BlockHeader.ToStream())
            .Append(NrTx)
            .Append(Helper.Util.Combine(Txs.Select(x => x.ToHash()).ToArray()))
            .Append(BlockPos.ToStream());
            return(ts.ToArray());
        }
Esempio n. 3
0
        public void Generate()
        {
            for (ulong i = 0; i < (ulong)ChainHeight; i++)
            {
                // todo gen blocks and transactions
                Block b = BlockSyncHelpers.GenerateValidBlockToSync(i);
                Chain.Add(b);

                var txList = new List <Transaction>();
                Txs.TryAdd(b, txList);

                for (ulong j = 0; j < 3; j++)
                {
                    Transaction t = new Transaction();
                    t.From        = Address.FromRawBytes(Hash.FromRawBytes(_from).ToByteArray());
                    t.To          = Address.FromRawBytes(Hash.FromRawBytes(_to).ToByteArray());
                    t.IncrementId = j;

                    txList.Add(t);
                    b.AddTransaction(t);
                }
            }
        }
Esempio n. 4
0
        public bool Verify()
        {
            string head = string.Empty;

            Enumerable.Range(1, Difficulty).ToList().ForEach(a => head += "0");

            if (!Hash.StartsWith(head))
            {
                return(false);
            }

            using (var sha256 = new SHA256Managed())
            {
                if (!Hash.Equals(
                        Convert.ToBase64String(
                            sha256.ComputeHash(
                                Encoding.Unicode.GetBytes(Nonce.ToString() + GetMessage())))))
                {
                    return(false);
                }
            }

            var tx         = Txs.Skip(1).ToList();
            var isVerified = true;

            tx.ForEach(a => { if (!a.Verify())
                              {
                                  isVerified = false;
                              }
                       });
            if (!isVerified)
            {
                return(false);
            }

            // 同一Inputがないか?
            var dic = new Dictionary <string, HashSet <string> >();

            tx.ForEach(a => a.Inputs.ForEach(b =>
            {
                if (!dic.ContainsKey(b.PublicKey))
                {
                    dic.Add(b.PublicKey, new HashSet <string>());
                }
                if (dic[b.PublicKey].Contains(b.TxHash))
                {
                    isVerified = false;
                }
                else
                {
                    dic[b.PublicKey].Add(b.TxHash);
                }
            }));
            if (!isVerified)
            {
                return(false);
            }

            //generation transaction
            var txGen = Txs.First();

            if (!txGen.Outputs.Sum(d => d.Amount)
                .Equals(tx.Sum(a => a.Inputs.Sum(b => b.Amount) - a.Outputs.Sum(c => c.Amount)) + 50m))
            {
                return(false);
            }

            return(true);
        }