事件插件部署(MongoDB)

本文主要讲解事件插件部署步骤,包括:MongoDB、Tron event subscribe plugin、Tron Event Query Service的部署命令以及Tron Event Query Service接口的详细介绍。

推荐配置

  • CPU/ RAM: 16Core / 32G
  • DISK: 500G
  • System: CentOS 64

插件逻辑

  • Tron event subscribe plugin的功能是从节点获取事件信息,存入MongoDB
  • MongoDB的功能是保存事件信息
  • Tron Event Query Service的功能是提供封装好的http接口,从MongoDB中获取事件信息。

部署Tron event subscribe plugin

#部署
git clone https://github.com/tronprotocol/event-plugin.git
cd eventplugin
./gradlew build
  • 配置节点配置文件
    在节点配置文件末尾追加,下面是例子。也可以参考README.md。
event.subscribe = {
    path = "/deploy/fullnode/event-plugin/build/plugins/plugin-mongodb-1.0.0.zip" // absolute path of plugin
    server = "127.0.0.1:27017" // target server address to receive event triggers
    dbconfig = "eventlog|tron|123456" // dbname|username|password
    topics = [
        {
          triggerName = "block" // block trigger, the value can't be modified
          enable = true
          topic = "block" // plugin topic, the value could be modified
        },
        {
          triggerName = "transaction"
          enable = true
          topic = "transaction"
        },
        {
          triggerName = "contractevent"
          enable = true
          topic = "contractevent"
        },
        {
          triggerName = "contractlog"
          enable = true
          topic = "contractlog"
        }
    ]

    filter = {
       fromblock = "" // the value could be "", "earliest" or a specified block number as the beginning of the queried range
       toblock = "" // the value could be "", "latest" or a specified block number as end of the queried range
       contractAddress = [
           "" // contract address you want to subscribe, if it's set to "", you will receive contract logs/events with any contract address.
       ]

       contractTopic = [
           "" // contract topic you want to subscribe, if it's set to "", you will receive contract logs/events with any contract topic.
       ]
    }
}

📘

字段解析

  • path: "plugin-kafka-1.0.0.zip" 或 "plugin-mongodb-1.0.0.zip"的绝对路径
  • server: 服务器地址+端口号(mongodb的)
  • dbconfig: mongodb的配置,按例子中的来
  • topics: 目前支持四种事件类型 block, transaction, contract log and contract event
  • triggerName: 触发类型,可以修改
  • enable: fasle就是禁用,true开启
  • topic: mongodb接收事件的集合

部署MongoDB

#1、安装
mkdir /home/java-tron
cd /home/java-tron
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.4.tgz
tar zxvf mongodb-linux-x86_64-4.0.4.tgz
mv mongodb-linux-x86_64-4.0.4 mongodb

#2、设置环境变量 

export MONGOPATH=/home/java-tron/mongodb/
export PATH=$PATH:$MONGOPATH/bin

#3、mongodb配置 

mkdir -p /home/java-tron/mongodb/{log,data}
cd /home/java-tron/mongodb/log/ && touch mongodb.log && cd
vim mgdb.conf

把创建的数据和日志文件夹,写到配置文件中(绝对路径)
配置文件示例:

dbpath=/home/java-tron/mongodb/data
logpath=/home/java-tron/mongodb/log/mongodb.log
port=27017
logappend=true
fork=true
bind_ip=0.0.0.0
auth=true
wiredTigerCacheSizeGB=2

📘

注:

  • bind_ip必须配置为0.0.0.0,否则将拒绝远程连接。
  • wiredTigerCacheSizeGB,必须配置为防止OOM
#4、启动mongodb
mongod --config ./mgdb.conf &

#5、创建管理员命令
mongo
use admin
db.createUser({user:"root",pwd:"admin",roles:[{role:"root",db:"admin"}]})

#6、创建db eventlog和账户命令
db.auth("root", "admin")
use eventlog
db.createUser({user:"tron",pwd:"123456",roles:[{role:"dbOwner",db:"eventlog"}]})

部署Tron Event Query Service

❗️

注:

Tron Event Query Service的部署依赖Java环境。在Linux Ubuntu系统上(例如Ubuntu 16.04.4 LTS),确保机器的JDK使用的是 Oracle JDK 8,而不是 OpenJDK 8

#1、下载源码
git clone https://github.com/tronprotocol/tron-eventquery.git cd troneventquery

