コード例 #1
0
 public static uint256 ParseStakeModifierV2(this RPCBlockTemplate blockTemplate)
 {
     if (blockTemplate != null && !string.IsNullOrWhiteSpace(blockTemplate.stakemodifierv2))
     {
         var value = uint256.Parse(blockTemplate.stakemodifierv2);
         if (value != uint256.Zero)
         {
             return(value);
         }
     }
     throw new InvalidOperationException($"{nameof(blockTemplate)} doesn't contain a valid {nameof(blockTemplate.stakemodifierv2)}.");
 }
コード例 #2
0
 public static SlimBlockHeader CreateSlimBlockHeader(this RPCBlockTemplate blockTemplate, bool isProofOfStake)
 {
     return(new SlimBlockHeader
     {
         Bits = blockTemplate.ParseBits(isProofOfStake),
         // caution: use uint256.Parse when parsing the strings to account for the endianness
         HashPrevBlock = uint256.Parse(blockTemplate.previousblockhash).ToBytes(),
         Version = blockTemplate.version,
         Nonce = 0,
         Timestamp = 0,
         MerkleRoot = null,
         Data = null
     });
 }
コード例 #3
0
        public static RPCBlockTemplate Clone(this RPCBlockTemplate blockTemplate)
        {
            var clone = new RPCBlockTemplate
            {
                height                     = blockTemplate.height,
                longpollid                 = blockTemplate.longpollid,
                transactions               = blockTemplate.transactions,
                stakemodifierv2            = blockTemplate.stakemodifierv2,
                coinbasevalue              = blockTemplate.coinbasevalue,
                bits                       = blockTemplate.bits,
                posbits                    = blockTemplate.posbits,
                previousblockhash          = blockTemplate.previousblockhash,
                version                    = blockTemplate.version,
                mintime                    = blockTemplate.mintime,
                capabilities               = blockTemplate.capabilities,
                curtime                    = blockTemplate.curtime,
                default_witness_commitment = blockTemplate.default_witness_commitment,
                mutable                    = blockTemplate.mutable,
                postarget                  = blockTemplate.postarget,
                previousblockconsensus     = blockTemplate.previousblockconsensus,
                previousblocktime          = blockTemplate.previousblocktime,
                target                     = blockTemplate.target,
                rules                      = blockTemplate.rules
            };

            List <RPCBlockTemplateTransaction> ttx = new List <RPCBlockTemplateTransaction>();

            foreach (var tx in blockTemplate.transactions)
            {
                ttx.Add(new RPCBlockTemplateTransaction
                {
                    txid    = tx.txid,
                    data    = tx.data,
                    depends = tx.depends,
                    fee     = tx.fee,
                    hash    = tx.hash,
                    sigops  = tx.sigops,
                    weight  = tx.weight
                });
            }

            clone.transactions = ttx.ToArray();

            return(clone);
        }
コード例 #4
0
        public static SlimBlock CreateTemplateBlock(this RPCBlockTemplate blockTemplate, uint time, bool isProofOfStake, Script scriptPubKey, Transaction coinstakeTransaction, Key blockSignatureKey)
        {
            if (!isProofOfStake && (scriptPubKey == null || coinstakeTransaction != null) ||
                isProofOfStake && (scriptPubKey != null || coinstakeTransaction == null))
            {
                throw new InvalidOperationException("Pow/pos parameter mismatch.");
            }

            var slimBlockHeader = blockTemplate.CreateSlimBlockHeader(isProofOfStake);

            slimBlockHeader.Timestamp = time;

            var slimBlock = new SlimBlock {
                IsProofOfStake = isProofOfStake, Height = blockTemplate.height, SlimBlockHeader = slimBlockHeader
            };

            if (isProofOfStake)
            {
                slimBlock.AddPoSCoinbaseTransaction();
                slimBlock.CoinstakeTransaction = coinstakeTransaction;
            }
            else
            {
                slimBlock.AddPoWCoinbaseTransaction(scriptPubKey, blockTemplate.coinbasevalue, blockTemplate.ExtraNonce);
            }

            slimBlock.SetPayloadTransactions(blockTemplate.transactions.Select(x =>
            {
                var tx = C.Network.CreateTransaction();
                tx.FromBytes(Encoders.Hex.DecodeData(x.data), C.ProtocolVersion);

                if (tx.GetHash() != uint256.Parse(x.txid))
                {
                    throw new InvalidOperationException();
                }

                return(tx);
            }));
            slimBlock.CreateWitnessCommitment();
            slimBlock.UpdateHeaderHashMerkleRoot();
            slimBlock.SignBlock(blockSignatureKey);
            slimBlock.ValidateWitnessCommitment();
            return(slimBlock);
        }
コード例 #5
0
 public static uint GetPreviousBlockTime(this RPCBlockTemplate blockTemplate)
 {
     return(blockTemplate.previousblocktime);
 }
コード例 #6
0
 public static uint256 ParsePreviousBlockHash(this RPCBlockTemplate blockTemplate)
 {
     return(uint256.Parse(blockTemplate.previousblockhash));
 }
コード例 #7
0
 public static uint ParseBits(this RPCBlockTemplate blockTemplate, bool isProofOfStake)
 {
     return(isProofOfStake
         ? Convert.ToUInt32(blockTemplate.posbits, 16)
         : Convert.ToUInt32(blockTemplate.bits, 16));
 }
コード例 #8
0
 public static int GetCurrentTipHeight(this RPCBlockTemplate blockTemplate)
 {
     return(blockTemplate.height - 1);
 }