This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSplash.cs
219 lines (192 loc) · 9.46 KB
/
Splash.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace PlenteumWallet
{
public partial class Splash : PlenteumWalletForm
{
public string WalletPath { get; set; }
public string WalletPassword { get; set; }
protected override CreateParams CreateParams
{
get
{
const int CS_DROPSHADOW = 0x20000;
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
public Splash(string _wallet, string _password)
{
InitializeComponent();
WalletPath = _wallet;
WalletPassword = _password;
versionLabel.Text = "v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
this.Text = Application.ProductName;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
Utilities.CloseProgram(e);
}
private void Splash_Load(object sender, EventArgs e)
{
StatusLabel.Text = "Connecting to daemon ...";
splashBackgroundWorker.RunWorkerAsync();
}
private void SplashBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
int failcount = 0;
string globalLastline = "";
try
{
var connReturn = ConnectionManager.StartDaemon(WalletPath, WalletPassword);
if (connReturn.Item1 == false)
{
this.StatusLabel.BeginInvoke((MethodInvoker)delegate () { this.StatusLabel.Text = "Daemon Connection Failed: " + connReturn.Item2; });
if (System.IO.File.Exists("wallet-service.log"))
{
using (StreamReader sr = new StreamReader("wallet-service.log"))
{
string contents = sr.ReadToEnd();
if (contents.Contains("Internal error") ||
contents.Contains("Corruption: no meta-nextfile entry in descriptor"))
{
if (MessageBox.Show("Unfortunately, your blockchain has got corrupted. " +
"This can occur when you exit the GUI without saving or " +
"you lose power, among other reasons. You can fix this by " +
"resyncing your blockchain. Open a link on how to do this?",
"Plenteum Wallet",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
System.Diagnostics.Process.Start("https://github.com/plenteum/plenteum/wiki/Bootstrapping-the-Blockchain");
}
throw new Exception("The GUI is unusable until this issue is fixed. It will now close.");
}
}
}
System.Threading.Thread.Sleep(5000);
Environment.Exit(3);
}
while (true)
{
string lastLine = "";
bool lineWasUpdated = false;
if (System.IO.File.Exists("wallet-service.log"))
{
var fs = new FileStream("wallet-service.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (var sr = new StreamReader(fs))
{
while (!sr.EndOfStream)
{
string linecache = sr.ReadLine();
if (String.IsNullOrWhiteSpace(linecache))
break;
lastLine = linecache;
if (globalLastline == lastLine)
lineWasUpdated = false;
else
lineWasUpdated = true;
globalLastline = linecache;
if (lastLine.Contains("The password is wrong") || lastLine.Contains("Restored view public key doesn't correspond to secret key"))
{
try
{
connReturn.Item3.CancelOutputRead();
connReturn.Item3.Kill();
}
catch
{
}
MessageBox.Show("The password is incorrect!", "Plenteum Wallet");
//will restart at selection screen instead of exiting
Program.jumpBack = true;
break;
}
}
}
fs.Close();
}
try
{
if (lastLine.Contains("Imported block with index"))
{
string stdUpdate = lastLine.Split(new string[] { " " }, StringSplitOptions.None)[1];
this.StatusLabel.BeginInvoke((MethodInvoker)delegate () { this.StatusLabel.Text = stdUpdate; });
System.Threading.Thread.Sleep(3000);
continue;
}
if (Process.GetProcessesByName("wallet-service").Length < 1 || connReturn.Item3 == null)
{
throw new Exception("Daemon exited!");
}
var resp = ConnectionManager.Request("getStatus");
if (resp.Item1 == false)
{
throw new Exception("No RPC connection/Failed");
}
var block_count = (int)resp.Item3["blockCount"];
var known_block_count = (int)resp.Item3["knownBlockCount"];
if (known_block_count == 0)
{
this.StatusLabel.BeginInvoke((MethodInvoker)delegate () { this.StatusLabel.Text = "Waiting on known block count..." ; });
continue;
}
this.StatusLabel.BeginInvoke((MethodInvoker)delegate () { this.StatusLabel.Text = "Syncing... [" + block_count.ToString() + " / " + known_block_count.ToString() + "]"; });
if (known_block_count > 0 && (block_count >= known_block_count - 1))
{
this.StatusLabel.BeginInvoke((MethodInvoker)delegate () { this.StatusLabel.Text = "Wallet is synced, opening..."; });
e.Result = connReturn.Item3;
break;
}
}
catch (Exception ex)
{
if (Process.GetProcessesByName("wallet-service").Length < 1 || connReturn.Item3 == null)
{
throw new Exception("Daemon exited!");
}
if (lineWasUpdated == false)
{
failcount += 1;
if (failcount >= 100)
{
throw new Exception("MAXTRYERROR: " + ex.Message);
}
this.StatusLabel.BeginInvoke((MethodInvoker)delegate () { this.StatusLabel.Text = "Waiting on Daemon, trying..(" + failcount.ToString() + "/100)"; });
}
else
{
this.StatusLabel.BeginInvoke((MethodInvoker)delegate () { this.StatusLabel.Text = "Waiting on Daemon to start sync..."; });
}
}
}
}
catch (Exception ex)
{
this.StatusLabel.BeginInvoke((MethodInvoker)delegate () { this.StatusLabel.Text = "Daemon Connection Failed: " + ex.Message ; });
MessageBox.Show("Daemon Connection Failed: " + ex.Message, "Plenteum Wallet", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(3);
}
}
private void SplashBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void SplashBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (Program.jumpBack)
{
Utilities.Close(this);
}
else
{
Utilities.Hide(this);
var walletWindow = new Wallet(WalletPath, WalletPassword, (Process)e.Result);
walletWindow.Closed += (s, args) => Utilities.Close(this);
walletWindow.Show();
}
}
}
}