-
Notifications
You must be signed in to change notification settings - Fork 28
/
MonitorTool.java
136 lines (108 loc) · 3.42 KB
/
MonitorTool.java
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
package snowblossom.client;
import io.grpc.stub.StreamObserver;
import snowblossom.proto.AddressUpdate;
import snowblossom.lib.AddressSpecHash;
import snowblossom.lib.NetworkParams;
import snowblossom.proto.RequestAddress;
import snowblossom.proto.RequestTransaction;
import snowblossom.proto.HistoryList;
import snowblossom.proto.HistoryEntry;
import snowblossom.proto.TransactionHashList;
import snowblossom.proto.Transaction;
import snowblossom.proto.TransactionInner;
import snowblossom.proto.TransactionInput;
import snowblossom.proto.TransactionOutput;
import snowblossom.lib.TransactionUtil;
import com.google.protobuf.ByteString;
import java.util.HashSet;
/**
* For any addresses added via addAddress this will cause the MonitorInterface
* to be called for any transactions in or out of that address.
*
* Includes all historical transactions.
*/
public class MonitorTool implements StreamObserver<AddressUpdate>
{
private NetworkParams params;
private StubHolder stub_holder;
private MonitorInterface monitor_interface;
private HashSet<AddressSpecHash> addresses = new HashSet<>();
// ByteString of address spec hash and then tx hash.
private HashSet<ByteString> processed_tx = new HashSet<>();
public MonitorTool(NetworkParams params, StubHolder stub_holder, MonitorInterface monitor_interface)
{
this.params = params;
this.stub_holder = stub_holder;
this.monitor_interface = monitor_interface;
}
public void addAddress(AddressSpecHash hash)
{
synchronized(addresses)
{
if (addresses.contains(hash)) return;
addresses.add(hash);
}
stub_holder.getAsyncStub().subscribeAddressUpdates(
RequestAddress.newBuilder().setAddressSpecHash(hash.getBytes()).build(),
this);
}
private void triggerAddress(AddressSpecHash hash)
{
RequestAddress ra = RequestAddress.newBuilder().setAddressSpecHash(hash.getBytes()).build();
HistoryList hl = stub_holder.getBlockingStub().getAddressHistory(ra);
TransactionHashList ml = stub_holder.getBlockingStub().getMempoolTransactionList(ra);
for(HistoryEntry he : hl.getEntriesList())
{
ByteString tx_hash = he.getTxHash();
sendNotices(hash, tx_hash);
}
for(ByteString tx_hash : ml.getTxHashesList())
{
sendNotices(hash, tx_hash);
}
}
private void sendNotices(AddressSpecHash hash, ByteString tx_hash)
{
ByteString key = hash.getBytes().concat(tx_hash);
synchronized(processed_tx)
{
if (processed_tx.contains(key)) return;
}
Transaction tx = stub_holder.getBlockingStub().getTransaction( RequestTransaction.newBuilder().setTxHash( tx_hash ).build() );
TransactionInner inner = TransactionUtil.getInner(tx);
int idx = 0;
for (TransactionInput in : inner.getInputsList())
{
if (hash.getBytes().equals(in.getSpecHash()))
{
monitor_interface.onOutbound(tx, idx);
}
idx++;
}
idx=0;
for (TransactionOutput out : inner.getOutputsList())
{
if (hash.getBytes().equals(out.getRecipientSpecHash()))
{
monitor_interface.onInbound(tx, idx);
}
idx++;
}
synchronized(processed_tx)
{
processed_tx.add(key);
}
}
public void onNext(AddressUpdate update)
{
AddressSpecHash hash = new AddressSpecHash(update.getAddress());
triggerAddress(hash);
}
public void onCompleted()
{
}
public void onError(Throwable t)
{
t.printStackTrace();
}
}