Event订阅

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

推荐配置

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

插件逻辑

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

部署Vision event subscribe plugin

#部署
git clone https://github.com/vision-consensus/vision-event-plugin.git
cd vision-event-plugin
./gradlew build

配置文件 -- 字段解析

  • path: "plugin-mongodb-1.0.0.zip"的绝对路径
  • server: 服务器地址+端口号(mongodb的)
  • dbconfig: mongodb的配置,按例子中的来
  • topics: 目前支持的事件类型
  • triggerName: 触发类型
  • enable: fasle就是禁用,true开启
  • topic: mongodb接收事件的集合
event.subscribe = {
  native = {
    useNativeQueue = false // if true, use native message queue, else use event plugin.
    bindport = 5555 // bind port
    sendqueuelength = 1000 //max length of send queue
  }
  path = "/data/vision/mongoplugin/plugin-mongodb-1.0.0.zip" // absolute path of plugin
  server = "127.0.0.1:37017" // target server address to receive event triggers
  dbconfig = "eventlog|vision|123456" // dbname|username|password
  contractParse = true,
  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"
    },
    {
      triggerName = "solidity" // solidity block event trigger, the value can't be modified
      enable = true            // the default value is true
      topic = "solidity"
    },
    {
      triggerName = "solidityevent"
      enable = true
      topic = "solidityevent"
    },
    {
      triggerName = "soliditylog"
      enable = true
      topic = "soliditylog"
    }
  ]
  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.
    ]
  }
}

部署Vision Event Query Service

#部署
git clone https://github.com/vision-consensus/vision-event-query.git 
cd vision-event-query
docker-compose -d up

配置文件 -- 字段解析

  • MONGO_HOST: 修改为本机IP地址
  • 以下环境变量配置可以根据自己需求调整
  • MONGO_PORT: eventquery连接需要,需要与mongodb ports保持一致
  • MONGO_INITDB_DATABASE:mongodb中创建记录event事件的数据库,需要与visioneventquery中保持一致
  • MONGO_DATEBASE_USERNAME: 访问event事件数据库的用户名,需要与visioneventquery中保持一致
  • MONGO_DATEBASE_PASSWORD:访问event事件数据库的密码,需要与visioneventquery中保持一致
version: '2.2'

services:
  mongodb:
    image: 'mongo:4.0'
    environment:
      MONGO_INITDB_ROOT_USERNAME: "root"   #the root username in mongodb
      MONGO_INITDB_ROOT_PASSWORD: "123456" #the raot password in mongodb
      MONGO_INITDB_DATABASE: "eventlog"    #the database of event log in mongodb
      MONGO_DATEBASE_USERNAME: "vision"    #the username of database event log
      MONGO_DATEBASE_PASSWORD: "123456"    #the password of database event log
    ports:
      - "27017:27017" # keep same with visioneventquery's parameter MONGO_PORT
    volumes:
      - 'mongodb_data:/data/db'
      - './mongo_init.sh:/docker-entrypoint-initdb.d/mongo_init.sh'
    networks:
      - eventquery

  visioneventquery:
    build:
      context: ./
    volumes:
      - 'eq_data:/usr/app'
    ports:
      - "8080:8080"
    environment:
      EQ_JAVA_OPTS: "-Xmx256m"
      MONGO_HOST: "mongodb"
      MONGO_PORT: "27017"
      MONGO_INITDB_DATABASE: "eventlog"  # keep same with mongodb's parameter MONGO_INITDB_DATABASE
      MONGO_DATEBASE_USERNAME: "vision"  # keep same with mongodb's parameter MONGO_DATEBASE_USERNAME
      MONGO_DATEBASE_PASSWORD: "123456"  # keep same with mongodb's parameter MONGO_DATEBASE_PASSWORD
    networks:
      - eventquery
    depends_on:
      - mongodb

networks:
  eventquery:
    driver: bridge

volumes:
  mongodb_data:
    driver: local
  eq_data:
    driver: local

启动和验证

#1、event订阅服务器优先启动(同上,部署Vision Event Query Service)
docker-compose -d up

#2、Fullnode的启动,配置文件config.conf添加plugin后,添加“--es”启动event订阅
java -jar FullNode.jar -c config.conf --es

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

#3、验证event订阅服务是否正常运行
http://localhost:8080/blocks

使用event query service

  • Main HTTP Service
** baseUrl: localhost

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://baseUrl/transactions?limit=1&sort=-timeStamp&start=2&block=0


Function: get transaction by hash
subpath: $baseUrl/transactions/{hash}
parameters   
hash: transaction id
Example: http://baseUrl/transactions/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://baseUrl/transfers?token=vs&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://baseUrl/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://baseUrl/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://baseUrl/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://baseUrl/events/VYKDHeQfG38C62Xo8tHLrGWiw5o9hL3QEK?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://baseUrl/events/contract/VYKDHeQfG38C62Xo8tHLrGWiw5o9hL3QEK/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://baseUrl/events/contract/VYKDHeQfG38C62Xo8tHLrGWiw5o9hL3QEK/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://baseUrl/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://baseUrl/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://baseUrl/blocks/000000000049c11f15d4x91e988bc950fa9f194d2cb2e04cda76675dbb349009


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://baseUrl/blocks?limit=1&sort=timeStamp&start=0&block=0


Function: get latest block number
subpath: $baseUrl/blocks/latestSolidifiedBlockNumber
parameters   
none
Example: http://baseUrl/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://baseUrl/contractlogs


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


Function: post abi string and get contract log list based on transactionId
subpath: $baseUrl/contract/transaction/{transactionId}
parameters   
transactionId
body:
abi: user self upload abi
Example: http://baseUrl/contract/transaction/{transactionId}


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


Function: post abi string and get contract log list based on contractAddress
subpath: $baseUrl/contract/contractAddress/{contractAddress}
parameters   
contractAddress
abi: user self upload abi
Example: http://baseUrl/contract/contractAddress/{contractAddress}


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


Function: post abi string and get contract log list based on uniqueId
subpath: $baseUrl/contract/uniqueId/{uniqueId}
parameters   
uniqueId
abi: user self upload abi
Example: http://baseUrl/contract/uniqueId/{uniqueId}