15
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

nem / symbolAdvent Calendar 2023

Day 16

Symbol REST改造 - timestampからblock取得 -

Last updated at Posted at 2023-12-15

はじめに

SymbolノードのRESTを改造してタイムスタンプからブロック情報を取得します。

なお、monakaさんのREST改造記事を参考にしています。とはいっても新たにAPIを増やすわけではなくクエリを増やすだけなのでもっと簡単です。

リポジトリコピー&restに移動

git clone https://github.com/symbol/symbol
cd symbol/client/rest

コード改変

client/rest/src/db/CatapultDb.js

// 195行目あたり
- blocks(signerPublicKey, beneficiaryAddress, options) {
+ blocks(signerPublicKey, beneficiaryAddress, fromTimestamp, toTimestamp, options) {

if (undefined !== beneficiaryAddress)
    conditions['block.beneficiaryAddress'] = Buffer.from(beneficiaryAddress);

////////

// 213行目あたりに以下を追加
+ if (undefined !== fromTimestamp || undefined !== toTimestamp) {
+    conditions['block.timestamp'] = {};
+    if (undefined !== fromTimestamp)
+        conditions['block.timestamp'].$gte = convertToLong(fromTimestamp);
+
+    if (undefined !== toTimestamp)
+        conditions['block.timestamp'].$lte = convertToLong(toTimestamp);
+    }

client/rest/src/routes/blockRoutes.js

module.exports = {
	register: (server, db, services) => {
		server.get('/blocks', (req, res, next) => {
			const { params } = req;
			const signerPublicKey = params.signerPublicKey ? routeUtils.parseArgument(params, 'signerPublicKey', 'publicKey') : undefined;
			const beneficiaryAddress = params.beneficiaryAddress
				? routeUtils.parseArgument(params, 'beneficiaryAddress', 'address')
				: undefined;

+			const fromTimestamp = params.fromTimestamp ? routeUtils.parseArgument(params, 'fromTimestamp', 'uint64') : undefined;
+			const toTimestamp = params.toTimestamp ? routeUtils.parseArgument(params, 'toTimestamp', 'uint64') : undefined;
			
            const offsetParsers = {
				id: 'objectId',
				height: 'uint64'
			};
			const options = routeUtils.parsePaginationArguments(params, services.config.pageSize, offsetParsers);

- 			return db.blocks(signerPublicKey, beneficiaryAddress, options)
+ 			return db.blocks(signerPublicKey, beneficiaryAddress, fromTimestamp, toTimestamp, options)
				.then(result => routeUtils.createSender(routeResultTypes.block).sendPage(res, next)(result));
		});
/////////


以上です。

あとはビルドしてcustom.ymlに追記してあげます

docker build -t hogehoge/custom_rest:latest .
custom.yml
symbolRestImage: hogehoge/custom_rest:latest

ブートストラップで起動してあげればOK

以下のようなクエリでfromTimestamp toTimestampで範囲指定してブロックを取得できます

ただし、ここでのタイムスタンプはSymbolのネメシスブロックのタイムスタンプが基準なので実際の日付から誘導されるタイムスタンプとは違います。

詳しくはここが分かりやすい

つまりメインネットであれば現在時刻をタイムスタンプにしてから1615853185を引いてあげる必要があります。

15
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?