Beispiel #1
0
        public Block CreateBlock(Block preblk, ref bool bBlkMining)
        {
            if (preblk == null)
            {
                return(null);
            }
            string address = Wallet.GetWallet().GetCurWallet().ToAddress();

            Block myblk = new Block();

            myblk.Address   = Wallet.GetWallet().GetCurWallet().ToAddress();
            myblk.prehash   = preblk != null ? preblk.hash : "";
            myblk.timestamp = nodeManager.GetNodeTime();
            myblk.height    = preblk != null ? preblk.height + 1 : 1;
            myblk.random    = System.Guid.NewGuid().ToString("N").Substring(0, 16);

            //引用上一周期的连接块
            var blks1 = blockMgr.GetBlock(preblk.height);
            var blks2 = BlockChainHelper.GetRuleBlk(consensus, blks1, preblk.prehash);

            for (int ii = 0; ii < blks2.Count; ii++)
            {
                myblk.AddBlock(ii, blks2[ii]);
            }

            // 比较相同高度的出块
            var blks3   = blockMgr.GetBlock(myblk.height);
            var blklast = blks3.Find(x => (x.Address == address && x.prehash == myblk.prehash && x.linksblk.Count >= myblk.linksblk.Count));

            if (blklast != null)
            {
                bBlkMining = false;
                return(blklast);
            }

            CalculatePower.SetDT(myblk, preblk, httpRule);

            RefTransfer(myblk);
            myblk.prehashmkl = myblk.ToHashMerkle(blockMgr);

            bBlkMining = true;

            //Log.Debug($"Rule.CreateBlock {myblk.height} linksblk:{myblk.linksblk.Count} linkstran:{myblk.linkstran.Count} blks1:{blks1.Count} blks3:{blks3.Count}");

            return(myblk);
        }