Skip to content

Commit

Permalink
Code cleanup to comply with new convention
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Apr 4, 2022
1 parent f54839b commit 0b16530
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Src/Denovo/Models/UtxoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public bool TryConvertToUtxo(out Utxo result, out string error)
return false;
}

var scr = new PubkeyScript(scrBa);
PubkeyScript scr = new(scrBa);
result = new Utxo(Index, Amount, scr);
error = null;
return true;
Expand Down
8 changes: 4 additions & 4 deletions Src/Denovo/Services/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void AppendData(byte[] data, string fileName, string dir)
string path = Path.Combine(dir, $"{fileName}.ddat");
if (File.Exists(path))
{
using FileStream stream = new FileStream(path, FileMode.Append);
using FileStream stream = new(path, FileMode.Append);
stream.Write(data);
}
else
Expand Down Expand Up @@ -133,7 +133,7 @@ private void WriteData(byte[] data, string fileName, string dir)
private void WriteBlockInfo(IBlock block)
{
// block hash[32] | block size[4] | file name[4]
var stream = new FastStream(32 + 4 + 4);
FastStream stream = new(32 + 4 + 4);
stream.Write(block.Header.GetHash(false));
stream.Write(block.TotalSize);
stream.Write(blockFileNum);
Expand All @@ -145,11 +145,11 @@ private void WriteBlockInfo(IBlock block)
/// <inheritdoc/>
public void WriteBlock(IBlock block)
{
var temp = new FastStream(block.TotalSize);
FastStream temp = new(block.TotalSize);
block.Serialize(temp);

string fileName = $"Block{blockFileNum:D6}";
var info = new FileInfo(Path.Combine(blockDir, fileName));
FileInfo info = new(Path.Combine(blockDir, fileName));
long len = info.Exists ? info.Length : 0;
if (len + temp.GetSize() > Max)
{
Expand Down
12 changes: 6 additions & 6 deletions Src/Denovo/Services/TestNetMiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public async Task<IBlock> Start(IBlock prev, int height, CancellationToken token
// and IBlockchain.GetTarget() to mine at the correct difficulty.
// For now it is a good way of mining any transaction that won't propagate in TestNet by bitcoin core clients.

var consensus = new Consensus(height, NetworkType.TestNet);
Consensus consensus = new(height, NetworkType.TestNet);
string cbText = "Mined using Denovo v0.1.0";
// A weak key used only for testing
using var key = new PrivateKey(new Sha256().ComputeHash(Encoding.UTF8.GetBytes(cbText)));
var pkScr = new PubkeyScript();
using PrivateKey key = new(new Sha256().ComputeHash(Encoding.UTF8.GetBytes(cbText)));
PubkeyScript pkScr = new();
pkScr.SetToP2WPKH(key.ToPublicKey());

byte[] commitment = null;
Expand All @@ -48,7 +48,7 @@ public async Task<IBlock> Start(IBlock prev, int height, CancellationToken token

ulong fee = 0;

var coinbase = new Transaction()
Transaction coinbase = new()
{
Version = 1,
TxInList = new TxIn[]
Expand All @@ -67,7 +67,7 @@ public async Task<IBlock> Start(IBlock prev, int height, CancellationToken token
((PubkeyScript)coinbase.TxOutList[1].PubScript).SetToReturn(Encoding.UTF8.GetBytes("Testing Mining View Model."));


var block = new Block()
Block block = new()
{
Header = new BlockHeader()
{
Expand All @@ -86,7 +86,7 @@ public async Task<IBlock> Start(IBlock prev, int height, CancellationToken token
new Witness(new byte[][]{ commitment })
};

var temp = new TxOut[coinbase.TxOutList.Length + 1];
TxOut[] temp = new TxOut[coinbase.TxOutList.Length + 1];
Array.Copy(coinbase.TxOutList, 0, temp, 0, coinbase.TxOutList.Length);
temp[^1] = new TxOut();

Expand Down
22 changes: 11 additions & 11 deletions Src/Denovo/Services/UtxoDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ private void Init()
byte[] data = fileMan.ReadData(DbName);
if (data is not null && data.Length != 0)
{
var stream = new FastStreamReader(data);
FastStreamReader stream = new(data);
while (true)
{
var utxo = new Utxo();
Utxo utxo = new();
if (stream.TryReadByteArray(32, out byte[] hash) && utxo.TryDeserialize(stream, out _))
{
if (database.ContainsKey(hash))
Expand All @@ -92,7 +92,7 @@ private void Init()
data = fileMan.ReadData(CoinBaseDbName);
if (data is not null && data.Length != 0)
{
var stream = new FastStreamReader(data);
FastStreamReader stream = new(data);
if (!stream.CheckRemaining(8))
{
return;
Expand All @@ -102,7 +102,7 @@ private void Init()
int index = 0;
while (true)
{
var coinbase = new Transaction();
Transaction coinbase = new();
if (coinbase.TryDeserialize(stream, out _))
{
coinbaseQueue[index++] = coinbase;
Expand All @@ -118,10 +118,10 @@ private void Init()

private void WriteCoinbaseToDisk()
{
var stream = new FastStream(coinbaseQueue.Length * 250);
FastStream stream = new(coinbaseQueue.Length * 250);
stream.Write(i1);
stream.Write(i2);
foreach (var item in coinbaseQueue)
foreach (ITransaction item in coinbaseQueue)
{
if (item is null)
{
Expand Down Expand Up @@ -164,10 +164,10 @@ private void UpdateCoinbase(ITransaction coinbase)

private void WriteDbToDisk()
{
var stream = new FastStream(database.Count * 90);
foreach (var item in database)
FastStream stream = new(database.Count * 90);
foreach (KeyValuePair<byte[], List<Utxo>> item in database)
{
foreach (var item2 in item.Value)
foreach (Utxo item2 in item.Value)
{
stream.Write(item.Key);
item2.Serialize(stream);
Expand All @@ -182,7 +182,7 @@ public IUtxo Find(TxIn tin)
{
if (database.TryGetValue(tin.TxHash, out List<Utxo> value))
{
var index = value.FindIndex(x => x.Index == tin.Index);
int index = value.FindIndex(x => x.Index == tin.Index);
return index < 0 ? null : value[index];
}
else
Expand Down Expand Up @@ -238,7 +238,7 @@ public void Undo(ITransaction[] txs, int lastIndex)

for (int i = 1; i <= lastIndex; i++)
{
foreach (var item in txs[i].TxInList)
foreach (TxIn item in txs[i].TxInList)
{
IUtxo utxo = Find(item);
if (utxo is not null)
Expand Down
2 changes: 1 addition & 1 deletion Src/Denovo/Services/WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class WindowManager : IWindowManager
{
public Task ShowDialog(VmWithSizeBase vm)
{
Window win = new Window()
Window win = new()
{
Content = vm,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Expand Down
2 changes: 1 addition & 1 deletion Src/Denovo/ViewModels/AboutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static void ShellExec(string cmd, bool waitForExit = true)
{
string escapedArgs = cmd.Replace("\"", "\\\"");

using var process = Process.Start(
using Process process = Process.Start(
new ProcessStartInfo
{
FileName = "/bin/sh",
Expand Down
16 changes: 8 additions & 8 deletions Src/Denovo/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ private void Init(Configuration config)
FileMan.SetBlockPath(config.BlockchainPath);

AllNodes = new NodePool(config.MaxConnectionCount);
var utxoDb = new UtxoDatabase(FileMan);
var memPool = new MemoryPool();
UtxoDatabase utxoDb = new(FileMan);
MemoryPool memPool = new();
clientSettings =
new FullClientSettings(
config.AcceptIncoming,
Expand Down Expand Up @@ -112,7 +112,7 @@ public async void OpenConfig()
config = new Configuration(Network) { IsDefault = true };
}

var vm = new ConfigurationViewModel(config);
ConfigurationViewModel vm = new(config);
await WinMan.ShowDialog(vm);

if (vm.IsChanged)
Expand All @@ -129,32 +129,32 @@ public async void OpenConfig()
public async void OpenMiner()
{
// TODO: A better way is to make the following VM disposable
var vm = new MinerViewModel();
MinerViewModel vm = new();
await WinMan.ShowDialog(vm);
vm.StopMining();
}

public async void OpenEcies()
{
var vm = new EciesViewModel();
EciesViewModel vm = new();
await WinMan.ShowDialog(vm);
}

public async void OpenVerifyTx()
{
var vm = new VerifyTxViewModel();
VerifyTxViewModel vm = new();
await WinMan.ShowDialog(vm);
}

public async void OpenWifHelper()
{
var vm = new WifHelperViewModel();
WifHelperViewModel vm = new();
await WinMan.ShowDialog(vm);
}

public async void OpenPushTx()
{
var vm = new PushTxViewModel();
PushTxViewModel vm = new();
await WinMan.ShowDialog(vm);
vm.Dispose();
}
Expand Down
8 changes: 4 additions & 4 deletions Src/Denovo/ViewModels/MinerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MinerViewModel : VmWithSizeBase
public MinerViewModel() : base(650, 650)
{
AllNodes = new NodePool(5);
var clientSettings = new MinimalClientSettings(NetworkType.TestNet, 5, AllNodes)
MinimalClientSettings clientSettings = new(NetworkType.TestNet, 5, AllNodes)
{
DnsSeeds = Configuration.DnsTest,
UserAgent = "/Satoshi:0.22.0/",
Expand Down Expand Up @@ -92,7 +92,7 @@ public async void StartMining()
tokenSource?.Dispose();
tokenSource = null;

var prvBlock = new Block();
Block prvBlock = new();
if (!Base16.TryDecode(PreviousBlockHex, out byte[] data))
{
BlockHex = "Invalid hex.";
Expand All @@ -111,11 +111,11 @@ public async void StartMining()

if (!(result is null))
{
var stream = new FastStream();
FastStream stream = new();
result.Serialize(stream);
BlockHex = stream.ToByteArray().ToBase16();

var msg = new Message(new BlockPayload(result), NetworkType.TestNet);
Message msg = new(new BlockPayload(result), NetworkType.TestNet);

client.Send(msg);
}
Expand Down
2 changes: 1 addition & 1 deletion Src/Denovo/ViewModels/VerifyTxViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private set
public BindableCommand VerifyCommand { get; private set; }
private void Verify()
{
var temp = new Utxo[UtxoList.Length];
Utxo[] temp = new Utxo[UtxoList.Length];
for (int i = 0; i < UtxoList.Length; i++)
{
if (!UtxoList[i].TryConvertToUtxo(out Utxo u, out string error))
Expand Down

0 comments on commit 0b16530

Please sign in to comment.