Compare commits
385 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b74decbe6 | |||
| b2b3a9d674 | |||
| 8b6fd9a1a8 | |||
| bc19f2caae | |||
| 82160c72ad | |||
| 3f4fd975a2 | |||
| 86de28aadd | |||
| 2ac15b0e5b | |||
| 4fcee923bd | |||
| 89c6cd280d | |||
| f372d8318e | |||
| 8af456d155 | |||
| 0696e81f6d | |||
| fbf0f634c9 | |||
| ac9fafd3be | |||
| a467b81404 | |||
| 3c11a5109a | |||
| 640eccb7bd | |||
| de981f32b8 | |||
| 91821b6365 | |||
| 5801801967 | |||
| 206d92034b | |||
| 49bf80b57f | |||
| 279f5d9474 | |||
| 7cf9f0613c | |||
| a816efe82a | |||
| 56305d4138 | |||
| 1aaeb6b78f | |||
| a68aeb79c6 | |||
| 1ced41dfc6 | |||
| 0c3dad17cd | |||
| c39b1df4a0 | |||
| af59050e0b | |||
| f7f5c2b738 | |||
| 994025c2c4 | |||
| f859840318 | |||
| c80eaaa9fc | |||
| c98f2dc431 | |||
| 797fb867b1 | |||
| 38e939a640 | |||
| 88a24c579f | |||
| e17d6e8209 | |||
| 787b5aba65 | |||
| 6494e57b7f | |||
| fc03425190 | |||
| 630f0e286c | |||
| 54d446adc3 | |||
| 5d40aadbac | |||
| b3ae01b2c7 | |||
| 5d73e452dc | |||
| e3c7342b4d | |||
| 6531c7bf8a | |||
| 612f373725 | |||
| b386be795e | |||
| 417bb96294 | |||
| a5f14071d3 | |||
| 1f18f54933 | |||
| fdd75e8393 | |||
| 7223ecd8a0 | |||
| 6169d54474 | |||
| 7d3b7451ea | |||
| b7ab2f055f | |||
| 8cdd29fda9 | |||
| d67bbe9c21 | |||
| ee708a22f1 | |||
| 27141e6e89 | |||
| 0386bc7ce8 | |||
| 1f0e696308 | |||
| 1748035656 | |||
| 3b58aa28c7 | |||
| 8bffd2d1e7 | |||
| 01570f1a96 | |||
| 6e3510ebb1 | |||
| 985cd7e189 | |||
| 6853f7e897 | |||
| 28c163a500 | |||
| b7798e41f4 | |||
| 404808f1b7 | |||
| 565a34b366 | |||
| 11b7b28212 | |||
| 7ca25d6791 | |||
| fc99cc2e3f | |||
| 2bb5b801b4 | |||
| 8e1d0db9cb | |||
| 8d96e4f525 | |||
| 00f063e2e4 | |||
| d5b12d89bd | |||
| ad8a3bedb7 | |||
| 20a0b1d4a2 | |||
| d13e186fdc | |||
| 50400787f1 | |||
| b451619555 | |||
| b82dddac7b | |||
| f372f792e5 | |||
| d6533332c6 | |||
| 96d9a5aca0 | |||
| f2c8b9bdf9 | |||
| d122d79022 | |||
| aab5c66c89 | |||
| 4f0d7b18f3 | |||
| e6d66181e1 | |||
| 4ac735dd73 | |||
| 341769d92a | |||
| c7a841e6cd | |||
| 71831f0948 | |||
| ca2ae004f2 | |||
| 908a0e5636 | |||
| aaada8cfb7 | |||
| c721eea8a6 | |||
| 769d942fc5 | |||
| d44be3b2ee | |||
| 888171efe6 | |||
| c3acca1516 | |||
| 02472751a5 | |||
| 27dd37bb4c | |||
| 61d9309a2d | |||
| 3a7d422445 | |||
| 64452c8cd6 | |||
| e91ece0a39 | |||
| c53ef3ee61 | |||
| 86c00f68b5 | |||
| 3a3e6acd7a | |||
| ceecc977e2 | |||
| ba637daa14 | |||
| ba18937cd5 | |||
| 3bf0226c8e | |||
| 9b990926c3 | |||
| 163f68f795 | |||
| b6ae493894 | |||
| 40cffc34fb | |||
| 40f01c6783 | |||
| c3447674e6 | |||
| df9697f8fa | |||
| 135a4152f0 | |||
| 11614daa3a | |||
| 627d889f78 | |||
| a2c2c4cf8a | |||
| 89aa03bdba | |||
| 01fd572376 | |||
| 5b351452f8 | |||
| ac8872bf22 | |||
| e065c1c6ef | |||
| 64548cf3e1 | |||
| 2fbc9ecda4 | |||
| eae3f16b44 | |||
| 2129945d4d | |||
| 6cd4a4d315 | |||
| e562fb3104 | |||
| 8214a4ad82 | |||
| fabcc5e96c | |||
| 41eab518e0 | |||
| d6d1c05bbf | |||
| 50fe19bce1 | |||
| 61a81e68e8 | |||
| 5b54b043f6 | |||
| a186d63b6d | |||
| d48c439dfb | |||
| 1409453556 | |||
| a633142e3d | |||
| 73cca21fae | |||
| d261cd4bc1 | |||
| 47e833a92e | |||
| 953633dd33 | |||
| a183eb420a | |||
| fe3692b387 | |||
| 983673d775 | |||
| dd6594a6d5 | |||
| 40ce288d20 | |||
| da0ff2f3c8 | |||
| 74ccb53358 | |||
| f498774496 | |||
| 99e957f418 | |||
| 106f45a7ee | |||
| 257c26d7b8 | |||
| 4ff6e3d209 | |||
| f4be437bbd | |||
| c11155456c | |||
| 49ef6696ca | |||
| 0f38ca6a6f | |||
| 9ae9e420ad | |||
| 34931afbdf | |||
| cb2b8bfdef | |||
| 8d8dee60a1 | |||
| 4c9f6e80f2 | |||
| d2633cdded | |||
| 6a10502240 | |||
| 582f423a1e | |||
| 5a09572e22 | |||
| e001770045 | |||
| 4c8d625eca | |||
| d725bda2b7 | |||
| fc48586365 | |||
| 74f146ae2a | |||
| e7e60f4e51 | |||
| 59e8e51a8d | |||
| f445fc9dd0 | |||
| 64e727bbff | |||
| 0655806ab4 | |||
| b310b4327e | |||
| 5e78f79c4f | |||
| 4838379bce | |||
| 13504e3d20 | |||
| 595599294a | |||
| c5d9cf4a96 | |||
| 23cb750527 | |||
| fec31246da | |||
| c411b17431 | |||
| 94cdc6b9a5 | |||
| 26884b9c27 | |||
| a2496283e5 | |||
| 4f66e6143e | |||
| 11a93481ef | |||
| 33d93a9f60 | |||
| 5caf21d656 | |||
| f7a499cb97 | |||
| 96f00f4969 | |||
| 9aaca9bca9 | |||
| 0c272f9bb4 | |||
| 4354482e0d | |||
| 9d2aacfdfa | |||
| 64e71cce82 | |||
| 5b3fbd0eb7 | |||
| 649a584f05 | |||
| 1f4016693e | |||
| ebaaaf4bb7 | |||
| 8bfa6e1c66 | |||
| 920ad417d4 | |||
| f0366232e2 | |||
| c8ce39131e | |||
| f1ee6acdf4 | |||
| daba656a5a | |||
| f17016160b | |||
| f3c6a07ab4 | |||
| 86d618e84c | |||
| 57b71cc82e | |||
| 39c74e28dd | |||
| 13b2932a00 | |||
| df4517d90b | |||
| a996185802 | |||
| 3c0df91013 | |||
| 034c8860a2 | |||
| 4e61626858 | |||
| a64af59725 | |||
| 4a762b64bc | |||
| b018a8fc34 | |||
| 00df96e05c | |||
| d22540167b | |||
| 957ec2a0c8 | |||
| da29a1d562 | |||
| 77af9982e6 | |||
| 9624a1e860 | |||
| 60baffb2f8 | |||
| 581d43e44d | |||
| b469964370 | |||
| a1fa6d20e8 | |||
| 9dd78d4f49 | |||
| ed5fdebbd3 | |||
| 78b5647e59 | |||
| a7ea1314ab | |||
| 35630441b8 | |||
| e6091f630b | |||
| 0e3096c2ab | |||
| bf6ba4f815 | |||
| 3dbd7a3ec3 | |||
| 9d1c749670 | |||
| fc24298531 | |||
| 33b82a1d76 | |||
| 8e2d6f240f | |||
| a8b08dd1ee | |||
| 3d79a3c568 | |||
| d74cd49a78 | |||
| 756e554bfc | |||
| 206a818563 | |||
| c34e9adde0 | |||
| 80438ba8c4 | |||
| d08244d5c5 | |||
| dae915edf8 | |||
| f54b91088f | |||
| 7ab8a16895 | |||
| 1d2d20ebde | |||
| adc79b5754 | |||
| 9b9f04530d | |||
| 6b182ec9c0 | |||
| 3b3e0615c7 | |||
| 83ceee1626 | |||
| 471d60e0b3 | |||
| 185d11dbd5 | |||
| 8996203cf9 | |||
| 24b700021f | |||
| fe549376cd | |||
| 36c8183c9d | |||
| fa1b16baf7 | |||
| 2b86ac8e58 | |||
| 77baa27210 | |||
| 211a210575 | |||
| f11a108cb9 | |||
| 4fd064949a | |||
| eac9221204 | |||
| 3051b618e5 | |||
| 73e3865f7b | |||
| 05d50842e2 | |||
| 6eb90312ef | |||
| 0fe292ffef | |||
| 11d88be525 | |||
| 42e402f721 | |||
| 09c279d472 | |||
| 34c9986834 | |||
| 1f33017578 | |||
| c0fac8ba7d | |||
| 0c4000626e | |||
| 044c1b62f3 | |||
| 8e8b20c871 | |||
| 1834b6d812 | |||
| d063b46621 | |||
| 87464a6687 | |||
| 2a2904949f | |||
| bd6985d414 | |||
| 97ed78782f | |||
| 90a0359430 | |||
| 191e1cd69d | |||
| e3523e7e24 | |||
| dac4ae69bc | |||
| 51078fe4aa | |||
| 20f2a44b48 | |||
| e436e822a6 | |||
| 149e5fcecf | |||
| 43d31b950c | |||
| 511893dcda | |||
| 5f36caab08 | |||
| 9bf61f1cab | |||
| 39513f1716 | |||
| 8424329b49 | |||
| d2f095700a | |||
| 2079195f82 | |||
| 478bb57fd8 | |||
| 410526856e | |||
| 017a6e5852 | |||
| 15197ccfd7 | |||
| da0dd9a51e | |||
| cbb2b3390f | |||
| f470caad2a | |||
| 855e1355f8 | |||
| da1efd54ab | |||
| 4bff17dca4 | |||
| d3a76f45ed | |||
| eeeec411a1 | |||
| 90b9fffc4a | |||
| 952390b262 | |||
| adca13c45d | |||
| 19f2e0decd | |||
| 66e0ef9922 | |||
| ee0f8e6584 | |||
| b26dba6653 | |||
| be95e0bb5b | |||
| 7442663647 | |||
| 258bd34b2b | |||
| 8fba5569b6 | |||
| 1b3135b68e | |||
| 2085649285 | |||
| b53464c824 | |||
| 8f3ad3e6aa | |||
| 39d92b8533 | |||
| 4dd5d4b387 | |||
| a1cb97eb32 | |||
| 5b7586f870 | |||
| 3966c93b14 | |||
| 3b7ac6702f | |||
| 7b0a666211 | |||
| 901e6de4c9 | |||
| 36a19670ff | |||
| 976372aa40 | |||
| 2bdcef524a | |||
| f0b78ad063 | |||
| b8640c7dc8 | |||
| cc600263b4 | |||
| 35bbe827dc | |||
| c312850240 | |||
| 84267a0b13 | |||
| 9ee7746cd2 | |||
| 70cf9f34c4 | |||
| 2a22055870 | |||
| 8376845e49 | |||
| ea2ad7c28e | |||
| 42bd0d3992 | |||
| a9819b26ee |
+18
-34
@@ -11,33 +11,8 @@ pids
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# TypeScript v1 declaration files
|
||||
typings/
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
@@ -51,18 +26,11 @@ typings/
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
|
||||
# next.js build output
|
||||
.next
|
||||
|
||||
.idea
|
||||
*.pem
|
||||
ecosystem.config.js
|
||||
package-lock.json
|
||||
connections.json
|
||||
connections.json2
|
||||
@@ -79,8 +47,6 @@ api/**/*.js
|
||||
api/**/*.js.map
|
||||
api/tests
|
||||
|
||||
hyperion-explorer/
|
||||
|
||||
hyperion-launcher.js
|
||||
hyperion-launcher.js.map
|
||||
|
||||
@@ -107,3 +73,21 @@ modules/**/*.js.map
|
||||
|
||||
addons/**/*.js
|
||||
addons/**/*.js.map
|
||||
|
||||
scripts/hpm.js
|
||||
scripts/hpm.js.map
|
||||
|
||||
scripts/hyp-config.js
|
||||
scripts/hyp-config.js.map
|
||||
|
||||
plugins/.state.json
|
||||
plugins/repos
|
||||
plugins/*.js
|
||||
.gitmodules
|
||||
|
||||
docker/redis/data
|
||||
docker/elasticsearch/data
|
||||
docker/rabbitmq/data
|
||||
docker/eosio/data
|
||||
|
||||
configuration_backups
|
||||
|
||||
-195
@@ -1,195 +0,0 @@
|
||||
### nodeos config.ini
|
||||
```
|
||||
state-history-dir = "state-history"
|
||||
trace-history = true
|
||||
chain-state-history = true
|
||||
state-history-endpoint = 127.0.0.1:8080
|
||||
plugin = eosio::state_history_plugin
|
||||
```
|
||||
|
||||
### NodeJS
|
||||
|
||||
```bash
|
||||
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
|
||||
sudo apt-get install -y nodejs
|
||||
```
|
||||
|
||||
### PM2
|
||||
|
||||
```bash
|
||||
sudo npm install pm2@latest -g
|
||||
sudo pm2 startup
|
||||
```
|
||||
|
||||
### Elasticsearch Installation
|
||||
|
||||
* Follow instructions on https://www.elastic.co/guide/en/elasticsearch/reference/current/deb.html (Ubuntu/Debian)
|
||||
|
||||
Edit `/etc/elasticsearch/elasticsearch.yml`
|
||||
|
||||
```
|
||||
cluster.name: myCluster
|
||||
bootstrap.memory_lock: true
|
||||
```
|
||||
|
||||
Edit `/etc/elasticsearch/jvm.options`
|
||||
```
|
||||
# Set your heap size, avoid allocating more than 31GB, even if you have enought RAM.
|
||||
# Test on your specific machine by changing -Xmx32g in the following command:
|
||||
# java -Xmx32g -XX:+UseCompressedOops -XX:+PrintFlagsFinal Oops | grep Oops
|
||||
-Xms16g
|
||||
-Xmx16g
|
||||
```
|
||||
|
||||
Disable swap on /etc/fstab
|
||||
|
||||
Allow memlock:
|
||||
run `sudo systemctl edit elasticsearch` and add the following lines
|
||||
|
||||
|
||||
```
|
||||
[Service]
|
||||
LimitMEMLOCK=infinity
|
||||
```
|
||||
|
||||
Start elasticsearch and check the logs (verify if the memory lock was successful)
|
||||
|
||||
```bash
|
||||
sudo service elasticsearch start
|
||||
sudo less /var/log/elasticsearch/myCluster.log
|
||||
sudo systemctl enable elasticsearch
|
||||
```
|
||||
|
||||
Test the REST API `curl http://localhost:9200`
|
||||
|
||||
```
|
||||
{
|
||||
"name" : "ip-172-31-5-121",
|
||||
"cluster_name" : "hyperion",
|
||||
"cluster_uuid" : "....",
|
||||
"version" : {
|
||||
"number" : "7.1.0",
|
||||
"build_flavor" : "default",
|
||||
"build_type" : "deb",
|
||||
"build_hash" : "606a173",
|
||||
"build_date" : "2019-05-16T00:43:15.323135Z",
|
||||
"build_snapshot" : false,
|
||||
"lucene_version" : "8.0.0",
|
||||
"minimum_wire_compatibility_version" : "6.8.0",
|
||||
"minimum_index_compatibility_version" : "6.0.0-beta1"
|
||||
},
|
||||
"tagline" : "You Know, for Search"
|
||||
}
|
||||
```
|
||||
|
||||
### Kibana Installation
|
||||
|
||||
```bash
|
||||
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.4.0-amd64.deb
|
||||
sudo apt install ./kibana-7.4.0-amd64.deb
|
||||
sudo systemctl enable kibana
|
||||
```
|
||||
|
||||
Open and test Kibana on `http://localhost:5601`
|
||||
|
||||
### RabbitMQ Installation
|
||||
|
||||
`https://www.rabbitmq.com/install-debian.html#installation-methods`
|
||||
|
||||
```bash
|
||||
sudo apt install rabbitmq-server
|
||||
sudo rabbitmq-plugins enable rabbitmq_management
|
||||
sudo rabbitmqctl add_vhost /hyperion
|
||||
sudo rabbitmqctl add_user my_user my_password
|
||||
sudo rabbitmqctl set_user_tags my_user administrator
|
||||
sudo rabbitmqctl set_permissions -p /hyperion my_user ".*" ".*" ".*"
|
||||
```
|
||||
|
||||
Check access to the WebUI `http://localhost:15672`
|
||||
|
||||
### Redis Installation
|
||||
|
||||
```bash
|
||||
sudo apt install redis-server
|
||||
```
|
||||
|
||||
Edit `/etc/redis/redis.conf` and change supervised to `systemd`
|
||||
|
||||
```bash
|
||||
sudo systemctl restart redis.service
|
||||
```
|
||||
|
||||
### Hyperion Indexer
|
||||
|
||||
```bash
|
||||
git clone https://github.com/eosrio/Hyperion-History-API.git
|
||||
cd Hyperion-History-API
|
||||
npm install
|
||||
cp example-ecosystem.config.js ecosystem.config.js
|
||||
```
|
||||
|
||||
Edit `ecosystem.config.js`
|
||||
|
||||
|
||||
### Setup Indices and Aliases
|
||||
|
||||
Load templates first by starting the Hyperion Indexer in preview mode `PREVIEW: 'true'`
|
||||
|
||||
Indices and aliases are created automatically using the `CREATE_INDICES` option (set it to your version suffix e.g, v1, v2, v3)
|
||||
If you want to create them manually, use the commands bellow on the kibana dev console
|
||||
```
|
||||
PUT mainnet-action-v1-000001
|
||||
PUT mainnet-abi-v1-000001
|
||||
PUT mainnet-block-v1-000001
|
||||
|
||||
POST _aliases
|
||||
{
|
||||
"actions": [
|
||||
{
|
||||
"add": {
|
||||
"index": "mainnet-abi-v1-000001",
|
||||
"alias": "mainnet-abi"
|
||||
}
|
||||
},
|
||||
{
|
||||
"add": {
|
||||
"index": "mainnet-action-v1-000001",
|
||||
"alias": "mainnet-action"
|
||||
}
|
||||
},
|
||||
{
|
||||
"add": {
|
||||
"index": "mainnet-block-v1-000001",
|
||||
"alias": "mainnet-block"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Before indexing actions into elasticsearch its required to do a ABI scan pass
|
||||
|
||||
Start with
|
||||
```
|
||||
ABI_CACHE_MODE: 'true',
|
||||
FETCH_BLOCK: 'false',
|
||||
FETCH_TRACES: 'false',
|
||||
INDEX_DELTAS: 'false',
|
||||
INDEX_ALL_DELTAS: 'false',
|
||||
```
|
||||
|
||||
When indexing is finished, change the settings back and restart the indexer. In case you do not have much contract updates, you do not need to run a full pass.
|
||||
|
||||
Tune your configs to your specific hardware using the following settings:
|
||||
```
|
||||
BATCH_SIZE
|
||||
READERS
|
||||
DESERIALIZERS
|
||||
DS_MULT
|
||||
ES_IDX_QUEUES
|
||||
ES_AD_IDX_QUEUES
|
||||
READ_PREFETCH
|
||||
BLOCK_PREFETCH
|
||||
INDEX_PREFETCH
|
||||
```
|
||||
|
||||
@@ -1,177 +1,99 @@
|
||||
# Hyperion History API - Private
|
||||
Scalable Full History API Solution for EOSIO based blockchains
|
||||
<!--suppress HtmlUnknownTarget, HtmlDeprecatedAttribute -->
|
||||
<br></br>
|
||||
<p align="center">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="https://eosrio.io/hyperion-white.png">
|
||||
<img alt="Hyperion Logo"
|
||||
src="https://eosrio.io/hyperion.png">
|
||||
</picture>
|
||||
</p>
|
||||
|
||||
Made with ♥ by [EOS Rio](https://eosrio.io/)
|
||||
<h4 align="center">
|
||||
Scalable Full History API Solution for
|
||||
<a href="https://antelope.io">
|
||||
Antelope
|
||||
</a>
|
||||
(former EOSIO) based blockchains
|
||||
</h4>
|
||||
|
||||
### Introducing an storage-optimized action format for EOSIO
|
||||
<br>
|
||||
|
||||
The original *history_plugin* bundled with eosio, that provided the v1 api, stored inline action traces nested inside their root actions. This led to an excessive amount of data being stored and also transferred whenever a user requested the action history for a given account. Also inline actions are used as a "event" mechanism to notify parties on a transaction. Based on those Hyperion implements some changes
|
||||
Made with ♥ by [Rio Blocks / EOS Rio](https://rioblocks.io/?lang=en)
|
||||
|
||||
1. actions are stored in a flattened format
|
||||
2. a parent field is added to the inline actions to point to the parent global sequence
|
||||
3. if the inline action data is identical to the parent it is considered a notification and thus removed from the database
|
||||
4. no blocks or transaction data is stored, all information can be reconstructed from actions
|
||||
### [Official documentation](https://hyperion.docs.eosrio.io)
|
||||
|
||||
With those changes the API format focus on delivering faster search times, lower bandwidth overhead and easier usability for UI/UX developers.
|
||||
### How to use
|
||||
|
||||
#### Action Data Structure (work in progress)
|
||||
- [For Providers](https://hyperion.docs.eosrio.io/manual_installation/)
|
||||
|
||||
- `@timestamp` - block time
|
||||
- `global_sequence` - unique action global_sequence, used as index id
|
||||
- `parent` - points to the parent action (in the case of an inline action) or equal to 0 if root level
|
||||
- `block_num` - block number where the action was processed
|
||||
- `trx_id` - transaction id
|
||||
- `producer` - block producer
|
||||
- `act`
|
||||
- `account` - contract account
|
||||
- `name` - contract method name
|
||||
- `authorization` - array of signers
|
||||
- `actor` - signing actor
|
||||
- `permission` - signing permission
|
||||
- `data` - action data input object
|
||||
- `account_ram_deltas` - array of ram deltas and payers
|
||||
- `account`
|
||||
- `delta`
|
||||
- `notified` - array of accounts that were notified (via inline action events)
|
||||
- [For Developers](https://hyperion.docs.eosrio.io/howtouse/)
|
||||
|
||||
## Dependencies
|
||||
### Official plugins:
|
||||
|
||||
This setup has only been tested with Ubuntu 18.04, but should work with other OS versions too
|
||||
- [Hyperion Lightweight Explorer](https://github.com/eosrio/hyperion-explorer-plugin)
|
||||
|
||||
- [Elasticsearch 7.5.X](https://www.elastic.co/downloads/elasticsearch)
|
||||
- [RabbitMQ](https://www.rabbitmq.com/install-debian.html)
|
||||
- [Redis](https://redis.io/topics/quickstart)
|
||||
- [Node.js v13](https://github.com/nodesource/distributions/blob/master/README.md#installation-instructions)
|
||||
- [PM2](http://pm2.keymetrics.io/docs/usage/quick-start/)
|
||||
- Nodeos w/ state_history_plugin and chain_api_plugin
|
||||
|
||||
> The indexer requires redis, pm2 and node.js to be on the same machine. Other dependencies might be installed on other machines, preferably over a very high speed and low latency network. Indexing speed will vary greatly depending on this configuration.
|
||||
|
||||
## Setup Instructions
|
||||
### 1. Overview
|
||||
|
||||
Install, configure and test all dependencies above before continuing
|
||||
Hyperion is a full history solution for indexing, storing and retrieving Antelope blockchain's historical data.
|
||||
Antelope protocol is highly scalable reaching up to tens of thousands of transactions per second demanding high
|
||||
performance indexing and optimized storage and querying solutions. Hyperion is developed to tackle those challenges
|
||||
providing open source software to be operated by block producers, infrastructure providers and dApp developers.
|
||||
|
||||
Read the step-by-step instructions here - [INSTALL.md](https://github.com/eosrio/Hyperion-History-API/blob/master/INSTALL.md)
|
||||
Focused on delivering faster search times, lower bandwidth overhead and easier usability for UI/UX developers,
|
||||
Hyperion implements an improved data structure. Actions are stored in a flattened format, transaction ids are added to
|
||||
all inline actions, allowing to group by transaction without storing a full transaction index. Besides that if the inline
|
||||
action data is identical to the parent, it is considered a notification and thus removed from the database.
|
||||
No full block or transaction data is stored, all information can be reconstructed from actions and deltas, only a block
|
||||
header index is stored.
|
||||
|
||||
#### 1. Clone & Install packages
|
||||
```bash
|
||||
git clone https://github.com/eosrio/Hyperion-History-API.git
|
||||
cd Hyperion-History-API
|
||||
npm install
|
||||
```
|
||||
### 2. Architecture
|
||||
|
||||
#### 2. Edit configs
|
||||
```
|
||||
cp example-ecosystem.config.js ecosystem.config.js
|
||||
nano ecosystem.config.js
|
||||
The following components are required in order to have a fully functional Hyperion API deployment.
|
||||
* For small use cases, it is absolutely fine to run all components on a single machine.
|
||||
* For larger chains and production environments, we recommend setting them up into different servers under a high-speed local network.
|
||||
|
||||
# Enter connection details here (chain name must match on the ecosystem file)
|
||||
cp example-connections.json connections.json
|
||||
nano connections.json
|
||||
```
|
||||
#### 2.1 Elasticsearch Cluster
|
||||
|
||||
connections.json Reference
|
||||
```
|
||||
{
|
||||
"amqp": {
|
||||
"host": "127.0.0.1:5672", // RabbitMQ Server
|
||||
"api": "127.0.0.1:15672", // RabbitMQ API Endpoint
|
||||
"user": "username",
|
||||
"pass": "password",
|
||||
"vhost": "hyperion" // RabbitMQ vhost
|
||||
},
|
||||
"elasticsearch": {
|
||||
"host": "127.0.0.1:9200", // Elasticsearch HTTP API Endpoint
|
||||
"ingest_nodes": ["127.0.0.1:9200"], // List of ES ingest nodes
|
||||
"user": "elastic",
|
||||
"pass": "password"
|
||||
},
|
||||
"redis": {
|
||||
"host": "127.0.0.1",
|
||||
"port": "6379"
|
||||
},
|
||||
"chains": {
|
||||
"eos": { // Chain name (must match on the ecosystem file)
|
||||
"http": "http://127.0.0.1:8888", // Nodeos Chain API Endpoint
|
||||
"ship": "ws://127.0.0.1:8080" // Nodeos State History Endpoint
|
||||
},
|
||||
"other_chain": {...}
|
||||
}
|
||||
}
|
||||
```
|
||||
The ES cluster is responsible for storing all indexed data.
|
||||
Direct access to the Hyperion API and Indexer must be provided. We recommend nodes in the
|
||||
cluster to have at least 32 GB of RAM and 8 cpu cores. SSD/NVME drives are recommended for
|
||||
maximum indexing throughput, although HDDs can be used for cold storage nodes.
|
||||
For production environments, a multi-node cluster is highly recommended.
|
||||
|
||||
ecosystem.config.js Reference
|
||||
```
|
||||
CHAIN: 'eos', // chain prefix for indexing
|
||||
ABI_CACHE_MODE: 'false', // only cache historical ABIs to redis
|
||||
DEBUG: 'false', // debug mode - display extra logs for debugging
|
||||
LIVE_READER: 'true', // enable continuous reading after reaching the head block
|
||||
FETCH_DELTAS: 'false', // read table deltas
|
||||
CREATE_INDICES: 'v1', // index suffix to be created, set to false to use existing aliases
|
||||
START_ON: 0, // start indexing on block (0=disable)
|
||||
STOP_ON: 0, // stop indexing on block (0=disable)
|
||||
AUTO_STOP: 0, // automatically stop Indexer after X seconds if no more blocks are being processed (0=disable)
|
||||
REWRITE: 'false', // force rewrite the target replay range
|
||||
PURGE_QUEUES: 'false', // clear rabbitmq queues before starting the indexer
|
||||
BATCH_SIZE: 2000, // parallel reader batch size in blocks
|
||||
QUEUE_THRESH: 8000, // queue size limit on rabbitmq
|
||||
LIVE_ONLY: 'false', // only reads realtime data serially
|
||||
FETCH_BLOCK: 'true', // Request full blocks from the state history plugin
|
||||
FETCH_TRACES: 'true', // Request traces from the state history plugin
|
||||
PREVIEW: 'false', // preview mode - prints worker map and exit
|
||||
DISABLE_READING: 'false', // completely disable block reading, for lagged queue processing
|
||||
READERS: 3, // parallel state history readers
|
||||
DESERIALIZERS: 4, // deserialization queues
|
||||
DS_MULT: 4, // deserialization threads per queue
|
||||
ES_IDX_QUEUES: 4, // elastic indexers per queue
|
||||
ES_AD_IDX_QUEUES: 2, // multiplier for action indexing queues
|
||||
READ_PREFETCH: 50, // Stage 1 prefecth size
|
||||
BLOCK_PREFETCH: 5, // Stage 2 prefecth size
|
||||
INDEX_PREFETCH: 500, // Stage 3 prefetch size
|
||||
ENABLE_INDEXING: 'true', // enable elasticsearch indexing
|
||||
INDEX_DELTAS: 'true', // index common table deltas (see delta on definitions/mappings)
|
||||
INDEX_ALL_DELTAS: 'false' // index all table deltas (WARNING)
|
||||
```
|
||||
|
||||
#### 3. Starting
|
||||
|
||||
`./run.sh Indexer`
|
||||
or
|
||||
```
|
||||
pm2 start --only Indexer --update-env
|
||||
pm2 logs Indexer
|
||||
```
|
||||
|
||||
#### 4. Stopping
|
||||
|
||||
Stop reading and wait for queues to flush
|
||||
```
|
||||
pm2 trigger Indexer stop
|
||||
```
|
||||
|
||||
Force stop
|
||||
```
|
||||
pm2 stop Indexer
|
||||
```
|
||||
|
||||
#### 5. Starting the API node
|
||||
|
||||
`./run.sh API`
|
||||
or
|
||||
```
|
||||
pm2 start --only API --update-env
|
||||
pm2 logs API
|
||||
```
|
||||
|
||||
## API Reference
|
||||
#### 2.2 Hyperion Indexer
|
||||
|
||||
Documentation is automatically generated by Swagger/OpenAPI.
|
||||
The Indexer is a Node.js based app that process data from the state history plugin and allows it to be indexed.
|
||||
The [PM2 process manager](https://pm2.keymetrics.io) is used to launch and operate the indexer. The configuration
|
||||
flexibility is very extensive,
|
||||
so system recommendations will depend on the use case and data load. It will require access to at least one ES node,
|
||||
RabbitMQ and the state history node.
|
||||
|
||||
Example: [OpenAPI Docs](https://eos.hyperion.eosrio.io/v2/docs)
|
||||
#### 2.3 Hyperion API
|
||||
|
||||
### Roadmap
|
||||
Parallelizable API server that provides the V2 and V1 (legacy history plugin) endpoints.
|
||||
It is launched by PM2 and can also operate in cluster mode. It requires direct access to
|
||||
at least one ES node for the queries and all other services for full healthcheck
|
||||
|
||||
- Table deltas storage & queries (in progress)
|
||||
- Real-time streaming support (in progress)
|
||||
- Plugin system (in progress)
|
||||
- Control GUI
|
||||
#### 2.4 RabbitMQ
|
||||
|
||||
Used as messaging queue and data transport between the indexer stages and for real-time data streaming
|
||||
|
||||
#### 2.5 Redis
|
||||
|
||||
Used for transient data storage across processes and for the preemptive transaction caching used on
|
||||
the `v2/history/get_transaction` and `v2/history/check_transaction` endpoints
|
||||
|
||||
#### 2.6 Leap State History
|
||||
|
||||
[Leap / Nodeos](https://github.com/AntelopeIO/leap/tree/main/plugins/state_history_plugin) plugin used
|
||||
to collect action traces and state deltas. Provides data via websocket to the indexer
|
||||
|
||||
#### 2.7 Hyperion Stream Client (optional)
|
||||
|
||||
Web and Node.js client for real-time streaming on enabled hyperion
|
||||
providers. [Documentation](https://hyperion.docs.eosrio.io/stream_client/)
|
||||
|
||||
#### 2.8 Hyperion Plugins (optional)
|
||||
|
||||
Hyperion includes a flexible plugin architecture to allow further customization.
|
||||
Plugins are managed by the `hpm` (hyperion plugin manager) command line tool.
|
||||
|
||||
@@ -4,6 +4,7 @@ export function generateOpenApiConfig(config: HyperionConfig) {
|
||||
const packageData = require('../../package');
|
||||
const health_link = `https://${config.api.server_name}/v2/health`;
|
||||
const explorer_link = `https://${config.api.server_name}/v2/explore`;
|
||||
|
||||
let description = `
|
||||
<img height="64" src="https://eosrio.io/hyperion.png">
|
||||
### Scalable Full History API Solution for EOSIO based blockchains
|
||||
@@ -13,9 +14,15 @@ export function generateOpenApiConfig(config: HyperionConfig) {
|
||||
#### Provided by [${config.api.provider_name}](${config.api.provider_url})
|
||||
#### Health API: <a target="_blank" href="${health_link}">${health_link}</a>
|
||||
`;
|
||||
if(config.api.enable_explorer) {
|
||||
description += `#### Integrated Explorer: <a target="_blank" href="${explorer_link}">${explorer_link}</a>`
|
||||
|
||||
if (config.plugins) {
|
||||
if (config.plugins.explorer) {
|
||||
if (config.plugins.explorer.enabled) {
|
||||
description += `#### Integrated Explorer: <a target="_blank" href="${explorer_link}">${explorer_link}</a>`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
routePrefix: '/v2/docs',
|
||||
exposeRoute: true,
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import {HyperionConfig} from "../../interfaces/hyperionConfig";
|
||||
import {createHash} from "crypto";
|
||||
import {FastifyRequest} from "fastify";
|
||||
|
||||
export interface CacheConfig {
|
||||
ttl: number;
|
||||
}
|
||||
|
||||
export interface CachedEntry {
|
||||
data: string;
|
||||
exp: number;
|
||||
}
|
||||
|
||||
export class CacheManager {
|
||||
|
||||
v1CacheConfigs: Map<string, CacheConfig> = new Map<string, CacheConfig>();
|
||||
v1Caches: Map<string, Map<string, CachedEntry>> = new Map<string, Map<string, CachedEntry>>();
|
||||
|
||||
constructor(conf: HyperionConfig) {
|
||||
if (conf.api.v1_chain_cache) {
|
||||
conf.api.v1_chain_cache.forEach(value => {
|
||||
this.v1CacheConfigs.set(value.path, {
|
||||
ttl: value.ttl
|
||||
});
|
||||
this.v1Caches.set(value.path, new Map<string, CachedEntry>());
|
||||
});
|
||||
}
|
||||
setInterval(() => {
|
||||
try {
|
||||
// remove expired entries
|
||||
let removeCount = 0;
|
||||
const now = Date.now();
|
||||
this.v1Caches.forEach((pathCacheMap: Map<string, CachedEntry>, pathKey: string) => {
|
||||
pathCacheMap.forEach((cache: CachedEntry, entryKey: string, map: Map<string, CachedEntry>) => {
|
||||
if (cache.exp < now) {
|
||||
map.delete(entryKey);
|
||||
removeCount++;
|
||||
}
|
||||
});
|
||||
});
|
||||
if (removeCount > 0) {
|
||||
console.log(`${removeCount} expired cache entries removed`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
hashPayload(input: string): string {
|
||||
return createHash('sha256').update(input).digest().toString('hex');
|
||||
}
|
||||
|
||||
setCachedData(hash: string, path: string, payload: any): void {
|
||||
if (this.v1CacheConfigs.has(path) && this.v1Caches.has(path)) {
|
||||
this.v1Caches.get(path).set(hash, {
|
||||
data: payload,
|
||||
exp: this.v1CacheConfigs.get(path).ttl + Date.now()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getCachedData(request: FastifyRequest): [string | null, string, string] {
|
||||
const urlParts = request.url.split("?");
|
||||
const pathComponents = urlParts[0].split('/');
|
||||
const path = pathComponents.at(-1);
|
||||
let payload = '';
|
||||
if (request.method === 'POST') {
|
||||
payload = JSON.stringify(request.body);
|
||||
} else if (request.method === 'GET') {
|
||||
payload = request.url;
|
||||
}
|
||||
const hashedString = this.hashPayload(payload);
|
||||
if (this.v1Caches.has(path)) {
|
||||
const entry = this.v1Caches.get(path).get(hashedString);
|
||||
if (entry && entry.exp > Date.now()) {
|
||||
return [entry.data, hashedString, path];
|
||||
} else {
|
||||
return [null, hashedString, path];
|
||||
}
|
||||
} else {
|
||||
return [null, hashedString, path];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+297
-20
@@ -1,12 +1,13 @@
|
||||
import {createHash} from "crypto";
|
||||
import * as _ from "lodash";
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest, HTTPMethod, RouteSchema} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest, FastifySchema, HTTPMethods} from "fastify";
|
||||
import got from "got";
|
||||
|
||||
export function extendResponseSchema(responseProps: any) {
|
||||
const props = {
|
||||
query_time_ms: {type: "number"},
|
||||
cached: {type: "boolean"},
|
||||
hot_only: {type: "boolean"},
|
||||
lib: {type: "number"},
|
||||
total: {
|
||||
type: "object",
|
||||
@@ -29,7 +30,7 @@ export function extendResponseSchema(responseProps: any) {
|
||||
};
|
||||
}
|
||||
|
||||
export function extendQueryStringSchema(queryParams: any) {
|
||||
export function extendQueryStringSchema(queryParams: any, required?: string[]) {
|
||||
const params = {
|
||||
limit: {
|
||||
description: 'limit of [n] results per page',
|
||||
@@ -47,10 +48,14 @@ export function extendQueryStringSchema(queryParams: any) {
|
||||
params[p] = queryParams[p];
|
||||
}
|
||||
}
|
||||
return {
|
||||
const schema = {
|
||||
type: 'object',
|
||||
properties: params
|
||||
}
|
||||
if (required && required.length > 0) {
|
||||
schema["required"] = required;
|
||||
}
|
||||
return schema;
|
||||
}
|
||||
|
||||
export async function getCacheByHash(redis, key, chain) {
|
||||
@@ -80,14 +85,15 @@ export function mergeDeltaMeta(delta: any) {
|
||||
return delta;
|
||||
}
|
||||
|
||||
export function setCacheByHash(fastify, hash, response) {
|
||||
export function setCacheByHash(fastify, hash, response, expiration?: number) {
|
||||
if (fastify.manager.config.api.enable_caching) {
|
||||
fastify.redis
|
||||
.set(hash,
|
||||
JSON.stringify(response),
|
||||
'EX',
|
||||
fastify.manager.config.api.cache_life)
|
||||
.catch(console.log);
|
||||
let exp;
|
||||
if (expiration) {
|
||||
exp = expiration;
|
||||
} else {
|
||||
exp = fastify.manager.config.api.cache_life;
|
||||
}
|
||||
fastify.redis.set(hash, JSON.stringify(response), 'EX', exp).catch(console.log);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,9 +104,11 @@ export function getRouteName(filename: string) {
|
||||
|
||||
export function addApiRoute(
|
||||
fastifyInstance: FastifyInstance,
|
||||
method: HTTPMethod | HTTPMethod[],
|
||||
method: HTTPMethods | HTTPMethods[],
|
||||
routeName: string,
|
||||
routeBuilder: (fastify: FastifyInstance, route: string) => (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => Promise<void>, schema: RouteSchema) {
|
||||
routeBuilder: (fastify: FastifyInstance, route: string) => (request: FastifyRequest, reply: FastifyReply) => Promise<void>,
|
||||
schema: FastifySchema
|
||||
) {
|
||||
fastifyInstance.route({
|
||||
url: '/' + routeName,
|
||||
method,
|
||||
@@ -113,8 +121,7 @@ export async function getCachedResponse(server: FastifyInstance, route: string,
|
||||
const chain = server.manager.chain;
|
||||
let resp, hash;
|
||||
if (server.manager.config.api.enable_caching) {
|
||||
const keystring = JSON.stringify(key);
|
||||
[resp, hash] = await getCacheByHash(server.redis, route + keystring, chain);
|
||||
[resp, hash] = await getCacheByHash(server.redis, route + JSON.stringify(key), chain);
|
||||
if (resp) {
|
||||
resp = JSON.parse(resp);
|
||||
resp['cached'] = true;
|
||||
@@ -135,8 +142,10 @@ export function getTrackTotalHits(query) {
|
||||
} else if (query.track === 'false') {
|
||||
trackTotalHits = false;
|
||||
} else {
|
||||
trackTotalHits = parseInt(query.track, 10);
|
||||
if (isNaN(trackTotalHits)) {
|
||||
const parsed = parseInt(query.track, 10);
|
||||
if (parsed > 0) {
|
||||
trackTotalHits = parsed;
|
||||
} else {
|
||||
throw new Error('failed to parse track param');
|
||||
}
|
||||
}
|
||||
@@ -148,6 +157,11 @@ function bigint2Milliseconds(input: bigint) {
|
||||
return parseFloat((parseInt(input.toString()) / 1000000).toFixed(3));
|
||||
}
|
||||
|
||||
const defaultRouteCacheMap = {
|
||||
get_resource_usage: 3600,
|
||||
get_creator: 3600 * 24
|
||||
}
|
||||
|
||||
export async function timedQuery(
|
||||
queryFunction: (fastify: FastifyInstance, request: FastifyRequest) => Promise<any>,
|
||||
fastify: FastifyInstance, request: FastifyRequest, route: string): Promise<any> {
|
||||
@@ -156,9 +170,13 @@ export async function timedQuery(
|
||||
const t0 = process.hrtime.bigint();
|
||||
|
||||
// check for cached data, return the response hash if caching is enabled
|
||||
const [cachedResponse, hash] = await getCachedResponse(fastify, route, request.query);
|
||||
const [cachedResponse, hash] = await getCachedResponse(
|
||||
fastify,
|
||||
route,
|
||||
request.method === 'POST' ? request.body : request.query
|
||||
);
|
||||
|
||||
if (cachedResponse) {
|
||||
if (cachedResponse && !request.query["ignoreCache"]) {
|
||||
// add cached query time
|
||||
cachedResponse['query_time_ms'] = bigint2Milliseconds(process.hrtime.bigint() - t0);
|
||||
return cachedResponse;
|
||||
@@ -169,7 +187,11 @@ export async function timedQuery(
|
||||
|
||||
// save response to cash
|
||||
if (hash) {
|
||||
setCacheByHash(fastify, hash, response);
|
||||
let EX = null;
|
||||
if (defaultRouteCacheMap[route]) {
|
||||
EX = defaultRouteCacheMap[route];
|
||||
}
|
||||
setCacheByHash(fastify, hash, response, EX);
|
||||
}
|
||||
|
||||
// add normal query time
|
||||
@@ -180,3 +202,258 @@ export async function timedQuery(
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export function chainApiHandler(fastify: FastifyInstance) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
// check cache
|
||||
const [cachedData, hash, path] = fastify.cacheManager.getCachedData(request);
|
||||
if (cachedData) {
|
||||
// console.log('cache hit:', path, hash);
|
||||
reply.headers({'hyperion-cached': true}).send(cachedData);
|
||||
} else {
|
||||
// call actual request
|
||||
const apiResponse = await handleChainApiRedirect(request, reply, fastify);
|
||||
// console.log('cache miss:', path, hash);
|
||||
fastify.cacheManager.setCachedData(hash, path, apiResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleChainApiRedirect(
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
fastify: FastifyInstance
|
||||
): Promise<string> {
|
||||
const urlParts = request.url.split("?");
|
||||
let reqUrl = fastify.chain_api + urlParts[0];
|
||||
|
||||
// const pathComponents = urlParts[0].split('/');
|
||||
// const path = pathComponents.at(-1);
|
||||
|
||||
|
||||
if (urlParts[0] === '/v1/chain/push_transaction' && fastify.push_api && fastify.push_api !== "") {
|
||||
reqUrl = fastify.push_api + urlParts[0];
|
||||
}
|
||||
|
||||
const opts = {};
|
||||
|
||||
if (request.method === 'POST') {
|
||||
if (request.body) {
|
||||
if (typeof request.body === 'string') {
|
||||
opts['body'] = request.body;
|
||||
} else if (typeof request.body === 'object') {
|
||||
opts['body'] = JSON.stringify(request.body);
|
||||
}
|
||||
} else {
|
||||
opts['body'] = "";
|
||||
}
|
||||
} else if (request.method === 'GET') {
|
||||
opts['json'] = request.query;
|
||||
}
|
||||
|
||||
try {
|
||||
const apiResponse = await got.post(reqUrl, opts);
|
||||
reply.headers({"Content-Type": "application/json"});
|
||||
if (request.method === 'HEAD') {
|
||||
reply.headers({"Content-Length": apiResponse.body.length});
|
||||
reply.send("");
|
||||
return '';
|
||||
} else {
|
||||
reply.send(apiResponse.body);
|
||||
return apiResponse.body;
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
if (error.response) {
|
||||
reply.status(error.response.statusCode).send(error.response.body);
|
||||
} else {
|
||||
console.log(error);
|
||||
reply.status(500).send();
|
||||
}
|
||||
|
||||
if (fastify.manager.config.api.chain_api_error_log) {
|
||||
try {
|
||||
if (error.response) {
|
||||
const error_msg = JSON.parse(error.response.body).error.details[0].message;
|
||||
console.log(`endpoint: ${request.raw.url} | status: ${error.response.statusCode} | error: ${error_msg}`);
|
||||
} else {
|
||||
console.log(error);
|
||||
}
|
||||
// if (request.req.url === '/v1/chain/push_transaction') {
|
||||
// const packedTrx = JSON.parse(opts['body']).packed_trx;
|
||||
// const trxBuffer = Buffer.from(packedTrx, 'hex');
|
||||
// const trxData = await fastify.eosjs.api.deserializeTransactionWithActions(trxBuffer);
|
||||
// console.log(trxData);
|
||||
// }
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
export function addChainApiRoute(fastify: FastifyInstance, routeName, description, props?, required?) {
|
||||
const baseSchema = {
|
||||
description: description,
|
||||
summary: description,
|
||||
tags: ['chain']
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
['GET', 'HEAD'],
|
||||
routeName,
|
||||
chainApiHandler,
|
||||
{
|
||||
...baseSchema,
|
||||
querystring: props ? {
|
||||
type: 'object',
|
||||
properties: props,
|
||||
required: required
|
||||
} : undefined
|
||||
}
|
||||
);
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'POST',
|
||||
routeName,
|
||||
chainApiHandler,
|
||||
{
|
||||
...baseSchema,
|
||||
body: props ? {
|
||||
type: ['object', 'string'],
|
||||
properties: props,
|
||||
required: required
|
||||
} : undefined
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export function addSharedSchemas(fastify: FastifyInstance) {
|
||||
fastify.addSchema({
|
||||
$id: "WholeNumber",
|
||||
title: "Integer",
|
||||
description: "Integer or String",
|
||||
anyOf: [
|
||||
{
|
||||
type: "string",
|
||||
pattern: "^\\d+$"
|
||||
},
|
||||
{
|
||||
type: "integer"
|
||||
}
|
||||
],
|
||||
});
|
||||
|
||||
fastify.addSchema({
|
||||
$id: "Symbol",
|
||||
type: "string",
|
||||
description: "A symbol composed of capital letters between 1-7.",
|
||||
pattern: "^([A-Z]{1,7})$",
|
||||
title: "Symbol"
|
||||
});
|
||||
|
||||
fastify.addSchema({
|
||||
$id: "Signature",
|
||||
type: "string",
|
||||
description: "String representation of an EOSIO compatible cryptographic signature",
|
||||
pattern: "^SIG_([RK]1|WA)_[1-9A-HJ-NP-Za-km-z]+$",
|
||||
title: "Signature"
|
||||
})
|
||||
|
||||
fastify.addSchema({
|
||||
$id: "AccountName",
|
||||
description: "String representation of an EOSIO compatible account name",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "String representation of privileged EOSIO name type",
|
||||
"pattern": "^(eosio[\\.][a-z1-5]{1,6})([a-j]{1})?$",
|
||||
"title": "NamePrivileged"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "String representation of basic EOSIO name type, must be 12 characters and contain only a-z and 0-5",
|
||||
"pattern": "^([a-z]{1}[a-z1-5]{11})([a-j]{1})?$",
|
||||
"title": "NameBasic"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "String representation of EOSIO bid name type, 1-12 characters and only a-z and 0-5 are allowed",
|
||||
"pattern": "^([a-z1-5]{1,12})([a-j]{1})?$",
|
||||
"title": "NameBid"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "String representation of EOSIO name type",
|
||||
"pattern": "^([a-z1-5]{1}[a-z1-5\\.]{0,10}[a-z1-5]{1})([a-j]{1})?$",
|
||||
"title": "NameCatchAll"
|
||||
}
|
||||
],
|
||||
"title": "Name"
|
||||
});
|
||||
|
||||
fastify.addSchema({
|
||||
$id: "Expiration",
|
||||
"description": "Time that transaction must be confirmed by.",
|
||||
"type": "string",
|
||||
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$",
|
||||
"title": "DateTime"
|
||||
});
|
||||
|
||||
fastify.addSchema({
|
||||
$id: "BlockExtensions",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"anyOf": [
|
||||
{"type": "integer"},
|
||||
{"type": "string"}
|
||||
]
|
||||
},
|
||||
"title": "Extension"
|
||||
})
|
||||
|
||||
fastify.addSchema({
|
||||
$id: "ActionItems",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"minProperties": 5,
|
||||
"required": [
|
||||
"account",
|
||||
"name",
|
||||
"authorization",
|
||||
"data",
|
||||
"hex_data"
|
||||
],
|
||||
"properties": {
|
||||
"account": {$ref: 'AccountName#'},
|
||||
"name": {$ref: 'AccountName#'},
|
||||
"authorization": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"minProperties": 2,
|
||||
"required": [
|
||||
"actor",
|
||||
"permission"
|
||||
],
|
||||
"properties": {
|
||||
"actor": {$ref: 'AccountName#'},
|
||||
"permission": {$ref: 'AccountName#'}
|
||||
},
|
||||
"title": "Authority"
|
||||
}
|
||||
},
|
||||
"data": {
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
},
|
||||
"hex_data": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"title": "Action"
|
||||
});
|
||||
}
|
||||
|
||||
+22
-12
@@ -2,22 +2,32 @@ import * as Fastify from "fastify";
|
||||
import {IncomingMessage, Server, ServerResponse} from "http";
|
||||
|
||||
// fastify plugins
|
||||
import * as fastify_elasticsearch from 'fastify-elasticsearch';
|
||||
import * as fastify_oas from 'fastify-oas';
|
||||
import * as fastify_cors from 'fastify-cors';
|
||||
import * as fastify_formbody from 'fastify-formbody';
|
||||
import * as fastify_redis from 'fastify-redis';
|
||||
import * as fastify_rate_limit from 'fastify-rate-limit';
|
||||
import * as fastifyElasticsearch from 'fastify-elasticsearch';
|
||||
import fastifySwagger from '@fastify/swagger';
|
||||
import fastifyCors from 'fastify-cors';
|
||||
import formBodyPlugin from 'fastify-formbody';
|
||||
import fastifyRedis from 'fastify-redis';
|
||||
import fastifyRateLimit from 'fastify-rate-limit';
|
||||
|
||||
// custom plugins
|
||||
import fastify_eosjs from "./plugins/fastify-eosjs";
|
||||
|
||||
export function registerPlugins(server: Fastify.FastifyInstance<Server, IncomingMessage, ServerResponse>, params: any) {
|
||||
server.register(fastify_elasticsearch, params.fastify_elasticsearch);
|
||||
server.register(fastify_oas, params.fastify_oas);
|
||||
server.register(fastify_cors);
|
||||
server.register(fastify_formbody);
|
||||
server.register(fastify_redis, params.fastify_redis);
|
||||
server.register(fastifyElasticsearch, params.fastify_elasticsearch);
|
||||
|
||||
if (params.fastify_swagger) {
|
||||
server.register(fastifySwagger, params.fastify_swagger);
|
||||
}
|
||||
|
||||
server.register(fastifyCors);
|
||||
|
||||
server.register(formBodyPlugin);
|
||||
|
||||
server.register(fastifyRedis, params.fastify_redis);
|
||||
|
||||
if (params.fastify_rate_limit) {
|
||||
server.register(fastifyRateLimit, params.fastify_rate_limit);
|
||||
}
|
||||
|
||||
server.register(fastify_eosjs, params.fastify_eosjs);
|
||||
server.register(fastify_rate_limit, params.fastify_rate_limit);
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
const fp = require('fastify-plugin');
|
||||
const {Api} = require("eosjs");
|
||||
const {ConnectionManager} = require('../../connections/manager');
|
||||
const manager = new ConnectionManager();
|
||||
|
||||
module.exports = fp(async (fastify, options, next) => {
|
||||
const rpc = manager.nodeosJsonRPC;
|
||||
const chain_data = await rpc.get_info();
|
||||
const api = new Api({
|
||||
rpc,
|
||||
signatureProvider: null,
|
||||
chainId: chain_data.chain_id,
|
||||
textDecoder: new TextDecoder(),
|
||||
textEncoder: new TextEncoder(),
|
||||
});
|
||||
fastify.decorate('eosjs', {api, rpc});
|
||||
next();
|
||||
}, {
|
||||
fastify: '>=2.0.0',
|
||||
name: 'fastify-eosjs'
|
||||
});
|
||||
@@ -1,20 +1,19 @@
|
||||
import * as fp from 'fastify-plugin';
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {FastifyInstance, FastifyPluginOptions} from "fastify";
|
||||
import {Api} from "eosjs/dist";
|
||||
import fp from "fastify-plugin";
|
||||
|
||||
export default fp(async (fastify: FastifyInstance, options, next) => {
|
||||
const rpc = fastify.manager.nodeosJsonRPC;
|
||||
const chain_data = await rpc.get_info();
|
||||
const api = new Api({
|
||||
rpc,
|
||||
signatureProvider: null,
|
||||
chainId: chain_data.chain_id,
|
||||
textDecoder: new TextDecoder(),
|
||||
textEncoder: new TextEncoder(),
|
||||
});
|
||||
fastify.decorate('eosjs', {api, rpc});
|
||||
next();
|
||||
export default fp(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => {
|
||||
const rpc = fastify.manager.nodeosJsonRPC;
|
||||
const chain_data = await rpc.get_info();
|
||||
const api = new Api({
|
||||
rpc,
|
||||
signatureProvider: null,
|
||||
chainId: chain_data.chain_id,
|
||||
textDecoder: new TextDecoder(),
|
||||
textEncoder: new TextEncoder(),
|
||||
});
|
||||
fastify.decorate('eosjs', {api, rpc});
|
||||
}, {
|
||||
fastify: '>=2.0.0',
|
||||
name: 'fastify-eosjs'
|
||||
fastify: '>=2.0.0',
|
||||
name: 'fastify-eosjs'
|
||||
});
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
const fp = require('fastify-plugin');
|
||||
const WebSocket = require('ws');
|
||||
|
||||
module.exports = fp((fastify, opts, next) => {
|
||||
|
||||
const wss = new WebSocket.Server({
|
||||
server: fastify.server
|
||||
});
|
||||
|
||||
const ws = new WebSocket('ws://127.0.0.1:7001', [], {
|
||||
perMessageDeflate: false
|
||||
});
|
||||
|
||||
ws.on('open', function open() {
|
||||
console.log('router connected');
|
||||
});
|
||||
|
||||
fastify.decorate('wss', wss);
|
||||
fastify.decorate('ws', ws);
|
||||
|
||||
fastify.addHook('onClose', (fastify, done) => {
|
||||
fastify.ws.close(done);
|
||||
fastify.wss.close(done);
|
||||
});
|
||||
|
||||
next();
|
||||
}, {
|
||||
fastify: '2.x',
|
||||
name: 'fastify-websocket'
|
||||
});
|
||||
+86
-21
@@ -1,56 +1,121 @@
|
||||
import * as fastify_static from "fastify-static";
|
||||
import {join} from "path";
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {FastifyError, FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {createReadStream} from "fs";
|
||||
import * as AutoLoad from "fastify-autoload";
|
||||
import {addSharedSchemas, handleChainApiRedirect} from "./helpers/functions";
|
||||
import autoLoad from 'fastify-autoload';
|
||||
import got from "got";
|
||||
|
||||
function addRedirect(server: FastifyInstance, url: string, redirectTo: string) {
|
||||
server.route({
|
||||
url,
|
||||
method: 'GET',
|
||||
schema: {hide: true},
|
||||
handler: async (request, reply) => {
|
||||
schema: {
|
||||
hide: true
|
||||
},
|
||||
handler: async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.redirect(redirectTo);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function addRoute(server: FastifyInstance, handlersPath: string, prefix: string) {
|
||||
server.register(AutoLoad, {
|
||||
server.register(autoLoad, {
|
||||
dir: join(__dirname, 'routes', handlersPath),
|
||||
ignorePattern: /.*(handler|schema).js/,
|
||||
dirNameRoutePrefix: false,
|
||||
options: {prefix}
|
||||
});
|
||||
}
|
||||
|
||||
export function registerRoutes(server: FastifyInstance) {
|
||||
|
||||
// build internal map of routes
|
||||
const routeSet = new Set<string>();
|
||||
server.decorate('routeSet', routeSet);
|
||||
const ignoreList = [
|
||||
'/v2',
|
||||
'/v2/history',
|
||||
'/v2/state',
|
||||
'/v1/chain/*',
|
||||
'/v1/chain'
|
||||
];
|
||||
server.addHook('onRoute', opts => {
|
||||
if (!ignoreList.includes(opts.url)) {
|
||||
if (opts.url.startsWith('/v')) {
|
||||
routeSet.add(opts.url);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Register fastify api routes
|
||||
addRoute(server, 'v2', '/v2');
|
||||
addRoute(server, 'v2-history', '/v2/history');
|
||||
addRoute(server, 'v2-state', '/v2/state');
|
||||
addRoute(server, 'v2-stats', '/v2/stats');
|
||||
|
||||
// legacy routes
|
||||
addRoute(server, 'v1-history', '/v1/history');
|
||||
// addRoute(server,'v1-chain', '/v1/chain');
|
||||
addRoute(server, 'v1-trace', '/v1/trace_api');
|
||||
|
||||
// Serve integrated explorer
|
||||
if(server.manager.config.api.enable_explorer) {
|
||||
server.register(fastify_static, {
|
||||
root: join(__dirname, '..', 'hyperion-explorer', 'dist'),
|
||||
redirect: true,
|
||||
wildcard: true,
|
||||
prefix: '/v2/explore'
|
||||
});
|
||||
}
|
||||
addSharedSchemas(server);
|
||||
|
||||
// steam client lib
|
||||
server.get('/stream-client.js', (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
const stream = createReadStream('./client_bundle.js');
|
||||
reply.type('application/javascript').send(stream);
|
||||
// chain api redirects
|
||||
addRoute(server, 'v1-chain', '/v1/chain');
|
||||
|
||||
// other v1 requests
|
||||
server.route({
|
||||
url: '/v1/chain/*',
|
||||
method: ["GET", "POST"],
|
||||
schema: {
|
||||
summary: "Wildcard chain api handler",
|
||||
tags: ["chain"]
|
||||
},
|
||||
handler: async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
await handleChainApiRedirect(request, reply, server);
|
||||
}
|
||||
});
|
||||
|
||||
// /v1/node/get_supported_apis
|
||||
server.route({
|
||||
url: '/v1/node/get_supported_apis',
|
||||
method: ["GET"],
|
||||
schema: {
|
||||
summary: "Get list of supported APIs",
|
||||
tags: ["node"]
|
||||
},
|
||||
handler: async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const data = await got.get(`${server.chain_api}/v1/node/get_supported_apis`).json() as any;
|
||||
if (data.apis && data.apis.length > 0) {
|
||||
const apiSet = new Set(server.routeSet);
|
||||
data.apis.forEach((a) => apiSet.add(a));
|
||||
reply.send({apis: [...apiSet]});
|
||||
} else {
|
||||
reply.send({apis: [...server.routeSet], error: 'nodeos did not send any data'});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
server.addHook('onError', (request: FastifyRequest, reply: FastifyReply, error: FastifyError, done) => {
|
||||
console.log(`[${request.headers['x-real-ip'] || request.ip}] ${request.method} ${request.url} failed >> ${error.message}`);
|
||||
done();
|
||||
});
|
||||
|
||||
if (server.manager.config.features.streaming) {
|
||||
// steam client lib
|
||||
server.get(
|
||||
'/stream-client.js',
|
||||
{schema: {tags: ['internal']}},
|
||||
(request: FastifyRequest, reply: FastifyReply) => {
|
||||
const stream = createReadStream('./hyperion-stream-client.js');
|
||||
reply.type('application/javascript').send(stream);
|
||||
});
|
||||
}
|
||||
|
||||
// Redirect routes to documentation
|
||||
addRedirect(server, '/v2', '/v2/docs');
|
||||
addRedirect(server, '/v2/history', '/v2/docs/index.html#/history');
|
||||
addRedirect(server, '/v2/state', '/v2/docs/index.html#/state');
|
||||
addRedirect(server, '/v1/chain', '/v2/docs/index.html#/chain');
|
||||
addRedirect(server, '/explorer', '/v2/explore');
|
||||
addRedirect(server, '/explore', '/v2/explore');
|
||||
}
|
||||
|
||||
@@ -1,227 +0,0 @@
|
||||
const async = require('async');
|
||||
const {Serialize} = require('eosjs');
|
||||
const {ConnectionManager} = require('../../connections/manager');
|
||||
let abi, types;
|
||||
let connected = false;
|
||||
let tables = new Map();
|
||||
const textEncoder = new TextEncoder();
|
||||
const textDecoder = new TextDecoder();
|
||||
const ws_opts = {compress: true, binary: true, mask: true, fin: true};
|
||||
const maxMessagesInFlight = 1;
|
||||
const blockReadingQueue = async.cargo(async.ensureAsync(processIncomingBlockArray), maxMessagesInFlight);
|
||||
|
||||
class BlockStore {
|
||||
localBlocks;
|
||||
pendingBlocks;
|
||||
pendingRequests;
|
||||
constructor() {
|
||||
this.pendingRequests = [];
|
||||
this.localBlocks = new Map();
|
||||
this.pendingBlocks = new Map();
|
||||
}
|
||||
|
||||
addBlock(num, data) {
|
||||
if (this.pendingBlocks.has(num)) {
|
||||
this.localBlocks.set(num, data);
|
||||
this.pendingBlocks.get(num)['callback']();
|
||||
this.pendingBlocks.delete(num);
|
||||
}
|
||||
}
|
||||
|
||||
requestRange(from_block, to_block, onBlock, onCompletion) {
|
||||
// console.log(`Requesting blocks from ${from_block} to ${to_block}`);
|
||||
const range = to_block - from_block;
|
||||
// console.log(`Range size: ${range}`);
|
||||
let requested = range;
|
||||
for (let i = 0; i < range; i++) {
|
||||
const blk_num = from_block + i;
|
||||
// console.log('Looking on local cache for block ' + blk_num);
|
||||
if (this.localBlocks.has(blk_num)) {
|
||||
onBlock(this.localBlocks.get(blk_num), true);
|
||||
requested--;
|
||||
} else {
|
||||
this.pendingBlocks.set(blk_num, {
|
||||
callback: () => {
|
||||
onBlock(this.localBlocks.get(blk_num), false);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (requested > 0) {
|
||||
requestBlockRange(from_block, to_block);
|
||||
const checker = setInterval(() => {
|
||||
if (this.pendingBlocks.size === 0) {
|
||||
onCompletion();
|
||||
clearInterval(checker);
|
||||
}
|
||||
}, 30);
|
||||
} else {
|
||||
onCompletion();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const blockStore = new BlockStore();
|
||||
|
||||
const baseRequest = {
|
||||
max_messages_in_flight: maxMessagesInFlight,
|
||||
have_positions: [],
|
||||
irreversible_only: false,
|
||||
fetch_block: true,
|
||||
fetch_traces: true,
|
||||
fetch_deltas: false
|
||||
};
|
||||
|
||||
const manager = new ConnectionManager();
|
||||
const shipClient = manager.shipClient;
|
||||
startWS();
|
||||
|
||||
function createSerialBuffer(array_data) {
|
||||
return new Serialize.SerialBuffer({textEncoder, textDecoder, array: array_data});
|
||||
}
|
||||
|
||||
|
||||
function serialize(type, value) {
|
||||
const buffer = createSerialBuffer();
|
||||
Serialize.getType(types, type).serialize(buffer, value);
|
||||
return buffer.asUint8Array();
|
||||
}
|
||||
|
||||
function deserialize(type, array) {
|
||||
const buffer = new Serialize.SerialBuffer({textEncoder, textDecoder, array});
|
||||
const sState = new Serialize.SerializerState({bytesAsUint8Array: true});
|
||||
return Serialize.getType(types, type).deserialize(buffer, sState);
|
||||
}
|
||||
|
||||
function processIncomingBlockArray(payload, cb) {
|
||||
processIncomingBlocks(payload).then(() => {
|
||||
cb();
|
||||
}).catch((err) => {
|
||||
console.log('FATAL ERROR READING BLOCKS', err);
|
||||
process.exit(1);
|
||||
})
|
||||
}
|
||||
|
||||
function handleLostConnection() {
|
||||
console.log('connection lost');
|
||||
connected = false;
|
||||
startWS();
|
||||
}
|
||||
|
||||
function ackBlocks(num) {
|
||||
shipClient.send(serialize('request',
|
||||
['get_blocks_ack_request_v0', {num_messages: num}]
|
||||
), ws_opts, (err) => {
|
||||
if (err) console.warn(err);
|
||||
});
|
||||
}
|
||||
|
||||
async function processIncomingBlocks(block_array) {
|
||||
if (abi) {
|
||||
ackBlocks(block_array.length);
|
||||
for (const block of block_array) {
|
||||
try {
|
||||
await onMessage(block);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
await onMessage(block_array[0]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function processFirstABI(data) {
|
||||
abi = JSON.parse(data);
|
||||
types = Serialize.getTypesFromAbi(Serialize.createInitialTypes(), abi);
|
||||
abi.tables.map(table => tables.set(table.name, table.type));
|
||||
}
|
||||
|
||||
function deserializeType(obj) {
|
||||
return deserialize(obj[0], obj[1], textEncoder, textDecoder, types);
|
||||
}
|
||||
|
||||
function onMessage(data) {
|
||||
if (abi) {
|
||||
const res = deserialize('result', data)[1];
|
||||
if (res['this_block']) {
|
||||
const blk_num = res['this_block']['block_num'];
|
||||
const block_data = {
|
||||
block: null,
|
||||
traces: [],
|
||||
deltas: []
|
||||
};
|
||||
if (res.block && res.block.length) {
|
||||
block_data.block = deserialize('signed_block', res.block, textEncoder, textDecoder, types);
|
||||
if (block_data.block === null) {
|
||||
console.log(res);
|
||||
} else {
|
||||
block_data.block.id = res['this_block']['block_id'];
|
||||
block_data.block.block_num = blk_num;
|
||||
block_data.block.transactions = [];
|
||||
|
||||
if (res['traces'] && res['traces'].length) {
|
||||
const trx_traces = deserialize('transaction_trace[]', res['traces'], textEncoder, textDecoder, types).map(trace => trace[1]);
|
||||
for (const trace of trx_traces) {
|
||||
trace.partial = trace.partial[1];
|
||||
trace.actions = [];
|
||||
for (const action_trace of trace.action_traces) {
|
||||
// console.log(action_trace);
|
||||
// action_trace.receipt = action_trace.receipt[1];
|
||||
const action = action_trace[1];
|
||||
action.receipt = action.receipt[1];
|
||||
action['act']['data'] = Buffer.from(action['act']['data']).toString('hex');
|
||||
trace.actions.push(action);
|
||||
}
|
||||
delete trace.action_traces;
|
||||
if (trace.actions[0]['act']['account'] === 'eosio' && trace.actions[0]['act']['name'] === 'onblock') {
|
||||
// ingore onblock
|
||||
} else {
|
||||
block_data.block.transactions.push(trace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// if (res['deltas'] && res['deltas'].length) {
|
||||
// block_data.deltas = deserialize('table_delta[]', res['deltas'], textEncoder, textDecoder, types);
|
||||
// }
|
||||
blockStore.addBlock(blk_num, block_data.block);
|
||||
}
|
||||
} else {
|
||||
processFirstABI(data);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
function send(req_data) {
|
||||
shipClient.send(serialize('request', req_data, textEncoder, textDecoder, types));
|
||||
}
|
||||
|
||||
function requestBlockRange(start, finish) {
|
||||
const request = baseRequest;
|
||||
request.start_block_num = start;
|
||||
request.end_block_num = finish;
|
||||
// console.log(request);
|
||||
if (connected) {
|
||||
send(['get_blocks_request_v0', request]);
|
||||
} else {
|
||||
startWS(() => {
|
||||
send(['get_blocks_request_v0', request]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function startWS(onConnect) {
|
||||
if (onConnect) {
|
||||
shipClient.connect(blockReadingQueue.push, handleLostConnection, null, onConnect);
|
||||
} else {
|
||||
shipClient.connect(blockReadingQueue.push, handleLostConnection);
|
||||
}
|
||||
connected = true;
|
||||
}
|
||||
|
||||
module.exports = {blockStore};
|
||||
@@ -1,72 +0,0 @@
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
const {getAbiSnapshotSchema} = require("../../schemas");
|
||||
|
||||
const enable_caching = process.env.ENABLE_CACHING === 'true';
|
||||
let cache_life = 30;
|
||||
if (process.env.CACHE_LIFE) {
|
||||
cache_life = parseInt(process.env.CACHE_LIFE);
|
||||
}
|
||||
|
||||
async function getAbiSnapshot(fastify, request) {
|
||||
const t0 = Date.now();
|
||||
const {redis, elastic} = fastify;
|
||||
const [cachedResponse, hash] = await getCacheByHash(redis, JSON.stringify(request.query));
|
||||
if (cachedResponse) {
|
||||
return JSON.parse(cachedResponse);
|
||||
}
|
||||
|
||||
const response = {
|
||||
query_time: 0,
|
||||
block_num: null
|
||||
};
|
||||
|
||||
const code = request.query.contract;
|
||||
const block = request.query.block;
|
||||
const should_fetch = request.query.fetch;
|
||||
|
||||
if (should_fetch) {
|
||||
console.log(should_fetch);
|
||||
}
|
||||
|
||||
const mustArray = [];
|
||||
|
||||
mustArray.push({"term": {"account": code}});
|
||||
|
||||
if (block) {
|
||||
mustArray.push({"range": {"block": {"lte": parseInt(block)}}});
|
||||
}
|
||||
|
||||
const results = await elastic.search({
|
||||
index: process.env.CHAIN + '-abi',
|
||||
size: 1,
|
||||
body: {
|
||||
query: {bool: {must: mustArray}},
|
||||
sort: [{block: {order: "desc"}}]
|
||||
}
|
||||
});
|
||||
if (results['body']['hits']['hits'].length > 0) {
|
||||
if (should_fetch) {
|
||||
response['abi'] = JSON.parse(results['body']['hits']['hits'][0]['_source']['abi']);
|
||||
} else {
|
||||
response['present'] = true;
|
||||
}
|
||||
response.block_num = results['body']['hits']['hits'][0]['_source']['block'];
|
||||
} else {
|
||||
response['present'] = false;
|
||||
response['error'] = 'abi not found for ' + code + ' until block ' + block;
|
||||
}
|
||||
response['query_time'] = Date.now() - t0;
|
||||
if (enable_caching) {
|
||||
redis.set(hash, JSON.stringify(response), 'EX', cache_life);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get('/get_abi_snapshot', {
|
||||
schema: getAbiSnapshotSchema.GET
|
||||
}, async (request, reply) => {
|
||||
reply.send(await getAbiSnapshot(fastify, request));
|
||||
});
|
||||
next()
|
||||
};
|
||||
@@ -1,378 +0,0 @@
|
||||
const prettyjson = require("prettyjson");
|
||||
const {getActionsSchema} = require("../../schemas");
|
||||
const {getCacheByHash, mergeActionMeta} = require("../../helpers/functions");
|
||||
|
||||
const maxActions = 1000;
|
||||
const route = '/get_actions';
|
||||
|
||||
const terms = [
|
||||
"notified.keyword",
|
||||
"act.authorization.actor"
|
||||
];
|
||||
|
||||
const extendedActions = new Set([
|
||||
"transfer",
|
||||
"newaccount",
|
||||
"updateauth",
|
||||
"buyram",
|
||||
"buyrambytes"
|
||||
]);
|
||||
|
||||
const primaryTerms = [
|
||||
"notified",
|
||||
"block_num",
|
||||
"global_sequence",
|
||||
"producer",
|
||||
"@timestamp",
|
||||
"creator_action_ordinal",
|
||||
"action_ordinal",
|
||||
"cpu_usage_us",
|
||||
"net_usage_words"
|
||||
];
|
||||
|
||||
const enable_caching = process.env.ENABLE_CACHING === 'true';
|
||||
let cache_life = 30;
|
||||
if (process.env.CACHE_LIFE) {
|
||||
cache_life = parseInt(process.env.CACHE_LIFE);
|
||||
}
|
||||
|
||||
function getTrackTotalHits(query) {
|
||||
let trackTotalHits = 15000;
|
||||
if (query.track) {
|
||||
if (query.track === 'true') {
|
||||
trackTotalHits = true;
|
||||
} else if (query.track === 'false') {
|
||||
trackTotalHits = false;
|
||||
} else {
|
||||
trackTotalHits = parseInt(query.track, 10);
|
||||
if (trackTotalHits !== trackTotalHits) {
|
||||
throw new Error('failed to parse track param');
|
||||
}
|
||||
}
|
||||
}
|
||||
return trackTotalHits;
|
||||
}
|
||||
|
||||
function addSortedBy(query, queryBody, sort_direction) {
|
||||
if (query['sortedBy']) {
|
||||
const opts = query['sortedBy'].split(":");
|
||||
const sortedByObj = {};
|
||||
sortedByObj[opts[0]] = opts[1];
|
||||
queryBody['sort'] = sortedByObj;
|
||||
} else {
|
||||
queryBody['sort'] = {
|
||||
"global_sequence": sort_direction
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function processMultiVars(queryStruct, parts, field) {
|
||||
const must = [];
|
||||
const mustNot = [];
|
||||
|
||||
parts.forEach(part => {
|
||||
if (part.startsWith("!")) {
|
||||
mustNot.push(part.replace("!", ""));
|
||||
} else {
|
||||
must.push(part);
|
||||
}
|
||||
});
|
||||
|
||||
if (must.length > 1) {
|
||||
queryStruct.bool.must.push({
|
||||
bool: {
|
||||
should: must.map(elem => {
|
||||
const _q = {};
|
||||
_q[field] = elem;
|
||||
return {term: _q}
|
||||
})
|
||||
}
|
||||
});
|
||||
} else if (must.length === 1) {
|
||||
const mustQuery = {};
|
||||
mustQuery[field] = must[0];
|
||||
queryStruct.bool.must.push({term: mustQuery});
|
||||
}
|
||||
|
||||
if (mustNot.length > 1) {
|
||||
queryStruct.bool.must_not.push({
|
||||
bool: {
|
||||
should: mustNot.map(elem => {
|
||||
const _q = {};
|
||||
_q[field] = elem;
|
||||
return {term: _q}
|
||||
})
|
||||
}
|
||||
});
|
||||
} else if (mustNot.length === 1) {
|
||||
const mustNotQuery = {};
|
||||
mustNotQuery[field] = mustNot[0].replace("!", "");
|
||||
queryStruct.bool.must_not.push({term: mustNotQuery});
|
||||
}
|
||||
}
|
||||
|
||||
function addRangeQuery(queryStruct, prop, pkey, query) {
|
||||
const _termQuery = {};
|
||||
const parts = query[prop].split("-");
|
||||
_termQuery[pkey] = {
|
||||
"gte": parts[0],
|
||||
"lte": parts[1]
|
||||
};
|
||||
queryStruct.bool.must.push({range: _termQuery});
|
||||
}
|
||||
|
||||
function applyTimeFilter(query, queryStruct) {
|
||||
if (query['after'] || query['before']) {
|
||||
let _lte = "now";
|
||||
let _gte = 0;
|
||||
if (query['before']) {
|
||||
_lte = query['before'];
|
||||
if (!_lte.endsWith("Z")) {
|
||||
_lte += "Z";
|
||||
}
|
||||
}
|
||||
if (query['after']) {
|
||||
_gte = query['after'];
|
||||
if (!_gte.endsWith("Z")) {
|
||||
_gte += "Z";
|
||||
}
|
||||
}
|
||||
if (!queryStruct.bool['filter']) {
|
||||
queryStruct.bool['filter'] = [];
|
||||
}
|
||||
queryStruct.bool['filter'].push({
|
||||
range: {
|
||||
"@timestamp": {
|
||||
"gte": _gte,
|
||||
"lte": _lte
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function applyGenericFilters(query, queryStruct) {
|
||||
for (const prop in query) {
|
||||
if (Object.prototype.hasOwnProperty.call(query, prop)) {
|
||||
const pair = prop.split(".");
|
||||
if (pair.length > 1 || primaryTerms.includes(pair[0])) {
|
||||
let pkey;
|
||||
if (pair.length > 1) {
|
||||
pkey = extendedActions.has(pair[0]) ? "@" + prop : prop;
|
||||
} else {
|
||||
pkey = prop;
|
||||
}
|
||||
if (query[prop].indexOf("-") !== -1) {
|
||||
addRangeQuery(queryStruct, prop, pkey, query);
|
||||
} else {
|
||||
const _termQuery = {};
|
||||
const parts = query[prop].split(",");
|
||||
if (parts.length > 1) {
|
||||
processMultiVars(queryStruct, parts, prop);
|
||||
} else if (parts.length === 1) {
|
||||
const andParts = parts[0].split(" ");
|
||||
if (andParts.length > 1) {
|
||||
andParts.forEach(value => {
|
||||
const _q = {};
|
||||
console.log(value);
|
||||
_q[pkey] = value;
|
||||
queryStruct.bool.must.push({term: _q});
|
||||
});
|
||||
} else {
|
||||
if (parts[0].startsWith("!")) {
|
||||
_termQuery[pkey] = parts[0].replace("!", "");
|
||||
queryStruct.bool.must_not.push({term: _termQuery});
|
||||
} else {
|
||||
_termQuery[pkey] = parts[0];
|
||||
queryStruct.bool.must.push({term: _termQuery});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function makeShouldArray(query) {
|
||||
const should_array = [];
|
||||
for (const entry of terms) {
|
||||
const tObj = {term: {}};
|
||||
tObj.term[entry] = query.account;
|
||||
should_array.push(tObj);
|
||||
}
|
||||
return should_array;
|
||||
}
|
||||
|
||||
function applyCodeActionFilters(query, queryStruct) {
|
||||
let filterObj = [];
|
||||
if (query.filter) {
|
||||
for (const filter of query.filter.split(',')) {
|
||||
if (filter !== '*:*') {
|
||||
const _arr = [];
|
||||
const parts = filter.split(':');
|
||||
if (parts.length === 2) {
|
||||
[code, method] = parts;
|
||||
if (code && code !== "*") {
|
||||
_arr.push({'term': {'act.account': code}});
|
||||
}
|
||||
if (method && method !== "*") {
|
||||
_arr.push({'term': {'act.name': method}});
|
||||
}
|
||||
}
|
||||
if (_arr.length > 0) {
|
||||
filterObj.push({bool: {must: _arr}});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (filterObj.length > 0) {
|
||||
queryStruct.bool['should'] = filterObj;
|
||||
queryStruct.bool['minimum_should_match'] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getSkipLimit(query) {
|
||||
let skip, limit;
|
||||
skip = parseInt(query.skip, 10);
|
||||
if (skip < 0) {
|
||||
throw new Error('invalid skip parameter');
|
||||
}
|
||||
limit = parseInt(query.limit, 10);
|
||||
if (limit < 1) {
|
||||
throw new Error('invalid limit parameter');
|
||||
}
|
||||
return {skip, limit};
|
||||
}
|
||||
|
||||
function getSortDir(query) {
|
||||
let sort_direction = 'desc';
|
||||
if (query.sort) {
|
||||
if (query.sort === 'asc' || query.sort === '1') {
|
||||
sort_direction = 'asc';
|
||||
} else if (query.sort === 'desc' || query.sort === '-1') {
|
||||
sort_direction = 'desc'
|
||||
} else {
|
||||
throw new Error('invalid sort direction');
|
||||
}
|
||||
}
|
||||
return sort_direction;
|
||||
}
|
||||
|
||||
function applyAccountFilters(query, queryStruct) {
|
||||
if (query.account) {
|
||||
queryStruct.bool.must.push({"bool": {should: makeShouldArray(query)}});
|
||||
}
|
||||
}
|
||||
|
||||
async function getActions(fastify, request) {
|
||||
const t0 = Date.now();
|
||||
const {redis, elastic, eosjs} = fastify;
|
||||
const query = request.query;
|
||||
let cachedResponse, hash;
|
||||
if (enable_caching) {
|
||||
[cachedResponse, hash] = await getCacheByHash(redis, route + JSON.stringify(query));
|
||||
if (cachedResponse) {
|
||||
cachedResponse = JSON.parse(cachedResponse);
|
||||
cachedResponse['query_time'] = Date.now() - t0;
|
||||
cachedResponse['cached'] = true;
|
||||
return cachedResponse;
|
||||
}
|
||||
}
|
||||
|
||||
const queryStruct = {
|
||||
"bool": {
|
||||
must: [],
|
||||
must_not: [],
|
||||
boost: 1.0
|
||||
}
|
||||
};
|
||||
|
||||
const {skip, limit} = getSkipLimit(query);
|
||||
|
||||
const sort_direction = getSortDir(query);
|
||||
|
||||
applyAccountFilters(query, queryStruct);
|
||||
|
||||
applyGenericFilters(query, queryStruct);
|
||||
|
||||
applyTimeFilter(query, queryStruct);
|
||||
|
||||
applyCodeActionFilters(query, queryStruct);
|
||||
|
||||
// allow precise counting of total hits
|
||||
const trackTotalHits = getTrackTotalHits(query);
|
||||
|
||||
// Prepare query body
|
||||
const query_body = {
|
||||
"track_total_hits": trackTotalHits,
|
||||
"query": queryStruct
|
||||
};
|
||||
|
||||
// Include sorting
|
||||
addSortedBy(query, query_body, sort_direction);
|
||||
|
||||
// console.log(prettyjson.render(queryStruct));
|
||||
|
||||
// Perform search
|
||||
const pResults = await Promise.all([eosjs.rpc.get_info(), elastic['search']({
|
||||
"index": process.env.CHAIN + '-action-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxActions ? maxActions : limit) || 10,
|
||||
"body": query_body
|
||||
})]);
|
||||
|
||||
const results = pResults[1]['body']['hits'];
|
||||
const response = {
|
||||
query_time: null,
|
||||
cached: false,
|
||||
lib: pResults[0].last_irreversible_block_num,
|
||||
total: results['total']
|
||||
};
|
||||
|
||||
if (query.simple) {
|
||||
response['simple_actions'] = [];
|
||||
} else {
|
||||
response['actions'] = [];
|
||||
}
|
||||
|
||||
if (results['hits'].length > 0) {
|
||||
const actions = results['hits'];
|
||||
for (let action of actions) {
|
||||
action = action._source;
|
||||
mergeActionMeta(action);
|
||||
if (query.simple) {
|
||||
response.simple_actions.push({
|
||||
block: action['block_num'],
|
||||
irreversible: action['block_num'] < pResults[0].last_irreversible_block_num,
|
||||
timestamp: action['@timestamp'],
|
||||
transaction_id: action['trx_id'],
|
||||
actors: action['act']['authorization'].map(a => `${a.actor}@${a.permission}`).join(","),
|
||||
notified: action['notified'].join(','),
|
||||
contract: action['act']['account'],
|
||||
action: action['act']['name'],
|
||||
data: action['act']['data']
|
||||
});
|
||||
} else {
|
||||
response.actions.push(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
response['query_time'] = Date.now() - t0;
|
||||
|
||||
if (enable_caching) {
|
||||
redis.set(hash, JSON.stringify(response), 'EX', cache_life);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get('/get_actions', {
|
||||
schema: getActionsSchema.GET
|
||||
}, async (request) => {
|
||||
return await getActions(fastify, request);
|
||||
});
|
||||
next()
|
||||
};
|
||||
@@ -1,49 +0,0 @@
|
||||
const {getBlocksSchema} = require("../../schemas");
|
||||
const {blockStore} = require('../blockStore');
|
||||
|
||||
async function getBlocks(fastify, request, reply) {
|
||||
const t0 = Date.now();
|
||||
let from_block, to_block;
|
||||
const {eosjs} = fastify;
|
||||
if (request.query.from) {
|
||||
from_block = parseInt(request.query.from);
|
||||
if (request.query.to) {
|
||||
to_block = parseInt(request.query.to);
|
||||
} else {
|
||||
to_block = from_block + 1;
|
||||
}
|
||||
} else {
|
||||
const head = (await eosjs.rpc.get_info()).head_block_num;
|
||||
from_block = head;
|
||||
to_block = head + 1;
|
||||
}
|
||||
|
||||
if (to_block > from_block) {
|
||||
if (to_block - from_block > 20) {
|
||||
reply.status(403).send('request over limit');
|
||||
} else {
|
||||
const response = {query_time: null, cached: false, blocks: []};
|
||||
console.log('Local Block store size:', blockStore.localBlocks.size);
|
||||
blockStore.requestRange(from_block, to_block, (block, cached) => {
|
||||
if (cached) {
|
||||
response.cached = true;
|
||||
}
|
||||
response.blocks.push(block);
|
||||
}, () => {
|
||||
response['query_time'] = Date.now() - t0;
|
||||
reply.send(response);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
reply.status(403).send('invalid block range');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get('/get_blocks', {
|
||||
schema: getBlocksSchema.GET
|
||||
}, async (request, reply) => {
|
||||
await getBlocks(fastify, request, reply);
|
||||
});
|
||||
next();
|
||||
};
|
||||
@@ -1,62 +0,0 @@
|
||||
const {getCreatedAccountsSchema} = require("../../schemas");
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
|
||||
const route = '/get_created_accounts';
|
||||
|
||||
async function getCreatedAccounts(fastify, request) {
|
||||
const t0 = Date.now();
|
||||
const {redis, elastic} = fastify;
|
||||
const [cachedResponse, hash] = await getCacheByHash(redis, JSON.stringify(request.query));
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
const results = await elastic['search']({
|
||||
"index": process.env.CHAIN + '-action-*',
|
||||
"body": {
|
||||
"query": {
|
||||
"bool": {
|
||||
must: [
|
||||
{term: {"act.authorization.actor": request.query.account.toLowerCase()}},
|
||||
{term: {"act.name": "newaccount"}},
|
||||
{term: {"act.account": "eosio"}}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: {
|
||||
"global_sequence": "desc"
|
||||
}
|
||||
}
|
||||
});
|
||||
const response = {
|
||||
"accounts": []
|
||||
};
|
||||
if (results['body']['hits']['hits'].length > 0) {
|
||||
const actions = results['body']['hits']['hits'];
|
||||
for (let action of actions) {
|
||||
action = action._source;
|
||||
const _tmp = {};
|
||||
if (action['act']['data']['newact']) {
|
||||
_tmp['name'] = action['act']['data']['newact'];
|
||||
} else if (action['@newaccount']['newact']) {
|
||||
_tmp['name'] = action['@newaccount']['newact'];
|
||||
} else {
|
||||
console.log(action);
|
||||
}
|
||||
_tmp['trx_id'] = action['trx_id'];
|
||||
_tmp['timestamp'] = action['@timestamp'];
|
||||
response.accounts.push(_tmp);
|
||||
}
|
||||
}
|
||||
response['query_time'] = Date.now() - t0;
|
||||
redis.set(hash, JSON.stringify(response), 'EX', 30);
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get(route, {
|
||||
schema: getCreatedAccountsSchema.GET
|
||||
}, async (request) => {
|
||||
return await getCreatedAccounts(fastify, request);
|
||||
});
|
||||
next()
|
||||
};
|
||||
@@ -1,108 +0,0 @@
|
||||
const {getCreatorSchema} = require("../../schemas");
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
|
||||
const route = '/get_creator';
|
||||
|
||||
async function getActionByGS(client, gs) {
|
||||
console.log(gs);
|
||||
const results = await client['search']({
|
||||
index: process.env.CHAIN + '-action-*',
|
||||
body: {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{term: {"global_sequence": gs}}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return results['body']['hits']['hits'][0]['_source'];
|
||||
}
|
||||
|
||||
async function getCreator(fastify, request) {
|
||||
const {redis, elastic, eosjs} = fastify;
|
||||
const [cachedResponse, hash] = await getCacheByHash(redis, route + JSON.stringify(request.query) + 'v3');
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
const newact = request.query.account;
|
||||
const response = {
|
||||
account: newact,
|
||||
creator: '',
|
||||
timestamp: '',
|
||||
block_num: 0,
|
||||
trx_id: '',
|
||||
};
|
||||
let account_data = null;
|
||||
try {
|
||||
account_data = await eosjs.rpc.get_account(newact);
|
||||
} catch (e) {
|
||||
const err = new Error();
|
||||
err.statusCode = 404;
|
||||
err.message = 'account not found';
|
||||
throw err;
|
||||
}
|
||||
response.timestamp = account_data['created'];
|
||||
const queryBody = {
|
||||
size: 1000,
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{term: {"act.name": "newaccount"}},
|
||||
{term: {"act.account": "eosio"}},
|
||||
{term: {"@timestamp": account_data['created']}}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
||||
const results = await elastic['search']({
|
||||
"index": process.env.CHAIN + '-action-*',
|
||||
"body": queryBody
|
||||
});
|
||||
for (const action of results['body']['hits']['hits']) {
|
||||
const actData = action._source.act.data;
|
||||
let valid = false;
|
||||
response.block_num = action._source.block_num;
|
||||
if (actData.newact === newact) {
|
||||
response.creator = actData.creator;
|
||||
response['trx_id'] = action._source['trx_id'];
|
||||
valid = true;
|
||||
} else {
|
||||
if (action._source['@newaccount']) {
|
||||
if (action._source['@newaccount']['newact'] === newact) {
|
||||
response.creator = actData.creator;
|
||||
response['trx_id'] = action._source['trx_id'];
|
||||
valid = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if (action._source.parent !== 0 && valid) {
|
||||
//
|
||||
// // Find indirect creator by global seq
|
||||
// const creationAction = await getActionByGS(elastic, action._source.parent);
|
||||
//
|
||||
// if (creationAction.act.name === 'transfer') {
|
||||
// response['indirect_creator'] = creationAction['@transfer']['from'];
|
||||
// response['trx_id'] = creationAction['trx_id'];
|
||||
// } else {
|
||||
// response['indirect_creator'] = creationAction.act.authorization[0].actor;
|
||||
// response['trx_id'] = creationAction['trx_id'];
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
redis.set(hash, JSON.stringify(response), 'EX', 600);
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get(route, {
|
||||
schema: getCreatorSchema.GET
|
||||
}, async (request) => {
|
||||
return await getCreator(fastify, request);
|
||||
});
|
||||
next()
|
||||
};
|
||||
@@ -1,99 +0,0 @@
|
||||
const {getDeltasSchema} = require("../../schemas");
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
|
||||
const route = '/get_deltas';
|
||||
const maxDeltas = 200;
|
||||
|
||||
async function getDeltas(fastify, request) {
|
||||
const t0 = Date.now();
|
||||
console.log(request.query);
|
||||
const {redis, elastic} = fastify;
|
||||
const [cachedResponse, hash] = await getCacheByHash(redis, route + JSON.stringify(request.query) + 'v2');
|
||||
|
||||
// if (cachedResponse) {
|
||||
// return cachedResponse;
|
||||
// }
|
||||
|
||||
let skip, limit;
|
||||
let sort_direction = 'desc';
|
||||
const mustArray = [];
|
||||
for (const param in request.query) {
|
||||
if (Object.prototype.hasOwnProperty.call(request.query, param)) {
|
||||
console.log(param, request.query[param]);
|
||||
const value = request.query[param];
|
||||
switch (param) {
|
||||
case 'limit': {
|
||||
limit = parseInt(value, 10);
|
||||
if (limit < 1) {
|
||||
return 'invalid limit parameter';
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'skip': {
|
||||
skip = parseInt(value, 10);
|
||||
if (skip < 0) {
|
||||
return 'invalid skip parameter';
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'sort': {
|
||||
if (value === 'asc' || value === '1') {
|
||||
sort_direction = 'asc';
|
||||
} else if (value === 'desc' || value === '-1') {
|
||||
sort_direction = 'desc'
|
||||
} else {
|
||||
return 'invalid sort direction';
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
const values = request.query[param].split(",");
|
||||
const terms = {};
|
||||
terms[param] = values;
|
||||
const shouldArray = {terms: terms};
|
||||
const boolStruct = {bool: {should: [shouldArray]}};
|
||||
mustArray.push(boolStruct);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let prefix = process.env.CHAIN;
|
||||
if (process.env.CHAIN === 'mainnet') {
|
||||
prefix = 'eos';
|
||||
}
|
||||
|
||||
const results = await elastic.search({
|
||||
"index": prefix + '-delta-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxDeltas ? maxDeltas : limit) || 10,
|
||||
"body": {
|
||||
query: {bool: {must: mustArray}},
|
||||
sort: {
|
||||
"block_num": sort_direction
|
||||
}
|
||||
}
|
||||
});
|
||||
const response = {
|
||||
query_time: null,
|
||||
total: results['body']['hits']['total'],
|
||||
deltas: results['body']['hits']['hits'].map((d) => {
|
||||
delete d._source.present;
|
||||
return d._source;
|
||||
})
|
||||
};
|
||||
response['query_time'] = Date.now() - t0;
|
||||
// redis.set(hash, JSON.stringify(response));
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get(route, {
|
||||
schema: getDeltasSchema.GET
|
||||
}, async (request) => {
|
||||
return await getDeltas(fastify, request);
|
||||
});
|
||||
next()
|
||||
};
|
||||
@@ -1,200 +0,0 @@
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
|
||||
const {getTransactedAccountsSchema} = require("../../schemas");
|
||||
|
||||
async function getTransactedAccounts(fastify, request) {
|
||||
const t0 = Date.now();
|
||||
const {redis, elastic} = fastify;
|
||||
const [cachedResponse, hash] = await getCacheByHash(redis, JSON.stringify(request.query));
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
const {account, min, max, direction, limit, contract, symbol} = request.query;
|
||||
let _limit = 100;
|
||||
if (limit) {
|
||||
_limit = parseInt(limit, 10);
|
||||
if (_limit > 500) {
|
||||
_limit = 500;
|
||||
}
|
||||
}
|
||||
let _min;
|
||||
if (min) {
|
||||
_min = parseInt(min, 10);
|
||||
if (_min < 0) {
|
||||
_min = 0;
|
||||
}
|
||||
}
|
||||
let _max;
|
||||
if (max) {
|
||||
_max = parseInt(max, 10);
|
||||
if (_max < 0) {
|
||||
_max = null;
|
||||
}
|
||||
}
|
||||
|
||||
const response = {
|
||||
query_time: null,
|
||||
account: account
|
||||
};
|
||||
|
||||
const must_array = [
|
||||
{term: {"notified": account}},
|
||||
{term: {"act.name": "transfer"}}
|
||||
];
|
||||
|
||||
if (min || max) {
|
||||
const _range = {
|
||||
"@transfer.amount": {}
|
||||
};
|
||||
if (min) {
|
||||
_range["@transfer.amount"]["gte"] = _min;
|
||||
}
|
||||
if (max) {
|
||||
_range["@transfer.amount"]["lt"] = _max;
|
||||
}
|
||||
must_array.push({range: _range});
|
||||
}
|
||||
|
||||
if (contract) {
|
||||
must_array.push({term: {"act.account": contract}});
|
||||
response['contract'] = contract;
|
||||
}
|
||||
|
||||
if (symbol) {
|
||||
must_array.push({term: {"@transfer.symbol": symbol}});
|
||||
response['symbol'] = symbol;
|
||||
}
|
||||
|
||||
const sum_agg = {
|
||||
"total_amount": {
|
||||
sum: {
|
||||
field: "@transfer.amount"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let filter_array = [];
|
||||
if (request.query['after'] || request.query['before']) {
|
||||
let _lte = "now";
|
||||
let _gte = 0;
|
||||
if (request.query['before']) {
|
||||
_lte = request.query['before'];
|
||||
}
|
||||
if (request.query['after']) {
|
||||
_gte = request.query['after'];
|
||||
}
|
||||
filter_array.push({
|
||||
range: {
|
||||
"@timestamp": {
|
||||
"gte": _gte,
|
||||
"lte": _lte
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
filter_array.push({
|
||||
range: {
|
||||
"block_num": {
|
||||
"gte": 0
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (direction === 'out' || direction === 'both') {
|
||||
const outResults = await elastic.search({
|
||||
index: process.env.CHAIN + '-action-*',
|
||||
body: {
|
||||
size: 0,
|
||||
aggs: {
|
||||
"receivers": {
|
||||
terms: {
|
||||
field: "@transfer.to",
|
||||
size: _limit,
|
||||
order: {
|
||||
"total_amount": "desc"
|
||||
}
|
||||
},
|
||||
aggs: sum_agg
|
||||
}
|
||||
},
|
||||
query: {
|
||||
bool: {
|
||||
must: must_array,
|
||||
filter: filter_array,
|
||||
must_not: [
|
||||
{term: {"@transfer.to": account}}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
response['total_out'] = 0;
|
||||
response['outputs'] = outResults['body']['aggregations']['receivers']['buckets'].map((bucket) => {
|
||||
const _sum = parseFloat(bucket.total_amount.value.toFixed(4));
|
||||
response['total_out'] += _sum;
|
||||
return {
|
||||
account: bucket.key,
|
||||
sum: _sum,
|
||||
transfers: bucket['doc_count'],
|
||||
average: parseFloat((bucket.total_amount.value / bucket['doc_count']).toFixed(4))
|
||||
};
|
||||
});
|
||||
response['total_out'] = parseFloat(response['total_out'].toFixed(4));
|
||||
}
|
||||
|
||||
if (direction === 'in' || direction === 'both') {
|
||||
const inResults = await elastic.search({
|
||||
index: process.env.CHAIN + '-action-*',
|
||||
body: {
|
||||
size: 0,
|
||||
aggs: {
|
||||
"senders": {
|
||||
terms: {
|
||||
field: "@transfer.from",
|
||||
size: _limit,
|
||||
order: {
|
||||
"total_amount": "desc"
|
||||
}
|
||||
},
|
||||
aggs: sum_agg
|
||||
}
|
||||
},
|
||||
query: {
|
||||
bool: {
|
||||
must: must_array,
|
||||
filter: filter_array,
|
||||
must_not: [
|
||||
{term: {"@transfer.from": account}}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
response['total_in'] = 0;
|
||||
response['inputs'] = inResults['body']['aggregations']['senders']['buckets'].map((bucket) => {
|
||||
const _sum = parseFloat(bucket.total_amount.value.toFixed(4));
|
||||
response['total_in'] += _sum;
|
||||
return {
|
||||
account: bucket.key,
|
||||
sum: _sum,
|
||||
transfers: bucket['doc_count'],
|
||||
average: parseFloat((bucket.total_amount.value / bucket['doc_count']).toFixed(4))
|
||||
};
|
||||
});
|
||||
response['total_in'] = parseFloat(response['total_in'].toFixed(4));
|
||||
}
|
||||
|
||||
response['query_time'] = Date.now() - t0;
|
||||
redis.set(hash, JSON.stringify(response), 'EX', 600);
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get('/get_transacted_accounts', {
|
||||
schema: getTransactedAccountsSchema.GET
|
||||
}, async (request, reply) => {
|
||||
reply.send(await getTransactedAccounts(fastify, request));
|
||||
});
|
||||
next()
|
||||
};
|
||||
@@ -1,55 +0,0 @@
|
||||
const {getTransactionSchema} = require("../../schemas");
|
||||
const _ = require('lodash');
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
|
||||
async function getTransaction(fastify, request) {
|
||||
const {redis, elastic, eosjs} = fastify;
|
||||
const [cachedResponse, hash] = await getCacheByHash(redis, JSON.stringify(request.query));
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
const pResults = await Promise.all([eosjs.rpc.get_info(), elastic['search']({
|
||||
"index": process.env.CHAIN + '-action-*',
|
||||
"body": {
|
||||
"query": {
|
||||
"bool": {
|
||||
must: [
|
||||
{term: {"trx_id": request.query.id.toLowerCase()}}
|
||||
]
|
||||
}
|
||||
},
|
||||
"sort": {
|
||||
"global_sequence": "asc"
|
||||
}
|
||||
}
|
||||
})]);
|
||||
const results = pResults[1];
|
||||
const response = {
|
||||
"trx_id": request.query.id,
|
||||
"lib": pResults[0].last_irreversible_block_num,
|
||||
"actions": []
|
||||
};
|
||||
const hits = results['body']['hits']['hits'];
|
||||
if (hits.length > 0) {
|
||||
for (let action of hits) {
|
||||
action = action._source;
|
||||
const name = action.act.name;
|
||||
if (action['@' + name]) {
|
||||
action['act']['data'] = _.merge(action['@' + name], action['act']['data']);
|
||||
delete action['@' + name];
|
||||
}
|
||||
response.actions.push(action);
|
||||
}
|
||||
}
|
||||
redis.set(hash, JSON.stringify(response), 'EX', 30);
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get('/get_transaction', {
|
||||
schema: getTransactionSchema.GET
|
||||
}, async (request, reply) => {
|
||||
reply.send(await getTransaction(fastify, request));
|
||||
});
|
||||
next()
|
||||
};
|
||||
@@ -1,98 +0,0 @@
|
||||
const {getTransfersSchema} = require("../../schemas");
|
||||
const _ = require('lodash');
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
const route = '/get_transfers';
|
||||
|
||||
const maxActions = 1000;
|
||||
|
||||
function processActions(results) {
|
||||
const action_traces = results['body']['hits']['hits'];
|
||||
const actions = [];
|
||||
let sum = 0;
|
||||
for (const aTrace of action_traces) {
|
||||
const action = aTrace['_source'];
|
||||
const name = action.act.name;
|
||||
if (action['@' + name]) {
|
||||
if (action['@transfer']) {
|
||||
sum += parseFloat(action['@transfer']['amount']);
|
||||
}
|
||||
action['act']['data'] = _.merge(action['@' + name], action['act']['data']);
|
||||
delete action['@' + name];
|
||||
}
|
||||
actions.push(action);
|
||||
}
|
||||
return [sum, actions];
|
||||
}
|
||||
|
||||
async function getTransfers(fastify, request) {
|
||||
const {redis, elastic} = fastify;
|
||||
const [cachedResponse, hash] = await getCacheByHash(redis, route + JSON.stringify(request.query));
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
const must_array = [];
|
||||
must_array.push({"term": {"act.name": {"value": "transfer"}}});
|
||||
if (request.query['from']) {
|
||||
must_array.push({"term": {"@transfer.from": {"value": request.query['from'].toLowerCase()}}});
|
||||
}
|
||||
if (request.query['to']) {
|
||||
must_array.push({"term": {"@transfer.to": {"value": request.query['to'].toLowerCase()}}});
|
||||
}
|
||||
if (request.query['symbol']) {
|
||||
must_array.push({"term": {"@transfer.symbol": {"value": request.query['symbol'].toUpperCase()}}});
|
||||
}
|
||||
if (request.query['contract']) {
|
||||
must_array.push({"term": {"act.account": {"value": request.query['contract'].toLowerCase()}}});
|
||||
}
|
||||
if (request.query['after'] || request.query['before']) {
|
||||
let _lte = "now";
|
||||
let _gte = 0;
|
||||
if (request.query['before']) {
|
||||
_lte = request.query['before'];
|
||||
}
|
||||
if (request.query['after']) {
|
||||
_gte = request.query['after'];
|
||||
}
|
||||
must_array.push({
|
||||
range: {
|
||||
"@timestamp": {
|
||||
"gte": _gte,
|
||||
"lte": _lte
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
let limit, skip;
|
||||
limit = parseInt(request.query.limit, 10);
|
||||
if (limit < 1) {
|
||||
return 'invalid limit parameter';
|
||||
}
|
||||
skip = parseInt(request.query.skip, 10);
|
||||
if (skip < 0) {
|
||||
return 'invalid skip parameter';
|
||||
}
|
||||
const body = {"query": {"bool": {"must": must_array}}};
|
||||
const results = await elastic.search({
|
||||
"index": process.env.CHAIN + '-action-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxActions ? maxActions : limit) || 10,
|
||||
"body": body
|
||||
});
|
||||
const [sum, _actions] = processActions(results);
|
||||
const response = {
|
||||
"action_count": results['body']['hits']['total']['value'],
|
||||
"total_amount": sum,
|
||||
"actions": _actions
|
||||
};
|
||||
redis.set(hash, JSON.stringify(response), 'EX', 30);
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get(route, {
|
||||
schema: getTransfersSchema.GET
|
||||
}, async (request, reply) => {
|
||||
reply.send(await getTransfers(fastify, request));
|
||||
});
|
||||
next()
|
||||
};
|
||||
@@ -1,65 +0,0 @@
|
||||
const {getAccountSchema} = require("../../schemas");
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
const route = '/get_account';
|
||||
const got = require('got');
|
||||
|
||||
const localApi = `http://${process.env.SERVER_ADDR}:${process.env.SERVER_PORT}/v2`;
|
||||
const getTokensApi = localApi + '/state/get_tokens';
|
||||
const getActionsApi = localApi + '/history/get_actions';
|
||||
|
||||
const enable_caching = process.env.ENABLE_CACHING === 'true';
|
||||
let cache_life = 30;
|
||||
if (process.env.CACHE_LIFE) {
|
||||
cache_life = parseInt(process.env.CACHE_LIFE);
|
||||
}
|
||||
|
||||
async function getAccount(fastify, request) {
|
||||
const t0 = Date.now();
|
||||
const {redis, elastic, eosjs} = fastify;
|
||||
|
||||
// let cachedResponse, hash;
|
||||
// if(enable_caching) {
|
||||
// [cachedResponse, hash] = await getCacheByHash(redis, route + JSON.stringify(request.query));
|
||||
// if (cachedResponse) {
|
||||
// cachedResponse = JSON.parse(cachedResponse);
|
||||
// cachedResponse['query_time'] = Date.now() - t0;
|
||||
// cachedResponse['cached'] = true;
|
||||
// return cachedResponse;
|
||||
// }
|
||||
// }
|
||||
|
||||
const response = {
|
||||
query_time: null,
|
||||
cached: false,
|
||||
account: null,
|
||||
actions: null,
|
||||
tokens: null,
|
||||
links: null
|
||||
};
|
||||
|
||||
const account = request.query.account;
|
||||
const reqQueue = [];
|
||||
reqQueue.push(eosjs.rpc.get_account(account));
|
||||
|
||||
// fetch recent actions
|
||||
reqQueue.push(got.get(`${getActionsApi}?account=${account}&limit=10`));
|
||||
|
||||
// fetch account tokens
|
||||
reqQueue.push(got.get(`${getTokensApi}?account=${account}`));
|
||||
|
||||
const results = await Promise.all(reqQueue);
|
||||
response.account = results[0];
|
||||
response.actions = JSON.parse(results[1].body).actions;
|
||||
response.tokens = JSON.parse(results[2].body).tokens;
|
||||
response['query_time'] = Date.now() - t0;
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get(route, {
|
||||
schema: getAccountSchema.GET
|
||||
}, async (request) => {
|
||||
return await getAccount(fastify, request);
|
||||
});
|
||||
next()
|
||||
};
|
||||
@@ -1,97 +0,0 @@
|
||||
const {getKeyAccountsSchema} = require("../../schemas");
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
const numeric = require('eosjs/dist/eosjs-numeric');
|
||||
|
||||
function invalidKey() {
|
||||
const err = new Error();
|
||||
err.statusCode = 400;
|
||||
err.message = 'invalid public key';
|
||||
throw err;
|
||||
}
|
||||
|
||||
async function getKeyAccounts(fastify, public_Key) {
|
||||
|
||||
const {redis, elastic} = fastify;
|
||||
let publicKey;
|
||||
|
||||
if (public_Key.startsWith("PUB_")) {
|
||||
publicKey = public_Key;
|
||||
} else if (public_Key.startsWith("EOS")) {
|
||||
try {
|
||||
publicKey = numeric.convertLegacyPublicKey(public_Key);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
invalidKey();
|
||||
}
|
||||
} else {
|
||||
invalidKey();
|
||||
}
|
||||
|
||||
const [cachedResponse, hash] = await getCacheByHash(redis, JSON.stringify(publicKey));
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
const _body = {
|
||||
query: {
|
||||
bool: {
|
||||
should: [
|
||||
{term: {"@updateauth.auth.keys.key.keyword": publicKey}},
|
||||
{term: {"@newaccount.active.keys.key.keyword": publicKey}},
|
||||
{term: {"@newaccount.owner.keys.key.keyword": publicKey}}
|
||||
],
|
||||
minimum_should_match: 1
|
||||
}
|
||||
},
|
||||
sort: [{"global_sequence": {"order": "desc"}}]
|
||||
};
|
||||
|
||||
const results = await elastic.search({
|
||||
index: process.env.CHAIN + '-action-*',
|
||||
body: _body
|
||||
});
|
||||
|
||||
const response = {
|
||||
account_names: []
|
||||
};
|
||||
if (results['body']['hits']['hits'].length > 0) {
|
||||
response.account_names = results['body']['hits']['hits'].map((v) => {
|
||||
if (v._source.act.name === 'newaccount') {
|
||||
if (v._source['@newaccount'].newact) {
|
||||
return v._source['@newaccount'].newact;
|
||||
} else if (v._source.act.data.newact) {
|
||||
return v._source.act.data.newact;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else if (v._source.act.name === 'updateauth') {
|
||||
return v._source.act.data.account;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
if (response.account_names.length > 0) {
|
||||
response.account_names = [...(new Set(response.account_names))];
|
||||
redis.set(hash, JSON.stringify(response), 'EX', 30);
|
||||
return response;
|
||||
} else {
|
||||
const err = new Error();
|
||||
err.statusCode = 404;
|
||||
err.message = 'no accounts associated with ' + public_Key;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get('/get_key_accounts', {
|
||||
schema: getKeyAccountsSchema.GET
|
||||
}, async (request) => {
|
||||
return getKeyAccounts(fastify, request.query.public_key);
|
||||
});
|
||||
fastify.post('/get_key_accounts', {
|
||||
schema: getKeyAccountsSchema.POST
|
||||
}, async (request) => {
|
||||
return getKeyAccounts(fastify, request.body.public_key);
|
||||
});
|
||||
next();
|
||||
};
|
||||
@@ -1,146 +0,0 @@
|
||||
const {getProposalsSchema} = require("../../schemas");
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
const route = '/get_proposals';
|
||||
|
||||
const maxActions = 1000;
|
||||
|
||||
const enable_caching = process.env.ENABLE_CACHING === 'true';
|
||||
let cache_life = 30;
|
||||
if (process.env.CACHE_LIFE) {
|
||||
cache_life = parseInt(process.env.CACHE_LIFE);
|
||||
}
|
||||
|
||||
function getTrackOpt(request) {
|
||||
let trackTotalHits = 10000;
|
||||
if (request.query.track) {
|
||||
if (request.query.track === 'true') {
|
||||
trackTotalHits = true;
|
||||
} else if (request.query.track === 'false') {
|
||||
trackTotalHits = false;
|
||||
} else {
|
||||
trackTotalHits = parseInt(request.query.track, 10);
|
||||
if (trackTotalHits !== trackTotalHits) {
|
||||
throw new Error('failed to parse track param');
|
||||
}
|
||||
}
|
||||
}
|
||||
return trackTotalHits;
|
||||
}
|
||||
|
||||
async function getProposals(fastify, request) {
|
||||
|
||||
const t0 = Date.now();
|
||||
const {redis, elastic, eosjs} = fastify;
|
||||
let cachedResponse, hash;
|
||||
if (enable_caching) {
|
||||
[cachedResponse, hash] = await getCacheByHash(redis, route + JSON.stringify(request.query));
|
||||
if (cachedResponse) {
|
||||
cachedResponse = JSON.parse(cachedResponse);
|
||||
cachedResponse['query_time'] = Date.now() - t0;
|
||||
cachedResponse['cached'] = true;
|
||||
return cachedResponse;
|
||||
}
|
||||
}
|
||||
|
||||
// Pagination
|
||||
let skip, limit;
|
||||
skip = parseInt(request.query.skip, 10);
|
||||
if (skip < 0) {
|
||||
return 'invalid skip parameter';
|
||||
}
|
||||
limit = parseInt(request.query.limit, 10);
|
||||
if (limit < 1) {
|
||||
return 'invalid limit parameter';
|
||||
}
|
||||
|
||||
let queryStruct = {
|
||||
"bool": {
|
||||
"must": []
|
||||
}
|
||||
};
|
||||
|
||||
// Filter by account
|
||||
if (request.query.account) {
|
||||
queryStruct.bool.must.push({
|
||||
"bool": {
|
||||
"should": [
|
||||
{"term": {"requested_approvals.actor": request.query.account}},
|
||||
{"term": {"provided_approvals.actor": request.query.account}}
|
||||
]
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Filter by proposer account
|
||||
if (request.query.proposer) {
|
||||
queryStruct.bool.must.push({"term": {"proposer": request.query.proposer}});
|
||||
}
|
||||
|
||||
// Filter by proposal name
|
||||
if (request.query.proposal) {
|
||||
queryStruct.bool.must.push({"term": {"proposal_name": request.query.proposal}});
|
||||
}
|
||||
|
||||
// Filter by execution status
|
||||
if (typeof request.query.executed !== 'undefined') {
|
||||
queryStruct.bool.must.push({"term": {"executed": request.query.executed}});
|
||||
}
|
||||
|
||||
// Filter by requested actors
|
||||
if (request.query.requested) {
|
||||
queryStruct.bool.must.push({"term": {"requested_approvals.actor": request.query.requested}});
|
||||
}
|
||||
|
||||
// Filter by provided actors
|
||||
if (request.query.provided) {
|
||||
queryStruct.bool.must.push({"term": {"provided_approvals.actor": request.query.provided}});
|
||||
}
|
||||
|
||||
// If no filter switch to full match
|
||||
if (queryStruct.bool.must.length === 0) {
|
||||
queryStruct = {
|
||||
"match_all": {}
|
||||
};
|
||||
}
|
||||
|
||||
console.log(JSON.stringify(queryStruct));
|
||||
|
||||
const results = await elastic.search({
|
||||
"index": process.env.CHAIN + '-table-proposals-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxActions ? maxActions : limit) || 10,
|
||||
"body": {
|
||||
"track_total_hits": getTrackOpt(request),
|
||||
"query": queryStruct,
|
||||
"sort": [{"block_num": "desc"}]
|
||||
}
|
||||
});
|
||||
|
||||
const response = {
|
||||
query_time: null,
|
||||
cached: false,
|
||||
total: results['body']['hits']['total'],
|
||||
proposals: []
|
||||
};
|
||||
|
||||
const hits = results['body']['hits']['hits'];
|
||||
for (const hit of hits) {
|
||||
const prop = hit._source;
|
||||
response.proposals.push(prop);
|
||||
}
|
||||
|
||||
response['query_time'] = Date.now() - t0;
|
||||
if (enable_caching) {
|
||||
redis.set(hash, JSON.stringify(response), 'EX', cache_life);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get(route, {
|
||||
schema: getProposalsSchema.GET
|
||||
}, async (request) => {
|
||||
return await getProposals(fastify, request);
|
||||
});
|
||||
next();
|
||||
};
|
||||
@@ -1,94 +0,0 @@
|
||||
const {getTokensSchema} = require("../../schemas");
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
const route = '/get_tokens';
|
||||
|
||||
const enable_caching = process.env.ENABLE_CACHING === 'true';
|
||||
let cache_life = 30;
|
||||
if(process.env.CACHE_LIFE) {
|
||||
cache_life = parseInt(process.env.CACHE_LIFE);
|
||||
}
|
||||
|
||||
async function getTokens(fastify, request) {
|
||||
const t0 = Date.now();
|
||||
const {redis, elastic, eosjs} = fastify;
|
||||
|
||||
let cachedResponse, hash;
|
||||
if(enable_caching) {
|
||||
[cachedResponse, hash] = await getCacheByHash(redis, route + JSON.stringify(request.query));
|
||||
if (cachedResponse) {
|
||||
cachedResponse = JSON.parse(cachedResponse);
|
||||
cachedResponse['query_time'] = Date.now() - t0;
|
||||
cachedResponse['cached'] = true;
|
||||
return cachedResponse;
|
||||
}
|
||||
}
|
||||
|
||||
const response = {
|
||||
query_time: null,
|
||||
cached: false,
|
||||
'account': request.query.account,
|
||||
'tokens': []
|
||||
};
|
||||
|
||||
const results = await elastic.search({
|
||||
"index": process.env.CHAIN + '-action-*',
|
||||
"body": {
|
||||
size: 0,
|
||||
query: {
|
||||
bool: {
|
||||
// must_not: {term: {"act.account": "eosio.token"}},
|
||||
filter: [
|
||||
{term: {"notified": request.query.account}},
|
||||
{terms: {"act.name": ["transfer", "issue"]}}
|
||||
]
|
||||
}
|
||||
},
|
||||
aggs: {
|
||||
tokens: {
|
||||
terms: {
|
||||
field: "act.account",
|
||||
size: 1000
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (const bucket of results['body']['aggregations']['tokens']['buckets']) {
|
||||
let token_data;
|
||||
try {
|
||||
token_data = await eosjs.rpc.get_currency_balance(bucket['key'], request.query.account);
|
||||
} catch (e) {
|
||||
console.log(`get_currency_balance error - contract:${bucket['key']} - account:${request.query.account}`);
|
||||
continue;
|
||||
}
|
||||
for (const entry of token_data) {
|
||||
let precision = 0;
|
||||
const [amount, symbol] = entry.split(" ");
|
||||
const amount_arr = amount.split(".");
|
||||
if (amount_arr.length === 2) {
|
||||
precision = amount_arr[1].length;
|
||||
}
|
||||
response.tokens.push({
|
||||
symbol: symbol,
|
||||
precision: precision,
|
||||
amount: parseFloat(amount),
|
||||
contract: bucket['key']
|
||||
});
|
||||
}
|
||||
}
|
||||
response['query_time'] = Date.now() - t0;
|
||||
if(enable_caching) {
|
||||
redis.set(hash, JSON.stringify(response), 'EX', cache_life);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get(route, {
|
||||
schema: getTokensSchema.GET
|
||||
}, async (request) => {
|
||||
return await getTokens(fastify, request);
|
||||
});
|
||||
next()
|
||||
};
|
||||
@@ -1,90 +0,0 @@
|
||||
const {getVotersSchema} = require("../../schemas");
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
const route = '/get_voters';
|
||||
|
||||
const maxActions = 1000;
|
||||
|
||||
async function getVoters(fastify, request) {
|
||||
|
||||
const t0 = Date.now();
|
||||
const {redis, elastic} = fastify;
|
||||
const [cachedResponse, hash] = await getCacheByHash(redis, route + JSON.stringify(request.query));
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
|
||||
let skip, limit;
|
||||
|
||||
skip = parseInt(request.query.skip, 10);
|
||||
if (skip < 0) {
|
||||
return 'invalid skip parameter';
|
||||
}
|
||||
limit = parseInt(request.query.limit, 10);
|
||||
if (limit < 1) {
|
||||
return 'invalid limit parameter';
|
||||
}
|
||||
|
||||
const response = {
|
||||
query_time: null,
|
||||
voter_count: 0,
|
||||
'voters': []
|
||||
};
|
||||
let queryStruct = {
|
||||
"bool": {
|
||||
"must": []
|
||||
}
|
||||
};
|
||||
|
||||
if (request.query.producer) {
|
||||
for (const bp of request.query.producer.split(",")) {
|
||||
queryStruct.bool.must.push({"term": {"producers": bp}});
|
||||
}
|
||||
}
|
||||
|
||||
if (request.query.proxy === 'true') {
|
||||
queryStruct.bool.must.push({"term": {"is_proxy": true}});
|
||||
}
|
||||
|
||||
if (queryStruct.bool.must.length === 0) {
|
||||
queryStruct = {
|
||||
"match_all": {}
|
||||
};
|
||||
}
|
||||
|
||||
let prefix = process.env.CHAIN;
|
||||
if (process.env.CHAIN === 'mainnet') {
|
||||
prefix = 'eos';
|
||||
}
|
||||
|
||||
const results = await elastic.search({
|
||||
"index": prefix + '-table-voters-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxActions ? maxActions : limit) || 10,
|
||||
"body": {
|
||||
"query": queryStruct,
|
||||
"sort": [{"last_vote_weight": "desc"}]
|
||||
}
|
||||
});
|
||||
const hits = results['body']['hits']['hits'];
|
||||
for (const hit of hits) {
|
||||
const voter = hit._source;
|
||||
response.voters.push({
|
||||
account: voter.voter,
|
||||
weight: voter.last_vote_weight,
|
||||
last_vote: voter.block_num
|
||||
});
|
||||
}
|
||||
response.voter_count = results['body']['hits']['total']['value'];
|
||||
response['query_time'] = Date.now() - t0;
|
||||
redis.set(hash, JSON.stringify(response), 'EX', 30);
|
||||
return response;
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.get(route, {
|
||||
schema: getVotersSchema.GET
|
||||
}, async (request) => {
|
||||
return await getVoters(fastify, request);
|
||||
});
|
||||
next();
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Returns an object containing rows from the specified table.',
|
||||
{
|
||||
"code": {$ref: 'AccountName#'},
|
||||
"action": {$ref: 'AccountName#'},
|
||||
"binargs": {
|
||||
"type": "string",
|
||||
"pattern": "^(0x)(([0-9a-f][0-9a-f])+)?$",
|
||||
"title": "Hex"
|
||||
}
|
||||
},
|
||||
["code", "action", "binargs"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Convert JSON object to binary',
|
||||
{
|
||||
"binargs": {
|
||||
"type": "string",
|
||||
"pattern": "^(0x)(([0-9a-f][0-9a-f])+)?$",
|
||||
"title": "Hex"
|
||||
}
|
||||
},
|
||||
["binargs"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retrieves the ABI for a contract based on its account name',
|
||||
{"account_name": {$ref: 'AccountName#'}},
|
||||
["account_name"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Returns an object containing various details about a specific account on the blockchain.',
|
||||
{
|
||||
"account_name": {$ref: 'AccountName#'}
|
||||
},
|
||||
["account_name"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retreives the activated protocol features for producer node',
|
||||
{
|
||||
"lower_bound": {
|
||||
"type": "integer",
|
||||
"description": "Lower bound"
|
||||
},
|
||||
"upper_bound": {
|
||||
"type": "integer",
|
||||
"description": "Upper bound"
|
||||
},
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"description": "The limit, default is 10"
|
||||
},
|
||||
"search_by_block_num": {
|
||||
"type": "boolean",
|
||||
"description": "Flag to indicate it is has to search by block number"
|
||||
},
|
||||
"reverse": {
|
||||
"type": "boolean",
|
||||
"description": "Flag to indicate it has to search in reverse"
|
||||
}
|
||||
}
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
const schema = {
|
||||
description: 'Returns an object containing various details about a specific block on the blockchain.',
|
||||
summary: 'Returns an object containing various details about a specific block on the blockchain.',
|
||||
tags: ['state'],
|
||||
body: {
|
||||
type: ['object', 'string'],
|
||||
properties: {
|
||||
"block_num_or_id": {
|
||||
description: 'Provide a block number or a block id',
|
||||
type: 'string'
|
||||
},
|
||||
},
|
||||
required: ["block_num_or_id"]
|
||||
}
|
||||
};
|
||||
|
||||
const route = '/get_block';
|
||||
const {blockStore} = require('../blockStore');
|
||||
const {getCacheByHash} = require("../../helpers/functions");
|
||||
|
||||
let last_get_info = 0;
|
||||
let chain_info;
|
||||
|
||||
async function getInfo(eosjs) {
|
||||
if (Date.now() - last_get_info > 1000) {
|
||||
chain_info = await eosjs.rpc.get_info();
|
||||
last_get_info = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
async function processBlockData(block, eosjs) {
|
||||
// Calculate total usage
|
||||
let total_cpu = 0;
|
||||
let total_net = 0;
|
||||
block.transactions.forEach((trx) => {
|
||||
total_cpu += trx['cpu_usage_us'];
|
||||
total_net += trx['net_usage_words'];
|
||||
});
|
||||
block['total_cpu_usage_us'] = total_cpu;
|
||||
block['total_net_usage_words'] = total_net;
|
||||
|
||||
// check irreversibility
|
||||
await getInfo(eosjs);
|
||||
block['last_irreversible_block_num'] = chain_info.last_irreversible_block_num;
|
||||
block['irreversible'] = block['block_num'] <= block['last_irreversible_block_num'];
|
||||
return block;
|
||||
}
|
||||
|
||||
async function getBlock(fastify, request, reply) {
|
||||
const {eosjs, redis} = fastify;
|
||||
let block_num_or_id;
|
||||
try {
|
||||
if (typeof request.body === 'object') {
|
||||
block_num_or_id = request.body['block_num_or_id'];
|
||||
} else {
|
||||
block_num_or_id = JSON.parse(request.body)['block_num_or_id'];
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(request.body);
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
if (parseInt(block_num_or_id) === 0) {
|
||||
await getInfo(eosjs);
|
||||
block_num_or_id = chain_info.head_block_num;
|
||||
}
|
||||
|
||||
if (block_num_or_id) {
|
||||
let cachedResponse, hash;
|
||||
const key = route + '_' + block_num_or_id;
|
||||
[cachedResponse, hash] = await getCacheByHash(redis, key);
|
||||
if (cachedResponse) {
|
||||
cachedResponse = JSON.parse(cachedResponse);
|
||||
if (cachedResponse['irreversible']) {
|
||||
reply.send(cachedResponse);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
let blockdata = await eosjs.rpc.get_block(block_num_or_id);
|
||||
blockdata = await processBlockData(blockdata, eosjs);
|
||||
const blockstring = JSON.stringify(blockdata);
|
||||
if (blockdata['irreversible']) {
|
||||
redis.set(hash, blockstring, 'EX', 86400);
|
||||
} else {
|
||||
redis.set(hash, blockstring, 'EX', 10);
|
||||
}
|
||||
reply.send(blockdata);
|
||||
} catch (e) {
|
||||
reply.code(400).send({
|
||||
"code": 400,
|
||||
"message": "Unknown Block",
|
||||
"error": {
|
||||
"code": 3100002,
|
||||
"name": "unknown_block_exception",
|
||||
"what": "Unknown block",
|
||||
"details": [
|
||||
{
|
||||
"message": "Could not find block: " + block_num_or_id,
|
||||
"file": "chain_plugin.cpp",
|
||||
"line_number": 1852,
|
||||
"method": "get_block"
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} else {
|
||||
reply.code(400).send({});
|
||||
}
|
||||
|
||||
// TODO: fallback to state history
|
||||
// let from_block;
|
||||
// if (request.body['block_num_or_id']) {
|
||||
// from_block = parseInt(request.body['block_num_or_id']);
|
||||
// let response;
|
||||
// console.log('Local Block store size:', blockStore.localBlocks.size);
|
||||
// blockStore.requestRange(from_block, from_block + 1, (block) => {
|
||||
// response = block;
|
||||
// }, () => {
|
||||
// reply.send(response);
|
||||
// });
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
module.exports = function (fastify, opts, next) {
|
||||
fastify.post(route, {schema}, async (request, reply) => {
|
||||
await getBlock(fastify, request, reply);
|
||||
});
|
||||
next();
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Returns an object containing various details about a specific block on the blockchain.',
|
||||
{
|
||||
"block_num_or_id": {
|
||||
description: "Provide a `block number` or a `block id`",
|
||||
type: 'string'
|
||||
}
|
||||
},
|
||||
["block_num_or_id"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retrieves the block header state',
|
||||
{
|
||||
"block_num_or_id": {
|
||||
"type": "string",
|
||||
"description": "Provide a block_number or a block_id"
|
||||
}
|
||||
},
|
||||
["block_num_or_id"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retrieves contract code',
|
||||
{
|
||||
"account_name": {$ref: 'AccountName#'},
|
||||
"code_as_wasm": {
|
||||
"type": "integer",
|
||||
"default": 1,
|
||||
"description": "This must be 1 (true)"
|
||||
}
|
||||
},
|
||||
["account_name", "code_as_wasm"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retrieves the current balance',
|
||||
{
|
||||
"code": {$ref: 'AccountName#'},
|
||||
"account": {$ref: 'AccountName#'},
|
||||
"symbol": {$ref: 'Symbol#'}
|
||||
},
|
||||
["code", "account", "symbol"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retrieves currency stats',
|
||||
{
|
||||
"code": {$ref: 'AccountName#'},
|
||||
"symbol": {
|
||||
description: 'token symbol',
|
||||
type: 'string',
|
||||
}
|
||||
},
|
||||
["code", "symbol"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Returns an object containing various details about the blockchain.'
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retrieves producers list',
|
||||
{
|
||||
"limit": {
|
||||
"type": "string",
|
||||
"description": "total number of producers to retrieve"
|
||||
},
|
||||
"lower_bound": {
|
||||
"type": "string",
|
||||
"description": "In conjunction with limit can be used to paginate through the results. For example, limit=10 and lower_bound=10 would be page 2"
|
||||
},
|
||||
"json": {
|
||||
"type": "boolean",
|
||||
"description": "return result in JSON format"
|
||||
}
|
||||
}
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retrieves raw ABI for a contract based on account name',
|
||||
{
|
||||
"account_name": {$ref: 'AccountName#'}
|
||||
},
|
||||
["account_name"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retrieves raw code and ABI for a contract based on account name',
|
||||
{
|
||||
"account_name": {$ref: 'AccountName#'}
|
||||
},
|
||||
["account_name"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retrieves the scheduled transaction',
|
||||
{
|
||||
"lower_bound": {
|
||||
"type": "string",
|
||||
"description": "Date/time string in the format YYYY-MM-DDTHH:MM:SS.sss",
|
||||
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}$",
|
||||
"title": "DateTimeSeconds"
|
||||
},
|
||||
"limit": {
|
||||
"description": "The maximum number of transactions to return",
|
||||
"type": "integer"
|
||||
},
|
||||
"json": {
|
||||
"description": "true/false whether the packed transaction is converted to json",
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Retrieves table scope',
|
||||
{
|
||||
"code": {
|
||||
"type": "string",
|
||||
"description": "`name` of the contract to return table data for"
|
||||
},
|
||||
"table": {
|
||||
"type": "string",
|
||||
"description": "Filter results by table"
|
||||
},
|
||||
"lower_bound": {
|
||||
"type": "string",
|
||||
"description": "Filters results to return the first element that is not less than provided value in set"
|
||||
},
|
||||
"upper_bound": {
|
||||
"type": "string",
|
||||
"description": "Filters results to return the first element that is greater than provided value in set"
|
||||
},
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"description": "Limit number of results returned."
|
||||
},
|
||||
"reverse": {
|
||||
"type": "boolean",
|
||||
"description": "Reverse the order of returned results"
|
||||
}
|
||||
},
|
||||
["code"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addChainApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addChainApiRoute(
|
||||
fastify,
|
||||
getRouteName(__filename),
|
||||
'Returns an object containing rows from the specified table.',
|
||||
{
|
||||
"code": {
|
||||
"type": "string",
|
||||
"description": "The name of the smart contract that controls the provided table"
|
||||
},
|
||||
"table": {
|
||||
"type": "string",
|
||||
"description": "The name of the table to query"
|
||||
},
|
||||
"scope": {
|
||||
"type": "string",
|
||||
"description": "The account to which this data belongs"
|
||||
},
|
||||
"index_position": {
|
||||
"type": "string",
|
||||
"description": "Position of the index used, accepted parameters `primary`, `secondary`, `tertiary`, `fourth`, `fifth`, `sixth`, `seventh`, `eighth`, `ninth` , `tenth`"
|
||||
},
|
||||
"key_type": {
|
||||
"type": "string",
|
||||
"description": "Type of key specified by index_position (for example - `uint64_t` or `name`)"
|
||||
},
|
||||
"encode_type": {
|
||||
"type": "string"
|
||||
},
|
||||
"upper_bound": {
|
||||
"type": "string"
|
||||
},
|
||||
"lower_bound": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
["code", "table", "scope"]
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, chainApiHandler, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'POST',
|
||||
getRouteName(__filename),
|
||||
chainApiHandler,
|
||||
{
|
||||
description: "This method expects a transaction in JSON format and will attempt to apply it to the blockchain.",
|
||||
summary: "This method expects a transaction in JSON format and will attempt to apply it to the blockchain.",
|
||||
tags: ['chain'],
|
||||
body: {
|
||||
type: ['object', 'string'],
|
||||
properties: {
|
||||
signatures: {
|
||||
"type": "array",
|
||||
"description": "array of signatures required to authorize transaction",
|
||||
"items": {$ref: 'Signature#'}
|
||||
},
|
||||
compression: {
|
||||
"type": "boolean",
|
||||
"description": "Compression used, usually false"
|
||||
},
|
||||
packed_context_free_data: {
|
||||
"type": "string",
|
||||
"description": "json to hex"
|
||||
},
|
||||
packed_trx: {
|
||||
"type": "string",
|
||||
"description": "Transaction object json to hex"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, chainApiHandler, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'POST',
|
||||
getRouteName(__filename),
|
||||
chainApiHandler,
|
||||
{
|
||||
description: "This method expects a transaction in JSON format and will attempt to apply it to the blockchain.",
|
||||
summary: "This method expects a transaction in JSON format and will attempt to apply it to the blockchain.",
|
||||
tags: ['chain'],
|
||||
body: {
|
||||
type: ['array', 'object', 'string'],
|
||||
items: {
|
||||
type: ['object', 'string'],
|
||||
additionalProperties: false,
|
||||
minProperties: 8,
|
||||
required: [
|
||||
"expiration",
|
||||
"ref_block_num",
|
||||
"ref_block_prefix",
|
||||
"max_net_usage_words",
|
||||
"max_cpu_usage_ms",
|
||||
"delay_sec",
|
||||
"context_free_actions",
|
||||
"actions"
|
||||
],
|
||||
properties: {
|
||||
"expiration": {$ref: 'Expiration#'},
|
||||
"ref_block_num": {"type": "integer"},
|
||||
"ref_block_prefix": {"type": "integer"},
|
||||
"max_net_usage_words": {$ref: 'WholeNumber#'},
|
||||
"max_cpu_usage_ms": {$ref: 'WholeNumber#'},
|
||||
"delay_sec": {"type": "integer"},
|
||||
"context_free_actions": {
|
||||
"type": "array",
|
||||
"items": {$ref: 'ActionItems#'}
|
||||
},
|
||||
"actions": {
|
||||
"type": "array",
|
||||
"items": {$ref: 'ActionItems#'}
|
||||
},
|
||||
"transaction_extensions": {
|
||||
"type": "array",
|
||||
"items": {$ref: 'BlockExtensions#'}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, chainApiHandler, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'POST',
|
||||
getRouteName(__filename),
|
||||
chainApiHandler,
|
||||
{
|
||||
description: "This method expects a transaction in JSON format and will attempt to apply it to the blockchain.",
|
||||
summary: "This method expects a transaction in JSON format and will attempt to apply it to the blockchain.",
|
||||
tags: ['chain'],
|
||||
body: {
|
||||
type: ['object', 'string'],
|
||||
properties: {
|
||||
signatures: {
|
||||
"type": "array",
|
||||
"description": "array of signatures required to authorize transaction",
|
||||
"items": {$ref: 'Signature#'}
|
||||
},
|
||||
compression: {
|
||||
"type": "boolean",
|
||||
"description": "Compression used, usually false"
|
||||
},
|
||||
packed_context_free_data: {
|
||||
"type": "string",
|
||||
"description": "json to hex"
|
||||
},
|
||||
packed_trx: {
|
||||
"type": "string",
|
||||
"description": "Transaction object json to hex"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -1,22 +1,24 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {mergeActionMeta, timedQuery} from "../../../helpers/functions";
|
||||
import flatstr from 'flatstr';
|
||||
|
||||
const terms = ["notified", "act.authorization.actor"];
|
||||
const extendedActions = new Set(["transfer", "newaccount", "updateauth"]);
|
||||
|
||||
async function getActions(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
if (typeof request.body === 'string') {
|
||||
request.body = JSON.parse(request.body);
|
||||
}
|
||||
const reqBody = request.body;
|
||||
|
||||
const reqBody = request.body as any;
|
||||
const should_array = [];
|
||||
for (const entry of terms) {
|
||||
const tObj = {term: {}};
|
||||
tObj.term[entry] = reqBody.account_name;
|
||||
should_array.push(tObj);
|
||||
}
|
||||
let code, method, pos, offset, parent;
|
||||
let code, method, pos, offset;
|
||||
let sort_direction = 'desc';
|
||||
let filterObj = [];
|
||||
if (reqBody.filter) {
|
||||
@@ -172,7 +174,7 @@ async function getActions(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
};
|
||||
mergeActionMeta(action);
|
||||
act.action_trace.act = action.act;
|
||||
act.action_trace.act.hex_data = Buffer.from(JSON.stringify(action.act.data)).toString('hex');
|
||||
act.action_trace.act.hex_data = Buffer.from(flatstr(JSON.stringify(action.act.data))).toString('hex');
|
||||
if (action.act.account_ram_deltas) {
|
||||
act.action_trace.account_ram_deltas = action.account_ram_deltas
|
||||
}
|
||||
@@ -188,7 +190,7 @@ async function getActions(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
}
|
||||
|
||||
export function getActionsHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getActions, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addApiRoute(fastify, 'POST', getRouteName(__filename), getActionsHandler, {
|
||||
description: 'legacy get actions query',
|
||||
summary: 'get actions',
|
||||
tags: ['actions', 'history'],
|
||||
tags: ['history'],
|
||||
body: {
|
||||
type: ['object', 'string'],
|
||||
properties: {
|
||||
|
||||
@@ -1,72 +1,72 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
import {ApiResponse} from "@elastic/elasticsearch";
|
||||
|
||||
async function getControlledAccounts(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
if (typeof request.body === 'string') {
|
||||
request.body = JSON.parse(request.body)
|
||||
}
|
||||
let controlling_account = request.body.controlling_account;
|
||||
const results: ApiResponse = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-action-*',
|
||||
size: 100,
|
||||
body: {
|
||||
query: {
|
||||
bool: {
|
||||
should: [
|
||||
{
|
||||
term: {"@updateauth.auth.accounts.permission.actor": controlling_account}},
|
||||
{
|
||||
bool: {
|
||||
must: [
|
||||
{term: {"act.account": "eosio"}},
|
||||
{term: {"act.name": "newaccount"}},
|
||||
{term: {"act.authorization.actor": controlling_account}}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
minimum_should_match: 1
|
||||
}
|
||||
},
|
||||
sort: [
|
||||
{"global_sequence": {"order": "desc"}}
|
||||
]
|
||||
}
|
||||
});
|
||||
if (typeof request.body === 'string') {
|
||||
request.body = JSON.parse(request.body)
|
||||
}
|
||||
let controlling_account = request.body["controlling_account"];
|
||||
const results: ApiResponse = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-action-*',
|
||||
size: 100,
|
||||
body: {
|
||||
query: {
|
||||
bool: {
|
||||
should: [
|
||||
{
|
||||
term: {"@updateauth.auth.accounts.permission.actor": controlling_account}
|
||||
},
|
||||
{
|
||||
bool: {
|
||||
must: [
|
||||
{term: {"act.account": "eosio"}},
|
||||
{term: {"act.name": "newaccount"}},
|
||||
{term: {"act.authorization.actor": controlling_account}}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
minimum_should_match: 1
|
||||
}
|
||||
},
|
||||
sort: [
|
||||
{"global_sequence": {"order": "desc"}}
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
const response = {
|
||||
controlled_accounts: []
|
||||
};
|
||||
const response = {
|
||||
controlled_accounts: []
|
||||
};
|
||||
|
||||
const hits = results.body.hits.hits;
|
||||
const hits = results.body.hits.hits;
|
||||
|
||||
if (hits.length > 0) {
|
||||
response.controlled_accounts = hits.map((v) => {
|
||||
if (v._source.act.name === 'newaccount') {
|
||||
if (v._source['@newaccount'].newact) {
|
||||
return v._source['@newaccount'].newact;
|
||||
} else if (v._source.act.data.newact) {
|
||||
return v._source.act.data.newact;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else if (v._source.act.name === 'updateauth') {
|
||||
return v._source.act.data.account;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
if (response.controlled_accounts.length > 0) {
|
||||
response.controlled_accounts = [...(new Set(response.controlled_accounts))];
|
||||
}
|
||||
return response;
|
||||
if (hits.length > 0) {
|
||||
response.controlled_accounts = hits.map((v) => {
|
||||
if (v._source.act.name === 'newaccount') {
|
||||
if (v._source['@newaccount'].newact) {
|
||||
return v._source['@newaccount'].newact;
|
||||
} else if (v._source.act.data.newact) {
|
||||
return v._source.act.data.newact;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else if (v._source.act.name === 'updateauth') {
|
||||
return v._source.act.data.account;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
if (response.controlled_accounts.length > 0) {
|
||||
response.controlled_accounts = [...(new Set(response.controlled_accounts))];
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getControlledAccountsHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
reply.send(await timedQuery(getControlledAccounts, fastify, request, route));
|
||||
}
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getControlledAccounts, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addApiRoute(fastify, 'POST', getRouteName(__filename), getControlledAccountsHandler, {
|
||||
description: 'get controlled accounts by controlling accounts',
|
||||
summary: 'get controlled accounts by controlling accounts',
|
||||
tags: ['state'],
|
||||
tags: ['accounts'],
|
||||
body: {
|
||||
type: ['object', 'string'],
|
||||
properties: {
|
||||
|
||||
@@ -8,9 +8,9 @@ export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addApiRoute(fastify, 'POST', getRouteName(__filename), getKeyAccountsHandler, {
|
||||
description: 'get accounts by public key',
|
||||
summary: 'get accounts by public key',
|
||||
tags: ['accounts', 'state'],
|
||||
tags: ['accounts'],
|
||||
body: {
|
||||
type: 'object',
|
||||
type: ['object', 'string'],
|
||||
properties: {"public_key": {description: 'public key', type: 'string'}},
|
||||
required: ["public_key"]
|
||||
},
|
||||
|
||||
@@ -1,192 +1,263 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {mergeActionMeta, timedQuery} from "../../../helpers/functions";
|
||||
import {createHash} from "crypto";
|
||||
import flatstr from 'flatstr';
|
||||
import {GetInfoResult} from "eosjs/dist/eosjs-rpc-interfaces";
|
||||
|
||||
async function getTransaction(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
if (typeof request.body === 'string') {
|
||||
request.body = JSON.parse(request.body)
|
||||
}
|
||||
const pResults = await Promise.all([fastify.eosjs.rpc.get_info(), fastify.elastic['search']({
|
||||
"index": fastify.manager.chain + '-action-*',
|
||||
"body": {
|
||||
"query": {
|
||||
"bool": {
|
||||
must: [
|
||||
{term: {"trx_id": request.body.id.toLowerCase()}}
|
||||
]
|
||||
}
|
||||
},
|
||||
"sort": {
|
||||
"global_sequence": "asc"
|
||||
}
|
||||
}
|
||||
})]);
|
||||
const results = pResults[1];
|
||||
const response: any = {
|
||||
"id": request.body.id,
|
||||
"trx": {
|
||||
"receipt": {
|
||||
"status": "executed",
|
||||
"cpu_usage_us": 0,
|
||||
"net_usage_words": 0,
|
||||
"trx": [1, {
|
||||
"signatures": "",
|
||||
"compression": "none",
|
||||
"packed_context_free_data": "",
|
||||
"packed_trx": ""
|
||||
}]
|
||||
},
|
||||
"trx": {}
|
||||
},
|
||||
"block_num": 0,
|
||||
"block_time": "",
|
||||
"last_irreversible_block": pResults[0].last_irreversible_block_num,
|
||||
"traces": []
|
||||
};
|
||||
if (typeof request.body === 'string') {
|
||||
request.body = JSON.parse(request.body)
|
||||
}
|
||||
const body: any = request.body;
|
||||
const redis = fastify.redis;
|
||||
const trxId = body.id.toLowerCase();
|
||||
const conf = fastify.manager.config;
|
||||
const cachedData = await redis.hgetall('trx_' + trxId);
|
||||
|
||||
const hits = results['body']['hits']['hits'];
|
||||
const response: any = {
|
||||
"id": body.id,
|
||||
"trx": {
|
||||
"receipt": {
|
||||
"status": "executed",
|
||||
"cpu_usage_us": 0,
|
||||
"net_usage_words": 0,
|
||||
"trx": [1, {
|
||||
"signatures": "",
|
||||
"compression": "none",
|
||||
"packed_context_free_data": "",
|
||||
"packed_trx": ""
|
||||
}]
|
||||
},
|
||||
"trx": {}
|
||||
},
|
||||
"block_num": 0,
|
||||
"block_time": "",
|
||||
"last_irreversible_block": undefined,
|
||||
"traces": []
|
||||
};
|
||||
|
||||
if (hits.length > 0) {
|
||||
const actions = hits;
|
||||
response.trx.trx = {
|
||||
"expiration": "",
|
||||
"ref_block_num": 0,
|
||||
"ref_block_prefix": 0,
|
||||
"max_net_usage_words": 0,
|
||||
"max_cpu_usage_ms": 0,
|
||||
"delay_sec": 0,
|
||||
"context_free_actions": [],
|
||||
"actions": [],
|
||||
"transaction_extensions": [],
|
||||
"signatures": [],
|
||||
"context_free_data": []
|
||||
};
|
||||
let traces = {};
|
||||
let seqNum = 0;
|
||||
for (let action of actions) {
|
||||
action = action._source;
|
||||
mergeActionMeta(action);
|
||||
action.act['hex_data'] = Buffer.from(JSON.stringify(action.act.data)).toString('hex');
|
||||
if (action.parent === 0) {
|
||||
response.trx.trx.actions.push(action.act);
|
||||
}
|
||||
response.block_num = action.block_num;
|
||||
response.block_time = action['@timestamp'];
|
||||
seqNum += 1;
|
||||
let trace = {
|
||||
receipt: {
|
||||
receiver: action.act.account,
|
||||
global_sequence: String(action.global_sequence),
|
||||
auth_sequence: [
|
||||
action.act.authorization[0].actor,
|
||||
seqNum
|
||||
],
|
||||
act_digest: '',
|
||||
recv_sequence: seqNum,
|
||||
code_sequence: 1,
|
||||
abi_sequence: 1
|
||||
},
|
||||
act: action.act,
|
||||
account_ram_deltas: action.account_ram_deltas || [],
|
||||
context_free: false,
|
||||
block_num: action.block_num,
|
||||
block_time: action['@timestamp'],
|
||||
console: "",
|
||||
elapsed: 0,
|
||||
except: null,
|
||||
inline_traces: [],
|
||||
producer_block_id: "",
|
||||
trx_id: request.body.id,
|
||||
notified: action.notified
|
||||
};
|
||||
let hash = createHash('sha256');
|
||||
hash.update(JSON.stringify(action.act));
|
||||
trace.receipt.act_digest = hash.digest('hex');
|
||||
traces[action.global_sequence] = trace;
|
||||
}
|
||||
let hits;
|
||||
|
||||
actions.forEach(action => {
|
||||
action = action._source;
|
||||
for (let i = 0; i < traces[action.global_sequence].notified.length; i++) {
|
||||
if (traces[action.global_sequence].notified[i] === action.act.account) {
|
||||
traces[action.global_sequence].notified.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (action.parent !== 0 && action.parent) {
|
||||
if (traces[action.parent]) {
|
||||
for (let i = 0; i < traces[action.parent].notified.length; i++) {
|
||||
if (traces[action.parent].notified[i] === action.act.account) {
|
||||
traces[action.parent].notified.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
traces[action.parent].inline_traces.push(traces[action.global_sequence]);
|
||||
}
|
||||
}
|
||||
});
|
||||
// build get_info request with caching
|
||||
const $getInfo = new Promise<GetInfoResult>(resolve => {
|
||||
const key = `${fastify.manager.chain}_get_info`;
|
||||
fastify.redis.get(key).then(value => {
|
||||
if (value) {
|
||||
resolve(JSON.parse(value));
|
||||
} else {
|
||||
fastify.eosjs.rpc.get_info().then(value1 => {
|
||||
fastify.redis.set(key, JSON.stringify(value1), 'EX', 6);
|
||||
resolve(value1);
|
||||
}).catch((reason) => {
|
||||
console.log(reason);
|
||||
response.error = 'failed to get last_irreversible_block_num'
|
||||
resolve(null);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
actions.forEach(action => {
|
||||
action = action._source;
|
||||
response.traces.push(traces[action.global_sequence]);
|
||||
traces[action.global_sequence].notified.forEach((note, index) => {
|
||||
seqNum += 1;
|
||||
let trace = {
|
||||
receipt: {
|
||||
receiver: note,
|
||||
global_sequence: String(action.global_sequence + index + 1),
|
||||
auth_sequence: [
|
||||
action.act.authorization[0].actor,
|
||||
seqNum
|
||||
],
|
||||
act_digest: traces[action.global_sequence].receipt.act_digest,
|
||||
recv_sequence: seqNum,
|
||||
code_sequence: 1,
|
||||
abi_sequence: 1
|
||||
},
|
||||
account_ram_deltas: action.account_ram_deltas || [],
|
||||
act: action.act,
|
||||
block_num: action.block_num,
|
||||
block_time: action['@timestamp'],
|
||||
console: "",
|
||||
context_free: false,
|
||||
elapsed: 0,
|
||||
except: null,
|
||||
inline_traces: [],
|
||||
producer_block_id: "",
|
||||
trx_id: request.body.id,
|
||||
};
|
||||
traces[action.global_sequence].inline_traces.unshift(trace);
|
||||
response.traces.push(trace);
|
||||
});
|
||||
delete traces[action.global_sequence].notified;
|
||||
});
|
||||
} else {
|
||||
const errmsg = "Transaction " + request.body.id.toLowerCase() + " not found in history and no block hint was given";
|
||||
return {
|
||||
code: 500,
|
||||
message: "Internal Service Error",
|
||||
error: {
|
||||
code: 3040011,
|
||||
name: "tx_not_found",
|
||||
what: "The transaction can not be found",
|
||||
details: [
|
||||
{
|
||||
message: errmsg,
|
||||
file: "",
|
||||
line_number: 1,
|
||||
method: "get_transaction"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
return response;
|
||||
// reconstruct hits from cached data
|
||||
if (cachedData && Object.keys(cachedData).length > 0) {
|
||||
const gsArr = [];
|
||||
for (let cachedDataKey in cachedData) {
|
||||
gsArr.push(cachedData[cachedDataKey]);
|
||||
}
|
||||
gsArr.sort((a, b) => {
|
||||
return a.global_sequence - b.global_sequence;
|
||||
});
|
||||
hits = gsArr.map(value => {
|
||||
return {
|
||||
_source: JSON.parse(value)
|
||||
};
|
||||
});
|
||||
const promiseResults = await Promise.all([
|
||||
redis.ttl('trx_' + trxId),
|
||||
$getInfo
|
||||
]);
|
||||
response.cache_expires_in = promiseResults[0];
|
||||
response.last_irreversible_block = promiseResults[1].last_irreversible_block_num;
|
||||
}
|
||||
|
||||
// search on ES if cache is not present
|
||||
if (!hits) {
|
||||
const _size = conf.api.limits.get_trx_actions || 100;
|
||||
const blockHint = parseInt(body.block_num_hint, 10);
|
||||
let indexPattern = '';
|
||||
if (blockHint) {
|
||||
const idxPart = Math.ceil(blockHint / conf.settings.index_partition_size).toString().padStart(6, '0');
|
||||
indexPattern = fastify.manager.chain + `-action-${conf.settings.index_version}-${idxPart}`;
|
||||
} else {
|
||||
indexPattern = fastify.manager.chain + '-action-*';
|
||||
}
|
||||
let pResults;
|
||||
try {
|
||||
|
||||
// build search request
|
||||
const $search = fastify.elastic.search({
|
||||
index: indexPattern,
|
||||
size: _size,
|
||||
body: {
|
||||
query: {bool: {must: [{term: {trx_id: trxId}}]}},
|
||||
sort: {global_sequence: "asc"}
|
||||
}
|
||||
});
|
||||
|
||||
// execute in parallel
|
||||
pResults = await Promise.all([$getInfo, $search]);
|
||||
} catch (e) {
|
||||
console.log(e.message);
|
||||
if (e.meta.statusCode === 404) {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
hits = pResults[1]['body']['hits']['hits'];
|
||||
response.last_irreversible_block = pResults[0].last_irreversible_block_num;
|
||||
}
|
||||
|
||||
|
||||
if (hits.length > 0) {
|
||||
const actions = hits;
|
||||
response.trx.trx = {
|
||||
"expiration": "",
|
||||
"ref_block_num": 0,
|
||||
"ref_block_prefix": 0,
|
||||
"max_net_usage_words": 0,
|
||||
"max_cpu_usage_ms": 0,
|
||||
"delay_sec": 0,
|
||||
"context_free_actions": [],
|
||||
"actions": [],
|
||||
"transaction_extensions": [],
|
||||
"signatures": [],
|
||||
"context_free_data": []
|
||||
};
|
||||
let traces = {};
|
||||
let seqNum = 0;
|
||||
for (let action of actions) {
|
||||
action = action._source;
|
||||
mergeActionMeta(action);
|
||||
action.act['hex_data'] = Buffer.from(flatstr(JSON.stringify(action.act.data))).toString('hex');
|
||||
if (action.parent === 0) {
|
||||
response.trx.trx.actions.push(action.act);
|
||||
}
|
||||
response.block_num = action.block_num;
|
||||
response.block_time = action['@timestamp'];
|
||||
seqNum += 1;
|
||||
let trace = {
|
||||
receipt: {
|
||||
receiver: action.act.account,
|
||||
global_sequence: String(action.global_sequence),
|
||||
auth_sequence: [
|
||||
action.act.authorization[0].actor,
|
||||
seqNum
|
||||
],
|
||||
act_digest: '',
|
||||
recv_sequence: seqNum,
|
||||
code_sequence: 1,
|
||||
abi_sequence: 1
|
||||
},
|
||||
act: action.act,
|
||||
account_ram_deltas: action.account_ram_deltas || [],
|
||||
context_free: false,
|
||||
block_num: action.block_num,
|
||||
block_time: action['@timestamp'],
|
||||
console: "",
|
||||
elapsed: 0,
|
||||
except: null,
|
||||
inline_traces: [],
|
||||
producer_block_id: "",
|
||||
trx_id: body.id,
|
||||
notified: action.notified
|
||||
};
|
||||
let hash = createHash('sha256');
|
||||
hash.update(JSON.stringify(action.act));
|
||||
trace.receipt.act_digest = hash.digest('hex');
|
||||
traces[action.global_sequence] = trace;
|
||||
}
|
||||
|
||||
actions.forEach(action => {
|
||||
action = action._source;
|
||||
for (let i = 0; i < traces[action.global_sequence].notified.length; i++) {
|
||||
if (traces[action.global_sequence].notified[i] === action.act.account) {
|
||||
traces[action.global_sequence].notified.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (action.parent !== 0 && action.parent) {
|
||||
if (traces[action.parent]) {
|
||||
for (let i = 0; i < traces[action.parent].notified.length; i++) {
|
||||
if (traces[action.parent].notified[i] === action.act.account) {
|
||||
traces[action.parent].notified.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
traces[action.parent].inline_traces.push(traces[action.global_sequence]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
actions.forEach(action => {
|
||||
action = action._source;
|
||||
response.traces.push(traces[action.global_sequence]);
|
||||
if (traces[action.global_sequence] && traces[action.global_sequence].notified) {
|
||||
traces[action.global_sequence].notified.forEach((note, index) => {
|
||||
seqNum += 1;
|
||||
let trace = {
|
||||
receipt: {
|
||||
receiver: note,
|
||||
global_sequence: String(action.global_sequence + index + 1),
|
||||
auth_sequence: [
|
||||
action.act.authorization[0].actor,
|
||||
seqNum
|
||||
],
|
||||
act_digest: traces[action.global_sequence].receipt.act_digest,
|
||||
recv_sequence: seqNum,
|
||||
code_sequence: 1,
|
||||
abi_sequence: 1
|
||||
},
|
||||
account_ram_deltas: action.account_ram_deltas || [],
|
||||
act: action.act,
|
||||
block_num: action.block_num,
|
||||
block_time: action['@timestamp'],
|
||||
console: "",
|
||||
context_free: false,
|
||||
elapsed: 0,
|
||||
except: null,
|
||||
inline_traces: [],
|
||||
producer_block_id: "",
|
||||
trx_id: body.id,
|
||||
};
|
||||
traces[action.global_sequence].inline_traces.unshift(trace);
|
||||
response.traces.push(trace);
|
||||
});
|
||||
delete traces[action.global_sequence].notified;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const errmsg = "Transaction " + body.id.toLowerCase() + " not found in history and no block hint was given";
|
||||
return {
|
||||
code: 500,
|
||||
message: "Internal Service Error",
|
||||
error: {
|
||||
code: 3040011,
|
||||
name: "tx_not_found",
|
||||
what: "The transaction can not be found",
|
||||
details: [
|
||||
{
|
||||
message: errmsg,
|
||||
file: "",
|
||||
line_number: 1,
|
||||
method: "get_transaction"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getTransactionHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
reply.send(await timedQuery(getTransaction, fastify, request, route));
|
||||
}
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getTransaction, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,13 @@ export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
addApiRoute(fastify, 'POST', getRouteName(__filename), getTransactionHandler, {
|
||||
description: 'get all actions belonging to the same transaction',
|
||||
summary: 'get transaction by id',
|
||||
tags: ['transactions', 'history'],
|
||||
tags: ['history'],
|
||||
body: {
|
||||
type: ['object', 'string'],
|
||||
properties: {id: {description: 'transaction id', type: 'string'}},
|
||||
properties: {
|
||||
id: {description: 'transaction id', type: 'string'},
|
||||
block_num_hint: {description: 'block number hint', type: 'integer'},
|
||||
},
|
||||
required: ["id"]
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
import * as _ from "lodash";
|
||||
|
||||
interface getBlockTraceResponse {
|
||||
id: string;
|
||||
number: number;
|
||||
previous_id: string;
|
||||
status: string;
|
||||
timestamp: string;
|
||||
producer: string;
|
||||
transactions: any[];
|
||||
}
|
||||
|
||||
async function getBlockTrace(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
if (typeof request.body === 'string') {
|
||||
request.body = JSON.parse(request.body);
|
||||
}
|
||||
|
||||
const reqBody: any = request.body;
|
||||
const targetBlock = parseInt(reqBody.block_num);
|
||||
let searchBody;
|
||||
|
||||
if (reqBody.block_id) {
|
||||
searchBody = {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{term: {block_id: reqBody.block_id}}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
||||
} else {
|
||||
if (targetBlock > 0) {
|
||||
searchBody = {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{term: {block_num: targetBlock}}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
||||
} else if (targetBlock < 0) {
|
||||
searchBody = {
|
||||
query: {match_all: {}},
|
||||
sort: {block_num: "desc"}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const response = {transactions: []} as getBlockTraceResponse;
|
||||
|
||||
if (searchBody) {
|
||||
|
||||
const getBlockHeader = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + "-block-*",
|
||||
size: 1,
|
||||
body: searchBody
|
||||
});
|
||||
|
||||
if (getBlockHeader.body.hits.hits.length === 1) {
|
||||
|
||||
const block = getBlockHeader.body.hits.hits[0]._source;
|
||||
const info = await fastify.eosjs.rpc.get_info();
|
||||
|
||||
response.id = block.block_id;
|
||||
response.number = block.block_num;
|
||||
response.previous_id = block.prev_id;
|
||||
response.status = info.last_irreversible_block_num > block.block_num ? "irreversible" : "pending"
|
||||
response.timestamp = block['@timestamp'];
|
||||
response.producer = block.producer;
|
||||
|
||||
// lookup all actions on block
|
||||
const getActionsResponse = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + "-action-*",
|
||||
size: fastify.manager.config.api.limits.get_actions || 1000,
|
||||
body: {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{term: {block_num: block.block_num}}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: {global_sequence: "asc"}
|
||||
}
|
||||
});
|
||||
|
||||
const hits = getActionsResponse.body.hits.hits;
|
||||
if (hits.length > 0) {
|
||||
_.forEach(_.groupBy(hits, (v) => v['_source']['trx_id']), (value, key) => {
|
||||
const actArray = [];
|
||||
for (const act of value) {
|
||||
const action = act['_source']
|
||||
for (const receipt of action.receipts) {
|
||||
actArray.push({
|
||||
receiver: receipt.receiver,
|
||||
account: action.act.account,
|
||||
action: action.act.name,
|
||||
authorization: action.act.authorization.map(auth => {
|
||||
return {account: auth.actor, permission: auth.permission};
|
||||
}),
|
||||
data: action.act.data
|
||||
});
|
||||
}
|
||||
}
|
||||
response.transactions.push({id: key, actions: actArray});
|
||||
});
|
||||
}
|
||||
return response;
|
||||
} else {
|
||||
throw new Error('block not found!');
|
||||
}
|
||||
} else {
|
||||
throw new Error("invalid block number or id");
|
||||
}
|
||||
}
|
||||
|
||||
export function getBlockTraceHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getBlockTrace, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
import {getBlockTraceHandler} from "./get_block";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get block traces',
|
||||
summary: 'get block traces',
|
||||
tags: ['history'],
|
||||
body: {
|
||||
type: ['object', 'string'],
|
||||
properties: {
|
||||
"block_num": {
|
||||
description: 'block number',
|
||||
type: 'integer'
|
||||
},
|
||||
"block_id": {
|
||||
description: 'block id',
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
},
|
||||
response: extendResponseSchema({
|
||||
"id": {type: "string"},
|
||||
"number": {type: "integer"},
|
||||
"previous_id": {type: "string"},
|
||||
"status": {type: "string"},
|
||||
"timestamp": {type: "string"},
|
||||
"producer": {type: "string"},
|
||||
"transactions": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"id": {type: "string"},
|
||||
"actions": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"receiver": {type: "string"},
|
||||
"account": {type: "string"},
|
||||
"action": {type: "string"},
|
||||
"authorization": {
|
||||
type: "array", items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"account": {type: "string"},
|
||||
"permission": {type: "string"},
|
||||
}
|
||||
}
|
||||
},
|
||||
"data": {
|
||||
additionalProperties: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'POST',
|
||||
getRouteName(__filename),
|
||||
getBlockTraceHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
|
||||
async function checkTransaction(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
const query: any = request.query;
|
||||
const txId = query.id.toLowerCase();
|
||||
const data = await fastify.redis.get(txId) as any;
|
||||
if (data) {
|
||||
const jsonObj = JSON.parse(data);
|
||||
const response = {
|
||||
id: txId,
|
||||
status: jsonObj.status,
|
||||
block_num: jsonObj.b,
|
||||
root_action: jsonObj.a,
|
||||
signatures: [],
|
||||
};
|
||||
if (jsonObj.s && jsonObj.s.length > 0) {
|
||||
response.signatures = jsonObj.s;
|
||||
}
|
||||
return response;
|
||||
} else {
|
||||
return {
|
||||
id: txId,
|
||||
status: 'unknown'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function checkTransactionHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(checkTransaction, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
import {checkTransactionHandler} from "./check_transaction";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'check if a transaction was included in a block',
|
||||
summary: 'check if a transaction was included in a block',
|
||||
tags: ['history'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"id": {
|
||||
description: 'transaction id',
|
||||
type: 'string',
|
||||
minLength: 64,
|
||||
maxLength: 64
|
||||
}
|
||||
},
|
||||
required: ["id"]
|
||||
},
|
||||
response: extendResponseSchema({
|
||||
"id": {type: "string"},
|
||||
"status": {type: "string"},
|
||||
"block_num": {type: "number"},
|
||||
"root_action": {
|
||||
type: "object",
|
||||
properties: {
|
||||
"account": {type: "string"},
|
||||
"name": {type: "string"},
|
||||
"authorization": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"actor": {type: "string"},
|
||||
"permission": {type: "string"}
|
||||
}
|
||||
}
|
||||
},
|
||||
"data": {type: "string"}
|
||||
}
|
||||
},
|
||||
"signatures": {type: "array", items: {type: 'string'}}
|
||||
})
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
checkTransactionHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
|
||||
async function exportActions(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
}
|
||||
|
||||
export function exportActionsHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(exportActions, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import {FastifyInstance, FastifySchema} from "fastify";
|
||||
import {addApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
import {getCreatorHandler} from "../get_creator/get_creator";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema: FastifySchema = {
|
||||
description: 'request large action data export',
|
||||
summary: 'request large action data export',
|
||||
tags: ['history']
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
getCreatorHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
|
||||
async function getAbiSnapshot(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
const response = {
|
||||
block_num: null
|
||||
};
|
||||
|
||||
const query: any = request.query;
|
||||
|
||||
const code = query.contract;
|
||||
const block = query.block;
|
||||
const should_fetch = query.fetch;
|
||||
|
||||
const mustArray = [];
|
||||
|
||||
mustArray.push({"term": {"account": code}});
|
||||
|
||||
if (block) {
|
||||
mustArray.push({"range": {"block": {"lte": parseInt(block)}}});
|
||||
}
|
||||
|
||||
const results = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-abi-*',
|
||||
size: 1,
|
||||
body: {
|
||||
query: {bool: {must: mustArray}},
|
||||
sort: [{block: {order: "desc"}}]
|
||||
}
|
||||
});
|
||||
if (results['body']['hits']['hits'].length > 0) {
|
||||
if (should_fetch) {
|
||||
response['abi'] = JSON.parse(results['body']['hits']['hits'][0]['_source']['abi']);
|
||||
} else {
|
||||
response['present'] = true;
|
||||
}
|
||||
response.block_num = results['body']['hits']['hits'][0]['_source']['block'];
|
||||
} else {
|
||||
response['present'] = false;
|
||||
response['error'] = 'abi not found for ' + code + ' until block ' + block;
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getAbiSnapshotHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getAbiSnapshot, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
import {getAbiSnapshotHandler} from "./get_abi_snapshot";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'fetch contract abi at specific block',
|
||||
summary: 'fetch abi at specific block',
|
||||
tags: ['history'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"contract": {
|
||||
description: 'contract account',
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
maxLength: 12
|
||||
},
|
||||
"block": {
|
||||
description: 'target block',
|
||||
type: 'integer',
|
||||
minimum: 1
|
||||
},
|
||||
"fetch": {
|
||||
description: 'should fetch the ABI',
|
||||
type: 'boolean'
|
||||
}
|
||||
},
|
||||
required: ["contract"]
|
||||
}
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
getAbiSnapshotHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
export const terms = [
|
||||
"notified.keyword",
|
||||
"notified",
|
||||
"act.authorization.actor"
|
||||
];
|
||||
|
||||
@@ -8,7 +8,10 @@ export const extendedActions = new Set([
|
||||
"newaccount",
|
||||
"updateauth",
|
||||
"buyram",
|
||||
"buyrambytes"
|
||||
"buyrambytes",
|
||||
"delegatebw",
|
||||
"undelegatebw",
|
||||
"voteproducer"
|
||||
]);
|
||||
|
||||
export const primaryTerms = [
|
||||
@@ -20,5 +23,6 @@ export const primaryTerms = [
|
||||
"creator_action_ordinal",
|
||||
"action_ordinal",
|
||||
"cpu_usage_us",
|
||||
"net_usage_words"
|
||||
"net_usage_words",
|
||||
"trx_id"
|
||||
];
|
||||
|
||||
@@ -1,213 +1,236 @@
|
||||
import {extendedActions, primaryTerms, terms} from "./definitions";
|
||||
import {primaryTerms, terms} from "./definitions";
|
||||
|
||||
export function addSortedBy(query, queryBody, sort_direction) {
|
||||
if (query['sortedBy']) {
|
||||
const opts = query['sortedBy'].split(":");
|
||||
const sortedByObj = {};
|
||||
sortedByObj[opts[0]] = opts[1];
|
||||
queryBody['sort'] = sortedByObj;
|
||||
} else {
|
||||
queryBody['sort'] = {
|
||||
"global_sequence": sort_direction
|
||||
};
|
||||
}
|
||||
if (query['sortedBy']) {
|
||||
const opts = query['sortedBy'].split(":");
|
||||
const sortedByObj = {};
|
||||
sortedByObj[opts[0]] = opts[1];
|
||||
queryBody['sort'] = sortedByObj;
|
||||
} else {
|
||||
queryBody['sort'] = {
|
||||
"global_sequence": sort_direction
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function processMultiVars(queryStruct, parts, field) {
|
||||
const must = [];
|
||||
const mustNot = [];
|
||||
const must = [];
|
||||
const mustNot = [];
|
||||
|
||||
parts.forEach(part => {
|
||||
if (part.startsWith("!")) {
|
||||
mustNot.push(part.replace("!", ""));
|
||||
} else {
|
||||
must.push(part);
|
||||
}
|
||||
});
|
||||
parts.forEach(part => {
|
||||
if (part.startsWith("!")) {
|
||||
mustNot.push(part.replace("!", ""));
|
||||
} else {
|
||||
must.push(part);
|
||||
}
|
||||
});
|
||||
|
||||
if (must.length > 1) {
|
||||
queryStruct.bool.must.push({
|
||||
bool: {
|
||||
should: must.map(elem => {
|
||||
const _q = {};
|
||||
_q[field] = elem;
|
||||
return {term: _q}
|
||||
})
|
||||
}
|
||||
});
|
||||
} else if (must.length === 1) {
|
||||
const mustQuery = {};
|
||||
mustQuery[field] = must[0];
|
||||
queryStruct.bool.must.push({term: mustQuery});
|
||||
}
|
||||
if (must.length > 1) {
|
||||
queryStruct.bool.must.push({
|
||||
bool: {
|
||||
should: must.map(elem => {
|
||||
const _q = {};
|
||||
_q[field] = elem;
|
||||
return {term: _q}
|
||||
})
|
||||
}
|
||||
});
|
||||
} else if (must.length === 1) {
|
||||
const mustQuery = {};
|
||||
mustQuery[field] = must[0];
|
||||
queryStruct.bool.must.push({term: mustQuery});
|
||||
}
|
||||
|
||||
if (mustNot.length > 1) {
|
||||
queryStruct.bool.must_not.push({
|
||||
bool: {
|
||||
should: mustNot.map(elem => {
|
||||
const _q = {};
|
||||
_q[field] = elem;
|
||||
return {term: _q}
|
||||
})
|
||||
}
|
||||
});
|
||||
} else if (mustNot.length === 1) {
|
||||
const mustNotQuery = {};
|
||||
mustNotQuery[field] = mustNot[0].replace("!", "");
|
||||
queryStruct.bool.must_not.push({term: mustNotQuery});
|
||||
}
|
||||
if (mustNot.length > 1) {
|
||||
queryStruct.bool.must_not.push({
|
||||
bool: {
|
||||
should: mustNot.map(elem => {
|
||||
const _q = {};
|
||||
_q[field] = elem;
|
||||
return {term: _q}
|
||||
})
|
||||
}
|
||||
});
|
||||
} else if (mustNot.length === 1) {
|
||||
const mustNotQuery = {};
|
||||
mustNotQuery[field] = mustNot[0].replace("!", "");
|
||||
queryStruct.bool.must_not.push({term: mustNotQuery});
|
||||
}
|
||||
}
|
||||
|
||||
function addRangeQuery(queryStruct, prop, pkey, query) {
|
||||
const _termQuery = {};
|
||||
const parts = query[prop].split("-");
|
||||
_termQuery[pkey] = {
|
||||
"gte": parts[0],
|
||||
"lte": parts[1]
|
||||
};
|
||||
queryStruct.bool.must.push({range: _termQuery});
|
||||
const _termQuery = {};
|
||||
const parts = query[prop].split("-");
|
||||
_termQuery[pkey] = {
|
||||
"gte": parts[0],
|
||||
"lte": parts[1]
|
||||
};
|
||||
queryStruct.bool.must.push({range: _termQuery});
|
||||
}
|
||||
|
||||
export function applyTimeFilter(query, queryStruct) {
|
||||
if (query['after'] || query['before']) {
|
||||
let _lte = "now";
|
||||
let _gte = "0";
|
||||
if (query['before']) {
|
||||
_lte = query['before'];
|
||||
if (!_lte.endsWith("Z")) {
|
||||
_lte += "Z";
|
||||
}
|
||||
}
|
||||
if (query['after']) {
|
||||
_gte = query['after'];
|
||||
if (!_gte.endsWith("Z")) {
|
||||
_gte += "Z";
|
||||
}
|
||||
}
|
||||
if (!queryStruct.bool['filter']) {
|
||||
queryStruct.bool['filter'] = [];
|
||||
}
|
||||
queryStruct.bool['filter'].push({
|
||||
range: {
|
||||
"@timestamp": {
|
||||
"gte": _gte,
|
||||
"lte": _lte
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if (query['after'] || query['before']) {
|
||||
let _lte = "now";
|
||||
let _gte = "0";
|
||||
if (query['before']) {
|
||||
try {
|
||||
_lte = new Date(query['before']).toISOString();
|
||||
} catch (e) {
|
||||
throw new Error(e.message + ' [before]');
|
||||
}
|
||||
}
|
||||
if (query['after']) {
|
||||
try {
|
||||
_gte = new Date(query['after']).toISOString();
|
||||
} catch (e) {
|
||||
throw new Error(e.message + ' [after]');
|
||||
}
|
||||
}
|
||||
if (!queryStruct.bool['filter']) {
|
||||
queryStruct.bool['filter'] = [];
|
||||
}
|
||||
queryStruct.bool['filter'].push({
|
||||
range: {
|
||||
"@timestamp": {
|
||||
"gte": _gte,
|
||||
"lte": _lte
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function applyGenericFilters(query, queryStruct) {
|
||||
for (const prop in query) {
|
||||
if (Object.prototype.hasOwnProperty.call(query, prop)) {
|
||||
const pair = prop.split(".");
|
||||
if (pair.length > 1 || primaryTerms.includes(pair[0])) {
|
||||
let pkey;
|
||||
if (pair.length > 1) {
|
||||
pkey = extendedActions.has(pair[0]) ? "@" + prop : prop;
|
||||
} else {
|
||||
pkey = prop;
|
||||
}
|
||||
if (query[prop].indexOf("-") !== -1) {
|
||||
addRangeQuery(queryStruct, prop, pkey, query);
|
||||
} else {
|
||||
const _termQuery = {};
|
||||
const parts = query[prop].split(",");
|
||||
if (parts.length > 1) {
|
||||
processMultiVars(queryStruct, parts, prop);
|
||||
} else if (parts.length === 1) {
|
||||
const andParts = parts[0].split(" ");
|
||||
if (andParts.length > 1) {
|
||||
andParts.forEach(value => {
|
||||
const _q = {};
|
||||
console.log(value);
|
||||
_q[pkey] = value;
|
||||
queryStruct.bool.must.push({term: _q});
|
||||
});
|
||||
} else {
|
||||
if (parts[0].startsWith("!")) {
|
||||
_termQuery[pkey] = parts[0].replace("!", "");
|
||||
queryStruct.bool.must_not.push({term: _termQuery});
|
||||
} else {
|
||||
_termQuery[pkey] = parts[0];
|
||||
queryStruct.bool.must.push({term: _termQuery});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
export function applyGenericFilters(query, queryStruct, allowedExtraParams: Set<string>) {
|
||||
for (const prop in query) {
|
||||
if (Object.prototype.hasOwnProperty.call(query, prop)) {
|
||||
const pair = prop.split(".");
|
||||
if (pair.length > 1 || primaryTerms.includes(pair[0])) {
|
||||
let pkey;
|
||||
if (pair.length > 1 && allowedExtraParams) {
|
||||
pkey = allowedExtraParams.has(pair[0]) ? "@" + prop : prop;
|
||||
} else {
|
||||
pkey = prop;
|
||||
}
|
||||
if (query[prop].indexOf("-") !== -1) {
|
||||
addRangeQuery(queryStruct, prop, pkey, query);
|
||||
} else {
|
||||
const _qObj = {};
|
||||
const parts = query[prop].split(",");
|
||||
if (parts.length > 1) {
|
||||
processMultiVars(queryStruct, parts, prop);
|
||||
} else if (parts.length === 1) {
|
||||
|
||||
// @transfer.memo special case
|
||||
if (pkey === '@transfer.memo') {
|
||||
_qObj[pkey] = {
|
||||
query: parts[0]
|
||||
};
|
||||
|
||||
if (query.match_fuzziness) {
|
||||
_qObj[pkey].fuzziness = query.match_fuzziness;
|
||||
}
|
||||
|
||||
if (query.match_operator) {
|
||||
_qObj[pkey].operator = query.match_operator;
|
||||
}
|
||||
|
||||
queryStruct.bool.must.push({
|
||||
match: _qObj
|
||||
});
|
||||
} else {
|
||||
const andParts = parts[0].split(" ");
|
||||
if (andParts.length > 1) {
|
||||
andParts.forEach(value => {
|
||||
const _q = {};
|
||||
_q[pkey] = value;
|
||||
queryStruct.bool.must.push({term: _q});
|
||||
});
|
||||
} else {
|
||||
if (parts[0].startsWith("!")) {
|
||||
_qObj[pkey] = parts[0].replace("!", "");
|
||||
queryStruct.bool.must_not.push({term: _qObj});
|
||||
} else {
|
||||
_qObj[pkey] = parts[0];
|
||||
queryStruct.bool.must.push({term: _qObj});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function makeShouldArray(query) {
|
||||
const should_array = [];
|
||||
for (const entry of terms) {
|
||||
const tObj = {term: {}};
|
||||
tObj.term[entry] = query.account;
|
||||
should_array.push(tObj);
|
||||
}
|
||||
return should_array;
|
||||
const should_array = [];
|
||||
for (const entry of terms) {
|
||||
const tObj = {term: {}};
|
||||
tObj.term[entry] = query.account;
|
||||
should_array.push(tObj);
|
||||
}
|
||||
return should_array;
|
||||
}
|
||||
|
||||
export function applyCodeActionFilters(query, queryStruct) {
|
||||
let filterObj = [];
|
||||
if (query.filter) {
|
||||
for (const filter of query.filter.split(',')) {
|
||||
if (filter !== '*:*') {
|
||||
const _arr = [];
|
||||
const parts = filter.split(':');
|
||||
if (parts.length === 2) {
|
||||
const [code, method] = parts;
|
||||
if (code && code !== "*") {
|
||||
_arr.push({'term': {'act.account': code}});
|
||||
}
|
||||
if (method && method !== "*") {
|
||||
_arr.push({'term': {'act.name': method}});
|
||||
}
|
||||
}
|
||||
if (_arr.length > 0) {
|
||||
filterObj.push({bool: {must: _arr}});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (filterObj.length > 0) {
|
||||
queryStruct.bool['should'] = filterObj;
|
||||
queryStruct.bool['minimum_should_match'] = 1;
|
||||
}
|
||||
}
|
||||
let filterObj = [];
|
||||
if (query.filter) {
|
||||
for (const filter of query.filter.split(',')) {
|
||||
if (filter !== '*:*') {
|
||||
const _arr = [];
|
||||
const parts = filter.split(':');
|
||||
if (parts.length === 2) {
|
||||
const [code, method] = parts;
|
||||
if (code && code !== "*") {
|
||||
_arr.push({'term': {'act.account': code}});
|
||||
}
|
||||
if (method && method !== "*") {
|
||||
_arr.push({'term': {'act.name': method}});
|
||||
}
|
||||
}
|
||||
if (_arr.length > 0) {
|
||||
filterObj.push({bool: {must: _arr}});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (filterObj.length > 0) {
|
||||
queryStruct.bool['should'] = filterObj;
|
||||
queryStruct.bool['minimum_should_match'] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getSkipLimit(query) {
|
||||
let skip, limit;
|
||||
skip = parseInt(query.skip, 10);
|
||||
if (skip < 0) {
|
||||
throw new Error('invalid skip parameter');
|
||||
}
|
||||
limit = parseInt(query.limit, 10);
|
||||
if (limit < 1) {
|
||||
throw new Error('invalid limit parameter');
|
||||
}
|
||||
return {skip, limit};
|
||||
export function getSkipLimit(query, max?: number) {
|
||||
let skip, limit;
|
||||
skip = parseInt(query.skip, 10);
|
||||
if (skip < 0) {
|
||||
throw new Error('invalid skip parameter');
|
||||
}
|
||||
limit = parseInt(query.limit, 10);
|
||||
if (limit < 1) {
|
||||
throw new Error('invalid limit parameter');
|
||||
} else if (limit > max) {
|
||||
throw new Error(`limit too big, maximum: ${max}`);
|
||||
}
|
||||
return {skip, limit};
|
||||
}
|
||||
|
||||
export function getSortDir(query) {
|
||||
let sort_direction = 'desc';
|
||||
if (query.sort) {
|
||||
if (query.sort === 'asc' || query.sort === '1') {
|
||||
sort_direction = 'asc';
|
||||
} else if (query.sort === 'desc' || query.sort === '-1') {
|
||||
sort_direction = 'desc'
|
||||
} else {
|
||||
throw new Error('invalid sort direction');
|
||||
}
|
||||
}
|
||||
return sort_direction;
|
||||
let sort_direction = 'desc';
|
||||
if (query.sort) {
|
||||
if (query.sort === 'asc' || query.sort === '1') {
|
||||
sort_direction = 'asc';
|
||||
} else if (query.sort === 'desc' || query.sort === '-1') {
|
||||
sort_direction = 'desc'
|
||||
} else {
|
||||
throw new Error('invalid sort direction');
|
||||
}
|
||||
}
|
||||
return sort_direction;
|
||||
}
|
||||
|
||||
export function applyAccountFilters(query, queryStruct) {
|
||||
if (query.account) {
|
||||
queryStruct.bool.must.push({"bool": {should: makeShouldArray(query)}});
|
||||
}
|
||||
if (query.account) {
|
||||
queryStruct.bool.must.push({"bool": {should: makeShouldArray(query)}});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,96 +1,118 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {getTrackTotalHits, mergeActionMeta, timedQuery} from "../../../helpers/functions";
|
||||
import {
|
||||
addSortedBy,
|
||||
applyAccountFilters,
|
||||
applyCodeActionFilters,
|
||||
applyGenericFilters,
|
||||
applyTimeFilter,
|
||||
getSkipLimit,
|
||||
getSortDir
|
||||
addSortedBy,
|
||||
applyAccountFilters,
|
||||
applyCodeActionFilters,
|
||||
applyGenericFilters,
|
||||
applyTimeFilter,
|
||||
getSkipLimit,
|
||||
getSortDir
|
||||
} from "./functions";
|
||||
|
||||
async function getActions(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
const query = request.query;
|
||||
const queryStruct = {
|
||||
"bool": {
|
||||
must: [],
|
||||
must_not: [],
|
||||
boost: 1.0
|
||||
}
|
||||
};
|
||||
const query: any = request.query;
|
||||
const maxActions = fastify.manager.config.api.limits.get_actions;
|
||||
const queryStruct = {
|
||||
"bool": {
|
||||
must: [],
|
||||
must_not: [],
|
||||
boost: 1.0
|
||||
}
|
||||
};
|
||||
|
||||
const {skip, limit} = getSkipLimit(query);
|
||||
const sort_direction = getSortDir(query);
|
||||
applyAccountFilters(query, queryStruct);
|
||||
applyGenericFilters(query, queryStruct);
|
||||
applyTimeFilter(query, queryStruct);
|
||||
applyCodeActionFilters(query, queryStruct);
|
||||
// allow precise counting of total hits
|
||||
const trackTotalHits = getTrackTotalHits(query);
|
||||
const {skip, limit} = getSkipLimit(query, maxActions);
|
||||
const sort_direction = getSortDir(query);
|
||||
applyAccountFilters(query, queryStruct);
|
||||
applyGenericFilters(query, queryStruct, fastify.allowedActionQueryParamSet);
|
||||
applyTimeFilter(query, queryStruct);
|
||||
applyCodeActionFilters(query, queryStruct);
|
||||
// allow precise counting of total hits
|
||||
const trackTotalHits = getTrackTotalHits(query);
|
||||
|
||||
// Prepare query body
|
||||
const query_body = {
|
||||
"track_total_hits": trackTotalHits,
|
||||
"query": queryStruct
|
||||
};
|
||||
// Prepare query body
|
||||
const query_body = {
|
||||
"track_total_hits": trackTotalHits,
|
||||
"query": queryStruct
|
||||
};
|
||||
|
||||
// Include sorting
|
||||
addSortedBy(query, query_body, sort_direction);
|
||||
// Include sorting
|
||||
addSortedBy(query, query_body, sort_direction);
|
||||
|
||||
// Perform search
|
||||
const maxActions = fastify.manager.config.api.limits.get_actions;
|
||||
const pResults = await Promise.all([
|
||||
fastify.eosjs.rpc.get_info(),
|
||||
fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-action-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxActions ? maxActions : limit) || 10,
|
||||
"body": query_body
|
||||
})
|
||||
]);
|
||||
// Perform search
|
||||
|
||||
const results = pResults[1]['body']['hits'];
|
||||
const response: any = {
|
||||
cached: false,
|
||||
lib: pResults[0].last_irreversible_block_num,
|
||||
total: results['total']
|
||||
};
|
||||
let indexPattern = fastify.manager.chain + '-action-*';
|
||||
if (query.hot_only) {
|
||||
indexPattern = fastify.manager.chain + '-action';
|
||||
}
|
||||
|
||||
if (query.simple) {
|
||||
response['simple_actions'] = [];
|
||||
} else {
|
||||
response['actions'] = [];
|
||||
}
|
||||
const esResults = await fastify.elastic.search({
|
||||
"index": indexPattern,
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxActions ? maxActions : limit) || 10,
|
||||
"body": query_body
|
||||
});
|
||||
|
||||
if (results['hits'].length > 0) {
|
||||
const actions = results['hits'];
|
||||
for (let action of actions) {
|
||||
action = action._source;
|
||||
mergeActionMeta(action);
|
||||
if (query.simple) {
|
||||
response.simple_actions.push({
|
||||
block: action['block_num'],
|
||||
irreversible: action['block_num'] < pResults[0].last_irreversible_block_num,
|
||||
timestamp: action['@timestamp'],
|
||||
transaction_id: action['trx_id'],
|
||||
actors: action['act']['authorization'].map(a => `${a.actor}@${a.permission}`).join(","),
|
||||
notified: action['notified'].join(','),
|
||||
contract: action['act']['account'],
|
||||
action: action['act']['name'],
|
||||
data: action['act']['data']
|
||||
});
|
||||
} else {
|
||||
response.actions.push(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
return response;
|
||||
const results = esResults['body']['hits'];
|
||||
const response: any = {
|
||||
cached: false,
|
||||
lib: 0,
|
||||
total: results['total']
|
||||
};
|
||||
|
||||
if (query.hot_only) {
|
||||
response.hot_only = true;
|
||||
}
|
||||
|
||||
if (query.checkLib) {
|
||||
response.lib = (await fastify.eosjs.rpc.get_info()).last_irreversible_block_num;
|
||||
}
|
||||
|
||||
if (query.simple) {
|
||||
response['simple_actions'] = [];
|
||||
} else {
|
||||
response['actions'] = [];
|
||||
}
|
||||
|
||||
if (results['hits'].length > 0) {
|
||||
const actions = results['hits'];
|
||||
for (let action of actions) {
|
||||
action = action._source;
|
||||
mergeActionMeta(action);
|
||||
|
||||
if (query.noBinary === true) {
|
||||
for (const key in action['act']['data']) {
|
||||
if (action['act']['data'].hasOwnProperty(key)) {
|
||||
if (typeof action['act']['data'][key] === 'string' && action['act']['data'][key].length > 256) {
|
||||
action['act']['data'][key] = action['act']['data'][key].slice(0, 32) + "...";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (query.simple) {
|
||||
response.simple_actions.push({
|
||||
block: action['block_num'],
|
||||
irreversible: response.lib !== 0 ? action['block_num'] < response.lib : undefined,
|
||||
timestamp: action['@timestamp'],
|
||||
transaction_id: action['trx_id'],
|
||||
actors: action['act']['authorization'].map(a => `${a.actor}@${a.permission}`).join(","),
|
||||
notified: action['notified'].join(','),
|
||||
contract: action['act']['account'],
|
||||
action: action['act']['name'],
|
||||
data: action['act']['data']
|
||||
});
|
||||
|
||||
} else {
|
||||
response.actions.push(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getActionsHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
reply.send(await timedQuery(getActions, fastify, request, route));
|
||||
}
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getActions, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,117 +1,132 @@
|
||||
import {FastifyInstance, RouteSchema} from "fastify";
|
||||
import {FastifyInstance, FastifySchema} from "fastify";
|
||||
import {getActionsHandler} from "./get_actions";
|
||||
import {addApiRoute, extendQueryStringSchema, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export const getActionResponseSchema = {
|
||||
"@timestamp": {type: "string"},
|
||||
"timestamp": {type: "string"},
|
||||
"block_num": {type: "number"},
|
||||
"trx_id": {type: "string"},
|
||||
"act": {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"account": {type: "string"},
|
||||
"name": {type: "string"}
|
||||
},
|
||||
additionalProperties: true
|
||||
},
|
||||
"notified": {
|
||||
type: "array", items: {type: "string"}
|
||||
},
|
||||
"cpu_usage_us": {type: "number"},
|
||||
"net_usage_words": {type: "number"},
|
||||
"account_ram_deltas": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
"account": {type: "string"},
|
||||
"delta": {type: "number"}
|
||||
},
|
||||
additionalProperties: true
|
||||
}
|
||||
},
|
||||
"global_sequence": {type: "number"},
|
||||
"receiver": {type: 'string'},
|
||||
"producer": {type: "string"},
|
||||
"parent": {type: "number"},
|
||||
"action_ordinal": {type: 'number'},
|
||||
"creator_action_ordinal": {type: 'number'},
|
||||
"signatures": {type: "array", items: {type: 'string'}}
|
||||
};
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema: RouteSchema = {
|
||||
description: 'get actions based on notified account. this endpoint also accepts generic filters based on indexed fields' +
|
||||
' (e.g. act.authorization.actor=eosio or act.name=delegatebw), if included they will be combined with a AND operator',
|
||||
summary: 'get root actions',
|
||||
tags: ['actions', 'history'],
|
||||
querystring: extendQueryStringSchema({
|
||||
"account": {
|
||||
description: 'notified account',
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
maxLength: 12
|
||||
},
|
||||
"track": {
|
||||
description: 'total results to track (count) [number or true]',
|
||||
type: 'string'
|
||||
},
|
||||
"filter": {
|
||||
description: 'code:name filter',
|
||||
type: 'string',
|
||||
minLength: 3
|
||||
},
|
||||
"sort": {
|
||||
description: 'sort direction',
|
||||
enum: ['desc', 'asc', '1', '-1'],
|
||||
type: 'string'
|
||||
},
|
||||
"after": {
|
||||
description: 'filter after specified date (ISO8601)',
|
||||
type: 'string'
|
||||
},
|
||||
"before": {
|
||||
description: 'filter before specified date (ISO8601)',
|
||||
type: 'string'
|
||||
},
|
||||
"simple": {
|
||||
description: 'simplified output mode',
|
||||
type: 'boolean'
|
||||
}
|
||||
}),
|
||||
response: extendResponseSchema({
|
||||
"simple_actions": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
"block": {type: "number"},
|
||||
"timestamp": {type: "string"},
|
||||
"irreversible": {type: "boolean"},
|
||||
"contract": {type: "string"},
|
||||
"action": {type: "string"},
|
||||
"actors": {type: "string"},
|
||||
"notified": {type: "string"},
|
||||
"transaction_id": {type: "string"},
|
||||
"data": {
|
||||
additionalProperties: true
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"actions": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"@timestamp": {type: "string"},
|
||||
"timestamp": {type: "string"},
|
||||
"block_num": {type: "number"},
|
||||
"trx_id": {type: "string"},
|
||||
"act": {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"account": {type: "string"},
|
||||
"name": {type: "string"}
|
||||
},
|
||||
additionalProperties: true
|
||||
},
|
||||
"notified": {
|
||||
type: "array", items: {type: "string"}
|
||||
},
|
||||
"cpu_usage_us": {type: "number"},
|
||||
"net_usage_words": {type: "number"},
|
||||
"account_ram_deltas": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
"account": {type: "string"},
|
||||
"delta": {type: "number"}
|
||||
},
|
||||
additionalProperties: true
|
||||
}
|
||||
},
|
||||
"global_sequence": {type: "number"},
|
||||
"receiver": {type: 'string'},
|
||||
"producer": {type: "string"},
|
||||
"parent": {type: "number"},
|
||||
"action_ordinal": {type: 'number'},
|
||||
"creator_action_ordinal": {type: 'number'}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
getActionsHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
const schema: FastifySchema = {
|
||||
description: 'get actions based on notified account. this endpoint also accepts generic filters based on indexed fields' +
|
||||
' (e.g. act.authorization.actor=eosio or act.name=delegatebw), if included they will be combined with a AND operator',
|
||||
summary: 'get root actions',
|
||||
tags: ['history'],
|
||||
querystring: extendQueryStringSchema({
|
||||
"account": {
|
||||
description: 'notified account',
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
maxLength: 12
|
||||
},
|
||||
"track": {
|
||||
description: 'total results to track (count) [number or true]',
|
||||
type: 'string'
|
||||
},
|
||||
"filter": {
|
||||
description: 'code:name filter',
|
||||
type: 'string',
|
||||
minLength: 3
|
||||
},
|
||||
"sort": {
|
||||
description: 'sort direction',
|
||||
enum: ['desc', 'asc', '1', '-1'],
|
||||
type: 'string'
|
||||
},
|
||||
"after": {
|
||||
description: 'filter after specified date (ISO8601)',
|
||||
type: 'string'
|
||||
},
|
||||
"before": {
|
||||
description: 'filter before specified date (ISO8601)',
|
||||
type: 'string'
|
||||
},
|
||||
"simple": {
|
||||
description: 'simplified output mode',
|
||||
type: 'boolean'
|
||||
},
|
||||
"hot_only": {
|
||||
description: 'search only the latest hot index',
|
||||
type: 'boolean'
|
||||
},
|
||||
"noBinary": {
|
||||
description: "exclude large binary data",
|
||||
type: 'boolean'
|
||||
},
|
||||
"checkLib": {
|
||||
description: "perform reversibility check",
|
||||
type: 'boolean'
|
||||
},
|
||||
}),
|
||||
response: extendResponseSchema({
|
||||
"simple_actions": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
"block": {type: "number"},
|
||||
"timestamp": {type: "string"},
|
||||
"irreversible": {type: "boolean"},
|
||||
"contract": {type: "string"},
|
||||
"action": {type: "string"},
|
||||
"actors": {type: "string"},
|
||||
"notified": {type: "string"},
|
||||
"transaction_id": {type: "string"},
|
||||
"data": {
|
||||
additionalProperties: true
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"actions": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: getActionResponseSchema
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
getActionsHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
import {getSkipLimit} from "../get_actions/functions";
|
||||
|
||||
async function getCreatedAccounts(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
|
||||
const query: any = request.query;
|
||||
const {skip, limit} = getSkipLimit(query);
|
||||
const maxActions = fastify.manager.config.api.limits.get_created_accounts;
|
||||
const results = await fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-action-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxActions ? maxActions : limit) || 100,
|
||||
"body": {
|
||||
"query": {
|
||||
"bool": {
|
||||
must: [
|
||||
{term: {"act.authorization.actor": query.account.toLowerCase()}},
|
||||
{term: {"act.name": "newaccount"}},
|
||||
{term: {"act.account": "eosio"}}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: {
|
||||
"global_sequence": "desc"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const response = {accounts: []};
|
||||
|
||||
if (results['body']['hits']['hits'].length > 0) {
|
||||
const actions = results['body']['hits']['hits'];
|
||||
for (let action of actions) {
|
||||
action = action._source;
|
||||
const _tmp = {};
|
||||
if (action['act']['data']['newact']) {
|
||||
_tmp['name'] = action['act']['data']['newact'];
|
||||
} else if (action['@newaccount']['newact']) {
|
||||
_tmp['name'] = action['@newaccount']['newact'];
|
||||
} else {
|
||||
console.log(action);
|
||||
}
|
||||
_tmp['trx_id'] = action['trx_id'];
|
||||
_tmp['timestamp'] = action['@timestamp'];
|
||||
response.accounts.push(_tmp);
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getCreatedAccountsHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getCreatedAccounts, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import {FastifyInstance, FastifySchema} from "fastify";
|
||||
import {getCreatedAccountsHandler} from "./get_created_accounts";
|
||||
import {addApiRoute, extendQueryStringSchema, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema: FastifySchema = {
|
||||
description: 'get all accounts created by one creator',
|
||||
summary: 'get created accounts',
|
||||
tags: ['accounts'],
|
||||
querystring: extendQueryStringSchema({
|
||||
"account": {
|
||||
description: 'creator account',
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
maxLength: 12
|
||||
}
|
||||
}, ["account"]),
|
||||
response: extendResponseSchema({
|
||||
"query_time": {
|
||||
type: "number"
|
||||
},
|
||||
"total": {
|
||||
type: "object",
|
||||
properties: {
|
||||
"value": {type: "number"},
|
||||
"relation": {type: "string"}
|
||||
}
|
||||
},
|
||||
"accounts": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
'name': {type: 'string'},
|
||||
'timestamp': {type: 'string'},
|
||||
'trx_id': {type: 'string'}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
addApiRoute(fastify, 'GET', getRouteName(__filename), getCreatedAccountsHandler, schema);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
|
||||
async function getCreator(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
const query: any = request.query;
|
||||
|
||||
const response = {
|
||||
account: query.account,
|
||||
creator: '',
|
||||
timestamp: '',
|
||||
block_num: 0,
|
||||
trx_id: '',
|
||||
};
|
||||
|
||||
if (query.account === fastify.manager.config.settings.eosio_alias) {
|
||||
try {
|
||||
const genesisBlock = await fastify.eosjs.rpc.get_block(1);
|
||||
if (genesisBlock) {
|
||||
response.timestamp = genesisBlock.timestamp;
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e.message);
|
||||
}
|
||||
response.creator = '__self__';
|
||||
response.block_num = 1;
|
||||
response.trx_id = "";
|
||||
return response;
|
||||
}
|
||||
|
||||
const results = await fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-action-*',
|
||||
"body": {
|
||||
size: 1,
|
||||
query: {
|
||||
bool: {
|
||||
must: [{term: {"@newaccount.newact": query.account}}]
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (results['body']['hits']['hits'].length === 1) {
|
||||
const result = results['body']['hits']['hits'][0]._source;
|
||||
response.trx_id = result.trx_id;
|
||||
response.block_num = result.block_num;
|
||||
response.creator = result.act.data.creator;
|
||||
response.timestamp = result['@timestamp'];
|
||||
return response;
|
||||
} else {
|
||||
let accountInfo;
|
||||
try {
|
||||
accountInfo = await fastify.eosjs.rpc.get_account(query.account);
|
||||
} catch (e) {
|
||||
throw new Error("account not found");
|
||||
}
|
||||
if (accountInfo) {
|
||||
try {
|
||||
response.timestamp = accountInfo.created;
|
||||
const blockHeader = await fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-block-*',
|
||||
"body": {
|
||||
size: 1,
|
||||
query: {
|
||||
bool: {
|
||||
must: [{term: {"@timestamp": response.timestamp}}]
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
const hits = blockHeader['body']['hits']['hits'];
|
||||
if (hits.length > 0 && hits[0]._source) {
|
||||
const blockId = blockHeader['body']['hits']['hits'][0]._source.block_id;
|
||||
const blockData = await fastify.eosjs.rpc.get_block(blockId);
|
||||
response.block_num = blockData.block_num;
|
||||
for (const transaction of blockData["transactions"]) {
|
||||
if (typeof transaction.trx !== 'string') {
|
||||
const actions = transaction.trx.transaction.actions;
|
||||
for (const act of actions) {
|
||||
if (act.name === 'newaccount') {
|
||||
if (act.data.name === query.account) {
|
||||
response.creator = act.data.creator;
|
||||
response.trx_id = transaction.id;
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return response;
|
||||
} catch (e) {
|
||||
console.log(e.message);
|
||||
throw new Error("account creation not found");
|
||||
}
|
||||
} else {
|
||||
throw new Error("account not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getCreatorHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getCreator, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import {FastifyInstance, FastifySchema} from "fastify";
|
||||
import {getCreatorHandler} from "./get_creator";
|
||||
import {addApiRoute, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema: FastifySchema = {
|
||||
description: 'get account creator',
|
||||
summary: 'get account creator',
|
||||
tags: ['accounts'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"account": {
|
||||
description: 'created account',
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
maxLength: 12
|
||||
}
|
||||
},
|
||||
required: ["account"]
|
||||
},
|
||||
response: extendResponseSchema({
|
||||
"account": {
|
||||
type: "string"
|
||||
},
|
||||
"creator": {
|
||||
type: "string"
|
||||
},
|
||||
"timestamp": {
|
||||
type: "string"
|
||||
},
|
||||
"block_num": {
|
||||
type: "integer"
|
||||
},
|
||||
"trx_id": {
|
||||
type: "string"
|
||||
},
|
||||
"indirect_creator": {
|
||||
type: "string"
|
||||
}
|
||||
})
|
||||
};
|
||||
addApiRoute(fastify, 'GET', getRouteName(__filename), getCreatorHandler, schema);
|
||||
next();
|
||||
}
|
||||
@@ -1,76 +1,91 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {mergeDeltaMeta, timedQuery} from "../../../helpers/functions";
|
||||
import {applyTimeFilter} from "../get_actions/functions";
|
||||
|
||||
async function getDeltas(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
let skip, limit;
|
||||
let sort_direction = 'desc';
|
||||
const mustArray = [];
|
||||
for (const param in request.query) {
|
||||
if (Object.prototype.hasOwnProperty.call(request.query, param)) {
|
||||
const value = request.query[param];
|
||||
switch (param) {
|
||||
case 'limit': {
|
||||
limit = parseInt(value, 10);
|
||||
if (limit < 1) {
|
||||
return 'invalid limit parameter';
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'skip': {
|
||||
skip = parseInt(value, 10);
|
||||
if (skip < 0) {
|
||||
return 'invalid skip parameter';
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'sort': {
|
||||
if (value === 'asc' || value === '1') {
|
||||
sort_direction = 'asc';
|
||||
} else if (value === 'desc' || value === '-1') {
|
||||
sort_direction = 'desc'
|
||||
} else {
|
||||
return 'invalid sort direction';
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
const values = request.query[param].split(",");
|
||||
const terms = {};
|
||||
terms[param] = values;
|
||||
const shouldArray = {terms: terms};
|
||||
const boolStruct = {bool: {should: [shouldArray]}};
|
||||
mustArray.push(boolStruct);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let skip, limit;
|
||||
let sort_direction = 'desc';
|
||||
const mustArray = [];
|
||||
const query: any = request.query;
|
||||
for (const param in query) {
|
||||
if (Object.prototype.hasOwnProperty.call(query, param)) {
|
||||
const value = query[param];
|
||||
switch (param) {
|
||||
case 'limit': {
|
||||
limit = parseInt(value, 10);
|
||||
if (limit < 1) {
|
||||
return 'invalid limit parameter';
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'skip': {
|
||||
skip = parseInt(value, 10);
|
||||
if (skip < 0) {
|
||||
return 'invalid skip parameter';
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'sort': {
|
||||
if (value === 'asc' || value === '1') {
|
||||
sort_direction = 'asc';
|
||||
} else if (value === 'desc' || value === '-1') {
|
||||
sort_direction = 'desc'
|
||||
} else {
|
||||
return 'invalid sort direction';
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'before': {
|
||||
break;
|
||||
}
|
||||
case 'after': {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
const values = query[param].split(",");
|
||||
if (values.length > 1) {
|
||||
const terms = {};
|
||||
terms[param] = values;
|
||||
mustArray.push({terms: terms});
|
||||
} else {
|
||||
const term = {};
|
||||
term[param] = values[0];
|
||||
mustArray.push({term: term});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const maxDeltas = fastify.manager.config.api.limits.get_deltas ?? 1000;
|
||||
const results = await fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-delta-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxDeltas ? maxDeltas : limit) || 10,
|
||||
"body": {
|
||||
query: {bool: {must: mustArray}},
|
||||
sort: {
|
||||
"block_num": sort_direction
|
||||
}
|
||||
}
|
||||
});
|
||||
const deltas = results['body']['hits']['hits'].map((d) => {
|
||||
return mergeDeltaMeta(d._source);
|
||||
});
|
||||
return {
|
||||
query_time: null,
|
||||
total: results['body']['hits']['total'],
|
||||
deltas
|
||||
};
|
||||
const maxDeltas = fastify.manager.config.api.limits.get_deltas ?? 1000;
|
||||
const queryStruct = {bool: {must: mustArray}};
|
||||
|
||||
applyTimeFilter(query, queryStruct);
|
||||
|
||||
const results = await fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-delta-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxDeltas ? maxDeltas : limit) || 10,
|
||||
"body": {
|
||||
query: queryStruct,
|
||||
sort: {
|
||||
"block_num": sort_direction
|
||||
}
|
||||
}
|
||||
});
|
||||
const deltas = results['body']['hits']['hits'].map((d) => {
|
||||
return mergeDeltaMeta(d._source);
|
||||
});
|
||||
return {
|
||||
query_time: null,
|
||||
total: results['body']['hits']['total'],
|
||||
deltas
|
||||
};
|
||||
}
|
||||
|
||||
export function getDeltasHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
reply.send(await timedQuery(getDeltas, fastify, request, route));
|
||||
}
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getDeltas, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import {FastifyInstance, RouteSchema} from "fastify";
|
||||
import {FastifyInstance, FastifySchema} from "fastify";
|
||||
import {getDeltasHandler} from "./get_deltas";
|
||||
import {addApiRoute, extendQueryStringSchema, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema: RouteSchema = {
|
||||
const schema: FastifySchema = {
|
||||
description: 'get state deltas',
|
||||
summary: 'get state deltas',
|
||||
tags: ['history'],
|
||||
@@ -23,7 +23,19 @@ export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
"payer": {
|
||||
description: 'payer account',
|
||||
type: 'string'
|
||||
}
|
||||
},
|
||||
"after": {
|
||||
description: 'filter after specified date (ISO8601)',
|
||||
type: 'string'
|
||||
},
|
||||
"before": {
|
||||
description: 'filter before specified date (ISO8601)',
|
||||
type: 'string'
|
||||
},
|
||||
"present": {
|
||||
description: 'delta present flag',
|
||||
type: 'number'
|
||||
},
|
||||
}),
|
||||
response: extendResponseSchema({
|
||||
"deltas": {
|
||||
@@ -32,13 +44,14 @@ export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"timestamp": {type: 'string'},
|
||||
"present": {type: 'number'},
|
||||
"code": {type: 'string'},
|
||||
"scope": {type: 'string'},
|
||||
"table": {type: 'string'},
|
||||
"primary_key": {type: 'string'},
|
||||
"payer": {type: 'string'},
|
||||
"present": {type: 'boolean'},
|
||||
"block_num": {type: 'number'},
|
||||
"block_id": {type: 'string'},
|
||||
"data": {
|
||||
type: 'object',
|
||||
additionalProperties: true
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
import {createHash} from "crypto";
|
||||
import {base58ToBinary, binaryToBase58} from "eosjs/dist/eosjs-numeric";
|
||||
import {Search} from "@elastic/elasticsearch/api/requestParams";
|
||||
|
||||
function convertToLegacyKey(block_signing_key: string) {
|
||||
if (block_signing_key.startsWith("PUB_K1_")) {
|
||||
const buf = base58ToBinary(37, block_signing_key.substr(7));
|
||||
const data = buf.slice(0, buf.length - 4);
|
||||
const merged = Buffer.concat([
|
||||
data,
|
||||
createHash('ripemd160')
|
||||
.update(data)
|
||||
.digest()
|
||||
.slice(0, 4)
|
||||
]);
|
||||
return "EOS" + binaryToBase58(merged);
|
||||
} else {
|
||||
return block_signing_key;
|
||||
}
|
||||
}
|
||||
|
||||
async function getSchedule(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
const query: any = request.query;
|
||||
|
||||
const response: any = {
|
||||
producers: []
|
||||
};
|
||||
const searchParams: Search<any> = {
|
||||
track_total_hits: true,
|
||||
index: fastify.manager.chain + "-block-*",
|
||||
size: 1,
|
||||
body: {
|
||||
query: {
|
||||
bool: {
|
||||
must: []
|
||||
}
|
||||
},
|
||||
sort: {block_num: "desc"}
|
||||
}
|
||||
};
|
||||
if (query.version) {
|
||||
searchParams.body.query.bool.must.push({"term": {"new_producers.version": {"value": query.version}}});
|
||||
} else {
|
||||
searchParams.body.query.bool.must.push({
|
||||
"exists": {
|
||||
"field": "new_producers.version"
|
||||
}
|
||||
});
|
||||
}
|
||||
const apiResponse = await fastify.elastic.search(searchParams);
|
||||
const results = apiResponse.body.hits.hits;
|
||||
if (results) {
|
||||
response.timestamp = results[0]._source["@timestamp"];
|
||||
response.block_num = results[0]._source.block_num;
|
||||
response.version = results[0]._source.new_producers.version;
|
||||
response.producers = results[0]._source.new_producers.producers.map(prod => {
|
||||
return {
|
||||
...prod,
|
||||
legacy_key: convertToLegacyKey(prod.block_signing_key)
|
||||
}
|
||||
});
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getScheduleHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getSchedule, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
import {getScheduleHandler} from "./get_schedule";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get producer schedule by version',
|
||||
summary: 'get producer schedule by version',
|
||||
tags: ['history'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"producer": {
|
||||
description: 'search by producer',
|
||||
type: 'string'
|
||||
},
|
||||
"key": {
|
||||
description: 'search by key',
|
||||
type: 'string'
|
||||
},
|
||||
"after": {
|
||||
description: 'filter after specified date (ISO8601)',
|
||||
type: 'string'
|
||||
},
|
||||
"before": {
|
||||
description: 'filter before specified date (ISO8601)',
|
||||
type: 'string'
|
||||
},
|
||||
"version": {
|
||||
description: 'schedule version',
|
||||
type: 'integer',
|
||||
minimum: 1
|
||||
}
|
||||
}
|
||||
},
|
||||
response: extendResponseSchema({
|
||||
"timestamp": {type: "string"},
|
||||
"block_num": {type: "number"},
|
||||
"version": {type: "number"},
|
||||
"producers": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
"producer_name": {type: "string"},
|
||||
"block_signing_key": {type: "string"},
|
||||
"legacy_key": {type: "string"}
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
getScheduleHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {mergeDeltaMeta, timedQuery} from "../../../helpers/functions";
|
||||
|
||||
async function getTableState(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
const query: any = request.query;
|
||||
const response = {
|
||||
...query,
|
||||
results: [],
|
||||
next_key: null
|
||||
};
|
||||
const mustArray: any[] = [
|
||||
{"term": {"code": query.code}},
|
||||
{"term": {"table": query.table}}
|
||||
];
|
||||
if (query.block_num) {
|
||||
mustArray.push({"range": {"block_num": {"lte": query.block_num}}});
|
||||
}
|
||||
let after_key = "";
|
||||
if (query.after_key) {
|
||||
after_key = query.after_key;
|
||||
}
|
||||
const results = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-delta-*',
|
||||
body: {
|
||||
"query": {
|
||||
"bool": {
|
||||
"must": mustArray
|
||||
}
|
||||
},
|
||||
"size": 0,
|
||||
"sort": [
|
||||
{"block_num": "desc"},
|
||||
{"primary_key": "desc"}
|
||||
],
|
||||
"aggs": {
|
||||
"scope_buckets": {
|
||||
"composite": {
|
||||
"size": 25,
|
||||
"after": {
|
||||
"scope_pk": after_key
|
||||
},
|
||||
"sources": [
|
||||
{
|
||||
"scope_pk": {
|
||||
"terms": {
|
||||
"script": {
|
||||
"source": "return doc['scope'].value + '-' + doc['primary_key'].value",
|
||||
"lang": "painless"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"aggs": {
|
||||
"last_doc": {
|
||||
"top_hits": {
|
||||
"size": 1,
|
||||
"sort": [{"block_num": "desc"}],
|
||||
"_source": {
|
||||
"excludes": ["code"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
const scope_buckets = results.body.aggregations.scope_buckets;
|
||||
if (scope_buckets && scope_buckets.buckets.length > 0) {
|
||||
if (scope_buckets.after_key) {
|
||||
response.next_key = scope_buckets.after_key.scope_pk;
|
||||
}
|
||||
response.results = scope_buckets.buckets.map(bucket => {
|
||||
const row = mergeDeltaMeta(bucket.last_doc.hits.hits[0]._source);
|
||||
delete row.table;
|
||||
return row;
|
||||
});
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getTableStateHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getTableState, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
import {getTableStateHandler} from "./get_table_state";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get table state at a specific block height',
|
||||
summary: 'get table state at a specific block height',
|
||||
tags: ['history'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
required: ["code","table"],
|
||||
properties: {
|
||||
"code": {
|
||||
description: 'search by contract',
|
||||
type: 'string'
|
||||
},
|
||||
"table": {
|
||||
description: 'search by key',
|
||||
type: 'string'
|
||||
},
|
||||
"block_num": {
|
||||
description: 'target block',
|
||||
type: 'integer',
|
||||
minimum: 1
|
||||
},
|
||||
"after_key": {
|
||||
description: 'last key for pagination',
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
},
|
||||
response: extendResponseSchema({
|
||||
"code": {type: 'string'},
|
||||
"table": {type: 'string'},
|
||||
"block_num": {type: 'number'},
|
||||
"after_key": {type: 'string'},
|
||||
"next_key": {type: 'string'},
|
||||
"results": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"scope": {type: 'string'},
|
||||
"primary_key": {type: 'string'},
|
||||
"payer": {type: 'string'},
|
||||
"timestamp": {type: 'string'},
|
||||
"block_num": {type: 'number'},
|
||||
"block_id": {type: 'string'},
|
||||
"present": {type: 'number'},
|
||||
"data": {
|
||||
type: 'object',
|
||||
additionalProperties: true
|
||||
}
|
||||
},
|
||||
additionalProperties: true
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
getTableStateHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -1,45 +1,128 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {mergeActionMeta, timedQuery} from "../../../helpers/functions";
|
||||
import {GetInfoResult} from "eosjs/dist/eosjs-rpc-interfaces";
|
||||
|
||||
async function getTransaction(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
const pResults = await Promise.all([
|
||||
fastify.eosjs.rpc.get_info(),
|
||||
fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-action-*',
|
||||
"body": {
|
||||
"query": {
|
||||
"bool": {
|
||||
must: [
|
||||
{term: {"trx_id": request.query.id.toLowerCase()}}
|
||||
]
|
||||
}
|
||||
},
|
||||
"sort": {
|
||||
"global_sequence": "asc"
|
||||
}
|
||||
}
|
||||
})
|
||||
]);
|
||||
const results = pResults[1];
|
||||
const response = {
|
||||
"trx_id": request.query.id,
|
||||
"lib": pResults[0].last_irreversible_block_num,
|
||||
"actions": []
|
||||
};
|
||||
const hits = results['body']['hits']['hits'];
|
||||
if (hits.length > 0) {
|
||||
for (let action of hits) {
|
||||
action = action._source;
|
||||
mergeActionMeta(action);
|
||||
response.actions.push(action);
|
||||
}
|
||||
}
|
||||
return response;
|
||||
const redis = fastify.redis;
|
||||
const query: any = request.query;
|
||||
const trxId = query.id.toLowerCase();
|
||||
const conf = fastify.manager.config;
|
||||
const cachedData = await redis.hgetall('trx_' + trxId);
|
||||
const response = {
|
||||
query_time_ms: undefined,
|
||||
executed: false,
|
||||
cached: undefined,
|
||||
cache_expires_in: undefined,
|
||||
trx_id: query.id,
|
||||
lib: undefined,
|
||||
cached_lib: false,
|
||||
actions: undefined,
|
||||
generated: undefined,
|
||||
error: undefined
|
||||
};
|
||||
let hits;
|
||||
|
||||
// build get_info request with caching
|
||||
const $getInfo = new Promise<GetInfoResult>(resolve => {
|
||||
const key = `${fastify.manager.chain}_get_info`;
|
||||
fastify.redis.get(key).then(value => {
|
||||
if (value) {
|
||||
response.cached_lib = true;
|
||||
resolve(JSON.parse(value));
|
||||
} else {
|
||||
fastify.eosjs.rpc.get_info().then(value1 => {
|
||||
fastify.redis.set(key, JSON.stringify(value1), 'EX', 6);
|
||||
response.cached_lib = false;
|
||||
resolve(value1);
|
||||
}).catch((reason) => {
|
||||
console.log(reason);
|
||||
response.error = 'failed to get last_irreversible_block_num'
|
||||
resolve(null);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// reconstruct hits from cached data
|
||||
if (cachedData && Object.keys(cachedData).length > 0) {
|
||||
const gsArr = [];
|
||||
for (let cachedDataKey in cachedData) {
|
||||
gsArr.push(cachedData[cachedDataKey]);
|
||||
}
|
||||
gsArr.sort((a, b) => {
|
||||
return a.global_sequence - b.global_sequence;
|
||||
});
|
||||
hits = gsArr.map(value => {
|
||||
return {
|
||||
_source: JSON.parse(value)
|
||||
};
|
||||
});
|
||||
const promiseResults = await Promise.all([
|
||||
redis.ttl('trx_' + trxId),
|
||||
$getInfo
|
||||
]);
|
||||
response.cache_expires_in = promiseResults[0];
|
||||
response.lib = promiseResults[1].last_irreversible_block_num;
|
||||
}
|
||||
|
||||
// search on ES if cache is not present
|
||||
if (!hits) {
|
||||
const _size = conf.api.limits.get_trx_actions || 100;
|
||||
const blockHint = parseInt(query.block_hint, 10);
|
||||
let indexPattern = '';
|
||||
if (blockHint) {
|
||||
const idxPart = Math.ceil(blockHint / conf.settings.index_partition_size).toString().padStart(6, '0');
|
||||
indexPattern = fastify.manager.chain + `-action-${conf.settings.index_version}-${idxPart}`;
|
||||
} else {
|
||||
indexPattern = fastify.manager.chain + '-action-*';
|
||||
}
|
||||
let pResults;
|
||||
try {
|
||||
|
||||
// build search request
|
||||
const $search = fastify.elastic.search({
|
||||
index: indexPattern,
|
||||
size: _size,
|
||||
body: {
|
||||
query: {bool: {must: [{term: {trx_id: trxId}}]}},
|
||||
sort: {global_sequence: "asc"}
|
||||
}
|
||||
});
|
||||
|
||||
// execute in parallel
|
||||
pResults = await Promise.all([$getInfo, $search]);
|
||||
} catch (e) {
|
||||
console.log(e.message);
|
||||
if (e.meta.statusCode === 404) {
|
||||
response.error = 'no data near block_hint'
|
||||
return response;
|
||||
}
|
||||
}
|
||||
hits = pResults[1]['body']['hits']['hits'];
|
||||
response.lib = pResults[0].last_irreversible_block_num;
|
||||
}
|
||||
|
||||
if (hits.length > 0) {
|
||||
let highestBlockNum = 0;
|
||||
for (let action of hits) {
|
||||
if (action._source.block_num > highestBlockNum) {
|
||||
highestBlockNum = action._source.block_num;
|
||||
}
|
||||
}
|
||||
response.actions = [];
|
||||
for (let action of hits) {
|
||||
if (action._source.block_num === highestBlockNum) {
|
||||
mergeActionMeta(action._source);
|
||||
response.actions.push(action._source);
|
||||
}
|
||||
}
|
||||
response.executed = true;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getTransactionHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
reply.send(await timedQuery(getTransaction, fastify, request, route));
|
||||
}
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getTransaction, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,13 +6,17 @@ export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get all actions belonging to the same transaction',
|
||||
summary: 'get transaction by id',
|
||||
tags: ['transactions', 'history'],
|
||||
tags: ['history'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"id": {
|
||||
id: {
|
||||
description: 'transaction id',
|
||||
type: 'string'
|
||||
},
|
||||
block_hint: {
|
||||
description: 'block hint to speed up tx recovery',
|
||||
type: 'integer'
|
||||
}
|
||||
},
|
||||
required: ["id"]
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {mergeActionMeta, timedQuery} from "../../../helpers/functions";
|
||||
|
||||
async function getTransaction(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
const _size = fastify.manager.config.api.limits.get_trx_actions || 100;
|
||||
const query: any = request.query;
|
||||
|
||||
let indexPattern = fastify.manager.chain + '-action-*';
|
||||
if (query.hot_only) {
|
||||
indexPattern = fastify.manager.chain + '-action';
|
||||
}
|
||||
|
||||
const pResults = await Promise.all([
|
||||
fastify.eosjs.rpc.get_info(),
|
||||
fastify.elastic.search({
|
||||
index: indexPattern,
|
||||
size: _size,
|
||||
body: {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{term: {trx_id: query.id.toLowerCase()}}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: {
|
||||
global_sequence: "asc"
|
||||
}
|
||||
}
|
||||
}),
|
||||
fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-gentrx-*',
|
||||
size: _size,
|
||||
body: {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{term: {trx_id: query.id.toLowerCase()}}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
]);
|
||||
|
||||
const results = pResults[1];
|
||||
const genTrxRes = pResults[2];
|
||||
|
||||
const response = {
|
||||
"executed": false,
|
||||
"hot_only": false,
|
||||
"trx_id": query.id,
|
||||
"lib": pResults[0].last_irreversible_block_num,
|
||||
"actions": [],
|
||||
"generated": undefined
|
||||
};
|
||||
|
||||
if (query.hot_only) {
|
||||
response.hot_only = true;
|
||||
}
|
||||
|
||||
const hits = results['body']['hits']['hits'];
|
||||
|
||||
if (hits.length > 0) {
|
||||
|
||||
// const producers = {};
|
||||
// for (let hit of hits) {
|
||||
// if (hit._source.producer) {
|
||||
// if (producers[hit._source.producer]) {
|
||||
// producers[hit._source.producer]++;
|
||||
// } else {
|
||||
// producers[hit._source.producer] = 1;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// let useBlockNumber;
|
||||
// if (Object.keys(producers).length > 1) {
|
||||
// // multiple producers of the same tx id, forked actions are present, attempt to clean-up
|
||||
// let trueProd = '';
|
||||
// let highestActCount = 0;
|
||||
// for (const prod in producers) {
|
||||
// if (producers.hasOwnProperty(prod)) {
|
||||
// if(producers[prod] === highestActCount) {
|
||||
// useBlockNumber = true;
|
||||
// } else if (producers[prod] > highestActCount) {
|
||||
// highestActCount = producers[prod];
|
||||
// trueProd = prod;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
let highestBlockNum = 0;
|
||||
for (let action of hits) {
|
||||
if (action._source.block_num > highestBlockNum) {
|
||||
highestBlockNum = action._source.block_num;
|
||||
}
|
||||
}
|
||||
|
||||
for (let action of hits) {
|
||||
if (action._source.block_num === highestBlockNum) {
|
||||
action = action._source;
|
||||
mergeActionMeta(action);
|
||||
response.actions.push(action);
|
||||
}
|
||||
}
|
||||
|
||||
response.executed = true;
|
||||
}
|
||||
|
||||
const hits2 = genTrxRes['body']['hits']['hits'];
|
||||
|
||||
if (hits2 && hits2.length > 0) {
|
||||
if (hits2[0]._source['@timestamp']) {
|
||||
hits2[0]._source['timestamp'] = hits2[0]._source['@timestamp'];
|
||||
delete hits2[0]._source['@timestamp'];
|
||||
}
|
||||
response.generated = hits2[0]._source;
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getTransactionHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getTransaction, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {getTransactionHandler} from "./get_transaction_legacy";
|
||||
import {addApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get all actions belonging to the same transaction',
|
||||
summary: 'get transaction by id',
|
||||
tags: ['history'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"id": {
|
||||
description: 'transaction id',
|
||||
type: 'string'
|
||||
}
|
||||
},
|
||||
required: ["id"]
|
||||
}
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
getTransactionHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -1,42 +1,53 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import got from "got";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
|
||||
async function getAccount(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
|
||||
const query: any = request.query;
|
||||
|
||||
const response = {
|
||||
query_time: null,
|
||||
cached: false,
|
||||
account: null,
|
||||
actions: null,
|
||||
total_actions: 0,
|
||||
tokens: null,
|
||||
links: null
|
||||
};
|
||||
|
||||
const account = request.query.account;
|
||||
const account = query.account;
|
||||
const reqQueue = [];
|
||||
reqQueue.push(fastify.eosjs.rpc.get_account(account));
|
||||
|
||||
try {
|
||||
response.account = await fastify.eosjs.rpc.get_account(account);
|
||||
} catch (e) {
|
||||
throw new Error("Account not found!");
|
||||
}
|
||||
|
||||
const localApi = `http://${fastify.manager.config.api.server_addr}:${fastify.manager.config.api.server_port}/v2`;
|
||||
const getTokensApi = localApi + '/state/get_tokens';
|
||||
const getActionsApi = localApi + '/history/get_actions';
|
||||
const getLinksApi = localApi + '/state/get_links';
|
||||
|
||||
// fetch recent actions
|
||||
reqQueue.push(got.get(`${getActionsApi}?account=${account}&limit=10`));
|
||||
reqQueue.push(got.get(`${getActionsApi}?account=${account}&limit=20&noBinary=true&track=true`).json());
|
||||
|
||||
// fetch account tokens
|
||||
reqQueue.push(got.get(`${getTokensApi}?account=${account}`));
|
||||
reqQueue.push(got.get(`${getTokensApi}?account=${account}`).json());
|
||||
|
||||
// fetch account permission links
|
||||
reqQueue.push(got.get(`${getLinksApi}?account=${account}`).json());
|
||||
|
||||
const results = await Promise.all(reqQueue);
|
||||
response.account = results[0];
|
||||
response.actions = JSON.parse(results[1].body).actions;
|
||||
response.tokens = JSON.parse(results[2].body).tokens;
|
||||
response.actions = results[0].actions;
|
||||
response.total_actions = results[0].total.value;
|
||||
response.tokens = results[1].tokens;
|
||||
response.links = results[2].links;
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getAccountHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getAccount, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,58 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {getAccountHandler} from "./get_account";
|
||||
import {addApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
import {addApiRoute, extendQueryStringSchema, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
import {getActionResponseSchema} from "../../v2-history/get_actions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get account data',
|
||||
summary: 'get account summary',
|
||||
tags: ['accounts', 'state'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"account": {
|
||||
description: 'account name',
|
||||
type: 'string'
|
||||
}
|
||||
tags: ['accounts'],
|
||||
querystring: extendQueryStringSchema({
|
||||
"account": {
|
||||
description: 'account name',
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
maxLength: 12
|
||||
}
|
||||
}
|
||||
}, ["account"]),
|
||||
response: extendResponseSchema({
|
||||
"account": {
|
||||
additionalProperties: true
|
||||
},
|
||||
"links": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"timestamp": {type: "string"},
|
||||
"permission": {type: "string"},
|
||||
"code": {type: "string"},
|
||||
"action": {type: "string"},
|
||||
}
|
||||
}
|
||||
},
|
||||
"tokens": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"symbol": {type: "string"},
|
||||
"precision": {type: "integer"},
|
||||
"amount": {type: "number"},
|
||||
"contract": {type: "string"},
|
||||
}
|
||||
}
|
||||
},
|
||||
"total_actions": {type: "number"},
|
||||
"actions": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: getActionResponseSchema
|
||||
}
|
||||
},
|
||||
})
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
|
||||
@@ -1,85 +1,139 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
import {Numeric} from "eosjs/dist";
|
||||
import {getSkipLimit} from "../../v2-history/get_actions/functions";
|
||||
|
||||
function invalidKey() {
|
||||
const err: any = new Error();
|
||||
err.statusCode = 400;
|
||||
err.message = 'invalid public key';
|
||||
throw err;
|
||||
const err: any = new Error();
|
||||
err.statusCode = 400;
|
||||
err.message = 'invalid public key';
|
||||
throw err;
|
||||
}
|
||||
|
||||
async function getKeyAccounts(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
let publicKey;
|
||||
const public_Key = request.req.method === 'POST' ? request.body.public_key : request.query.public_key;
|
||||
let publicKey;
|
||||
if (typeof request.body === 'string' && request.method === 'POST') {
|
||||
request.body = JSON.parse(request.body);
|
||||
}
|
||||
|
||||
if (public_Key.startsWith("PUB_")) {
|
||||
publicKey = public_Key;
|
||||
} else if (public_Key.startsWith("EOS")) {
|
||||
try {
|
||||
publicKey = Numeric.convertLegacyPublicKey(public_Key);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
invalidKey();
|
||||
}
|
||||
} else {
|
||||
invalidKey();
|
||||
}
|
||||
const body: any = request.body;
|
||||
const query: any = request.query;
|
||||
|
||||
const _body = {
|
||||
query: {
|
||||
bool: {
|
||||
should: [
|
||||
{term: {"@updateauth.auth.keys.key.keyword": publicKey}},
|
||||
{term: {"@newaccount.active.keys.key.keyword": publicKey}},
|
||||
{term: {"@newaccount.owner.keys.key.keyword": publicKey}}
|
||||
],
|
||||
minimum_should_match: 1
|
||||
}
|
||||
},
|
||||
sort: [{"global_sequence": {"order": "desc"}}]
|
||||
};
|
||||
const {skip, limit} = getSkipLimit(request.query);
|
||||
const maxDocs = fastify.manager.config.api.limits.get_key_accounts ?? 1000;
|
||||
|
||||
const results = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-action-*',
|
||||
body: _body
|
||||
});
|
||||
const public_Key = request.method === 'POST' ? body.public_key : query.public_key;
|
||||
|
||||
const response = {
|
||||
account_names: []
|
||||
};
|
||||
if (results['body']['hits']['hits'].length > 0) {
|
||||
response.account_names = results['body']['hits']['hits'].map((v) => {
|
||||
if (v._source.act.name === 'newaccount') {
|
||||
if (v._source['@newaccount'].newact) {
|
||||
return v._source['@newaccount'].newact;
|
||||
} else if (v._source.act.data.newact) {
|
||||
return v._source.act.data.newact;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else if (v._source.act.name === 'updateauth') {
|
||||
return v._source.act.data.account;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
if (response.account_names.length > 0) {
|
||||
response.account_names = [...(new Set(response.account_names))];
|
||||
return response;
|
||||
} else {
|
||||
const err: any = new Error();
|
||||
err.statusCode = 404;
|
||||
err.message = 'no accounts associated with ' + public_Key;
|
||||
throw err;
|
||||
}
|
||||
if (public_Key.startsWith("PUB_")) {
|
||||
publicKey = public_Key;
|
||||
} else if (public_Key.startsWith("EOS")) {
|
||||
try {
|
||||
publicKey = Numeric.convertLegacyPublicKey(public_Key);
|
||||
} catch (e) {
|
||||
console.log(e.message);
|
||||
invalidKey();
|
||||
}
|
||||
} else {
|
||||
invalidKey();
|
||||
}
|
||||
|
||||
const response = {
|
||||
account_names: []
|
||||
} as any;
|
||||
|
||||
if (request.method === 'GET' && query.details) {
|
||||
response.permissions = [];
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
const permTableResults = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-perm-*',
|
||||
size: (limit > maxDocs ? maxDocs : limit) || 100,
|
||||
from: skip || 0,
|
||||
body: {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{
|
||||
term: {
|
||||
"auth.keys.key.keyword": publicKey
|
||||
}
|
||||
}
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (permTableResults.body.hits.hits.length > 0) {
|
||||
for (const perm of permTableResults.body.hits.hits) {
|
||||
response.account_names.push(perm._source.owner);
|
||||
if (request.method === 'GET' && query.details) {
|
||||
response.permissions.push(perm._source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
if (response.account_names.length > 0) {
|
||||
response.account_names = [...(new Set(response.account_names))];
|
||||
return response;
|
||||
}
|
||||
|
||||
// Fallback to action search
|
||||
const _body = {
|
||||
query: {
|
||||
bool: {
|
||||
should: [
|
||||
{term: {"@updateauth.auth.keys.key.keyword": publicKey}},
|
||||
{term: {"@newaccount.active.keys.key.keyword": publicKey}},
|
||||
{term: {"@newaccount.owner.keys.key.keyword": publicKey}}
|
||||
],
|
||||
minimum_should_match: 1
|
||||
}
|
||||
},
|
||||
sort: [{"global_sequence": {"order": "desc"}}]
|
||||
};
|
||||
|
||||
const results = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-action-*',
|
||||
size: (limit > maxDocs ? maxDocs : limit) || 100,
|
||||
from: skip || 0,
|
||||
body: _body
|
||||
});
|
||||
|
||||
if (results['body']['hits']['hits'].length > 0) {
|
||||
response.account_names = results['body']['hits']['hits'].map((v) => {
|
||||
if (v._source.act.name === 'newaccount') {
|
||||
if (v._source['@newaccount'].newact) {
|
||||
return v._source['@newaccount'].newact;
|
||||
} else if (v._source.act.data.newact) {
|
||||
return v._source.act.data.newact;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else if (v._source.act.name === 'updateauth') {
|
||||
return v._source.act.data.account;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (response.account_names.length > 0) {
|
||||
response.account_names = [...(new Set(response.account_names))];
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getKeyAccountsHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
reply.send(await timedQuery(getKeyAccounts, fastify, request, route));
|
||||
}
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getKeyAccounts, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {getKeyAccountsHandler} from "./get_key_accounts";
|
||||
import {addApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
import {addApiRoute, extendQueryStringSchema, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
|
||||
@@ -8,25 +8,38 @@ export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const getSchema = {
|
||||
description: 'get accounts by public key',
|
||||
summary: 'get accounts by public key',
|
||||
tags: ['accounts', 'state'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"public_key": {
|
||||
description: 'public key',
|
||||
type: 'string'
|
||||
},
|
||||
tags: ['accounts'],
|
||||
querystring: extendQueryStringSchema({
|
||||
"public_key": {
|
||||
description: 'public key',
|
||||
type: 'string'
|
||||
},
|
||||
required: ["public_key"]
|
||||
},
|
||||
"details": {
|
||||
description: 'include permission details',
|
||||
type: 'boolean'
|
||||
},
|
||||
}, ["public_key"]),
|
||||
response: {
|
||||
200: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"account_names": {
|
||||
type: "array",
|
||||
items: {type: "string"}
|
||||
},
|
||||
"permissions": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string"
|
||||
type: "object",
|
||||
properties: {
|
||||
owner: {type: 'string'},
|
||||
block_num: {type: 'integer'},
|
||||
parent: {type: 'string'},
|
||||
last_updated: {type: 'string'},
|
||||
auth: {},
|
||||
name: {type: 'string'},
|
||||
present: {type: 'number'}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,67 +1,71 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {getTrackTotalHits, timedQuery} from "../../../helpers/functions";
|
||||
import {ApiResponse} from "@elastic/elasticsearch";
|
||||
import {getSkipLimit} from "../../v2-history/get_actions/functions";
|
||||
|
||||
async function getLinks(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
const query = request.query;
|
||||
const {account, code, action, permissions} = query;
|
||||
const {skip, limit} = getSkipLimit(query);
|
||||
const query: any = request.query;
|
||||
const {account, code, action, permissions} = query;
|
||||
const {skip, limit} = getSkipLimit(query);
|
||||
|
||||
const queryStruct = {
|
||||
"bool": {
|
||||
must: []
|
||||
}
|
||||
};
|
||||
const queryStruct = {
|
||||
"bool": {
|
||||
must: [],
|
||||
must_not: []
|
||||
}
|
||||
};
|
||||
|
||||
if (account) {
|
||||
queryStruct.bool.must.push({'term': {'account': account}});
|
||||
}
|
||||
if (account) {
|
||||
queryStruct.bool.must.push({'term': {'account': account}});
|
||||
}
|
||||
|
||||
if (code) {
|
||||
queryStruct.bool.must.push({'term': {'code': code}});
|
||||
}
|
||||
if (code) {
|
||||
queryStruct.bool.must.push({'term': {'code': code}});
|
||||
}
|
||||
|
||||
if (action) {
|
||||
queryStruct.bool.must.push({'term': {'action': action}});
|
||||
}
|
||||
if (action) {
|
||||
queryStruct.bool.must.push({'term': {'action': action}});
|
||||
}
|
||||
|
||||
if (permissions) {
|
||||
queryStruct.bool.must.push({'term': {'permissions': permissions}});
|
||||
}
|
||||
if (permissions) {
|
||||
queryStruct.bool.must.push({'term': {'permissions': permissions}});
|
||||
}
|
||||
|
||||
// Prepare query body
|
||||
const query_body = {
|
||||
track_total_hits: getTrackTotalHits(query),
|
||||
query: queryStruct,
|
||||
sort: {block_num: 'desc'}
|
||||
};
|
||||
// only present deltas
|
||||
queryStruct.bool.must_not.push({'term': {'present': 0}});
|
||||
|
||||
const maxLinks = fastify.manager.config.api.limits.get_links;
|
||||
const results: ApiResponse = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-link-*',
|
||||
from: skip || 0,
|
||||
size: (limit > maxLinks ? maxLinks : limit) || 10,
|
||||
body: query_body
|
||||
});
|
||||
const hits = results.body.hits.hits;
|
||||
const response: any = {
|
||||
cached: false,
|
||||
total: results.body.hits.total,
|
||||
links: []
|
||||
};
|
||||
for (const hit of hits) {
|
||||
const link = hit._source;
|
||||
link.timestamp = link['@timestamp'];
|
||||
delete link['@timestamp'];
|
||||
response.links.push(link);
|
||||
}
|
||||
return response;
|
||||
// Prepare query body
|
||||
const query_body = {
|
||||
track_total_hits: getTrackTotalHits(query),
|
||||
query: queryStruct,
|
||||
sort: {block_num: 'desc'}
|
||||
};
|
||||
|
||||
const maxLinks = fastify.manager.config.api.limits.get_links;
|
||||
const results: ApiResponse = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-link-*',
|
||||
from: skip || 0,
|
||||
size: (limit > maxLinks ? maxLinks : limit) || 50,
|
||||
body: query_body
|
||||
});
|
||||
const hits = results.body.hits.hits;
|
||||
const response: any = {
|
||||
cached: false,
|
||||
total: results.body.hits.total,
|
||||
links: []
|
||||
};
|
||||
|
||||
for (const hit of hits) {
|
||||
const link = hit._source;
|
||||
link.timestamp = link['@timestamp'];
|
||||
delete link['@timestamp'];
|
||||
response.links.push(link);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getLinksHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
reply.send(await timedQuery(getLinks, fastify, request, route));
|
||||
}
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getLinks, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get permission links',
|
||||
summary: 'get permission links',
|
||||
tags: ['accounts', 'state'],
|
||||
tags: ['accounts'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {getTrackTotalHits, timedQuery} from "../../../helpers/functions";
|
||||
|
||||
async function getProposals(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
const query: any = request.query;
|
||||
|
||||
// Pagination
|
||||
let skip, limit;
|
||||
skip = parseInt(request.query.skip, 10);
|
||||
skip = parseInt(query.skip, 10);
|
||||
if (skip < 0) {
|
||||
return 'invalid skip parameter';
|
||||
}
|
||||
limit = parseInt(request.query.limit, 10);
|
||||
limit = parseInt(query.limit, 10);
|
||||
if (limit < 1) {
|
||||
return 'invalid limit parameter';
|
||||
}
|
||||
@@ -21,41 +22,44 @@ async function getProposals(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
}
|
||||
};
|
||||
|
||||
// Filter by account
|
||||
if (request.query.account) {
|
||||
queryStruct.bool.must.push({
|
||||
"bool": {
|
||||
"should": [
|
||||
{"term": {"requested_approvals.actor": request.query.account}},
|
||||
{"term": {"provided_approvals.actor": request.query.account}}
|
||||
]
|
||||
}
|
||||
});
|
||||
// Filter by accounts
|
||||
if (query.account) {
|
||||
const accounts = query.account.split(',');
|
||||
for(const acc of accounts) {
|
||||
queryStruct.bool.must.push({
|
||||
"bool": {
|
||||
"should": [
|
||||
{"term": {"requested_approvals.actor": acc}},
|
||||
{"term": {"provided_approvals.actor": acc}}
|
||||
]
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by proposer account
|
||||
if (request.query.proposer) {
|
||||
queryStruct.bool.must.push({"term": {"proposer": request.query.proposer}});
|
||||
if (query.proposer) {
|
||||
queryStruct.bool.must.push({"term": {"proposer": query.proposer}});
|
||||
}
|
||||
|
||||
// Filter by proposal name
|
||||
if (request.query.proposal) {
|
||||
queryStruct.bool.must.push({"term": {"proposal_name": request.query.proposal}});
|
||||
if (query.proposal) {
|
||||
queryStruct.bool.must.push({"term": {"proposal_name": query.proposal}});
|
||||
}
|
||||
|
||||
// Filter by execution status
|
||||
if (typeof request.query.executed !== 'undefined') {
|
||||
queryStruct.bool.must.push({"term": {"executed": request.query.executed}});
|
||||
if (typeof query.executed !== 'undefined') {
|
||||
queryStruct.bool.must.push({"term": {"executed": query.executed}});
|
||||
}
|
||||
|
||||
// Filter by requested actors
|
||||
if (request.query.requested) {
|
||||
queryStruct.bool.must.push({"term": {"requested_approvals.actor": request.query.requested}});
|
||||
if (query.requested) {
|
||||
queryStruct.bool.must.push({"term": {"requested_approvals.actor": query.requested}});
|
||||
}
|
||||
|
||||
// Filter by provided actors
|
||||
if (request.query.provided) {
|
||||
queryStruct.bool.must.push({"term": {"provided_approvals.actor": request.query.provided}});
|
||||
if (query.provided) {
|
||||
queryStruct.bool.must.push({"term": {"provided_approvals.actor": query.provided}});
|
||||
}
|
||||
|
||||
// If no filter switch to full match
|
||||
@@ -94,7 +98,7 @@ async function getProposals(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
}
|
||||
|
||||
export function getProposalsHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getProposals, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get proposals',
|
||||
summary: 'get proposals',
|
||||
tags: ['state'],
|
||||
tags: ['system'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
|
||||
@@ -1,68 +1,90 @@
|
||||
import {ServerResponse} from "http";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {getSkipLimit} from "../../v2-history/get_actions/functions";
|
||||
|
||||
|
||||
async function getTokens(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
const query: any = request.query;
|
||||
|
||||
const response = {
|
||||
query_time: null,
|
||||
cached: false,
|
||||
'account': request.query.account,
|
||||
'tokens': []
|
||||
account: query.account,
|
||||
tokens: []
|
||||
};
|
||||
|
||||
const results = await fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-action-*',
|
||||
const {skip, limit} = getSkipLimit(request.query);
|
||||
const maxDocs = fastify.manager.config.api.limits.get_tokens ?? 100;
|
||||
|
||||
const stateResult = await fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-table-accounts-*',
|
||||
"size": (limit > maxDocs ? maxDocs : limit) || 50,
|
||||
"from": skip || 0,
|
||||
"body": {
|
||||
size: 0,
|
||||
query: {
|
||||
bool: {
|
||||
filter: [
|
||||
{term: {"notified": request.query.account}},
|
||||
{terms: {"act.name": ["transfer", "issue"]}}
|
||||
]
|
||||
}
|
||||
},
|
||||
aggs: {
|
||||
tokens: {
|
||||
terms: {
|
||||
field: "act.account",
|
||||
size: 1000
|
||||
}
|
||||
filter: [{term: {scope: query.account}}]
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (const bucket of results['body']['aggregations']['tokens']['buckets']) {
|
||||
let token_data;
|
||||
try {
|
||||
token_data = await fastify.eosjs.rpc.get_currency_balance(bucket['key'], request.query.account);
|
||||
} catch (e) {
|
||||
console.log(`get_currency_balance error - contract:${bucket['key']} - account:${request.query.account}`);
|
||||
const testSet = new Set();
|
||||
for (const hit of stateResult.body.hits.hits) {
|
||||
const data = hit._source;
|
||||
if (typeof data.present !== "undefined" && data.present === 0) {
|
||||
continue;
|
||||
}
|
||||
for (const entry of token_data) {
|
||||
let precision = 0;
|
||||
const [amount, symbol] = entry.split(" ");
|
||||
const amount_arr = amount.split(".");
|
||||
if (amount_arr.length === 2) {
|
||||
precision = amount_arr[1].length;
|
||||
}
|
||||
response.tokens.push({
|
||||
symbol: symbol,
|
||||
precision: precision,
|
||||
amount: parseFloat(amount),
|
||||
contract: bucket['key']
|
||||
});
|
||||
let precision;
|
||||
let token_data;
|
||||
let errorMsg;
|
||||
|
||||
const key = `${data.code}_${data.symbol}`;
|
||||
|
||||
if (testSet.has(key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
testSet.add(key);
|
||||
if (!fastify.tokenCache) {
|
||||
fastify.tokenCache = new Map<string, any>();
|
||||
}
|
||||
if (fastify.tokenCache.has(key)) {
|
||||
precision = fastify.tokenCache.get(key).precision;
|
||||
} else {
|
||||
try {
|
||||
token_data = await fastify.eosjs.rpc.get_currency_balance(data.code, query.account, data.symbol);
|
||||
if (token_data.length > 0) {
|
||||
const [amount, symbol] = token_data[0].split(" ");
|
||||
const amount_arr = amount.split(".");
|
||||
if (amount_arr.length === 2) {
|
||||
precision = amount_arr[1].length;
|
||||
fastify.tokenCache.set(key, {precision});
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
errorMsg = e.message;
|
||||
}
|
||||
}
|
||||
|
||||
const resp: Record<string, any> = {
|
||||
symbol: data.symbol,
|
||||
precision: precision,
|
||||
amount: parseFloat(data.amount),
|
||||
contract: data.code,
|
||||
};
|
||||
|
||||
if (errorMsg) {
|
||||
resp.error = errorMsg;
|
||||
}
|
||||
|
||||
response.tokens.push(resp);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getTokensHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getTokens, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
import {addApiRoute, extendQueryStringSchema, getRouteName} from "../../../helpers/functions";
|
||||
import {getTokensHandler} from "./get_tokens";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get account data',
|
||||
summary: 'get account summary',
|
||||
tags: ['accounts', 'state'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"account": {
|
||||
description: 'account name',
|
||||
type: 'string'
|
||||
}
|
||||
description: 'get tokens from an account',
|
||||
summary: 'get all tokens',
|
||||
tags: ['accounts'],
|
||||
querystring: extendQueryStringSchema({
|
||||
"account": {
|
||||
description: 'account name',
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
maxLength: 12
|
||||
}
|
||||
}
|
||||
}, ["account"])
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
|
||||
@@ -1,72 +1,63 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
import {getSkipLimit} from "../../v2-history/get_actions/functions";
|
||||
|
||||
async function getVoters(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
|
||||
let skip, limit;
|
||||
const query: any = request.query;
|
||||
const {skip, limit} = getSkipLimit(request.query);
|
||||
|
||||
skip = parseInt(request.query.skip, 10);
|
||||
if (skip < 0) {
|
||||
return 'invalid skip parameter';
|
||||
}
|
||||
limit = parseInt(request.query.limit, 10);
|
||||
if (limit < 1) {
|
||||
return 'invalid limit parameter';
|
||||
}
|
||||
const response = {
|
||||
voter_count: 0,
|
||||
'voters': []
|
||||
};
|
||||
let queryStruct: any = {
|
||||
"bool": {
|
||||
"must": []
|
||||
}
|
||||
};
|
||||
|
||||
const response = {
|
||||
query_time: null,
|
||||
voter_count: 0,
|
||||
'voters': []
|
||||
};
|
||||
let queryStruct: any = {
|
||||
"bool": {
|
||||
"must": []
|
||||
}
|
||||
};
|
||||
if (query.producer) {
|
||||
for (const bp of query.producer.split(",")) {
|
||||
queryStruct.bool.must.push({"term": {"producers": bp}});
|
||||
}
|
||||
}
|
||||
|
||||
if (request.query.producer) {
|
||||
for (const bp of request.query.producer.split(",")) {
|
||||
queryStruct.bool.must.push({"term": {"producers": bp}});
|
||||
}
|
||||
}
|
||||
if (query.proxy === 'true') {
|
||||
queryStruct.bool.must.push({"term": {"is_proxy": true}});
|
||||
}
|
||||
|
||||
if (request.query.proxy === 'true') {
|
||||
queryStruct.bool.must.push({"term": {"is_proxy": true}});
|
||||
}
|
||||
if (queryStruct.bool.must.length === 0) {
|
||||
queryStruct = {
|
||||
"match_all": {}
|
||||
};
|
||||
}
|
||||
|
||||
if (queryStruct.bool.must.length === 0) {
|
||||
queryStruct = {
|
||||
"match_all": {}
|
||||
};
|
||||
}
|
||||
|
||||
const maxDocs = fastify.manager.config.api.limits.get_voters ?? 100;
|
||||
const results = await fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-table-voters-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxDocs ? maxDocs : limit) || 10,
|
||||
"body": {
|
||||
"query": queryStruct,
|
||||
"sort": [{"last_vote_weight": "desc"}]
|
||||
}
|
||||
});
|
||||
const hits = results['body']['hits']['hits'];
|
||||
for (const hit of hits) {
|
||||
const voter = hit._source;
|
||||
response.voters.push({
|
||||
account: voter.voter,
|
||||
weight: voter.last_vote_weight,
|
||||
last_vote: voter.block_num
|
||||
});
|
||||
}
|
||||
response.voter_count = results['body']['hits']['total']['value'];
|
||||
return response;
|
||||
const maxDocs = fastify.manager.config.api.limits.get_voters ?? 100;
|
||||
const results = await fastify.elastic.search({
|
||||
"index": fastify.manager.chain + '-table-voters-*',
|
||||
"from": skip || 0,
|
||||
"size": (limit > maxDocs ? maxDocs : limit) || 10,
|
||||
"body": {
|
||||
"query": queryStruct,
|
||||
"sort": [{"last_vote_weight": "desc"}]
|
||||
}
|
||||
});
|
||||
const hits = results['body']['hits']['hits'];
|
||||
for (const hit of hits) {
|
||||
const voter = hit._source;
|
||||
response.voters.push({
|
||||
account: voter.voter,
|
||||
weight: voter.last_vote_weight,
|
||||
last_vote: voter.block_num
|
||||
});
|
||||
}
|
||||
response.voter_count = results['body']['hits']['total']['value'];
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getVotersHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
reply.send(await timedQuery(getVoters, fastify, request, route));
|
||||
}
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getVoters, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,36 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {getVotersHandler} from "./get_voters";
|
||||
import {addApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
import {addApiRoute, extendQueryStringSchema, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get voters',
|
||||
summary: 'get voters',
|
||||
tags: ['accounts', 'state'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"producer": {
|
||||
description: 'filter by voted producer (comma separated)',
|
||||
type: 'string'
|
||||
},
|
||||
"skip": {
|
||||
description: 'skip [n] actions (pagination)',
|
||||
type: 'integer',
|
||||
minimum: 0
|
||||
},
|
||||
"limit": {
|
||||
description: 'limit of [n] actions per page',
|
||||
type: 'integer',
|
||||
minimum: 1
|
||||
tags: ['system'],
|
||||
querystring: extendQueryStringSchema({
|
||||
"producer": {
|
||||
description: 'filter by voted producer (comma separated)',
|
||||
type: 'string',
|
||||
minLength: 1,
|
||||
maxLength: 12
|
||||
}
|
||||
}),
|
||||
response: extendResponseSchema({
|
||||
"voters": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
"account": {type: "string"},
|
||||
"weight": {type: "number"},
|
||||
"last_vote": {type: "number"},
|
||||
"data": {
|
||||
additionalProperties: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
|
||||
async function getLastSeq(fastify: FastifyInstance, date: string) {
|
||||
const req = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-action-*',
|
||||
body: {
|
||||
"size": 1,
|
||||
"query": {
|
||||
"bool": {
|
||||
"must": [
|
||||
{"range": {"@timestamp": {"lt": date}}}
|
||||
]
|
||||
}
|
||||
},
|
||||
"_source": {
|
||||
"includes": ["global_sequence"]
|
||||
},
|
||||
"sort": [{"global_sequence": "desc"}]
|
||||
}
|
||||
});
|
||||
return req.body.hits.hits[0]._source.global_sequence;
|
||||
}
|
||||
|
||||
async function getTxCount(fastify: FastifyInstance, dateFrom: string, dateTo: string) {
|
||||
const req = await fastify.elastic.count({
|
||||
index: fastify.manager.chain + '-action-*',
|
||||
body: {
|
||||
"query": {
|
||||
"bool": {
|
||||
"must": [
|
||||
{"term": {"creator_action_ordinal": 0}},
|
||||
{"range": {"@timestamp": {"gt": dateFrom, "lt": dateTo}}}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return req.body.count;
|
||||
}
|
||||
|
||||
async function getUniqueActors(fastify: FastifyInstance, dateFrom: string, dateTo: string) {
|
||||
const req = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + '-action-*',
|
||||
size: 0,
|
||||
body: {
|
||||
"aggs": {
|
||||
"unique_actors": {
|
||||
"cardinality": {
|
||||
"field": "act.authorization.actor"
|
||||
}
|
||||
}
|
||||
},
|
||||
"query": {
|
||||
"bool": {
|
||||
"filter": [{"range": {"@timestamp": {"gt": dateFrom, "lt": dateTo}}}]
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return req.body.aggregations.unique_actors.value;
|
||||
}
|
||||
|
||||
async function getActionUsage(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
const query: any = request.query;
|
||||
let now = new Date();
|
||||
let expiration = 86400;
|
||||
if (query.end_date) {
|
||||
now = new Date(query.end_date);
|
||||
}
|
||||
now.setMinutes(0, 0, 0);
|
||||
let period;
|
||||
if (query.period === '1h') {
|
||||
period = 3600000;
|
||||
} else if (query.period === '24h' || query.period === '1d') {
|
||||
period = 86400000;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!query.end_date) {
|
||||
expiration = Math.floor((period - (Date.now() - now.getTime())) / 1000);
|
||||
}
|
||||
|
||||
const response = {} as any;
|
||||
const periodStart = new Date(now.getTime() - period);
|
||||
const pStart = periodStart.toISOString();
|
||||
const pEnd = now.toISOString();
|
||||
|
||||
const cacheKey = `${fastify.manager.chain}_act_stats_${pStart}_${pEnd}_${query.unique_actors}`;
|
||||
const cachedValue = await fastify.redis.get(cacheKey);
|
||||
if (cachedValue) {
|
||||
const cachedResponse = JSON.parse(cachedValue);
|
||||
cachedResponse.cached = true;
|
||||
cachedResponse.cache_exp = expiration;
|
||||
return cachedResponse;
|
||||
}
|
||||
|
||||
const firstReq = await getLastSeq(fastify, pStart);
|
||||
const lastReq = await getLastSeq(fastify, pEnd);
|
||||
response.action_count = lastReq - firstReq;
|
||||
response.tx_count = await getTxCount(fastify, pStart, pEnd);
|
||||
|
||||
if (query.unique_actors) {
|
||||
response.unique_actors = await getUniqueActors(fastify, pStart, pEnd);
|
||||
}
|
||||
|
||||
response.period = query.period;
|
||||
response.from = pStart;
|
||||
response.to = pEnd;
|
||||
|
||||
await fastify.redis.set(cacheKey, JSON.stringify(response), 'EX', expiration);
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getActionUsageHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getActionUsage, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
import {getActionUsageHandler} from "./get_action_usage";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get action and transaction stats for a given period',
|
||||
summary: 'get action and transaction stats for a given period',
|
||||
tags: ['stats'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
required: ["period"],
|
||||
properties: {
|
||||
"period": {
|
||||
description: 'analysis period',
|
||||
type: 'string'
|
||||
},
|
||||
"end_date": {
|
||||
description: 'final date',
|
||||
type: 'string'
|
||||
},
|
||||
"unique_actors": {
|
||||
description: 'compute unique actors',
|
||||
type: 'boolean'
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
getActionUsageHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
import {Search} from "@elastic/elasticsearch/api/requestParams";
|
||||
import {applyTimeFilter} from "../../v2-history/get_actions/functions";
|
||||
|
||||
async function getMissedBlocks(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
const query: any = request.query;
|
||||
const response = {
|
||||
stats: {
|
||||
by_producer: {}
|
||||
},
|
||||
events: []
|
||||
};
|
||||
const searchParams: Search<any> = {
|
||||
track_total_hits: true,
|
||||
index: fastify.manager.chain + "-logs-*",
|
||||
size: 100,
|
||||
body: {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{term: {"type": "missed_blocks"}}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: {
|
||||
"@timestamp": "desc"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let minBlocks = 0;
|
||||
if (query.min_blocks) {
|
||||
minBlocks = parseInt(query.min_blocks);
|
||||
searchParams.body.query.bool.must.push({range: {"missed_blocks.size": {gte: minBlocks}}})
|
||||
}
|
||||
|
||||
if (query.producer) {
|
||||
searchParams.body.query.bool.must.push({term: {"missed_blocks.producer": query.producer}});
|
||||
}
|
||||
|
||||
applyTimeFilter(query, searchParams.body.query);
|
||||
|
||||
const apiResponse = await fastify.elastic.search(searchParams);
|
||||
apiResponse.body.hits.hits.forEach(v => {
|
||||
const ev = v._source;
|
||||
response.events.push({
|
||||
"@timestamp": ev["@timestamp"],
|
||||
...ev.missed_blocks
|
||||
});
|
||||
if (!response.stats.by_producer[ev.missed_blocks.producer]) {
|
||||
response.stats.by_producer[ev.missed_blocks.producer] = ev.missed_blocks.size;
|
||||
} else {
|
||||
response.stats.by_producer[ev.missed_blocks.producer] += ev.missed_blocks.size;
|
||||
}
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
export function getMissedBlocksHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getMissedBlocks, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, extendResponseSchema, getRouteName} from "../../../helpers/functions";
|
||||
import {getMissedBlocksHandler} from "./get_missed_blocks";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get missed blocks',
|
||||
summary: 'get missed blocks',
|
||||
tags: ['stats'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
"producer": {
|
||||
description: 'filter by producer',
|
||||
type: 'string'
|
||||
},
|
||||
"after": {
|
||||
description: 'filter after specified date (ISO8601)',
|
||||
type: 'string'
|
||||
},
|
||||
"before": {
|
||||
description: 'filter before specified date (ISO8601)',
|
||||
type: 'string'
|
||||
},
|
||||
"min_blocks": {
|
||||
description: 'min. blocks threshold',
|
||||
type: 'integer',
|
||||
minimum: 1
|
||||
}
|
||||
}
|
||||
},
|
||||
response: extendResponseSchema({
|
||||
"stats": {
|
||||
type: "object",
|
||||
properties: {
|
||||
"by_producer": {}
|
||||
}
|
||||
},
|
||||
"events": {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
"@timestamp": {type: "string"},
|
||||
"last_block": {type: "number"},
|
||||
"schedule_version": {type: "number"},
|
||||
"size": {type: "number"},
|
||||
"producer": {type: "string"},
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
getMissedBlocksHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
|
||||
const percentiles = [1, 5, 25, 50, 75, 95, 99];
|
||||
|
||||
async function getResourceUsage(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
const query: any = request.query;
|
||||
const searchBody: any = {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{term: {"act.account": query.code}},
|
||||
{term: {"act.name": query.action}},
|
||||
{term: {"action_ordinal": {"value": 1}}},
|
||||
{
|
||||
range: {
|
||||
"@timestamp": {
|
||||
gte: "now-2d/d",
|
||||
lt: "now/d"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
track_total_hits: true,
|
||||
size: 0,
|
||||
aggs: {
|
||||
cpu_extended_stats: {extended_stats: {field: "cpu_usage_us"}},
|
||||
net_extended_stats: {extended_stats: {field: "net_usage_words"}},
|
||||
cpu_percentiles: {
|
||||
percentiles: {
|
||||
field: "cpu_usage_us",
|
||||
percents: percentiles
|
||||
}
|
||||
},
|
||||
net_percentiles: {
|
||||
percentiles: {
|
||||
field: "net_usage_words",
|
||||
percents: percentiles
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (request.query['@transfer.to']) {
|
||||
searchBody.query.bool.must.push({term: {"@transfer.to": request.query['@transfer.to']}});
|
||||
}
|
||||
|
||||
const results = await fastify.elastic.search({
|
||||
index: fastify.manager.chain + "-action-*",
|
||||
body: searchBody
|
||||
});
|
||||
if (results.body) {
|
||||
const data = results.body.aggregations;
|
||||
return {
|
||||
cpu: {
|
||||
stats: data.cpu_extended_stats,
|
||||
percentiles: data.cpu_percentiles.values
|
||||
},
|
||||
net: {
|
||||
stats: data.net_extended_stats,
|
||||
percentiles: data.net_percentiles.values
|
||||
}
|
||||
};
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export function getResourceUsageHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getResourceUsage, fastify, request, route));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import {FastifyInstance} from "fastify";
|
||||
import {addApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
import {getResourceUsageHandler} from "./get_resource_usage";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema = {
|
||||
description: 'get resource usage stats for a specific action',
|
||||
summary: 'get resource usage stats for a specific action',
|
||||
tags: ['stats'],
|
||||
querystring: {
|
||||
type: 'object',
|
||||
required: ["code", "action"],
|
||||
properties: {
|
||||
"code": {
|
||||
description: 'contract',
|
||||
type: 'string'
|
||||
},
|
||||
"action": {
|
||||
description: 'action name',
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
getResourceUsageHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
|
||||
import {ServerResponse} from "http";
|
||||
import {connect} from "amqplib";
|
||||
import {timedQuery} from "../../../helpers/functions";
|
||||
import {getLastIndexedBlock} from "../../../../helpers/common_functions";
|
||||
import {getFirstIndexedBlock, getLastIndexedBlockWithTotalBlocks} from "../../../../helpers/common_functions";
|
||||
|
||||
async function checkRabbit(fastify: FastifyInstance) {
|
||||
|
||||
async function checkRabbit(fastify: FastifyInstance): Promise<ServiceResponse<any>> {
|
||||
try {
|
||||
const connection = await connect(fastify.manager.ampqUrl);
|
||||
await connection.close();
|
||||
@@ -15,12 +15,20 @@ async function checkRabbit(fastify: FastifyInstance) {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkNodeos(fastify: FastifyInstance) {
|
||||
interface NodeosService {
|
||||
head_block_num: number;
|
||||
head_block_time: string;
|
||||
time_offset: number;
|
||||
last_irreversible_block: number;
|
||||
chain_id: string;
|
||||
}
|
||||
|
||||
async function checkNodeos(fastify: FastifyInstance): Promise<ServiceResponse<NodeosService>> {
|
||||
const rpc = fastify.manager.nodeosJsonRPC;
|
||||
try {
|
||||
const results = await rpc.get_info();
|
||||
if (results) {
|
||||
const diff = (new Date().getTime()) - (new Date(results.head_block_time).getTime());
|
||||
const diff = (new Date().getTime()) - (new Date(results.head_block_time + '+00:00').getTime());
|
||||
return createHealth('NodeosRPC', 'OK', {
|
||||
head_block_num: results.head_block_num,
|
||||
head_block_time: results.head_block_time,
|
||||
@@ -36,13 +44,47 @@ async function checkNodeos(fastify: FastifyInstance) {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkElastic(fastify: FastifyInstance) {
|
||||
interface ESService {
|
||||
first_indexed_block: number;
|
||||
last_indexed_block: number;
|
||||
total_indexed_blocks: number;
|
||||
active_shards: string;
|
||||
missing_blocks: number;
|
||||
missing_pct: string;
|
||||
head_offset: number;
|
||||
}
|
||||
|
||||
interface ServiceResponse<T> {
|
||||
service: string;
|
||||
time: number;
|
||||
status: any
|
||||
service_data?: T;
|
||||
}
|
||||
|
||||
async function checkElastic(fastify: FastifyInstance): Promise<ServiceResponse<ESService>> {
|
||||
try {
|
||||
let esStatus = await fastify.elastic.cat.health({format: 'json', v: true});
|
||||
let lastIndexedBlock = await getLastIndexedBlock(fastify.elastic, fastify.manager.chain);
|
||||
const data = {
|
||||
let firstIndexedBlock: number;
|
||||
const fib = await fastify.redis.get(`${fastify.manager.chain}::fib`);
|
||||
if (fib) {
|
||||
firstIndexedBlock = parseInt(fib);
|
||||
} else {
|
||||
firstIndexedBlock = await getFirstIndexedBlock(fastify.elastic, fastify.manager.chain);
|
||||
await fastify.redis.set(`${fastify.manager.chain}::fib`, firstIndexedBlock);
|
||||
}
|
||||
let indexedBlocks = await getLastIndexedBlockWithTotalBlocks(fastify.elastic, fastify.manager.chain);
|
||||
const lastIndexedBlock = indexedBlocks[0];
|
||||
const totalIndexed = indexedBlocks[1] - 1;
|
||||
const missingCounter = (lastIndexedBlock - firstIndexedBlock) - totalIndexed;
|
||||
const missingPct = (missingCounter * 100 / indexedBlocks[1]).toFixed(2) + "%";
|
||||
const data: ESService = {
|
||||
active_shards: esStatus.body[0]['active_shards_percent'],
|
||||
head_offset: null,
|
||||
first_indexed_block: firstIndexedBlock,
|
||||
last_indexed_block: lastIndexedBlock,
|
||||
active_shards: esStatus.body[0]['active_shards_percent']
|
||||
total_indexed_blocks: totalIndexed,
|
||||
missing_blocks: missingCounter,
|
||||
missing_pct: missingPct
|
||||
};
|
||||
let stat = 'OK';
|
||||
esStatus.body.forEach(status => {
|
||||
@@ -59,7 +101,7 @@ async function checkElastic(fastify: FastifyInstance) {
|
||||
}
|
||||
}
|
||||
|
||||
function createHealth(name: string, status, data?: any) {
|
||||
function createHealth(name: string, status, data?: any): ServiceResponse<any> {
|
||||
let time = Date.now();
|
||||
return {
|
||||
service: name,
|
||||
@@ -69,22 +111,27 @@ function createHealth(name: string, status, data?: any) {
|
||||
}
|
||||
}
|
||||
|
||||
async function getHealthQuery(fastify: FastifyInstance, request: FastifyRequest) {
|
||||
async function getHealthQuery(fastify: FastifyInstance) {
|
||||
let response = {
|
||||
version: fastify.manager.current_version,
|
||||
version_hash: fastify.manager.getServerHash(),
|
||||
host: fastify.manager.config.api.server_name,
|
||||
features: fastify.manager.config.features,
|
||||
health: []
|
||||
health: [],
|
||||
features: fastify.manager.config.features
|
||||
};
|
||||
response.health.push(await checkRabbit(fastify));
|
||||
response.health.push(await checkNodeos(fastify));
|
||||
response.health.push(await checkElastic(fastify));
|
||||
response.health = await Promise.all([
|
||||
checkRabbit(fastify),
|
||||
checkNodeos(fastify),
|
||||
checkElastic(fastify)
|
||||
]);
|
||||
const es = response.health.find(value => value.service === 'Elasticsearch');
|
||||
const nodeos = response.health.find(value => value.service === 'NodeosRPC');
|
||||
es.service_data.head_offset = nodeos.service_data.head_block_num - es.service_data.last_indexed_block;
|
||||
return response;
|
||||
}
|
||||
|
||||
export function healthHandler(fastify: FastifyInstance, route: string) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply<ServerResponse>) => {
|
||||
return async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
reply.send(await timedQuery(getHealthQuery, fastify, request, route));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import {FastifyInstance, RouteSchema} from "fastify";
|
||||
import {FastifyInstance, FastifySchema} from "fastify";
|
||||
import {addApiRoute, getRouteName} from "../../../helpers/functions";
|
||||
import {healthHandler} from "./health";
|
||||
|
||||
export default function (fastify: FastifyInstance, opts: any, next) {
|
||||
const schema: RouteSchema = {
|
||||
tags: ['status'],
|
||||
summary: "API Service Health Report"
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
healthHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
const schema: FastifySchema = {
|
||||
tags: ['status'],
|
||||
summary: "API Service Health Report"
|
||||
};
|
||||
addApiRoute(
|
||||
fastify,
|
||||
'GET',
|
||||
getRouteName(__filename),
|
||||
healthHandler,
|
||||
schema
|
||||
);
|
||||
next();
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user