-
Notifications
You must be signed in to change notification settings - Fork 1
/
TransactionLoggerExample.java
69 lines (59 loc) · 2.5 KB
/
TransactionLoggerExample.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
import com.satergo.ergonnection.ErgoSocket;
import com.satergo.ergonnection.Version;
import com.satergo.ergonnection.messages.Inv;
import com.satergo.ergonnection.messages.ModifierRequest;
import com.satergo.ergonnection.messages.ModifierResponse;
import com.satergo.ergonnection.modifiers.ErgoTransaction;
import com.satergo.ergonnection.protocol.ProtocolMessage;
import com.satergo.ergonnection.records.Peer;
import com.satergo.ergonnection.records.RawModifier;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
public class TransactionLoggerExample {
public TransactionLoggerExample() throws IOException {
ErgoSocket ergoSocket = new ErgoSocket(
new InetSocketAddress("127.0.0.1", 9030),
new Peer("ergoref", "ergo-mainnet-5.0.12", Version.parse("5.0.12"), ErgoSocket.BASIC_FEATURE_SET));
ergoSocket.sendHandshake();
ergoSocket.acceptHandshake();
ArrayList<byte[]> requestedIds = new ArrayList<>();
System.out.println("Peer info: " + ergoSocket.getPeerInfo());
while (true) {
ProtocolMessage msg = ergoSocket.acceptMessage();
if (msg instanceof Inv inv) {
if (inv.typeId() == ErgoTransaction.TYPE_ID) {
System.out.println("[" + hhmmss() + "] Received ID(s) of transaction(s) in Inv message: " + inv.elements().stream()
.map(HexFormat.of()::formatHex)
.collect(Collectors.joining(", ")));
// request the data of it or them
requestedIds.addAll(inv.elements());
ergoSocket.send(new ModifierRequest(ErgoTransaction.TYPE_ID, requestedIds));
}
} else if (msg instanceof ModifierResponse mr) {
for (RawModifier modifier : mr.rawModifiers()) {
if (modifier.typeId() != ErgoTransaction.TYPE_ID) continue;
// check requestedIds for rawModifier.id() then remove it from the list and print the deserialized transaction
for (Iterator<byte[]> iterator = requestedIds.iterator(); iterator.hasNext();) {
byte[] id = iterator.next();
if (Arrays.equals(id, modifier.id())) {
iterator.remove();
ErgoTransaction tx = ErgoTransaction.deserialize(modifier.id(), modifier.object());
System.out.println("[" + hhmmss() + "] Transaction: " + tx);
break;
}
}
}
}
}
}
public static void main(String[] args) throws IOException {
new TransactionLoggerExample();
}
private static String hhmmss() {
return LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
}
}