Skip to content

Commit

Permalink
Improve the random security (#1145)
Browse files Browse the repository at this point in the history
* Remove global randoms

* Wallet

* Optimize

* Use random class

* Revert wallet
  • Loading branch information
shargon committed Oct 15, 2019
1 parent 29378af commit 2ca5b6b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion neo/Consensus/ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ internal class ConsensusContext : IDisposable, ISerializable
private int _witnessSize;
private readonly Wallet wallet;
private readonly Store store;
private readonly Random random = new Random();

public int F => (Validators.Length - 1) / 3;
public int M => Validators.Length - F;
Expand Down Expand Up @@ -266,6 +265,7 @@ internal void EnsureMaxBlockSize(IEnumerable<Transaction> txs)

public ConsensusPayload MakePrepareRequest()
{
var random = new Random();
byte[] buffer = new byte[sizeof(ulong)];
random.NextBytes(buffer);
Block.ConsensusData.Nonce = BitConverter.ToUInt64(buffer, 0);
Expand Down
6 changes: 3 additions & 3 deletions neo/Network/RPC/TransactionManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Neo.Cryptography.ECC;
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC.Models;
Expand All @@ -15,7 +15,6 @@ namespace Neo.Network.RPC
/// </summary>
public class TransactionManager
{
private static readonly Random rand = new Random();
private readonly RpcClient rpcClient;
private readonly UInt160 sender;

Expand Down Expand Up @@ -50,11 +49,12 @@ public TransactionManager(RpcClient rpc, UInt160 sender)
/// <returns></returns>
public TransactionManager MakeTransaction(byte[] script, TransactionAttribute[] attributes = null, Cosigner[] cosigners = null, long networkFee = 0)
{
var random = new Random();
uint height = rpcClient.GetBlockCount() - 1;
Tx = new Transaction
{
Version = 0,
Nonce = (uint)rand.Next(),
Nonce = (uint)random.Next(),
Script = script,
Sender = sender,
ValidUntilBlock = height + Transaction.MaxValidUntilBlockIncrement,
Expand Down

0 comments on commit 2ca5b6b

Please sign in to comment.