Exemple #1
0
        public async Task <int> CountGlobalTransactions()
        {
            int result = 0;

            string current = await blocks.ActualBlockGet();

            if (current.Length != 0)
            {
                bool trigger = true;

                while (trigger)
                {
                    byte[] temp = await blocks.SearchBlock(current);

                    if (current.Length != 0)
                    {
                        Additional.Block block = new Additional.Block();

                        block = blocks.BlockDeSerialize(temp);

                        result += block.transactions_count;

                        if (block.previous == Blocks.first_block)
                        {
                            trigger = false;
                        }
                        current = block.previous;
                    }
                    else
                    {
                        trigger = false;
                    }
                }
            }
            return(result);
        }
Exemple #2
0
        public async Task <List <Transaction> > GetTransactionsListToPublicKey(string wallet)
        {
            List <Transaction> transactions = new List <Transaction>();

            try
            {
                Transaction        temp_tran = new Transaction();
                List <Transaction> black     = new List <Transaction>();

                string current = await blocks.ActualBlockGet();

                int    tst = Transactions.size_transaction;
                byte[] temp_transaction = new byte[tst];

                Block temp = new Block();

                bool trigger = true;

                if (current.Length != 0)
                {
                    while (trigger)
                    {
                        byte[] data = await blocks.SearchBlock(current);

                        temp = blocks.BlockDeSerialize(data);

                        for (int i = 0; i < temp.transactions_count; i++)
                        {
                            Array.Copy(temp.transactions, i * tst, temp_transaction, 0, tst);
                            temp_tran = ParseTMessage(temp_transaction);

                            if (temp_tran.output.put == wallet)
                            {
                                transactions.Add(temp_tran);
                            }
                        }

                        if (temp.previous == Blocks.first_block)
                        {
                            trigger = false;
                        }

                        current = temp.previous;
                    }

                    trigger = true;
                    current = await blocks.ActualBlockGet();

                    while (trigger)
                    {
                        byte[] data = await blocks.SearchBlock(current);

                        temp = blocks.BlockDeSerialize(data);

                        for (int i = 0; i < temp.transactions_count; i++)
                        {
                            Array.Copy(temp.transactions, i * tst, temp_transaction, 0, tst);
                            temp_tran = ParseTMessage(temp_transaction);

                            foreach (Transaction trn in transactions)
                            {
                                if (temp_tran.input.put == trn.name)
                                {
                                    black.Add(trn);
                                }
                            }
                        }


                        if (temp.previous == Blocks.first_block)
                        {
                            trigger = false;
                        }

                        current = temp.previous;
                    }
                }

                List <string> local_list = new List <string>();
                string        path       = filesystem.FSConfig.db_path;

                local_list = filesystem.GetFilesListFromDirectory(path);

                foreach (string local_el in local_list)
                {
                    Transaction local = new Transaction();


                    byte[] local_by = new byte[0];

                    local_by = await filesystem.GetFromFileAsync(path + @"\" + local_el);

                    local = ParseTMessage(local_by);

                    foreach (Transaction trn in transactions)
                    {
                        if (local.input.put == trn.name)
                        {
                            black.Add(trn);
                        }
                    }
                }

                foreach (Transaction trn in black)
                {
                    transactions.Remove(trn);
                }
            }
            catch (Exception ex)
            {
                window.WriteLine(ex.ToString());
            }
            window.WriteLine("Found: " + transactions.Count);
            return(transactions);
        }
Exemple #3
0
        private async Task AnswerLogic(byte[] incoming_data, IPEndPoint source)
        {
            try
            {
                TwoBytesArrays TWdata    = new TwoBytesArrays();
                byte[]         data      = new byte[0];
                string         operation = string.Empty;
                string         path      = string.Empty;
                int            size      = 0;
                string         name;

                TWdata    = ByteArrayCut(incoming_data, GetLogic.operation_size);
                operation = BytesToOperation(TWdata.part1);

                window.WriteLine("Got AnswerLogic: " + operation);
                if (TWdata.part2.Length != 0)
                {
                    switch (operation)
                    {
                    case "signature":
                        path = filesystem.FSConfig.root_path + @"\signature";
                        window.WriteLine("Writing: " + path + " " + TWdata.part2.Length);
                        await filesystem.AddInfoToFileAsync(path, TWdata.part2, true);

                        await information.ActualizeSelfSignature();

                        break;

                    case "actualblock":
                        if (waitingforactual)
                        {
                            string new_actual = Encoding.UTF8.GetString(TWdata.part2);
                            path = filesystem.FSConfig.db_blocks_path + @"\actualblock";

                            window.WriteLine("AnswerLogic.actualblock " + new_actual);
                            data = await blocks.SearchBlock(new_actual);

                            if (data.Length == 0)
                            {
                                waitingfornewactual = true;
                            }
                            else
                            {
                                await filesystem.AddInfoToFileAsync(path, TWdata.part2, true);

                                path = filesystem.FSConfig.db_blocks_path;
                                string current       = Encoding.UTF8.GetString(await filesystem.GetFromFileAsync(path));
                                byte[] current_array = await filesystem.GetFromFileAsync(path + @"\" + current);

                                byte[] new_array = await filesystem.GetFromFileAsync(path + @"\" + new_actual);

                                Block current_block = new Block();
                                Block new_block     = new Block();

                                current_block = blocks.BlockDeSerialize(current_array);
                                new_block     = blocks.BlockDeSerialize(new_array);

                                if (current_block.time < new_block.time)
                                {
                                    await blocks.ActualBlockSet(new_actual);
                                }
                            }
                            waitingforactual = false;
                        }
                        break;

                    case "block":
                        //window.WriteLine("Got AnswerLogic block");

                        size = Convert.ToInt32(Encoding.UTF8.GetString(TWdata.part2));
                        window.WriteLine("getting block from: " + source.ToString());
                        data = await filetransfering.TcpDataGet(source, size);

                        window.WriteLine("Block got");
                        name = blocks.GetBlockName(data);

                        if (name != string.Empty)
                        {
                            path = filesystem.FSConfig.db_blocks_path + @"\" + name;
                            window.WriteLine("new block: " + path);
                            await filesystem.AddInfoToFileAsync(path, data, true);

                            if (waitingfornewactual)
                            {
                                waitingfornewactual = false;
                                await blocks.ActualBlockSet(name);
                            }
                            else
                            {
                                await blocks.BlockChainActualize();
                            }
                        }

                        if (name.Length != 0)
                        {
                            window.WriteLine("block got: " + name);
                        }
                        else
                        {
                            window.WriteLine("wrong block");
                        }
                        break;

                    case "newblock":
                        //window.WriteLine("Got AnswerLogic block");

                        size = Convert.ToInt32(Encoding.UTF8.GetString(TWdata.part2));
                        window.WriteLine("getting newblock from: " + source.ToString());
                        data = await filetransfering.TcpDataGet(source, size);

                        window.WriteLine("newBlock got");
                        name = blocks.GetBlockName(data);

                        if (name != string.Empty)
                        {
                            path = filesystem.FSConfig.db_blocks_path + @"\" + name;
                            window.WriteLine("new block: " + path);
                            await filesystem.AddInfoToFileAsync(path, data, true);

                            await blocks.ActualBlockSet(name);
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                window.WriteLine(ex.ToString());
            }
        }