#2、构建命令
wget https://mirrors.cnnic.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz --no-check-certificate
tar zxvf apache-maven-3.5.4-bin.tar.gz
export M2_HOME=$HOME/maven/apache-maven-3.5.4
export PATH=$PATH:$M2_HOME/bin
mvn --version
mvn package

命令执行成功后,会在troneventquery/target下生成jar包,还有生成配置文件,路径是troneventquery/config.conf ,配置内容为:

mongo.host=IP
mongo.port=27017
mongo.dbname=eventlog
mongo.username=tron
mongo.password=123456
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#3、启动tron event query service
sh troneventquery/deploy.sh
sh troneventquery/insertIndex.sh

📘

注:

  • 默认端口8080,如果要改,修改脚本troneventquery/deploy.sh:
    nohup java -jar -Dserver.port=8081 target/troneventquery-1.0.0-SNAPSHOT.jar 2>&1 &

启动和验证

#1、Fullnode的启动命令
java -jar FullNode.jar -c config.conf --es
#注:在启动全节点前要先启动mongodb。
#全节点的安装参考:https://github.com/tronprotocol/java-tron/blob/develop/build.md

#2、验证插件加载
tail -f logs/tron.log |grep -i eventplugin
#出现下方字样即成功
#o.t.c.l.EventPluginLoader 'your plugin path/plugin-kafka-1.0.0.zip' loaded

#3、验证数据是否存到mongodb
#如果有返回信息说明数据可以从节点获取,通过事件订阅,存储到mongodb。否则,查看fullnode日志来逐步排查。
mongo 47.90.245.68:27017
use eventlog
db.auth("tron", "123456")
show collections
db.block.find()

使用event query service

  • Main HTTP Service
#Function: get transaction list
subpath: $baseUrl/transactions
parameters   
limit: each page size, default is 25
sort: sort Field, default is sort by timeStamp descending order
start: start page, default is 1
block: start block number, default is 0
Example: http://127.0.0.1:8080/transactions?limit=1&sort=-timeStamp&start=2&block=0

#Function: get transaction by hash
subpath: $baseUrl/transactions/{hash}
parameters   
hash: transaction id
Example: http://127.0.0.1:8080/9a4f096700672d7420889cd76570ea47bfe9ef815bb2137b0d4c71b3d23309e9


#Function: get transfers list
subpath: $baseUrl/transfers 
parameters   
limit: each page size, default is 25
sort: sort Field, default is sort by timeStamp descending order
start: start page, default is 1
from: from address, default is ""
to: to address, default is ""
token: tokenName, default is ""
Example: http://127.0.0.1:8080/transfers?token=trx&limit=1&sort=timeStamp&start=2&block=0&from=TJ7yJNWS8RmvpXcAyXBhvFDfGpV9ZYc3vt&to=TAEcoD8J7P5QjWT32r31gat8L7Sga2qUy8

#Function: get transfers by transactionId
subpath: $baseUrl/transfers/{hash}
parameters   
hash: transfer hash
Example: http://127.0.0.1:8080/transfers/70d655a17e04d6b6b7ee5d53e7f37655974f4e71b0edd6bcb311915a151a4700

#Function: get events list
subpath: $baseUrl/events
parameters   
limit: each page size, default is 25
sort: sort Field, default is sort by timeStamp descending order
since: start time of event occurrence, timeStamp >= since will be shown
start: start page, default is 1
block: block number, block number >= block will be shown
Example: http://127.0.0.1:8080/events?limit=1&sort=timeStamp&since=0&block=0&start=0

#Function: get events by transactionId
subpath: $baseUrl/events/transaction/{transactionId}
parameters   
transactionId
Example: http://127.0.0.1:8080/events/transaction/cd402e64cad7e69c086649401f6427f5852239f41f51a100abfc7beaa8aa0f9c

#Function: get events by contract address
subpath: $baseUrl/events/{contractAddress}
parameters   
limit: each page size, default is 25
sort: sort Field, default is sort by timeStamp descending order
since: start time of event occurrence, timeStamp >= since will be shown
block: block number, block number >= block will be shown
contractAddress: contract address
start: start page, default is 1
Example: http://127.0.0.1:8080/events/TMYcx6eoRXnePKT1jVn25ZNeMNJ6828HWk?limit=1&sort=-timeStamp&since=0&block=0&start=4

