Beispiel #1
0
        static void RunWithTransactionPool()
        {
            var       txn5     = SetupTransactions();
            IKeyStore keyStore = new KeyStore(HashFun.Generate256BitKey());

            IMBlock block1 = new MBlock(0, keyStore);
            IMBlock block2 = new MBlock(1, keyStore);
            IMBlock block3 = new MBlock(2, keyStore);
            IMBlock block4 = new MBlock(3, keyStore);

            AddTransactionsToBlocksAndCalculateHashes(block1, block2, block3, block4);

            MBlockChain chain = new MBlockChain();

            chain.AcceptBlock(block1);
            chain.AcceptBlock(block2);
            chain.AcceptBlock(block3);
            chain.AcceptBlock(block4);

            chain.VerifyChain();

            Console.WriteLine("");
            Console.WriteLine("");

            txn5.ClaimNumber = "weqwewe";
            chain.VerifyChain();

            Console.WriteLine();
        }
Beispiel #2
0
 public void HandleData(ushort[] data, ushort start, ushort count)
 {
     for (int i = 0; i < count; i++)
     {
         ushort point = data[i];
         MBlock block = new MBlock((byte)((point & 0xff00) >> 8), (char)(point & 0x00ff));
         Data[start + i] = block;
     }
 }
Beispiel #3
0
        /// <summary>
        /// 读取区块文件
        /// </summary>
        /// <param name="index"></param>
        /// <param name="hash"></param>
        /// <returns></returns>m
        private async Task <MBlock> ReadBlockByFileAsync(int index, string hash)
        {
            string filePath = Path.Combine(_dataDirectoryPath, $"{index}_{hash}.json");

            if (!File.Exists(filePath))
            {
                return(null);
            }
            string jsonData = await File.ReadAllTextAsync(filePath);

            MBlock block = MBlock.FormJson(jsonData);

            return(block);
        }
Beispiel #4
0
        /// <summary>
        /// 读取区块文件
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        private async Task <MBlock> ReadBlockByFileAsync(int index)
        {
            var directoryInfo = new DirectoryInfo(_dataDirectoryPath);

            FileInfo[] fileInfos = directoryInfo.GetFiles($"{index}_*.json");
            if (fileInfos.Length != 1)
            {
                return(null);
            }
            FileInfo fileInfo = fileInfos[0];
            string   jsonData = await File.ReadAllTextAsync(fileInfo.FullName);

            MBlock block = MBlock.FormJson(jsonData);

            return(block);
        }
Beispiel #5
0
        /// <summary>
        /// 读取最新的区块文件
        /// </summary>
        /// <returns></returns>
        private async Task <MBlock> ReadNewestBlockByFileAsync()
        {
            var    index   = 0;
            MBlock upBlock = null;

            while (true)
            {
                MBlock block = await ReadBlockByFileAsync(index ++);

                if (block == null)
                {
                    return(upBlock);
                }
                upBlock = block;
            }
        }
Beispiel #6
0
        private static void RunMultuipleTransactionsBlockExample()
        {
            IMBlock block1 = new MBlock(0);
            IMBlock block2 = new MBlock(1);
            IMBlock block3 = new MBlock(2);
            IMBlock block4 = new MBlock(3);

            block1.AddTransaction(txn1);
            block1.AddTransaction(txn2);
            block1.AddTransaction(txn3);
            block1.AddTransaction(txn4);

            block2.AddTransaction(txn5);
            block2.AddTransaction(txn6);
            block2.AddTransaction(txn7);
            block2.AddTransaction(txn8);

            block3.AddTransaction(txn9);
            block3.AddTransaction(txn10);
            block3.AddTransaction(txn11);
            block3.AddTransaction(txn12);

            block4.AddTransaction(txn13);
            block4.AddTransaction(txn14);
            block4.AddTransaction(txn15);
            block4.AddTransaction(txn16);

            block1.SetBlockHash(null);
            block2.SetBlockHash(block1);
            block3.SetBlockHash(block2);
            block4.SetBlockHash(block4);

            MBlockChain chain = new MBlockChain();

            chain.AcceptBlock(block1);
            chain.AcceptBlock(block2);
            chain.AcceptBlock(block3);
            chain.AcceptBlock(block4);

            chain.VerifyChain();

            Console.WriteLine("");
            Console.WriteLine("");

            txn4.ClaimNumber = "El ciupos";
            chain.VerifyChain();
        }
Beispiel #7
0
        /// <summary>
        /// 获得所有信息
        /// </summary>
        /// <returns></returns>
        public async Task <List <string> > GetAllInformationAsync()
        {
            var    result      = new List <string>();
            MBlock targetBlock = _nowBlock;

            while (targetBlock != null)
            {
                int index = targetBlock.Index;
                result.Insert(0, targetBlock.GetData <string>());
                targetBlock = await ReadBlockByFileAsync(targetBlock.Index - 1, targetBlock.PrevHash);

                if (targetBlock == null && index != 0)
                {
                    result.Clear();
                }
            }
            return(result);
        }
