Skip to content

Commit 87cc72b

Browse files
Merge pull request #50 from 317787106/hotfix/verify_data
hotfix: fix the bug of triggerProcessLoop
2 parents d6642da + 9fcb0b6 commit 87cc72b

21 files changed

Lines changed: 530 additions & 325 deletions

File tree

README.md

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
# Tron eventsubscribe plugin
1+
# Tron event subscribe plugin
22

3-
This is an implementation of Tron eventsubscribe model.
3+
This is an implementation of Tron event subscribe model.
44

55
* **api** module defines IPluginEventListener, a protocol between Java-tron and event plugin.
66
* **app** module is an example for loading plugin, developers could use it for debugging.
77
* **kafkaplugin** module is the implementation for kafka, it implements IPluginEventListener, it receives events subscribed from Java-tron and relay events to kafka server.
88
* **mongodbplugin** mongodbplugin module is the implementation for mongodb.
99
### Setup/Build
10-
Eventplugin can be built with JDK 8 or JDK17.
10+
Event-plugin can be built with JDK 8 or JDK 17.
1111
1. Clone the repo
1212
2. Go to eventplugin `cd eventplugin`
1313
3. run `./gradlew build`
1414

1515
* This will produce plugin zips, named `plugin-kafka-1.0.0.zip` and `plugin-mongodb-1.0.0.zip`, located in the `eventplugin/build/plugins/` directory.
1616

1717

18-
### Edit **config.conf** of Java-tron, add the following fileds:
18+
### Edit **config.conf** of Java-tron, add the following fields:
1919
```
2020
event.subscribe = {
2121
path = "" // absolute path of plugin
2222
server = "" // target server address to receive event triggers
23-
# dbname|username|password, if you want to create indexes for collections when the collections
24-
# are not exist, you can add version and set it to 2, as dbname|username|password|version
25-
# if you use version 2 and one collection not exists, it will create index automaticaly;
26-
# if you use version 2 and one collection exists, it will not create index, you must create index manually;
23+
# dbname|username|password or dbname|username|password|version
24+
# If you use version 2 and one collection not exists, it will create index automatically;
25+
# In any other case, it will not create index, you must create index manually
2726
dbconfig = ""
2827
topics = [
2928
{
@@ -99,13 +98,14 @@ event.subscribe = {
9998
remove comment and set listeners=PLAINTEXT://:9092
10099
remove comment and set advertised.listeners to PLAINTEXT://host_ip:9092
101100

101+
### How to use kafka plugin
102102
##### Install Kafka
103-
**On Mac**:
103+
*On Mac*:
104104
```
105105
brew install kafka
106106
```
107107

108-
**On Linux**:
108+
*On Linux*:
109109
```
110110
cd /usr/local
111111
wget http://archive.apache.org/dist/kafka/0.10.2.2/kafka_2.10-0.10.2.2.tgz
@@ -119,12 +119,12 @@ source /etc/profile
119119
**Note**: make sure the version of Kafka is the same as the version set in build.gradle of eventplugin project.(kafka_2.10-0.10.2.2 kafka)
120120

121121
##### Run Kafka
122-
**On Mac**:
122+
*On Mac*:
123123
```
124124
zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties & kafka-server-start /usr/local/etc/kafka/server.properties
125125
```
126126

127-
**On Linux**:
127+
*On Linux*:
128128
```
129129
zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties &
130130
Sleep about 3 seconds
@@ -133,7 +133,7 @@ kafka-server-start.sh /usr/local/kafka/config/server.properties &
133133

134134
#### Create topics to receive events, the topic is defined in config.conf
135135

136-
**On Mac**:
136+
*On Mac*:
137137
```
138138
kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic block
139139
kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic transaction
@@ -144,7 +144,7 @@ kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partit
144144
kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic soliditylog
145145
```
146146

147-
**On Linux**:
147+
*On Linux*:
148148
```
149149
kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic block
150150
kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic transaction
@@ -157,7 +157,7 @@ kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --par
157157

158158
#### Kafka consumer
159159

160-
**On Mac**:
160+
*On Mac*:
161161
```
162162
kafka-console-consumer --bootstrap-server localhost:9092 --topic block
163163
kafka-console-consumer --bootstrap-server localhost:9092 --topic transaction
@@ -168,7 +168,7 @@ kafka-console-consumer --bootstrap-server localhost:9092 --topic solidityevent
168168
kafka-console-consumer --bootstrap-server localhost:9092 --topic soliditylog
169169
```
170170

171-
**On Linux**:
171+
*On Linux*:
172172
```
173173
kafka-console-consumer.sh --zookeeper localhost:2181 --topic block
174174
kafka-console-consumer.sh --zookeeper localhost:2181 --topic transaction
@@ -179,6 +179,29 @@ kafka-console-consumer.sh --zookeeper localhost:2181 --topic solidityevent
179179
kafka-console-consumer.sh --zookeeper localhost:2181 --topic soliditylog
180180
```
181181

