FrmMain: Load and save current status.

This commit is contained in:
2020-09-07 00:48:03 +02:00
parent 127add53c0
commit 1f7822e6a6
2 changed files with 150 additions and 97 deletions

View File

@@ -33,17 +33,17 @@
this.lsbPendingTransactions = new System.Windows.Forms.ListBox(); this.lsbPendingTransactions = new System.Windows.Forms.ListBox();
this.btnMine = new System.Windows.Forms.Button(); this.btnMine = new System.Windows.Forms.Button();
this.grpAddTransaction = new System.Windows.Forms.GroupBox(); this.grpAddTransaction = new System.Windows.Forms.GroupBox();
this.btnAdd = new System.Windows.Forms.Button();
this.lblFrom = new System.Windows.Forms.Label();
this.lblTo = new System.Windows.Forms.Label();
this.lblAmount = new System.Windows.Forms.Label();
this.txtMinerName = new System.Windows.Forms.TextBox();
this.txtFrom = new System.Windows.Forms.TextBox();
this.txtTo = new System.Windows.Forms.TextBox();
this.numAmount = new System.Windows.Forms.NumericUpDown();
this.btnClearFrom = new System.Windows.Forms.Button();
this.btnClearTo = new System.Windows.Forms.Button();
this.btnClearAmount = new System.Windows.Forms.Button(); this.btnClearAmount = new System.Windows.Forms.Button();
this.btnClearTo = new System.Windows.Forms.Button();
this.btnClearFrom = new System.Windows.Forms.Button();
this.numAmount = new System.Windows.Forms.NumericUpDown();
this.txtTo = new System.Windows.Forms.TextBox();
this.txtFrom = new System.Windows.Forms.TextBox();
this.lblAmount = new System.Windows.Forms.Label();
this.lblTo = new System.Windows.Forms.Label();
this.lblFrom = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.txtMinerName = new System.Windows.Forms.TextBox();
this.lsbUsers = new System.Windows.Forms.ListBox(); this.lsbUsers = new System.Windows.Forms.ListBox();
this.grpAddTransaction.SuspendLayout(); this.grpAddTransaction.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.numAmount)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.numAmount)).BeginInit();
@@ -96,63 +96,35 @@
this.grpAddTransaction.TabStop = false; this.grpAddTransaction.TabStop = false;
this.grpAddTransaction.Text = "Add transaction"; this.grpAddTransaction.Text = "Add transaction";
// //
// btnAdd // btnClearAmount
// //
this.btnAdd.Location = new System.Drawing.Point(94, 97); this.btnClearAmount.Location = new System.Drawing.Point(148, 71);
this.btnAdd.Name = "btnAdd"; this.btnClearAmount.Name = "btnClearAmount";
this.btnAdd.Size = new System.Drawing.Size(75, 23); this.btnClearAmount.Size = new System.Drawing.Size(21, 20);
this.btnAdd.TabIndex = 0; this.btnClearAmount.TabIndex = 10;
this.btnAdd.Text = "Add"; this.btnClearAmount.Text = "X";
this.btnAdd.UseVisualStyleBackColor = true; this.btnClearAmount.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click); this.btnClearAmount.Click += new System.EventHandler(this.btnClearAmount_Click);
// //
// lblFrom // btnClearTo
// //
this.lblFrom.AutoSize = true; this.btnClearTo.Location = new System.Drawing.Point(148, 45);
this.lblFrom.Location = new System.Drawing.Point(6, 22); this.btnClearTo.Name = "btnClearTo";
this.lblFrom.Name = "lblFrom"; this.btnClearTo.Size = new System.Drawing.Size(21, 20);
this.lblFrom.Size = new System.Drawing.Size(30, 13); this.btnClearTo.TabIndex = 9;
this.lblFrom.TabIndex = 1; this.btnClearTo.Text = "X";
this.lblFrom.Text = "From"; this.btnClearTo.UseVisualStyleBackColor = true;
this.btnClearTo.Click += new System.EventHandler(this.btnClearTo_Click);
// //
// lblTo // btnClearFrom
// //
this.lblTo.AutoSize = true; this.btnClearFrom.Location = new System.Drawing.Point(148, 19);
this.lblTo.Location = new System.Drawing.Point(6, 48); this.btnClearFrom.Name = "btnClearFrom";
this.lblTo.Name = "lblTo"; this.btnClearFrom.Size = new System.Drawing.Size(21, 20);
this.lblTo.Size = new System.Drawing.Size(20, 13); this.btnClearFrom.TabIndex = 8;
this.lblTo.TabIndex = 2; this.btnClearFrom.Text = "X";
this.lblTo.Text = "To"; this.btnClearFrom.UseVisualStyleBackColor = true;
// this.btnClearFrom.Click += new System.EventHandler(this.btnClearFrom_Click);
// lblAmount
//
this.lblAmount.AutoSize = true;
this.lblAmount.Location = new System.Drawing.Point(6, 74);
this.lblAmount.Name = "lblAmount";
this.lblAmount.Size = new System.Drawing.Size(43, 13);
this.lblAmount.TabIndex = 3;
this.lblAmount.Text = "Amount";
//
// txtMinerName
//
this.txtMinerName.Location = new System.Drawing.Point(69, 103);
this.txtMinerName.Name = "txtMinerName";
this.txtMinerName.Size = new System.Drawing.Size(87, 20);
this.txtMinerName.TabIndex = 4;
//
// txtFrom
//
this.txtFrom.Location = new System.Drawing.Point(47, 19);
this.txtFrom.Name = "txtFrom";
this.txtFrom.Size = new System.Drawing.Size(95, 20);
this.txtFrom.TabIndex = 4;
//
// txtTo
//
this.txtTo.Location = new System.Drawing.Point(47, 45);
this.txtTo.Name = "txtTo";
this.txtTo.Size = new System.Drawing.Size(95, 20);
this.txtTo.TabIndex = 5;
// //
// numAmount // numAmount
// //
@@ -166,35 +138,63 @@
this.numAmount.Size = new System.Drawing.Size(95, 20); this.numAmount.Size = new System.Drawing.Size(95, 20);
this.numAmount.TabIndex = 7; this.numAmount.TabIndex = 7;
// //
// btnClearFrom // txtTo
// //
this.btnClearFrom.Location = new System.Drawing.Point(148, 19); this.txtTo.Location = new System.Drawing.Point(47, 45);
this.btnClearFrom.Name = "btnClearFrom"; this.txtTo.Name = "txtTo";
this.btnClearFrom.Size = new System.Drawing.Size(21, 20); this.txtTo.Size = new System.Drawing.Size(95, 20);
this.btnClearFrom.TabIndex = 8; this.txtTo.TabIndex = 5;
this.btnClearFrom.Text = "X";
this.btnClearFrom.UseVisualStyleBackColor = true;
this.btnClearFrom.Click += new System.EventHandler(this.btnClearFrom_Click);
// //
// btnClearTo // txtFrom
// //
this.btnClearTo.Location = new System.Drawing.Point(148, 45); this.txtFrom.Location = new System.Drawing.Point(47, 19);
this.btnClearTo.Name = "btnClearTo"; this.txtFrom.Name = "txtFrom";
this.btnClearTo.Size = new System.Drawing.Size(21, 20); this.txtFrom.Size = new System.Drawing.Size(95, 20);
this.btnClearTo.TabIndex = 9; this.txtFrom.TabIndex = 4;
this.btnClearTo.Text = "X";
this.btnClearTo.UseVisualStyleBackColor = true;
this.btnClearTo.Click += new System.EventHandler(this.btnClearTo_Click);
// //
// btnClearAmount // lblAmount
// //
this.btnClearAmount.Location = new System.Drawing.Point(148, 71); this.lblAmount.AutoSize = true;
this.btnClearAmount.Name = "btnClearAmount"; this.lblAmount.Location = new System.Drawing.Point(6, 74);
this.btnClearAmount.Size = new System.Drawing.Size(21, 20); this.lblAmount.Name = "lblAmount";
this.btnClearAmount.TabIndex = 10; this.lblAmount.Size = new System.Drawing.Size(43, 13);
this.btnClearAmount.Text = "X"; this.lblAmount.TabIndex = 3;
this.btnClearAmount.UseVisualStyleBackColor = true; this.lblAmount.Text = "Amount";
this.btnClearAmount.Click += new System.EventHandler(this.btnClearAmount_Click); //
// lblTo
//
this.lblTo.AutoSize = true;
this.lblTo.Location = new System.Drawing.Point(6, 48);
this.lblTo.Name = "lblTo";
this.lblTo.Size = new System.Drawing.Size(20, 13);
this.lblTo.TabIndex = 2;
this.lblTo.Text = "To";
//
// lblFrom
//
this.lblFrom.AutoSize = true;
this.lblFrom.Location = new System.Drawing.Point(6, 22);
this.lblFrom.Name = "lblFrom";
this.lblFrom.Size = new System.Drawing.Size(30, 13);
this.lblFrom.TabIndex = 1;
this.lblFrom.Text = "From";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(94, 97);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 23);
this.btnAdd.TabIndex = 0;
this.btnAdd.Text = "Add";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// txtMinerName
//
this.txtMinerName.Location = new System.Drawing.Point(69, 103);
this.txtMinerName.Name = "txtMinerName";
this.txtMinerName.Size = new System.Drawing.Size(87, 20);
this.txtMinerName.TabIndex = 4;
// //
// lsbUsers // lsbUsers
// //
@@ -219,6 +219,7 @@
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "FrmMain"; this.Name = "FrmMain";
this.Text = "BasicBlockChain"; this.Text = "BasicBlockChain";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmMain_FormClosing);
this.grpAddTransaction.ResumeLayout(false); this.grpAddTransaction.ResumeLayout(false);
this.grpAddTransaction.PerformLayout(); this.grpAddTransaction.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.numAmount)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.numAmount)).EndInit();