Beispiel #8
0
 /// <summary>
 /// 记录新的信息
 /// </summary>
 /// <param name="message"></param>
 public async Task RecordNewMessage(string message)
 {
     if (_nowBlock == null)
     {
         _nowBlock = new MBlock(message);
     }
     else
     {
         var newBlock = new MBlock(_nowBlock, message);
         if (_nowBlock.IsNextBlock(newBlock))
         {
             _nowBlock = newBlock;
         }
         else
         {
             throw new MateralBlockchainException("记录数据失败");
         }
     }
     await SaveToJsonFileAsync(_nowBlock);
 }
Beispiel #9
0
        void MBlockTest()
        {
            MBlock container;

            container = new MBlock(new FileAddress(0, 20));

            container.InsertAsset(null, 3, 8);
            container.InsertAsset(null, 10, 15);
            container.InsertAsset(null, 10, 12);
            container.InsertAsset(null, 0, 1);

            StringBuilder sb = new StringBuilder();

            foreach (MBlock b in container)
            {
                sb.AppendLine(string.Format("{0:X2}:{1:X2}",
                                            b.Address.Start,
                                            b.Address.End));
            }
            outRichTextBox.Text = sb.ToString();
        }
Beispiel #10
0
 private void Setup()
 {
     Data = new MBlock[VERTICAL_LINES * HORIZONTAL_LINES];
 }
Beispiel #11
0
 /// <summary>
 /// 保存到Json文件
 /// </summary>
 /// <param name="block"></param>
 private async Task SaveToJsonFileAsync(MBlock block)
 {
     string filePath   = Path.Combine(_dataDirectoryPath, $"{block.Index}_{block.Hash}.json");
     string jsonString = block.ToJson();
     await TextFileManager.WriteTextAsync(filePath, jsonString);
 }
        SettedBlock[] GenerateSegmentedLayer(int layerNum, out int MaxHeight)
        {
            SettedBlock[] layer = new SettedBlock[rawScheme.GetLength(1) + 1];
            MaxHeight = 0;

            List <MBlock> mBlocks = new List <MBlock>();
            UnsettedBlock last    = new UnsettedBlock {
                ID = -1, Set = ColorType.Normal
            };
            MBlock curMBlock = new MBlock();

            curMBlock.Add(new UnsettedBlock {
                ID = -1, Set = ColorType.Normal
            });
            bool isUp = rawScheme[layerNum, 0].Set == ColorType.Light;

            for (int i = 0; i < rawScheme.GetLength(1); ++i)
            {
                if (!isUp)
                {
                    if (rawScheme[layerNum, i].Set == ColorType.Dark || rawScheme[layerNum, i].Set == ColorType.Normal)
                    {
                        curMBlock.Add(rawScheme[layerNum, i]);
                    }
                    else
                    {
                        isUp = true;
                        curMBlock.Add(rawScheme[layerNum, i]);
                    }
                }
                else
                {
                    if (rawScheme[layerNum, i].Set == ColorType.Light || rawScheme[layerNum, i].Set == ColorType.Normal)
                    {
                        curMBlock.Add(rawScheme[layerNum, i]);
                    }
                    else
                    {
                        isUp = false;
                        mBlocks.Add(curMBlock.Clone() as MBlock);
                        curMBlock = new MBlock();
                        curMBlock.Add(rawScheme[layerNum, i]);
                    }
                }
            }
            mBlocks.Add(curMBlock.Clone() as MBlock);
            foreach (MBlock mb in mBlocks)
            {
                mb.Calculate();
            }
            int minH = 0;

            for (int j = 1; j < mBlocks.Count; ++j)
            {
                if (mBlocks[j].StartH >= mBlocks[j - 1].EndH)
                {
                    mBlocks[j].Shift = mBlocks[j].StartH - mBlocks[j - 1].EndH + 1;
                    if (minH > mBlocks[j].Shift)
                    {
                        minH = mBlocks[j].Shift;
                    }
                    for (int k = j - 1; k >= 0; --k)
                    {
                        if (mBlocks[k].EndH <= mBlocks[k + 1].StartH)
                        {
                            mBlocks[k].Shift += mBlocks[k + 1].StartH - mBlocks[k].EndH + 1;
                        }
                    }
                }
            }
            int curPos = -1;

            for (int j = 0; j < mBlocks.Count; ++j)
            {
                if (MaxHeight < mBlocks[j].StartH)
                {
                    MaxHeight = mBlocks[j].StartH;
                }
                if (MaxHeight < mBlocks[j].EndH)
                {
                    MaxHeight = mBlocks[j].EndH;
                }
                SettedBlock[] cur = mBlocks[j].Get();
                for (int k = 0; k < cur.Length; ++k)
                {
                    layer[++curPos] = cur[k];
                }
            }
            return(layer);
        }