Skip to content

Commit 3f0413d

Browse files
authored
Merge pull request #52 from tronprotocol/release_v2.2.0
Release v2.2.0
2 parents 0cdf454 + 51b084c commit 3f0413d

36 files changed

Lines changed: 1254 additions & 1284 deletions

README.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
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-
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-
dbconfig = "" // dbname|username|password, if you want to create indexes for collections when the collections are not exist, you can add version and set it to 2, as dbname|username|password|version
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
26+
dbconfig = ""
2427
topics = [
2528
{
2629
triggerName = "block" // block trigger, the value can't be modified
@@ -95,13 +98,14 @@ event.subscribe = {
9598
remove comment and set listeners=PLAINTEXT://:9092
9699
remove comment and set advertised.listeners to PLAINTEXT://host_ip:9092
97100

101+
### How to use kafka plugin
98102
##### Install Kafka
99-
**On Mac**:
103+
*On Mac*:
100104
```
101105
brew install kafka
102106
```
103107

104-
**On Linux**:
108+
*On Linux*:
105109
```
106110
cd /usr/local
107111
wget http://archive.apache.org/dist/kafka/0.10.2.2/kafka_2.10-0.10.2.2.tgz
@@ -115,12 +119,12 @@ source /etc/profile
115119
**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)
116120

117121
##### Run Kafka
118-
**On Mac**:
122+
*On Mac*:
119123
```
120124
zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties & kafka-server-start /usr/local/etc/kafka/server.properties
121125
```
122126

123-
**On Linux**:
127+
*On Linux*:
124128
```
125129
zookeeper-server-start.sh /usr/local/kafka/config/zookeeper.properties &
126130
Sleep about 3 seconds
@@ -129,7 +133,7 @@ kafka-server-start.sh /usr/local/kafka/config/server.properties &
129133

130134
#### Create topics to receive events, the topic is defined in config.conf
131135

132-
**On Mac**:
136+
*On Mac*:
133137
```
134138
kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic block
135139
kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic transaction
@@ -140,7 +144,7 @@ kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partit
140144
kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic soliditylog
141145
```
142146

143-
**On Linux**:
147+
*On Linux*:
144148
```
145149
kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic block
146150
kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic transaction
@@ -153,7 +157,7 @@ kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --par
153157

154158
#### Kafka consumer
155159

156-
**On Mac**:
160+
*On Mac*:
157161
```
158162
kafka-console-consumer --bootstrap-server localhost:9092 --topic block
159163
kafka-console-consumer --bootstrap-server localhost:9092 --topic transaction
@@ -164,7 +168,7 @@ kafka-console-consumer --bootstrap-server localhost:9092 --topic solidityevent
164168
kafka-console-consumer --bootstrap-server localhost:9092 --topic soliditylog
165169
```
166170

167-
**On Linux**:
171+
*On Linux*:
168172
```
169173
kafka-console-consumer.sh --zookeeper localhost:2181 --topic block
170174
kafka-console-consumer.sh --zookeeper localhost:2181 --topic transaction
@@ -175,6 +179,29 @@ kafka-console-consumer.sh --zookeeper localhost:2181 --topic solidityevent
175179
kafka-console-consumer.sh --zookeeper localhost:2181 --topic soliditylog
176180
```
177181

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+
178205
### Load plugin in Java-tron
179206
* add --es to command line, for example:
180207
```

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,31 @@
33
import org.pf4j.ExtensionPoint;
44

55
public interface IPluginEventListener extends ExtensionPoint {
6-
void setServerAddress(String address);
76

8-
void setTopic(int eventType, String topic);
7+
void setServerAddress(String address);
98

10-
void setDBConfig(String dbConfig);
9+
void setTopic(int eventType, String topic);
1110

12-
// start should be called after setServerAddress, setTopic, setDBConfig
13-
void start();
11+
void setDBConfig(String dbConfig);
1412

15-
void handleBlockEvent(Object data);
13+
// start should be called after setServerAddress, setTopic, setDBConfig
14+
void start();
1615

17-
void handleTransactionTrigger(Object data);
16+
void stop();
1817

19-
void handleContractLogTrigger(Object data);
18+
void handleBlockEvent(Object data);
2019

21-
void handleContractEventTrigger(Object data);
20+
void handleTransactionTrigger(Object data);
2221

23-
void handleSolidityTrigger(Object trigger);
22+
void handleContractLogTrigger(Object data);
2423

25-
void handleSolidityLogTrigger(Object trigger);
24+
void handleContractEventTrigger(Object data);
2625

27-
void handleSolidityEventTrigger(Object trigger);
26+
void handleSolidityTrigger(Object trigger);
2827

29-
int getPendingSize();
28+
void handleSolidityLogTrigger(Object trigger);
29+
30+
void handleSolidityEventTrigger(Object trigger);
31+
32+
int getPendingSize();
3033
}
Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
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

108
public class BlockLogTrigger extends Trigger {
9+
1110
@Getter
1211
@Setter
1312
private long blockNumber;
@@ -22,47 +21,24 @@ public class BlockLogTrigger extends Trigger {
2221

2322
@Getter
2423
@Setter
25-
private List<String> transactionList = new ArrayList<>();
26-
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() {
44-
setTriggerName(Trigger.BLOCK_TRIGGER_NAME);
31+
setTriggerName(EventTopic.BLOCK_TRIGGER.getName());
4532
}
4633

4734
@Override
4835
public String toString() {
49-
return new StringBuilder().append("triggerName: ").append(getTriggerName())
50-
.append("timestamp: ")
51-
.append(timeStamp)
52-
.append(", blockNumber: ")
53-
.append(blockNumber)
54-
.append(", blockhash: ")
55-
.append(blockHash)
56-
.append(", transactionSize: ")
57-
.append(transactionSize)
58-
.append(", transactionList: ")
59-
.append(transactionList)
60-
.append(", witnessAddress: ")
61-
.append(witnessAddress)
62-
.append(", witnessPayPerBlock: ")
63-
.append(witnessPayPerBlock)
64-
.append(", witnessMap: ")
65-
.append(witnessMap)
66-
.toString();
36+
return "triggerName: " + getTriggerName()
37+
+ ", timestamp: " + timeStamp
38+
+ ", blockNumber: " + blockNumber
39+
+ ", blockhash: " + blockHash
40+
+ ", transactionSize: " + transactionSize
41+
+ ", latestSolidifiedBlockNumber: " + latestSolidifiedBlockNumber
42+
+ ", transactionList: " + transactionList;
6743
}
6844
}

api/src/main/java/org/tron/common/logsfilter/trigger/ContractEventTrigger.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ public class ContractEventTrigger extends ContractTrigger {
3737
@Setter
3838
private Map<String, String> dataMap;
3939

40-
4140
public ContractEventTrigger() {
4241
super();
43-
setTriggerName(Trigger.CONTRACTEVENT_TRIGGER_NAME);
42+
setTriggerName(EventTopic.CONTRACT_EVENT_TRIGGER.getName());
4443
}
4544
}

api/src/main/java/org/tron/common/logsfilter/trigger/ContractLogTrigger.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.Setter;
66

77
public class ContractLogTrigger extends ContractTrigger {
8+
89
/**
910
* topic list produced by the smart contract LOG function
1011
*/
@@ -21,6 +22,6 @@ public class ContractLogTrigger extends ContractTrigger {
2122

2223
public ContractLogTrigger() {
2324
super();
24-
setTriggerName(Trigger.CONTRACTLOG_TRIGGER_NAME);
25+
setTriggerName(EventTopic.CONTRACT_LOG_TRIGGER.getName());
2526
}
2627
}

api/src/main/java/org/tron/common/logsfilter/trigger/ContractTrigger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import lombok.Getter;
44
import lombok.Setter;
55

6-
public class ContractTrigger extends Trigger {
6+
public abstract class ContractTrigger extends Trigger {
77

88
/**
99
* id of the transaction which produce this event.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.tron.common.logsfilter.trigger;
2+
3+
4+
import lombok.Getter;
5+
6+
@Getter
7+
public enum EventTopic {
8+
BLOCK_TRIGGER(0, "blockTrigger"),
9+
TRANSACTION_TRIGGER(1, "transactionTrigger"),
10+
CONTRACT_LOG_TRIGGER(2, "contractLogTrigger"),
11+
CONTRACT_EVENT_TRIGGER(3, "contractEventTrigger"),
12+
SOLIDITY_TRIGGER(4, "solidityTrigger"),
13+
SOLIDITY_EVENT(5, "solidityEventTrigger"),
14+
SOLIDITY_LOG(6, "solidityLogTrigger");
15+
16+
private final Integer type;
17+
private final String name;
18+
19+
EventTopic(Integer type, String name) {
20+
this.type = type;
21+
this.name = name;
22+
}
23+
24+
public static EventTopic getEventTopicByType(int topicType) {
25+
for (EventTopic member : values()) {
26+
if (member.getType() == topicType) {
27+
return member;
28+
}
29+
}
30+
return null;
31+
}
32+
33+
public static EventTopic getEventTopicByName(String topicName) {
34+
for (EventTopic member : values()) {
35+
if (member.getName().equals(topicName)) {
36+
return member;
37+
}
38+
}
39+
return null;
40+
}
41+
}

api/src/main/java/org/tron/common/logsfilter/trigger/SolidityTrigger.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
import lombok.Setter;
55

66
public class SolidityTrigger extends Trigger {
7+
78
@Getter
89
@Setter
910
private long latestSolidifiedBlockNumber;
1011

1112
@Override
1213
public String toString() {
13-
return new StringBuilder().append("triggerName: ").append(getTriggerName())
14-
.append("timestamp: ")
15-
.append(timeStamp)
16-
.append(", latestSolidifiedBlockNumber: ")
17-
.append(latestSolidifiedBlockNumber).toString();
14+
return "triggerName: " + getTriggerName()
15+
+ ", timestamp: " + timeStamp
16+
+ ", latestSolidifiedBlockNumber: " + latestSolidifiedBlockNumber;
1817
}
1918

2019
public SolidityTrigger() {
21-
setTriggerName(Trigger.SOLIDITY_TRIGGER_NAME);
20+
setTriggerName(EventTopic.SOLIDITY_TRIGGER.getName());
2221
}
2322
}

api/src/main/java/org/tron/common/logsfilter/trigger/TransactionLogTrigger.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.tron.common.logsfilter.trigger;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54
import lombok.Getter;
65
import lombok.Setter;
@@ -96,6 +95,6 @@ public void setTimeStamp(long ts) {
9695
private List<InternalTransactionPojo> internalTrananctionList;
9796

9897
public TransactionLogTrigger() {
99-
setTriggerName(Trigger.TRANSACTION_TRIGGER_NAME);
98+
setTriggerName(EventTopic.TRANSACTION_TRIGGER.getName());
10099
}
101100
}

api/src/main/java/org/tron/common/logsfilter/trigger/Trigger.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,13 @@
33
import lombok.Getter;
44
import lombok.Setter;
55

6-
public class Trigger {
6+
public abstract class Trigger {
7+
78
@Getter
89
@Setter
910
protected long timeStamp;
1011

1112
@Getter
1213
@Setter
1314
private String triggerName;
14-
15-
public static final int BLOCK_TRIGGER = 0;
16-
public static final int TRANSACTION_TRIGGER = 1;
17-
public static final int CONTRACTLOG_TRIGGER = 2;
18-
public static final int CONTRACTEVENT_TRIGGER = 3;
19-
public static final int SOLIDITY_TRIGGER = 4;
20-
public static final int SOLIDITY_EVENT = 5;
21-
public static final int SOLIDITY_LOG = 6;
22-
23-
public static final String BLOCK_TRIGGER_NAME = "blockTrigger";
24-
public static final String TRANSACTION_TRIGGER_NAME = "transactionTrigger";
25-
public static final String CONTRACTLOG_TRIGGER_NAME = "contractLogTrigger";
26-
public static final String CONTRACTEVENT_TRIGGER_NAME = "contractEventTrigger";
27-
public static final String SOLIDITY_TRIGGER_NAME = "solidityTrigger";
2815
}

0 commit comments

Comments
 (0)