当前位置: 首页>>代码示例>>C#>>正文


C# UInt256.ToByteArray方法代码示例

本文整理汇总了C#中UInt256.ToByteArray方法的典型用法代码示例。如果您正苦于以下问题:C# UInt256.ToByteArray方法的具体用法?C# UInt256.ToByteArray怎么用?C# UInt256.ToByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UInt256的用法示例。


在下文中一共展示了UInt256.ToByteArray方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MineBlockHeader

        public static BlockHeader MineBlockHeader(BlockHeader blockHeader, UInt256 hashTarget)
        {
            var blockHeaderBytes = DataCalculator.EncodeBlockHeader(blockHeader);

            var hashTargetBytes = hashTarget.ToByteArray();

            var start = 0;
            var finish = UInt32.MaxValue;
            var total = 0L;
            var nonceIndex = 76;
            var minedNonce = (UInt32?)null;

            //Debug.WriteLine("Starting mining: {0}".Format2(DateTime.Now.ToString("hh:mm:ss")));

            var stopwatch = new Stopwatch();
            stopwatch.Start();

            Parallel.For(
                start, finish,
                () => new LocalMinerState(blockHeaderBytes),
                (nonceLong, loopState, localState) =>
                {
                    localState.total++;

                    var nonce = (UInt32)nonceLong;
                    var nonceBytes = Bits.GetBytes(nonce);
                    Buffer.BlockCopy(nonceBytes, 0, localState.headerBytes, nonceIndex, 4);

                    var headerBytes = localState.headerBytes;
                    var sha256 = localState.sha256;
                    var hashBytes = sha256.ComputeHash(sha256.ComputeHash(headerBytes));

                    if (BytesCompareLE(hashBytes, hashTargetBytes) < 0)
                    {
                        minedNonce = nonce;
                        loopState.Break();
                    }

                    return localState;
                },
                localState => { Interlocked.Add(ref total, localState.total); });

            stopwatch.Stop();

            var hashRate = ((float)total / 1000 / 1000) / ((float)stopwatch.ElapsedMilliseconds / 1000);

            if (minedNonce != null)
            {
                //Debug.WriteLine("Found block in {0} hh:mm:ss at Nonce {1}, Hash Rate: {2} mHash/s, Total Hash Attempts: {3}, Found Hash: {4}".Format2(stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), minedNonce, hashRate, total.ToString("#,##0"), blockHeader.With(Nonce: minedNonce).Hash));
                return blockHeader.With(Nonce: minedNonce);
            }
            else
            {
                //Debug.WriteLine("No block found in {0} hh:mm:ss, Hash Rate: {1} mHash/s, Total Hash Attempts: {2}, Found Hash: {3}".Format2(stopwatch.Elapsed.ToString(@"hh\:mm\:ss"), hashRate, total.ToString("#,##0"), blockHeader.With(Nonce: minedNonce).Hash));
                return null;
            }
        }
开发者ID:holinov,项目名称:BitSharp,代码行数:57,代码来源:Miner.cs

示例2: TxLookupKey

        /// <summary>
        /// Initializes a new instance of <see cref="TxLookupKey"/> with the specified block hash and transaction index.
        /// </summary>
        /// <param name="blockHash">The hash of the block containing the transaction.</param>
        /// <param name="txIndex">The index of the transaction within its block.</param>
        public TxLookupKey(UInt256 blockHash, int txIndex)
        {
            BlockHash = blockHash;
            TxIndex = txIndex;

            var hashBytes = new byte[36];
            blockHash.ToByteArray(hashBytes);
            Bits.EncodeInt32(txIndex, hashBytes, 32);
            hashCode = Bits.ToInt32(new xxHash(32).ComputeHash(hashBytes));
        }
开发者ID:pmlyon,项目名称:BitSharp,代码行数:15,代码来源:TxLookupKey.cs

示例3: MineBlockHeader

        public BlockHeader MineBlockHeader(BlockHeader blockHeader, UInt256 hashTarget)
        {
            var blockHeaderBytes = DataEncoder.EncodeBlockHeader(blockHeader);

            var hashTargetBytes = hashTarget.ToByteArray();

            var start = 0;
            var finish = UInt32.MaxValue;
            var total = 0L;
            var nonceIndex = 76;
            var minedNonce = (UInt32?)null;

            logger.Debug($"Starting mining: {DateTime.Now:hh':'mm':'ss}");

            var stopwatch = Stopwatch.StartNew();

            Parallel.For(
                start, finish,
                () => new LocalMinerState(blockHeaderBytes),
                (nonceLong, loopState, localState) =>
                {
                    localState.total++;

                    var nonce = (UInt32)nonceLong;
                    var nonceBytes = Bits.GetBytes(nonce);
                    Buffer.BlockCopy(nonceBytes, 0, localState.headerBytes, nonceIndex, 4);

                    var headerBytes = localState.headerBytes;
                    var hashBytes = SHA256Static.ComputeDoubleHash(headerBytes);

                    if (BytesCompareLE(hashBytes, hashTargetBytes) < 0)
                    {
                        minedNonce = nonce;
                        loopState.Stop();
                    }

                    return localState;
                },
                localState => { Interlocked.Add(ref total, localState.total); });

            stopwatch.Stop();

            var hashRate = ((float)total / 1000 / 1000) / ((float)stopwatch.ElapsedMilliseconds / 1000);

            if (minedNonce != null)
            {
                logger.Debug($"Found block in {stopwatch.Elapsed:hh':'mm':'ss} hh:mm:ss at Nonce {minedNonce}, Hash Rate: {hashRate} mHash/s, Total Hash Attempts: {total:N0}, Found Hash: {blockHeader.With(Nonce: minedNonce).Hash}");
                return blockHeader.With(Nonce: minedNonce);
            }
            else
            {
                logger.Debug($"No block found in {stopwatch.Elapsed:hh':'mm':'ss} hh:mm:ss, Hash Rate: {hashRate} mHash/s, Total Hash Attempts: {total:N0}, Found Hash: {blockHeader.With(Nonce: minedNonce).Hash}");
                return null;
            }
        }
