コード例 #1
0
        private Block CreateNewBlock(ChainedBlock pindexPrev, bool proofOfStake, out long fee)
        {
            fee = 0;

            // create the block
            var block = new Block();

            var height = pindexPrev.Height + 1;

            // Create coinbase tx
            var txNew = new Transaction();

            txNew.AddInput(new TxIn());
            txNew.AddOutput(new TxOut(Money.Zero, Script.Empty));

            if (!proofOfStake)
            {
                throw new MinedBlockException("Only POS transactions supported");
            }
            else
            {
                // Height first in coinbase required for block.version=2
                txNew.Inputs[0].ScriptSig = new Script(BitConverter.GetBytes(height));                 //(CScript() << nHeight) + COINBASE_FLAGS;
                if (!(txNew.Inputs[0].ScriptSig.Length <= 100))
                {
                    throw new MinedBlockException();
                }
            }

            // Add our coinbase tx as first transaction
            block.AddTransaction(txNew);

            // Largest block you're willing to create:
            uint nBlockMaxSize = 1000000 / 2 / 2;         //GetArg("-blockmaxsize", MAX_BLOCK_SIZE_GEN / 2);

            // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
            nBlockMaxSize = Math.Max((uint)1000, Math.Min((uint)(1000000 - 1000), nBlockMaxSize));

            // How much of the block should be dedicated to high-priority transactions,
            // included regardless of the fees they pay
            uint nBlockPrioritySize = 27000;             //GetArg("-blockprioritysize", 27000);

            nBlockPrioritySize = Math.Min(nBlockMaxSize, nBlockPrioritySize);

            // Minimum block size you want to create; block will be filled with free transactions
            // until there are no more or the block reaches this size:
            uint nBlockMinSize = 0;             //GetArg("-blockminsize", 0);

            nBlockMinSize = Math.Min(nBlockMaxSize, nBlockMinSize);

            // Fee-per-kilobyte amount considered the same as "free"
            // Be careful setting this: if you set it to zero then
            // a transaction spammer can cheaply fill blocks using
            // 1-satoshi-fee transactions. It should be set above the real
            // cost to you of processing a transaction.
            long nMinTxFee = 10000;             //MIN_TX_FEE;

            //if (mapArgs.count("-mintxfee"))
            //	ParseMoney(mapArgs["-mintxfee"], nMinTxFee);

            block.Header.Bits = BlockValidator.GetNextTargetRequired(pindexPrev, this.Context.Network.Consensus, proofOfStake);

            // Collect memory pool transactions into the block
            // ============================
            // todo: add transactions from the mem pool when its implemented
            // ============================

            //if (fDebug && GetBoolArg("-printpriority", false))
            //	LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize);

            if (!proofOfStake)
            {
                block.Transactions[0].Outputs[0].Value = BlockValidator.GetProofOfWorkReward(this.chainIndex, fee);
            }

            //if (pFees)
            //	*pFees = nFees;

            // Fill in header
            block.Header.HashPrevBlock = pindexPrev.HashBlock;
            block.Header.BlockTime     = pindexPrev.GetMedianTimePast() + TimeSpan.FromSeconds(1.0);         //pblock->nTime = max(pindexPrev->GetPastTimeLimit() + 1, pblock->GetMaxTransactionTime());
            if (!proofOfStake)
            {
                block.Header.UpdateTime(this.Context.Network, pindexPrev);
            }

            block.Header.Nonce = 0;

            return(block);
        }