182+
See more details on [developers](https://developers.tron.network/docs/event-plugin-deployment-kafka).
183+
184+
### How to use MongoDB plugin
185+
These are default indexes when build automatically:
186+
```
187+
db.block.createIndex({ blockNumber: 1 },{ name: "blockNumber",unique: true});
188+
189+
db.transaction.createIndex({ transactionId: 1 },{ name: "transactionId",unique: true });
190+
191+
db.solidity.createIndex({ latestSolidifiedBlockNumber: 1 },{ name: "latestSolidifiedBlockNumber",unique: true });
192+
193+
db.solidityevent.createIndex({ uniqueId: 1 },{ name: "uniqueId",unique: true });
194+
195+
db.contractevent.createIndex({ uniqueId: 1 },{ name: "uniqueId",unique: true });
196+
197+
db.soliditylog.createIndex({ uniqueId: 1 },{ name: "uniqueId",unique: true });
198+
db.soliditylog.createIndex({ contractAddress: 1 },{ name: "contractAddress" });
199+
200+
db.contractlog.createIndex({ uniqueId: 1 },{ name: "uniqueId",unique: true });
201+
db.contractlog.createIndex({ contractAddress: 1 },{ name: "contractAddress" });
202+
```
203+
You can also create other indexes as necessary. See more details on [developers](https://developers.tron.network/docs/event-plugin-deployment-mongodb).
204+
182205
### Load plugin in Java-tron
183206
* add --es to command line, for example:
184207
```

api/src/main/java/org/tron/common/logsfilter/IPluginEventListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public interface IPluginEventListener extends ExtensionPoint {
1313
// start should be called after setServerAddress, setTopic, setDBConfig
1414
void start();
1515

16+
void stop();
17+
1618
void handleBlockEvent(Object data);
1719

1820
void handleTransactionTrigger(Object data);
Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.tron.common.logsfilter.trigger;
22

33
import java.util.ArrayList;
4-
import java.util.HashMap;
54
import java.util.List;
6-
import java.util.Map;
75
import lombok.Getter;
86
import lombok.Setter;
97

@@ -23,22 +21,11 @@ public class BlockLogTrigger extends Trigger {
2321

2422
@Getter
2523
@Setter
26-
private List<String> transactionList = new ArrayList<>();
27-
28-
/**
29-
* address of witness
30-
*/
31-
@Getter
32-
@Setter
33-
private String witnessAddress;
24+
private long latestSolidifiedBlockNumber;
3425

3526
@Getter
3627
@Setter
37-
private long witnessPayPerBlock;
38-
39-
@Getter
40-
@Setter
41-
Map<String, Long> witnessMap = new HashMap<>();
28+
private List<String> transactionList = new ArrayList<>();
4229

4330
public BlockLogTrigger() {
4431
setTriggerName(Trigger.BLOCK_TRIGGER_NAME);
@@ -51,9 +38,7 @@ public String toString() {
5138
+ ", blockNumber: " + blockNumber
5239
+ ", blockhash: " + blockHash
5340
+ ", transactionSize: " + transactionSize
54-
+ ", transactionList: " + transactionList
55-
+ ", witnessAddress: " + witnessAddress
56-
+ ", witnessPayPerBlock: " + witnessPayPerBlock
57-
+ ", witnessMap: " + witnessMap;
41+
+ ", latestSolidifiedBlockNumber: " + latestSolidifiedBlockNumber
42+
+ ", transactionList: " + transactionList;
5843
}
5944
}

app/build.gradle

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,38 @@ apply plugin: 'application'
33
mainClassName = 'org.tron.eventplugin.app.PluginLauncher'
44

55
dependencies {
6-
implementation project(':api')
7-
implementation (group: 'org.pf4j', name: 'pf4j', version: "${pf4jVersion}") {
8-
exclude group: "org.slf4j"
9-
}
10-
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.5'
11-
implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
12-
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.18.3'
13-
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.18.3'
14-
implementation "com.madgag.spongycastle:core:1.58.0.0"
15-
implementation "com.madgag.spongycastle:prov:1.58.0.0"
6+
implementation project(':api')
7+
8+
implementation(group: 'org.pf4j', name: 'pf4j', version: "${pf4jVersion}")
9+
implementation 'org.apache.commons:commons-lang3:3.5'
10+
implementation 'org.slf4j:slf4j-api:2.0.9'
11+
implementation 'ch.qos.logback:logback-classic:1.3.14'
12+
implementation 'com.fasterxml.jackson.core:jackson-databind:2.18.3'
13+
implementation 'com.fasterxml.jackson.core:jackson-core:2.18.3'
14+
implementation "com.madgag.spongycastle:core:1.58.0.0"
15+
implementation "com.madgag.spongycastle:prov:1.58.0.0"
16+
implementation 'com.alibaba:fastjson:1.2.83'
1617
}
1718

1819
task uberjar(type: Jar, dependsOn: ['compileJava']) {
19-
zip64 true
20-
from configurations.runtimeClasspath.asFileTree.files.collect {
20+
zip64 true
21+
22+
from {
23+
configurations.runtimeClasspath.collect { file ->
24+
file.isDirectory() ? file : zipTree(file)
25+
}
26+
}
27+
2128
exclude "META-INF/*.SF"
2229
exclude "META-INF/*.DSA"
2330
exclude "META-INF/*.RSA"
24-
zipTree(it)
25-
}
26-
from files(sourceSets.main.output.classesDirs)
27-
from files(sourceSets.main.resources)
28-
manifest {
29-
attributes 'Main-Class': mainClassName
30-
}
31-
32-
baseName = "${project.name}-plugin-demo"
33-
classifier = "uberjar"
34-
}
3531

32+
from sourceSets.main.output
33+
34+
manifest {
35+
attributes 'Main-Class': mainClassName
36+
}
37+
38+
archiveBaseName.set("${project.name}-plugin-demo")
39+
archiveClassifier.set("uberjar")
40+
}

app/src/main/java/org/tron/eventplugin/app/PluginLauncher.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@
1616

1717
package org.tron.eventplugin.app;
1818

19+
import com.fasterxml.jackson.core.JsonProcessingException;
1920
import com.fasterxml.jackson.databind.ObjectMapper;
2021
import java.io.File;
2122
import java.util.List;
2223
import java.util.Objects;
24+
import java.util.Random;
25+
import lombok.extern.slf4j.Slf4j;
2326
import org.pf4j.CompoundPluginDescriptorFinder;
2427
import org.pf4j.DefaultPluginManager;
2528
import org.pf4j.ManifestPluginDescriptorFinder;
2629
import org.pf4j.PluginManager;
27-
import org.slf4j.Logger;
28-
import org.slf4j.LoggerFactory;
2930
import org.tron.common.logsfilter.IPluginEventListener;
3031
import org.tron.common.logsfilter.trigger.BlockLogTrigger;
3132
import org.tron.common.logsfilter.trigger.Trigger;
3233

34+
@Slf4j
3335
public class PluginLauncher {
3436

35-
private static final Logger logger = LoggerFactory.getLogger(PluginLauncher.class);
36-
3737
public static void main(String[] args) {
3838
String path = "/Users/tron/sourcecode/eventplugin/build/plugins/plugin-mongodb-1.0.0.zip";
3939

@@ -55,6 +55,7 @@ protected CompoundPluginDescriptorFinder createPluginDescriptorFinder() {
5555
List<IPluginEventListener> eventListeners;
5656
eventListeners = pluginManager.getExtensions(IPluginEventListener.class);
5757

58+
log.info("start plugin...");
5859
if (Objects.isNull(eventListeners)) {
5960
return;
6061
}
@@ -80,30 +81,33 @@ protected CompoundPluginDescriptorFinder createPluginDescriptorFinder() {
8081
eventListeners.forEach(IPluginEventListener::start);
8182

8283
ObjectMapper objectMapper = new ObjectMapper();
83-
for (int index = 0; index < 1000; ++index) {
84+
for (int index = 0; index < 2; ++index) {
8485
BlockLogTrigger trigger = new BlockLogTrigger();
85-
trigger.setBlockNumber(index);
86-
trigger.setBlockHash("000000000002f5834df6036318999576bfa23ff1a57e0538fa87d5a90319659e");
86+
trigger.setBlockNumber(new Random().nextInt(10000)); //blockNumber is unique key
87+
trigger.setBlockHash("000000000002f5834df6036318999576bfa23ff1a57e0538fa87d5a90319659f");
8788
trigger.setTimeStamp(System.currentTimeMillis());
8889
trigger.setTransactionSize(100);
8990

91+
String triggerData;
92+
try {
93+
triggerData = objectMapper.writeValueAsString(trigger);//convert to json
94+
} catch (JsonProcessingException e) {
95+
log.error("", e);
96+
continue;
97+
}
9098
eventListeners.forEach(listener -> {
91-
try {
92-
listener.handleBlockEvent(objectMapper.writeValueAsString(trigger));
93-
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
94-
e.printStackTrace();
95-
}
99+
listener.handleBlockEvent(triggerData);
96100
});
97101
}
98102

99-
while (true) {
100-
try {
101-
Thread.sleep(1000);
102-
} catch (InterruptedException e) {
103-
e.printStackTrace();
104-
}
103+
try {
104+
Thread.sleep(10_000);
105+
} catch (InterruptedException e) {
106+
//ignore
105107
}
108+
log.info("try to close plugin...");
106109

107-
//pluginManager.stopPlugins();
110+
// will invoke stop method of KafkaLogFilterPlugin or MongodbLogFilterPlugin
111+
pluginManager.stopPlugins();
108112
}
109113
}

app/src/main/resources/log4j.properties

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/src/main/resources/logback.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<configuration>
4+
5+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
6+
<encoder>
7+
<pattern>
8+
%d{HH:mm:ss.SSS} %-5level [%t] [%c{1}]\(%F:%L\) %m%n
9+
</pattern>
10+
</encoder>
11+
</appender>
12+
13+
<root level="INFO">
14+
<appender-ref ref="CONSOLE"/>
15+
</root>
16+
17+
<logger name="org.pf4j" level="DEBUG"/>
18+
</configuration>

0 commit comments

Comments
 (0)