View File

@@ -1,6 +1,8 @@
using System; using System;
using System.IO;
using System.Windows.Forms; using System.Windows.Forms;
using BasicBlockChain.Core; using BasicBlockChain.Core;
using VAR.Json;
namespace BasicBlockChain namespace BasicBlockChain
{ {
@@ -11,7 +13,7 @@ namespace BasicBlockChain
public FrmMain() public FrmMain()
{ {
InitializeComponent(); InitializeComponent();
_nullCoin = new BlockChain(genesisDate: new DateTime(2000, 1, 1), difficulty: 2); InitBlockChain();
} }
private void btnAdd_Click(object sender, EventArgs e) private void btnAdd_Click(object sender, EventArgs e)
@@ -47,6 +49,26 @@ namespace BasicBlockChain
numAmount.Value = 0; numAmount.Value = 0;
} }
private void lsbUsers_MouseDoubleClick(object sender, MouseEventArgs e)
{
Wallet wallet = (Wallet)lsbUsers.SelectedItem;
if (wallet == null) { return; }
if (string.IsNullOrEmpty(txtFrom.Text))
{
txtFrom.Text = wallet.User;
return;
}
if (string.IsNullOrEmpty(txtTo.Text))
{
txtTo.Text = wallet.User;
return;
}
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
SaveBlockChain();
}
private void Lists_Update() private void Lists_Update()
{ {
lsbTransactions.Items.Clear(); lsbTransactions.Items.Clear();
@@ -88,20 +110,50 @@ namespace BasicBlockChain
return string.Format("{0} - {1} - {2} - {3}", transaction.Date, transaction.Sender, transaction.Receiver, transaction.MicroCoinAmount); return string.Format("{0} - {1} - {2} - {3}", transaction.Date, transaction.Sender, transaction.Receiver, transaction.MicroCoinAmount);
} }
private void lsbUsers_MouseDoubleClick(object sender, MouseEventArgs e) private const string BlockChainFile = "BlockChain.json";
private void InitBlockChain()
{ {
Wallet wallet = (Wallet)lsbUsers.SelectedItem; FrmMainBlockChain blockChain = null;
if (wallet == null) { return; } if (File.Exists(BlockChainFile))
if (string.IsNullOrEmpty(txtFrom.Text))
{ {
txtFrom.Text = wallet.User; string contentBlockChainFile = File.ReadAllText(BlockChainFile);
return; blockChain = JsonParser.ParseText(contentBlockChainFile, typeof(FrmMainBlockChain), typeof(BlockChain), typeof(Block), typeof(Transaction)) as FrmMainBlockChain;
} }
if (string.IsNullOrEmpty(txtTo.Text))
if (blockChain != null)
{ {
txtTo.Text = wallet.User; _nullCoin = blockChain.BlockChain;
return; txtMinerName.Text = blockChain.Miner;
} }
else
{
_nullCoin = new BlockChain(genesisDate: new DateTime(2000, 1, 1), difficulty: 2);
txtMinerName.Text = string.Empty;
}
Lists_Update();
} }
private void SaveBlockChain()
{
if (File.Exists(BlockChainFile))
{
File.Delete(BlockChainFile);
}
FrmMainBlockChain blockChain = new FrmMainBlockChain
{
BlockChain = _nullCoin,
Miner = txtMinerName.Text,
};
string strBlockChain = JsonWriter.WriteObject(blockChain, indent: true);
File.WriteAllText(BlockChainFile, strBlockChain);
}
public class FrmMainBlockChain
{
public BlockChain BlockChain { get; set; }
public string Miner { get; set; }
};
} }
} }