-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntryItem.cs
103 lines (91 loc) · 3.13 KB
/
EntryItem.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
using System;
using FactomSharp.Factomd;
using FactomSharp.Factomd.API;
namespace FactomSharp
{
public class EntryItem
{
public enum State
{
Exception = -100,
CommitFail = -10,
RevealFail = -20,
Queued = 0,
CommitOK = 10,
RevealOK = 20,
TransactionACK = 30,
DBlockConfirmed = 40,
Complete = 50,
CompleteConfirmed = 60
}
public event EventHandler Complete;
public event EventHandler<State> StatusChange;
Action<EntryItem> Process;
public string TxId {get; set;}
public string EntryHash {get; set;}
public Chain.BlockingMode Blocking {get; set;}
public Chain Chain {get; set;}
public Factomd.API.CommitEntry Commit {get; set;}
public Factomd.API.RevealEntry Reveal {get; set;}
public APIError ApiError {get; set;}
State _state = State.Queued;
public State Status
{ get
{
return _state;
}
set
{
if (_state != value)
{
_state = value;
StatusChange?.Invoke(this, _state);
Chain.SetQueueItemStatusChange(this,_state);
if (_state == State.DBlockConfirmed) Complete?.Invoke(this,null);
}
}
}
public EntryItem(Chain chain, Action<EntryItem> action, Chain.BlockingMode blocking = Chain.BlockingMode.Request)
{
Process = action;
Blocking = blocking;
Chain = chain;
}
public void Run()
{
try
{
var lastStatus = Status;
if (Status == State.Queued)
{
Process.Invoke(this);
}
else if (Status >= State.RevealOK || Status < State.DBlockConfirmed)
{
var ack = new Ack(Chain.FactomD);
switch (ack.CheckReveal(Chain.ChainID,EntryHash))
{
case Ack.Status.DBlockConfirmed:
Status = State.DBlockConfirmed;
break;
case Ack.Status.TransactionACK:
Status = State.TransactionACK;
break;
case Ack.Status.NotConfirmed:
case Ack.Status.RequestFailed:
case Ack.Status.Unknown:
break;
}
}
else
{
//retry?
}
}catch (Exception ex)
{
ApiError = new APIError(ex);
Status = State.Exception;
}
}
}
}