コード例 #1
0
ファイル: Blockchain.cs プロジェクト: jhonkus/UbudKusCoin
        private static void Initialize()
        {
            var blocks = GetBlocks();

            if (blocks.Count() < 1)
            {
                // start block time
                var startTimer = DateTime.UtcNow;

                // crate genesis transaction
                Transaction.CreateGenesisTransction();

                // crate ICO transaction
                Transaction.CreateIcoTransction();

                // get all ico transaction from pool
                // var trxPool = Transaction.GetPool();
                // var transactions = trxPool.FindAll().ToList();

                var transactions = Transaction.GetPool();

                //convert transaction to json for more easy
                //var strTransactions = JsonConvert.SerializeObject(transactions);

                // create genesis block
                var block = Block.GenesisBlock(transactions);

                var str = System.Text.Json.JsonSerializer.Serialize(block);
                block.Size = str.Length;

                // get build time
                var endTimer  = DateTime.UtcNow;
                var buildTime = endTimer - startTimer;
                block.BuildTime = buildTime.Milliseconds;
                // end of

                // update accoiunt table
                var txns = block.Transactions;
                AccountServiceImpl.UpdateAccountBalance(txns);

                // add genesis block to blockchain
                AddBlock(block);

                // move all record in trx pool to transactions table
                foreach (Transaction trx in transactions)
                {
                    Transaction.Add(trx);
                }

                // clear mempool
                // trxPool.DeleteAll();
                Transaction.DeletePool();
            }
        }
コード例 #2
0
ファイル: Blockchain.cs プロジェクト: jhonkus/UbudKusCoin
        public static void BuildNewBlock()
        {
            // start build time
            var startTimer = DateTime.UtcNow;

            // get transaction from pool
            var trxPool = Transaction.GetPool();

            //// get last block to get prev hash and last height
            var lastBlock = GetLastBlock();
            var height    = lastBlock.Height + 1;
            var timestamp = Utils.GetTime();
            var prevHash  = lastBlock.Hash;
            var validator = Stake.GetValidator();


            var transactions = new List <Transaction>(); // JsonConvert.SerializeObject(new List<Transaction>());


            // validator will get coin reward from genesis account
            // to keep total coin in Blockchain not changed
            var conbaseTrx = new Transaction
            {
                Amount = 0,
                // Recipient = "UKC_QPQY9wHP0jxi/0c/YRlch2Uk5ur/T8lcOaawqyoe66o=",
                Recipient = "UkcDrMshfqdHrfckb2SLoSCoG8Lp6MBdrkZ2T83FivTpWC8",
                Fee       = COINT_REWARD,
                TimeStamp = timestamp,
                // Sender = "UKC_rcyChuW7cQcIVoKi1LfSXKfCxZBHysTwyPm88ZsN0BM="
                Sender = "UkcDEfU9gGnm9tGjmFtXRjirf2LuohU5CzjWunEkPNbUcFW",
            };

            if (trxPool.Count() > 0)
            {
                //Get all tx from pool
                conbaseTrx.Recipient = validator;
                //sum all fees and give block creator as reward
                conbaseTrx.Amount = GetTotalFees(trxPool);
                conbaseTrx.Build();

                // add coinbase trx to list
                transactions.Add(conbaseTrx);
                transactions.AddRange(trxPool);

                // clear mempool
                Transaction.DeletePool();
            }
            else
            {
                conbaseTrx.Build();
                transactions.Add(conbaseTrx);
            }

            var block = new Block
            {
                Height       = height,
                TimeStamp    = timestamp,
                PrevHash     = prevHash,
                Transactions = GiveOtherInfos(transactions, height),
                Validator    = validator
            };

            block.Build();


            //block size
            var str = System.Text.Json.JsonSerializer.Serialize(block);

            block.Size = str.Length;

            // get build time
            var endTimer  = DateTime.UtcNow;
            var buildTime = endTimer - startTimer;

            block.BuildTime = buildTime.Milliseconds;
            // end of


            // update  balance

            var txns = block.Transactions;

            AccountServiceImpl.UpdateAccountBalance(txns);


            AddBlock(block);
            PrintBlock(block);

            // move all record in trx pool to transactions table
            foreach (var trx in transactions)
            {
                Transaction.Add(trx);
            }
        }