Skip to content

Commit 81ae93b

Browse files
authored
Merge pull request #62 from eosrio/es-8-alpha
Es 8 alpha
2 parents 49d3b34 + eda6655 commit 81ae93b

File tree

5 files changed

+141
-68
lines changed

5 files changed

+141
-68
lines changed

api/routes/v1-history/get_transaction/get_transaction.ts

+86-18
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,18 @@ import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
22
import {mergeActionMeta, timedQuery} from "../../../helpers/functions";
33
import {createHash} from "crypto";
44
import flatstr from 'flatstr';
5+
import {GetInfoResult} from "eosjs/dist/eosjs-rpc-interfaces";
56

67
async function getTransaction(fastify: FastifyInstance, request: FastifyRequest) {
78
if (typeof request.body === 'string') {
89
request.body = JSON.parse(request.body)
910
}
1011
const body: any = request.body;
11-
const pResults = await Promise.all([fastify.eosjs.rpc.get_info(), fastify.elastic['search']({
12-
"index": fastify.manager.chain + '-action-*',
13-
"body": {
14-
"query": {
15-
"bool": {
16-
must: [
17-
{term: {"trx_id": body.id.toLowerCase()}}
18-
]
19-
}
20-
},
21-
"sort": {
22-
"global_sequence": "asc"
23-
}
24-
}
25-
})]);
26-
const results = pResults[1];
12+
const redis = fastify.redis;
13+
const trxId = body.id.toLowerCase();
14+
const conf = fastify.manager.config;
15+
const cachedData = await redis.hgetall('trx_' + trxId);
16+
2717
const response: any = {
2818
"id": body.id,
2919
"trx": {
@@ -42,11 +32,89 @@ async function getTransaction(fastify: FastifyInstance, request: FastifyRequest)
4232
},
4333
"block_num": 0,
4434
"block_time": "",
45-
"last_irreversible_block": pResults[0].last_irreversible_block_num,
35+
"last_irreversible_block": undefined,
4636
"traces": []
4737
};
4838

49-
const hits = results['body']['hits']['hits'];
39+
let hits;
40+
41+
// build get_info request with caching
42+
const $getInfo = new Promise<GetInfoResult>(resolve => {
43+
const key = `${fastify.manager.chain}_get_info`;
44+
fastify.redis.get(key).then(value => {
45+
if (value) {
46+
resolve(JSON.parse(value));
47+
} else {
48+
fastify.eosjs.rpc.get_info().then(value1 => {
49+
fastify.redis.set(key, JSON.stringify(value1), 'EX', 6);
50+
resolve(value1);
51+
}).catch((reason) => {
52+
console.log(reason);
53+
response.error = 'failed to get last_irreversible_block_num'
54+
resolve(null);
55+
});
56+
}
57+
});
58+
});
59+
60+
// reconstruct hits from cached data
61+
if (cachedData && Object.keys(cachedData).length > 0) {
62+
const gsArr = [];
63+
for (let cachedDataKey in cachedData) {
64+
gsArr.push(cachedData[cachedDataKey]);
65+
}
66+
gsArr.sort((a, b) => {
67+
return a.global_sequence - b.global_sequence;
68+
});
69+
hits = gsArr.map(value => {
70+
return {
71+
_source: JSON.parse(value)
72+
};
73+
});
74+
const promiseResults = await Promise.all([
75+
redis.ttl('trx_' + trxId),
76+
$getInfo
77+
]);
78+
response.cache_expires_in = promiseResults[0];
79+
response.last_irreversible_block = promiseResults[1].last_irreversible_block_num;
80+
}
81+
82+
// search on ES if cache is not present
83+
if (!hits) {
84+
const _size = conf.api.limits.get_trx_actions || 100;
85+
const blockHint = parseInt(body.block_num_hint, 10);
86+
let indexPattern = '';
87+
if (blockHint) {
88+
const idxPart = Math.ceil(blockHint / conf.settings.index_partition_size).toString().padStart(6, '0');
89+
indexPattern = fastify.manager.chain + `-action-${conf.settings.index_version}-${idxPart}`;
90+
} else {
91+
indexPattern = fastify.manager.chain + '-action-*';
92+
}
93+
let pResults;
94+
try {
95+
96+
// build search request
97+
const $search = fastify.elastic.search({
98+
index: indexPattern,
99+
size: _size,
100+
body: {
101+
query: {bool: {must: [{term: {trx_id: trxId}}]}},
102+
sort: {global_sequence: "asc"}
103+
}
104+
});
105+
106+
// execute in parallel
107+
pResults = await Promise.all([$getInfo, $search]);
108+
} catch (e) {
109+
console.log(e.message);
110+
if (e.meta.statusCode === 404) {
111+
return response;
112+
}
113+
}
114+
hits = pResults[1]['body']['hits']['hits'];
115+
response.last_irreversible_block = pResults[0].last_irreversible_block_num;
116+
}
117+
50118

51119
if (hits.length > 0) {
52120
const actions = hits;

api/routes/v1-history/get_transaction/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ export default function (fastify: FastifyInstance, opts: any, next) {
99
tags: ['history'],
1010
body: {
1111
type: ['object', 'string'],
12-
properties: {id: {description: 'transaction id', type: 'string'}},
12+
properties: {
13+
id: {description: 'transaction id', type: 'string'},
14+
block_num_hint: {description: 'block number hint', type: 'integer'},
15+
},
1316
required: ["id"]
1417
}
1518
});

helpers/elastic-routes.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,11 @@ export class ElasticRoutes {
370370
});
371371
}
372372
this.ingestNodeCounters[minIdx].docs += bulkData.body.length;
373-
if (this.ingestNodeCounters[minIdx].docs > 1000) {
373+
if (this.ingestNodeCounters[minIdx].docs > 10000) {
374374
this.resetCounters();
375375
}
376+
// print full request
377+
// console.log(JSON.stringify(bulkData));
376378
return this.cm.ingestClients[minIdx]['bulk'](bulkData);
377379
}
378380

package-lock.json

+42-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"license": "MIT",
2828
"dependencies": {
29-
"@elastic/elasticsearch": "^7.13.0",
29+
"@elastic/elasticsearch": "^7.14.0",
3030
"@eosrio/node-abieos": "^2.1.1",
3131
"@pm2/io": "^5.0.0",
3232
"amqplib": "^0.8.0",
@@ -35,7 +35,7 @@
3535
"cross-fetch": "^3.1.4",
3636
"eosjs": "^22.1.0",
3737
"fast-json-stringify": "^2.7.8",
38-
"fastify": "^3.20.1",
38+
"fastify": "^3.20.2",
3939
"fastify-autoload": "^3.8.0",
4040
"fastify-compress": "^3.6.0",
4141
"fastify-cors": "^6.0.2",
@@ -58,15 +58,15 @@
5858
"socket.io-redis": "^6.1.1",
5959
"telegraf": "^4.4.1",
6060
"typescript": "^4.3.5",
61-
"ws": "^8.0.0",
62-
"yargs": "^17.1.0"
61+
"ws": "^8.1.0",
62+
"yargs": "^17.1.1"
6363
},
6464
"devDependencies": {
6565
"@types/amqplib": "^0.8.1",
6666
"@types/async": "^3.2.7",
67-
"@types/ioredis": "^4.26.6",
67+
"@types/ioredis": "^4.26.7",
6868
"@types/lodash": "^4.14.172",
69-
"@types/node": "^16.4.12",
69+
"@types/node": "^16.6.1",
7070
"@types/nodemailer": "^6.4.4",
7171
"@types/ws": "^7.4.7"
7272
},

0 commit comments

Comments
 (0)