本文整理汇总了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;
}
}
示例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));
}
示例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;
}
}
示例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;
}
示例5: GetBytes
public static byte[] GetBytes(UInt256 value)
{
return value.ToByteArray();
}
示例6: Write32Bytes
public static void Write32Bytes(this BinaryWriter writer, UInt256 value)
{
writer.Write(value.ToByteArray());
}
示例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;
}