#Function: get events by contract address and event name
subpath: $baseUrl/events/contract/{contractAddress}/{eventName}
parameters   
limit: each page size, default is 25
sort: sort Field, default is sort by timeStamp descending order
since: start time of event occurrence, timeStamp >= since will be shown
contract`Address`: contract address
start: start page, default is 1
eventName: event name
Example: http://127.0.0.1:8080/events/contract/TMYcx6eoRXnePKT1jVn25ZNeMNJ6828HWk/Bet?limit=1&sort=timeStamp&since=1&start=0

#Function: get events by contract address, event name and block number
subpath: $baseUrl/events/contract/{contractAddress}/{eventName}/{blockNumber}
parameters   
contractAddress: contract address
blockNumber: block number, block number >= block will be shown
eventName: event name
Example: http://127.0.0.1:8080/events/contract/TMYcx6eoRXnePKT1jVn25ZNeMNJ6828HWk/Bet/4835773

#Function: get events by timeStamp
subpath: $baseUrl/events/timestamp
parameters   
since: start time of event occurrence, timeStamp >= since will be shown
limit: each page size, default is 25
sort: sort Field, default is sort by timeStamp descending order
start: start page, default is 1
contract: contract address
Example: http://127.0.0.1:8080/events/timestamp?since=1544483426749&limit=1&start=1&sort=timeStamp

#Function: get confirm events list
subpath: $baseUrl/events/confirmed
parameters   
since: start time of event occurrence, timeStamp >= since will be shown
limit: each page size, default is 25
sort: sort Field, default is sort by timeStamp descending order
start: start page, default is 1
Example: http://127.0.0.1:8080/events/confirmed?since=1544483426749&limit=1&start=1&sort=timeStamp

#Function: get block by block hash
subpath: $baseUrl/blocks/{hash}
parameters   
hash: block hash
Example: http://127.0.0.1:8080/blocks/000000000049c11f15d4e91e988bc950fa9f194d2cb2e04cda76675dbb349009

#Function: get block list
subpath: $baseUrl/blocks
parameters   
limit: each page size, default is 25
sort: sort Field, default is sort by timeStamp descending order
start: start page, default is 1
block: block number, block number >= block will be shown 
Example: http://127.0.0.1:8080/blocks?limit=1&sort=timeStamp&start=0&block=0

#Function: get latest block number
subpath: $baseUrl/blocks/latestSolidifiedBlockNumber
parameters   
none
Example: http://127.0.0.1:8080/blocks/latestSolidifiedBlockNumber

#Function: get contract log list
subpath: $baseUrl/contractlogs
parameters   
limit: each page size, default is 25
sort: sort Field, default is sort by timeStamp descending order
start: start page, default is 1
block: block number, block number >= block will be shown 
Example: http://127.0.0.1:8080/contractlogs

#Function: get contract log list based on transactionId
subpath: $baseUrl/contractlogs/transaction/{transactionId}
parameters   
transactionId
Example: http://127.0.0.1:8080/contractlogs/transaction/{transactionId}

#Function: post abi string and get contract log list based on transactionId(release on 3.6)
subpath: $baseUrl/contract/transaction/{transactionId}
parameters   
transactionId
body:
abi: user self upload abi
Example: http://127.0.0.1:8080/contract/transaction/{transactionId}

#Function: get contract log list based on contractAddress
subpath: $baseUrl/contractlogs/contract/{contractAddress}
parameters   
contractAddress
Example: http://127.0.0.1:8080/contractlogs/contract/{contractAddress}

#Function: post abi string and get contract log list based on contractAddress(release on 3.6)
subpath: $baseUrl/contract/contractAddress/{contractAddress}
parameters   
contractAddress
abi: user self upload abi
Example: http://127.0.0.1:8080/contract/contractAddress/{contractAddress}

#Function: get contract log list based on uniqueId
subpath: $baseUrl/contractlogs/uniqueId/{uniqueId}
parameters   
uniqueId
Example: http://127.0.0.1:8080/contractlogs/uniqueId/{uniqueId}

#Function: post abi string and get contract log list based on uniqueId(release on 3.6)
subpath: $baseUrl/contract/uniqueId/{uniqueId}
parameters   
uniqueId
abi: user self upload abi
Example: http://127.0.0.1:8080/contract/uniqueId/{uniqueId}