开发者ID:cole2295,项目名称:BitSharp,代码行数:55,代码来源:Miner.cs

示例4: MineBlockHeader

        public BlockHeader MineBlockHeader(BlockHeader blockHeader, UInt256 hashTarget)
        {
            var blockHeaderBytes = DataEncoder.EncodeBlockHeader(blockHeader);

            var hashTargetBytes = hashTarget.ToByteArray();

            var start = 0;
            var finish = UInt32.MaxValue;
            var total = 0L;
            var nonceIndex = 76;
            var minedNonce = (UInt32?)null;

            var stopwatch = Stopwatch.StartNew();

            Parallel.For(
                start, finish,
                () => new LocalMinerState(blockHeaderBytes),
                (nonceLong, loopState, localState) =>
                {
                    localState.total++;

                    var nonce = (UInt32)nonceLong;
                    var nonceBytes = Bits.GetBytes(nonce);
                    Buffer.BlockCopy(nonceBytes, 0, localState.headerBytes, nonceIndex, 4);

                    var headerBytes = localState.headerBytes;
                    var hashBytes = SHA256Static.ComputeDoubleHash(headerBytes);

                    if (BytesCompareLE(hashBytes, hashTargetBytes) < 0)
                    {
                        minedNonce = nonce;
                        loopState.Stop();
                    }

                    return localState;
                },
                localState => { Interlocked.Add(ref total, localState.total); });

            stopwatch.Stop();

            var hashRate = total / stopwatch.Elapsed.TotalSeconds;

            if (minedNonce == null)
                throw new InvalidOperationException();

            var minedHeader = blockHeader.With(Nonce: minedNonce);

            logger.Debug($"Found block in {stopwatch.Elapsed.TotalMilliseconds:N3}ms at Nonce {minedNonce}, Hash Rate: {hashRate / 1.MILLION()} mHash/s, Total Hash Attempts: {total:N0}, Found Hash: {minedHeader.Hash}");
            return minedHeader;
        }
开发者ID:pmlyon,项目名称:BitSharp,代码行数:50,代码来源:Miner.cs

示例5: GetBytes

 public static byte[] GetBytes(UInt256 value)
 {
     return value.ToByteArray();
 }
开发者ID:holinov,项目名称:BitSharp,代码行数:4,代码来源:Bits.cs

示例6: Write32Bytes

 public static void Write32Bytes(this BinaryWriter writer, UInt256 value)
 {
     writer.Write(value.ToByteArray());
 }
开发者ID:holinov,项目名称:BitSharp,代码行数:4,代码来源:WriterExtensionMethods.cs

示例7: CreateBlock

        public Block CreateBlock(UInt256 previousBlockHash, int txCount, UInt256 target = null)
        {
            var coinbaseTx = new Transaction
            (
                version: 0,
                inputs: ImmutableArray.Create
                (
                    new TxInput
                    (
                        previousTxOutputKey: new TxOutputKey
                        (
                            txHash: UInt256.Zero,
                            txOutputIndex: 0
                        ),
                        scriptSignature: previousBlockHash.ToByteArray().Concat(random.NextBytes(100)).ToImmutableArray(),
                        sequence: 0
                    )
                ),
                outputs: ImmutableArray.Create
                (
                    new TxOutput
                    (
                        value: 50 * SATOSHI_PER_BTC,
                        scriptPublicKey: this.txManager.CreatePublicKeyScript(coinbasePublicKey).ToImmutableArray()
                    )
                ),
                lockTime: 0
            );

            var transactionsBuilder = ImmutableArray.CreateBuilder<Transaction>(txCount + 1);
            transactionsBuilder.Add(coinbaseTx);

            var prevTx = coinbaseTx;
            for (var i = 1; i < transactionsBuilder.Capacity; i++)
            {
                var outputs =
                    i % 2 == 0 ?
                    ImmutableArray.Create(
                        new TxOutput(prevTx.Outputs[0].Value - 1, coinbaseTx.Outputs[0].ScriptPublicKey),
                        new TxOutput(1, coinbaseTx.Outputs[0].ScriptPublicKey))
                    :
                    ImmutableArray.Create(new TxOutput(prevTx.Outputs[0].Value - 1, coinbaseTx.Outputs[0].ScriptPublicKey));

                var tx = new Transaction(
                    version: 0,
                    inputs: ImmutableArray.Create(new TxInput(new TxOutputKey(prevTx.Hash, 0), new byte[100].ToImmutableArray(), 0)),
                    outputs: outputs,
                    lockTime: 0);

                transactionsBuilder.Add(tx);
                prevTx = tx;
            }

            var transactions = transactionsBuilder.MoveToImmutable();

            var merkleRoot = MerkleTree.CalculateMerkleRoot(transactions);

            var block = new Block
            (
                header: new BlockHeader
                (
                    version: 0,
                    previousBlock: previousBlockHash,
                    merkleRoot: merkleRoot,
                    time: 0,
                    bits: DataCalculator.TargetToBits(target ?? UnitTestRules.Target0),
                    nonce: 0
                ),
                transactions: transactions
            );

            return block;
        }
开发者ID:ArsenShnurkov,项目名称:BitSharp,代码行数:73,代码来源:TestBlocks.cs


注:本文中的UInt256.ToByteArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。