From f96b4b5662aea68e3f1d919bd2398611c414179f Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 9 May 2022 11:45:27 +0900 Subject: [PATCH 001/166] Add middleware.js --- client/index.js | 10 ++++------ client/middleware.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 client/middleware.js diff --git a/client/index.js b/client/index.js index 97400698c..e0c0d5709 100755 --- a/client/index.js +++ b/client/index.js @@ -6,7 +6,6 @@ const express = require('express'); const cors = require('cors'); // NOTE(liayoo): To use async/await (ref: https://github.com/tedeh/jayson#promises) const jayson = require('jayson/promise'); -const rateLimit = require('express-rate-limit'); const ipWhitelist = require('ip-whitelist'); const matchUrl = require('match-url-wildcard'); const BlockchainNode = require('../node'); @@ -27,6 +26,7 @@ const { DevFlags } = require('../common/constants'); const { DevClientApiResultCode } = require('../common/result-code'); +const Middleware = require('./middleware'); const MAX_BLOCKS = 20; @@ -39,12 +39,10 @@ app.use(express.urlencoded({ const corsOrigin = NodeConfigs.CORS_WHITELIST === '*' ? NodeConfigs.CORS_WHITELIST : CommonUtil.getRegexpList(NodeConfigs.CORS_WHITELIST); app.use(cors({ origin: corsOrigin })); +// NOTE(minsulee2): complex express middleware is now built in middleware.js +const middleware = new Middleware(); if (NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT) { - const limiter = rateLimit({ - windowMs: 60 * 1000, // 1 minute - max: 60 // limit each IP to 60 requests per windowMs - }); - app.use(limiter); + app.use(middleware.limiter); } const eventHandler = NodeConfigs.ENABLE_EVENT_HANDLER === true ? new EventHandler() : null; diff --git a/client/middleware.js b/client/middleware.js new file mode 100644 index 000000000..93c08298e --- /dev/null +++ b/client/middleware.js @@ -0,0 +1,15 @@ +const rateLimit = require('express-rate-limit'); + +class Middleware { + constructor () { + } + + limiter() { + return rateLimit({ + windowMs: 6 * 1000, // 1 minute + max: 6 // limit each IP to 60 requests per windowMs + }); + } +} + +module.exports = Middleware; From 0cd8476eae49005df3182d067906402b7973861b Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 9 May 2022 14:13:44 +0900 Subject: [PATCH 002/166] Add MAX_READ_RATE_LIMIT and MAX_WRITE_RATE_LIMIT --- blockchain-configs/afan-shard/node_params.json | 2 ++ blockchain-configs/base/node_params.json | 2 ++ blockchain-configs/he-shard/node_params.json | 2 ++ blockchain-configs/mainnet-prod/node_params.json | 2 ++ blockchain-configs/sim-shard/node_params.json | 2 ++ blockchain-configs/testnet-dev/node_params.json | 2 ++ blockchain-configs/testnet-exp/node_params.json | 2 ++ blockchain-configs/testnet-prod/node_params.json | 2 ++ blockchain-configs/testnet-sandbox/node_params.json | 2 ++ blockchain-configs/testnet-staging/node_params.json | 2 ++ 10 files changed, 20 insertions(+) diff --git a/blockchain-configs/afan-shard/node_params.json b/blockchain-configs/afan-shard/node_params.json index 121b46723..a5719cf4c 100644 --- a/blockchain-configs/afan-shard/node_params.json +++ b/blockchain-configs/afan-shard/node_params.json @@ -43,6 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 3, "MAX_NUM_SNAPSHOTS": 10, + "MAX_READ_RATE_LIMIT": 1, + "MAX_WRITE_RATE_LIMIT": 10, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/base/node_params.json b/blockchain-configs/base/node_params.json index ac05467f3..9fa87c113 100644 --- a/blockchain-configs/base/node_params.json +++ b/blockchain-configs/base/node_params.json @@ -43,6 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, + "MAX_READ_RATE_LIMIT": 1, + "MAX_WRITE_RATE_LIMIT": 10, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/he-shard/node_params.json b/blockchain-configs/he-shard/node_params.json index 94979852c..ef6b9e069 100644 --- a/blockchain-configs/he-shard/node_params.json +++ b/blockchain-configs/he-shard/node_params.json @@ -43,6 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, + "MAX_READ_RATE_LIMIT": 1, + "MAX_WRITE_RATE_LIMIT": 10, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/mainnet-prod/node_params.json b/blockchain-configs/mainnet-prod/node_params.json index a19f4980c..edbbaf836 100644 --- a/blockchain-configs/mainnet-prod/node_params.json +++ b/blockchain-configs/mainnet-prod/node_params.json @@ -42,6 +42,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, + "MAX_READ_RATE_LIMIT": 1, + "MAX_WRITE_RATE_LIMIT": 10, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/sim-shard/node_params.json b/blockchain-configs/sim-shard/node_params.json index b548d1289..d20edda8b 100644 --- a/blockchain-configs/sim-shard/node_params.json +++ b/blockchain-configs/sim-shard/node_params.json @@ -43,6 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, + "MAX_READ_RATE_LIMIT": 1, + "MAX_WRITE_RATE_LIMIT": 10, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-dev/node_params.json b/blockchain-configs/testnet-dev/node_params.json index f8e71f5a9..707f6e075 100644 --- a/blockchain-configs/testnet-dev/node_params.json +++ b/blockchain-configs/testnet-dev/node_params.json @@ -43,6 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, + "MAX_READ_RATE_LIMIT": 1, + "MAX_WRITE_RATE_LIMIT": 10, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-exp/node_params.json b/blockchain-configs/testnet-exp/node_params.json index c66cc9975..daec0982e 100644 --- a/blockchain-configs/testnet-exp/node_params.json +++ b/blockchain-configs/testnet-exp/node_params.json @@ -43,6 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, + "MAX_READ_RATE_LIMIT": 1, + "MAX_WRITE_RATE_LIMIT": 10, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-prod/node_params.json b/blockchain-configs/testnet-prod/node_params.json index 4fff5afaa..e4a77939f 100644 --- a/blockchain-configs/testnet-prod/node_params.json +++ b/blockchain-configs/testnet-prod/node_params.json @@ -43,6 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, + "MAX_READ_RATE_LIMIT": 1, + "MAX_WRITE_RATE_LIMIT": 10, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-sandbox/node_params.json b/blockchain-configs/testnet-sandbox/node_params.json index 393fed3bd..41f979874 100644 --- a/blockchain-configs/testnet-sandbox/node_params.json +++ b/blockchain-configs/testnet-sandbox/node_params.json @@ -43,6 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, + "MAX_READ_RATE_LIMIT": 1, + "MAX_WRITE_RATE_LIMIT": 10, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-staging/node_params.json b/blockchain-configs/testnet-staging/node_params.json index 65deb2378..d1d2632db 100644 --- a/blockchain-configs/testnet-staging/node_params.json +++ b/blockchain-configs/testnet-staging/node_params.json @@ -43,6 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, + "MAX_READ_RATE_LIMIT": 1, + "MAX_WRITE_RATE_LIMIT": 10, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, From 64b699748f78ca2a89e761acb2d3cd82858dc17e Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 9 May 2022 14:40:06 +0900 Subject: [PATCH 003/166] Update Middleware --- client/index.js | 3 --- client/middleware.js | 45 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/client/index.js b/client/index.js index e0c0d5709..e96148065 100755 --- a/client/index.js +++ b/client/index.js @@ -41,9 +41,6 @@ const corsOrigin = NodeConfigs.CORS_WHITELIST === '*' ? app.use(cors({ origin: corsOrigin })); // NOTE(minsulee2): complex express middleware is now built in middleware.js const middleware = new Middleware(); -if (NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT) { - app.use(middleware.limiter); -} const eventHandler = NodeConfigs.ENABLE_EVENT_HANDLER === true ? new EventHandler() : null; const node = new BlockchainNode(null, eventHandler); diff --git a/client/middleware.js b/client/middleware.js index 93c08298e..4af32103c 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -1,14 +1,49 @@ const rateLimit = require('express-rate-limit'); +const { NodeConfigs } = require('../common/constants'); + class Middleware { constructor () { + this.readRateLimit = this.setReadRateLimit(); + this.writeRateLimit = this.setWriteRateLimit(); + } + + setReadRateLimit() { + this.readRateLimit = NodeConfigs.MAX_READ_RATE_LIMIT; + return this; + } + + setWriteRateLimit() { + this.writeRateLimit = NodeConfigs.MAX_WRITE_RATE_LIMIT; + return this; + } + + getReadRateLimit() { + return this.readRateLimit; + } + + getWriteRateLimit() { + return this.writeRateLimit; + } + + emptyHandler(req, res, next) { + return next(); + } + + readLimiter() { + return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? + rateLimit({ + windowMs: 1000, // 1 second + max: this.getReadRateLimit() // limit each IP to maximum of read rate limit + }) : this.emptyHandler(); } - limiter() { - return rateLimit({ - windowMs: 6 * 1000, // 1 minute - max: 6 // limit each IP to 60 requests per windowMs - }); + writeLimiter() { + return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? + rateLimit({ + windowMs: 1000, // 1 second + max: this.getWriteRateLimit() // limit each IP to maximum of write rate limit + }) : this.emptyHandler(); } } From 9f8decc9beb7eb8f436016eea4e2565cd29cbe50 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 9 May 2022 15:17:00 +0900 Subject: [PATCH 004/166] Add middleware.readLimiter --- client/index.js | 94 ++++++++++++++++++++++---------------------- client/middleware.js | 14 ++++--- 2 files changed, 55 insertions(+), 53 deletions(-) diff --git a/client/index.js b/client/index.js index e96148065..4cde0a8d6 100755 --- a/client/index.js +++ b/client/index.js @@ -71,7 +71,7 @@ app.post( jayson.server(jsonRpcApis).middleware() ); -app.get('/', (req, res, next) => { +app.get('/', middleware.readLimiter(), (req, res, next) => { const welcome = `[Welcome to AIN Blockchain Node]\n\n- CURRENT_PROTOCOL_VERSION: ${BlockchainConsts.CURRENT_PROTOCOL_VERSION}\n- DATA_PROTOCOL_VERSION: ${BlockchainConsts.DATA_PROTOCOL_VERSION}\n- CONSENSUS_PROTOCOL_VERSION: ${BlockchainConsts.CONSENSUS_PROTOCOL_VERSION}\n\nDevelopers Guide: ${NodeConfigs.BLOCKCHAIN_GUIDE_URL}`; res.status(200) .set('Content-Type', 'text/plain') @@ -79,7 +79,7 @@ app.get('/', (req, res, next) => { .end(); }); -app.get('/health_check', (req, res, next) => { +app.get('/health_check', middleware.readLimiter(), (req, res, next) => { const result = p2pServer.getNodeHealth(); res.status(200) .set('Content-Type', 'text/plain') @@ -88,7 +88,7 @@ app.get('/health_check', (req, res, next) => { }); // Exports metrics for Prometheus. -app.get('/metrics', async (req, res, next) => { +app.get('/metrics', middleware.readLimiter(), async (req, res, next) => { const beginTime = Date.now(); const status = p2pClient.getStatus(); const result = CommonUtil.objToMetrics(status); @@ -108,7 +108,7 @@ app.get('/metrics', async (req, res, next) => { }); // Used in wait_until_node_sync_gcp.sh -app.get('/last_block_number', (req, res, next) => { +app.get('/last_block_number', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.bc.lastBlockNumber(); const latency = Date.now() - beginTime; @@ -129,7 +129,7 @@ app.use(ipWhitelist((ip) => { * Dev Client GET APIs (available to whitelisted IPs) */ -app.get('/get_value', (req, res, next) => { +app.get('/get_value', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.getValue(req.query.ref, CommonUtil.toGetOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -143,7 +143,7 @@ app.get('/get_value', (req, res, next) => { .end(); }); -app.get('/get_function', (req, res, next) => { +app.get('/get_function', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.getFunction(req.query.ref, CommonUtil.toGetOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -157,7 +157,7 @@ app.get('/get_function', (req, res, next) => { .end(); }); -app.get('/get_rule', (req, res, next) => { +app.get('/get_rule', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.getRule(req.query.ref, CommonUtil.toGetOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -171,7 +171,7 @@ app.get('/get_rule', (req, res, next) => { .end(); }); -app.get('/get_owner', (req, res, next) => { +app.get('/get_owner', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.getOwner(req.query.ref, CommonUtil.toGetOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -188,7 +188,7 @@ app.get('/get_owner', (req, res, next) => { /** * Returns the state proof at the given full database path. */ -app.get('/get_state_proof', (req, res, next) => { +app.get('/get_state_proof', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.getStateProof(req.query.ref); const latency = Date.now() - beginTime; @@ -205,7 +205,7 @@ app.get('/get_state_proof', (req, res, next) => { /** * Returns the state proof hash at the given full database path. */ -app.get('/get_proof_hash', (req, res, next) => { +app.get('/get_proof_hash', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.getProofHash(req.query.ref); const latency = Date.now() - beginTime; @@ -222,7 +222,7 @@ app.get('/get_proof_hash', (req, res, next) => { /** * Returns the state information at the given full database path. */ -app.get('/get_state_info', (req, res, next) => { +app.get('/get_state_info', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.getStateInfo(req.query.ref); const latency = Date.now() - beginTime; @@ -239,7 +239,7 @@ app.get('/get_state_info', (req, res, next) => { /** * Returns the state usage of the given app. */ -app.get('/get_state_usage', (req, res, next) => { +app.get('/get_state_usage', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.getStateUsageWithStakingInfo(req.query.app_name); const latency = Date.now() - beginTime; @@ -253,7 +253,7 @@ app.get('/get_state_usage', (req, res, next) => { .end(); }); -app.get('/match_function', (req, res, next) => { +app.get('/match_function', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.matchFunction(req.query.ref, CommonUtil.toMatchOrEvalOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -267,7 +267,7 @@ app.get('/match_function', (req, res, next) => { .end(); }); -app.get('/match_rule', (req, res, next) => { +app.get('/match_rule', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.matchRule(req.query.ref, CommonUtil.toMatchOrEvalOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -281,7 +281,7 @@ app.get('/match_rule', (req, res, next) => { .end(); }); -app.get('/match_owner', (req, res, next) => { +app.get('/match_owner', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.matchOwner(req.query.ref, CommonUtil.toMatchOrEvalOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -295,7 +295,7 @@ app.get('/match_owner', (req, res, next) => { .end(); }); -app.post('/eval_rule', (req, res, next) => { +app.post('/eval_rule', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const body = req.body; const auth = {}; @@ -319,7 +319,7 @@ app.post('/eval_rule', (req, res, next) => { .end(); }); -app.post('/eval_owner', (req, res, next) => { +app.post('/eval_owner', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const body = req.body; const auth = {}; @@ -342,7 +342,7 @@ app.post('/eval_owner', (req, res, next) => { .end(); }); -app.post('/get', (req, res, next) => { +app.post('/get', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.db.get(req.body.op_list); const latency = Date.now() - beginTime; @@ -356,7 +356,7 @@ app.post('/get', (req, res, next) => { .end(); }); -app.get('/status', (req, res, next) => { +app.get('/status', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = p2pClient.getStatus(); const latency = Date.now() - beginTime; @@ -367,7 +367,7 @@ app.get('/status', (req, res, next) => { .end(); }); -app.get('/node_status', (req, res, next) => { +app.get('/node_status', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = p2pServer.getNodeStatus(); const latency = Date.now() - beginTime; @@ -378,7 +378,7 @@ app.get('/node_status', (req, res, next) => { .end(); }); -app.get('/connection_status', (req, res) => { +app.get('/connection_status', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = p2pClient.getConnectionStatus(); const latency = Date.now() - beginTime; @@ -389,7 +389,7 @@ app.get('/connection_status', (req, res) => { .end(); }) -app.get('/client_status', (req, res) => { +app.get('/client_status', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = p2pClient.getClientStatus(); const latency = Date.now() - beginTime; @@ -400,7 +400,7 @@ app.get('/client_status', (req, res) => { .end(); }) -app.get('/blocks', (req, res, next) => { +app.get('/blocks', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const blockEnd = node.bc.lastBlockNumber() + 1; const blockBegin = Math.max(blockEnd - MAX_BLOCKS, 0); @@ -413,7 +413,7 @@ app.get('/blocks', (req, res, next) => { .end(); }); -app.get('/last_block', (req, res, next) => { +app.get('/last_block', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.bc.lastBlock(); const latency = Date.now() - beginTime; @@ -424,7 +424,7 @@ app.get('/last_block', (req, res, next) => { .end(); }); -app.get('/tx_pool', (req, res, next) => { +app.get('/tx_pool', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = Object.fromEntries(node.tp.transactions); const latency = Date.now() - beginTime; @@ -435,7 +435,7 @@ app.get('/tx_pool', (req, res, next) => { .end(); }); -app.get('/tx_tracker', (req, res, next) => { +app.get('/tx_tracker', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = Object.fromEntries(node.tp.transactionTracker); const latency = Date.now() - beginTime; @@ -446,7 +446,7 @@ app.get('/tx_tracker', (req, res, next) => { .end(); }); -app.get('/committed_nonce_tracker', (req, res, next) => { +app.get('/committed_nonce_tracker', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.tp.committedNonceTracker; const latency = Date.now() - beginTime; @@ -457,7 +457,7 @@ app.get('/committed_nonce_tracker', (req, res, next) => { .end(); }); -app.get('/pending_nonce_tracker', (req, res, next) => { +app.get('/pending_nonce_tracker', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.tp.pendingNonceTracker; const latency = Date.now() - beginTime; @@ -468,7 +468,7 @@ app.get('/pending_nonce_tracker', (req, res, next) => { .end(); }); -app.get('/protocol_versions', (req, res) => { +app.get('/protocol_versions', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = p2pClient.server.getProtocolInfo(); const latency = Date.now() - beginTime; @@ -479,7 +479,7 @@ app.get('/protocol_versions', (req, res) => { .end(); }); -app.get('/state_versions', (req, res) => { +app.get('/state_versions', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = p2pServer.getStateVersionStatus(); const latency = Date.now() - beginTime; @@ -491,7 +491,7 @@ app.get('/state_versions', (req, res) => { }); // TODO(platfowner): Support for subtree snapshots (i.e. with ref path). -app.get('/get_final_state_snapshot', (req, res) => { +app.get('/get_final_state_snapshot', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = node.takeFinalStateSnapshot(CommonUtil.toGetOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -503,7 +503,7 @@ app.get('/get_final_state_snapshot', (req, res) => { }); // TODO(platfowner): Support for subtree snapshots (i.e. with ref path). -app.get('/get_final_radix_snapshot', (req, res) => { +app.get('/get_final_radix_snapshot', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = node.takeFinalRadixSnapshot(); const latency = Date.now() - beginTime; @@ -514,7 +514,7 @@ app.get('/get_final_radix_snapshot', (req, res) => { .end(); }); -app.get('/tx_pool_size_util', (req, res) => { +app.get('/tx_pool_size_util', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const address = req.query.address; const txPoolSizeUtil = node.getTxPoolSizeUtilization(address); @@ -526,7 +526,7 @@ app.get('/tx_pool_size_util', (req, res) => { .end(); }); -app.get('/get_transaction', (req, res, next) => { +app.get('/get_transaction', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const transactionInfo = node.getTransactionByHash(req.query.hash); const latency = Date.now() - beginTime; @@ -537,7 +537,7 @@ app.get('/get_transaction', (req, res, next) => { .end(); }); -app.get('/get_block_by_hash', (req, res, next) => { +app.get('/get_block_by_hash', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const block = node.bc.getBlockByHash(req.query.hash); const latency = Date.now() - beginTime; @@ -548,7 +548,7 @@ app.get('/get_block_by_hash', (req, res, next) => { .end(); }); -app.get('/get_block_by_number', (req, res) => { +app.get('/get_block_by_number', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const block = node.bc.getBlockByNumber(req.query.number); const latency = Date.now() - beginTime; @@ -559,7 +559,7 @@ app.get('/get_block_by_number', (req, res) => { .end(); }); -app.get('/get_block_info_by_number', (req, res) => { +app.get('/get_block_info_by_number', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const blockInfo = node.bc.getBlockInfoByNumber(req.query.number); const latency = Date.now() - beginTime; @@ -570,7 +570,7 @@ app.get('/get_block_info_by_number', (req, res) => { .end(); }); -app.get('/get_address', (req, res, next) => { +app.get('/get_address', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.account ? node.account.address : null; const latency = Date.now() - beginTime; @@ -581,7 +581,7 @@ app.get('/get_address', (req, res, next) => { .end(); }); -app.get('/get_nonce', (req, res, next) => { +app.get('/get_nonce', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.getNonceForAddr(req.query.address, req.query.from === 'pending'); const latency = Date.now() - beginTime; @@ -592,7 +592,7 @@ app.get('/get_nonce', (req, res, next) => { .end(); }); -app.get('/get_timestamp', (req, res, next) => { +app.get('/get_timestamp', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.getTimestampForAddr(req.query.address, req.query.from === 'pending'); const latency = Date.now() - beginTime; @@ -603,7 +603,7 @@ app.get('/get_timestamp', (req, res, next) => { .end(); }); -app.get('/validate_app_name', (req, res, next) => { +app.get('/validate_app_name', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.validateAppName(req.query.app_name); const latency = Date.now() - beginTime; @@ -614,7 +614,7 @@ app.get('/validate_app_name', (req, res, next) => { .end(); }); -app.get('/get_sharding', (req, res, next) => { +app.get('/get_sharding', middleware.readLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = node.getSharding(); const latency = Date.now() - beginTime; @@ -628,7 +628,7 @@ app.get('/get_sharding', (req, res, next) => { .end(); }); -app.get('/get_raw_consensus_status', (req, res) => { +app.get('/get_raw_consensus_status', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = p2pServer.consensus.getRawStatus(); const latency = Date.now() - beginTime; @@ -639,7 +639,7 @@ app.get('/get_raw_consensus_status', (req, res) => { .end(); }); -app.get('/get_consensus_status', (req, res) => { +app.get('/get_consensus_status', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = p2pServer.consensus.getStatus(); const latency = Date.now() - beginTime; @@ -650,7 +650,7 @@ app.get('/get_consensus_status', (req, res) => { .end(); }); -app.get('/get_network_id', (req, res) => { +app.get('/get_network_id', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = p2pServer.node.getBlockchainParam('genesis/network_id'); const latency = Date.now() - beginTime; @@ -661,7 +661,7 @@ app.get('/get_network_id', (req, res) => { .end(); }); -app.get('/get_chain_id', (req, res) => { +app.get('/get_chain_id', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = p2pServer.node.getBlockchainParam('genesis/chain_id'); const latency = Date.now() - beginTime; @@ -672,7 +672,7 @@ app.get('/get_chain_id', (req, res) => { .end(); }); -app.get('/get_config', (req, res) => { +app.get('/get_config', middleware.readLimiter(), (req, res) => { const beginTime = Date.now(); const result = p2pClient.getConfig(); const latency = Date.now() - beginTime; diff --git a/client/middleware.js b/client/middleware.js index 4af32103c..aa835e155 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -8,6 +8,12 @@ class Middleware { this.writeRateLimit = this.setWriteRateLimit(); } + _emptyHandler() { + return (req, res, next) => { + return next(); + } + } + setReadRateLimit() { this.readRateLimit = NodeConfigs.MAX_READ_RATE_LIMIT; return this; @@ -26,16 +32,12 @@ class Middleware { return this.writeRateLimit; } - emptyHandler(req, res, next) { - return next(); - } - readLimiter() { return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? rateLimit({ windowMs: 1000, // 1 second max: this.getReadRateLimit() // limit each IP to maximum of read rate limit - }) : this.emptyHandler(); + }) : this._emptyHandler(); } writeLimiter() { @@ -43,7 +45,7 @@ class Middleware { rateLimit({ windowMs: 1000, // 1 second max: this.getWriteRateLimit() // limit each IP to maximum of write rate limit - }) : this.emptyHandler(); + }) : this._emptyHandler(); } } From 4c4de93c56e5b205cb4778685491dc2e47923a26 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 9 May 2022 15:47:11 +0900 Subject: [PATCH 005/166] Move express middleware into middleware.js --- client/index.js | 22 +++++++++------------- client/middleware.js | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/client/index.js b/client/index.js index 4cde0a8d6..ca9cbac42 100755 --- a/client/index.js +++ b/client/index.js @@ -31,16 +31,18 @@ const Middleware = require('./middleware'); const MAX_BLOCKS = 20; const app = express(); -app.use(express.json({ limit: NodeConfigs.REQUEST_BODY_SIZE_LIMIT })); -app.use(express.urlencoded({ - extended: true, - limit: NodeConfigs.REQUEST_BODY_SIZE_LIMIT -})); +// NOTE(minsulee2): complex express middleware is now built in middleware.js +const middleware = new Middleware(); +app.use(middleware.expressJsonRequestBodySizeLimiter()); +app.use(middleware.expressUrlencdedRequestBodySizeLimiter()); const corsOrigin = NodeConfigs.CORS_WHITELIST === '*' ? NodeConfigs.CORS_WHITELIST : CommonUtil.getRegexpList(NodeConfigs.CORS_WHITELIST); app.use(cors({ origin: corsOrigin })); -// NOTE(minsulee2): complex express middleware is now built in middleware.js -const middleware = new Middleware(); +app.use(ipWhitelist((ip) => { + return CommonUtil.isWildcard(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || + matchUrl(ip, NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || + matchUrl(convertIpv6ToIpv4(ip), NodeConfigs.DEV_CLIENT_API_IP_WHITELIST); +})); const eventHandler = NodeConfigs.ENABLE_EVENT_HANDLER === true ? new EventHandler() : null; const node = new BlockchainNode(null, eventHandler); @@ -119,12 +121,6 @@ app.get('/last_block_number', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.use(ipWhitelist((ip) => { - return CommonUtil.isWildcard(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || - matchUrl(ip, NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || - matchUrl(convertIpv6ToIpv4(ip), NodeConfigs.DEV_CLIENT_API_IP_WHITELIST); -})); - /** * Dev Client GET APIs (available to whitelisted IPs) */ diff --git a/client/middleware.js b/client/middleware.js index aa835e155..b8518cd14 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -1,9 +1,11 @@ +const express = require('express'); const rateLimit = require('express-rate-limit'); const { NodeConfigs } = require('../common/constants'); class Middleware { constructor () { + this.expressRequestBodySizeLimit = this.setExpressRequestBodySizeLimit(); this.readRateLimit = this.setReadRateLimit(); this.writeRateLimit = this.setWriteRateLimit(); } @@ -14,6 +16,11 @@ class Middleware { } } + setExpressRequestBodySizeLimit() { + this.expressRequestBodySizeLimit = NodeConfigs.REQUEST_BODY_SIZE_LIMIT; + return this; + } + setReadRateLimit() { this.readRateLimit = NodeConfigs.MAX_READ_RATE_LIMIT; return this; @@ -24,6 +31,10 @@ class Middleware { return this; } + getExpressRequestBodySizeLimit() { + return this.expressRequestBodySizeLimit; + } + getReadRateLimit() { return this.readRateLimit; } @@ -32,6 +43,17 @@ class Middleware { return this.writeRateLimit; } + expressJsonRequestBodySizeLimiter() { + return express.json({ limit: this.getExpressRequestBodySizeLimit() }); + } + + expressUrlencdedRequestBodySizeLimiter() { + return express.urlencoded({ + extended: true, + limit: this.getExpressRequestBodySizeLimit() + }); + } + readLimiter() { return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? rateLimit({ From 2369e28741c7cec508f585e475dcf730f3636d60 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 9 May 2022 16:15:09 +0900 Subject: [PATCH 006/166] Move cors setting at middleware.js --- client/index.js | 5 +---- client/middleware.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/client/index.js b/client/index.js index ca9cbac42..fa9508525 100755 --- a/client/index.js +++ b/client/index.js @@ -3,7 +3,6 @@ const logger = new (require('../logger'))('CLIENT'); const express = require('express'); -const cors = require('cors'); // NOTE(liayoo): To use async/await (ref: https://github.com/tedeh/jayson#promises) const jayson = require('jayson/promise'); const ipWhitelist = require('ip-whitelist'); @@ -35,9 +34,7 @@ const app = express(); const middleware = new Middleware(); app.use(middleware.expressJsonRequestBodySizeLimiter()); app.use(middleware.expressUrlencdedRequestBodySizeLimiter()); -const corsOrigin = NodeConfigs.CORS_WHITELIST === '*' ? - NodeConfigs.CORS_WHITELIST : CommonUtil.getRegexpList(NodeConfigs.CORS_WHITELIST); -app.use(cors({ origin: corsOrigin })); +app.use(middleware.corsLimiter()); app.use(ipWhitelist((ip) => { return CommonUtil.isWildcard(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || matchUrl(ip, NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || diff --git a/client/middleware.js b/client/middleware.js index b8518cd14..8cb70092b 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -1,11 +1,14 @@ const express = require('express'); +const cors = require('cors'); const rateLimit = require('express-rate-limit'); const { NodeConfigs } = require('../common/constants'); +const { getRegexpList } = require('../common/common-util'); class Middleware { constructor () { this.expressRequestBodySizeLimit = this.setExpressRequestBodySizeLimit(); + this.corsOriginList = this.setCorsOriginList(); this.readRateLimit = this.setReadRateLimit(); this.writeRateLimit = this.setWriteRateLimit(); } @@ -21,6 +24,12 @@ class Middleware { return this; } + setCorsOriginList() { + this.corsOriginList = NodeConfigs.CORS_WHITELIST === '*' ? + NodeConfigs.CORS_WHITELIST : getRegexpList(NodeConfigs.CORS_WHITELIST); + return this; + } + setReadRateLimit() { this.readRateLimit = NodeConfigs.MAX_READ_RATE_LIMIT; return this; @@ -35,6 +44,10 @@ class Middleware { return this.expressRequestBodySizeLimit; } + getCorsOriginList() { + return this.corsOriginList; + } + getReadRateLimit() { return this.readRateLimit; } @@ -54,6 +67,10 @@ class Middleware { }); } + corsLimiter() { + return cors({ origin: this.getCorsOriginList() }) + } + readLimiter() { return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? rateLimit({ From 6fda32eaf393598e5594b807d641e41a3137bc83 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 9 May 2022 16:49:27 +0900 Subject: [PATCH 007/166] Init all middleware at middleware.js --- client/index.js | 10 +++------- client/middleware.js | 42 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/client/index.js b/client/index.js index fa9508525..e61fe3fa2 100755 --- a/client/index.js +++ b/client/index.js @@ -5,7 +5,6 @@ const logger = new (require('../logger'))('CLIENT'); const express = require('express'); // NOTE(liayoo): To use async/await (ref: https://github.com/tedeh/jayson#promises) const jayson = require('jayson/promise'); -const ipWhitelist = require('ip-whitelist'); const matchUrl = require('match-url-wildcard'); const BlockchainNode = require('../node'); const P2pClient = require('../p2p'); @@ -30,16 +29,13 @@ const Middleware = require('./middleware'); const MAX_BLOCKS = 20; const app = express(); -// NOTE(minsulee2): complex express middleware is now built in middleware.js +// NOTE(minsulee2): complex express middleware is now built at middleware.js const middleware = new Middleware(); +middleware.printAll(); app.use(middleware.expressJsonRequestBodySizeLimiter()); app.use(middleware.expressUrlencdedRequestBodySizeLimiter()); app.use(middleware.corsLimiter()); -app.use(ipWhitelist((ip) => { - return CommonUtil.isWildcard(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || - matchUrl(ip, NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || - matchUrl(convertIpv6ToIpv4(ip), NodeConfigs.DEV_CLIENT_API_IP_WHITELIST); -})); +app.use(middleware.ipWhitelistLimiter()); const eventHandler = NodeConfigs.ENABLE_EVENT_HANDLER === true ? new EventHandler() : null; const node = new BlockchainNode(null, eventHandler); diff --git a/client/middleware.js b/client/middleware.js index 8cb70092b..5a59ef14d 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -1,16 +1,22 @@ const express = require('express'); const cors = require('cors'); +const ipWhitelist = require('ip-whitelist'); const rateLimit = require('express-rate-limit'); const { NodeConfigs } = require('../common/constants'); -const { getRegexpList } = require('../common/common-util'); +const { + getRegexpList, + isWildcard +} = require('../common/common-util'); +const { convertIpv6ToIpv4 } = require('../common/network-util'); class Middleware { constructor () { - this.expressRequestBodySizeLimit = this.setExpressRequestBodySizeLimit(); - this.corsOriginList = this.setCorsOriginList(); - this.readRateLimit = this.setReadRateLimit(); - this.writeRateLimit = this.setWriteRateLimit(); + this.setExpressRequestBodySizeLimit(); + this.setCorsOriginList(); + this.setDevClientApiIpWhitelist(); + this.setReadRateLimit(); + this.setWriteRateLimit(); } _emptyHandler() { @@ -30,6 +36,11 @@ class Middleware { return this; } + setDevClientApiIpWhitelist() { + this.devClientApiIpWhitelist = NodeConfigs.DEV_CLIENT_API_IP_WHITELIST; + return this; + } + setReadRateLimit() { this.readRateLimit = NodeConfigs.MAX_READ_RATE_LIMIT; return this; @@ -48,6 +59,10 @@ class Middleware { return this.corsOriginList; } + getDevClientApiIpWhitelist() { + return this.devClientApiIpWhitelist; + } + getReadRateLimit() { return this.readRateLimit; } @@ -71,6 +86,15 @@ class Middleware { return cors({ origin: this.getCorsOriginList() }) } + ipWhitelistLimiter() { + return ipWhitelist((ip) => { + const whitelist = this.getDevClientApiIpWhitelist(); + return isWildcard(whitelist) || + matchUrl(ip, whitelist) || + matchUrl(convertIpv6ToIpv4(ip), whitelist); + }) + } + readLimiter() { return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? rateLimit({ @@ -86,6 +110,14 @@ class Middleware { max: this.getWriteRateLimit() // limit each IP to maximum of write rate limit }) : this._emptyHandler(); } + + // NOTE(minsulee2): debugging purpose + printAll() { + console.log(this.getCorsOriginList()); + console.log(this.getDevClientApiIpWhitelist()); + console.log(this.getExpressRequestBodySizeLimit()); + console.log(this.getReadRateLimit(), this.getWriteRateLimit()); + } } module.exports = Middleware; From 2369b13c01721394465f8a3fa92ae37c2bbd4267 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 9 May 2022 16:52:17 +0900 Subject: [PATCH 008/166] Add writeLimiter --- client/index.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/client/index.js b/client/index.js index e61fe3fa2..19ff0cefa 100755 --- a/client/index.js +++ b/client/index.js @@ -677,7 +677,7 @@ app.get('/get_config', middleware.readLimiter(), (req, res) => { */ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { - app.post('/set_value', (req, res, next) => { + app.post('/set_value', middleware.writeLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.SET_VALUE)); @@ -693,7 +693,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/inc_value', (req, res, next) => { + app.post('/inc_value', middleware.writeLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.INC_VALUE)); @@ -709,7 +709,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/dec_value', (req, res, next) => { + app.post('/dec_value', middleware.writeLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.DEC_VALUE)); @@ -725,7 +725,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/set_function', (req, res, next) => { + app.post('/set_function', middleware.writeLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.SET_FUNCTION)); @@ -741,7 +741,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/set_rule', (req, res, next) => { + app.post('/set_rule', middleware.writeLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.SET_RULE)); @@ -757,7 +757,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/set_owner', (req, res, next) => { + app.post('/set_owner', middleware.writeLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.SET_OWNER)); @@ -775,7 +775,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { // A custom address can be used as a devel method for bypassing the trasaction verification. // TODO(platfowner): Replace custom address with real signature. - app.post('/set', (req, res, next) => { + app.post('/set', middleware.writeLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createMultiSetTxBody(req.body)); const latency = Date.now() - beginTime; @@ -790,7 +790,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/batch', (req, res, next) => { + app.post('/batch', middleware.writeLimiter(), (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createBatchTxBody(req.body)); const latency = Date.now() - beginTime; @@ -801,7 +801,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/sign_transaction', (req, res) => { + app.post('/sign_transaction', middleware.writeLimiter(), (req, res) => { const beginTime = Date.now(); const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.CLIENT_API_SET, latency); @@ -811,7 +811,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }) - app.post('/broadcast_consensus_msg', (req, res) => { + app.post('/broadcast_consensus_msg', middleware.writeLimiter(), (req, res) => { const beginTime = Date.now(); p2pClient.broadcastConsensusMessage(req.body); const latency = Date.now() - beginTime; @@ -825,7 +825,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { if (eventHandler) { // NOTE(cshcomcom): For event handler load balancer! It doesn't mean healthy. - app.get('/eh_load_balancer_health_check', (req, res, next) => { + app.get('/eh_load_balancer_health_check', middleware.readLimiter(), (req, res, next) => { const result = eventHandler.getEventHandlerHealth(); res.status(200) .set('Content-Type', 'text/plain') From 05399d87379e5fa6872fccb7ec3ac2be8d51c76e Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 9 May 2022 16:53:03 +0900 Subject: [PATCH 009/166] Move some dependencies --- client/index.js | 6 +----- client/middleware.js | 1 + 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/client/index.js b/client/index.js index 19ff0cefa..3859f153e 100755 --- a/client/index.js +++ b/client/index.js @@ -5,16 +5,12 @@ const logger = new (require('../logger'))('CLIENT'); const express = require('express'); // NOTE(liayoo): To use async/await (ref: https://github.com/tedeh/jayson#promises) const jayson = require('jayson/promise'); -const matchUrl = require('match-url-wildcard'); const BlockchainNode = require('../node'); const P2pClient = require('../p2p'); const EventHandler = require('../event-handler'); const CommonUtil = require('../common/common-util'); const VersionUtil = require('../common/version-util'); -const { - convertIpv6ToIpv4, - sendGetRequest -} = require('../common/network-util'); +const { sendGetRequest } = require('../common/network-util'); const { BlockchainConsts, NodeConfigs, diff --git a/client/middleware.js b/client/middleware.js index 5a59ef14d..042ddbcf9 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -2,6 +2,7 @@ const express = require('express'); const cors = require('cors'); const ipWhitelist = require('ip-whitelist'); const rateLimit = require('express-rate-limit'); +const matchUrl = require('match-url-wildcard'); const { NodeConfigs } = require('../common/constants'); const { From ffd97755f9fbb81f1621f6eb90d2511260e7dd5f Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Wed, 11 May 2022 14:34:02 +0900 Subject: [PATCH 010/166] Define json rpc method for account --- client/index.js | 1 + client/middleware.js | 7 +++++++ json_rpc/account.js | 11 ++++++----- test/integration/blockchain.test.js | 8 ++------ test/integration/node.test.js | 23 +++++++++++------------ 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/client/index.js b/client/index.js index 3859f153e..e23cda766 100755 --- a/client/index.js +++ b/client/index.js @@ -59,6 +59,7 @@ const jsonRpcApis = require('../json_rpc')( app.post( '/json-rpc', VersionUtil.validateVersion.bind({ minProtocolVersion, maxProtocolVersion }), + middleware.test(), jayson.server(jsonRpcApis).middleware() ); diff --git a/client/middleware.js b/client/middleware.js index 042ddbcf9..9d1c2581c 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -112,6 +112,13 @@ class Middleware { }) : this._emptyHandler(); } + test() { + return (req, res, next) => { + console.log(req) + return next(); + } + } + // NOTE(minsulee2): debugging purpose printAll() { console.log(this.getCorsOriginList()); diff --git a/json_rpc/account.js b/json_rpc/account.js index 923e861de..7c46d39d9 100644 --- a/json_rpc/account.js +++ b/json_rpc/account.js @@ -4,10 +4,11 @@ const { } = require('../common/constants'); const PathUtil = require('../common/path-util'); const JsonRpcUtil = require('./json-rpc-util'); +const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getAccountApis(node) { return { - ain_getAddress: function(args, done) { + [JSON_RPC_METHOD.AIN_GETADDRESS]: function(args, done) { const beginTime = Date.now(); const result = node.account ? node.account.address : null; const latency = Date.now() - beginTime; @@ -15,7 +16,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getBalance: function(args, done) { + [JSON_RPC_METHOD.AIN_GETBALANCE]: function(args, done) { const beginTime = Date.now(); const address = args.address; const balance = node.db.getValue(PathUtil.getAccountBalancePath(address)) || 0; @@ -24,7 +25,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: balance })); }, - ain_getNonce: function(args, done) { + [JSON_RPC_METHOD.AIN_GETNONCE]: function(args, done) { const beginTime = Date.now(); const result = node.getNonceForAddr(args.address, args.from === 'pending'); const latency = Date.now() - beginTime; @@ -32,7 +33,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getTimestamp: function(args, done) { + [JSON_RPC_METHOD.AIN_GETTIMESTAMP]: function(args, done) { const beginTime = Date.now(); const result = node.getTimestampForAddr(args.address, args.from === 'pending'); const latency = Date.now() - beginTime; @@ -40,7 +41,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getValidatorInfo: function(args, done) { + [JSON_RPC_METHOD.AIN_GETVALIDATORINFO]: function(args, done) { const beginTime = Date.now(); const addr = args.address; const isWhitelisted = node.db.getValue(PathUtil.getConsensusProposerWhitelistAddrPath(addr)) || false; diff --git a/test/integration/blockchain.test.js b/test/integration/blockchain.test.js index bbeb2fdbe..82efa14e9 100644 --- a/test/integration/blockchain.test.js +++ b/test/integration/blockchain.test.js @@ -16,11 +16,11 @@ const { waitUntilTxFinalized, waitForNewBlocks, waitUntilNetworkIsReady, - waitUntilNodeSyncs, parseOrLog, setUpApp } = require('../test-util'); const { Block } = require('../../blockchain/block'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); const PROJECT_ROOT = require('path').dirname(__filename) + '/../../'; const TRACKER_SERVER = PROJECT_ROOT + 'tracker-server/index.js'; @@ -58,14 +58,10 @@ const JSON_RPC_GET_BLOCKS = 'ain_getBlockList'; const JSON_RPC_GET_BLOCK_HEADERS = 'ain_getBlockHeadersList'; const JSON_RPC_GET_BLOCK_BY_HASH = 'ain_getBlockByHash'; const JSON_RPC_GET_BLOCK_BY_NUMBER = 'ain_getBlockByNumber'; -const JSON_RPC_GET_NONCE = 'ain_getNonce'; -const JSON_RPC_NET_SYNCING = 'net_syncing'; const SET_VALUE_ENDPOINT = '/set_value'; const GET_VALUE_ENDPOINT = '/get_value' -const BLOCKS_ENDPOINT = '/blocks' const GET_ADDR_ENDPOINT = '/get_address'; -const LAST_BLOCK_NUMBER_ENDPOINT = '/last_block_number' // Data options RANDOM_OPERATION = [ @@ -184,7 +180,7 @@ async function sendTransactions(sentOperations) { 'POST', serverList[serverIndex] + '/json-rpc', { json: { jsonrpc: '2.0', - method: 'ain_getNonce', + method: JSON_RPC_METHOD.AIN_GETNONCE, id: 0, params: { address, diff --git a/test/integration/node.test.js b/test/integration/node.test.js index d38291842..b4c97bf57 100644 --- a/test/integration/node.test.js +++ b/test/integration/node.test.js @@ -9,20 +9,18 @@ const jayson = require('jayson/promise'); const ainUtil = require('@ainblockchain/ain-util'); const { BlockchainConsts, BlockchainParams, NodeConfigs } = require('../../common/constants'); const CommonUtil = require('../../common/common-util'); -const PathUtil = require('../../common/path-util'); const { JsonRpcApiResultCode } = require('../../common/result-code'); const { verifyStateProof, } = require('../../db/state-util'); -const DB = require('../../db'); const { parseOrLog, setUpApp, waitUntilNetworkIsReady, waitUntilTxFinalized, - getBlockByNumber, - eraseEvalResMatched, + eraseEvalResMatched } = require('../test-util'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); const PROJECT_ROOT = require('path').dirname(__filename) + "/../../" const TRACKER_SERVER = PROJECT_ROOT + "tracker-server/index.js" @@ -1332,12 +1330,13 @@ describe('Blockchain Node', () => { }); }) - describe('ain_getAddress api', () => { + describe(`${JSON_RPC_METHOD.AIN_GETADDRESS} api`, () => { it('returns the correct node address', () => { const expAddr = '0x01A0980d2D4e418c7F27e1ef539d01A5b5E93204'; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request('ain_getAddress', { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) - .then(res => { + return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GETADDRESS, { + protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION + }).then(res => { expect(res.result.result).to.equal(expAddr); }); }); @@ -3130,7 +3129,7 @@ describe('Blockchain Node', () => { it('accepts a transaction with numbered nonce', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_getNonce', { + return client.request(JSON_RPC_METHOD.AIN_GETNONCE, { address: account.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3190,7 +3189,7 @@ describe('Blockchain Node', () => { it('accepts a transaction with account registration gas amount from nonce', () => { // NOTE: account2 does not have balance nor nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_getNonce', { + return client.request(JSON_RPC_METHOD.AIN_GETNONCE, { address: account2.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3250,7 +3249,7 @@ describe('Blockchain Node', () => { it('accepts a transaction without account registration gas amount from nonce', () => { // NOTE: account2 already has nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_getNonce', { + return client.request(JSON_RPC_METHOD.AIN_GETNONCE, { address: account2.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3514,7 +3513,7 @@ describe('Blockchain Node', () => { it('accepts a multi-set transaction with account registration gas amount from nonce', () => { // NOTE: account4 does not have balance nor nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_getNonce', { + return client.request(JSON_RPC_METHOD.AIN_GETNONCE, { address: account4.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3592,7 +3591,7 @@ describe('Blockchain Node', () => { it('accepts a multi-set transaction without account registration gas amount from nonce', () => { // NOTE: account4 already has nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_getNonce', { + return client.request(JSON_RPC_METHOD.AIN_GETNONCE, { address: account4.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION From 49ec45f091b2a2f120300258330d8f862433d678 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Wed, 11 May 2022 14:51:58 +0900 Subject: [PATCH 011/166] Update json rpc constants for admin --- json_rpc/account.js | 10 +++++----- json_rpc/admin.js | 19 ++++++++++++------- test/integration/blockchain.test.js | 2 +- test/integration/node.test.js | 14 +++++++------- .../addToDevClientApiIpWhitelist.js | 5 +++-- .../removeFromDevClientApiIpWhitelist.js | 5 +++-- 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/json_rpc/account.js b/json_rpc/account.js index 7c46d39d9..f21fb3ee8 100644 --- a/json_rpc/account.js +++ b/json_rpc/account.js @@ -8,7 +8,7 @@ const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getAccountApis(node) { return { - [JSON_RPC_METHOD.AIN_GETADDRESS]: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_ADDRESS]: function(args, done) { const beginTime = Date.now(); const result = node.account ? node.account.address : null; const latency = Date.now() - beginTime; @@ -16,7 +16,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GETBALANCE]: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_BALANCE]: function(args, done) { const beginTime = Date.now(); const address = args.address; const balance = node.db.getValue(PathUtil.getAccountBalancePath(address)) || 0; @@ -25,7 +25,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: balance })); }, - [JSON_RPC_METHOD.AIN_GETNONCE]: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_NONCE]: function(args, done) { const beginTime = Date.now(); const result = node.getNonceForAddr(args.address, args.from === 'pending'); const latency = Date.now() - beginTime; @@ -33,7 +33,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GETTIMESTAMP]: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_TIMESTAMP]: function(args, done) { const beginTime = Date.now(); const result = node.getTimestampForAddr(args.address, args.from === 'pending'); const latency = Date.now() - beginTime; @@ -41,7 +41,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GETVALIDATORINFO]: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_VALIDATOR_INFO]: function(args, done) { const beginTime = Date.now(); const addr = args.address; const isWhitelisted = node.db.getValue(PathUtil.getConsensusProposerWhitelistAddrPath(addr)) || false; diff --git a/json_rpc/admin.js b/json_rpc/admin.js index 90d27d113..cf243c171 100644 --- a/json_rpc/admin.js +++ b/json_rpc/admin.js @@ -8,25 +8,29 @@ const { const { JsonRpcApiResultCode } = require('../common/result-code'); const CommonUtil = require('../common/common-util'); const JsonRpcUtil = require('./json-rpc-util'); +const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getApiAccessApis(node) { return { - ain_getDevClientApiIpWhitelist: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { const beginTime = Date.now(); const verified = node.verifyNodeAccountSignature(args.message, args.signature); const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_GET, latency); - if (_.get(args.message, 'method') === 'ain_getDevClientApiIpWhitelist' && verified) { - done(null, JsonRpcUtil.addProtocolVersion({ result: NodeConfigs.DEV_CLIENT_API_IP_WHITELIST })); + if (_.get(args.message, 'method') === JSON_RPC_METHOD.AIN_GET_DEV_CLIENT_API_IP_WHITELIST && + verified) { + done(null, + JsonRpcUtil.addProtocolVersion({ result: NodeConfigs.DEV_CLIENT_API_IP_WHITELIST })); } else { done({ code: 403, message: 'Forbidden' }); } }, - ain_addToDevClientApiIpWhitelist: function(args, done) { + [JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { const beginTime = Date.now(); const verified = node.verifyNodeAccountSignature(args.message, args.signature); - if (_.get(args.message, 'method') !== 'ain_addToDevClientApiIpWhitelist' || !verified) { + if (_.get(args.message, 'method') !== JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST || + !verified) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done({ code: 403, message: 'Forbidden' }); @@ -81,10 +85,11 @@ module.exports = function getApiAccessApis(node) { } }, - ain_removeFromDevClientApiIpWhitelist: function(args, done) { + [JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { const beginTime = Date.now(); const verified = node.verifyNodeAccountSignature(args.message, args.signature); - if (_.get(args.message, 'method') !== 'ain_removeFromDevClientApiIpWhitelist' || !verified) { + if (_.get(args.message, 'method') !== JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST || + !verified) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done({ code: 403, message: 'Forbidden' }); diff --git a/test/integration/blockchain.test.js b/test/integration/blockchain.test.js index 82efa14e9..dfd4ac7a5 100644 --- a/test/integration/blockchain.test.js +++ b/test/integration/blockchain.test.js @@ -180,7 +180,7 @@ async function sendTransactions(sentOperations) { 'POST', serverList[serverIndex] + '/json-rpc', { json: { jsonrpc: '2.0', - method: JSON_RPC_METHOD.AIN_GETNONCE, + method: JSON_RPC_METHOD.AIN_GET_NONCE, id: 0, params: { address, diff --git a/test/integration/node.test.js b/test/integration/node.test.js index b4c97bf57..0ff3660dc 100644 --- a/test/integration/node.test.js +++ b/test/integration/node.test.js @@ -1330,11 +1330,11 @@ describe('Blockchain Node', () => { }); }) - describe(`${JSON_RPC_METHOD.AIN_GETADDRESS} api`, () => { + describe(`${JSON_RPC_METHOD.AIN_GET_ADDRESS} api`, () => { it('returns the correct node address', () => { const expAddr = '0x01A0980d2D4e418c7F27e1ef539d01A5b5E93204'; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GETADDRESS, { + return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_ADDRESS, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }).then(res => { expect(res.result.result).to.equal(expAddr); @@ -3129,7 +3129,7 @@ describe('Blockchain Node', () => { it('accepts a transaction with numbered nonce', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GETNONCE, { + return client.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address: account.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3189,7 +3189,7 @@ describe('Blockchain Node', () => { it('accepts a transaction with account registration gas amount from nonce', () => { // NOTE: account2 does not have balance nor nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GETNONCE, { + return client.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address: account2.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3249,7 +3249,7 @@ describe('Blockchain Node', () => { it('accepts a transaction without account registration gas amount from nonce', () => { // NOTE: account2 already has nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GETNONCE, { + return client.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address: account2.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3513,7 +3513,7 @@ describe('Blockchain Node', () => { it('accepts a multi-set transaction with account registration gas amount from nonce', () => { // NOTE: account4 does not have balance nor nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GETNONCE, { + return client.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address: account4.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3591,7 +3591,7 @@ describe('Blockchain Node', () => { it('accepts a multi-set transaction without account registration gas amount from nonce', () => { // NOTE: account4 already has nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GETNONCE, { + return client.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address: account4.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION diff --git a/tools/api-access/addToDevClientApiIpWhitelist.js b/tools/api-access/addToDevClientApiIpWhitelist.js index b87c9717a..de7c1293e 100644 --- a/tools/api-access/addToDevClientApiIpWhitelist.js +++ b/tools/api-access/addToDevClientApiIpWhitelist.js @@ -4,18 +4,19 @@ const ainUtil = require('@ainblockchain/ain-util'); const stringify = require('fast-json-stable-stringify'); const { BlockchainConsts } = require('../../common/constants'); const { getAccountPrivateKey } = require('./util'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); async function sendAddToDevClientApiIpWhitelistRequest(endpointUrl, privateKey, chainId, ip) { const message = { timestamp: Date.now(), - method: 'ain_addToDevClientApiIpWhitelist', + method: JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, ip, }; const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); return await axios.post( `${endpointUrl}/json-rpc`, { - method: 'ain_addToDevClientApiIpWhitelist', + method: JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, message, diff --git a/tools/api-access/removeFromDevClientApiIpWhitelist.js b/tools/api-access/removeFromDevClientApiIpWhitelist.js index 3edd2da33..3f44db1d0 100644 --- a/tools/api-access/removeFromDevClientApiIpWhitelist.js +++ b/tools/api-access/removeFromDevClientApiIpWhitelist.js @@ -4,18 +4,19 @@ const ainUtil = require('@ainblockchain/ain-util'); const stringify = require('fast-json-stable-stringify'); const { BlockchainConsts } = require('../../common/constants'); const { getAccountPrivateKey } = require('./util'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); async function sendRemoveFromToDevClientApiIpWhitelistRequest(endpointUrl, privateKey, chainId, ip) { const message = { timestamp: Date.now(), - method: 'ain_removeFromDevClientApiIpWhitelist', + method: JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST, ip, }; const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); return await axios.post( `${endpointUrl}/json-rpc`, { - method: 'ain_removeFromDevClientApiIpWhitelist', + method: JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, message, From e423e52feb3f86b91d0d2cbf401737a4c077341b Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Wed, 11 May 2022 14:58:23 +0900 Subject: [PATCH 012/166] Update json rpc method for app --- json_rpc/app.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/json_rpc/app.js b/json_rpc/app.js index c5219b8b8..c6635d897 100644 --- a/json_rpc/app.js +++ b/json_rpc/app.js @@ -3,10 +3,11 @@ const { trafficStatsManager, } = require('../common/constants'); const JsonRpcUtil = require('./json-rpc-util'); +const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getAppApis(node) { return { - ain_validateAppName: function(args, done) { + [JSON_RPC_METHOD.AIN_VALIDATE_APP_NAME]: function(args, done) { const beginTime = Date.now(); const result = node.validateAppName(args.app_name); const latency = Date.now() - beginTime; From 184b884dc7c4863422bcc4f2df4e3ee9c7df7077 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Wed, 11 May 2022 15:53:09 +0900 Subject: [PATCH 013/166] Update json rpc method for block --- json_rpc/block.js | 25 ++--- test/integration/blockchain.test.js | 111 ++++++++++++----------- test/integration/consensus.test.js | 4 +- tools/proposer-stats/getProposerStats.js | 3 +- 4 files changed, 73 insertions(+), 70 deletions(-) diff --git a/json_rpc/block.js b/json_rpc/block.js index 424f6a351..444e2500b 100644 --- a/json_rpc/block.js +++ b/json_rpc/block.js @@ -3,10 +3,11 @@ const { trafficStatsManager, } = require('../common/constants'); const JsonRpcUtil = require('./json-rpc-util'); +const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getBlockApis(node) { return { - ain_getBlockList: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_BLOCK_LIST]: function(args, done) { const beginTime = Date.now(); const blocks = node.bc.getBlockList(args.from, args.to); const latency = Date.now() - beginTime; @@ -14,7 +15,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: blocks })); }, - ain_getLastBlock: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_LAST_BLOCK]: function(args, done) { const beginTime = Date.now(); const result = node.bc.lastBlock(); const latency = Date.now() - beginTime; @@ -22,7 +23,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getLastBlockNumber: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_LAST_BLOCK_NUMBER]: function(args, done) { const beginTime = Date.now(); const result = node.bc.lastBlockNumber(); const latency = Date.now() - beginTime; @@ -30,7 +31,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getBlockHeadersList: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_BLOCK_HEADERS_LIST]: function(args, done) { const beginTime = Date.now(); const blocks = node.bc.getBlockList(args.from, args.to); const blockHeaders = []; @@ -42,7 +43,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: blockHeaders })); }, - ain_getBlockByHash: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_BLOCK_BY_HASH]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByHash(args.hash); if (block && !args.getFullTransactions) { @@ -53,7 +54,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: block })); }, - ain_getBlockByNumber: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByNumber(args.number); if (!block || args.getFullTransactions) { @@ -68,7 +69,7 @@ module.exports = function getBlockApis(node) { } }, - ain_getProposerByHash: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_PROPOSER_BY_HASH]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByHash(args.hash); const result = block ? block.proposer : null; @@ -77,7 +78,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getProposerByNumber: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_PROPOSER_BY_NUMBER]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByNumber(args.number); const result = block ? block.proposer : null; @@ -86,7 +87,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getValidatorsByNumber: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_VALIDATORS_BY_NUMBER]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByNumber(args.number); const result = block ? block.validators : null; @@ -95,7 +96,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getValidatorsByHash: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_VALIDATORS_BY_HASH]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByHash(args.hash); const result = block ? block.validators : null; @@ -104,7 +105,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getBlockTransactionCountByHash: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_BLOCK_TRANSACTION_COUNT_BY_HASH]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByHash(args.hash); const result = block ? block.transactions.length : null; @@ -113,7 +114,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getBlockTransactionCountByNumber: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_BLOCK_TRANSACTION_COUNT_BY_NUMBER]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByNumber(args.number); const result = block ? block.transactions.length : null; diff --git a/test/integration/blockchain.test.js b/test/integration/blockchain.test.js index dfd4ac7a5..3a8b1df1d 100644 --- a/test/integration/blockchain.test.js +++ b/test/integration/blockchain.test.js @@ -46,18 +46,12 @@ const ENV_VARIABLES = [ ]; // Server configurations -const trackerServer = 'http://localhost:5000'; const server1 = 'http://localhost:8081'; const server2 = 'http://localhost:8082'; const server3 = 'http://localhost:8083'; const serverList = [server1, server2, server3 ]; const JSON_RPC_ENDPOINT = '/json-rpc'; -const JSON_RPC_GET_LAST_BLOCK = 'ain_getLastBlock'; -const JSON_RPC_GET_BLOCKS = 'ain_getBlockList'; -const JSON_RPC_GET_BLOCK_HEADERS = 'ain_getBlockHeadersList'; -const JSON_RPC_GET_BLOCK_BY_HASH = 'ain_getBlockByHash'; -const JSON_RPC_GET_BLOCK_BY_NUMBER = 'ain_getBlockByNumber'; const SET_VALUE_ENDPOINT = '/set_value'; const GET_VALUE_ENDPOINT = '/get_value' @@ -234,7 +228,7 @@ describe('Blockchain Cluster', () => { await waitUntilNetworkIsReady(serverList); jsonRpcClient = jayson.client.http(server2 + JSON_RPC_ENDPOINT); promises.push(new Promise((resolve) => { - jsonRpcClient.request(JSON_RPC_GET_LAST_BLOCK, + jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_LAST_BLOCK, {protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { if (err) { resolve(); @@ -278,31 +272,33 @@ describe('Blockchain Cluster', () => { await sendTransactions(sentOperations); return new Promise((resolve) => { jayson.client.http(server1 + JSON_RPC_ENDPOINT) - .request(JSON_RPC_GET_BLOCKS, {protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, + .request(JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, + { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, function(err, response) { - if (err) throw err; - baseChain = response.result.result; - resolve(); - }); + if (err) throw err; + baseChain = response.result.result; + resolve(); + }); }).then(() => { return new Promise((resolve) => { - jayson.client.http(serverList[i] + JSON_RPC_ENDPOINT).request(JSON_RPC_GET_BLOCKS, - {protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, - function(err, response) { - if (err) throw err; - const newChain = response.result.result; - const diff = Math.abs(baseChain.length - newChain.length); - assert.isBelow(diff, MAX_CHAIN_LENGTH_DIFF); - while (baseChain.length !== newChain.length) { - if (baseChain.length > newChain.length) { - baseChain.pop(); - } else { - newChain.pop(); + jayson.client.http(serverList[i] + JSON_RPC_ENDPOINT) + .request(JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, + { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, + function (err, response) { + if (err) throw err; + const newChain = response.result.result; + const diff = Math.abs(baseChain.length - newChain.length); + assert.isBelow(diff, MAX_CHAIN_LENGTH_DIFF); + while (baseChain.length !== newChain.length) { + if (baseChain.length > newChain.length) { + baseChain.pop(); + } else { + newChain.pop(); + } } - } - assert.deepEqual(newChain.length, baseChain.length); - assert.deepEqual(newChain, baseChain); - resolve(); + assert.deepEqual(newChain.length, baseChain.length); + assert.deepEqual(newChain, baseChain); + resolve(); }); }); }); @@ -368,7 +364,7 @@ describe('Blockchain Cluster', () => { 'POST', serverList[i] + '/json-rpc', { json: { jsonrpc: '2.0', - method: JSON_RPC_GET_BLOCKS, + method: JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, id: 0, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -415,9 +411,13 @@ describe('Blockchain Cluster', () => { for (let i = 0; i < serverList.length; i++) { await sendTransactions(sentOperations); const blocks = parseOrLog(syncRequest('POST', serverList[i] + '/json-rpc', - {json: {jsonrpc: '2.0', method: JSON_RPC_GET_BLOCKS, id: 0, - params: {protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}}}) - .body.toString('utf-8')).result.result; + { + json: { + jsonrpc: '2.0', method: JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, id: 0, + params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION } + } + }) + .body.toString('utf-8')).result.result; const len = blocks.length; for (let j = 0; j < len; j++) { const block = blocks[j]; @@ -505,10 +505,10 @@ describe('Blockchain Cluster', () => { }); describe('Block API', () => { - it('ain_getBlockHeadersList', async () => { + it(JSON_RPC_METHOD.AIN_GET_BLOCK_HEADERS_LIST, async () => { await sendTransactions(sentOperations); return new Promise((resolve) => { - jsonRpcClient.request(JSON_RPC_GET_BLOCK_HEADERS, + jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_BLOCK_HEADERS_LIST, {from: 2, to: 4, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { if (err) throw err; @@ -521,17 +521,17 @@ describe('Blockchain Cluster', () => { }) }); - it('ain_getBlockByHash and ain_getBlockByNumber', async () => { + it(`${JSON_RPC_METHOD.AIN_GET_BLOCK_BY_HASH} and ${JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER}`, async () => { await sendTransactions(sentOperations); return new Promise((resolve) => { - jsonRpcClient.request(JSON_RPC_GET_BLOCK_BY_NUMBER, + jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, {number: 2, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { if (err) throw err; resolve(response.result.result); }); }).then((resultByNumber) => { return new Promise((resolve) => { - jsonRpcClient.request(JSON_RPC_GET_BLOCK_BY_HASH, + jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_BLOCK_BY_HASH, {hash: resultByNumber.hash, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { if (err) throw err; @@ -617,25 +617,26 @@ describe('Blockchain Cluster', () => { describe('Gas fee', () => { it('collected gas cost matches the gas_cost_total in the block', () => { return new Promise((resolve) => { - jayson.client.http(serverList[1] + JSON_RPC_ENDPOINT).request - (JSON_RPC_GET_BLOCKS, {protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { - if (err) throw err; - const chain = response.result.result; - for (const block of chain) { - if (block.number > 0) { - // Amount specified in block - const gasCostTotal = block.gas_cost_total; - // Amount actually collected & distributed. Write rule prevents writing a gas_cost_total - // that is different from the value at /service_accounts/gas_fee/gas_fee/${block.number}/balance. - const collectedGas = parseOrLog(syncRequest( - 'GET', server1 + GET_VALUE_ENDPOINT + `?ref=/consensus/number/${block.number}/propose/gas_cost_total`) - .body.toString('utf-8')).result; - assert.deepEqual(gasCostTotal, collectedGas); + jayson.client.http(serverList[1] + JSON_RPC_ENDPOINT) + .request(JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, + { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, + function (err, response) { + if (err) throw err; + const chain = response.result.result; + for (const block of chain) { + if (block.number > 0) { + // Amount specified in block + const gasCostTotal = block.gas_cost_total; + // Amount actually collected & distributed. Write rule prevents writing a gas_cost_total + // that is different from the value at /service_accounts/gas_fee/gas_fee/${block.number}/balance. + const collectedGas = parseOrLog(syncRequest( + 'GET', server1 + GET_VALUE_ENDPOINT + `?ref=/consensus/number/${block.number}/propose/gas_cost_total`) + .body.toString('utf-8')).result; + assert.deepEqual(gasCostTotal, collectedGas); + } } - } - resolve(); - } - ); + resolve(); + }); }); }); }); diff --git a/test/integration/consensus.test.js b/test/integration/consensus.test.js index 64064b4e9..d3db8cb52 100644 --- a/test/integration/consensus.test.js +++ b/test/integration/consensus.test.js @@ -29,6 +29,7 @@ const { const { Block } = require('../../blockchain/block'); const Functions = require('../../db/functions'); const ConsensusUtil = require('../../consensus/consensus-util'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); const PROJECT_ROOT = require('path').dirname(__filename) + '/../../'; const TRACKER_SERVER = PROJECT_ROOT + 'tracker-server/index.js'; @@ -72,7 +73,6 @@ const server5 = 'http://localhost:8085'; const serverList = [server1, server2, server3, server4, server5]; const JSON_RPC_ENDPOINT = '/json-rpc'; -const JSON_RPC_GET_LAST_BLOCK = 'ain_getLastBlock'; class Process { constructor(application, envVariables) { @@ -142,7 +142,7 @@ describe('Consensus', () => { await waitUntilNetworkIsReady(serverList); jsonRpcClient = jayson.client.http(server2 + JSON_RPC_ENDPOINT); promises.push(new Promise((resolve) => { - jsonRpcClient.request(JSON_RPC_GET_LAST_BLOCK, + jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_LAST_BLOCK, {protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { if (err) { resolve(); diff --git a/tools/proposer-stats/getProposerStats.js b/tools/proposer-stats/getProposerStats.js index 36e84594a..f0600b9a9 100644 --- a/tools/proposer-stats/getProposerStats.js +++ b/tools/proposer-stats/getProposerStats.js @@ -6,13 +6,14 @@ const axios = require('axios'); const _ = require('lodash'); const CommonUtil = require('../../common/common-util'); const { BlockchainConsts, NodeConfigs } = require('../../common/constants'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); async function getBlockList(from, to, endpointUrl) { console.log(`getting block list from ${from} to ${to}`); return await axios.post( `${endpointUrl}/json-rpc`, { - method: 'ain_getBlockList', + method: JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, params: { from, to, From ff0cb20f7f4e8263cac58bcfda2723c54c23a043 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Wed, 11 May 2022 16:20:44 +0900 Subject: [PATCH 014/166] Update json rpc method for database --- json_rpc/constants.js | 39 +++++++++++++++++ json_rpc/database.js | 21 +++++----- p2p/server.js | 5 ++- test/integration/node.test.js | 54 ++++++++++++------------ test/integration/sharding.test.js | 65 +++++++++++++++-------------- tools/checkin/sendCloseCheckinTx.js | 3 +- 6 files changed, 115 insertions(+), 72 deletions(-) create mode 100644 json_rpc/constants.js diff --git a/json_rpc/constants.js b/json_rpc/constants.js new file mode 100644 index 000000000..7eb10e48c --- /dev/null +++ b/json_rpc/constants.js @@ -0,0 +1,39 @@ +const JSON_RPC_METHOD = { + // GET + AIN_GET_ADDRESS: 'ain_getAddress', + AIN_GET_BALANCE: 'ain_getBalance', + AIN_GET_NONCE: 'ain_getNonce', + AIN_GET_TIMESTAMP: 'ain_getTimestamp', + AIN_GET_VALIDATOR_INFO: 'ain_getValidatorInfo', + AIN_GET_DEV_CLIENT_API_IP_WHITELIST: 'ain_getDevClientApiIpWhitelist', + AIN_VALIDATE_APP_NAME: 'ain_validateAppName', + AIN_GET_BLOCK_LIST: 'ain_getBlockList', + AIN_GET_LAST_BLOCK: 'ain_getLastBlock', + AIN_GET_LAST_BLOCK_NUMBER: 'ain_getLastBlockNumber', + AIN_GET_BLOCK_HEADERS_LIST: 'ain_getBlockHeadersList', + AIN_GET_BLOCK_BY_HASH: 'ain_getBlockByHash', + AIN_GET_BLOCK_BY_NUMBER: 'ain_getBlockByNumber', + AIN_GET_PROPOSER_BY_HASH: 'ain_getProposerByHash', + AIN_GET_PROPOSER_BY_NUMBER: 'ain_getProposerByNumber', + AIN_GET_VALIDATORS_BY_NUMBER: 'ain_getValidatorsByNumber', + AIN_GET_VALIDATORS_BY_HASH: 'ain_getValidatorsByHash', + AIN_GET_BLOCK_TRANSACTION_COUNT_BY_HASH: 'ain_getBlockTransactionCountByHash', + AIN_GET_BLOCK_TRANSACTION_COUNT_BY_NUMBER: 'ain_getBlockTransactionCountByNumber', + AIN_GET: 'ain_get', + AIN_MATCH_FUNCTION: 'ain_matchFunction', + AIN_MATCH_RULE: 'ain_matchRule', + AIN_MATCH_OWNER: 'ain_matchOwner', + AIN_EVAL_RULE: 'ain_evalRule', + AIN_EVAL_OWNER: 'ain_evalOwner', + AIN_GET_STATE_PROOF: 'ain_getStateProof', + AIN_GET_PROOF_HASH: 'ain_getProofHash', + AIN_GET_STATE_INFO: 'ain_getStateInfo', + AIN_GET_STATE_USAGE: 'ain_getStateUsage', + // SET + AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', + AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', +} + +module.exports = { + JSON_RPC_METHOD +}; diff --git a/json_rpc/database.js b/json_rpc/database.js index 7605adcca..c586618bb 100644 --- a/json_rpc/database.js +++ b/json_rpc/database.js @@ -6,10 +6,11 @@ const { const { JsonRpcApiResultCode } = require('../common/result-code'); const CommonUtil = require('../common/common-util'); const JsonRpcUtil = require('./json-rpc-util'); +const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getDatabaseApis(node) { return { - ain_get: function(args, done) { + [JSON_RPC_METHOD.AIN_GET]: function(args, done) { const beginTime = Date.now(); let result; let latency; @@ -56,7 +57,7 @@ module.exports = function getDatabaseApis(node) { } }, - ain_matchFunction: function(args, done) { + [JSON_RPC_METHOD.AIN_MATCH_FUNCTION]: function(args, done) { const beginTime = Date.now(); const result = node.db.matchFunction(args.ref, CommonUtil.toMatchOrEvalOptions(args, true)); @@ -65,7 +66,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_matchRule: function(args, done) { + [JSON_RPC_METHOD.AIN_MATCH_RULE]: function(args, done) { const beginTime = Date.now(); const result = node.db.matchRule(args.ref, CommonUtil.toMatchOrEvalOptions(args, true)); const latency = Date.now() - beginTime; @@ -73,7 +74,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_matchOwner: function(args, done) { + [JSON_RPC_METHOD.AIN_MATCH_OWNER]: function(args, done) { const beginTime = Date.now(); const result = node.db.matchOwner(args.ref, CommonUtil.toMatchOrEvalOptions(args, true)); const latency = Date.now() - beginTime; @@ -81,7 +82,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_evalRule: function(args, done) { + [JSON_RPC_METHOD.AIN_EVAL_RULE]: function(args, done) { const beginTime = Date.now(); const auth = {}; if (args.address) { @@ -98,7 +99,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_evalOwner: function(args, done) { + [JSON_RPC_METHOD.AIN_EVAL_OWNER]: function(args, done) { const beginTime = Date.now(); const auth = {}; if (args.address) { @@ -114,7 +115,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getStateProof: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_STATE_PROOF]: function(args, done) { const beginTime = Date.now(); const result = node.db.getStateProof(args.ref); const latency = Date.now() - beginTime; @@ -122,7 +123,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getProofHash: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_PROOF_HASH]: function(args, done) { const beginTime = Date.now(); const result = node.db.getProofHash(args.ref); const latency = Date.now() - beginTime; @@ -130,7 +131,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getStateInfo: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_STATE_INFO]: function(args, done) { const beginTime = Date.now(); const result = node.db.getStateInfo(args.ref); const latency = Date.now() - beginTime; @@ -138,7 +139,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getStateUsage: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_STATE_USAGE]: function(args, done) { const beginTime = Date.now(); const result = node.getStateUsageWithStakingInfo(args.app_name); const latency = Date.now() - beginTime; diff --git a/p2p/server.js b/p2p/server.js index 47054bee2..5f208b4db 100644 --- a/p2p/server.js +++ b/p2p/server.js @@ -41,6 +41,7 @@ const { } = require('../common/network-util'); const P2pUtil = require('./p2p-util'); const PathUtil = require('../common/path-util'); +const { JSON_RPC_METHOD } = require('../json_rpc/constants'); const DISK_USAGE_PATH = os.platform() === 'win32' ? 'c:' : '/'; @@ -960,7 +961,7 @@ class P2pServer { static async getLastReportedBlockNumber(parentChainEndpoint, shardingPath) { const resp = await sendGetRequest( parentChainEndpoint, - 'ain_get', + JSON_RPC_METHOD.AIN_GET, { type: ReadDbOperations.GET_VALUE, ref: `${shardingPath}/${PredefinedDbPaths.DOT_SHARD}/${ShardingProperties.LATEST_BLOCK_NUMBER}` @@ -970,7 +971,7 @@ class P2pServer { } static async getShardingAppConfig(parentChainEndpoint, appName) { - const resp = await sendGetRequest(parentChainEndpoint, 'ain_get', { + const resp = await sendGetRequest(parentChainEndpoint, JSON_RPC_METHOD.AIN_GET, { type: ReadDbOperations.GET_VALUE, ref: PathUtil.getManageAppConfigPath(appName) }); diff --git a/test/integration/node.test.js b/test/integration/node.test.js index 0ff3660dc..ca37ced99 100644 --- a/test/integration/node.test.js +++ b/test/integration/node.test.js @@ -825,11 +825,11 @@ describe('Blockchain Node', () => { }); }); - describe('ain_get api', () => { + describe(`${JSON_RPC_METHOD.AIN_GET} api`, () => { it('returns the correct value', () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request('ain_get', { + return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/test/test_value/some/path" @@ -841,7 +841,7 @@ describe('Blockchain Node', () => { it('returns error when invalid op_list is given', () => { const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request('ain_get', { + return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET', op_list: null @@ -868,7 +868,7 @@ describe('Blockchain Node', () => { console.error(`Failed to check finalization of tx.`); } const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request('ain_get', { + return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/test/test_value/some/path", @@ -894,7 +894,7 @@ describe('Blockchain Node', () => { console.error(`Failed to check finalization of tx.`); } const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request('ain_get', { + return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/test/test_value/some/path", @@ -908,11 +908,11 @@ describe('Blockchain Node', () => { }); }); - describe('ain_matchFunction api', () => { + describe(`${JSON_RPC_METHOD.AIN_MATCH_FUNCTION} api`, () => { it('returns correct value', () => { const ref = "/apps/test/test_function/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchFunction', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_FUNCTION, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -936,11 +936,11 @@ describe('Blockchain Node', () => { }) }) - describe('ain_matchRule api', () => { + describe(`${JSON_RPC_METHOD.AIN_MATCH_RULE} api`, () => { it('returns correct value (write)', () => { const ref = "/apps/test/test_rule/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchRule', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) .then(res => { assert.deepEqual(res.result.result, { "write": { @@ -975,7 +975,7 @@ describe('Blockchain Node', () => { it('returns correct value (state)', () => { const ref = "/apps/test/test_rule/state"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchRule', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) .then(res => { assert.deepEqual(res.result.result, { "write": { @@ -1023,7 +1023,7 @@ describe('Blockchain Node', () => { it('returns correct value (state & write)', () => { const ref = "/apps/test/test_rule/state/and/write"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchRule', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) .then(res => { assert.deepEqual(res.result.result, { "write": { @@ -1062,11 +1062,11 @@ describe('Blockchain Node', () => { }) }) - describe('ain_matchOwner api', () => { + describe(`${JSON_RPC_METHOD.AIN_MATCH_OWNER} api`, () => { it('returns correct value', () => { const ref = "/apps/test/test_owner/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchOwner', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -1091,13 +1091,13 @@ describe('Blockchain Node', () => { }) }) - describe('ain_evalRule api', () => { + describe(`${JSON_RPC_METHOD.AIN_EVAL_RULE} api`, () => { it('returns true', () => { const ref = "/apps/test/test_rule/some/path"; const value = "value"; const address = "abcd"; const request = { ref, value, address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_evalRule', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_RULE, request) .then(res => { assert.deepEqual(eraseEvalResMatched(res.result.result), { "code": 0, @@ -1111,7 +1111,7 @@ describe('Blockchain Node', () => { const value = "value"; const address = "efgh"; const request = { ref, value, address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_evalRule', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_RULE, request) .then(res => { res.result.result.message = 'erased'; assert.deepEqual(eraseEvalResMatched(res.result.result), { @@ -1123,13 +1123,13 @@ describe('Blockchain Node', () => { }) }) - describe('ain_evalOwner api', () => { + describe(`${JSON_RPC_METHOD.AIN_EVAL_OWNER} api`, () => { it('returns correct value', () => { const ref = "/apps/test/test_owner/some/path"; const address = "abcd"; const permission = "write_owner"; const request = { ref, permission, address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_evalOwner', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "code": 0, @@ -1167,11 +1167,11 @@ describe('Blockchain Node', () => { }) }) - describe('ain_getStateProof api', () => { + describe(`${JSON_RPC_METHOD.AIN_GET_STATE_PROOF} api`, () => { it('returns correct value', () => { const ref = '/values/blockchain_params/token/symbol'; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_getStateProof', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_GET_STATE_PROOF, request) .then(res => { expect(res.result.result['#state_ph']).to.not.equal(null); const verifResult = verifyStateProof(res.result.result); @@ -1187,22 +1187,22 @@ describe('Blockchain Node', () => { }) }) - describe('ain_getProofHash api', () => { + describe(`${JSON_RPC_METHOD.AIN_GET_PROOF_HASH} api`, () => { it('returns correct value', () => { const ref = '/values/blockchain_params/token/symbol'; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_getProofHash', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_GET_PROOF_HASH, request) .then(res => { expect(res.result.result).to.not.equal(null); }) }) }) - describe('ain_getStateInfo api', () => { + describe(`${JSON_RPC_METHOD.AIN_GET_STATE_INFO} api`, () => { it('returns correct value', () => { const ref = '/values/apps/test/test_state_info/some/path'; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_getStateInfo', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_GET_STATE_INFO, request) .then(res => { const stateInfo = res.result.result; // Erase some properties for stable comparison. @@ -1221,10 +1221,10 @@ describe('Blockchain Node', () => { }) }) - describe('ain_getStateUsage api', () => { + describe(`${JSON_RPC_METHOD.AIN_GET_STATE_USAGE} api`, () => { it('with existing app name', () => { const request = { app_name: 'test', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_getStateUsage', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_GET_STATE_USAGE, request) .then(res => { const stateUsage = res.result.result; assert.deepEqual(stateUsage, { @@ -1249,7 +1249,7 @@ describe('Blockchain Node', () => { it('with non-existing app name', () => { const request = { app_name: 'app_non_existing', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_getStateUsage', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_GET_STATE_USAGE, request) .then(res => { const stateUsage = res.result.result; assert.deepEqual(stateUsage, { diff --git a/test/integration/sharding.test.js b/test/integration/sharding.test.js index 26843e9cb..c06af7b16 100644 --- a/test/integration/sharding.test.js +++ b/test/integration/sharding.test.js @@ -34,6 +34,7 @@ const { setUpApp, eraseEvalResMatched, } = require('../test-util'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); const PROJECT_ROOT = require('path').dirname(__filename) + "/../../" const TRACKER_SERVER = PROJECT_ROOT + "tracker-server/index.js" @@ -1247,11 +1248,11 @@ describe('Sharding', () => { }) }) - describe('ain_get api', () => { - it('ain_get with is_global = false', () => { + describe(`${JSON_RPC_METHOD.AIN_GET}`, () => { + it(`${JSON_RPC_METHOD.AIN_GET} with is_global = false`, () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request('ain_get', { + return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/test/test_value/some/path" @@ -1261,10 +1262,10 @@ describe('Sharding', () => { }); }); - it('ain_get with is_global = false (explicit)', () => { + it(`${JSON_RPC_METHOD.AIN_GET} with is_global = false (explicit)`, () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request('ain_get', { + return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/test/test_value/some/path", @@ -1275,10 +1276,10 @@ describe('Sharding', () => { }); }); - it('ain_get with is_global = true', () => { + it(`${JSON_RPC_METHOD.AIN_GET} with is_global = true`, () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request('ain_get', { + return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/afan/apps/test/test_value/some/path", @@ -1290,11 +1291,11 @@ describe('Sharding', () => { }); }) - describe('ain_matchFunction api', () => { - it('ain_matchFunction with is_global = false', () => { + describe(`${JSON_RPC_METHOD.AIN_MATCH_FUNCTION} api`, () => { + it(`${JSON_RPC_METHOD.AIN_MATCH_FUNCTION} with is_global = false`, () => { const ref = "/apps/test/test_function/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchFunction', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_FUNCTION, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -1317,10 +1318,10 @@ describe('Sharding', () => { }) }) - it('ain_matchFunction with is_global = true', () => { + it(`${JSON_RPC_METHOD.AIN_MATCH_FUNCTION} with is_global = true`, () => { const ref = "/apps/afan/apps/test/test_function/some/path"; const request = { ref, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchFunction', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_FUNCTION, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -1344,11 +1345,11 @@ describe('Sharding', () => { }) }) - describe('ain_matchRule api', () => { - it('ain_matchRule with is_global = false', () => { + describe(`${JSON_RPC_METHOD.AIN_MATCH_RULE} api`, () => { + it(`${JSON_RPC_METHOD.AIN_MATCH_RULE} with is_global = false`, () => { const ref = "/apps/test/test_rule/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchRule', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) .then(res => { assert.deepEqual(res.result.result, { "write": { @@ -1380,10 +1381,10 @@ describe('Sharding', () => { }) }) - it('ain_matchRule with is_global = true', () => { + it(`${JSON_RPC_METHOD.AIN_MATCH_RULE} with is_global = true`, () => { const ref = "/apps/afan/apps/test/test_rule/some/path"; const request = { ref, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchRule', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) .then(res => { assert.deepEqual(res.result.result, { "write": { @@ -1416,11 +1417,11 @@ describe('Sharding', () => { }) }) - describe('ain_matchOwner api', () => { - it('ain_matchOwner with is_global = false', () => { + describe(`${JSON_RPC_METHOD.AIN_MATCH_OWNER} api`, () => { + it(`${JSON_RPC_METHOD.AIN_MATCH_OWNER} with is_global = false`, () => { const ref = "/apps/test/test_owner/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchOwner', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -1444,10 +1445,10 @@ describe('Sharding', () => { }) }) - it('ain_matchOwner with is_global = true', () => { + it(`${JSON_RPC_METHOD.AIN_MATCH_OWNER} with is_global = true`, () => { const ref = "/apps/afan/apps/test/test_owner/some/path"; const request = { ref, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_matchOwner', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -1472,13 +1473,13 @@ describe('Sharding', () => { }) }) - describe('ain_evalRule api', () => { - it('ain_evalRule with is_global = false', () => { + describe(`${JSON_RPC_METHOD.AIN_EVAL_RULE} api`, () => { + it(`${JSON_RPC_METHOD.AIN_EVAL_RULE} with is_global = false`, () => { const ref = "/apps/test/test_rule/some/path"; const value = "value"; const address = "abcd"; const request = { ref, value, address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_evalRule', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_RULE, request) .then(res => { assert.deepEqual(eraseEvalResMatched(res.result.result), { "code": 0, @@ -1487,13 +1488,13 @@ describe('Sharding', () => { }) }) - it('ain_evalRule with is_global = true', () => { + it(`${JSON_RPC_METHOD.AIN_EVAL_RULE} with is_global = true`, () => { const ref = "/apps/afan/apps/test/test_rule/some/path"; const value = "value"; const address = "abcd"; const request = { ref, value, address, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_evalRule', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_RULE, request) .then(res => { assert.deepEqual(eraseEvalResMatched(res.result.result), { "code": 0, @@ -1503,13 +1504,13 @@ describe('Sharding', () => { }) }) - describe('ain_evalOwner api', () => { - it('ain_evalOwner with is_global = false', () => { + describe(`${JSON_RPC_METHOD.AIN_EVAL_OWNER} api`, () => { + it(`${JSON_RPC_METHOD.AIN_EVAL_OWNER} with is_global = false`, () => { const ref = "/apps/test/test_owner/some/path"; const address = "abcd"; const permission = "write_owner"; const request = { ref, permission, address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_evalOwner', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "code": 0, @@ -1546,13 +1547,13 @@ describe('Sharding', () => { }) }) - it('ain_evalOwner with is_global = true', () => { + it(`${JSON_RPC_METHOD.AIN_EVAL_OWNER} with is_global = true`, () => { const ref = "/apps/afan/apps/test/test_owner/some/path"; const address = "abcd"; const permission = "write_owner"; const request = { ref, permission, address, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request('ain_evalOwner', request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "code": 0, diff --git a/tools/checkin/sendCloseCheckinTx.js b/tools/checkin/sendCloseCheckinTx.js index 54e995adb..a78020240 100644 --- a/tools/checkin/sendCloseCheckinTx.js +++ b/tools/checkin/sendCloseCheckinTx.js @@ -2,10 +2,11 @@ const path = require('path'); const _ = require('lodash'); const { signAndSendTx, confirmTransaction } = require('../util'); const { sendGetRequest } = require('../../common/network-util'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); let config = {}; async function buildCloseCheckinTxBody(fromAddr, checkinId, failed = false) { - const request = (await sendGetRequest(`${config.endpointUrl}/json-rpc`, 'ain_get', { + const request = (await sendGetRequest(`${config.endpointUrl}/json-rpc`, JSON_RPC_METHOD.AIN_GET, { type: 'GET_VALUE', ref: `/checkin/requests/ETH/3/0xB16c0C80a81f73204d454426fC413CAe455525A7/${fromAddr}/${checkinId}`, })).data.result.result; From 944b9bf71240fbc1bc957bf453cd02937156f057 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Wed, 11 May 2022 18:50:28 +0900 Subject: [PATCH 015/166] Update json rpc route for event handler --- json_rpc/constants.js | 3 +++ json_rpc/event-handler.js | 7 ++++--- test/integration/event_handler.test.js | 7 ++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/json_rpc/constants.js b/json_rpc/constants.js index 7eb10e48c..1965bfef2 100644 --- a/json_rpc/constants.js +++ b/json_rpc/constants.js @@ -29,6 +29,9 @@ const JSON_RPC_METHOD = { AIN_GET_PROOF_HASH: 'ain_getProofHash', AIN_GET_STATE_INFO: 'ain_getStateInfo', AIN_GET_STATE_USAGE: 'ain_getStateUsage', + NET_GET_EVENT_HANDLER_NETWORK_INFO: 'net_getEventHandlerNetworkInfo', + AIN_GET_EVENT_HANDLER_FILTER_INFO: 'ain_getEventHandlerFilterInfo', + AIN_GET_EVENT_HANDLER_CHANNEL_INFO: 'ain_getEventHandlerChannelInfo', // SET AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', diff --git a/json_rpc/event-handler.js b/json_rpc/event-handler.js index 0b6d31618..4d6817783 100644 --- a/json_rpc/event-handler.js +++ b/json_rpc/event-handler.js @@ -3,11 +3,12 @@ const { trafficStatsManager, } = require('../common/constants'); const JsonRpcUtil = require('./json-rpc-util'); +const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getEventHandlerApis(eventHandler) { return { // NOTE(cshcomcom): Async function doesn't need a done parameter. (Ref: https://www.npmjs.com/package/jayson#promises) - net_getEventHandlerNetworkInfo: async function(args) { + [JSON_RPC_METHOD.NET_GET_EVENT_HANDLER_NETWORK_INFO]: async function(args) { const beginTime = Date.now(); const result = await eventHandler.eventChannelManager.getNetworkInfo(); const latency = Date.now() - beginTime; @@ -15,7 +16,7 @@ module.exports = function getEventHandlerApis(eventHandler) { return JsonRpcUtil.addProtocolVersion({ result }); }, - ain_getEventHandlerFilterInfo: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_EVENT_HANDLER_FILTER_INFO]: function(args, done) { const beginTime = Date.now(); const result = eventHandler.getFilterInfo(); const latency = Date.now() - beginTime; @@ -23,7 +24,7 @@ module.exports = function getEventHandlerApis(eventHandler) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getEventHandlerChannelInfo: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_EVENT_HANDLER_CHANNEL_INFO]: function(args, done) { const beginTime = Date.now(); const result = eventHandler.eventChannelManager.getChannelInfo(); const latency = Date.now() - beginTime; diff --git a/test/integration/event_handler.test.js b/test/integration/event_handler.test.js index 7acb92545..66d09a3a7 100644 --- a/test/integration/event_handler.test.js +++ b/test/integration/event_handler.test.js @@ -18,6 +18,7 @@ const { setUpApp, waitUntilNetworkIsReady, } = require('../test-util'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); const SET_VALUE_ENDPOINT = '/set_value'; const PROJECT_ROOT = require('path').dirname(__filename) + '/../../'; const TRACKER_SERVER = PROJECT_ROOT + 'tracker-server/index.js'; @@ -69,7 +70,7 @@ function startServer(application, serverName, envVars, stdioInherit = false) { function getEventHandlerNetworkInfo() { return _.get(parseOrLog(syncRequest('POST', serverList[EVENT_HANDLER_NODE_INDEX] + '/json-rpc', { json: { - jsonrpc: '2.0', method: 'net_getEventHandlerNetworkInfo', id: 0, + jsonrpc: '2.0', method: JSON_RPC_METHOD.NET_GET_EVENT_HANDLER_NETWORK_INFO, id: 0, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, }, }).body.toString('utf-8')), 'result.result'); @@ -78,7 +79,7 @@ function getEventHandlerNetworkInfo() { function getEventHandlerChannelInfo() { return _.get(parseOrLog(syncRequest('POST', serverList[EVENT_HANDLER_NODE_INDEX] + '/json-rpc', { json: { - jsonrpc: '2.0', method: 'ain_getEventHandlerChannelInfo', id: 0, + jsonrpc: '2.0', method: JSON_RPC_METHOD.AIN_GET_EVENT_HANDLER_CHANNEL_INFO, id: 0, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, }, }).body.toString('utf-8')), 'result.result'); @@ -87,7 +88,7 @@ function getEventHandlerChannelInfo() { function getEventHandlerFilterInfo() { return _.get(parseOrLog(syncRequest('POST', serverList[EVENT_HANDLER_NODE_INDEX] + '/json-rpc', { json: { - jsonrpc: '2.0', method: 'ain_getEventHandlerFilterInfo', id: 0, + jsonrpc: '2.0', method: JSON_RPC_METHOD.AIN_GET_EVENT_HANDLER_FILTER_INFO, id: 0, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, }, }).body.toString('utf-8')), 'result.result'); From 42b67afcf76c6d6b04efd3e4cb0cd3182dfbac5b Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 12 May 2022 10:10:43 +0900 Subject: [PATCH 016/166] Update json rpc method for injection --- inject_account_gcp.js | 9 +++++---- json_rpc/constants.js | 4 ++++ json_rpc/injection.js | 9 +++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/inject_account_gcp.js b/inject_account_gcp.js index c607ffaba..fdccd1b7d 100644 --- a/inject_account_gcp.js +++ b/inject_account_gcp.js @@ -5,12 +5,13 @@ const ainUtil = require('@ainblockchain/ain-util'); const { BlockchainConsts } = require('./common/constants'); const { sleep } = require('./common/common-util'); const prompt = require('prompt'); +const { JSON_RPC_METHOD } = require('./json_rpc/constants'); async function sendGetBootstrapPubKeyRequest(endpointUrl) { return await axios.post( `${endpointUrl}/json-rpc`, { - method: 'ain_getBootstrapPubKey', + method: JSON_RPC_METHOD.AIN_GET_BOOTSTRAP_PUB_KEY, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, }, @@ -103,13 +104,13 @@ async function injectAccount(endpointUrl, accountInjectionOption) { const params = {}; switch (accountInjectionOption) { case '--private-key': - method = 'ain_injectAccountFromPrivateKey'; + method = JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY; Object.assign(params, { encryptedPrivateKey: await ainUtil.encryptWithPublicKey(bootstrapPubKey, input.privateKey) }) break; case '--keystore': - method = 'ain_injectAccountFromKeystore'; + method = JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_KEYSTORE; const keystore = JSON.stringify(JSON.parse(fs.readFileSync(input.keystorePath))); Object.assign(params, { encryptedKeystore: await ainUtil.encryptWithPublicKey(bootstrapPubKey, keystore) @@ -119,7 +120,7 @@ async function injectAccount(endpointUrl, accountInjectionOption) { }) break; case '--mnemonic': - method = 'ain_injectAccountFromHDWallet'; + method = JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_HD_WALLET; Object.assign(params, { encryptedMnemonic: await ainUtil.encryptWithPublicKey(bootstrapPubKey, input.mnemonic), index: input.index diff --git a/json_rpc/constants.js b/json_rpc/constants.js index 1965bfef2..2abca017a 100644 --- a/json_rpc/constants.js +++ b/json_rpc/constants.js @@ -32,9 +32,13 @@ const JSON_RPC_METHOD = { NET_GET_EVENT_HANDLER_NETWORK_INFO: 'net_getEventHandlerNetworkInfo', AIN_GET_EVENT_HANDLER_FILTER_INFO: 'ain_getEventHandlerFilterInfo', AIN_GET_EVENT_HANDLER_CHANNEL_INFO: 'ain_getEventHandlerChannelInfo', + AIN_GET_BOOTSTRAP_PUB_KEY: 'ain_getBootstrapPubKey', // SET AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', + AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: 'ain_injectAccountFromPrivateKey', + AIN_INJECT_ACCOUNT_FROM_KEYSTORE: 'ain_injectAccountFromKeystore', + AIN_INJECT_ACCOUNT_FROM_HD_WALLET: 'ain_injectAccountFromHDWallet', } module.exports = { diff --git a/json_rpc/injection.js b/json_rpc/injection.js index 61d6c4381..59c20ecf0 100644 --- a/json_rpc/injection.js +++ b/json_rpc/injection.js @@ -3,10 +3,11 @@ const { trafficStatsManager, } = require('../common/constants'); const JsonRpcUtil = require('./json-rpc-util'); +const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getInjectionApis(node, p2pServer) { return { - ain_getBootstrapPubKey: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_BOOTSTRAP_PUB_KEY]: function(args, done) { const beginTime = Date.now(); const result = node.bootstrapAccount ? node.bootstrapAccount.public_key : null; const latency = Date.now() - beginTime; @@ -14,7 +15,7 @@ module.exports = function getInjectionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_injectAccountFromPrivateKey: async function(args, done) { + [JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY]: async function(args, done) { const beginTime = Date.now(); let result = false; if (await node.injectAccountFromPrivateKey(args.encryptedPrivateKey)) { @@ -26,7 +27,7 @@ module.exports = function getInjectionApis(node, p2pServer) { return JsonRpcUtil.addProtocolVersion({ result }); }, - ain_injectAccountFromKeystore: async function(args, done) { + [JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_KEYSTORE]: async function(args, done) { const beginTime = Date.now(); let result = false; if (await node.injectAccountFromKeystore(args.encryptedKeystore, args.encryptedPassword)) { @@ -38,7 +39,7 @@ module.exports = function getInjectionApis(node, p2pServer) { return JsonRpcUtil.addProtocolVersion({ result }); }, - ain_injectAccountFromHDWallet: async function(args, done) { + [JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_HD_WALLET]: async function(args, done) { const beginTime = Date.now(); let result = false; if (await node.injectAccountFromHDWallet(args.encryptedMnemonic, args.index)) { From 334c877d4953a09e124c5437de6f08d2dd3ed275 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 12 May 2022 10:22:30 +0900 Subject: [PATCH 017/166] Update json rpc method for network --- json_rpc/constants.js | 8 ++++++++ json_rpc/index.js | 6 +++--- json_rpc/network.js | 17 +++++++++-------- p2p/index.js | 8 ++++---- test/test-util.js | 12 ++++++++---- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/json_rpc/constants.js b/json_rpc/constants.js index 2abca017a..89697241c 100644 --- a/json_rpc/constants.js +++ b/json_rpc/constants.js @@ -33,6 +33,14 @@ const JSON_RPC_METHOD = { AIN_GET_EVENT_HANDLER_FILTER_INFO: 'ain_getEventHandlerFilterInfo', AIN_GET_EVENT_HANDLER_CHANNEL_INFO: 'ain_getEventHandlerChannelInfo', AIN_GET_BOOTSTRAP_PUB_KEY: 'ain_getBootstrapPubKey', + NET_LISTENING: 'net_listening', + NET_PEER_COUNT: 'net_peerCount', + NET_SYNCING: 'net_syncing', + NET_GET_NETWORK_ID: 'net_getNetworkId', + NET_GET_CHAIN_ID: 'net_getChainId', + NET_CONSENSUS_STATUS: 'net_consensusStatus', + NET_RAW_CONSENSUS_STATUS: 'net_rawConsensusStatus', + P2P_GET_PEER_CANDIDATE_INFO: 'p2p_getPeerCandidateInfo', // SET AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', diff --git a/json_rpc/index.js b/json_rpc/index.js index 69cb41279..1309f7fd1 100644 --- a/json_rpc/index.js +++ b/json_rpc/index.js @@ -11,6 +11,7 @@ const getEventHandlerApis = require('./event-handler'); const getNetworkApis = require('./network'); const getTransactionApis = require('./transaction'); const getVersionApis = require('./version'); +const { JSON_RPC_METHOD } = require('./constants'); /** * Defines the list of funtions which are accessibly to clients through the @@ -41,9 +42,8 @@ module.exports = function getApis(node, p2pServer, eventHandler, minProtocolVers ...getVersionApis(minProtocolVersion, maxProtocolVersion), }); } else { - Object.assign(apis, { - p2p_getPeerCandidateInfo: getNetworkApis(node, p2pServer).p2p_getPeerCandidateInfo, - }); + Object.assign(apis, { [JSON_RPC_METHOD.P2P_GET_PEER_CANDIDATE_INFO]: + getNetworkApis(node, p2pServer).p2p_getPeerCandidateInfo }); } if (eventHandler !== null) { Object.assign(apis, getEventHandlerApis(eventHandler)); diff --git a/json_rpc/network.js b/json_rpc/network.js index ff201bc27..53c8a9a95 100644 --- a/json_rpc/network.js +++ b/json_rpc/network.js @@ -4,10 +4,11 @@ const { trafficStatsManager, } = require('../common/constants'); const JsonRpcUtil = require('./json-rpc-util'); +const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getNetworkApis(node, p2pServer) { return { - net_listening: function(args, done) { + [JSON_RPC_METHOD.NET_LISTENING]: function(args, done) { const beginTime = Date.now(); const peerCount = Object.keys(p2pServer.inbound).length; const result = !!peerCount; @@ -16,7 +17,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - net_peerCount: function(args, done) { + [JSON_RPC_METHOD.NET_PEER_COUNT]: function(args, done) { const beginTime = Date.now(); const peerCount = Object.keys(p2pServer.inbound).length; const latency = Date.now() - beginTime; @@ -24,7 +25,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result: peerCount })); }, - net_syncing: function(args, done) { + [JSON_RPC_METHOD.NET_SYNCING]: function(args, done) { const beginTime = Date.now(); const result = (node.state === BlockchainNodeStates.CHAIN_SYNCING); const latency = Date.now() - beginTime; @@ -34,7 +35,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - net_getNetworkId: function(args, done) { + [JSON_RPC_METHOD.NET_GET_NETWORK_ID]: function(args, done) { const beginTime = Date.now(); const result = node.getBlockchainParam('genesis/network_id'); const latency = Date.now() - beginTime; @@ -42,7 +43,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - net_getChainId: function(args, done) { + [JSON_RPC_METHOD.NET_GET_CHAIN_ID]: function(args, done) { const beginTime = Date.now(); const result = node.getBlockchainParam('genesis/chain_id'); const latency = Date.now() - beginTime; @@ -50,7 +51,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - net_consensusStatus: function(args, done) { + [JSON_RPC_METHOD.NET_CONSENSUS_STATUS]: function(args, done) { const beginTime = Date.now(); const result = p2pServer.consensus.getStatus(); const latency = Date.now() - beginTime; @@ -58,7 +59,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - net_rawConsensusStatus: function(args, done) { + [JSON_RPC_METHOD.NET_RAW_CONSENSUS_STATUS]: function(args, done) { const beginTime = Date.now(); const result = p2pServer.consensus.getRawStatus(); const latency = Date.now() - beginTime; @@ -66,7 +67,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - p2p_getPeerCandidateInfo: function(args, done) { + [JSON_RPC_METHOD.P2P_GET_PEER_CANDIDATE_INFO]: function(args, done) { const beginTime = Date.now(); const result = p2pServer.client.getPeerCandidateInfo(); const latency = Date.now() - beginTime; diff --git a/p2p/index.js b/p2p/index.js index 4ac6ae422..e6d02d543 100644 --- a/p2p/index.js +++ b/p2p/index.js @@ -22,10 +22,9 @@ const { } = require('../common/constants'); const FileUtil = require('../common/file-util'); const P2pUtil = require('./p2p-util'); -const { - sendGetRequest -} = require('../common/network-util'); +const { sendGetRequest } = require('../common/network-util'); const { Block } = require('../blockchain/block'); +const { JSON_RPC_METHOD } = require('../json_rpc/constants'); class P2pClient { constructor(node, minProtocolVersion, maxProtocolVersion) { @@ -1178,7 +1177,8 @@ class P2pClient { this.peerCandidates.delete(peerCandidateJsonRpcUrl); return; } - const resp = await sendGetRequest(peerCandidateJsonRpcUrl, 'p2p_getPeerCandidateInfo', { }); + const resp = await sendGetRequest( + peerCandidateJsonRpcUrl, JSON_RPC_METHOD.P2P_GET_PEER_CANDIDATE_INFO, { }); const peerCandidateInfo = _.get(resp, 'data.result.result'); if (!peerCandidateInfo) { logger.error(`Invalid peer candidate info from peer candidate url ` + diff --git a/test/test-util.js b/test/test-util.js index 051801d63..2ca962ae5 100644 --- a/test/test-util.js +++ b/test/test-util.js @@ -6,10 +6,10 @@ const { Block } = require('../blockchain/block'); const DB = require('../db'); const { BlockchainConsts, - StateVersions, - BlockchainParams, + StateVersions } = require('../common/constants'); const CommonUtil = require('../common/common-util'); +const { JSON_RPC_METHOD } = require('../json_rpc/constants'); const GET_OPTIONS_INCLUDE_ALL = { includeTreeInfo: true, @@ -175,8 +175,12 @@ async function waitUntilNodeSyncs(server) { while (isSyncing) { try { isSyncing = parseOrLog(syncRequest('POST', server + '/json-rpc', - {json: {jsonrpc: '2.0', method: 'net_syncing', id: 0, - params: {protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}}}) + { + json: { + jsonrpc: '2.0', method: JSON_RPC_METHOD.NET_SYNCING, id: 0, + params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION } + } + }) .body.toString('utf-8')).result.result; } catch (e) { // server may not be ready yet From 55893185636295b0808f4b54914eab52ad382231 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 12 May 2022 10:41:47 +0900 Subject: [PATCH 018/166] Update json rpc method for transaction --- common/network-util.js | 5 +-- json_rpc/constants.js | 7 ++++ json_rpc/transaction.js | 15 +++++---- test/integration/consensus.test.js | 2 +- test/integration/function.test.js | 17 +++++----- test/integration/node.test.js | 54 +++++++++++++++--------------- test/integration/sharding.test.js | 32 +++++++++--------- tools/simple-load-test/index.js | 5 +-- tools/util.js | 5 +-- 9 files changed, 77 insertions(+), 65 deletions(-) diff --git a/common/network-util.js b/common/network-util.js index f9c4bbba8..d23674baa 100644 --- a/common/network-util.js +++ b/common/network-util.js @@ -7,6 +7,7 @@ const ip = require('ip'); const extIp = require('ext-ip')(); const CommonUtil = require('../common/common-util'); const DB = require('../db'); +const { JSON_RPC_METHOD } = require('../json_rpc/constants'); const GCP_EXTERNAL_IP_URL = 'http://metadata.google.internal/computeMetadata/v1/instance' + '/network-interfaces/0/access-configs/0/external-ip'; const GCP_INTERNAL_IP_URL = 'http://metadata.google.internal/computeMetadata/v1/instance' + @@ -18,7 +19,7 @@ async function _waitUntilTxFinalize(endpoint, txHash) { while (true) { const confirmed = await sendGetRequest( endpoint, - 'ain_getTransactionByHash', + JSON_RPC_METHOD.AIN_GET_TRANSACTION_BY_HASH, { hash: txHash } ) .then((resp) => { @@ -50,7 +51,7 @@ async function sendSignedTx(endpoint, params) { return await axios.post( endpoint, { - method: 'ain_sendSignedTransaction', + method: JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, params, jsonrpc: '2.0', id: 0 diff --git a/json_rpc/constants.js b/json_rpc/constants.js index 89697241c..7686678d2 100644 --- a/json_rpc/constants.js +++ b/json_rpc/constants.js @@ -41,12 +41,19 @@ const JSON_RPC_METHOD = { NET_CONSENSUS_STATUS: 'net_consensusStatus', NET_RAW_CONSENSUS_STATUS: 'net_rawConsensusStatus', P2P_GET_PEER_CANDIDATE_INFO: 'p2p_getPeerCandidateInfo', + AIN_GET_PENDING_TRANSACTIONS: 'ain_getPendingTransactions', + AIN_GET_TRANSACTION_POOL_SIZE_UTILIZATION: 'ain_getTransactionPoolSizeUtilization', + AIN_GET_TRANSACTION_BY_HASH: 'ain_getTransactionByHash', + AIN_GET_TRANSACTION_BY_BLOCK_HASH_AND_INDEX: 'ain_getTransactionByBlockHashAndIndex', + AIN_GET_TRANSACTION_BY_BLOCK_NUMBER_AND_INDEX: 'ain_getTransactionByBlockNumberAndIndex', // SET AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: 'ain_injectAccountFromPrivateKey', AIN_INJECT_ACCOUNT_FROM_KEYSTORE: 'ain_injectAccountFromKeystore', AIN_INJECT_ACCOUNT_FROM_HD_WALLET: 'ain_injectAccountFromHDWallet', + AIN_SEND_SIGNED_TRANSACTION: 'ain_sendSignedTransaction', + AIN_SEND_SIGNED_TRANSACTION_BATCH: 'ain_sendSignedTransactionBatch', } module.exports = { diff --git a/json_rpc/transaction.js b/json_rpc/transaction.js index 0532e51b1..83ef0a594 100644 --- a/json_rpc/transaction.js +++ b/json_rpc/transaction.js @@ -8,10 +8,11 @@ const { const { JsonRpcApiResultCode } = require('../common/result-code'); const CommonUtil = require('../common/common-util'); const Transaction = require('../tx-pool/transaction'); +const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getTransactionApis(node, p2pServer) { return { - ain_getPendingTransactions: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_PENDING_TRANSACTIONS]: function(args, done) { const beginTime = Date.now(); const result = node.tp.transactions; const latency = Date.now() - beginTime; @@ -19,7 +20,7 @@ module.exports = function getTransactionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getTransactionPoolSizeUtilization: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_TRANSACTION_POOL_SIZE_UTILIZATION]: function(args, done) { const beginTime = Date.now(); const address = args.address; const txPoolSizeUtil = node.getTxPoolSizeUtilization(address); @@ -28,7 +29,7 @@ module.exports = function getTransactionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result: txPoolSizeUtil })); }, - ain_getTransactionByHash: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_TRANSACTION_BY_HASH]: function(args, done) { const beginTime = Date.now(); const transactionInfo = node.getTransactionByHash(args.hash); const latency = Date.now() - beginTime; @@ -36,7 +37,7 @@ module.exports = function getTransactionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result: transactionInfo })); }, - ain_getTransactionByBlockHashAndIndex: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_TRANSACTION_BY_BLOCK_HASH_AND_INDEX]: function(args, done) { const beginTime = Date.now(); let result = null; if (args.block_hash && Number.isInteger(args.index)) { @@ -55,7 +56,7 @@ module.exports = function getTransactionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_getTransactionByBlockNumberAndIndex: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_TRANSACTION_BY_BLOCK_NUMBER_AND_INDEX]: function(args, done) { const beginTime = Date.now(); let result = null; if (Number.isInteger(args.block_number) && Number.isInteger(args.index)) { @@ -74,7 +75,7 @@ module.exports = function getTransactionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_sendSignedTransaction: function(args, done) { + [JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION]: function(args, done) { const beginTime = Date.now(); const txBytesLimit = node.getBlockchainParam('resource/tx_bytes_limit'); if (sizeof(args) > txBytesLimit) { @@ -126,7 +127,7 @@ module.exports = function getTransactionApis(node, p2pServer) { } }, - ain_sendSignedTransactionBatch: function(args, done) { + [JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH]: function(args, done) { const beginTime = Date.now(); const batchTxListSizeLimit = node.getBlockchainParam('resource/batch_tx_list_size_limit'); if (!args.tx_list || !CommonUtil.isArray(args.tx_list)) { diff --git a/test/integration/consensus.test.js b/test/integration/consensus.test.js index d3db8cb52..0420fcf66 100644 --- a/test/integration/consensus.test.js +++ b/test/integration/consensus.test.js @@ -192,7 +192,7 @@ describe('Consensus', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('a2b5848760d81afe205884284716f90356ad82be5ab77b8130980bdb0b7ba2ba', 'hex')); - const res = await client.request('ain_sendSignedTransaction', { + const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION diff --git a/test/integration/function.test.js b/test/integration/function.test.js index 4e4fc05a7..8e8c51a9d 100644 --- a/test/integration/function.test.js +++ b/test/integration/function.test.js @@ -23,6 +23,7 @@ const { getBlockByNumber, eraseStateGas, } = require('../test-util'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); const PROJECT_ROOT = require('path').dirname(__filename) + "/../../" const TRACKER_SERVER = PROJECT_ROOT + "tracker-server/index.js" @@ -932,7 +933,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('a2b5848760d81afe205884284716f90356ad82be5ab77b8130980bdb0b7ba2ba', 'hex')); - const res = await client.request('ain_sendSignedTransaction', { + const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -4818,7 +4819,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request('ain_sendSignedTransaction', { + const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -4861,7 +4862,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request('ain_sendSignedTransaction', { + const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -5010,7 +5011,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request('ain_sendSignedTransaction', { + const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -5614,7 +5615,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request('ain_sendSignedTransaction', { + const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -5660,7 +5661,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request('ain_sendSignedTransaction', { + const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -5839,7 +5840,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction( closeCheckinTxBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const closeCheckinRes = await client.request('ain_sendSignedTransaction', { + const closeCheckinRes = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: closeCheckinTxBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -5957,7 +5958,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request('ain_sendSignedTransaction', { + const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION diff --git a/test/integration/node.test.js b/test/integration/node.test.js index ca37ced99..dc798eaf4 100644 --- a/test/integration/node.test.js +++ b/test/integration/node.test.js @@ -2977,7 +2977,7 @@ describe('Blockchain Node', () => { }) }) - describe('ain_sendSignedTransaction api', () => { + describe(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION} api`, () => { const account = { address: "0x9534bC7529961E5737a3Dd317BdEeD41AC08a52D", private_key: "e96292ef0676287908fc3461f747f106b7b9336f183b1766f83672fbe893664d", @@ -3091,7 +3091,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3148,7 +3148,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3208,7 +3208,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account2.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3268,7 +3268,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account2.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3321,7 +3321,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account09.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3389,7 +3389,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account09.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3457,7 +3457,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account09.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3542,7 +3542,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account4.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3620,7 +3620,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account4.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3683,7 +3683,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3764,7 +3764,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3811,7 +3811,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3840,7 +3840,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { transaction: txBody, // wrong field name signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3869,7 +3869,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3898,7 +3898,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature: signature + '0', // invalid signature protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3914,7 +3914,7 @@ describe('Blockchain Node', () => { }) }) - describe('ain_sendSignedTransactionBatch api', () => { + describe(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH} api`, () => { const account = { address: "0x85a620A5A46d01cc1fCF49E73ab00710d4da943E", private_key: "b542fc2ca4a68081b3ba238888d3a8783354c3aa81711340fd69f6ff32798525", @@ -4113,7 +4113,7 @@ describe('Blockchain Node', () => { signature }); } - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: txList, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }).then((res) => { @@ -4139,7 +4139,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: { // should be an array tx_body: txBody, signature, @@ -4179,7 +4179,7 @@ describe('Blockchain Node', () => { signature, }); } - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: txList, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }).then((res) => { @@ -4215,7 +4215,7 @@ describe('Blockchain Node', () => { signature, }); } - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: txList, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }).then((res) => { @@ -4262,7 +4262,7 @@ describe('Blockchain Node', () => { signature, }); } - const res1 = await client.request('ain_sendSignedTransactionBatch', { + const res1 = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: txList1, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }); @@ -4286,7 +4286,7 @@ describe('Blockchain Node', () => { tx_body: txBody, signature, }); - const res2 = await client.request('ain_sendSignedTransactionBatch', { + const res2 = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: txList2, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }); @@ -4322,7 +4322,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBodyBefore, @@ -4362,7 +4362,7 @@ describe('Blockchain Node', () => { timestamp: Date.now(), nonce: -1 }; - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBodyBefore, @@ -4403,7 +4403,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBodyBefore, @@ -4444,7 +4444,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBodyBefore, diff --git a/test/integration/sharding.test.js b/test/integration/sharding.test.js index c06af7b16..dc7b116b5 100644 --- a/test/integration/sharding.test.js +++ b/test/integration/sharding.test.js @@ -2023,8 +2023,8 @@ describe('Sharding', () => { }) }) - describe('ain_sendSignedTransaction api', () => { - it('ain_sendSignedTransaction with is_global = false', () => { + describe(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION} api`, () => { + it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION} with is_global = false`, () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2038,7 +2038,7 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) .then((res) => { const result = _.get(res, 'result.result', null); @@ -2072,7 +2072,7 @@ describe('Sharding', () => { }) }) - it('ain_sendSignedTransaction with is_global = false (explicit)', () => { + it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION} with is_global = false (explicit)`, () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2087,8 +2087,8 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { tx_body: txBody, signature, - protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, + { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) .then((res) => { const result = _.get(res, 'result.result', null); expect(result).to.not.equal(null); @@ -2121,7 +2121,7 @@ describe('Sharding', () => { }) }) - it('ain_sendSignedTransaction with is_global = true', () => { + it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION} with is_global = true`, () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2136,8 +2136,8 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransaction', { tx_body: txBody, signature, - protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, + { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) .then((res) => { const result = _.get(res, 'result.result', null); expect(result).to.not.equal(null); @@ -2171,8 +2171,8 @@ describe('Sharding', () => { }) }) - describe('ain_sendSignedTransactionBatch api', () => { - it('ain_sendSignedTransactionBatch with is_global = false', () => { + describe(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH} api`, () => { + it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH} with is_global = false`, () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2186,7 +2186,7 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBody, @@ -2229,7 +2229,7 @@ describe('Sharding', () => { }) }) - it('ain_sendSignedTransactionBatch with is_global = false (explicit)', () => { + it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH} with is_global = false (explicit)`, () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2244,7 +2244,7 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBody, @@ -2290,7 +2290,7 @@ describe('Sharding', () => { }) }) - it('ain_sendSignedTransactionBatch with is_global = true', () => { + it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH} with is_global = true`, () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2305,7 +2305,7 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request('ain_sendSignedTransactionBatch', { + return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBody, diff --git a/tools/simple-load-test/index.js b/tools/simple-load-test/index.js index 34e298705..10d2232ea 100644 --- a/tools/simple-load-test/index.js +++ b/tools/simple-load-test/index.js @@ -8,7 +8,8 @@ const axios = require('axios'); const commandLineArgs = require('command-line-args'); const getUsage = require('command-line-usage'); const CommonUtil = require('../../common/common-util'); -const {signTx} = require('../util'); +const { signTx } = require('../util'); +const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); const delay = (time) => new Promise((resolve) => setTimeout(resolve, time)); const testPath = '/apps/loadtest'; const ainPrivateKey = '4207f5dcacb1b601d3a1f8cb10afaca158f6ebe383c0b30d02b39f8d2060cce3'; @@ -57,7 +58,7 @@ const sections = [ function sendTx(endpointUrl, signedTx) { return axios.post(`${endpointUrl}/json-rpc`, { - method: 'ain_sendSignedTransaction', + method: JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, params: signedTx, jsonrpc: '2.0', id: 0, diff --git a/tools/util.js b/tools/util.js index 51628539f..fea443259 100644 --- a/tools/util.js +++ b/tools/util.js @@ -2,6 +2,7 @@ const _ = require('lodash'); const axios = require('axios'); const { BlockchainConsts } = require('../common/constants'); const CommonUtil = require('../common/common-util'); +const { JSON_RPC_METHOD } = require('../json_rpc/constants'); // FIXME(minsulee2): this is duplicated function see: ./common/network-util.js function signAndSendTx(endpointUrl, txBody, privateKey, chainId) { @@ -14,7 +15,7 @@ function signAndSendTx(endpointUrl, txBody, privateKey, chainId) { return axios.post( `${endpointUrl}/json-rpc`, { - method: 'ain_sendSignedTransaction', + method: JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, params: signedTx, jsonrpc: '2.0', id: 0 @@ -35,7 +36,7 @@ async function sendGetTxByHashRequest(endpointUrl, txHash) { return await axios.post( `${endpointUrl}/json-rpc`, { - method: 'ain_getTransactionByHash', + method: JSON_RPC_METHOD.AIN_GET_TRANSACTION_BY_HASH, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, hash: txHash, From f5c4a2acef7aba42d707d1cb55ce4e74c2d62706 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 12 May 2022 10:48:53 +0900 Subject: [PATCH 019/166] Update json rpc method for version --- common/version-util.js | 5 +++-- json_rpc/constants.js | 2 ++ json_rpc/version.js | 5 +++-- test/integration/node.test.js | 16 ++++++++-------- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/common/version-util.js b/common/version-util.js index dbac15cf8..632cfe1b1 100644 --- a/common/version-util.js +++ b/common/version-util.js @@ -1,6 +1,7 @@ const semver = require('semver'); const { BlockchainConsts } = require('../common/constants'); const { DevClientApiResultCode } = require('../common/result-code'); +const { JSON_RPC_METHOD } = require('../json_rpc/constants'); class VersionUtil { static isValidProtocolVersion(version) { @@ -42,8 +43,8 @@ class VersionUtil { version = req.body.params.protoVer; } const coercedVer = semver.coerce(version); - if (req.body.method === 'ain_getProtocolVersion' || - req.body.method === 'ain_checkProtocolVersion') { + if (req.body.method === JSON_RPC_METHOD.AIN_GET_PROTOCOL_VERSION || + req.body.method === JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION) { next(); } else if (version === undefined) { res.status(200) diff --git a/json_rpc/constants.js b/json_rpc/constants.js index 7686678d2..956684a19 100644 --- a/json_rpc/constants.js +++ b/json_rpc/constants.js @@ -46,6 +46,8 @@ const JSON_RPC_METHOD = { AIN_GET_TRANSACTION_BY_HASH: 'ain_getTransactionByHash', AIN_GET_TRANSACTION_BY_BLOCK_HASH_AND_INDEX: 'ain_getTransactionByBlockHashAndIndex', AIN_GET_TRANSACTION_BY_BLOCK_NUMBER_AND_INDEX: 'ain_getTransactionByBlockNumberAndIndex', + AIN_GET_PROTOCOL_VERSION: 'ain_getProtocolVersion', + AIN_CHECK_PROTOCOL_VERSION: 'ain_checkProtocolVersion', // SET AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', diff --git a/json_rpc/version.js b/json_rpc/version.js index e0e8521cf..7ac262e70 100644 --- a/json_rpc/version.js +++ b/json_rpc/version.js @@ -6,10 +6,11 @@ const { } = require('../common/constants'); const { JsonRpcApiResultCode } = require('../common/result-code'); const JsonRpcUtil = require('./json-rpc-util'); +const { JSON_RPC_METHOD } = require('./constants'); module.exports = function getVersionApis(minProtocolVersion, maxProtocolVersion) { return { - ain_getProtocolVersion: function(args, done) { + [JSON_RPC_METHOD.AIN_GET_PROTOCOL_VERSION]: function(args, done) { const beginTime = Date.now(); const result = BlockchainConsts.CURRENT_PROTOCOL_VERSION; const latency = Date.now() - beginTime; @@ -17,7 +18,7 @@ module.exports = function getVersionApis(minProtocolVersion, maxProtocolVersion) done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - ain_checkProtocolVersion: function(args, done) { + [JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION]: function(args, done) { const beginTime = Date.now(); const version = args.protoVer; const coercedVer = semver.coerce(version); diff --git a/test/integration/node.test.js b/test/integration/node.test.js index dc798eaf4..49a9b73e3 100644 --- a/test/integration/node.test.js +++ b/test/integration/node.test.js @@ -1273,20 +1273,20 @@ describe('Blockchain Node', () => { }) }) - describe('ain_getProtocolVersion api', () => { + describe(`${JSON_RPC_METHOD.AIN_GET_PROTOCOL_VERSION} api`, () => { it('returns the correct version', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_getProtocolVersion', {}) + return client.request(JSON_RPC_METHOD.AIN_GET_PROTOCOL_VERSION, {}) .then(res => { expect(res.result.protoVer).to.equal(BlockchainConsts.CURRENT_PROTOCOL_VERSION); }) }); }); - describe('ain_checkProtocolVersion api', () => { + describe(`${JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION} api`, () => { it('returns success code', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_checkProtocolVersion', { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) + return client.request(JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) .then(res => { expect(res.result.code).to.equal(0); expect(res.result.result).to.equal("Success"); @@ -1295,7 +1295,7 @@ describe('Blockchain Node', () => { it('returns version not specified code', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_checkProtocolVersion', {}) + return client.request(JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION, {}) .then(res => { expect(res.result.code).to.equal(30101); expect(res.result.message).to.equal("Protocol version not specified."); @@ -1304,7 +1304,7 @@ describe('Blockchain Node', () => { it('returns invalid version code', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_checkProtocolVersion', { protoVer: 'a.b.c' }) + return client.request(JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION, { protoVer: 'a.b.c' }) .then(res => { expect(res.result.code).to.equal(30102); expect(res.result.message).to.equal("Invalid protocol version."); @@ -1313,7 +1313,7 @@ describe('Blockchain Node', () => { it('returns incompatible version code for ill-formatted version', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_checkProtocolVersion', { protoVer: 0 }) + return client.request(JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION, { protoVer: 0 }) .then(res => { expect(res.result.code).to.equal(30103); expect(res.result.message).to.equal("Incompatible protocol version."); @@ -1322,7 +1322,7 @@ describe('Blockchain Node', () => { it('returns incompatible version code for low version', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request('ain_checkProtocolVersion', { protoVer: '0.0.1' }) + return client.request(JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION, { protoVer: '0.0.1' }) .then(res => { expect(res.result.code).to.equal(30103); expect(res.result.message).to.equal("Incompatible protocol version."); From d2b775d86f9afc74b06630d8d15736bc101e57f0 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 12 May 2022 12:57:54 +0900 Subject: [PATCH 020/166] Limit json rpc call as well --- client/index.js | 2 +- client/middleware.js | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/client/index.js b/client/index.js index e23cda766..72826bdf5 100755 --- a/client/index.js +++ b/client/index.js @@ -59,7 +59,7 @@ const jsonRpcApis = require('../json_rpc')( app.post( '/json-rpc', VersionUtil.validateVersion.bind({ minProtocolVersion, maxProtocolVersion }), - middleware.test(), + middleware.jsonRpcLimiter(), jayson.server(jsonRpcApis).middleware() ); diff --git a/client/middleware.js b/client/middleware.js index 9d1c2581c..a10414127 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -1,3 +1,4 @@ +const _ = require('lodash'); const express = require('express'); const cors = require('cors'); const ipWhitelist = require('ip-whitelist'); @@ -10,6 +11,7 @@ const { isWildcard } = require('../common/common-util'); const { convertIpv6ToIpv4 } = require('../common/network-util'); +const { JSON_RPC_METHOD } = require('../json_rpc/constants'); class Middleware { constructor () { @@ -112,11 +114,20 @@ class Middleware { }) : this._emptyHandler(); } - test() { - return (req, res, next) => { - console.log(req) - return next(); - } + jsonRpcLimiter(req, res, next) { + const jsonRpcMethod = _.get(req, 'body.method'); + switch (jsonRpcMethod) { + case JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: + case JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: + case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: + case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_KEYSTORE: + case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_HD_WALLET: + case JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION: + case JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH: + return this.writeLimiter(); + default: + return this.readLimiter(); + } } // NOTE(minsulee2): debugging purpose From e45548eb896b66eef5b367506cac81100e655eca Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 12 May 2022 17:04:45 +0900 Subject: [PATCH 021/166] Fix read and write limit numbers --- blockchain-configs/afan-shard/node_params.json | 4 ++-- blockchain-configs/base/node_params.json | 4 ++-- blockchain-configs/he-shard/node_params.json | 4 ++-- blockchain-configs/mainnet-prod/node_params.json | 4 ++-- blockchain-configs/sim-shard/node_params.json | 4 ++-- blockchain-configs/testnet-dev/node_params.json | 4 ++-- blockchain-configs/testnet-exp/node_params.json | 4 ++-- blockchain-configs/testnet-prod/node_params.json | 4 ++-- blockchain-configs/testnet-sandbox/node_params.json | 4 ++-- blockchain-configs/testnet-staging/node_params.json | 4 ++-- 10 files changed, 20 insertions(+), 20 deletions(-) diff --git a/blockchain-configs/afan-shard/node_params.json b/blockchain-configs/afan-shard/node_params.json index a5719cf4c..092c93ae3 100644 --- a/blockchain-configs/afan-shard/node_params.json +++ b/blockchain-configs/afan-shard/node_params.json @@ -43,8 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 3, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 1, - "MAX_WRITE_RATE_LIMIT": 10, + "MAX_READ_RATE_LIMIT": 10, + "MAX_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/base/node_params.json b/blockchain-configs/base/node_params.json index 9fa87c113..ef6bccdeb 100644 --- a/blockchain-configs/base/node_params.json +++ b/blockchain-configs/base/node_params.json @@ -43,8 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 1, - "MAX_WRITE_RATE_LIMIT": 10, + "MAX_READ_RATE_LIMIT": 10, + "MAX_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/he-shard/node_params.json b/blockchain-configs/he-shard/node_params.json index ef6b9e069..4898e3e7d 100644 --- a/blockchain-configs/he-shard/node_params.json +++ b/blockchain-configs/he-shard/node_params.json @@ -43,8 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 1, - "MAX_WRITE_RATE_LIMIT": 10, + "MAX_READ_RATE_LIMIT": 10, + "MAX_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/mainnet-prod/node_params.json b/blockchain-configs/mainnet-prod/node_params.json index edbbaf836..9e527f90d 100644 --- a/blockchain-configs/mainnet-prod/node_params.json +++ b/blockchain-configs/mainnet-prod/node_params.json @@ -42,8 +42,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 1, - "MAX_WRITE_RATE_LIMIT": 10, + "MAX_READ_RATE_LIMIT": 10, + "MAX_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/sim-shard/node_params.json b/blockchain-configs/sim-shard/node_params.json index d20edda8b..2ab8a2783 100644 --- a/blockchain-configs/sim-shard/node_params.json +++ b/blockchain-configs/sim-shard/node_params.json @@ -43,8 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 1, - "MAX_WRITE_RATE_LIMIT": 10, + "MAX_READ_RATE_LIMIT": 10, + "MAX_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-dev/node_params.json b/blockchain-configs/testnet-dev/node_params.json index 707f6e075..975bee5dd 100644 --- a/blockchain-configs/testnet-dev/node_params.json +++ b/blockchain-configs/testnet-dev/node_params.json @@ -43,8 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 1, - "MAX_WRITE_RATE_LIMIT": 10, + "MAX_READ_RATE_LIMIT": 10, + "MAX_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-exp/node_params.json b/blockchain-configs/testnet-exp/node_params.json index daec0982e..178572233 100644 --- a/blockchain-configs/testnet-exp/node_params.json +++ b/blockchain-configs/testnet-exp/node_params.json @@ -43,8 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 1, - "MAX_WRITE_RATE_LIMIT": 10, + "MAX_READ_RATE_LIMIT": 10, + "MAX_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-prod/node_params.json b/blockchain-configs/testnet-prod/node_params.json index e4a77939f..7a1365e6b 100644 --- a/blockchain-configs/testnet-prod/node_params.json +++ b/blockchain-configs/testnet-prod/node_params.json @@ -43,8 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 1, - "MAX_WRITE_RATE_LIMIT": 10, + "MAX_READ_RATE_LIMIT": 10, + "MAX_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-sandbox/node_params.json b/blockchain-configs/testnet-sandbox/node_params.json index 41f979874..d78b9ea5e 100644 --- a/blockchain-configs/testnet-sandbox/node_params.json +++ b/blockchain-configs/testnet-sandbox/node_params.json @@ -43,8 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 1, - "MAX_WRITE_RATE_LIMIT": 10, + "MAX_READ_RATE_LIMIT": 10, + "MAX_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-staging/node_params.json b/blockchain-configs/testnet-staging/node_params.json index d1d2632db..9d8bbcb89 100644 --- a/blockchain-configs/testnet-staging/node_params.json +++ b/blockchain-configs/testnet-staging/node_params.json @@ -43,8 +43,8 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 1, - "MAX_WRITE_RATE_LIMIT": 10, + "MAX_READ_RATE_LIMIT": 10, + "MAX_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, From ccd9f97534018c79458bdd5804e847d145887f96 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 12 May 2022 17:20:29 +0900 Subject: [PATCH 022/166] Remove individual limiters for restful api --- client/index.js | 117 ++++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 58 deletions(-) diff --git a/client/index.js b/client/index.js index 72826bdf5..2fc70641c 100755 --- a/client/index.js +++ b/client/index.js @@ -32,6 +32,7 @@ app.use(middleware.expressJsonRequestBodySizeLimiter()); app.use(middleware.expressUrlencdedRequestBodySizeLimiter()); app.use(middleware.corsLimiter()); app.use(middleware.ipWhitelistLimiter()); +// app.use(middleware.test()); const eventHandler = NodeConfigs.ENABLE_EVENT_HANDLER === true ? new EventHandler() : null; const node = new BlockchainNode(null, eventHandler); @@ -63,7 +64,7 @@ app.post( jayson.server(jsonRpcApis).middleware() ); -app.get('/', middleware.readLimiter(), (req, res, next) => { +app.get('/', (req, res, next) => { const welcome = `[Welcome to AIN Blockchain Node]\n\n- CURRENT_PROTOCOL_VERSION: ${BlockchainConsts.CURRENT_PROTOCOL_VERSION}\n- DATA_PROTOCOL_VERSION: ${BlockchainConsts.DATA_PROTOCOL_VERSION}\n- CONSENSUS_PROTOCOL_VERSION: ${BlockchainConsts.CONSENSUS_PROTOCOL_VERSION}\n\nDevelopers Guide: ${NodeConfigs.BLOCKCHAIN_GUIDE_URL}`; res.status(200) .set('Content-Type', 'text/plain') @@ -71,7 +72,7 @@ app.get('/', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/health_check', middleware.readLimiter(), (req, res, next) => { +app.get('/health_check', (req, res, next) => { const result = p2pServer.getNodeHealth(); res.status(200) .set('Content-Type', 'text/plain') @@ -80,7 +81,7 @@ app.get('/health_check', middleware.readLimiter(), (req, res, next) => { }); // Exports metrics for Prometheus. -app.get('/metrics', middleware.readLimiter(), async (req, res, next) => { +app.get('/metrics', async (req, res, next) => { const beginTime = Date.now(); const status = p2pClient.getStatus(); const result = CommonUtil.objToMetrics(status); @@ -100,7 +101,7 @@ app.get('/metrics', middleware.readLimiter(), async (req, res, next) => { }); // Used in wait_until_node_sync_gcp.sh -app.get('/last_block_number', middleware.readLimiter(), (req, res, next) => { +app.get('/last_block_number', (req, res, next) => { const beginTime = Date.now(); const result = node.bc.lastBlockNumber(); const latency = Date.now() - beginTime; @@ -115,7 +116,7 @@ app.get('/last_block_number', middleware.readLimiter(), (req, res, next) => { * Dev Client GET APIs (available to whitelisted IPs) */ -app.get('/get_value', middleware.readLimiter(), (req, res, next) => { +app.get('/get_value', (req, res, next) => { const beginTime = Date.now(); const result = node.db.getValue(req.query.ref, CommonUtil.toGetOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -129,7 +130,7 @@ app.get('/get_value', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/get_function', middleware.readLimiter(), (req, res, next) => { +app.get('/get_function', (req, res, next) => { const beginTime = Date.now(); const result = node.db.getFunction(req.query.ref, CommonUtil.toGetOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -143,7 +144,7 @@ app.get('/get_function', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/get_rule', middleware.readLimiter(), (req, res, next) => { +app.get('/get_rule', (req, res, next) => { const beginTime = Date.now(); const result = node.db.getRule(req.query.ref, CommonUtil.toGetOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -157,7 +158,7 @@ app.get('/get_rule', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/get_owner', middleware.readLimiter(), (req, res, next) => { +app.get('/get_owner', (req, res, next) => { const beginTime = Date.now(); const result = node.db.getOwner(req.query.ref, CommonUtil.toGetOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -174,7 +175,7 @@ app.get('/get_owner', middleware.readLimiter(), (req, res, next) => { /** * Returns the state proof at the given full database path. */ -app.get('/get_state_proof', middleware.readLimiter(), (req, res, next) => { +app.get('/get_state_proof', (req, res, next) => { const beginTime = Date.now(); const result = node.db.getStateProof(req.query.ref); const latency = Date.now() - beginTime; @@ -191,7 +192,7 @@ app.get('/get_state_proof', middleware.readLimiter(), (req, res, next) => { /** * Returns the state proof hash at the given full database path. */ -app.get('/get_proof_hash', middleware.readLimiter(), (req, res, next) => { +app.get('/get_proof_hash', (req, res, next) => { const beginTime = Date.now(); const result = node.db.getProofHash(req.query.ref); const latency = Date.now() - beginTime; @@ -208,7 +209,7 @@ app.get('/get_proof_hash', middleware.readLimiter(), (req, res, next) => { /** * Returns the state information at the given full database path. */ -app.get('/get_state_info', middleware.readLimiter(), (req, res, next) => { +app.get('/get_state_info', (req, res, next) => { const beginTime = Date.now(); const result = node.db.getStateInfo(req.query.ref); const latency = Date.now() - beginTime; @@ -225,7 +226,7 @@ app.get('/get_state_info', middleware.readLimiter(), (req, res, next) => { /** * Returns the state usage of the given app. */ -app.get('/get_state_usage', middleware.readLimiter(), (req, res, next) => { +app.get('/get_state_usage', (req, res, next) => { const beginTime = Date.now(); const result = node.getStateUsageWithStakingInfo(req.query.app_name); const latency = Date.now() - beginTime; @@ -239,7 +240,7 @@ app.get('/get_state_usage', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/match_function', middleware.readLimiter(), (req, res, next) => { +app.get('/match_function', (req, res, next) => { const beginTime = Date.now(); const result = node.db.matchFunction(req.query.ref, CommonUtil.toMatchOrEvalOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -253,7 +254,7 @@ app.get('/match_function', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/match_rule', middleware.readLimiter(), (req, res, next) => { +app.get('/match_rule', (req, res, next) => { const beginTime = Date.now(); const result = node.db.matchRule(req.query.ref, CommonUtil.toMatchOrEvalOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -267,7 +268,7 @@ app.get('/match_rule', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/match_owner', middleware.readLimiter(), (req, res, next) => { +app.get('/match_owner', (req, res, next) => { const beginTime = Date.now(); const result = node.db.matchOwner(req.query.ref, CommonUtil.toMatchOrEvalOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -281,7 +282,7 @@ app.get('/match_owner', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.post('/eval_rule', middleware.readLimiter(), (req, res, next) => { +app.post('/eval_rule', (req, res, next) => { const beginTime = Date.now(); const body = req.body; const auth = {}; @@ -305,7 +306,7 @@ app.post('/eval_rule', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.post('/eval_owner', middleware.readLimiter(), (req, res, next) => { +app.post('/eval_owner', (req, res, next) => { const beginTime = Date.now(); const body = req.body; const auth = {}; @@ -328,7 +329,7 @@ app.post('/eval_owner', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.post('/get', middleware.readLimiter(), (req, res, next) => { +app.post('/get', (req, res, next) => { const beginTime = Date.now(); const result = node.db.get(req.body.op_list); const latency = Date.now() - beginTime; @@ -342,7 +343,7 @@ app.post('/get', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/status', middleware.readLimiter(), (req, res, next) => { +app.get('/status', (req, res, next) => { const beginTime = Date.now(); const result = p2pClient.getStatus(); const latency = Date.now() - beginTime; @@ -353,7 +354,7 @@ app.get('/status', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/node_status', middleware.readLimiter(), (req, res, next) => { +app.get('/node_status', (req, res, next) => { const beginTime = Date.now(); const result = p2pServer.getNodeStatus(); const latency = Date.now() - beginTime; @@ -364,7 +365,7 @@ app.get('/node_status', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/connection_status', middleware.readLimiter(), (req, res) => { +app.get('/connection_status', (req, res) => { const beginTime = Date.now(); const result = p2pClient.getConnectionStatus(); const latency = Date.now() - beginTime; @@ -375,7 +376,7 @@ app.get('/connection_status', middleware.readLimiter(), (req, res) => { .end(); }) -app.get('/client_status', middleware.readLimiter(), (req, res) => { +app.get('/client_status', (req, res) => { const beginTime = Date.now(); const result = p2pClient.getClientStatus(); const latency = Date.now() - beginTime; @@ -386,7 +387,7 @@ app.get('/client_status', middleware.readLimiter(), (req, res) => { .end(); }) -app.get('/blocks', middleware.readLimiter(), (req, res, next) => { +app.get('/blocks', (req, res, next) => { const beginTime = Date.now(); const blockEnd = node.bc.lastBlockNumber() + 1; const blockBegin = Math.max(blockEnd - MAX_BLOCKS, 0); @@ -399,7 +400,7 @@ app.get('/blocks', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/last_block', middleware.readLimiter(), (req, res, next) => { +app.get('/last_block', (req, res, next) => { const beginTime = Date.now(); const result = node.bc.lastBlock(); const latency = Date.now() - beginTime; @@ -410,7 +411,7 @@ app.get('/last_block', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/tx_pool', middleware.readLimiter(), (req, res, next) => { +app.get('/tx_pool', (req, res, next) => { const beginTime = Date.now(); const result = Object.fromEntries(node.tp.transactions); const latency = Date.now() - beginTime; @@ -421,7 +422,7 @@ app.get('/tx_pool', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/tx_tracker', middleware.readLimiter(), (req, res, next) => { +app.get('/tx_tracker', (req, res, next) => { const beginTime = Date.now(); const result = Object.fromEntries(node.tp.transactionTracker); const latency = Date.now() - beginTime; @@ -432,7 +433,7 @@ app.get('/tx_tracker', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/committed_nonce_tracker', middleware.readLimiter(), (req, res, next) => { +app.get('/committed_nonce_tracker', (req, res, next) => { const beginTime = Date.now(); const result = node.tp.committedNonceTracker; const latency = Date.now() - beginTime; @@ -443,7 +444,7 @@ app.get('/committed_nonce_tracker', middleware.readLimiter(), (req, res, next) = .end(); }); -app.get('/pending_nonce_tracker', middleware.readLimiter(), (req, res, next) => { +app.get('/pending_nonce_tracker', (req, res, next) => { const beginTime = Date.now(); const result = node.tp.pendingNonceTracker; const latency = Date.now() - beginTime; @@ -454,7 +455,7 @@ app.get('/pending_nonce_tracker', middleware.readLimiter(), (req, res, next) => .end(); }); -app.get('/protocol_versions', middleware.readLimiter(), (req, res) => { +app.get('/protocol_versions', (req, res) => { const beginTime = Date.now(); const result = p2pClient.server.getProtocolInfo(); const latency = Date.now() - beginTime; @@ -465,7 +466,7 @@ app.get('/protocol_versions', middleware.readLimiter(), (req, res) => { .end(); }); -app.get('/state_versions', middleware.readLimiter(), (req, res) => { +app.get('/state_versions', (req, res) => { const beginTime = Date.now(); const result = p2pServer.getStateVersionStatus(); const latency = Date.now() - beginTime; @@ -477,7 +478,7 @@ app.get('/state_versions', middleware.readLimiter(), (req, res) => { }); // TODO(platfowner): Support for subtree snapshots (i.e. with ref path). -app.get('/get_final_state_snapshot', middleware.readLimiter(), (req, res) => { +app.get('/get_final_state_snapshot', (req, res) => { const beginTime = Date.now(); const result = node.takeFinalStateSnapshot(CommonUtil.toGetOptions(req.query, true)); const latency = Date.now() - beginTime; @@ -489,7 +490,7 @@ app.get('/get_final_state_snapshot', middleware.readLimiter(), (req, res) => { }); // TODO(platfowner): Support for subtree snapshots (i.e. with ref path). -app.get('/get_final_radix_snapshot', middleware.readLimiter(), (req, res) => { +app.get('/get_final_radix_snapshot', (req, res) => { const beginTime = Date.now(); const result = node.takeFinalRadixSnapshot(); const latency = Date.now() - beginTime; @@ -500,7 +501,7 @@ app.get('/get_final_radix_snapshot', middleware.readLimiter(), (req, res) => { .end(); }); -app.get('/tx_pool_size_util', middleware.readLimiter(), (req, res) => { +app.get('/tx_pool_size_util', (req, res) => { const beginTime = Date.now(); const address = req.query.address; const txPoolSizeUtil = node.getTxPoolSizeUtilization(address); @@ -512,7 +513,7 @@ app.get('/tx_pool_size_util', middleware.readLimiter(), (req, res) => { .end(); }); -app.get('/get_transaction', middleware.readLimiter(), (req, res, next) => { +app.get('/get_transaction', (req, res, next) => { const beginTime = Date.now(); const transactionInfo = node.getTransactionByHash(req.query.hash); const latency = Date.now() - beginTime; @@ -523,7 +524,7 @@ app.get('/get_transaction', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/get_block_by_hash', middleware.readLimiter(), (req, res, next) => { +app.get('/get_block_by_hash', (req, res, next) => { const beginTime = Date.now(); const block = node.bc.getBlockByHash(req.query.hash); const latency = Date.now() - beginTime; @@ -534,7 +535,7 @@ app.get('/get_block_by_hash', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/get_block_by_number', middleware.readLimiter(), (req, res) => { +app.get('/get_block_by_number', (req, res) => { const beginTime = Date.now(); const block = node.bc.getBlockByNumber(req.query.number); const latency = Date.now() - beginTime; @@ -545,7 +546,7 @@ app.get('/get_block_by_number', middleware.readLimiter(), (req, res) => { .end(); }); -app.get('/get_block_info_by_number', middleware.readLimiter(), (req, res) => { +app.get('/get_block_info_by_number', (req, res) => { const beginTime = Date.now(); const blockInfo = node.bc.getBlockInfoByNumber(req.query.number); const latency = Date.now() - beginTime; @@ -556,7 +557,7 @@ app.get('/get_block_info_by_number', middleware.readLimiter(), (req, res) => { .end(); }); -app.get('/get_address', middleware.readLimiter(), (req, res, next) => { +app.get('/get_address', (req, res, next) => { const beginTime = Date.now(); const result = node.account ? node.account.address : null; const latency = Date.now() - beginTime; @@ -567,7 +568,7 @@ app.get('/get_address', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/get_nonce', middleware.readLimiter(), (req, res, next) => { +app.get('/get_nonce', (req, res, next) => { const beginTime = Date.now(); const result = node.getNonceForAddr(req.query.address, req.query.from === 'pending'); const latency = Date.now() - beginTime; @@ -578,7 +579,7 @@ app.get('/get_nonce', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/get_timestamp', middleware.readLimiter(), (req, res, next) => { +app.get('/get_timestamp', (req, res, next) => { const beginTime = Date.now(); const result = node.getTimestampForAddr(req.query.address, req.query.from === 'pending'); const latency = Date.now() - beginTime; @@ -589,7 +590,7 @@ app.get('/get_timestamp', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/validate_app_name', middleware.readLimiter(), (req, res, next) => { +app.get('/validate_app_name', (req, res, next) => { const beginTime = Date.now(); const result = node.validateAppName(req.query.app_name); const latency = Date.now() - beginTime; @@ -600,7 +601,7 @@ app.get('/validate_app_name', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/get_sharding', middleware.readLimiter(), (req, res, next) => { +app.get('/get_sharding', (req, res, next) => { const beginTime = Date.now(); const result = node.getSharding(); const latency = Date.now() - beginTime; @@ -614,7 +615,7 @@ app.get('/get_sharding', middleware.readLimiter(), (req, res, next) => { .end(); }); -app.get('/get_raw_consensus_status', middleware.readLimiter(), (req, res) => { +app.get('/get_raw_consensus_status', (req, res) => { const beginTime = Date.now(); const result = p2pServer.consensus.getRawStatus(); const latency = Date.now() - beginTime; @@ -625,7 +626,7 @@ app.get('/get_raw_consensus_status', middleware.readLimiter(), (req, res) => { .end(); }); -app.get('/get_consensus_status', middleware.readLimiter(), (req, res) => { +app.get('/get_consensus_status', (req, res) => { const beginTime = Date.now(); const result = p2pServer.consensus.getStatus(); const latency = Date.now() - beginTime; @@ -636,7 +637,7 @@ app.get('/get_consensus_status', middleware.readLimiter(), (req, res) => { .end(); }); -app.get('/get_network_id', middleware.readLimiter(), (req, res) => { +app.get('/get_network_id', (req, res) => { const beginTime = Date.now(); const result = p2pServer.node.getBlockchainParam('genesis/network_id'); const latency = Date.now() - beginTime; @@ -647,7 +648,7 @@ app.get('/get_network_id', middleware.readLimiter(), (req, res) => { .end(); }); -app.get('/get_chain_id', middleware.readLimiter(), (req, res) => { +app.get('/get_chain_id', (req, res) => { const beginTime = Date.now(); const result = p2pServer.node.getBlockchainParam('genesis/chain_id'); const latency = Date.now() - beginTime; @@ -658,7 +659,7 @@ app.get('/get_chain_id', middleware.readLimiter(), (req, res) => { .end(); }); -app.get('/get_config', middleware.readLimiter(), (req, res) => { +app.get('/get_config', (req, res) => { const beginTime = Date.now(); const result = p2pClient.getConfig(); const latency = Date.now() - beginTime; @@ -674,7 +675,7 @@ app.get('/get_config', middleware.readLimiter(), (req, res) => { */ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { - app.post('/set_value', middleware.writeLimiter(), (req, res, next) => { + app.post('/set_value', (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.SET_VALUE)); @@ -690,7 +691,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/inc_value', middleware.writeLimiter(), (req, res, next) => { + app.post('/inc_value', (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.INC_VALUE)); @@ -706,7 +707,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/dec_value', middleware.writeLimiter(), (req, res, next) => { + app.post('/dec_value', (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.DEC_VALUE)); @@ -722,7 +723,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/set_function', middleware.writeLimiter(), (req, res, next) => { + app.post('/set_function', (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.SET_FUNCTION)); @@ -738,7 +739,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/set_rule', middleware.writeLimiter(), (req, res, next) => { + app.post('/set_rule', (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.SET_RULE)); @@ -754,7 +755,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/set_owner', middleware.writeLimiter(), (req, res, next) => { + app.post('/set_owner', (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createSingleSetTxBody( req.body, WriteDbOperations.SET_OWNER)); @@ -772,7 +773,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { // A custom address can be used as a devel method for bypassing the trasaction verification. // TODO(platfowner): Replace custom address with real signature. - app.post('/set', middleware.writeLimiter(), (req, res, next) => { + app.post('/set', (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createMultiSetTxBody(req.body)); const latency = Date.now() - beginTime; @@ -787,7 +788,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/batch', middleware.writeLimiter(), (req, res, next) => { + app.post('/batch', (req, res, next) => { const beginTime = Date.now(); const result = createAndExecuteTransaction(createBatchTxBody(req.body)); const latency = Date.now() - beginTime; @@ -798,7 +799,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }); - app.post('/sign_transaction', middleware.writeLimiter(), (req, res) => { + app.post('/sign_transaction', (req, res) => { const beginTime = Date.now(); const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.CLIENT_API_SET, latency); @@ -808,7 +809,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { .end(); }) - app.post('/broadcast_consensus_msg', middleware.writeLimiter(), (req, res) => { + app.post('/broadcast_consensus_msg', (req, res) => { const beginTime = Date.now(); p2pClient.broadcastConsensusMessage(req.body); const latency = Date.now() - beginTime; @@ -822,7 +823,7 @@ if (NodeConfigs.ENABLE_DEV_CLIENT_SET_API) { if (eventHandler) { // NOTE(cshcomcom): For event handler load balancer! It doesn't mean healthy. - app.get('/eh_load_balancer_health_check', middleware.readLimiter(), (req, res, next) => { + app.get('/eh_load_balancer_health_check', (req, res, next) => { const result = eventHandler.getEventHandlerHealth(); res.status(200) .set('Content-Type', 'text/plain') From 9f4e4dbe771cb801237a3ac51857b3e4f7951e3d Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Fri, 13 May 2022 11:26:57 +0900 Subject: [PATCH 023/166] Rename node params --- blockchain-configs/afan-shard/node_params.json | 5 +++-- blockchain-configs/base/node_params.json | 5 +++-- blockchain-configs/he-shard/node_params.json | 5 +++-- blockchain-configs/mainnet-prod/node_params.json | 5 +++-- blockchain-configs/sim-shard/node_params.json | 5 +++-- blockchain-configs/testnet-dev/node_params.json | 5 +++-- blockchain-configs/testnet-exp/node_params.json | 5 +++-- blockchain-configs/testnet-prod/node_params.json | 5 +++-- blockchain-configs/testnet-sandbox/node_params.json | 5 +++-- blockchain-configs/testnet-staging/node_params.json | 5 +++-- 10 files changed, 30 insertions(+), 20 deletions(-) diff --git a/blockchain-configs/afan-shard/node_params.json b/blockchain-configs/afan-shard/node_params.json index 092c93ae3..f21fb9717 100644 --- a/blockchain-configs/afan-shard/node_params.json +++ b/blockchain-configs/afan-shard/node_params.json @@ -43,8 +43,9 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 3, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 10, - "MAX_WRITE_RATE_LIMIT": 1, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/base/node_params.json b/blockchain-configs/base/node_params.json index ef6bccdeb..cfc2826b8 100644 --- a/blockchain-configs/base/node_params.json +++ b/blockchain-configs/base/node_params.json @@ -43,8 +43,9 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 10, - "MAX_WRITE_RATE_LIMIT": 1, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/he-shard/node_params.json b/blockchain-configs/he-shard/node_params.json index 4898e3e7d..a97010da4 100644 --- a/blockchain-configs/he-shard/node_params.json +++ b/blockchain-configs/he-shard/node_params.json @@ -43,8 +43,9 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 10, - "MAX_WRITE_RATE_LIMIT": 1, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/mainnet-prod/node_params.json b/blockchain-configs/mainnet-prod/node_params.json index 9e527f90d..1525852dc 100644 --- a/blockchain-configs/mainnet-prod/node_params.json +++ b/blockchain-configs/mainnet-prod/node_params.json @@ -42,8 +42,9 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 10, - "MAX_WRITE_RATE_LIMIT": 1, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/sim-shard/node_params.json b/blockchain-configs/sim-shard/node_params.json index 2ab8a2783..b48760822 100644 --- a/blockchain-configs/sim-shard/node_params.json +++ b/blockchain-configs/sim-shard/node_params.json @@ -43,8 +43,9 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 10, - "MAX_WRITE_RATE_LIMIT": 1, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-dev/node_params.json b/blockchain-configs/testnet-dev/node_params.json index 975bee5dd..c43acbd40 100644 --- a/blockchain-configs/testnet-dev/node_params.json +++ b/blockchain-configs/testnet-dev/node_params.json @@ -43,8 +43,9 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 10, - "MAX_WRITE_RATE_LIMIT": 1, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-exp/node_params.json b/blockchain-configs/testnet-exp/node_params.json index 178572233..ed9d0fac9 100644 --- a/blockchain-configs/testnet-exp/node_params.json +++ b/blockchain-configs/testnet-exp/node_params.json @@ -43,8 +43,9 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 10, - "MAX_WRITE_RATE_LIMIT": 1, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-prod/node_params.json b/blockchain-configs/testnet-prod/node_params.json index 7a1365e6b..a24774c82 100644 --- a/blockchain-configs/testnet-prod/node_params.json +++ b/blockchain-configs/testnet-prod/node_params.json @@ -43,8 +43,9 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 10, - "MAX_WRITE_RATE_LIMIT": 1, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-sandbox/node_params.json b/blockchain-configs/testnet-sandbox/node_params.json index d78b9ea5e..302094f7d 100644 --- a/blockchain-configs/testnet-sandbox/node_params.json +++ b/blockchain-configs/testnet-sandbox/node_params.json @@ -43,8 +43,9 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 10, - "MAX_WRITE_RATE_LIMIT": 1, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-staging/node_params.json b/blockchain-configs/testnet-staging/node_params.json index 9d8bbcb89..4a0c66ed7 100644 --- a/blockchain-configs/testnet-staging/node_params.json +++ b/blockchain-configs/testnet-staging/node_params.json @@ -43,8 +43,9 @@ "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_READ_RATE_LIMIT": 10, - "MAX_WRITE_RATE_LIMIT": 1, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, From 0bde910f63637217deec7615940bcb32c3c55760 Mon Sep 17 00:00:00 2001 From: pengin7384 Date: Fri, 13 May 2022 15:24:41 +0900 Subject: [PATCH 024/166] Make USER event to send only by JSON_RPC --- node/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/index.js b/node/index.js index 4444ba50f..111b9efbb 100644 --- a/node/index.js +++ b/node/index.js @@ -1065,7 +1065,7 @@ class BlockchainNode { if (NodeConfigs.UPDATE_NEW_FINAL_FRONT_DB_WITH_TX_POOL) { // Apply the txs from the tx pool to the new final front db. this.executeAndGetValidTransactions( - null, lastFinalizedBlock.number, lastFinalizedBlock.timestamp, this.db, true, ValueChangedEventSources.USER); + null, lastFinalizedBlock.number, lastFinalizedBlock.timestamp, this.db, true); } // Clean up block pool this.bp.cleanUpAfterFinalization(this.bc.lastBlock(), recordedInvalidBlocks); From db57782aff4c59ebb8f1f97653337a314f483769 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Fri, 13 May 2022 15:25:19 +0900 Subject: [PATCH 025/166] Set blockchain api rate limit as 20 sec --- client/index.js | 4 ++-- client/middleware.js | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/client/index.js b/client/index.js index 2fc70641c..d67789737 100755 --- a/client/index.js +++ b/client/index.js @@ -2,6 +2,7 @@ const logger = new (require('../logger'))('CLIENT'); +const _ = require('lodash'); const express = require('express'); // NOTE(liayoo): To use async/await (ref: https://github.com/tedeh/jayson#promises) const jayson = require('jayson/promise'); @@ -27,12 +28,11 @@ const MAX_BLOCKS = 20; const app = express(); // NOTE(minsulee2): complex express middleware is now built at middleware.js const middleware = new Middleware(); -middleware.printAll(); app.use(middleware.expressJsonRequestBodySizeLimiter()); app.use(middleware.expressUrlencdedRequestBodySizeLimiter()); app.use(middleware.corsLimiter()); app.use(middleware.ipWhitelistLimiter()); -// app.use(middleware.test()); +app.use(middleware.blockchainApiLimiter()); const eventHandler = NodeConfigs.ENABLE_EVENT_HANDLER === true ? new EventHandler() : null; const node = new BlockchainNode(null, eventHandler); diff --git a/client/middleware.js b/client/middleware.js index a10414127..ad2fa3a6e 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -15,9 +15,11 @@ const { JSON_RPC_METHOD } = require('../json_rpc/constants'); class Middleware { constructor () { + this.minuteAsSeconds = 60; this.setExpressRequestBodySizeLimit(); this.setCorsOriginList(); this.setDevClientApiIpWhitelist(); + this.setBlockchainApiRateLimit(); this.setReadRateLimit(); this.setWriteRateLimit(); } @@ -44,13 +46,18 @@ class Middleware { return this; } + setBlockchainApiRateLimit() { + this.blockchainApiRateLimit = NodeConfigs.MAX_BLOCKCHAIN_API_RATE_LIMIT; + return this; + } + setReadRateLimit() { - this.readRateLimit = NodeConfigs.MAX_READ_RATE_LIMIT; + this.readRateLimit = NodeConfigs.MAX_JSON_RPC_API_READ_RATE_LIMIT; return this; } setWriteRateLimit() { - this.writeRateLimit = NodeConfigs.MAX_WRITE_RATE_LIMIT; + this.writeRateLimit = NodeConfigs.MAX_JSON_RPC_API_WRITE_RATE_LIMIT; return this; } @@ -66,6 +73,10 @@ class Middleware { return this.devClientApiIpWhitelist; } + getBlockchainApiRateLimit() { + return this.blockchainApiRateLimit; + } + getReadRateLimit() { return this.readRateLimit; } @@ -98,20 +109,28 @@ class Middleware { }) } + blockchainApiLimiter() { + return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? + rateLimit({ + windowMs: this.minuteAsSeconds * 1000, // 1 minute + max: this.minuteAsSeconds * this.getBlockchainApiRateLimit() + }) : this._emptyHandler(); + } + readLimiter() { return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? - rateLimit({ - windowMs: 1000, // 1 second - max: this.getReadRateLimit() // limit each IP to maximum of read rate limit - }) : this._emptyHandler(); + rateLimit({ + windowMs: this.minuteAsSeconds * 1000, // 1 minute + max: this.minuteAsSeconds * this.getReadRateLimit() + }) : this._emptyHandler(); } writeLimiter() { return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? - rateLimit({ - windowMs: 1000, // 1 second - max: this.getWriteRateLimit() // limit each IP to maximum of write rate limit - }) : this._emptyHandler(); + rateLimit({ + windowMs: this.minuteAsSeconds * 1000, // 1 minute + max: this.minuteAsSeconds * this.getWriteRateLimit() + }) : this._emptyHandler(); } jsonRpcLimiter(req, res, next) { @@ -135,7 +154,7 @@ class Middleware { console.log(this.getCorsOriginList()); console.log(this.getDevClientApiIpWhitelist()); console.log(this.getExpressRequestBodySizeLimit()); - console.log(this.getReadRateLimit(), this.getWriteRateLimit()); + console.log(this.getBlockchainApiRateLimit(), this.getReadRateLimit(), this.getWriteRateLimit()); } } From 47dbf9e598f163d0b9759bd4a83c87c2cb5ccb9c Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Fri, 13 May 2022 21:15:31 +0900 Subject: [PATCH 026/166] Test read and write limiters --- client/index.js | 2 +- client/middleware.js | 56 ++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/client/index.js b/client/index.js index d67789737..de547271a 100755 --- a/client/index.js +++ b/client/index.js @@ -60,7 +60,7 @@ const jsonRpcApis = require('../json_rpc')( app.post( '/json-rpc', VersionUtil.validateVersion.bind({ minProtocolVersion, maxProtocolVersion }), - middleware.jsonRpcLimiter(), + middleware.jsonRpcLimiter, jayson.server(jsonRpcApis).middleware() ); diff --git a/client/middleware.js b/client/middleware.js index ad2fa3a6e..af19bb087 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -109,7 +109,7 @@ class Middleware { }) } - blockchainApiLimiter() { + blockchainApiLimiter = () => { return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? rateLimit({ windowMs: this.minuteAsSeconds * 1000, // 1 minute @@ -117,36 +117,40 @@ class Middleware { }) : this._emptyHandler(); } - readLimiter() { - return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? - rateLimit({ - windowMs: this.minuteAsSeconds * 1000, // 1 minute - max: this.minuteAsSeconds * this.getReadRateLimit() - }) : this._emptyHandler(); + readLimiter = () => { + console.log('read'); + return rateLimit({ + windowMs: this.minuteAsSeconds * 1000, // 1 minute + max: 5 + }); } - writeLimiter() { - return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? - rateLimit({ - windowMs: this.minuteAsSeconds * 1000, // 1 minute - max: this.minuteAsSeconds * this.getWriteRateLimit() - }) : this._emptyHandler(); + writeLimiter = () => { + console.log('write'); + return rateLimit({ + windowMs: this.minuteAsSeconds * 1000, // 1 minute + max: 3 + }); } - jsonRpcLimiter(req, res, next) { + jsonRpcLimiter = (req, res, next) => { const jsonRpcMethod = _.get(req, 'body.method'); - switch (jsonRpcMethod) { - case JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: - case JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: - case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: - case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_KEYSTORE: - case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_HD_WALLET: - case JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION: - case JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH: - return this.writeLimiter(); - default: - return this.readLimiter(); - } + console.log(jsonRpcMethod); + console.log(111) + switch (jsonRpcMethod) { + case JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: + case JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: + case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: + case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_KEYSTORE: + case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_HD_WALLET: + case JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION: + case JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH: + case JSON_RPC_METHOD.AIN_GET_LAST_BLOCK_NUMBER: + console.log(22) + return this.readLimiter()(req, res, next); + default: + return this.writeLimiter()(req, res, next); + } } // NOTE(minsulee2): debugging purpose From b1bde6129336442fefb95522ff0f59c21c40a150 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 16 May 2022 17:12:07 +0900 Subject: [PATCH 027/166] Fix test integration blockchain --- test/integration/blockchain.test.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/integration/blockchain.test.js b/test/integration/blockchain.test.js index 3a8b1df1d..c03c91b7d 100644 --- a/test/integration/blockchain.test.js +++ b/test/integration/blockchain.test.js @@ -555,9 +555,9 @@ describe('Blockchain Cluster', () => { it('pendingNonceTracker', () => { return new Promise((resolve, reject) => { let promises = []; - promises.push(jsonRpcClient.request(JSON_RPC_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); - promises.push(jsonRpcClient.request(JSON_RPC_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); Promise.all(promises).then(res => { promises = []; @@ -570,9 +570,9 @@ describe('Blockchain Cluster', () => { value: 'testing...' } }).body.toString('utf-8')).result.tx_hash; - promises.push(jsonRpcClient.request(JSON_RPC_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); - promises.push(jsonRpcClient.request(JSON_RPC_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); Promise.all(promises).then(async resAfterBroadcast => { promises = []; @@ -595,9 +595,9 @@ describe('Blockchain Cluster', () => { return new Promise(async (resolve, reject) => { await waitForNewBlocks(server2); let promises = []; - promises.push(jsonRpcClient.request(JSON_RPC_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); - promises.push(jsonRpcClient.request(JSON_RPC_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, { address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); Promise.all(promises).then(resAfterCommit => { const committedNonceAfterCommit = resAfterCommit[0].result.result; @@ -689,7 +689,7 @@ describe('Blockchain Cluster', () => { describe('Protocol versions', () => { it('accepts API calls with correct protoVer', () => { - return jsonRpcClient.request(JSON_RPC_GET_BLOCK_BY_NUMBER, + return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, { number: 0, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) .then(res => { expect(res.result.result.number).to.equal(0); @@ -699,7 +699,7 @@ describe('Blockchain Cluster', () => { it('rejects API calls with invalid protoVer - case 1', () => { return jsonRpcClient.request( - JSON_RPC_GET_BLOCK_BY_NUMBER, + JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, { number: 0, protoVer: 'a.b.c' }) .then(res => { expect(res.code).to.equal(40102); @@ -709,7 +709,7 @@ describe('Blockchain Cluster', () => { it('rejects API calls with invalid protoVer - case 2', () => { return jsonRpcClient.request( - JSON_RPC_GET_BLOCK_BY_NUMBER, + JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, { number: 0, protoVer: '0.01.0' }) .then(res => { expect(res.code).to.equal(40102); @@ -719,7 +719,7 @@ describe('Blockchain Cluster', () => { it('rejects API calls with incompatible protoVer - case 1', () => { return jsonRpcClient.request( - JSON_RPC_GET_BLOCK_BY_NUMBER, + JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, { number: 0, protoVer: 'v0.1' }) .then(res => { expect(res.code).to.equal(40103); @@ -729,7 +729,7 @@ describe('Blockchain Cluster', () => { it('rejects API calls with incompatible protoVer - case 2', () => { return jsonRpcClient.request( - JSON_RPC_GET_BLOCK_BY_NUMBER, + JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, { number: 0, protoVer: '0.1.0' }) .then(res => { expect(res.code).to.equal(40103); @@ -739,7 +739,7 @@ describe('Blockchain Cluster', () => { it('rejects API calls with no protoVer', () => { return jsonRpcClient.request( - JSON_RPC_GET_BLOCK_BY_NUMBER, + JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, { number: 0 }) .then(res => { expect(res.code).to.equal(40101); From 882774f949db6d954e3e05291638585992850fdd Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Tue, 17 May 2022 00:16:20 +0900 Subject: [PATCH 028/166] Set read and write limit differently --- client/middleware.js | 54 +++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/client/middleware.js b/client/middleware.js index af19bb087..ddfcf28ad 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -22,12 +22,14 @@ class Middleware { this.setBlockchainApiRateLimit(); this.setReadRateLimit(); this.setWriteRateLimit(); - } - - _emptyHandler() { - return (req, res, next) => { - return next(); - } + this.jsonRpcReadLimiter = rateLimit({ + windowMs: this.minuteAsSeconds * 1000, // 1 minute + max: this.minuteAsSeconds * this.getReadRateLimit() + }); + this.jsonRpcWriteLimiter = rateLimit({ + windowMs: this.minuteAsSeconds * 1000, // 1 minute + max: this.minuteAsSeconds * this.getWriteRateLimit() + }); } setExpressRequestBodySizeLimit() { @@ -109,34 +111,25 @@ class Middleware { }) } - blockchainApiLimiter = () => { - return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? - rateLimit({ - windowMs: this.minuteAsSeconds * 1000, // 1 minute - max: this.minuteAsSeconds * this.getBlockchainApiRateLimit() - }) : this._emptyHandler(); - } - - readLimiter = () => { - console.log('read'); - return rateLimit({ - windowMs: this.minuteAsSeconds * 1000, // 1 minute - max: 5 - }); + _emptyHandler = () => { + return (req, res, next) => { + return next(); + } } - writeLimiter = () => { - console.log('write'); - return rateLimit({ - windowMs: this.minuteAsSeconds * 1000, // 1 minute - max: 3 - }); + blockchainApiLimiter = () => { + return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? + rateLimit({ + windowMs: this.minuteAsSeconds * 1000, // 1 minute window + max: this.minuteAsSeconds * this.getBlockchainApiRateLimit() + }) : this._emptyHandler(); } jsonRpcLimiter = (req, res, next) => { + if (!NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT) { + return next(); + } const jsonRpcMethod = _.get(req, 'body.method'); - console.log(jsonRpcMethod); - console.log(111) switch (jsonRpcMethod) { case JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: case JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: @@ -146,10 +139,9 @@ class Middleware { case JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION: case JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH: case JSON_RPC_METHOD.AIN_GET_LAST_BLOCK_NUMBER: - console.log(22) - return this.readLimiter()(req, res, next); + return this.jsonRpcWriteLimiter(req, res, next); default: - return this.writeLimiter()(req, res, next); + return this.jsonRpcReadLimiter(req, res, next); } } From 4b19e0f98a027dcca3a134c653c909433a677cb4 Mon Sep 17 00:00:00 2001 From: kriii Date: Tue, 17 May 2022 11:56:51 +0900 Subject: [PATCH 029/166] Change deploy_docker.sh --- README.md | 12 ++++++------ deploy_docker.sh | 48 ++++++++++++++++-------------------------------- 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 896a6a993..ec9707cd2 100644 --- a/README.md +++ b/README.md @@ -152,21 +152,21 @@ bash start_node_genesis_gcp.sh {dev|spring|summer} - Pull Docker image from [Docker Hub](https://hub.docker.com/repository/docker/ainblockchain/ain-blockchain) ``` -docker pull ainblockchain/ain-blockchain:dev -docker pull ainblockchain/ain-blockchain:dev-1.0.6 -docker pull ainblockchain/ain-blockchain:{mainnet|summer|spring|sandbox|staging|exp|dev}- +docker pull ainblockchain/ain-blockchain:latest +docker pull ainblockchain/ain-blockchain: ``` - Or build Docker image yourself ``` -docker build -t ain-blockchain --build-arg SEASON={mainnet|summer|spring|sandbox|staging|exp|dev} . +docker build -t ain-blockchain . ``` - Run with Docker image example ``` -docker run -e ACCOUNT_INJECTION_OPTION=private_key -e SYNC_MODE=peer -e STAKE=10000 --network="host" -d ainblockchain/ain-blockchain:dev -docker run -e ACCOUNT_INJECTION_OPTION=keystore -e SYNC_MODE=peer -e STAKE=10000 --network="host" -d ainblockchain/ain-blockchain:mainnet +docker run -e ACCOUNT_INJECTION_OPTION=private_key -e SYNC_MODE=peer -e STAKE=10000 -e SEASON=dev --network="host" -d ainblockchain/ain-blockchain:latest +docker run -e ACCOUNT_INJECTION_OPTION=keystore -e SYNC_MODE=peer -e STAKE=10000 -e SEASON=mainnet --network="host" -d ainblockchain/ain-blockchain:latest ``` You can use some environment variables, and these have the following options. ``` +-e SEASON={mainnet|summer|spring|sandbox|staging|exp|dev} -e ACCOUNT_INJECTION_OPTION={private_key|keystore|mnemonic} -e SYNC_MODE={fast|full|peer} -e STAKE= diff --git a/deploy_docker.sh b/deploy_docker.sh index b18a2560b..807155a96 100644 --- a/deploy_docker.sh +++ b/deploy_docker.sh @@ -1,47 +1,31 @@ #!/bin/bash -if [[ "$#" -lt 1 ]]; then - printf "Usage: bash deploy_docker.sh [dev|staging|sandbox|exp|spring|summer|mainnet]\n" - printf "Example: bash deploy_docker.sh dev\n" +if [[ "$#" -lt 0 ]]; then + printf "Usage: bash deploy_docker.sh\n" + printf "Example: bash deploy_docker.sh\n" printf "\n" exit fi printf "\n[[[[[ deploy_docker.sh ]]]]]\n\n" -if [[ "$1" != 'dev' ]] && [[ "$1" != 'staging' ]] && [[ "$1" != 'sandbox' ]] && [[ "$1" != 'exp' ]] && [[ "$1" != 'spring' ]] && [[ "$1" != 'summer' ]] && [[ "$1" != 'mainnet' ]]; then - printf "Invalid season argument: $1\n" - exit -fi - -SEASON="$1" - # Get confirmation. -if [[ "$SEASON" = "mainnet" ]]; then - printf "\n" - printf "Do you want to proceed for $SEASON? Enter [mainnet]: " - read CONFIRM - printf "\n\n" - if [[ ! $CONFIRM = "mainnet" ]] - then - [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell - fi -else - printf "\n" - read -p "Do you want to proceed for $SEASON? [y/N]: " -n 1 -r - printf "\n\n" - if [[ ! $REPLY =~ ^[Yy]$ ]]; then - [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell - fi +printf "\n" +printf "Do you want to proceed? Enter [deploy]: " +read CONFIRM +printf "\n\n" +if [[ ! $CONFIRM = "deploy" ]] +then + [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell fi -IMAGE_NAME=ainblockchain/ain-blockchain:$SEASON PACKAGE_VERSION=$(jq -r '.version' < package.json) -IMAGE_NAME_WITH_VERSION=$IMAGE_NAME-$PACKAGE_VERSION +IMAGE_NAME=ainblockchain/ain-blockchain:$PACKAGE_VERSION +IMAGE_NAME_LATEST=ainblockchain/ain-blockchain:latest docker login -docker build -t $IMAGE_NAME --build-arg SEASON=$SEASON . -docker tag $IMAGE_NAME $IMAGE_NAME_WITH_VERSION +docker build -t $IMAGE_NAME . +docker tag $IMAGE_NAME $IMAGE_NAME_LATEST docker push $IMAGE_NAME -docker push $IMAGE_NAME_WITH_VERSION +docker push $IMAGE_NAME_LATEST docker image rm $IMAGE_NAME -docker image rm $IMAGE_NAME_WITH_VERSION +docker image rm $IMAGE_NAME_LATEST From 856d26d0487fbf2b9112cd5c3b038c1f91ff3c8b Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Tue, 17 May 2022 18:36:26 +0900 Subject: [PATCH 030/166] Reorder MAX_BLOCKCHAIN_API_RATE_LIMIT --- blockchain-configs/afan-shard/node_params.json | 2 +- blockchain-configs/base/node_params.json | 2 +- blockchain-configs/he-shard/node_params.json | 2 +- blockchain-configs/mainnet-prod/node_params.json | 2 +- blockchain-configs/sim-shard/node_params.json | 2 +- blockchain-configs/testnet-dev/node_params.json | 2 +- blockchain-configs/testnet-exp/node_params.json | 2 +- blockchain-configs/testnet-prod/node_params.json | 2 +- blockchain-configs/testnet-sandbox/node_params.json | 2 +- blockchain-configs/testnet-staging/node_params.json | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/blockchain-configs/afan-shard/node_params.json b/blockchain-configs/afan-shard/node_params.json index f21fb9717..d9789cb41 100644 --- a/blockchain-configs/afan-shard/node_params.json +++ b/blockchain-configs/afan-shard/node_params.json @@ -37,13 +37,13 @@ "GET_RESP_MAX_SIBLINGS": 50000, "HOSTING_ENV": "local", "LIGHTWEIGHT": false, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 3, "MAX_NUM_SNAPSHOTS": 10, - "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, diff --git a/blockchain-configs/base/node_params.json b/blockchain-configs/base/node_params.json index cfc2826b8..c765a5447 100644 --- a/blockchain-configs/base/node_params.json +++ b/blockchain-configs/base/node_params.json @@ -37,13 +37,13 @@ "GET_RESP_MAX_SIBLINGS": 50000, "HOSTING_ENV": "local", "LIGHTWEIGHT": false, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, diff --git a/blockchain-configs/he-shard/node_params.json b/blockchain-configs/he-shard/node_params.json index a97010da4..4b4f58734 100644 --- a/blockchain-configs/he-shard/node_params.json +++ b/blockchain-configs/he-shard/node_params.json @@ -37,13 +37,13 @@ "GET_RESP_MAX_SIBLINGS": 50000, "HOSTING_ENV": "local", "LIGHTWEIGHT": false, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, diff --git a/blockchain-configs/mainnet-prod/node_params.json b/blockchain-configs/mainnet-prod/node_params.json index 1525852dc..d9d2d707a 100644 --- a/blockchain-configs/mainnet-prod/node_params.json +++ b/blockchain-configs/mainnet-prod/node_params.json @@ -36,13 +36,13 @@ "GET_RESP_MAX_SIBLINGS": 50000, "HOSTING_ENV": "gcp", "LIGHTWEIGHT": false, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, diff --git a/blockchain-configs/sim-shard/node_params.json b/blockchain-configs/sim-shard/node_params.json index b48760822..de32478cb 100644 --- a/blockchain-configs/sim-shard/node_params.json +++ b/blockchain-configs/sim-shard/node_params.json @@ -37,13 +37,13 @@ "GET_RESP_MAX_SIBLINGS": 50000, "HOSTING_ENV": "local", "LIGHTWEIGHT": false, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, diff --git a/blockchain-configs/testnet-dev/node_params.json b/blockchain-configs/testnet-dev/node_params.json index c43acbd40..329dadf17 100644 --- a/blockchain-configs/testnet-dev/node_params.json +++ b/blockchain-configs/testnet-dev/node_params.json @@ -37,13 +37,13 @@ "GET_RESP_MAX_SIBLINGS": 50000, "HOSTING_ENV": "gcp", "LIGHTWEIGHT": false, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, diff --git a/blockchain-configs/testnet-exp/node_params.json b/blockchain-configs/testnet-exp/node_params.json index ed9d0fac9..7aafc0382 100644 --- a/blockchain-configs/testnet-exp/node_params.json +++ b/blockchain-configs/testnet-exp/node_params.json @@ -37,13 +37,13 @@ "GET_RESP_MAX_SIBLINGS": 50000, "HOSTING_ENV": "gcp", "LIGHTWEIGHT": false, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, diff --git a/blockchain-configs/testnet-prod/node_params.json b/blockchain-configs/testnet-prod/node_params.json index a24774c82..d23a403c2 100644 --- a/blockchain-configs/testnet-prod/node_params.json +++ b/blockchain-configs/testnet-prod/node_params.json @@ -37,13 +37,13 @@ "GET_RESP_MAX_SIBLINGS": 50000, "HOSTING_ENV": "gcp", "LIGHTWEIGHT": false, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, diff --git a/blockchain-configs/testnet-sandbox/node_params.json b/blockchain-configs/testnet-sandbox/node_params.json index 302094f7d..005dd21b8 100644 --- a/blockchain-configs/testnet-sandbox/node_params.json +++ b/blockchain-configs/testnet-sandbox/node_params.json @@ -37,13 +37,13 @@ "GET_RESP_MAX_SIBLINGS": 50000, "HOSTING_ENV": "gcp", "LIGHTWEIGHT": false, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, diff --git a/blockchain-configs/testnet-staging/node_params.json b/blockchain-configs/testnet-staging/node_params.json index 4a0c66ed7..dbb432dfd 100644 --- a/blockchain-configs/testnet-staging/node_params.json +++ b/blockchain-configs/testnet-staging/node_params.json @@ -37,13 +37,13 @@ "GET_RESP_MAX_SIBLINGS": 50000, "HOSTING_ENV": "gcp", "LIGHTWEIGHT": false, + "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, From 8674ea6a7654cb83392baa63873fbc8d973f345f Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Tue, 17 May 2022 18:40:33 +0900 Subject: [PATCH 031/166] Use NodeConfigs.REQUEST_BODY_SIZE_LIMIT directly --- client/middleware.js | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/client/middleware.js b/client/middleware.js index ddfcf28ad..ae2df5388 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -16,7 +16,6 @@ const { JSON_RPC_METHOD } = require('../json_rpc/constants'); class Middleware { constructor () { this.minuteAsSeconds = 60; - this.setExpressRequestBodySizeLimit(); this.setCorsOriginList(); this.setDevClientApiIpWhitelist(); this.setBlockchainApiRateLimit(); @@ -32,11 +31,6 @@ class Middleware { }); } - setExpressRequestBodySizeLimit() { - this.expressRequestBodySizeLimit = NodeConfigs.REQUEST_BODY_SIZE_LIMIT; - return this; - } - setCorsOriginList() { this.corsOriginList = NodeConfigs.CORS_WHITELIST === '*' ? NodeConfigs.CORS_WHITELIST : getRegexpList(NodeConfigs.CORS_WHITELIST); @@ -63,10 +57,6 @@ class Middleware { return this; } - getExpressRequestBodySizeLimit() { - return this.expressRequestBodySizeLimit; - } - getCorsOriginList() { return this.corsOriginList; } @@ -88,13 +78,13 @@ class Middleware { } expressJsonRequestBodySizeLimiter() { - return express.json({ limit: this.getExpressRequestBodySizeLimit() }); + return express.json({ limit: NodeConfigs.REQUEST_BODY_SIZE_LIMIT }); } expressUrlencdedRequestBodySizeLimiter() { return express.urlencoded({ extended: true, - limit: this.getExpressRequestBodySizeLimit() + limit: NodeConfigs.REQUEST_BODY_SIZE_LIMIT }); } @@ -149,7 +139,6 @@ class Middleware { printAll() { console.log(this.getCorsOriginList()); console.log(this.getDevClientApiIpWhitelist()); - console.log(this.getExpressRequestBodySizeLimit()); console.log(this.getBlockchainApiRateLimit(), this.getReadRateLimit(), this.getWriteRateLimit()); } } From 75dbc57a38c025d6c7eab967a2b232f88fa44906 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Tue, 17 May 2022 18:47:59 +0900 Subject: [PATCH 032/166] Get NodeConfigs.CORS_WHITELIST directly --- client/middleware.js | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/client/middleware.js b/client/middleware.js index ae2df5388..3db04f47c 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -16,7 +16,6 @@ const { JSON_RPC_METHOD } = require('../json_rpc/constants'); class Middleware { constructor () { this.minuteAsSeconds = 60; - this.setCorsOriginList(); this.setDevClientApiIpWhitelist(); this.setBlockchainApiRateLimit(); this.setReadRateLimit(); @@ -31,12 +30,6 @@ class Middleware { }); } - setCorsOriginList() { - this.corsOriginList = NodeConfigs.CORS_WHITELIST === '*' ? - NodeConfigs.CORS_WHITELIST : getRegexpList(NodeConfigs.CORS_WHITELIST); - return this; - } - setDevClientApiIpWhitelist() { this.devClientApiIpWhitelist = NodeConfigs.DEV_CLIENT_API_IP_WHITELIST; return this; @@ -57,10 +50,6 @@ class Middleware { return this; } - getCorsOriginList() { - return this.corsOriginList; - } - getDevClientApiIpWhitelist() { return this.devClientApiIpWhitelist; } @@ -89,7 +78,8 @@ class Middleware { } corsLimiter() { - return cors({ origin: this.getCorsOriginList() }) + return cors({ origin: NodeConfigs.CORS_WHITELIST === '*' ? + NodeConfigs.CORS_WHITELIST : getRegexpList(NodeConfigs.CORS_WHITELIST) }); } ipWhitelistLimiter() { @@ -137,7 +127,6 @@ class Middleware { // NOTE(minsulee2): debugging purpose printAll() { - console.log(this.getCorsOriginList()); console.log(this.getDevClientApiIpWhitelist()); console.log(this.getBlockchainApiRateLimit(), this.getReadRateLimit(), this.getWriteRateLimit()); } From 07f3246423e40a18c7e142734740b9584c45107b Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Tue, 17 May 2022 18:49:55 +0900 Subject: [PATCH 033/166] Set NodeConfigs.DEV_CLIENT_API_IP_WHITELIST directly --- client/middleware.js | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/client/middleware.js b/client/middleware.js index 3db04f47c..3473a30a2 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -16,7 +16,6 @@ const { JSON_RPC_METHOD } = require('../json_rpc/constants'); class Middleware { constructor () { this.minuteAsSeconds = 60; - this.setDevClientApiIpWhitelist(); this.setBlockchainApiRateLimit(); this.setReadRateLimit(); this.setWriteRateLimit(); @@ -30,11 +29,6 @@ class Middleware { }); } - setDevClientApiIpWhitelist() { - this.devClientApiIpWhitelist = NodeConfigs.DEV_CLIENT_API_IP_WHITELIST; - return this; - } - setBlockchainApiRateLimit() { this.blockchainApiRateLimit = NodeConfigs.MAX_BLOCKCHAIN_API_RATE_LIMIT; return this; @@ -50,10 +44,6 @@ class Middleware { return this; } - getDevClientApiIpWhitelist() { - return this.devClientApiIpWhitelist; - } - getBlockchainApiRateLimit() { return this.blockchainApiRateLimit; } @@ -84,10 +74,9 @@ class Middleware { ipWhitelistLimiter() { return ipWhitelist((ip) => { - const whitelist = this.getDevClientApiIpWhitelist(); - return isWildcard(whitelist) || - matchUrl(ip, whitelist) || - matchUrl(convertIpv6ToIpv4(ip), whitelist); + return isWildcard(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || + matchUrl(ip, NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || + matchUrl(convertIpv6ToIpv4(ip), NodeConfigs.DEV_CLIENT_API_IP_WHITELIST); }) } From 22dd1d5a10d26450836f5ee18d8b6a0afbd58011 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Tue, 17 May 2022 18:58:14 +0900 Subject: [PATCH 034/166] Keep consistency --- client/middleware.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/client/middleware.js b/client/middleware.js index 3473a30a2..8c7e7edb9 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -80,13 +80,13 @@ class Middleware { }) } - _emptyHandler = () => { + _emptyHandler() { return (req, res, next) => { return next(); } } - blockchainApiLimiter = () => { + blockchainApiLimiter() { return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? rateLimit({ windowMs: this.minuteAsSeconds * 1000, // 1 minute window @@ -113,12 +113,6 @@ class Middleware { return this.jsonRpcReadLimiter(req, res, next); } } - - // NOTE(minsulee2): debugging purpose - printAll() { - console.log(this.getDevClientApiIpWhitelist()); - console.log(this.getBlockchainApiRateLimit(), this.getReadRateLimit(), this.getWriteRateLimit()); - } } module.exports = Middleware; From 5aca72313854a5fd38466e2baa3638d23663c3a4 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Wed, 18 May 2022 15:18:30 +0900 Subject: [PATCH 035/166] Rollback some string values --- test/integration/blockchain.test.js | 83 +++++++++++++++-------------- test/integration/sharding.test.js | 54 +++++++++---------- 2 files changed, 69 insertions(+), 68 deletions(-) diff --git a/test/integration/blockchain.test.js b/test/integration/blockchain.test.js index c03c91b7d..1982ce704 100644 --- a/test/integration/blockchain.test.js +++ b/test/integration/blockchain.test.js @@ -54,7 +54,8 @@ const serverList = [server1, server2, server3 ]; const JSON_RPC_ENDPOINT = '/json-rpc'; const SET_VALUE_ENDPOINT = '/set_value'; -const GET_VALUE_ENDPOINT = '/get_value' +const GET_VALUE_ENDPOINT = '/get_value'; +const BLOCKS_ENDPOINT = '/blocks'; // NOTE(minsulee2): keep it for commented out part const GET_ADDR_ENDPOINT = '/get_address'; // Data options @@ -274,32 +275,32 @@ describe('Blockchain Cluster', () => { jayson.client.http(server1 + JSON_RPC_ENDPOINT) .request(JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, - function(err, response) { - if (err) throw err; - baseChain = response.result.result; - resolve(); - }); + function(err, response) { + if (err) throw err; + baseChain = response.result.result; + resolve(); + }); }).then(() => { return new Promise((resolve) => { jayson.client.http(serverList[i] + JSON_RPC_ENDPOINT) .request(JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, - function (err, response) { - if (err) throw err; - const newChain = response.result.result; - const diff = Math.abs(baseChain.length - newChain.length); - assert.isBelow(diff, MAX_CHAIN_LENGTH_DIFF); - while (baseChain.length !== newChain.length) { - if (baseChain.length > newChain.length) { - baseChain.pop(); - } else { - newChain.pop(); - } - } - assert.deepEqual(newChain.length, baseChain.length); - assert.deepEqual(newChain, baseChain); - resolve(); - }); + function (err, response) { + if (err) throw err; + const newChain = response.result.result; + const diff = Math.abs(baseChain.length - newChain.length); + assert.isBelow(diff, MAX_CHAIN_LENGTH_DIFF); + while (baseChain.length !== newChain.length) { + if (baseChain.length > newChain.length) { + baseChain.pop(); + } else { + newChain.pop(); + } + } + assert.deepEqual(newChain.length, baseChain.length); + assert.deepEqual(newChain, baseChain); + resolve(); + }); }); }); } @@ -505,7 +506,7 @@ describe('Blockchain Cluster', () => { }); describe('Block API', () => { - it(JSON_RPC_METHOD.AIN_GET_BLOCK_HEADERS_LIST, async () => { + it('ain_getBlockHeadersList', async () => { await sendTransactions(sentOperations); return new Promise((resolve) => { jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_BLOCK_HEADERS_LIST, @@ -521,7 +522,7 @@ describe('Blockchain Cluster', () => { }) }); - it(`${JSON_RPC_METHOD.AIN_GET_BLOCK_BY_HASH} and ${JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER}`, async () => { + it('ain_getBlockByHash and ain_getBlockByNumber', async () => { await sendTransactions(sentOperations); return new Promise((resolve) => { jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, @@ -620,23 +621,23 @@ describe('Blockchain Cluster', () => { jayson.client.http(serverList[1] + JSON_RPC_ENDPOINT) .request(JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, - function (err, response) { - if (err) throw err; - const chain = response.result.result; - for (const block of chain) { - if (block.number > 0) { - // Amount specified in block - const gasCostTotal = block.gas_cost_total; - // Amount actually collected & distributed. Write rule prevents writing a gas_cost_total - // that is different from the value at /service_accounts/gas_fee/gas_fee/${block.number}/balance. - const collectedGas = parseOrLog(syncRequest( - 'GET', server1 + GET_VALUE_ENDPOINT + `?ref=/consensus/number/${block.number}/propose/gas_cost_total`) - .body.toString('utf-8')).result; - assert.deepEqual(gasCostTotal, collectedGas); - } - } - resolve(); - }); + function (err, response) { + if (err) throw err; + const chain = response.result.result; + for (const block of chain) { + if (block.number > 0) { + // Amount specified in block + const gasCostTotal = block.gas_cost_total; + // Amount actually collected & distributed. Write rule prevents writing a gas_cost_total + // that is different from the value at /service_accounts/gas_fee/gas_fee/${block.number}/balance. + const collectedGas = parseOrLog(syncRequest( + 'GET', server1 + GET_VALUE_ENDPOINT + `?ref=/consensus/number/${block.number}/propose/gas_cost_total`) + .body.toString('utf-8')).result; + assert.deepEqual(gasCostTotal, collectedGas); + } + } + resolve(); + }); }); }); }); diff --git a/test/integration/sharding.test.js b/test/integration/sharding.test.js index dc7b116b5..45756dc74 100644 --- a/test/integration/sharding.test.js +++ b/test/integration/sharding.test.js @@ -1248,8 +1248,8 @@ describe('Sharding', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_GET}`, () => { - it(`${JSON_RPC_METHOD.AIN_GET} with is_global = false`, () => { + describe('ain_get api', () => { + it('ain_get with is_global = false', () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { @@ -1262,7 +1262,7 @@ describe('Sharding', () => { }); }); - it(`${JSON_RPC_METHOD.AIN_GET} with is_global = false (explicit)`, () => { + it('ain_get with is_global = false (explicit)', () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { @@ -1276,7 +1276,7 @@ describe('Sharding', () => { }); }); - it(`${JSON_RPC_METHOD.AIN_GET} with is_global = true`, () => { + it('ain_get with is_global = true', () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { @@ -1291,8 +1291,8 @@ describe('Sharding', () => { }); }) - describe(`${JSON_RPC_METHOD.AIN_MATCH_FUNCTION} api`, () => { - it(`${JSON_RPC_METHOD.AIN_MATCH_FUNCTION} with is_global = false`, () => { + describe('ain_matchFunction api', () => { + it('ain_matchFunction with is_global = false', () => { const ref = "/apps/test/test_function/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_FUNCTION, request) @@ -1318,7 +1318,7 @@ describe('Sharding', () => { }) }) - it(`${JSON_RPC_METHOD.AIN_MATCH_FUNCTION} with is_global = true`, () => { + it('ain_matchFunction with is_global = true', () => { const ref = "/apps/afan/apps/test/test_function/some/path"; const request = { ref, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_FUNCTION, request) @@ -1345,8 +1345,8 @@ describe('Sharding', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_MATCH_RULE} api`, () => { - it(`${JSON_RPC_METHOD.AIN_MATCH_RULE} with is_global = false`, () => { + describe('ain_matchRule api', () => { + it('ain_matchRule with is_global = false', () => { const ref = "/apps/test/test_rule/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) @@ -1381,7 +1381,7 @@ describe('Sharding', () => { }) }) - it(`${JSON_RPC_METHOD.AIN_MATCH_RULE} with is_global = true`, () => { + it('ain_matchRule with is_global = true', () => { const ref = "/apps/afan/apps/test/test_rule/some/path"; const request = { ref, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) @@ -1417,8 +1417,8 @@ describe('Sharding', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_MATCH_OWNER} api`, () => { - it(`${JSON_RPC_METHOD.AIN_MATCH_OWNER} with is_global = false`, () => { + describe('ain_matchOwner api', () => { + it('ain_matchOwner with is_global = false', () => { const ref = "/apps/test/test_owner/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_OWNER, request) @@ -1445,7 +1445,7 @@ describe('Sharding', () => { }) }) - it(`${JSON_RPC_METHOD.AIN_MATCH_OWNER} with is_global = true`, () => { + it('ain_matchOwner with is_global = true', () => { const ref = "/apps/afan/apps/test/test_owner/some/path"; const request = { ref, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_OWNER, request) @@ -1473,8 +1473,8 @@ describe('Sharding', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_EVAL_RULE} api`, () => { - it(`${JSON_RPC_METHOD.AIN_EVAL_RULE} with is_global = false`, () => { + describe('ain_evalRule api', () => { + it('ain_evalRule with is_global = false', () => { const ref = "/apps/test/test_rule/some/path"; const value = "value"; const address = "abcd"; @@ -1488,7 +1488,7 @@ describe('Sharding', () => { }) }) - it(`${JSON_RPC_METHOD.AIN_EVAL_RULE} with is_global = true`, () => { + it('ain_evalRule with is_global = true', () => { const ref = "/apps/afan/apps/test/test_rule/some/path"; const value = "value"; const address = "abcd"; @@ -1504,8 +1504,8 @@ describe('Sharding', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_EVAL_OWNER} api`, () => { - it(`${JSON_RPC_METHOD.AIN_EVAL_OWNER} with is_global = false`, () => { + describe('ain_evalOwner api', () => { + it('ain_evalOwner with is_global = false', () => { const ref = "/apps/test/test_owner/some/path"; const address = "abcd"; const permission = "write_owner"; @@ -1547,7 +1547,7 @@ describe('Sharding', () => { }) }) - it(`${JSON_RPC_METHOD.AIN_EVAL_OWNER} with is_global = true`, () => { + it('ain_evalOwner with is_global = true', () => { const ref = "/apps/afan/apps/test/test_owner/some/path"; const address = "abcd"; const permission = "write_owner"; @@ -2023,8 +2023,8 @@ describe('Sharding', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION} api`, () => { - it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION} with is_global = false`, () => { + describe('ain_sendSignedTransaction api', () => { + it('ain_sendSignedTransaction with is_global = false', () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2072,7 +2072,7 @@ describe('Sharding', () => { }) }) - it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION} with is_global = false (explicit)`, () => { + it('ain_sendSignedTransaction with is_global = false (explicit)', () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2121,7 +2121,7 @@ describe('Sharding', () => { }) }) - it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION} with is_global = true`, () => { + it('ain_sendSignedTransaction with is_global = true', () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2171,8 +2171,8 @@ describe('Sharding', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH} api`, () => { - it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH} with is_global = false`, () => { + describe('ain_sendSignedTransactionBatch api', () => { + it('ain_sendSignedTransactionBatch with is_global = false', () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2229,7 +2229,7 @@ describe('Sharding', () => { }) }) - it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH} with is_global = false (explicit)`, () => { + it('ain_sendSignedTransactionBatch with is_global = false (explicit)', () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { @@ -2290,7 +2290,7 @@ describe('Sharding', () => { }) }) - it(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH} with is_global = true`, () => { + it('ain_sendSignedTransactionBatch with is_global = true', () => { const client = jayson.client.http(server1 + '/json-rpc'); const txBody = { operation: { From b0600bff4de3e8f9b5c7f95731bdc1b8cab71ec2 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Wed, 18 May 2022 20:46:22 +0900 Subject: [PATCH 036/166] Tidy JSON_RPC_METHODS up --- client/index.js | 6 +- client/middleware.js | 18 +-- common/network-util.js | 6 +- common/version-util.js | 6 +- inject_account_gcp.js | 10 +- json_rpc/account.js | 12 +- json_rpc/admin.js | 14 +- json_rpc/app.js | 4 +- json_rpc/block.js | 26 ++-- json_rpc/constants.js | 94 ++++++------ json_rpc/database.js | 22 +-- json_rpc/event-handler.js | 8 +- json_rpc/index.js | 4 +- json_rpc/injection.js | 10 +- json_rpc/network.js | 18 +-- json_rpc/transaction.js | 16 +- json_rpc/version.js | 6 +- p2p/index.js | 4 +- p2p/server.js | 6 +- test/integration/blockchain.test.js | 46 +++--- test/integration/consensus.test.js | 6 +- test/integration/event_handler.test.js | 8 +- test/integration/function.test.js | 18 +-- test/integration/node.test.js | 140 +++++++++--------- test/integration/sharding.test.js | 40 ++--- test/test-util.js | 4 +- .../addToDevClientApiIpWhitelist.js | 6 +- .../removeFromDevClientApiIpWhitelist.js | 6 +- tools/checkin/sendCloseCheckinTx.js | 4 +- tools/proposer-stats/getProposerStats.js | 4 +- tools/simple-load-test/index.js | 4 +- tools/util.js | 6 +- 32 files changed, 294 insertions(+), 288 deletions(-) diff --git a/client/index.js b/client/index.js index de547271a..47a24a3ec 100755 --- a/client/index.js +++ b/client/index.js @@ -37,9 +37,9 @@ app.use(middleware.blockchainApiLimiter()); const eventHandler = NodeConfigs.ENABLE_EVENT_HANDLER === true ? new EventHandler() : null; const node = new BlockchainNode(null, eventHandler); // NOTE(platfowner): This is very useful when the server dies without any logs. -process.on('uncaughtException', function(err) { - logger.error(err); -}); +// process.on('uncaughtException', function(err) { +// logger.error(err); +// }); process.on('SIGINT', (_) => { logger.info('Stopping the blockchain client....'); diff --git a/client/middleware.js b/client/middleware.js index 8c7e7edb9..6610c01d8 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -11,7 +11,7 @@ const { isWildcard } = require('../common/common-util'); const { convertIpv6ToIpv4 } = require('../common/network-util'); -const { JSON_RPC_METHOD } = require('../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../json_rpc/constants'); class Middleware { constructor () { @@ -100,14 +100,14 @@ class Middleware { } const jsonRpcMethod = _.get(req, 'body.method'); switch (jsonRpcMethod) { - case JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: - case JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: - case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: - case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_KEYSTORE: - case JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_HD_WALLET: - case JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION: - case JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH: - case JSON_RPC_METHOD.AIN_GET_LAST_BLOCK_NUMBER: + case JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: + case JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: + case JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: + case JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_KEYSTORE: + case JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_HD_WALLET: + case JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION: + case JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH: + case JSON_RPC_METHODS.AIN_GET_LAST_BLOCK_NUMBER: return this.jsonRpcWriteLimiter(req, res, next); default: return this.jsonRpcReadLimiter(req, res, next); diff --git a/common/network-util.js b/common/network-util.js index d23674baa..a7594c501 100644 --- a/common/network-util.js +++ b/common/network-util.js @@ -7,7 +7,7 @@ const ip = require('ip'); const extIp = require('ext-ip')(); const CommonUtil = require('../common/common-util'); const DB = require('../db'); -const { JSON_RPC_METHOD } = require('../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../json_rpc/constants'); const GCP_EXTERNAL_IP_URL = 'http://metadata.google.internal/computeMetadata/v1/instance' + '/network-interfaces/0/access-configs/0/external-ip'; const GCP_INTERNAL_IP_URL = 'http://metadata.google.internal/computeMetadata/v1/instance' + @@ -19,7 +19,7 @@ async function _waitUntilTxFinalize(endpoint, txHash) { while (true) { const confirmed = await sendGetRequest( endpoint, - JSON_RPC_METHOD.AIN_GET_TRANSACTION_BY_HASH, + JSON_RPC_METHODS.AIN_GET_TRANSACTION_BY_HASH, { hash: txHash } ) .then((resp) => { @@ -51,7 +51,7 @@ async function sendSignedTx(endpoint, params) { return await axios.post( endpoint, { - method: JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, + method: JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, params, jsonrpc: '2.0', id: 0 diff --git a/common/version-util.js b/common/version-util.js index 632cfe1b1..3ba8473a4 100644 --- a/common/version-util.js +++ b/common/version-util.js @@ -1,7 +1,7 @@ const semver = require('semver'); const { BlockchainConsts } = require('../common/constants'); const { DevClientApiResultCode } = require('../common/result-code'); -const { JSON_RPC_METHOD } = require('../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../json_rpc/constants'); class VersionUtil { static isValidProtocolVersion(version) { @@ -43,8 +43,8 @@ class VersionUtil { version = req.body.params.protoVer; } const coercedVer = semver.coerce(version); - if (req.body.method === JSON_RPC_METHOD.AIN_GET_PROTOCOL_VERSION || - req.body.method === JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION) { + if (req.body.method === JSON_RPC_METHODS.AIN_GET_PROTOCOL_VERSION || + req.body.method === JSON_RPC_METHODS.AIN_CHECK_PROTOCOL_VERSION) { next(); } else if (version === undefined) { res.status(200) diff --git a/inject_account_gcp.js b/inject_account_gcp.js index fdccd1b7d..2946406e5 100644 --- a/inject_account_gcp.js +++ b/inject_account_gcp.js @@ -5,13 +5,13 @@ const ainUtil = require('@ainblockchain/ain-util'); const { BlockchainConsts } = require('./common/constants'); const { sleep } = require('./common/common-util'); const prompt = require('prompt'); -const { JSON_RPC_METHOD } = require('./json_rpc/constants'); +const { JSON_RPC_METHODS } = require('./json_rpc/constants'); async function sendGetBootstrapPubKeyRequest(endpointUrl) { return await axios.post( `${endpointUrl}/json-rpc`, { - method: JSON_RPC_METHOD.AIN_GET_BOOTSTRAP_PUB_KEY, + method: JSON_RPC_METHODS.AIN_GET_BOOTSTRAP_PUB_KEY, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, }, @@ -104,13 +104,13 @@ async function injectAccount(endpointUrl, accountInjectionOption) { const params = {}; switch (accountInjectionOption) { case '--private-key': - method = JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY; + method = JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY; Object.assign(params, { encryptedPrivateKey: await ainUtil.encryptWithPublicKey(bootstrapPubKey, input.privateKey) }) break; case '--keystore': - method = JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_KEYSTORE; + method = JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_KEYSTORE; const keystore = JSON.stringify(JSON.parse(fs.readFileSync(input.keystorePath))); Object.assign(params, { encryptedKeystore: await ainUtil.encryptWithPublicKey(bootstrapPubKey, keystore) @@ -120,7 +120,7 @@ async function injectAccount(endpointUrl, accountInjectionOption) { }) break; case '--mnemonic': - method = JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_HD_WALLET; + method = JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_HD_WALLET; Object.assign(params, { encryptedMnemonic: await ainUtil.encryptWithPublicKey(bootstrapPubKey, input.mnemonic), index: input.index diff --git a/json_rpc/account.js b/json_rpc/account.js index f21fb3ee8..c7773239e 100644 --- a/json_rpc/account.js +++ b/json_rpc/account.js @@ -4,11 +4,11 @@ const { } = require('../common/constants'); const PathUtil = require('../common/path-util'); const JsonRpcUtil = require('./json-rpc-util'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); module.exports = function getAccountApis(node) { return { - [JSON_RPC_METHOD.AIN_GET_ADDRESS]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_ADDRESS]: function(args, done) { const beginTime = Date.now(); const result = node.account ? node.account.address : null; const latency = Date.now() - beginTime; @@ -16,7 +16,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_BALANCE]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_BALANCE]: function(args, done) { const beginTime = Date.now(); const address = args.address; const balance = node.db.getValue(PathUtil.getAccountBalancePath(address)) || 0; @@ -25,7 +25,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: balance })); }, - [JSON_RPC_METHOD.AIN_GET_NONCE]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_NONCE]: function(args, done) { const beginTime = Date.now(); const result = node.getNonceForAddr(args.address, args.from === 'pending'); const latency = Date.now() - beginTime; @@ -33,7 +33,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_TIMESTAMP]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_TIMESTAMP]: function(args, done) { const beginTime = Date.now(); const result = node.getTimestampForAddr(args.address, args.from === 'pending'); const latency = Date.now() - beginTime; @@ -41,7 +41,7 @@ module.exports = function getAccountApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_VALIDATOR_INFO]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_VALIDATOR_INFO]: function(args, done) { const beginTime = Date.now(); const addr = args.address; const isWhitelisted = node.db.getValue(PathUtil.getConsensusProposerWhitelistAddrPath(addr)) || false; diff --git a/json_rpc/admin.js b/json_rpc/admin.js index cf243c171..bb85bc7fb 100644 --- a/json_rpc/admin.js +++ b/json_rpc/admin.js @@ -8,16 +8,16 @@ const { const { JsonRpcApiResultCode } = require('../common/result-code'); const CommonUtil = require('../common/common-util'); const JsonRpcUtil = require('./json-rpc-util'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); module.exports = function getApiAccessApis(node) { return { - [JSON_RPC_METHOD.AIN_GET_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { const beginTime = Date.now(); const verified = node.verifyNodeAccountSignature(args.message, args.signature); const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_GET, latency); - if (_.get(args.message, 'method') === JSON_RPC_METHOD.AIN_GET_DEV_CLIENT_API_IP_WHITELIST && + if (_.get(args.message, 'method') === JSON_RPC_METHODS.AIN_GET_DEV_CLIENT_API_IP_WHITELIST && verified) { done(null, JsonRpcUtil.addProtocolVersion({ result: NodeConfigs.DEV_CLIENT_API_IP_WHITELIST })); @@ -26,10 +26,10 @@ module.exports = function getApiAccessApis(node) { } }, - [JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { + [JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { const beginTime = Date.now(); const verified = node.verifyNodeAccountSignature(args.message, args.signature); - if (_.get(args.message, 'method') !== JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST || + if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST || !verified) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); @@ -85,10 +85,10 @@ module.exports = function getApiAccessApis(node) { } }, - [JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { + [JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { const beginTime = Date.now(); const verified = node.verifyNodeAccountSignature(args.message, args.signature); - if (_.get(args.message, 'method') !== JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST || + if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST || !verified) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); diff --git a/json_rpc/app.js b/json_rpc/app.js index c6635d897..8d1eb5b4b 100644 --- a/json_rpc/app.js +++ b/json_rpc/app.js @@ -3,11 +3,11 @@ const { trafficStatsManager, } = require('../common/constants'); const JsonRpcUtil = require('./json-rpc-util'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); module.exports = function getAppApis(node) { return { - [JSON_RPC_METHOD.AIN_VALIDATE_APP_NAME]: function(args, done) { + [JSON_RPC_METHODS.AIN_VALIDATE_APP_NAME]: function(args, done) { const beginTime = Date.now(); const result = node.validateAppName(args.app_name); const latency = Date.now() - beginTime; diff --git a/json_rpc/block.js b/json_rpc/block.js index 444e2500b..dfa70b412 100644 --- a/json_rpc/block.js +++ b/json_rpc/block.js @@ -3,11 +3,11 @@ const { trafficStatsManager, } = require('../common/constants'); const JsonRpcUtil = require('./json-rpc-util'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); module.exports = function getBlockApis(node) { return { - [JSON_RPC_METHOD.AIN_GET_BLOCK_LIST]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_BLOCK_LIST]: function(args, done) { const beginTime = Date.now(); const blocks = node.bc.getBlockList(args.from, args.to); const latency = Date.now() - beginTime; @@ -15,7 +15,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: blocks })); }, - [JSON_RPC_METHOD.AIN_GET_LAST_BLOCK]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_LAST_BLOCK]: function(args, done) { const beginTime = Date.now(); const result = node.bc.lastBlock(); const latency = Date.now() - beginTime; @@ -23,7 +23,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_LAST_BLOCK_NUMBER]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_LAST_BLOCK_NUMBER]: function(args, done) { const beginTime = Date.now(); const result = node.bc.lastBlockNumber(); const latency = Date.now() - beginTime; @@ -31,7 +31,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_BLOCK_HEADERS_LIST]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_BLOCK_HEADERS_LIST]: function(args, done) { const beginTime = Date.now(); const blocks = node.bc.getBlockList(args.from, args.to); const blockHeaders = []; @@ -43,7 +43,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: blockHeaders })); }, - [JSON_RPC_METHOD.AIN_GET_BLOCK_BY_HASH]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_BLOCK_BY_HASH]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByHash(args.hash); if (block && !args.getFullTransactions) { @@ -54,7 +54,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: block })); }, - [JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_BLOCK_BY_NUMBER]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByNumber(args.number); if (!block || args.getFullTransactions) { @@ -69,7 +69,7 @@ module.exports = function getBlockApis(node) { } }, - [JSON_RPC_METHOD.AIN_GET_PROPOSER_BY_HASH]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_PROPOSER_BY_HASH]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByHash(args.hash); const result = block ? block.proposer : null; @@ -78,7 +78,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_PROPOSER_BY_NUMBER]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_PROPOSER_BY_NUMBER]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByNumber(args.number); const result = block ? block.proposer : null; @@ -87,7 +87,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_VALIDATORS_BY_NUMBER]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_VALIDATORS_BY_NUMBER]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByNumber(args.number); const result = block ? block.validators : null; @@ -96,7 +96,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_VALIDATORS_BY_HASH]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_VALIDATORS_BY_HASH]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByHash(args.hash); const result = block ? block.validators : null; @@ -105,7 +105,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_BLOCK_TRANSACTION_COUNT_BY_HASH]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_BLOCK_TRANSACTION_COUNT_BY_HASH]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByHash(args.hash); const result = block ? block.transactions.length : null; @@ -114,7 +114,7 @@ module.exports = function getBlockApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_BLOCK_TRANSACTION_COUNT_BY_NUMBER]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_BLOCK_TRANSACTION_COUNT_BY_NUMBER]: function(args, done) { const beginTime = Date.now(); const block = node.bc.getBlockByNumber(args.number); const result = block ? block.transactions.length : null; diff --git a/json_rpc/constants.js b/json_rpc/constants.js index 956684a19..a40f2399a 100644 --- a/json_rpc/constants.js +++ b/json_rpc/constants.js @@ -1,63 +1,69 @@ -const JSON_RPC_METHOD = { - // GET +const JSON_RPC_METHODS = { + AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', + AIN_CHECK_PROTOCOL_VERSION: 'ain_checkProtocolVersion', + AIN_EVAL_RULE: 'ain_evalRule', + AIN_EVAL_OWNER: 'ain_evalOwner', + AIN_GET: 'ain_get', AIN_GET_ADDRESS: 'ain_getAddress', AIN_GET_BALANCE: 'ain_getBalance', - AIN_GET_NONCE: 'ain_getNonce', - AIN_GET_TIMESTAMP: 'ain_getTimestamp', - AIN_GET_VALIDATOR_INFO: 'ain_getValidatorInfo', - AIN_GET_DEV_CLIENT_API_IP_WHITELIST: 'ain_getDevClientApiIpWhitelist', - AIN_VALIDATE_APP_NAME: 'ain_validateAppName', - AIN_GET_BLOCK_LIST: 'ain_getBlockList', - AIN_GET_LAST_BLOCK: 'ain_getLastBlock', - AIN_GET_LAST_BLOCK_NUMBER: 'ain_getLastBlockNumber', - AIN_GET_BLOCK_HEADERS_LIST: 'ain_getBlockHeadersList', AIN_GET_BLOCK_BY_HASH: 'ain_getBlockByHash', AIN_GET_BLOCK_BY_NUMBER: 'ain_getBlockByNumber', - AIN_GET_PROPOSER_BY_HASH: 'ain_getProposerByHash', - AIN_GET_PROPOSER_BY_NUMBER: 'ain_getProposerByNumber', - AIN_GET_VALIDATORS_BY_NUMBER: 'ain_getValidatorsByNumber', - AIN_GET_VALIDATORS_BY_HASH: 'ain_getValidatorsByHash', + AIN_GET_BLOCK_HEADERS_LIST: 'ain_getBlockHeadersList', + AIN_GET_BLOCK_LIST: 'ain_getBlockList', AIN_GET_BLOCK_TRANSACTION_COUNT_BY_HASH: 'ain_getBlockTransactionCountByHash', AIN_GET_BLOCK_TRANSACTION_COUNT_BY_NUMBER: 'ain_getBlockTransactionCountByNumber', - AIN_GET: 'ain_get', - AIN_MATCH_FUNCTION: 'ain_matchFunction', - AIN_MATCH_RULE: 'ain_matchRule', - AIN_MATCH_OWNER: 'ain_matchOwner', - AIN_EVAL_RULE: 'ain_evalRule', - AIN_EVAL_OWNER: 'ain_evalOwner', - AIN_GET_STATE_PROOF: 'ain_getStateProof', + AIN_GET_BOOTSTRAP_PUB_KEY: 'ain_getBootstrapPubKey', + AIN_GET_DEV_CLIENT_API_IP_WHITELIST: 'ain_getDevClientApiIpWhitelist', + AIN_GET_EVENT_HANDLER_CHANNEL_INFO: 'ain_getEventHandlerChannelInfo', + AIN_GET_EVENT_HANDLER_FILTER_INFO: 'ain_getEventHandlerFilterInfo', + AIN_GET_LAST_BLOCK: 'ain_getLastBlock', + AIN_GET_LAST_BLOCK_NUMBER: 'ain_getLastBlockNumber', + AIN_GET_NONCE: 'ain_getNonce', + AIN_GET_PENDING_TRANSACTIONS: 'ain_getPendingTransactions', AIN_GET_PROOF_HASH: 'ain_getProofHash', + AIN_GET_PROPOSER_BY_HASH: 'ain_getProposerByHash', + AIN_GET_PROPOSER_BY_NUMBER: 'ain_getProposerByNumber', + AIN_GET_PROTOCOL_VERSION: 'ain_getProtocolVersion', AIN_GET_STATE_INFO: 'ain_getStateInfo', + AIN_GET_STATE_PROOF: 'ain_getStateProof', AIN_GET_STATE_USAGE: 'ain_getStateUsage', - NET_GET_EVENT_HANDLER_NETWORK_INFO: 'net_getEventHandlerNetworkInfo', - AIN_GET_EVENT_HANDLER_FILTER_INFO: 'ain_getEventHandlerFilterInfo', - AIN_GET_EVENT_HANDLER_CHANNEL_INFO: 'ain_getEventHandlerChannelInfo', - AIN_GET_BOOTSTRAP_PUB_KEY: 'ain_getBootstrapPubKey', - NET_LISTENING: 'net_listening', - NET_PEER_COUNT: 'net_peerCount', - NET_SYNCING: 'net_syncing', - NET_GET_NETWORK_ID: 'net_getNetworkId', - NET_GET_CHAIN_ID: 'net_getChainId', - NET_CONSENSUS_STATUS: 'net_consensusStatus', - NET_RAW_CONSENSUS_STATUS: 'net_rawConsensusStatus', - P2P_GET_PEER_CANDIDATE_INFO: 'p2p_getPeerCandidateInfo', - AIN_GET_PENDING_TRANSACTIONS: 'ain_getPendingTransactions', - AIN_GET_TRANSACTION_POOL_SIZE_UTILIZATION: 'ain_getTransactionPoolSizeUtilization', AIN_GET_TRANSACTION_BY_HASH: 'ain_getTransactionByHash', AIN_GET_TRANSACTION_BY_BLOCK_HASH_AND_INDEX: 'ain_getTransactionByBlockHashAndIndex', AIN_GET_TRANSACTION_BY_BLOCK_NUMBER_AND_INDEX: 'ain_getTransactionByBlockNumberAndIndex', - AIN_GET_PROTOCOL_VERSION: 'ain_getProtocolVersion', - AIN_CHECK_PROTOCOL_VERSION: 'ain_checkProtocolVersion', - // SET - AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', - AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', - AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: 'ain_injectAccountFromPrivateKey', - AIN_INJECT_ACCOUNT_FROM_KEYSTORE: 'ain_injectAccountFromKeystore', + AIN_GET_TRANSACTION_POOL_SIZE_UTILIZATION: 'ain_getTransactionPoolSizeUtilization', + AIN_GET_TIMESTAMP: 'ain_getTimestamp', + AIN_GET_VALIDATOR_INFO: 'ain_getValidatorInfo', + AIN_GET_VALIDATORS_BY_HASH: 'ain_getValidatorsByHash', + AIN_GET_VALIDATORS_BY_NUMBER: 'ain_getValidatorsByNumber', AIN_INJECT_ACCOUNT_FROM_HD_WALLET: 'ain_injectAccountFromHDWallet', + AIN_INJECT_ACCOUNT_FROM_KEYSTORE: 'ain_injectAccountFromKeystore', + AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: 'ain_injectAccountFromPrivateKey', + AIN_MATCH_FUNCTION: 'ain_matchFunction', + AIN_MATCH_OWNER: 'ain_matchOwner', + AIN_MATCH_RULE: 'ain_matchRule', + AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', AIN_SEND_SIGNED_TRANSACTION: 'ain_sendSignedTransaction', AIN_SEND_SIGNED_TRANSACTION_BATCH: 'ain_sendSignedTransactionBatch', + AIN_VALIDATE_APP_NAME: 'ain_validateAppName', + NET_CONSENSUS_STATUS: 'net_consensusStatus', + NET_GET_CHAIN_ID: 'net_getChainId', + NET_GET_EVENT_HANDLER_NETWORK_INFO: 'net_getEventHandlerNetworkInfo', + NET_GET_NETWORK_ID: 'net_getNetworkId', + NET_LISTENING: 'net_listening', + NET_PEER_COUNT: 'net_peerCount', + NET_RAW_CONSENSUS_STATUS: 'net_rawConsensusStatus', + NET_SYNCING: 'net_syncing', + P2P_GET_PEER_CANDIDATE_INFO: 'p2p_getPeerCandidateInfo', } +// AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', +// AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', +// AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: 'ain_injectAccountFromPrivateKey', +// AIN_INJECT_ACCOUNT_FROM_KEYSTORE: 'ain_injectAccountFromKeystore', +// AIN_INJECT_ACCOUNT_FROM_HD_WALLET: 'ain_injectAccountFromHDWallet', +// AIN_SEND_SIGNED_TRANSACTION: 'ain_sendSignedTransaction', +// AIN_SEND_SIGNED_TRANSACTION_BATCH: 'ain_sendSignedTransactionBatch', + module.exports = { - JSON_RPC_METHOD + JSON_RPC_METHODS }; diff --git a/json_rpc/database.js b/json_rpc/database.js index c586618bb..e50b57ec6 100644 --- a/json_rpc/database.js +++ b/json_rpc/database.js @@ -6,11 +6,11 @@ const { const { JsonRpcApiResultCode } = require('../common/result-code'); const CommonUtil = require('../common/common-util'); const JsonRpcUtil = require('./json-rpc-util'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); module.exports = function getDatabaseApis(node) { return { - [JSON_RPC_METHOD.AIN_GET]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET]: function(args, done) { const beginTime = Date.now(); let result; let latency; @@ -57,7 +57,7 @@ module.exports = function getDatabaseApis(node) { } }, - [JSON_RPC_METHOD.AIN_MATCH_FUNCTION]: function(args, done) { + [JSON_RPC_METHODS.AIN_MATCH_FUNCTION]: function(args, done) { const beginTime = Date.now(); const result = node.db.matchFunction(args.ref, CommonUtil.toMatchOrEvalOptions(args, true)); @@ -66,7 +66,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_MATCH_RULE]: function(args, done) { + [JSON_RPC_METHODS.AIN_MATCH_RULE]: function(args, done) { const beginTime = Date.now(); const result = node.db.matchRule(args.ref, CommonUtil.toMatchOrEvalOptions(args, true)); const latency = Date.now() - beginTime; @@ -74,7 +74,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_MATCH_OWNER]: function(args, done) { + [JSON_RPC_METHODS.AIN_MATCH_OWNER]: function(args, done) { const beginTime = Date.now(); const result = node.db.matchOwner(args.ref, CommonUtil.toMatchOrEvalOptions(args, true)); const latency = Date.now() - beginTime; @@ -82,7 +82,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_EVAL_RULE]: function(args, done) { + [JSON_RPC_METHODS.AIN_EVAL_RULE]: function(args, done) { const beginTime = Date.now(); const auth = {}; if (args.address) { @@ -99,7 +99,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_EVAL_OWNER]: function(args, done) { + [JSON_RPC_METHODS.AIN_EVAL_OWNER]: function(args, done) { const beginTime = Date.now(); const auth = {}; if (args.address) { @@ -115,7 +115,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_STATE_PROOF]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_STATE_PROOF]: function(args, done) { const beginTime = Date.now(); const result = node.db.getStateProof(args.ref); const latency = Date.now() - beginTime; @@ -123,7 +123,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_PROOF_HASH]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_PROOF_HASH]: function(args, done) { const beginTime = Date.now(); const result = node.db.getProofHash(args.ref); const latency = Date.now() - beginTime; @@ -131,7 +131,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_STATE_INFO]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_STATE_INFO]: function(args, done) { const beginTime = Date.now(); const result = node.db.getStateInfo(args.ref); const latency = Date.now() - beginTime; @@ -139,7 +139,7 @@ module.exports = function getDatabaseApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_STATE_USAGE]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_STATE_USAGE]: function(args, done) { const beginTime = Date.now(); const result = node.getStateUsageWithStakingInfo(args.app_name); const latency = Date.now() - beginTime; diff --git a/json_rpc/event-handler.js b/json_rpc/event-handler.js index 4d6817783..7141ab6a8 100644 --- a/json_rpc/event-handler.js +++ b/json_rpc/event-handler.js @@ -3,12 +3,12 @@ const { trafficStatsManager, } = require('../common/constants'); const JsonRpcUtil = require('./json-rpc-util'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); module.exports = function getEventHandlerApis(eventHandler) { return { // NOTE(cshcomcom): Async function doesn't need a done parameter. (Ref: https://www.npmjs.com/package/jayson#promises) - [JSON_RPC_METHOD.NET_GET_EVENT_HANDLER_NETWORK_INFO]: async function(args) { + [JSON_RPC_METHODS.NET_GET_EVENT_HANDLER_NETWORK_INFO]: async function(args) { const beginTime = Date.now(); const result = await eventHandler.eventChannelManager.getNetworkInfo(); const latency = Date.now() - beginTime; @@ -16,7 +16,7 @@ module.exports = function getEventHandlerApis(eventHandler) { return JsonRpcUtil.addProtocolVersion({ result }); }, - [JSON_RPC_METHOD.AIN_GET_EVENT_HANDLER_FILTER_INFO]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_EVENT_HANDLER_FILTER_INFO]: function(args, done) { const beginTime = Date.now(); const result = eventHandler.getFilterInfo(); const latency = Date.now() - beginTime; @@ -24,7 +24,7 @@ module.exports = function getEventHandlerApis(eventHandler) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_EVENT_HANDLER_CHANNEL_INFO]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_EVENT_HANDLER_CHANNEL_INFO]: function(args, done) { const beginTime = Date.now(); const result = eventHandler.eventChannelManager.getChannelInfo(); const latency = Date.now() - beginTime; diff --git a/json_rpc/index.js b/json_rpc/index.js index 1309f7fd1..c7dd0f60b 100644 --- a/json_rpc/index.js +++ b/json_rpc/index.js @@ -11,7 +11,7 @@ const getEventHandlerApis = require('./event-handler'); const getNetworkApis = require('./network'); const getTransactionApis = require('./transaction'); const getVersionApis = require('./version'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); /** * Defines the list of funtions which are accessibly to clients through the @@ -42,7 +42,7 @@ module.exports = function getApis(node, p2pServer, eventHandler, minProtocolVers ...getVersionApis(minProtocolVersion, maxProtocolVersion), }); } else { - Object.assign(apis, { [JSON_RPC_METHOD.P2P_GET_PEER_CANDIDATE_INFO]: + Object.assign(apis, { [JSON_RPC_METHODS.P2P_GET_PEER_CANDIDATE_INFO]: getNetworkApis(node, p2pServer).p2p_getPeerCandidateInfo }); } if (eventHandler !== null) { diff --git a/json_rpc/injection.js b/json_rpc/injection.js index 59c20ecf0..95b1c2d0e 100644 --- a/json_rpc/injection.js +++ b/json_rpc/injection.js @@ -3,11 +3,11 @@ const { trafficStatsManager, } = require('../common/constants'); const JsonRpcUtil = require('./json-rpc-util'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); module.exports = function getInjectionApis(node, p2pServer) { return { - [JSON_RPC_METHOD.AIN_GET_BOOTSTRAP_PUB_KEY]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_BOOTSTRAP_PUB_KEY]: function(args, done) { const beginTime = Date.now(); const result = node.bootstrapAccount ? node.bootstrapAccount.public_key : null; const latency = Date.now() - beginTime; @@ -15,7 +15,7 @@ module.exports = function getInjectionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY]: async function(args, done) { + [JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY]: async function(args, done) { const beginTime = Date.now(); let result = false; if (await node.injectAccountFromPrivateKey(args.encryptedPrivateKey)) { @@ -27,7 +27,7 @@ module.exports = function getInjectionApis(node, p2pServer) { return JsonRpcUtil.addProtocolVersion({ result }); }, - [JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_KEYSTORE]: async function(args, done) { + [JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_KEYSTORE]: async function(args, done) { const beginTime = Date.now(); let result = false; if (await node.injectAccountFromKeystore(args.encryptedKeystore, args.encryptedPassword)) { @@ -39,7 +39,7 @@ module.exports = function getInjectionApis(node, p2pServer) { return JsonRpcUtil.addProtocolVersion({ result }); }, - [JSON_RPC_METHOD.AIN_INJECT_ACCOUNT_FROM_HD_WALLET]: async function(args, done) { + [JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_HD_WALLET]: async function(args, done) { const beginTime = Date.now(); let result = false; if (await node.injectAccountFromHDWallet(args.encryptedMnemonic, args.index)) { diff --git a/json_rpc/network.js b/json_rpc/network.js index 53c8a9a95..68c82c310 100644 --- a/json_rpc/network.js +++ b/json_rpc/network.js @@ -4,11 +4,11 @@ const { trafficStatsManager, } = require('../common/constants'); const JsonRpcUtil = require('./json-rpc-util'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); module.exports = function getNetworkApis(node, p2pServer) { return { - [JSON_RPC_METHOD.NET_LISTENING]: function(args, done) { + [JSON_RPC_METHODS.NET_LISTENING]: function(args, done) { const beginTime = Date.now(); const peerCount = Object.keys(p2pServer.inbound).length; const result = !!peerCount; @@ -17,7 +17,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.NET_PEER_COUNT]: function(args, done) { + [JSON_RPC_METHODS.NET_PEER_COUNT]: function(args, done) { const beginTime = Date.now(); const peerCount = Object.keys(p2pServer.inbound).length; const latency = Date.now() - beginTime; @@ -25,7 +25,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result: peerCount })); }, - [JSON_RPC_METHOD.NET_SYNCING]: function(args, done) { + [JSON_RPC_METHODS.NET_SYNCING]: function(args, done) { const beginTime = Date.now(); const result = (node.state === BlockchainNodeStates.CHAIN_SYNCING); const latency = Date.now() - beginTime; @@ -35,7 +35,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.NET_GET_NETWORK_ID]: function(args, done) { + [JSON_RPC_METHODS.NET_GET_NETWORK_ID]: function(args, done) { const beginTime = Date.now(); const result = node.getBlockchainParam('genesis/network_id'); const latency = Date.now() - beginTime; @@ -43,7 +43,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.NET_GET_CHAIN_ID]: function(args, done) { + [JSON_RPC_METHODS.NET_GET_CHAIN_ID]: function(args, done) { const beginTime = Date.now(); const result = node.getBlockchainParam('genesis/chain_id'); const latency = Date.now() - beginTime; @@ -51,7 +51,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.NET_CONSENSUS_STATUS]: function(args, done) { + [JSON_RPC_METHODS.NET_CONSENSUS_STATUS]: function(args, done) { const beginTime = Date.now(); const result = p2pServer.consensus.getStatus(); const latency = Date.now() - beginTime; @@ -59,7 +59,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.NET_RAW_CONSENSUS_STATUS]: function(args, done) { + [JSON_RPC_METHODS.NET_RAW_CONSENSUS_STATUS]: function(args, done) { const beginTime = Date.now(); const result = p2pServer.consensus.getRawStatus(); const latency = Date.now() - beginTime; @@ -67,7 +67,7 @@ module.exports = function getNetworkApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.P2P_GET_PEER_CANDIDATE_INFO]: function(args, done) { + [JSON_RPC_METHODS.P2P_GET_PEER_CANDIDATE_INFO]: function(args, done) { const beginTime = Date.now(); const result = p2pServer.client.getPeerCandidateInfo(); const latency = Date.now() - beginTime; diff --git a/json_rpc/transaction.js b/json_rpc/transaction.js index 83ef0a594..f9ac617a6 100644 --- a/json_rpc/transaction.js +++ b/json_rpc/transaction.js @@ -8,11 +8,11 @@ const { const { JsonRpcApiResultCode } = require('../common/result-code'); const CommonUtil = require('../common/common-util'); const Transaction = require('../tx-pool/transaction'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); module.exports = function getTransactionApis(node, p2pServer) { return { - [JSON_RPC_METHOD.AIN_GET_PENDING_TRANSACTIONS]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_PENDING_TRANSACTIONS]: function(args, done) { const beginTime = Date.now(); const result = node.tp.transactions; const latency = Date.now() - beginTime; @@ -20,7 +20,7 @@ module.exports = function getTransactionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_TRANSACTION_POOL_SIZE_UTILIZATION]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_TRANSACTION_POOL_SIZE_UTILIZATION]: function(args, done) { const beginTime = Date.now(); const address = args.address; const txPoolSizeUtil = node.getTxPoolSizeUtilization(address); @@ -29,7 +29,7 @@ module.exports = function getTransactionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result: txPoolSizeUtil })); }, - [JSON_RPC_METHOD.AIN_GET_TRANSACTION_BY_HASH]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_TRANSACTION_BY_HASH]: function(args, done) { const beginTime = Date.now(); const transactionInfo = node.getTransactionByHash(args.hash); const latency = Date.now() - beginTime; @@ -37,7 +37,7 @@ module.exports = function getTransactionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result: transactionInfo })); }, - [JSON_RPC_METHOD.AIN_GET_TRANSACTION_BY_BLOCK_HASH_AND_INDEX]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_TRANSACTION_BY_BLOCK_HASH_AND_INDEX]: function(args, done) { const beginTime = Date.now(); let result = null; if (args.block_hash && Number.isInteger(args.index)) { @@ -56,7 +56,7 @@ module.exports = function getTransactionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_GET_TRANSACTION_BY_BLOCK_NUMBER_AND_INDEX]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_TRANSACTION_BY_BLOCK_NUMBER_AND_INDEX]: function(args, done) { const beginTime = Date.now(); let result = null; if (Number.isInteger(args.block_number) && Number.isInteger(args.index)) { @@ -75,7 +75,7 @@ module.exports = function getTransactionApis(node, p2pServer) { done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION]: function(args, done) { + [JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION]: function(args, done) { const beginTime = Date.now(); const txBytesLimit = node.getBlockchainParam('resource/tx_bytes_limit'); if (sizeof(args) > txBytesLimit) { @@ -127,7 +127,7 @@ module.exports = function getTransactionApis(node, p2pServer) { } }, - [JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH]: function(args, done) { + [JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH]: function(args, done) { const beginTime = Date.now(); const batchTxListSizeLimit = node.getBlockchainParam('resource/batch_tx_list_size_limit'); if (!args.tx_list || !CommonUtil.isArray(args.tx_list)) { diff --git a/json_rpc/version.js b/json_rpc/version.js index 7ac262e70..75491f88a 100644 --- a/json_rpc/version.js +++ b/json_rpc/version.js @@ -6,11 +6,11 @@ const { } = require('../common/constants'); const { JsonRpcApiResultCode } = require('../common/result-code'); const JsonRpcUtil = require('./json-rpc-util'); -const { JSON_RPC_METHOD } = require('./constants'); +const { JSON_RPC_METHODS } = require('./constants'); module.exports = function getVersionApis(minProtocolVersion, maxProtocolVersion) { return { - [JSON_RPC_METHOD.AIN_GET_PROTOCOL_VERSION]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_PROTOCOL_VERSION]: function(args, done) { const beginTime = Date.now(); const result = BlockchainConsts.CURRENT_PROTOCOL_VERSION; const latency = Date.now() - beginTime; @@ -18,7 +18,7 @@ module.exports = function getVersionApis(minProtocolVersion, maxProtocolVersion) done(null, JsonRpcUtil.addProtocolVersion({ result })); }, - [JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION]: function(args, done) { + [JSON_RPC_METHODS.AIN_CHECK_PROTOCOL_VERSION]: function(args, done) { const beginTime = Date.now(); const version = args.protoVer; const coercedVer = semver.coerce(version); diff --git a/p2p/index.js b/p2p/index.js index e6d02d543..b0105def5 100644 --- a/p2p/index.js +++ b/p2p/index.js @@ -24,7 +24,7 @@ const FileUtil = require('../common/file-util'); const P2pUtil = require('./p2p-util'); const { sendGetRequest } = require('../common/network-util'); const { Block } = require('../blockchain/block'); -const { JSON_RPC_METHOD } = require('../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../json_rpc/constants'); class P2pClient { constructor(node, minProtocolVersion, maxProtocolVersion) { @@ -1178,7 +1178,7 @@ class P2pClient { return; } const resp = await sendGetRequest( - peerCandidateJsonRpcUrl, JSON_RPC_METHOD.P2P_GET_PEER_CANDIDATE_INFO, { }); + peerCandidateJsonRpcUrl, JSON_RPC_METHODS.P2P_GET_PEER_CANDIDATE_INFO, { }); const peerCandidateInfo = _.get(resp, 'data.result.result'); if (!peerCandidateInfo) { logger.error(`Invalid peer candidate info from peer candidate url ` + diff --git a/p2p/server.js b/p2p/server.js index 5f208b4db..d20faa4c9 100644 --- a/p2p/server.js +++ b/p2p/server.js @@ -41,7 +41,7 @@ const { } = require('../common/network-util'); const P2pUtil = require('./p2p-util'); const PathUtil = require('../common/path-util'); -const { JSON_RPC_METHOD } = require('../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../json_rpc/constants'); const DISK_USAGE_PATH = os.platform() === 'win32' ? 'c:' : '/'; @@ -961,7 +961,7 @@ class P2pServer { static async getLastReportedBlockNumber(parentChainEndpoint, shardingPath) { const resp = await sendGetRequest( parentChainEndpoint, - JSON_RPC_METHOD.AIN_GET, + JSON_RPC_METHODS.AIN_GET, { type: ReadDbOperations.GET_VALUE, ref: `${shardingPath}/${PredefinedDbPaths.DOT_SHARD}/${ShardingProperties.LATEST_BLOCK_NUMBER}` @@ -971,7 +971,7 @@ class P2pServer { } static async getShardingAppConfig(parentChainEndpoint, appName) { - const resp = await sendGetRequest(parentChainEndpoint, JSON_RPC_METHOD.AIN_GET, { + const resp = await sendGetRequest(parentChainEndpoint, JSON_RPC_METHODS.AIN_GET, { type: ReadDbOperations.GET_VALUE, ref: PathUtil.getManageAppConfigPath(appName) }); diff --git a/test/integration/blockchain.test.js b/test/integration/blockchain.test.js index 1982ce704..bba48685d 100644 --- a/test/integration/blockchain.test.js +++ b/test/integration/blockchain.test.js @@ -20,7 +20,7 @@ const { setUpApp } = require('../test-util'); const { Block } = require('../../blockchain/block'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); const PROJECT_ROOT = require('path').dirname(__filename) + '/../../'; const TRACKER_SERVER = PROJECT_ROOT + 'tracker-server/index.js'; @@ -175,7 +175,7 @@ async function sendTransactions(sentOperations) { 'POST', serverList[serverIndex] + '/json-rpc', { json: { jsonrpc: '2.0', - method: JSON_RPC_METHOD.AIN_GET_NONCE, + method: JSON_RPC_METHODS.AIN_GET_NONCE, id: 0, params: { address, @@ -229,7 +229,7 @@ describe('Blockchain Cluster', () => { await waitUntilNetworkIsReady(serverList); jsonRpcClient = jayson.client.http(server2 + JSON_RPC_ENDPOINT); promises.push(new Promise((resolve) => { - jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_LAST_BLOCK, + jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_LAST_BLOCK, {protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { if (err) { resolve(); @@ -273,7 +273,7 @@ describe('Blockchain Cluster', () => { await sendTransactions(sentOperations); return new Promise((resolve) => { jayson.client.http(server1 + JSON_RPC_ENDPOINT) - .request(JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, + .request(JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, function(err, response) { if (err) throw err; @@ -283,7 +283,7 @@ describe('Blockchain Cluster', () => { }).then(() => { return new Promise((resolve) => { jayson.client.http(serverList[i] + JSON_RPC_ENDPOINT) - .request(JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, + .request(JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, function (err, response) { if (err) throw err; @@ -365,7 +365,7 @@ describe('Blockchain Cluster', () => { 'POST', serverList[i] + '/json-rpc', { json: { jsonrpc: '2.0', - method: JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, + method: JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, id: 0, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -414,7 +414,7 @@ describe('Blockchain Cluster', () => { const blocks = parseOrLog(syncRequest('POST', serverList[i] + '/json-rpc', { json: { - jsonrpc: '2.0', method: JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, id: 0, + jsonrpc: '2.0', method: JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, id: 0, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION } } }) @@ -509,7 +509,7 @@ describe('Blockchain Cluster', () => { it('ain_getBlockHeadersList', async () => { await sendTransactions(sentOperations); return new Promise((resolve) => { - jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_BLOCK_HEADERS_LIST, + jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_BLOCK_HEADERS_LIST, {from: 2, to: 4, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { if (err) throw err; @@ -525,14 +525,14 @@ describe('Blockchain Cluster', () => { it('ain_getBlockByHash and ain_getBlockByNumber', async () => { await sendTransactions(sentOperations); return new Promise((resolve) => { - jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, + jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_BLOCK_BY_NUMBER, {number: 2, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { if (err) throw err; resolve(response.result.result); }); }).then((resultByNumber) => { return new Promise((resolve) => { - jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_BLOCK_BY_HASH, + jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_BLOCK_BY_HASH, {hash: resultByNumber.hash, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { if (err) throw err; @@ -556,9 +556,9 @@ describe('Blockchain Cluster', () => { it('pendingNonceTracker', () => { return new Promise((resolve, reject) => { let promises = []; - promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); - promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); Promise.all(promises).then(res => { promises = []; @@ -571,9 +571,9 @@ describe('Blockchain Cluster', () => { value: 'testing...' } }).body.toString('utf-8')).result.tx_hash; - promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); - promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); Promise.all(promises).then(async resAfterBroadcast => { promises = []; @@ -596,9 +596,9 @@ describe('Blockchain Cluster', () => { return new Promise(async (resolve, reject) => { await waitForNewBlocks(server2); let promises = []; - promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); - promises.push(jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_NONCE, + promises.push(jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION })); Promise.all(promises).then(resAfterCommit => { const committedNonceAfterCommit = resAfterCommit[0].result.result; @@ -619,7 +619,7 @@ describe('Blockchain Cluster', () => { it('collected gas cost matches the gas_cost_total in the block', () => { return new Promise((resolve) => { jayson.client.http(serverList[1] + JSON_RPC_ENDPOINT) - .request(JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, + .request(JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, function (err, response) { if (err) throw err; @@ -690,7 +690,7 @@ describe('Blockchain Cluster', () => { describe('Protocol versions', () => { it('accepts API calls with correct protoVer', () => { - return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_BLOCK_BY_NUMBER, { number: 0, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) .then(res => { expect(res.result.result.number).to.equal(0); @@ -700,7 +700,7 @@ describe('Blockchain Cluster', () => { it('rejects API calls with invalid protoVer - case 1', () => { return jsonRpcClient.request( - JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, + JSON_RPC_METHODS.AIN_GET_BLOCK_BY_NUMBER, { number: 0, protoVer: 'a.b.c' }) .then(res => { expect(res.code).to.equal(40102); @@ -710,7 +710,7 @@ describe('Blockchain Cluster', () => { it('rejects API calls with invalid protoVer - case 2', () => { return jsonRpcClient.request( - JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, + JSON_RPC_METHODS.AIN_GET_BLOCK_BY_NUMBER, { number: 0, protoVer: '0.01.0' }) .then(res => { expect(res.code).to.equal(40102); @@ -720,7 +720,7 @@ describe('Blockchain Cluster', () => { it('rejects API calls with incompatible protoVer - case 1', () => { return jsonRpcClient.request( - JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, + JSON_RPC_METHODS.AIN_GET_BLOCK_BY_NUMBER, { number: 0, protoVer: 'v0.1' }) .then(res => { expect(res.code).to.equal(40103); @@ -730,7 +730,7 @@ describe('Blockchain Cluster', () => { it('rejects API calls with incompatible protoVer - case 2', () => { return jsonRpcClient.request( - JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, + JSON_RPC_METHODS.AIN_GET_BLOCK_BY_NUMBER, { number: 0, protoVer: '0.1.0' }) .then(res => { expect(res.code).to.equal(40103); @@ -740,7 +740,7 @@ describe('Blockchain Cluster', () => { it('rejects API calls with no protoVer', () => { return jsonRpcClient.request( - JSON_RPC_METHOD.AIN_GET_BLOCK_BY_NUMBER, + JSON_RPC_METHODS.AIN_GET_BLOCK_BY_NUMBER, { number: 0 }) .then(res => { expect(res.code).to.equal(40101); diff --git a/test/integration/consensus.test.js b/test/integration/consensus.test.js index 0420fcf66..71a0c0825 100644 --- a/test/integration/consensus.test.js +++ b/test/integration/consensus.test.js @@ -29,7 +29,7 @@ const { const { Block } = require('../../blockchain/block'); const Functions = require('../../db/functions'); const ConsensusUtil = require('../../consensus/consensus-util'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); const PROJECT_ROOT = require('path').dirname(__filename) + '/../../'; const TRACKER_SERVER = PROJECT_ROOT + 'tracker-server/index.js'; @@ -142,7 +142,7 @@ describe('Consensus', () => { await waitUntilNetworkIsReady(serverList); jsonRpcClient = jayson.client.http(server2 + JSON_RPC_ENDPOINT); promises.push(new Promise((resolve) => { - jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_LAST_BLOCK, + jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_LAST_BLOCK, {protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION}, function(err, response) { if (err) { resolve(); @@ -192,7 +192,7 @@ describe('Consensus', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('a2b5848760d81afe205884284716f90356ad82be5ab77b8130980bdb0b7ba2ba', 'hex')); - const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + const res = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION diff --git a/test/integration/event_handler.test.js b/test/integration/event_handler.test.js index 66d09a3a7..6443dc271 100644 --- a/test/integration/event_handler.test.js +++ b/test/integration/event_handler.test.js @@ -18,7 +18,7 @@ const { setUpApp, waitUntilNetworkIsReady, } = require('../test-util'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); const SET_VALUE_ENDPOINT = '/set_value'; const PROJECT_ROOT = require('path').dirname(__filename) + '/../../'; const TRACKER_SERVER = PROJECT_ROOT + 'tracker-server/index.js'; @@ -70,7 +70,7 @@ function startServer(application, serverName, envVars, stdioInherit = false) { function getEventHandlerNetworkInfo() { return _.get(parseOrLog(syncRequest('POST', serverList[EVENT_HANDLER_NODE_INDEX] + '/json-rpc', { json: { - jsonrpc: '2.0', method: JSON_RPC_METHOD.NET_GET_EVENT_HANDLER_NETWORK_INFO, id: 0, + jsonrpc: '2.0', method: JSON_RPC_METHODS.NET_GET_EVENT_HANDLER_NETWORK_INFO, id: 0, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, }, }).body.toString('utf-8')), 'result.result'); @@ -79,7 +79,7 @@ function getEventHandlerNetworkInfo() { function getEventHandlerChannelInfo() { return _.get(parseOrLog(syncRequest('POST', serverList[EVENT_HANDLER_NODE_INDEX] + '/json-rpc', { json: { - jsonrpc: '2.0', method: JSON_RPC_METHOD.AIN_GET_EVENT_HANDLER_CHANNEL_INFO, id: 0, + jsonrpc: '2.0', method: JSON_RPC_METHODS.AIN_GET_EVENT_HANDLER_CHANNEL_INFO, id: 0, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, }, }).body.toString('utf-8')), 'result.result'); @@ -88,7 +88,7 @@ function getEventHandlerChannelInfo() { function getEventHandlerFilterInfo() { return _.get(parseOrLog(syncRequest('POST', serverList[EVENT_HANDLER_NODE_INDEX] + '/json-rpc', { json: { - jsonrpc: '2.0', method: JSON_RPC_METHOD.AIN_GET_EVENT_HANDLER_FILTER_INFO, id: 0, + jsonrpc: '2.0', method: JSON_RPC_METHODS.AIN_GET_EVENT_HANDLER_FILTER_INFO, id: 0, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, }, }).body.toString('utf-8')), 'result.result'); diff --git a/test/integration/function.test.js b/test/integration/function.test.js index 8e8c51a9d..4c1a9b939 100644 --- a/test/integration/function.test.js +++ b/test/integration/function.test.js @@ -23,7 +23,7 @@ const { getBlockByNumber, eraseStateGas, } = require('../test-util'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); const PROJECT_ROOT = require('path').dirname(__filename) + "/../../" const TRACKER_SERVER = PROJECT_ROOT + "tracker-server/index.js" @@ -933,7 +933,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('a2b5848760d81afe205884284716f90356ad82be5ab77b8130980bdb0b7ba2ba', 'hex')); - const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + const res = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -4819,7 +4819,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + const res = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -4862,7 +4862,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + const res = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -5011,7 +5011,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + const res = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -5615,7 +5615,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + const res = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -5661,7 +5661,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + const res = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -5840,7 +5840,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction( closeCheckinTxBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const closeCheckinRes = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + const closeCheckinRes = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: closeCheckinTxBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -5958,7 +5958,7 @@ describe('Native Function', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from('d42f73de4ee706a4891dad643e0a65c0677020dbc2425f585442d0de2c742a44', 'hex')); - const res = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + const res = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION diff --git a/test/integration/node.test.js b/test/integration/node.test.js index 49a9b73e3..7efbcc4a0 100644 --- a/test/integration/node.test.js +++ b/test/integration/node.test.js @@ -20,7 +20,7 @@ const { waitUntilTxFinalized, eraseEvalResMatched } = require('../test-util'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); const PROJECT_ROOT = require('path').dirname(__filename) + "/../../" const TRACKER_SERVER = PROJECT_ROOT + "tracker-server/index.js" @@ -825,11 +825,11 @@ describe('Blockchain Node', () => { }); }); - describe(`${JSON_RPC_METHOD.AIN_GET} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_GET} api`, () => { it('returns the correct value', () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/test/test_value/some/path" @@ -841,7 +841,7 @@ describe('Blockchain Node', () => { it('returns error when invalid op_list is given', () => { const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET', op_list: null @@ -868,7 +868,7 @@ describe('Blockchain Node', () => { console.error(`Failed to check finalization of tx.`); } const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/test/test_value/some/path", @@ -894,7 +894,7 @@ describe('Blockchain Node', () => { console.error(`Failed to check finalization of tx.`); } const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/test/test_value/some/path", @@ -908,11 +908,11 @@ describe('Blockchain Node', () => { }); }); - describe(`${JSON_RPC_METHOD.AIN_MATCH_FUNCTION} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_MATCH_FUNCTION} api`, () => { it('returns correct value', () => { const ref = "/apps/test/test_function/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_FUNCTION, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_FUNCTION, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -936,11 +936,11 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_MATCH_RULE} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_MATCH_RULE} api`, () => { it('returns correct value (write)', () => { const ref = "/apps/test/test_rule/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_RULE, request) .then(res => { assert.deepEqual(res.result.result, { "write": { @@ -975,7 +975,7 @@ describe('Blockchain Node', () => { it('returns correct value (state)', () => { const ref = "/apps/test/test_rule/state"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_RULE, request) .then(res => { assert.deepEqual(res.result.result, { "write": { @@ -1023,7 +1023,7 @@ describe('Blockchain Node', () => { it('returns correct value (state & write)', () => { const ref = "/apps/test/test_rule/state/and/write"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_RULE, request) .then(res => { assert.deepEqual(res.result.result, { "write": { @@ -1062,11 +1062,11 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_MATCH_OWNER} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_MATCH_OWNER} api`, () => { it('returns correct value', () => { const ref = "/apps/test/test_owner/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_OWNER, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -1091,13 +1091,13 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_EVAL_RULE} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_EVAL_RULE} api`, () => { it('returns true', () => { const ref = "/apps/test/test_rule/some/path"; const value = "value"; const address = "abcd"; const request = { ref, value, address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_RULE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_EVAL_RULE, request) .then(res => { assert.deepEqual(eraseEvalResMatched(res.result.result), { "code": 0, @@ -1111,7 +1111,7 @@ describe('Blockchain Node', () => { const value = "value"; const address = "efgh"; const request = { ref, value, address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_RULE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_EVAL_RULE, request) .then(res => { res.result.result.message = 'erased'; assert.deepEqual(eraseEvalResMatched(res.result.result), { @@ -1123,13 +1123,13 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_EVAL_OWNER} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_EVAL_OWNER} api`, () => { it('returns correct value', () => { const ref = "/apps/test/test_owner/some/path"; const address = "abcd"; const permission = "write_owner"; const request = { ref, permission, address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_OWNER, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_EVAL_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "code": 0, @@ -1167,11 +1167,11 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_GET_STATE_PROOF} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_GET_STATE_PROOF} api`, () => { it('returns correct value', () => { const ref = '/values/blockchain_params/token/symbol'; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_GET_STATE_PROOF, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_GET_STATE_PROOF, request) .then(res => { expect(res.result.result['#state_ph']).to.not.equal(null); const verifResult = verifyStateProof(res.result.result); @@ -1187,22 +1187,22 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_GET_PROOF_HASH} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_GET_PROOF_HASH} api`, () => { it('returns correct value', () => { const ref = '/values/blockchain_params/token/symbol'; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_GET_PROOF_HASH, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_GET_PROOF_HASH, request) .then(res => { expect(res.result.result).to.not.equal(null); }) }) }) - describe(`${JSON_RPC_METHOD.AIN_GET_STATE_INFO} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_GET_STATE_INFO} api`, () => { it('returns correct value', () => { const ref = '/values/apps/test/test_state_info/some/path'; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_GET_STATE_INFO, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_GET_STATE_INFO, request) .then(res => { const stateInfo = res.result.result; // Erase some properties for stable comparison. @@ -1221,10 +1221,10 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_GET_STATE_USAGE} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_GET_STATE_USAGE} api`, () => { it('with existing app name', () => { const request = { app_name: 'test', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_GET_STATE_USAGE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_GET_STATE_USAGE, request) .then(res => { const stateUsage = res.result.result; assert.deepEqual(stateUsage, { @@ -1249,7 +1249,7 @@ describe('Blockchain Node', () => { it('with non-existing app name', () => { const request = { app_name: 'app_non_existing', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_GET_STATE_USAGE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_GET_STATE_USAGE, request) .then(res => { const stateUsage = res.result.result; assert.deepEqual(stateUsage, { @@ -1273,20 +1273,20 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_GET_PROTOCOL_VERSION} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_GET_PROTOCOL_VERSION} api`, () => { it('returns the correct version', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GET_PROTOCOL_VERSION, {}) + return client.request(JSON_RPC_METHODS.AIN_GET_PROTOCOL_VERSION, {}) .then(res => { expect(res.result.protoVer).to.equal(BlockchainConsts.CURRENT_PROTOCOL_VERSION); }) }); }); - describe(`${JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_CHECK_PROTOCOL_VERSION} api`, () => { it('returns success code', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) + return client.request(JSON_RPC_METHODS.AIN_CHECK_PROTOCOL_VERSION, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) .then(res => { expect(res.result.code).to.equal(0); expect(res.result.result).to.equal("Success"); @@ -1295,7 +1295,7 @@ describe('Blockchain Node', () => { it('returns version not specified code', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION, {}) + return client.request(JSON_RPC_METHODS.AIN_CHECK_PROTOCOL_VERSION, {}) .then(res => { expect(res.result.code).to.equal(30101); expect(res.result.message).to.equal("Protocol version not specified."); @@ -1304,7 +1304,7 @@ describe('Blockchain Node', () => { it('returns invalid version code', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION, { protoVer: 'a.b.c' }) + return client.request(JSON_RPC_METHODS.AIN_CHECK_PROTOCOL_VERSION, { protoVer: 'a.b.c' }) .then(res => { expect(res.result.code).to.equal(30102); expect(res.result.message).to.equal("Invalid protocol version."); @@ -1313,7 +1313,7 @@ describe('Blockchain Node', () => { it('returns incompatible version code for ill-formatted version', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION, { protoVer: 0 }) + return client.request(JSON_RPC_METHODS.AIN_CHECK_PROTOCOL_VERSION, { protoVer: 0 }) .then(res => { expect(res.result.code).to.equal(30103); expect(res.result.message).to.equal("Incompatible protocol version."); @@ -1322,7 +1322,7 @@ describe('Blockchain Node', () => { it('returns incompatible version code for low version', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_CHECK_PROTOCOL_VERSION, { protoVer: '0.0.1' }) + return client.request(JSON_RPC_METHODS.AIN_CHECK_PROTOCOL_VERSION, { protoVer: '0.0.1' }) .then(res => { expect(res.result.code).to.equal(30103); expect(res.result.message).to.equal("Incompatible protocol version."); @@ -1330,11 +1330,11 @@ describe('Blockchain Node', () => { }); }) - describe(`${JSON_RPC_METHOD.AIN_GET_ADDRESS} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_GET_ADDRESS} api`, () => { it('returns the correct node address', () => { const expAddr = '0x01A0980d2D4e418c7F27e1ef539d01A5b5E93204'; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET_ADDRESS, { + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET_ADDRESS, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }).then(res => { expect(res.result.result).to.equal(expAddr); @@ -2977,7 +2977,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION} api`, () => { const account = { address: "0x9534bC7529961E5737a3Dd317BdEeD41AC08a52D", private_key: "e96292ef0676287908fc3461f747f106b7b9336f183b1766f83672fbe893664d", @@ -3091,7 +3091,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3129,7 +3129,7 @@ describe('Blockchain Node', () => { it('accepts a transaction with numbered nonce', () => { const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GET_NONCE, { + return client.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address: account.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3148,7 +3148,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3189,7 +3189,7 @@ describe('Blockchain Node', () => { it('accepts a transaction with account registration gas amount from nonce', () => { // NOTE: account2 does not have balance nor nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GET_NONCE, { + return client.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address: account2.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3208,7 +3208,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account2.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3249,7 +3249,7 @@ describe('Blockchain Node', () => { it('accepts a transaction without account registration gas amount from nonce', () => { // NOTE: account2 already has nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GET_NONCE, { + return client.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address: account2.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3268,7 +3268,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account2.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3321,7 +3321,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account09.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3389,7 +3389,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account09.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3457,7 +3457,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account09.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3513,7 +3513,7 @@ describe('Blockchain Node', () => { it('accepts a multi-set transaction with account registration gas amount from nonce', () => { // NOTE: account4 does not have balance nor nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GET_NONCE, { + return client.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address: account4.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3542,7 +3542,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account4.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3591,7 +3591,7 @@ describe('Blockchain Node', () => { it('accepts a multi-set transaction without account registration gas amount from nonce', () => { // NOTE: account4 already has nonce/timestamp. const client = jayson.client.http(server1 + '/json-rpc'); - return client.request(JSON_RPC_METHOD.AIN_GET_NONCE, { + return client.request(JSON_RPC_METHODS.AIN_GET_NONCE, { address: account4.address, from: 'pending', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3620,7 +3620,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account4.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3683,7 +3683,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3764,7 +3764,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3811,7 +3811,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3840,7 +3840,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { transaction: txBody, // wrong field name signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3869,7 +3869,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3898,7 +3898,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature: signature + '0', // invalid signature protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION @@ -3914,7 +3914,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH} api`, () => { + describe(`${JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH} api`, () => { const account = { address: "0x85a620A5A46d01cc1fCF49E73ab00710d4da943E", private_key: "b542fc2ca4a68081b3ba238888d3a8783354c3aa81711340fd69f6ff32798525", @@ -4113,7 +4113,7 @@ describe('Blockchain Node', () => { signature }); } - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: txList, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }).then((res) => { @@ -4139,7 +4139,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: { // should be an array tx_body: txBody, signature, @@ -4179,7 +4179,7 @@ describe('Blockchain Node', () => { signature, }); } - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: txList, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }).then((res) => { @@ -4215,7 +4215,7 @@ describe('Blockchain Node', () => { signature, }); } - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: txList, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }).then((res) => { @@ -4262,7 +4262,7 @@ describe('Blockchain Node', () => { signature, }); } - const res1 = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + const res1 = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: txList1, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }); @@ -4286,7 +4286,7 @@ describe('Blockchain Node', () => { tx_body: txBody, signature, }); - const res2 = await client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + const res2 = await client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: txList2, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }); @@ -4322,7 +4322,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBodyBefore, @@ -4362,7 +4362,7 @@ describe('Blockchain Node', () => { timestamp: Date.now(), nonce: -1 }; - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBodyBefore, @@ -4403,7 +4403,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBodyBefore, @@ -4444,7 +4444,7 @@ describe('Blockchain Node', () => { }; const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBodyBefore, diff --git a/test/integration/sharding.test.js b/test/integration/sharding.test.js index 45756dc74..b28f52b86 100644 --- a/test/integration/sharding.test.js +++ b/test/integration/sharding.test.js @@ -34,7 +34,7 @@ const { setUpApp, eraseEvalResMatched, } = require('../test-util'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); const PROJECT_ROOT = require('path').dirname(__filename) + "/../../" const TRACKER_SERVER = PROJECT_ROOT + "tracker-server/index.js" @@ -1252,7 +1252,7 @@ describe('Sharding', () => { it('ain_get with is_global = false', () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/test/test_value/some/path" @@ -1265,7 +1265,7 @@ describe('Sharding', () => { it('ain_get with is_global = false (explicit)', () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/test/test_value/some/path", @@ -1279,7 +1279,7 @@ describe('Sharding', () => { it('ain_get with is_global = true', () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); - return jsonRpcClient.request(JSON_RPC_METHOD.AIN_GET, { + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, type: 'GET_VALUE', ref: "/apps/afan/apps/test/test_value/some/path", @@ -1295,7 +1295,7 @@ describe('Sharding', () => { it('ain_matchFunction with is_global = false', () => { const ref = "/apps/test/test_function/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_FUNCTION, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_FUNCTION, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -1321,7 +1321,7 @@ describe('Sharding', () => { it('ain_matchFunction with is_global = true', () => { const ref = "/apps/afan/apps/test/test_function/some/path"; const request = { ref, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_FUNCTION, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_FUNCTION, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -1349,7 +1349,7 @@ describe('Sharding', () => { it('ain_matchRule with is_global = false', () => { const ref = "/apps/test/test_rule/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_RULE, request) .then(res => { assert.deepEqual(res.result.result, { "write": { @@ -1384,7 +1384,7 @@ describe('Sharding', () => { it('ain_matchRule with is_global = true', () => { const ref = "/apps/afan/apps/test/test_rule/some/path"; const request = { ref, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_RULE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_RULE, request) .then(res => { assert.deepEqual(res.result.result, { "write": { @@ -1421,7 +1421,7 @@ describe('Sharding', () => { it('ain_matchOwner with is_global = false', () => { const ref = "/apps/test/test_owner/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_OWNER, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -1448,7 +1448,7 @@ describe('Sharding', () => { it('ain_matchOwner with is_global = true', () => { const ref = "/apps/afan/apps/test/test_owner/some/path"; const request = { ref, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_MATCH_OWNER, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_MATCH_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "matched_path": { @@ -1479,7 +1479,7 @@ describe('Sharding', () => { const value = "value"; const address = "abcd"; const request = { ref, value, address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_RULE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_EVAL_RULE, request) .then(res => { assert.deepEqual(eraseEvalResMatched(res.result.result), { "code": 0, @@ -1494,7 +1494,7 @@ describe('Sharding', () => { const address = "abcd"; const request = { ref, value, address, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_RULE, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_EVAL_RULE, request) .then(res => { assert.deepEqual(eraseEvalResMatched(res.result.result), { "code": 0, @@ -1510,7 +1510,7 @@ describe('Sharding', () => { const address = "abcd"; const permission = "write_owner"; const request = { ref, permission, address, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_OWNER, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_EVAL_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "code": 0, @@ -1553,7 +1553,7 @@ describe('Sharding', () => { const permission = "write_owner"; const request = { ref, permission, address, is_global: true, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; - return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHOD.AIN_EVAL_OWNER, request) + return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_EVAL_OWNER, request) .then(res => { assert.deepEqual(res.result.result, { "code": 0, @@ -2038,7 +2038,7 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) .then((res) => { const result = _.get(res, 'result.result', null); @@ -2087,7 +2087,7 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) .then((res) => { const result = _.get(res, 'result.result', null); @@ -2136,7 +2136,7 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, { tx_body: txBody, signature, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) .then((res) => { const result = _.get(res, 'result.result', null); @@ -2186,7 +2186,7 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBody, @@ -2244,7 +2244,7 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBody, @@ -2305,7 +2305,7 @@ describe('Sharding', () => { } const signature = ainUtil.ecSignTransaction(txBody, Buffer.from(account.private_key, 'hex')); - return client.request(JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION_BATCH, { + return client.request(JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, { tx_list: [ { tx_body: txBody, diff --git a/test/test-util.js b/test/test-util.js index 2ca962ae5..6c112089c 100644 --- a/test/test-util.js +++ b/test/test-util.js @@ -9,7 +9,7 @@ const { StateVersions } = require('../common/constants'); const CommonUtil = require('../common/common-util'); -const { JSON_RPC_METHOD } = require('../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../json_rpc/constants'); const GET_OPTIONS_INCLUDE_ALL = { includeTreeInfo: true, @@ -177,7 +177,7 @@ async function waitUntilNodeSyncs(server) { isSyncing = parseOrLog(syncRequest('POST', server + '/json-rpc', { json: { - jsonrpc: '2.0', method: JSON_RPC_METHOD.NET_SYNCING, id: 0, + jsonrpc: '2.0', method: JSON_RPC_METHODS.NET_SYNCING, id: 0, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION } } }) diff --git a/tools/api-access/addToDevClientApiIpWhitelist.js b/tools/api-access/addToDevClientApiIpWhitelist.js index de7c1293e..1bfed6702 100644 --- a/tools/api-access/addToDevClientApiIpWhitelist.js +++ b/tools/api-access/addToDevClientApiIpWhitelist.js @@ -4,19 +4,19 @@ const ainUtil = require('@ainblockchain/ain-util'); const stringify = require('fast-json-stable-stringify'); const { BlockchainConsts } = require('../../common/constants'); const { getAccountPrivateKey } = require('./util'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); async function sendAddToDevClientApiIpWhitelistRequest(endpointUrl, privateKey, chainId, ip) { const message = { timestamp: Date.now(), - method: JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, + method: JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, ip, }; const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); return await axios.post( `${endpointUrl}/json-rpc`, { - method: JSON_RPC_METHOD.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, + method: JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, message, diff --git a/tools/api-access/removeFromDevClientApiIpWhitelist.js b/tools/api-access/removeFromDevClientApiIpWhitelist.js index 3f44db1d0..031c3da90 100644 --- a/tools/api-access/removeFromDevClientApiIpWhitelist.js +++ b/tools/api-access/removeFromDevClientApiIpWhitelist.js @@ -4,19 +4,19 @@ const ainUtil = require('@ainblockchain/ain-util'); const stringify = require('fast-json-stable-stringify'); const { BlockchainConsts } = require('../../common/constants'); const { getAccountPrivateKey } = require('./util'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); async function sendRemoveFromToDevClientApiIpWhitelistRequest(endpointUrl, privateKey, chainId, ip) { const message = { timestamp: Date.now(), - method: JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST, + method: JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST, ip, }; const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); return await axios.post( `${endpointUrl}/json-rpc`, { - method: JSON_RPC_METHOD.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST, + method: JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, message, diff --git a/tools/checkin/sendCloseCheckinTx.js b/tools/checkin/sendCloseCheckinTx.js index a78020240..1f1d13259 100644 --- a/tools/checkin/sendCloseCheckinTx.js +++ b/tools/checkin/sendCloseCheckinTx.js @@ -2,11 +2,11 @@ const path = require('path'); const _ = require('lodash'); const { signAndSendTx, confirmTransaction } = require('../util'); const { sendGetRequest } = require('../../common/network-util'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); let config = {}; async function buildCloseCheckinTxBody(fromAddr, checkinId, failed = false) { - const request = (await sendGetRequest(`${config.endpointUrl}/json-rpc`, JSON_RPC_METHOD.AIN_GET, { + const request = (await sendGetRequest(`${config.endpointUrl}/json-rpc`, JSON_RPC_METHODS.AIN_GET, { type: 'GET_VALUE', ref: `/checkin/requests/ETH/3/0xB16c0C80a81f73204d454426fC413CAe455525A7/${fromAddr}/${checkinId}`, })).data.result.result; diff --git a/tools/proposer-stats/getProposerStats.js b/tools/proposer-stats/getProposerStats.js index f0600b9a9..eddddb6cb 100644 --- a/tools/proposer-stats/getProposerStats.js +++ b/tools/proposer-stats/getProposerStats.js @@ -6,14 +6,14 @@ const axios = require('axios'); const _ = require('lodash'); const CommonUtil = require('../../common/common-util'); const { BlockchainConsts, NodeConfigs } = require('../../common/constants'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); async function getBlockList(from, to, endpointUrl) { console.log(`getting block list from ${from} to ${to}`); return await axios.post( `${endpointUrl}/json-rpc`, { - method: JSON_RPC_METHOD.AIN_GET_BLOCK_LIST, + method: JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, params: { from, to, diff --git a/tools/simple-load-test/index.js b/tools/simple-load-test/index.js index 10d2232ea..04e5f3a3b 100644 --- a/tools/simple-load-test/index.js +++ b/tools/simple-load-test/index.js @@ -9,7 +9,7 @@ const commandLineArgs = require('command-line-args'); const getUsage = require('command-line-usage'); const CommonUtil = require('../../common/common-util'); const { signTx } = require('../util'); -const { JSON_RPC_METHOD } = require('../../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); const delay = (time) => new Promise((resolve) => setTimeout(resolve, time)); const testPath = '/apps/loadtest'; const ainPrivateKey = '4207f5dcacb1b601d3a1f8cb10afaca158f6ebe383c0b30d02b39f8d2060cce3'; @@ -58,7 +58,7 @@ const sections = [ function sendTx(endpointUrl, signedTx) { return axios.post(`${endpointUrl}/json-rpc`, { - method: JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, + method: JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, params: signedTx, jsonrpc: '2.0', id: 0, diff --git a/tools/util.js b/tools/util.js index fea443259..33a977fe1 100644 --- a/tools/util.js +++ b/tools/util.js @@ -2,7 +2,7 @@ const _ = require('lodash'); const axios = require('axios'); const { BlockchainConsts } = require('../common/constants'); const CommonUtil = require('../common/common-util'); -const { JSON_RPC_METHOD } = require('../json_rpc/constants'); +const { JSON_RPC_METHODS } = require('../json_rpc/constants'); // FIXME(minsulee2): this is duplicated function see: ./common/network-util.js function signAndSendTx(endpointUrl, txBody, privateKey, chainId) { @@ -15,7 +15,7 @@ function signAndSendTx(endpointUrl, txBody, privateKey, chainId) { return axios.post( `${endpointUrl}/json-rpc`, { - method: JSON_RPC_METHOD.AIN_SEND_SIGNED_TRANSACTION, + method: JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, params: signedTx, jsonrpc: '2.0', id: 0 @@ -36,7 +36,7 @@ async function sendGetTxByHashRequest(endpointUrl, txHash) { return await axios.post( `${endpointUrl}/json-rpc`, { - method: JSON_RPC_METHOD.AIN_GET_TRANSACTION_BY_HASH, + method: JSON_RPC_METHODS.AIN_GET_TRANSACTION_BY_HASH, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, hash: txHash, From 09f4d77e2a296e2cd079dabe16840944bf4ec7cb Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Wed, 18 May 2022 20:56:17 +0900 Subject: [PATCH 037/166] Define JSON_RPC_SET_METHODS_TYPE_SET --- client/middleware.js | 18 +++++------------- json_rpc/constants.js | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/client/middleware.js b/client/middleware.js index 6610c01d8..e1b5ae0d7 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -11,7 +11,7 @@ const { isWildcard } = require('../common/common-util'); const { convertIpv6ToIpv4 } = require('../common/network-util'); -const { JSON_RPC_METHODS } = require('../json_rpc/constants'); +const { JSON_RPC_SET_METHODS_TYPE_SET } = require('../json_rpc/constants'); class Middleware { constructor () { @@ -99,18 +99,10 @@ class Middleware { return next(); } const jsonRpcMethod = _.get(req, 'body.method'); - switch (jsonRpcMethod) { - case JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: - case JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: - case JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: - case JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_KEYSTORE: - case JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_HD_WALLET: - case JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION: - case JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH: - case JSON_RPC_METHODS.AIN_GET_LAST_BLOCK_NUMBER: - return this.jsonRpcWriteLimiter(req, res, next); - default: - return this.jsonRpcReadLimiter(req, res, next); + if (JSON_RPC_SET_METHODS_TYPE_SET.has(jsonRpcMethod)) { + return this.jsonRpcWriteLimiter(req, res, next); + } else { + return this.jsonRpcReadLimiter(req, res, next); } } } diff --git a/json_rpc/constants.js b/json_rpc/constants.js index a40f2399a..dd420c1eb 100644 --- a/json_rpc/constants.js +++ b/json_rpc/constants.js @@ -56,14 +56,17 @@ const JSON_RPC_METHODS = { P2P_GET_PEER_CANDIDATE_INFO: 'p2p_getPeerCandidateInfo', } -// AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', -// AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', -// AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY: 'ain_injectAccountFromPrivateKey', -// AIN_INJECT_ACCOUNT_FROM_KEYSTORE: 'ain_injectAccountFromKeystore', -// AIN_INJECT_ACCOUNT_FROM_HD_WALLET: 'ain_injectAccountFromHDWallet', -// AIN_SEND_SIGNED_TRANSACTION: 'ain_sendSignedTransaction', -// AIN_SEND_SIGNED_TRANSACTION_BATCH: 'ain_sendSignedTransactionBatch', +const JSON_RPC_SET_METHODS_TYPE_SET = new Set([ + JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, + JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_HD_WALLET, + JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_KEYSTORE, + JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY, + JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST, + JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, + JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH +]); module.exports = { - JSON_RPC_METHODS + JSON_RPC_METHODS, + JSON_RPC_SET_METHODS_TYPE_SET }; From a37d51b275d9434636ce53fb651e0ec2444d8515 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 19 May 2022 16:52:11 +0900 Subject: [PATCH 038/166] Update middleware constants --- blockchain-configs/afan-shard/node_params.json | 5 +++-- blockchain-configs/base/node_params.json | 5 +++-- blockchain-configs/he-shard/node_params.json | 5 +++-- blockchain-configs/mainnet-prod/node_params.json | 5 +++-- blockchain-configs/sim-shard/node_params.json | 5 +++-- blockchain-configs/testnet-dev/node_params.json | 5 +++-- blockchain-configs/testnet-exp/node_params.json | 5 +++-- blockchain-configs/testnet-prod/node_params.json | 5 +++-- blockchain-configs/testnet-sandbox/node_params.json | 5 +++-- blockchain-configs/testnet-staging/node_params.json | 5 +++-- client/middleware.js | 13 ++++++------- json_rpc/constants.js | 6 +++--- 12 files changed, 39 insertions(+), 30 deletions(-) diff --git a/blockchain-configs/afan-shard/node_params.json b/blockchain-configs/afan-shard/node_params.json index 17411f8c5..dec40b552 100644 --- a/blockchain-configs/afan-shard/node_params.json +++ b/blockchain-configs/afan-shard/node_params.json @@ -30,6 +30,7 @@ "ENABLE_TX_SIG_VERIF_WORKAROUND": false, "EVENT_HANDLER_PORT": 6000, "EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000, + "EXPRESS_RATE_LIMIT_WINDOW_SECS": 60, "FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1, "FREE_TX_POOL_SIZE_LIMIT_RATIO_PER_ACCOUNT": 0.1, "GET_OP_LIST_SIZE_LIMIT": 50, @@ -39,13 +40,13 @@ "LIGHTWEIGHT": false, "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 3, "MAX_NUM_SNAPSHOTS": 10, - "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, - "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/base/node_params.json b/blockchain-configs/base/node_params.json index 9dd455bf9..6c902e57a 100644 --- a/blockchain-configs/base/node_params.json +++ b/blockchain-configs/base/node_params.json @@ -30,6 +30,7 @@ "ENABLE_TX_SIG_VERIF_WORKAROUND": false, "EVENT_HANDLER_PORT": 6000, "EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000, + "EXPRESS_RATE_LIMIT_WINDOW_SECS": 60, "FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1, "FREE_TX_POOL_SIZE_LIMIT_RATIO_PER_ACCOUNT": 0.1, "GET_OP_LIST_SIZE_LIMIT": 50, @@ -39,13 +40,13 @@ "LIGHTWEIGHT": false, "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, - "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/he-shard/node_params.json b/blockchain-configs/he-shard/node_params.json index c20fc7670..213796a92 100644 --- a/blockchain-configs/he-shard/node_params.json +++ b/blockchain-configs/he-shard/node_params.json @@ -30,6 +30,7 @@ "ENABLE_TX_SIG_VERIF_WORKAROUND": false, "EVENT_HANDLER_PORT": 6000, "EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000, + "EXPRESS_RATE_LIMIT_WINDOW_SECS": 60, "FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1, "FREE_TX_POOL_SIZE_LIMIT_RATIO_PER_ACCOUNT": 0.1, "GET_OP_LIST_SIZE_LIMIT": 50, @@ -39,13 +40,13 @@ "LIGHTWEIGHT": false, "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, - "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/mainnet-prod/node_params.json b/blockchain-configs/mainnet-prod/node_params.json index 2273c1a5a..f37e07201 100644 --- a/blockchain-configs/mainnet-prod/node_params.json +++ b/blockchain-configs/mainnet-prod/node_params.json @@ -29,6 +29,7 @@ "ENABLE_TX_SIG_VERIF_WORKAROUND": false, "EVENT_HANDLER_PORT": 6000, "EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000, + "EXPRESS_RATE_LIMIT_WINDOW_SECS": 60, "FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1, "FREE_TX_POOL_SIZE_LIMIT_RATIO_PER_ACCOUNT": 0.1, "GET_OP_LIST_SIZE_LIMIT": 50, @@ -38,13 +39,13 @@ "LIGHTWEIGHT": false, "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, - "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/sim-shard/node_params.json b/blockchain-configs/sim-shard/node_params.json index e889fc350..deb4987b4 100644 --- a/blockchain-configs/sim-shard/node_params.json +++ b/blockchain-configs/sim-shard/node_params.json @@ -30,6 +30,7 @@ "ENABLE_TX_SIG_VERIF_WORKAROUND": false, "EVENT_HANDLER_PORT": 6000, "EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000, + "EXPRESS_RATE_LIMIT_WINDOW_SECS": 60, "FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1, "FREE_TX_POOL_SIZE_LIMIT_RATIO_PER_ACCOUNT": 0.1, "GET_OP_LIST_SIZE_LIMIT": 50, @@ -39,13 +40,13 @@ "LIGHTWEIGHT": false, "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, - "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-dev/node_params.json b/blockchain-configs/testnet-dev/node_params.json index 04dac2665..02839ae5a 100644 --- a/blockchain-configs/testnet-dev/node_params.json +++ b/blockchain-configs/testnet-dev/node_params.json @@ -30,6 +30,7 @@ "ENABLE_TX_SIG_VERIF_WORKAROUND": false, "EVENT_HANDLER_PORT": 6000, "EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000, + "EXPRESS_RATE_LIMIT_WINDOW_SECS": 60, "FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1, "FREE_TX_POOL_SIZE_LIMIT_RATIO_PER_ACCOUNT": 0.1, "GET_OP_LIST_SIZE_LIMIT": 50, @@ -39,13 +40,13 @@ "LIGHTWEIGHT": false, "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, - "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-exp/node_params.json b/blockchain-configs/testnet-exp/node_params.json index b75870f3f..7be366050 100644 --- a/blockchain-configs/testnet-exp/node_params.json +++ b/blockchain-configs/testnet-exp/node_params.json @@ -30,6 +30,7 @@ "ENABLE_TX_SIG_VERIF_WORKAROUND": false, "EVENT_HANDLER_PORT": 6000, "EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000, + "EXPRESS_RATE_LIMIT_WINDOW_SECS": 60, "FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1, "FREE_TX_POOL_SIZE_LIMIT_RATIO_PER_ACCOUNT": 0.1, "GET_OP_LIST_SIZE_LIMIT": 50, @@ -39,13 +40,13 @@ "LIGHTWEIGHT": false, "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, - "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-prod/node_params.json b/blockchain-configs/testnet-prod/node_params.json index 095bbb8ab..e83139f31 100644 --- a/blockchain-configs/testnet-prod/node_params.json +++ b/blockchain-configs/testnet-prod/node_params.json @@ -30,6 +30,7 @@ "ENABLE_TX_SIG_VERIF_WORKAROUND": false, "EVENT_HANDLER_PORT": 6000, "EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000, + "EXPRESS_RATE_LIMIT_WINDOW_SECS": 60, "FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1, "FREE_TX_POOL_SIZE_LIMIT_RATIO_PER_ACCOUNT": 0.1, "GET_OP_LIST_SIZE_LIMIT": 50, @@ -39,13 +40,13 @@ "LIGHTWEIGHT": false, "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, - "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-sandbox/node_params.json b/blockchain-configs/testnet-sandbox/node_params.json index 65d91f44d..85fe94ea0 100644 --- a/blockchain-configs/testnet-sandbox/node_params.json +++ b/blockchain-configs/testnet-sandbox/node_params.json @@ -30,6 +30,7 @@ "ENABLE_TX_SIG_VERIF_WORKAROUND": false, "EVENT_HANDLER_PORT": 6000, "EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000, + "EXPRESS_RATE_LIMIT_WINDOW_SECS": 60, "FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1, "FREE_TX_POOL_SIZE_LIMIT_RATIO_PER_ACCOUNT": 0.1, "GET_OP_LIST_SIZE_LIMIT": 50, @@ -39,13 +40,13 @@ "LIGHTWEIGHT": false, "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, - "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/blockchain-configs/testnet-staging/node_params.json b/blockchain-configs/testnet-staging/node_params.json index d2e18d033..f9b5a5c60 100644 --- a/blockchain-configs/testnet-staging/node_params.json +++ b/blockchain-configs/testnet-staging/node_params.json @@ -30,6 +30,7 @@ "ENABLE_TX_SIG_VERIF_WORKAROUND": false, "EVENT_HANDLER_PORT": 6000, "EVENT_HANDLER_HEARTBEAT_INTERVAL_MS": 15000, + "EXPRESS_RATE_LIMIT_WINDOW_SECS": 60, "FREE_TX_POOL_SIZE_LIMIT_RATIO": 0.1, "FREE_TX_POOL_SIZE_LIMIT_RATIO_PER_ACCOUNT": 0.1, "GET_OP_LIST_SIZE_LIMIT": 50, @@ -39,13 +40,13 @@ "LIGHTWEIGHT": false, "MAX_BLOCKCHAIN_API_RATE_LIMIT": 20, "MAX_FINALIZED_BLOCK_INFO_ON_MEM": 1000, + "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, + "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "MAX_NUM_EVENT_CHANNELS": 10, "MAX_NUM_EVENT_FILTERS": 20, "MAX_NUM_EVENT_FILTERS_PER_CHANNEL": 5, "MAX_NUM_INBOUND_CONNECTION": 6, "MAX_NUM_SNAPSHOTS": 10, - "MAX_JSON_RPC_API_READ_RATE_LIMIT": 10, - "MAX_JSON_RPC_API_WRITE_RATE_LIMIT": 1, "OLD_CHAIN_SEGMENT_LENGTH": 20, "OLD_CHAIN_SEGMENT_SLEEP_MS": 10000, "ON_MEMORY_CHAIN_LENGTH": 10, diff --git a/client/middleware.js b/client/middleware.js index e1b5ae0d7..963d8f598 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -11,21 +11,20 @@ const { isWildcard } = require('../common/common-util'); const { convertIpv6ToIpv4 } = require('../common/network-util'); -const { JSON_RPC_SET_METHODS_TYPE_SET } = require('../json_rpc/constants'); +const { JSON_RPC_SET_METHOD_SET } = require('../json_rpc/constants'); class Middleware { constructor () { - this.minuteAsSeconds = 60; this.setBlockchainApiRateLimit(); this.setReadRateLimit(); this.setWriteRateLimit(); this.jsonRpcReadLimiter = rateLimit({ - windowMs: this.minuteAsSeconds * 1000, // 1 minute - max: this.minuteAsSeconds * this.getReadRateLimit() + windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, // 1 minute + max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * this.getReadRateLimit() }); this.jsonRpcWriteLimiter = rateLimit({ - windowMs: this.minuteAsSeconds * 1000, // 1 minute - max: this.minuteAsSeconds * this.getWriteRateLimit() + windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, // 1 minute + max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * this.getWriteRateLimit() }); } @@ -99,7 +98,7 @@ class Middleware { return next(); } const jsonRpcMethod = _.get(req, 'body.method'); - if (JSON_RPC_SET_METHODS_TYPE_SET.has(jsonRpcMethod)) { + if (JSON_RPC_SET_METHOD_SET.has(jsonRpcMethod)) { return this.jsonRpcWriteLimiter(req, res, next); } else { return this.jsonRpcReadLimiter(req, res, next); diff --git a/json_rpc/constants.js b/json_rpc/constants.js index dd420c1eb..6193ffd13 100644 --- a/json_rpc/constants.js +++ b/json_rpc/constants.js @@ -27,9 +27,9 @@ const JSON_RPC_METHODS = { AIN_GET_STATE_INFO: 'ain_getStateInfo', AIN_GET_STATE_PROOF: 'ain_getStateProof', AIN_GET_STATE_USAGE: 'ain_getStateUsage', - AIN_GET_TRANSACTION_BY_HASH: 'ain_getTransactionByHash', AIN_GET_TRANSACTION_BY_BLOCK_HASH_AND_INDEX: 'ain_getTransactionByBlockHashAndIndex', AIN_GET_TRANSACTION_BY_BLOCK_NUMBER_AND_INDEX: 'ain_getTransactionByBlockNumberAndIndex', + AIN_GET_TRANSACTION_BY_HASH: 'ain_getTransactionByHash', AIN_GET_TRANSACTION_POOL_SIZE_UTILIZATION: 'ain_getTransactionPoolSizeUtilization', AIN_GET_TIMESTAMP: 'ain_getTimestamp', AIN_GET_VALIDATOR_INFO: 'ain_getValidatorInfo', @@ -56,7 +56,7 @@ const JSON_RPC_METHODS = { P2P_GET_PEER_CANDIDATE_INFO: 'p2p_getPeerCandidateInfo', } -const JSON_RPC_SET_METHODS_TYPE_SET = new Set([ +const JSON_RPC_SET_METHOD_SET = new Set([ JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_HD_WALLET, JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_KEYSTORE, @@ -68,5 +68,5 @@ const JSON_RPC_SET_METHODS_TYPE_SET = new Set([ module.exports = { JSON_RPC_METHODS, - JSON_RPC_SET_METHODS_TYPE_SET + JSON_RPC_SET_METHOD_SET }; From 510086527881e967e4978c2fb09f45cb63f2723b Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 19 May 2022 17:02:15 +0900 Subject: [PATCH 039/166] Update some indentation --- test/integration/blockchain.test.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/integration/blockchain.test.js b/test/integration/blockchain.test.js index bba48685d..e57ab0dd5 100644 --- a/test/integration/blockchain.test.js +++ b/test/integration/blockchain.test.js @@ -282,10 +282,9 @@ describe('Blockchain Cluster', () => { }); }).then(() => { return new Promise((resolve) => { - jayson.client.http(serverList[i] + JSON_RPC_ENDPOINT) - .request(JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, + jayson.client.http(serverList[i] + JSON_RPC_ENDPOINT).request(JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, - function (err, response) { + function (err, response) { if (err) throw err; const newChain = response.result.result; const diff = Math.abs(baseChain.length - newChain.length); @@ -300,7 +299,7 @@ describe('Blockchain Cluster', () => { assert.deepEqual(newChain.length, baseChain.length); assert.deepEqual(newChain, baseChain); resolve(); - }); + }); }); }); } @@ -618,10 +617,9 @@ describe('Blockchain Cluster', () => { describe('Gas fee', () => { it('collected gas cost matches the gas_cost_total in the block', () => { return new Promise((resolve) => { - jayson.client.http(serverList[1] + JSON_RPC_ENDPOINT) - .request(JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, + jayson.client.http(serverList[1] + JSON_RPC_ENDPOINT).request(JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, - function (err, response) { + function (err, response) { if (err) throw err; const chain = response.result.result; for (const block of chain) { From 85848119627730b58272bf8a1d29fb7990e6e875 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 19 May 2022 17:04:10 +0900 Subject: [PATCH 040/166] Remove all getters and setters for simplicity --- client/middleware.js | 36 +++--------------------------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/client/middleware.js b/client/middleware.js index 963d8f598..b87480c90 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -15,46 +15,16 @@ const { JSON_RPC_SET_METHOD_SET } = require('../json_rpc/constants'); class Middleware { constructor () { - this.setBlockchainApiRateLimit(); - this.setReadRateLimit(); - this.setWriteRateLimit(); this.jsonRpcReadLimiter = rateLimit({ windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, // 1 minute - max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * this.getReadRateLimit() + max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * NodeConfigs.MAX_JSON_RPC_API_READ_RATE_LIMIT }); this.jsonRpcWriteLimiter = rateLimit({ windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, // 1 minute - max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * this.getWriteRateLimit() + max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * NodeConfigs.MAX_JSON_RPC_API_WRITE_RATE_LIMIT }); } - setBlockchainApiRateLimit() { - this.blockchainApiRateLimit = NodeConfigs.MAX_BLOCKCHAIN_API_RATE_LIMIT; - return this; - } - - setReadRateLimit() { - this.readRateLimit = NodeConfigs.MAX_JSON_RPC_API_READ_RATE_LIMIT; - return this; - } - - setWriteRateLimit() { - this.writeRateLimit = NodeConfigs.MAX_JSON_RPC_API_WRITE_RATE_LIMIT; - return this; - } - - getBlockchainApiRateLimit() { - return this.blockchainApiRateLimit; - } - - getReadRateLimit() { - return this.readRateLimit; - } - - getWriteRateLimit() { - return this.writeRateLimit; - } - expressJsonRequestBodySizeLimiter() { return express.json({ limit: NodeConfigs.REQUEST_BODY_SIZE_LIMIT }); } @@ -89,7 +59,7 @@ class Middleware { return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? rateLimit({ windowMs: this.minuteAsSeconds * 1000, // 1 minute window - max: this.minuteAsSeconds * this.getBlockchainApiRateLimit() + max: this.minuteAsSeconds * NodeConfigs.MAX_BLOCKCHAIN_API_RATE_LIMIT }) : this._emptyHandler(); } From 242e7a0df4a0041fbb89708c72e0a5596feb6a00 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 19 May 2022 17:08:25 +0900 Subject: [PATCH 041/166] Make some methods name clearer --- client/index.js | 2 +- client/middleware.js | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/client/index.js b/client/index.js index 47a24a3ec..8a7b4b630 100755 --- a/client/index.js +++ b/client/index.js @@ -60,7 +60,7 @@ const jsonRpcApis = require('../json_rpc')( app.post( '/json-rpc', VersionUtil.validateVersion.bind({ minProtocolVersion, maxProtocolVersion }), - middleware.jsonRpcLimiter, + middleware.jsonRpcRateLimiter, jayson.server(jsonRpcApis).middleware() ); diff --git a/client/middleware.js b/client/middleware.js index b87480c90..352bd64c3 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -15,11 +15,11 @@ const { JSON_RPC_SET_METHOD_SET } = require('../json_rpc/constants'); class Middleware { constructor () { - this.jsonRpcReadLimiter = rateLimit({ + this.jsonRpcReadRateLimiter = rateLimit({ windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, // 1 minute max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * NodeConfigs.MAX_JSON_RPC_API_READ_RATE_LIMIT }); - this.jsonRpcWriteLimiter = rateLimit({ + this.jsonRpcWriteRateLimiter = rateLimit({ windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, // 1 minute max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * NodeConfigs.MAX_JSON_RPC_API_WRITE_RATE_LIMIT }); @@ -63,15 +63,15 @@ class Middleware { }) : this._emptyHandler(); } - jsonRpcLimiter = (req, res, next) => { + jsonRpcRateLimiter = (req, res, next) => { if (!NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT) { return next(); } const jsonRpcMethod = _.get(req, 'body.method'); if (JSON_RPC_SET_METHOD_SET.has(jsonRpcMethod)) { - return this.jsonRpcWriteLimiter(req, res, next); + return this.jsonRpcWriteRateLimiter(req, res, next); } else { - return this.jsonRpcReadLimiter(req, res, next); + return this.jsonRpcReadRateLimiter(req, res, next); } } } From 524a05e4ac5d2c8ecee69281dffcd4c69ca7fdc2 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 19 May 2022 17:34:56 +0900 Subject: [PATCH 042/166] Simplify middleware.js --- client/index.js | 8 ++++---- client/middleware.js | 22 ++++++++++------------ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/client/index.js b/client/index.js index 8a7b4b630..cfcb31efd 100755 --- a/client/index.js +++ b/client/index.js @@ -32,14 +32,14 @@ app.use(middleware.expressJsonRequestBodySizeLimiter()); app.use(middleware.expressUrlencdedRequestBodySizeLimiter()); app.use(middleware.corsLimiter()); app.use(middleware.ipWhitelistLimiter()); -app.use(middleware.blockchainApiLimiter()); +app.use(middleware.blockchainApiRateLimiter); const eventHandler = NodeConfigs.ENABLE_EVENT_HANDLER === true ? new EventHandler() : null; const node = new BlockchainNode(null, eventHandler); // NOTE(platfowner): This is very useful when the server dies without any logs. -// process.on('uncaughtException', function(err) { -// logger.error(err); -// }); +process.on('uncaughtException', function(err) { + logger.error(err); +}); process.on('SIGINT', (_) => { logger.info('Stopping the blockchain client....'); diff --git a/client/middleware.js b/client/middleware.js index 352bd64c3..77a1c3e0f 100644 --- a/client/middleware.js +++ b/client/middleware.js @@ -15,12 +15,16 @@ const { JSON_RPC_SET_METHOD_SET } = require('../json_rpc/constants'); class Middleware { constructor () { + this.allBlockchainApiRateLimiter = rateLimit({ + windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, // 1 minute window + max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * NodeConfigs.MAX_BLOCKCHAIN_API_RATE_LIMIT + }); this.jsonRpcReadRateLimiter = rateLimit({ - windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, // 1 minute + windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * NodeConfigs.MAX_JSON_RPC_API_READ_RATE_LIMIT }); this.jsonRpcWriteRateLimiter = rateLimit({ - windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, // 1 minute + windowMs: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * 1000, max: NodeConfigs.EXPRESS_RATE_LIMIT_WINDOW_SECS * NodeConfigs.MAX_JSON_RPC_API_WRITE_RATE_LIMIT }); } @@ -49,18 +53,11 @@ class Middleware { }) } - _emptyHandler() { - return (req, res, next) => { + blockchainApiRateLimiter = (req, res, next) => { + if (!NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT) { return next(); } - } - - blockchainApiLimiter() { - return NodeConfigs.ENABLE_EXPRESS_RATE_LIMIT ? - rateLimit({ - windowMs: this.minuteAsSeconds * 1000, // 1 minute window - max: this.minuteAsSeconds * NodeConfigs.MAX_BLOCKCHAIN_API_RATE_LIMIT - }) : this._emptyHandler(); + return this.allBlockchainApiRateLimiter(req, res, next); } jsonRpcRateLimiter = (req, res, next) => { @@ -68,6 +65,7 @@ class Middleware { return next(); } const jsonRpcMethod = _.get(req, 'body.method'); + // NOTE(minsulee2): Write request is controlled tightest that is 1 tps per ip. if (JSON_RPC_SET_METHOD_SET.has(jsonRpcMethod)) { return this.jsonRpcWriteRateLimiter(req, res, next); } else { From d420594e375e9d8094612c04d03c50ad416308b0 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Mon, 23 May 2022 13:55:08 +0900 Subject: [PATCH 043/166] Fix minor indent --- test/integration/blockchain.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/blockchain.test.js b/test/integration/blockchain.test.js index e57ab0dd5..24976e4db 100644 --- a/test/integration/blockchain.test.js +++ b/test/integration/blockchain.test.js @@ -283,7 +283,7 @@ describe('Blockchain Cluster', () => { }).then(() => { return new Promise((resolve) => { jayson.client.http(serverList[i] + JSON_RPC_ENDPOINT).request(JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, - { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, + { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, function (err, response) { if (err) throw err; const newChain = response.result.result; @@ -618,7 +618,7 @@ describe('Blockchain Cluster', () => { it('collected gas cost matches the gas_cost_total in the block', () => { return new Promise((resolve) => { jayson.client.http(serverList[1] + JSON_RPC_ENDPOINT).request(JSON_RPC_METHODS.AIN_GET_BLOCK_LIST, - { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, + { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }, function (err, response) { if (err) throw err; const chain = response.result.result; From f58d165aa9468344f5877b55aafc93d36e320279 Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Tue, 24 May 2022 12:52:08 +0900 Subject: [PATCH 044/166] Set error pipe for test run --- deploy_test_gcp.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy_test_gcp.sh b/deploy_test_gcp.sh index 909bbaa83..c604d88bf 100644 --- a/deploy_test_gcp.sh +++ b/deploy_test_gcp.sh @@ -144,7 +144,7 @@ function deploy_test() { if [[ $FOREGROUND_OPTION = "--fg" ]]; then TEST_CMD="cd ./ain-blockchain; yarn run ${testing_option}" else - TEST_CMD="cd ./ain-blockchain; nohup yarn run ${testing_option} > test_log.txt &" + TEST_CMD="cd ./ain-blockchain; nohup yarn run ${testing_option} >test_log.txt 2>&1 &" fi printf "\nTEST_CMD=$TEST_CMD\n\n" gcloud compute ssh ${test_target_addr} --command "$TEST_CMD" --project $PROJECT_ID --zone ${TEST_ZONE} From 9a059d06b87af61ce200ff8a2b054493c2b6f325 Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Tue, 24 May 2022 19:02:28 +0900 Subject: [PATCH 045/166] Add write rule keyword token blacklist --- db/state-util.js | 29 +++++++++++++++++++++++------ test/unit/state-util.test.js | 3 +++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/db/state-util.js b/db/state-util.js index 7c8cf33a3..e271dec64 100644 --- a/db/state-util.js +++ b/db/state-util.js @@ -41,9 +41,13 @@ const WRITE_RULE_ID_TOKEN_WHITELIST_BASE = [ 'String', // type casting 'Boolean', // type casting ]; -const WRITE_RULE_PUNC_TOKEN_BLACKLIST = [ +const WRITE_RULE_KEYWORD_TOKEN_BLACKLIST_SET = new Set([ + 'function', +]); +const WRITE_RULE_PUNC_TOKEN_BLACKLIST_SET = new Set([ '=', // assignment -]; + '=>', // arrow function +]); function isEmptyNode(node) { return node.getIsLeaf() && node.getValue() === null; @@ -285,6 +289,13 @@ function getTopLevelIdTokens(tokenList) { }).map((token) => token.value); } +/** + * Extract keyword tokens from the given token list. + */ +function getKeywordTokens(tokenList) { + return tokenList.filter((token) => token.type === 'Keyword').map((token) => token.value); +} + /** * Extract punctuator tokens from the given token list. */ @@ -315,12 +326,18 @@ function isValidWriteRule(variableLabels, ruleString) { return false; } } - const puncTokenBlacklistSet = new Set([ - ...WRITE_RULE_PUNC_TOKEN_BLACKLIST - ]); + const keywordTokens = getKeywordTokens(tokenList); + for (const token of keywordTokens) { + if (WRITE_RULE_KEYWORD_TOKEN_BLACKLIST_SET.has(token)) { + logger.info( + `[${LOG_HEADER}] Rule includes a not-allowed keyword token (${token}) ` + + `in rule string: ${ruleString}`); + return false; + } + } const puncTokens = getPuncTokens(tokenList); for (const token of puncTokens) { - if (puncTokenBlacklistSet.has(token)) { + if (WRITE_RULE_PUNC_TOKEN_BLACKLIST_SET.has(token)) { logger.info( `[${LOG_HEADER}] Rule includes a not-allowed punctuator token (${token}) ` + `in rule string: ${ruleString}`); diff --git a/test/unit/state-util.test.js b/test/unit/state-util.test.js index e13f82ffd..4e2fe6c3a 100644 --- a/test/unit/state-util.test.js +++ b/test/unit/state-util.test.js @@ -690,6 +690,9 @@ describe("state-util", () => { expect(isValidWriteRule({}, "newData = 'some code'")).to.equal(false); // assignment & invoke expect(isValidWriteRule({}, "newData = 'some code'; newData();")).to.equal(false); + // function + expect(isValidWriteRule({}, "[function(){while(true){}}][0]()")).to.equal(false); + expect(isValidWriteRule({}, "[()=>{while(true){}}][0]()")).to.equal(false); }) it('when valid input', () => { From cebcf36d11a98eda7ee3fa0d293beed3e371549e Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 26 May 2022 15:07:27 +0900 Subject: [PATCH 046/166] Work init load test --- tools/simple-load-test/index.js | 77 +++++++++++++-------------------- 1 file changed, 29 insertions(+), 48 deletions(-) diff --git a/tools/simple-load-test/index.js b/tools/simple-load-test/index.js index 04e5f3a3b..8300d7463 100644 --- a/tools/simple-load-test/index.js +++ b/tools/simple-load-test/index.js @@ -8,12 +8,12 @@ const axios = require('axios'); const commandLineArgs = require('command-line-args'); const getUsage = require('command-line-usage'); const CommonUtil = require('../../common/common-util'); -const { signTx } = require('../util'); +const { signAndSendTx } = require('../util'); const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); const delay = (time) => new Promise((resolve) => setTimeout(resolve, time)); const testPath = '/apps/loadtest'; -const ainPrivateKey = '4207f5dcacb1b601d3a1f8cb10afaca158f6ebe383c0b30d02b39f8d2060cce3'; -const ainAddress = '0xF2be7f1356347a8960630c112AcB6Da61eE94632'; +const ainPrivateKey = 'b22c95ffc4a5c096f7d7d0487ba963ce6ac945bdc91c79b64ce209de289bec96'; +const ainAddress = '0x00ADEc28B6a845a085e03591bE7550dd68673C1C'; const TIMEOUT_MS = 10 * 1000; const optionDefinitions = [ { @@ -73,60 +73,39 @@ function sendTx(endpointUrl, signedTx) { } async function initPermission(targetUrl) { - const setOwnerTx = { - operation: { - type: 'SET_OWNER', - ref: testPath, - value: { - '.owner': { - owners: { - '*': { - write_owner: true, - write_rule: true, - write_function: true, - branch_owner: true, - }, - }, - }, - }, - }, - timestamp: Date.now(), - nonce: -1, - }; - const setRuleTx = { + const setRuleTxBody = { operation: { type: 'SET_RULE', - ref: testPath, + ref: `/apps/loadtest`, value: { '.rule': { 'write': true, - } + }, }, }, timestamp: Date.now(), + gas_price: 500, nonce: -1, }; - const setValueTx = { + + const createAppTxBody = { operation: { type: 'SET_VALUE', - ref: testPath, - value: 0, + ref: `/manage_app/loadtest/create/${Date.now()}`, + value: { + admin: { + [ainAddress]: true, + }, + }, }, timestamp: Date.now(), nonce: -1, + gas_price: 500, }; - const {signedTx: signedSetOwnerTx} = CommonUtil.signTransaction(setOwnerTx, ainPrivateKey); - const {signedTx: signedSetRuleTx} = CommonUtil.signTransaction(setRuleTx, ainPrivateKey); - const {signedTx: signedSetValueTx} = CommonUtil.signTransaction(setValueTx, ainPrivateKey); - const promiseList = []; - promiseList.push(sendTx(targetUrl, signedSetOwnerTx)); - promiseList.push(sendTx(targetUrl, signedSetRuleTx)); - promiseList.push(sendTx(targetUrl, signedSetValueTx)); - const resultList = await Promise.all(promiseList); - if (resultList.includes(false)) { - throw Error(`Error while init permission`); - } - await delay(10 * 1000); + + const result = await signAndSendTx(targetUrl, createAppTxBody, ainPrivateKey, 0); + const result2 = await signAndSendTx(targetUrl, setRuleTxBody, ainPrivateKey, 0); + console.log(result2); } function makeBaseTransaction() { @@ -137,6 +116,7 @@ function makeBaseTransaction() { value: 1, }, nonce: -1, + gas_price: 500 }; } @@ -157,8 +137,9 @@ async function sendTxs(targetUrl, duration, numberOfTransactions) { new Promise((resolve, reject) => { setTimeout((txTimestamp) => { baseTx.timestamp = txTimestamp; - const {signedTx} = CommonUtil.signTransaction(baseTx, ainPrivateKey); + const { signedTx } = CommonUtil.signTransaction(baseTx, ainPrivateKey); sendTx(targetUrl, signedTx).then((result) => { + console.log(result); if (result === true) { sendCnt++; } @@ -185,12 +166,12 @@ async function main() { } const targetUrl = args.target_url || 'http://localhost:8081'; const duration = args.duration || 60; // 60: 1min - const numberOfTransactions = args.number_txs || 300; - console.log(`Initialize permission (${testPath})`); - await initPermission(targetUrl); - console.log(`Start to send transactions (${numberOfTransactions})`); - const sendCnt = await sendTxs(targetUrl, duration, numberOfTransactions); - console.log(`Finish load test! (${sendCnt}/${numberOfTransactions})`); + const numberOfTransactions = args.number_txs || 30; + console.log(`Initialize loadTestApp (${testPath})`); + // await initLoadTestApp(targetUrl); + // console.log(`Start to send transactions (${numberOfTransactions})`); + // const sendCnt = await sendTxs(targetUrl, duration, numberOfTransactions); + // console.log(`Finish load test! (${sendCnt}/${numberOfTransactions})`); } main(); From ea4de3f21e17f91efddd595b47965b93769c23e8 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 26 May 2022 15:42:23 +0900 Subject: [PATCH 047/166] Set txs in second --- tools/simple-load-test/index.js | 86 +++++++++------------------------ 1 file changed, 22 insertions(+), 64 deletions(-) diff --git a/tools/simple-load-test/index.js b/tools/simple-load-test/index.js index 8300d7463..53988b1c9 100644 --- a/tools/simple-load-test/index.js +++ b/tools/simple-load-test/index.js @@ -3,18 +3,15 @@ * This tool increases '/apps/loadtest/visit_count' * Usage: 'node index.js --help' */ -const _ = require('lodash'); -const axios = require('axios'); const commandLineArgs = require('command-line-args'); const getUsage = require('command-line-usage'); -const CommonUtil = require('../../common/common-util'); const { signAndSendTx } = require('../util'); -const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); + const delay = (time) => new Promise((resolve) => setTimeout(resolve, time)); const testPath = '/apps/loadtest'; const ainPrivateKey = 'b22c95ffc4a5c096f7d7d0487ba963ce6ac945bdc91c79b64ce209de289bec96'; const ainAddress = '0x00ADEc28B6a845a085e03591bE7550dd68673C1C'; -const TIMEOUT_MS = 10 * 1000; + const optionDefinitions = [ { name: 'help', @@ -38,13 +35,14 @@ const optionDefinitions = [ group: 'options', }, { - name: 'number_txs', + name: 'num_txs', alias: 'n', type: Number, - description: 'Number of transactions (Default: 300)', + description: 'transactions per second (Default: 2)', group: 'options', }, ]; + const sections = [ { header: 'AIN Simple load test', @@ -56,23 +54,7 @@ const sections = [ } ]; -function sendTx(endpointUrl, signedTx) { - return axios.post(`${endpointUrl}/json-rpc`, { - method: JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, - params: signedTx, - jsonrpc: '2.0', - id: 0, - }, { - timeout: TIMEOUT_MS, - }).then((result) => { - return _.get(result, 'data.result.result.result', false); - }).catch((err) => { - console.error(err); - return false; - }); -} - -async function initPermission(targetUrl) { +async function initLoadTestApp(targetUrl) { const setRuleTxBody = { operation: { type: 'SET_RULE', @@ -103,58 +85,35 @@ async function initPermission(targetUrl) { gas_price: 500, }; - const result = await signAndSendTx(targetUrl, createAppTxBody, ainPrivateKey, 0); - const result2 = await signAndSendTx(targetUrl, setRuleTxBody, ainPrivateKey, 0); - console.log(result2); + const createAppTxResult = await signAndSendTx(targetUrl, createAppTxBody, ainPrivateKey, 0); + const setRuleTxResult = await signAndSendTx(targetUrl, setRuleTxBody, ainPrivateKey, 0); + console.log(createAppTxResult); + console.log(setRuleTxResult); } -function makeBaseTransaction() { +function buildIncTxBody() { return { operation: { type: 'INC_VALUE', ref: `${testPath}/visit_count`, value: 1, }, + timestamp: Date.now(), nonce: -1, gas_price: 500 }; } -async function sendTxs(targetUrl, duration, numberOfTransactions) { - const delayTime = duration / numberOfTransactions * 1000; - const sendTxPromiseList = []; - const timestamp = Date.now(); - const baseTx = makeBaseTransaction(); - let sendCnt = 0; +async function sendTxInSecond(targetUrl, numberOfTransactions) { + if (numberOfTransactions < 0) { + return; + } + const delayMs = 1000 / numberOfTransactions; for (let i = 0; i < numberOfTransactions; i++) { - await delay(delayTime); - if (i % 1000 === 0) { - console.log(`[${i}/${numberOfTransactions}]`); - } - - sendTxPromiseList.push( - new Promise((resolve, reject) => { - setTimeout((txTimestamp) => { - baseTx.timestamp = txTimestamp; - const { signedTx } = CommonUtil.signTransaction(baseTx, ainPrivateKey); - sendTx(targetUrl, signedTx).then((result) => { - console.log(result); - if (result === true) { - sendCnt++; - } - resolve(result); - }).catch((err) => { - console.error(err); - resolve(false); - }); - }, 0, timestamp + i); - }), - ); + const result = signAndSendTx(targetUrl, buildIncTxBody(), ainPrivateKey, 0); + await delay(delayMs); } - - await Promise.all(sendTxPromiseList); - return sendCnt; } async function main() { @@ -166,12 +125,11 @@ async function main() { } const targetUrl = args.target_url || 'http://localhost:8081'; const duration = args.duration || 60; // 60: 1min - const numberOfTransactions = args.number_txs || 30; + const numberOfTransactionsInSecond = args.num_txs || 2; console.log(`Initialize loadTestApp (${testPath})`); // await initLoadTestApp(targetUrl); - // console.log(`Start to send transactions (${numberOfTransactions})`); - // const sendCnt = await sendTxs(targetUrl, duration, numberOfTransactions); - // console.log(`Finish load test! (${sendCnt}/${numberOfTransactions})`); + console.log(`TPS: ${numberOfTransactionsInSecond}`); + await sendTxInSecond(targetUrl, numberOfTransactionsInSecond); } main(); From dc00492761bf01ae7073e8812bda8465d10dd702 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 26 May 2022 15:45:27 +0900 Subject: [PATCH 048/166] Set appName separately --- tools/simple-load-test/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/simple-load-test/index.js b/tools/simple-load-test/index.js index 53988b1c9..5bc4e577b 100644 --- a/tools/simple-load-test/index.js +++ b/tools/simple-load-test/index.js @@ -8,7 +8,8 @@ const getUsage = require('command-line-usage'); const { signAndSendTx } = require('../util'); const delay = (time) => new Promise((resolve) => setTimeout(resolve, time)); -const testPath = '/apps/loadtest'; +const appName = 'loadtest'; +const testPath = `/apps/${appName}`; const ainPrivateKey = 'b22c95ffc4a5c096f7d7d0487ba963ce6ac945bdc91c79b64ce209de289bec96'; const ainAddress = '0x00ADEc28B6a845a085e03591bE7550dd68673C1C'; @@ -58,7 +59,7 @@ async function initLoadTestApp(targetUrl) { const setRuleTxBody = { operation: { type: 'SET_RULE', - ref: `/apps/loadtest`, + ref: `/apps/${appName}`, value: { '.rule': { 'write': true, @@ -73,7 +74,7 @@ async function initLoadTestApp(targetUrl) { const createAppTxBody = { operation: { type: 'SET_VALUE', - ref: `/manage_app/loadtest/create/${Date.now()}`, + ref: `/manage_app/${appName}/create/${Date.now()}`, value: { admin: { [ainAddress]: true, From 6078e2c128eb6db2337846a8c4e934d28e529a4b Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 26 May 2022 16:02:04 +0900 Subject: [PATCH 049/166] Update load tester --- tools/simple-load-test/index.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/tools/simple-load-test/index.js b/tools/simple-load-test/index.js index 5bc4e577b..9a3841f51 100644 --- a/tools/simple-load-test/index.js +++ b/tools/simple-load-test/index.js @@ -106,15 +106,25 @@ function buildIncTxBody() { } async function sendTxInSecond(targetUrl, numberOfTransactions) { - if (numberOfTransactions < 0) { - return; - } - const delayMs = 1000 / numberOfTransactions; for (let i = 0; i < numberOfTransactions; i++) { const result = signAndSendTx(targetUrl, buildIncTxBody(), ainPrivateKey, 0); await delay(delayMs); } + return numberOfTransactions; +} + +async function runLoadtest(targetUrl, numberOfTransactionsInSecond, duration) { + if (numberOfTransactionsInSecond < 0 || duration < 0) { + return; + } + + let count = 0; + for (let i = 0; i < duration; i++) { + const tmpCount = await sendTxInSecond(targetUrl, numberOfTransactionsInSecond); + count += tmpCount; + } + return count; } async function main() { @@ -125,12 +135,16 @@ async function main() { process.exit(0); } const targetUrl = args.target_url || 'http://localhost:8081'; - const duration = args.duration || 60; // 60: 1min + const duration = args.duration || 10; // 60: 1min const numberOfTransactionsInSecond = args.num_txs || 2; console.log(`Initialize loadTestApp (${testPath})`); // await initLoadTestApp(targetUrl); - console.log(`TPS: ${numberOfTransactionsInSecond}`); - await sendTxInSecond(targetUrl, numberOfTransactionsInSecond); + const total = await runLoadtest(targetUrl, numberOfTransactionsInSecond, duration); + + console.log('===========================REPORT==========================='); + console.log(`Target TPS: ${numberOfTransactionsInSecond}`); + console.log(`TXS(sent/target): ${total}/${duration * numberOfTransactionsInSecond} in ${duration} seconds`); + console.log(`TPS: ${total / duration}`); } main(); From e94ce0a338983bdad39a3a3bad292855b0112a8c Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 26 May 2022 16:03:31 +0900 Subject: [PATCH 050/166] Minor update --- tools/simple-load-test/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/simple-load-test/index.js b/tools/simple-load-test/index.js index 9a3841f51..d2c58bb69 100644 --- a/tools/simple-load-test/index.js +++ b/tools/simple-load-test/index.js @@ -135,10 +135,10 @@ async function main() { process.exit(0); } const targetUrl = args.target_url || 'http://localhost:8081'; - const duration = args.duration || 10; // 60: 1min + const duration = args.duration || 60; // 1 minute by default const numberOfTransactionsInSecond = args.num_txs || 2; console.log(`Initialize loadTestApp (${testPath})`); - // await initLoadTestApp(targetUrl); + await initLoadTestApp(targetUrl); const total = await runLoadtest(targetUrl, numberOfTransactionsInSecond, duration); console.log('===========================REPORT==========================='); From 67fa35c9f254075e201eb7e7f1b0175c49fe7393 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 26 May 2022 16:04:30 +0900 Subject: [PATCH 051/166] Add json rpc tester as well --- tools/json-rpc-test/index.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tools/json-rpc-test/index.js diff --git a/tools/json-rpc-test/index.js b/tools/json-rpc-test/index.js new file mode 100644 index 000000000..5bdb4f895 --- /dev/null +++ b/tools/json-rpc-test/index.js @@ -0,0 +1,28 @@ +const _ = require('lodash'); +const axios = require('axios'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); + +const queryOnNode = (method, params) => { + return axios.post( + 'http://localhost:8081/json-rpc', + { + method, + params: Object.assign(params, { protoVer: '1.0.6' }), + jsonrpc: '2.0', + id: 1 + } + ).then((resp) => { + return _.get(resp, 'data.result.result'); + }).catch((err) => { + console.error(`Failed to send get request: ${err}`); + return null; + }); +} + +const main = async () => { + const result = await queryOnNode(JSON_RPC_METHODS.AIN_GET_LAST_BLOCK, { }); + // const result = await queryOnNode(JSON_RPC_METHODS.AIN_GET_LAST_BLOCK_NUMBER, { }); + console.log(result); +} + +main(); From e32bb88ce71aedcaa466ab0bb9a690e97fc74f2d Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 26 May 2022 23:26:10 +0900 Subject: [PATCH 052/166] Update json-rpc-test script --- tools/json-rpc-test/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/json-rpc-test/index.js b/tools/json-rpc-test/index.js index 5bdb4f895..9800a3e7c 100644 --- a/tools/json-rpc-test/index.js +++ b/tools/json-rpc-test/index.js @@ -1,10 +1,11 @@ const _ = require('lodash'); const axios = require('axios'); const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); +const { endpoint } = require('./config_local'); const queryOnNode = (method, params) => { return axios.post( - 'http://localhost:8081/json-rpc', + endpoint, { method, params: Object.assign(params, { protoVer: '1.0.6' }), @@ -19,7 +20,15 @@ const queryOnNode = (method, params) => { }); } +const usage = () => { + console.log('\nExample commandlines:\n node index.js\n'); +} + const main = async () => { + if (process.argv.length !== 2) { + usage(); + process.exit(0); + } const result = await queryOnNode(JSON_RPC_METHODS.AIN_GET_LAST_BLOCK, { }); // const result = await queryOnNode(JSON_RPC_METHODS.AIN_GET_LAST_BLOCK_NUMBER, { }); console.log(result); From f1765e3cd4ed30fc0009b63a1d29fd0216c79e70 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 26 May 2022 23:27:46 +0900 Subject: [PATCH 053/166] Rename dir and file names --- tools/json-rpc/config_local.js | 3 +++ tools/{json-rpc-test/index.js => json-rpc/getLastBlock.js} | 0 2 files changed, 3 insertions(+) create mode 100644 tools/json-rpc/config_local.js rename tools/{json-rpc-test/index.js => json-rpc/getLastBlock.js} (100%) diff --git a/tools/json-rpc/config_local.js b/tools/json-rpc/config_local.js new file mode 100644 index 000000000..b925dbf8d --- /dev/null +++ b/tools/json-rpc/config_local.js @@ -0,0 +1,3 @@ +module.exports = { + endpoint: 'http://localhost:8081/json-rpc' +}; diff --git a/tools/json-rpc-test/index.js b/tools/json-rpc/getLastBlock.js similarity index 100% rename from tools/json-rpc-test/index.js rename to tools/json-rpc/getLastBlock.js From caa933e7f1fde3f69b5b8d33ce92e992e7f99f81 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Thu, 26 May 2022 23:35:30 +0900 Subject: [PATCH 054/166] Get global var from config --- tools/simple-load-test/config.js | 6 ++++++ tools/simple-load-test/index.js | 10 ++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 tools/simple-load-test/config.js diff --git a/tools/simple-load-test/config.js b/tools/simple-load-test/config.js new file mode 100644 index 000000000..641f71928 --- /dev/null +++ b/tools/simple-load-test/config.js @@ -0,0 +1,6 @@ +module.exports = { + appName: 'loadtest', + testPath: `/apps/loadtest`, + ainPrivateKey: 'b22c95ffc4a5c096f7d7d0487ba963ce6ac945bdc91c79b64ce209de289bec96', + ainAddress: '0x00ADEc28B6a845a085e03591bE7550dd68673C1C' +}; diff --git a/tools/simple-load-test/index.js b/tools/simple-load-test/index.js index d2c58bb69..ea8e1df55 100644 --- a/tools/simple-load-test/index.js +++ b/tools/simple-load-test/index.js @@ -6,12 +6,14 @@ const commandLineArgs = require('command-line-args'); const getUsage = require('command-line-usage'); const { signAndSendTx } = require('../util'); +const { + appName, + testPath, + ainPrivateKey, + ainAddress +} = require('./config'); const delay = (time) => new Promise((resolve) => setTimeout(resolve, time)); -const appName = 'loadtest'; -const testPath = `/apps/${appName}`; -const ainPrivateKey = 'b22c95ffc4a5c096f7d7d0487ba963ce6ac945bdc91c79b64ce209de289bec96'; -const ainAddress = '0x00ADEc28B6a845a085e03591bE7550dd68673C1C'; const optionDefinitions = [ { From 5af151ac3136e74244682b3af44a6c0121875ce9 Mon Sep 17 00:00:00 2001 From: kriii Date: Fri, 27 May 2022 11:29:00 +0900 Subject: [PATCH 055/166] Admin node param APIs --- common/result-code.js | 16 ++-- json_rpc/admin.js | 178 +++++++++++++++++++++++++++++++----------- json_rpc/constants.js | 7 +- 3 files changed, 146 insertions(+), 55 deletions(-) diff --git a/common/result-code.js b/common/result-code.js index 4d768dc85..f1606edf5 100644 --- a/common/result-code.js +++ b/common/result-code.js @@ -173,15 +173,15 @@ const JsonRpcApiResultCode = { BATCH_TX_MISSING_PROPERTIES: 30404, BATCH_TX_INVALID_FORMAT: 30405, BATCH_TX_INVALID_SIGNATURE: 30406, - // ain_addToDevClientApiIpWhitelist - INVALID_IP: 30501, - IP_ALREADY_IN_WHITELIST: 30502, - // ain_removeFromDevClientApiIpWhitelist - IP_NOT_IN_WHITELIST: 30601, + // Admin APIs + PARAM_INVALID: 30501, + VALUE_INCOMPATIBLE: 30502, + ALREADY_IN_WHITELIST: 30503, + NOT_IN_WHITELIST: 30504, // ain_validateAppName - INVALID_APP_NAME_FOR_STATE_LABEL: 30701, - INVALID_APP_NAME_FOR_SERVICE_NAME: 30702, - APP_NAME_ALREADY_IN_USE: 30703, + INVALID_APP_NAME_FOR_STATE_LABEL: 30601, + INVALID_APP_NAME_FOR_SERVICE_NAME: 30602, + APP_NAME_ALREADY_IN_USE: 30603, }; /** diff --git a/json_rpc/admin.js b/json_rpc/admin.js index bb85bc7fb..325492434 100644 --- a/json_rpc/admin.js +++ b/json_rpc/admin.js @@ -1,4 +1,3 @@ -const net = require('net'); const _ = require('lodash'); const { NodeConfigs, @@ -10,124 +9,215 @@ const CommonUtil = require('../common/common-util'); const JsonRpcUtil = require('./json-rpc-util'); const { JSON_RPC_METHODS } = require('./constants'); -module.exports = function getApiAccessApis(node) { +function checkCompatibility(valueA, valueB) { + if (CommonUtil.isBool(valueA)) { + return CommonUtil.isBool(valueB); + } else if (CommonUtil.isIntegerString(valueA) || CommonUtil.isFloatString(valueA)) { + return CommonUtil.isIntegerString(valueB) || CommonUtil.isFloatString(valueB); + } else if (CommonUtil.isArray(valueA) || CommonUtil.isWildcard(valueA)) { + return CommonUtil.isArray(valueB) || CommonUtil.isWildcard(valueB); + } else { + // TODO(kriii): Decide how to work on the object (e.g. TRAFFIC_STATS_PERIOD_SECS_LIST). + return false; + } +} + +module.exports = function getAdminApis(node) { return { - [JSON_RPC_METHODS.AIN_GET_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { + [JSON_RPC_METHODS.AIN_GET_NODE_PARAM]: function(args, done) { const beginTime = Date.now(); const verified = node.verifyNodeAccountSignature(args.message, args.signature); + if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_GET_NODE_PARAM || !verified) { + const latency = Date.now() - beginTime; + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); + done({ code: 403, message: 'Forbidden' }); + return; + } + + const param = args.message.param; const latency = Date.now() - beginTime; - trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_GET, latency); - if (_.get(args.message, 'method') === JSON_RPC_METHODS.AIN_GET_DEV_CLIENT_API_IP_WHITELIST && - verified) { - done(null, - JsonRpcUtil.addProtocolVersion({ result: NodeConfigs.DEV_CLIENT_API_IP_WHITELIST })); + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); + if (NodeConfigs[param] === undefined) { + done(null, JsonRpcUtil.addProtocolVersion({ + result: { + code: JsonRpcApiResultCode.PARAM_INVALID, + message: `Param [${param}] is not exist.` + } + })); } else { - done({ code: 403, message: 'Forbidden' }); + done(null, JsonRpcUtil.addProtocolVersion({ result: NodeConfigs[param] })); } }, - [JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { + [JSON_RPC_METHODS.AIN_SET_NODE_PARAM]: function(args, done) { const beginTime = Date.now(); const verified = node.verifyNodeAccountSignature(args.message, args.signature); - if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST || - !verified) { + if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_SET_NODE_PARAM || !verified) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done({ code: 403, message: 'Forbidden' }); return; } - if (CommonUtil.isWildcard(args.message.ip)) { - NodeConfigs.DEV_CLIENT_API_IP_WHITELIST = '*'; + + const param = args.message.param; + if (NodeConfigs[param] === undefined) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.SUCCESS, - message: `Added IP (${args.message.ip}) to whitelist: ${JSON.stringify(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST)}` + code: JsonRpcApiResultCode.PARAM_INVALID, + message: `Param [${param}] is not exists.` } })); return; } - if (!net.isIPv4(args.message.ip)) { + + if (!checkCompatibility(NodeConfigs[param], args.message.value)) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.INVALID_IP, - message: `Invalid IP: ${args.message.ip}` + code: JsonRpcApiResultCode.VALUE_INCOMPATIBLE, + message: `(${args.message.value}) is incompatible with param [${param}]: ${JSON.stringify(NodeConfigs[param])}` } })); return; } - if (!CommonUtil.isArray(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST)) { - // NOTE(liayoo): if the whitelist was "*" previously, adding an IP will no longer "allow-all". - NodeConfigs.DEV_CLIENT_API_IP_WHITELIST = []; + + NodeConfigs[param] = args.message.value; + const latency = Date.now() - beginTime; + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); + done(null, JsonRpcUtil.addProtocolVersion({ + result: { + code: JsonRpcApiResultCode.SUCCESS, + message: `Param [${param}] is now set as: ${JSON.stringify(NodeConfigs[param])}` + } + })); + }, + + [JSON_RPC_METHODS.AIN_ADD_TO_WHITELIST_NODE_PARAM]: function(args, done) { + const beginTime = Date.now(); + const verified = node.verifyNodeAccountSignature(args.message, args.signature); + if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_ADD_TO_WHITELIST_NODE_PARAM || !verified) { + const latency = Date.now() - beginTime; + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); + done({ code: 403, message: 'Forbidden' }); + return; } - if (NodeConfigs.DEV_CLIENT_API_IP_WHITELIST.includes(args.message.ip)) { + + const param = args.message.param; + if (NodeConfigs[param] === undefined) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.IP_ALREADY_IN_WHITELIST, - message: `IP (${args.message.ip}) already in whitelist: ${JSON.stringify(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST)}` + code: JsonRpcApiResultCode.PARAM_INVALID, + message: `Param [${param}] is not exists.` } })); - } else { - NodeConfigs.DEV_CLIENT_API_IP_WHITELIST.push(args.message.ip); + return; + } + if (!CommonUtil.isArray(NodeConfigs[param]) && + !CommonUtil.isWildcard(NodeConfigs[param])) { + const latency = Date.now() - beginTime; + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); + done(null, JsonRpcUtil.addProtocolVersion({ + result: { + code: JsonRpcApiResultCode.PARAM_INVALID, + message: `Param [${param}] is not whitelist` + } + })); + return; + } + + if (!CommonUtil.isArray(NodeConfigs[param])) { + // NOTE(liayoo): if the whitelist was "*" previously, adding an IP will no longer "allow-all". + NodeConfigs[param] = []; + } + if (NodeConfigs[param].includes(args.message.value)) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.SUCCESS, - message: `Added IP (${args.message.ip}) to whitelist: ${JSON.stringify(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST)}` + code: JsonRpcApiResultCode.ALREADY_IN_WHITELIST, + message: `(${args.message.value}) already in whitelist [${param}]: ${JSON.stringify(NodeConfigs[param])}` } })); + return; } + + if (CommonUtil.isWildcard(args.message.value)) { + NodeConfigs[param] = '*'; + } else { + NodeConfigs[param].push(args.message.value); + } + const latency = Date.now() - beginTime; + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); + done(null, JsonRpcUtil.addProtocolVersion({ + result: { + code: JsonRpcApiResultCode.SUCCESS, + message: `Added (${args.message.value}) to whitelist [${param}]: ${JSON.stringify(NodeConfigs[param])}` + } + })); }, - [JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST]: function(args, done) { + [JSON_RPC_METHODS.AIN_REMOVE_FROM_WHITELIST_NODE_PARAM]: function(args, done) { const beginTime = Date.now(); const verified = node.verifyNodeAccountSignature(args.message, args.signature); - if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST || - !verified) { + if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_REMOVE_FROM_WHITELIST_NODE_PARAM || !verified) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done({ code: 403, message: 'Forbidden' }); return; } - if (CommonUtil.isWildcard(args.message.ip) - && CommonUtil.isWildcard(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST)) { - NodeConfigs.DEV_CLIENT_API_IP_WHITELIST = []; + + const param = args.message.param; + if (NodeConfigs[param] === undefined) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.SUCCESS, - message: `Removed IP (${args.message.ip}) from whitelist: ${JSON.stringify(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST)}` + code: JsonRpcApiResultCode.PARAM_INVALID, + message: `Param [${param}] is not exists.` } })); return; } - if (!CommonUtil.isArray(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) || - !NodeConfigs.DEV_CLIENT_API_IP_WHITELIST.includes(args.message.ip)) { + if (!CommonUtil.isArray(NodeConfigs[param]) && + !CommonUtil.isWildcard(NodeConfigs[param])) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.IP_NOT_IN_WHITELIST, - message: `IP (${args.message.ip}) not in whitelist: ${JSON.stringify(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST)}` + code: JsonRpcApiResultCode.PARAM_INVALID, + message: `Param [${param}] is not whitelist` } })); return; } - NodeConfigs.DEV_CLIENT_API_IP_WHITELIST = NodeConfigs.DEV_CLIENT_API_IP_WHITELIST - .filter((ip) => ip !== args.message.ip); + + if (CommonUtil.isWildcard(args.message.value)) { + NodeConfigs[param] = []; + } else if (!CommonUtil.isArray(NodeConfigs[param]) || + !NodeConfigs[param].includes(args.message.value)) { + const latency = Date.now() - beginTime; + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); + done(null, JsonRpcUtil.addProtocolVersion({ + result: { + code: JsonRpcApiResultCode.NOT_IN_WHITELIST, + message: `(${args.message.value}) not in whitelist [${param}]: ${JSON.stringify(NodeConfigs[param])}` + } + })); + return; + } else { + NodeConfigs[param] = NodeConfigs[param].filter((value) => value !== args.message.value); + } const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { code: JsonRpcApiResultCode.SUCCESS, - message: `Removed IP (${args.message.ip}) from whitelist: ${JSON.stringify(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST)}` + message: `Removed (${args.message.value}) from whitelist [${param}]: ${JSON.stringify(NodeConfigs[param])}` } })); }, diff --git a/json_rpc/constants.js b/json_rpc/constants.js index 6193ffd13..f3bc541c4 100644 --- a/json_rpc/constants.js +++ b/json_rpc/constants.js @@ -1,5 +1,5 @@ const JSON_RPC_METHODS = { - AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST: 'ain_addToDevClientApiIpWhitelist', + AIN_ADD_TO_WHITELIST_NODE_PARAM: 'ain_addToWhitelistNodeParam', AIN_CHECK_PROTOCOL_VERSION: 'ain_checkProtocolVersion', AIN_EVAL_RULE: 'ain_evalRule', AIN_EVAL_OWNER: 'ain_evalOwner', @@ -13,11 +13,11 @@ const JSON_RPC_METHODS = { AIN_GET_BLOCK_TRANSACTION_COUNT_BY_HASH: 'ain_getBlockTransactionCountByHash', AIN_GET_BLOCK_TRANSACTION_COUNT_BY_NUMBER: 'ain_getBlockTransactionCountByNumber', AIN_GET_BOOTSTRAP_PUB_KEY: 'ain_getBootstrapPubKey', - AIN_GET_DEV_CLIENT_API_IP_WHITELIST: 'ain_getDevClientApiIpWhitelist', AIN_GET_EVENT_HANDLER_CHANNEL_INFO: 'ain_getEventHandlerChannelInfo', AIN_GET_EVENT_HANDLER_FILTER_INFO: 'ain_getEventHandlerFilterInfo', AIN_GET_LAST_BLOCK: 'ain_getLastBlock', AIN_GET_LAST_BLOCK_NUMBER: 'ain_getLastBlockNumber', + AIN_GET_NODE_PARAM: 'ain_getNodeParam', AIN_GET_NONCE: 'ain_getNonce', AIN_GET_PENDING_TRANSACTIONS: 'ain_getPendingTransactions', AIN_GET_PROOF_HASH: 'ain_getProofHash', @@ -41,9 +41,10 @@ const JSON_RPC_METHODS = { AIN_MATCH_FUNCTION: 'ain_matchFunction', AIN_MATCH_OWNER: 'ain_matchOwner', AIN_MATCH_RULE: 'ain_matchRule', - AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST: 'ain_removeFromDevClientApiIpWhitelist', + AIN_REMOVE_FROM_WHITELIST_NODE_PARAM: 'ain_removeFromWhitelistNodeParam', AIN_SEND_SIGNED_TRANSACTION: 'ain_sendSignedTransaction', AIN_SEND_SIGNED_TRANSACTION_BATCH: 'ain_sendSignedTransactionBatch', + AIN_SET_NODE_PARAM: 'ain_setNodeParam', AIN_VALIDATE_APP_NAME: 'ain_validateAppName', NET_CONSENSUS_STATUS: 'net_consensusStatus', NET_GET_CHAIN_ID: 'net_getChainId', From 59c70bd96ca9657c69f9e73e1b152d56f528e430 Mon Sep 17 00:00:00 2001 From: kriii Date: Fri, 27 May 2022 11:29:10 +0900 Subject: [PATCH 056/166] Fix tools --- tools/api-access/addToDevClientApiIpWhitelist.js | 13 +++++++------ tools/api-access/getDevClientApiIpWhitelist.js | 6 ++++-- .../api-access/removeFromDevClientApiIpWhitelist.js | 13 +++++++------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/tools/api-access/addToDevClientApiIpWhitelist.js b/tools/api-access/addToDevClientApiIpWhitelist.js index 1bfed6702..f75a2ee8b 100644 --- a/tools/api-access/addToDevClientApiIpWhitelist.js +++ b/tools/api-access/addToDevClientApiIpWhitelist.js @@ -9,14 +9,15 @@ const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); async function sendAddToDevClientApiIpWhitelistRequest(endpointUrl, privateKey, chainId, ip) { const message = { timestamp: Date.now(), - method: JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, - ip, + method: JSON_RPC_METHODS.AIN_ADD_TO_WHITELIST_NODE_PARAM, + param: 'DEV_CLIENT_API_IP_WHITELIST', + value: ip, }; const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); return await axios.post( `${endpointUrl}/json-rpc`, { - method: JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, + method: JSON_RPC_METHODS.AIN_ADD_TO_WHITELIST_NODE_PARAM, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, message, @@ -61,9 +62,9 @@ async function processArguments() { function usage() { console.log('\nUsage:\n node addToDevClientApiIpWhitelist.js [] \n'); console.log('\nExamples:'); - console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 private_key 127.0.0.1'); - console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 mnemonic 127.0.0.1'); - console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file 127.0.0.1'); + console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 private_key 127.0.0.1'); + console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 mnemonic 127.0.0.1'); + console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file 127.0.0.1'); process.exit(0); } diff --git a/tools/api-access/getDevClientApiIpWhitelist.js b/tools/api-access/getDevClientApiIpWhitelist.js index f1277bdd7..7fb66a2f5 100644 --- a/tools/api-access/getDevClientApiIpWhitelist.js +++ b/tools/api-access/getDevClientApiIpWhitelist.js @@ -4,17 +4,19 @@ const ainUtil = require('@ainblockchain/ain-util'); const stringify = require('fast-json-stable-stringify'); const { BlockchainConsts } = require('../../common/constants'); const { getAccountPrivateKey } = require('./util'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); async function sendGetDevClientApiIpWhitelistRequest(endpointUrl, privateKey, chainId) { const message = { timestamp: Date.now(), - method: 'ain_getDevClientApiIpWhitelist', + method: JSON_RPC_METHODS.AIN_GET_NODE_PARAM, + param: 'DEV_CLIENT_API_IP_WHITELIST', }; const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); return await axios.post( `${endpointUrl}/json-rpc`, { - method: 'ain_getDevClientApiIpWhitelist', + method: JSON_RPC_METHODS.AIN_GET_NODE_PARAM, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, message, diff --git a/tools/api-access/removeFromDevClientApiIpWhitelist.js b/tools/api-access/removeFromDevClientApiIpWhitelist.js index 031c3da90..d974eb6e0 100644 --- a/tools/api-access/removeFromDevClientApiIpWhitelist.js +++ b/tools/api-access/removeFromDevClientApiIpWhitelist.js @@ -9,14 +9,15 @@ const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); async function sendRemoveFromToDevClientApiIpWhitelistRequest(endpointUrl, privateKey, chainId, ip) { const message = { timestamp: Date.now(), - method: JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST, - ip, + method: JSON_RPC_METHODS.AIN_REMOVE_FROM_WHITELIST_NODE_PARAM, + param: 'DEV_CLIENT_API_IP_WHITELIST', + value: ip, }; const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); return await axios.post( `${endpointUrl}/json-rpc`, { - method: JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST, + method: JSON_RPC_METHODS.AIN_REMOVE_FROM_WHITELIST_NODE_PARAM, params: { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, message, @@ -61,9 +62,9 @@ async function processArguments() { function usage() { console.log('\nUsage:\n node removeFromDevClientApiIpWhitelist.js [] \n'); console.log('\nExamples:'); - console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 private_key 127.0.0.1'); - console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 mnemonic 127.0.0.1'); - console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file 127.0.0.1'); + console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 private_key 127.0.0.1'); + console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 mnemonic 127.0.0.1'); + console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file 127.0.0.1'); process.exit(0); } From 71f2ef94baaa347306df945ff50c94a969732947 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Fri, 27 May 2022 13:15:03 +0900 Subject: [PATCH 057/166] Fix sendSignedTransaction json rpc --- json_rpc/transaction.js | 1 + test/integration/node.test.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/json_rpc/transaction.js b/json_rpc/transaction.js index f9ac617a6..4a13c67e1 100644 --- a/json_rpc/transaction.js +++ b/json_rpc/transaction.js @@ -118,6 +118,7 @@ module.exports = function getTransactionApis(node, p2pServer) { message: `Invalid transaction signature.` } })); + return; } const result = p2pServer.executeAndBroadcastTransaction(createdTx); const latency = Date.now() - beginTime; diff --git a/test/integration/node.test.js b/test/integration/node.test.js index 7efbcc4a0..d21e5914a 100644 --- a/test/integration/node.test.js +++ b/test/integration/node.test.js @@ -2977,7 +2977,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION} api`, () => { + describe('ain_sendSignedTransaction api', () => { const account = { address: "0x9534bC7529961E5737a3Dd317BdEeD41AC08a52D", private_key: "e96292ef0676287908fc3461f747f106b7b9336f183b1766f83672fbe893664d", @@ -3914,7 +3914,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH} api`, () => { + describe('ain_sendSignedTransactionBatch api', () => { const account = { address: "0x85a620A5A46d01cc1fCF49E73ab00710d4da943E", private_key: "b542fc2ca4a68081b3ba238888d3a8783354c3aa81711340fd69f6ff32798525", From 4e5eb75fb903d6c83cadaab7c96adfede0367a4c Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Fri, 27 May 2022 16:44:55 +0900 Subject: [PATCH 058/166] Use else case --- json_rpc/transaction.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/json_rpc/transaction.js b/json_rpc/transaction.js index 4a13c67e1..b6d6b0477 100644 --- a/json_rpc/transaction.js +++ b/json_rpc/transaction.js @@ -118,12 +118,12 @@ module.exports = function getTransactionApis(node, p2pServer) { message: `Invalid transaction signature.` } })); - return; + } else { + const result = p2pServer.executeAndBroadcastTransaction(createdTx); + const latency = Date.now() - beginTime; + trafficStatsManager.addEvent(TrafficEventTypes.JSON_RPC_SET, latency); + done(null, JsonRpcUtil.addProtocolVersion({ result })); } - const result = p2pServer.executeAndBroadcastTransaction(createdTx); - const latency = Date.now() - beginTime; - trafficStatsManager.addEvent(TrafficEventTypes.JSON_RPC_SET, latency); - done(null, JsonRpcUtil.addProtocolVersion({ result })); } } }, From 20f8bed73c5c34854898326d84d89080fb58a297 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Fri, 27 May 2022 18:12:23 +0900 Subject: [PATCH 059/166] mv files to json-rpc-get dir --- .../{json-rpc/getLastBlock.js => json-rpc-get/ainGetLastBlock.js} | 0 tools/{json-rpc => json-rpc-get}/config_local.js | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tools/{json-rpc/getLastBlock.js => json-rpc-get/ainGetLastBlock.js} (100%) rename tools/{json-rpc => json-rpc-get}/config_local.js (100%) diff --git a/tools/json-rpc/getLastBlock.js b/tools/json-rpc-get/ainGetLastBlock.js similarity index 100% rename from tools/json-rpc/getLastBlock.js rename to tools/json-rpc-get/ainGetLastBlock.js diff --git a/tools/json-rpc/config_local.js b/tools/json-rpc-get/config_local.js similarity index 100% rename from tools/json-rpc/config_local.js rename to tools/json-rpc-get/config_local.js From 77c7a88a4e7a1d6c809a913806fc19ea01801463 Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Mon, 16 May 2022 16:08:46 +0900 Subject: [PATCH 060/166] Set fromApi=true for get operations as well --- db/index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/db/index.js b/db/index.js index 3925fe08e..b71538b66 100644 --- a/db/index.js +++ b/db/index.js @@ -720,7 +720,7 @@ class DB { checkRespTreeLimitsForEvalOrMatch(rootLabel, localPath, options) { if (options && options.fromApi) { - const targetStateRoot = options.isFinal ? this.stateManager.getFinalRoot() : this.stateRoot; + const targetStateRoot = options.isFinal ? this.stateManager.getFinalRoot() : this.stateRoot; const fullPath = DB.getFullPath(localPath, rootLabel); const stateNode = DB.getRefForReadingFromStateRoot(targetStateRoot, fullPath); if (stateNode !== null) { @@ -753,19 +753,19 @@ class DB { const resultList = []; for (const op of opList) { if (op.type === undefined || op.type === ReadDbOperations.GET_VALUE) { - resultList.push(this.getValue(op.ref, CommonUtil.toGetOptions(op))); + resultList.push(this.getValue(op.ref, CommonUtil.toGetOptions(op, true))); } else if (op.type === ReadDbOperations.GET_RULE) { - resultList.push(this.getRule(op.ref, CommonUtil.toGetOptions(op))); + resultList.push(this.getRule(op.ref, CommonUtil.toGetOptions(op, true))); } else if (op.type === ReadDbOperations.GET_FUNCTION) { - resultList.push(this.getFunction(op.ref, CommonUtil.toGetOptions(op))); + resultList.push(this.getFunction(op.ref, CommonUtil.toGetOptions(op, true))); } else if (op.type === ReadDbOperations.GET_OWNER) { - resultList.push(this.getOwner(op.ref, CommonUtil.toGetOptions(op))); + resultList.push(this.getOwner(op.ref, CommonUtil.toGetOptions(op, true))); } else if (op.type === ReadDbOperations.MATCH_FUNCTION) { - resultList.push(this.matchFunction(op.ref, CommonUtil.toMatchOrEvalOptions(op))); + resultList.push(this.matchFunction(op.ref, CommonUtil.toMatchOrEvalOptions(op, true))); } else if (op.type === ReadDbOperations.MATCH_RULE) { - resultList.push(this.matchRule(op.ref, CommonUtil.toMatchOrEvalOptions(op))); + resultList.push(this.matchRule(op.ref, CommonUtil.toMatchOrEvalOptions(op, true))); } else if (op.type === ReadDbOperations.MATCH_OWNER) { - resultList.push(this.matchOwner(op.ref, CommonUtil.toMatchOrEvalOptions(op))); + resultList.push(this.matchOwner(op.ref, CommonUtil.toMatchOrEvalOptions(op, true))); } else if (op.type === ReadDbOperations.EVAL_RULE) { const auth = {}; if (op.address) { @@ -775,7 +775,7 @@ class DB { auth.fid = op.fid; } const timestamp = op.timestamp || Date.now(); - const options = Object.assign(CommonUtil.toMatchOrEvalOptions(op), { timestamp }); + const options = Object.assign(CommonUtil.toMatchOrEvalOptions(op, true), { timestamp }); resultList.push(this.evalRule(op.ref, op.value, auth, options)); } else if (op.type === ReadDbOperations.EVAL_OWNER) { const auth = {}; @@ -786,7 +786,7 @@ class DB { auth.fid = op.fid; } resultList.push(this.evalOwner( - op.ref, op.permission, auth, CommonUtil.toMatchOrEvalOptions(op))); + op.ref, op.permission, auth, CommonUtil.toMatchOrEvalOptions(op, true))); } } return resultList; From f6313d2bc41c72f156755ec129d9d3ba66f4b79b Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Tue, 17 May 2022 16:00:40 +0900 Subject: [PATCH 061/166] Add some todos for subtree rule/function/owner retrieval --- db/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/db/index.js b/db/index.js index b71538b66..cc0372784 100644 --- a/db/index.js +++ b/db/index.js @@ -2270,6 +2270,7 @@ class DB { return funcs; } + // TODO(platfowner): Consider optimizing subtree function retrieval logic. getSubtreeFunctions(funcNode) { return this.getSubtreeFunctionsRecursive(0, funcNode); } @@ -2417,6 +2418,7 @@ class DB { return rules; } + // TODO(platfowner): Consider optimizing subtree rule retrieval logic. getSubtreeRules(ruleNode, ruleProp) { return this.getSubtreeRulesRecursive(0, ruleNode, ruleProp); } @@ -2682,6 +2684,7 @@ class DB { return owners; } + // TODO(platfowner): Consider optimizing subtree owner retrieval logic. getSubtreeOwners(ownerNode) { return this.getSubtreeOwnersRecursive(0, ownerNode); } From 09146b0ac07fef6015e31756a20792b0145b0ee3 Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Tue, 17 May 2022 20:42:18 +0900 Subject: [PATCH 062/166] Add is_partial option for get APIs --- common/common-util.js | 3 +++ db/state-node.js | 4 +++- test/unit/db.test.js | 40 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/common/common-util.js b/common/common-util.js index 8f6b5517a..a6f775d9a 100644 --- a/common/common-util.js +++ b/common/common-util.js @@ -244,6 +244,9 @@ class CommonUtil { if (args.is_shallow !== undefined) { options.isShallow = CommonUtil.toBool(args.is_shallow); } + if (args.is_partial !== undefined) { + options.isPartial = CommonUtil.toBool(args.is_partial); + } if (args.include_version !== undefined) { options.includeVersion = CommonUtil.toBool(args.include_version); } diff --git a/db/state-node.js b/db/state-node.js index 93e95a1fd..3dc687d6a 100644 --- a/db/state-node.js +++ b/db/state-node.js @@ -128,6 +128,7 @@ class StateNode { */ toStateSnapshot(options) { const isShallow = options && options.isShallow; + const isPartial = options && options.isPartial; const includeVersion = options && options.includeVersion; const includeTreeInfo = options && options.includeTreeInfo; const includeProof = options && options.includeProof; @@ -135,6 +136,7 @@ class StateNode { return this.getValue(); } const obj = {}; + // TODO(platfowner): Get only partial child labels for isPartial = true. for (const label of this.getChildLabels()) { const childNode = this.getChild(label); if (childNode.getIsLeaf()) { @@ -153,7 +155,7 @@ class StateNode { obj[`${StateLabelProperties.STATE_PROOF_HASH}:${label}`] = childNode.getProofHash(); } } else { - obj[label] = isShallow ? + obj[label] = (isShallow || isPartial) ? { [`${StateLabelProperties.STATE_PROOF_HASH}`]: childNode.getProofHash() } : childNode.toStateSnapshot(options); } diff --git a/test/unit/db.test.js b/test/unit/db.test.js index cf1e19b56..2090816de 100644 --- a/test/unit/db.test.js +++ b/test/unit/db.test.js @@ -339,6 +339,26 @@ describe("DB operations", () => { }) }); + it('getValue to retrieve value near top of database with is_partial', () => { + assert.deepEqual(node.db.getValue('/apps/test', { isPartial: true }), { + 'ai': { + "#state_ph": "0x4c6895fec04b40d425d1542b7cfb2f78b0e8cd2dc4d35d0106100f1ecc168cec" + }, + 'increment': { + "#state_ph": "0x11d1aa4946a3e44e3d467d4da85617d56aecd2559fdd6d9e5dd8fb6b5ded71b8" + }, + 'decrement': { + "#state_ph": "0x11d1aa4946a3e44e3d467d4da85617d56aecd2559fdd6d9e5dd8fb6b5ded71b8" + }, + 'nested': { + "#state_ph": "0x8763e301c728729e38c1f5500a2af7163783bdf0948a7baf7bc87b35f33b347f" + }, + 'shards': { + "#state_ph": "0xbe0fbf9fec28b21de391ebb202517a420f47ee199aece85153e8fb4d9453f223" + }, + }) + }); + it('getValue to retrieve value with include_tree_info', () => { assert.deepEqual(node.db.getValue('/apps/test', { includeTreeInfo: true }), { "#num_children": 5, @@ -964,6 +984,14 @@ describe("DB operations", () => { }, }); }) + + it("getFunction to retrieve existing function config with is_partial", () => { + assert.deepEqual(node.db.getFunction('/apps/test/test_function', { isPartial: true }), { + some: { + "#state_ph": "0x637e4fb9edc3f569e3a4bced647d706bf33742bca14b1aae3ca01fd5b44120d5" + }, + }); + }) }) describe("matchFunction:", () => { @@ -1224,8 +1252,8 @@ describe("DB operations", () => { }); }) - it('getRule to retrieve existing rule config with is_shallow', () => { - assert.deepEqual(node.db.getRule('/apps/test/test_rule', { isShallow: true }), { + it('getRule to retrieve existing rule config with is_partial', () => { + assert.deepEqual(node.db.getRule('/apps/test/test_rule', { isPartial: true }), { "some": { "#state_ph": "0x2be40be7d05dfe5a88319f6aa0f1a7eb61691f8f5fae8c7c993f10892cd29038" }, @@ -1771,6 +1799,14 @@ describe("DB operations", () => { }, }) }) + + it("getOwner to retrieve existing owner config with is_partial", () => { + assert.deepEqual(node.db.getOwner("/apps/test/test_owner", { isPartial: true }), { + some: { + "#state_ph": "0x6127bafe410040319f8d36b1ec0491e16db32d2d0be00f8fc28015c564582b80" + }, + }) + }) }) describe("matchOwner:", () => { From b03c48f409c779397cd6ebda1476ab1c5e8505ba Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Wed, 18 May 2022 11:41:30 +0900 Subject: [PATCH 063/166] Add isPartial param to state and radix node interface --- db/radix-node.js | 3 ++- db/radix-tree.js | 8 ++++---- db/state-node.js | 11 +++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/db/radix-node.js b/db/radix-node.js index 73cf32b15..5433dd123 100644 --- a/db/radix-node.js +++ b/db/radix-node.js @@ -336,7 +336,8 @@ class RadixNode { return false; } - getChildStateNodeList() { + // TODO(platfowner): Get only partial child labels for isPartial = true. + getChildStateNodeList(isPartial = false) { const stateNodeList = []; if (this.hasChildStateNode()) { stateNodeList.push({ diff --git a/db/radix-tree.js b/db/radix-tree.js index 844d13559..6b0f4bb97 100644 --- a/db/radix-tree.js +++ b/db/radix-tree.js @@ -377,16 +377,16 @@ class RadixTree { this.numChildStateNodes-- } - getChildStateLabels() { + getChildStateLabels(isPartial = false) { const labelList = []; - for (const stateNode of this.getChildStateNodes()) { + for (const stateNode of this.getChildStateNodes(isPartial)) { labelList.push(stateNode.getLabel()); } return labelList; } - getChildStateNodes() { - return this.root.getChildStateNodeList().sort((a, b) => a.serial - b.serial) + getChildStateNodes(isPartial = false) { + return this.root.getChildStateNodeList(isPartial).sort((a, b) => a.serial - b.serial) .map(entry => entry.stateNode); } diff --git a/db/state-node.js b/db/state-node.js index 3dc687d6a..de96cbe28 100644 --- a/db/state-node.js +++ b/db/state-node.js @@ -136,8 +136,7 @@ class StateNode { return this.getValue(); } const obj = {}; - // TODO(platfowner): Get only partial child labels for isPartial = true. - for (const label of this.getChildLabels()) { + for (const label of this.getChildLabels(isPartial)) { const childNode = this.getChild(label); if (childNode.getIsLeaf()) { obj[label] = childNode.toStateSnapshot(options); @@ -354,12 +353,12 @@ class StateNode { } } - getChildLabels() { - return [...this.radixTree.getChildStateLabels()]; + getChildLabels(isPartial = false) { + return [...this.radixTree.getChildStateLabels(isPartial)]; } - getChildNodes() { - return [...this.radixTree.getChildStateNodes()]; + getChildNodes(isPartial = false) { + return [...this.radixTree.getChildStateNodes(isPartial)]; } hasChildren() { From 333cad7051db6cd6d594132adbe71f64a10cf7de Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Wed, 18 May 2022 15:24:07 +0900 Subject: [PATCH 064/166] Apply maxListSize for isPartial = true --- common/common-util.js | 2 + db/radix-node.js | 16 +++++- db/radix-tree.js | 11 ++-- db/state-node.js | 12 ++-- test/unit/radix-node.test.js | 104 +++++++++++++++++++++++++++++++++++ test/unit/radix-tree.test.js | 23 ++++++++ 6 files changed, 156 insertions(+), 12 deletions(-) diff --git a/common/common-util.js b/common/common-util.js index a6f775d9a..6d401b273 100644 --- a/common/common-util.js +++ b/common/common-util.js @@ -246,6 +246,8 @@ class CommonUtil { } if (args.is_partial !== undefined) { options.isPartial = CommonUtil.toBool(args.is_partial); + options.lastEndLabel = args.last_end_label !== undefined ? + CommonUtil.toString(args.last_end_label) : null; } if (args.include_version !== undefined) { options.includeVersion = CommonUtil.toBool(args.include_version); diff --git a/db/radix-node.js b/db/radix-node.js index 5433dd123..01048b118 100644 --- a/db/radix-node.js +++ b/db/radix-node.js @@ -336,17 +336,27 @@ class RadixNode { return false; } - // TODO(platfowner): Get only partial child labels for isPartial = true. - getChildStateNodeList(isPartial = false) { + getChildStateNodeList(maxListSize = null) { const stateNodeList = []; + if (CommonUtil.isNumber(maxListSize) && maxListSize <= 0) { + return stateNodeList; + } if (this.hasChildStateNode()) { stateNodeList.push({ serial: this.getSerial(), stateNode: this.getChildStateNode() }); } + if (CommonUtil.isNumber(maxListSize) && stateNodeList.length === maxListSize) { + return stateNodeList; + } for (const child of this.getChildNodes()) { - stateNodeList.push(...child.getChildStateNodeList()); + const maxListSizeForChild = CommonUtil.isNumber(maxListSize) ? + maxListSize - stateNodeList.length : null; + stateNodeList.push(...child.getChildStateNodeList(maxListSizeForChild)); + if (CommonUtil.isNumber(maxListSize) && stateNodeList.length === maxListSize) { + return stateNodeList; + } } return stateNodeList; } diff --git a/db/radix-tree.js b/db/radix-tree.js index 6b0f4bb97..6e92017d0 100644 --- a/db/radix-tree.js +++ b/db/radix-tree.js @@ -2,6 +2,7 @@ const logger = new (require('../logger'))('RADIX_TREE'); const CommonUtil = require('../common/common-util'); const { + NodeConfigs, StateLabelProperties, } = require('../common/constants'); const RadixNode = require('./radix-node'); @@ -377,16 +378,18 @@ class RadixTree { this.numChildStateNodes-- } - getChildStateLabels(isPartial = false) { + getChildStateLabels(isPartial = false, lastEndLabel = null) { const labelList = []; - for (const stateNode of this.getChildStateNodes(isPartial)) { + for (const stateNode of this.getChildStateNodes(isPartial, lastEndLabel)) { labelList.push(stateNode.getLabel()); } return labelList; } - getChildStateNodes(isPartial = false) { - return this.root.getChildStateNodeList(isPartial).sort((a, b) => a.serial - b.serial) + // TODO(platfowner): Apply lastEndLabel and return endLabel. + getChildStateNodes(isPartial = false, lastEndLabel = null) { + const maxListSize = isPartial ? NodeConfigs.GET_RESP_MAX_SIBLINGS : null; + return this.root.getChildStateNodeList(maxListSize).sort((a, b) => a.serial - b.serial) .map(entry => entry.stateNode); } diff --git a/db/state-node.js b/db/state-node.js index de96cbe28..256c6d146 100644 --- a/db/state-node.js +++ b/db/state-node.js @@ -129,6 +129,8 @@ class StateNode { toStateSnapshot(options) { const isShallow = options && options.isShallow; const isPartial = options && options.isPartial; + const lastEndLabel = (options && options.lastEndLabel !== undefined) ? + options.lastEndLabel : null; const includeVersion = options && options.includeVersion; const includeTreeInfo = options && options.includeTreeInfo; const includeProof = options && options.includeProof; @@ -136,7 +138,7 @@ class StateNode { return this.getValue(); } const obj = {}; - for (const label of this.getChildLabels(isPartial)) { + for (const label of this.getChildLabels(isPartial, lastEndLabel)) { const childNode = this.getChild(label); if (childNode.getIsLeaf()) { obj[label] = childNode.toStateSnapshot(options); @@ -353,12 +355,12 @@ class StateNode { } } - getChildLabels(isPartial = false) { - return [...this.radixTree.getChildStateLabels(isPartial)]; + getChildLabels(isPartial = false, lastEndLabel = null) { + return [...this.radixTree.getChildStateLabels(isPartial, lastEndLabel)]; } - getChildNodes(isPartial = false) { - return [...this.radixTree.getChildStateNodes(isPartial)]; + getChildNodes(isPartial = false, lastEndLabel = null) { + return [...this.radixTree.getChildStateNodes(isPartial, lastEndLabel)]; } hasChildren() { diff --git a/test/unit/radix-node.test.js b/test/unit/radix-node.test.js index 945858c86..787a9a477 100644 --- a/test/unit/radix-node.test.js +++ b/test/unit/radix-node.test.js @@ -1304,6 +1304,110 @@ describe("radix-node", () => { ]); }); + it("getChildStateNodeList with non-null maxListSize", () => { + // maxListSize = -1 + const stateNodes1 = node.getChildStateNodeList(-1); + expect(stateNodes1.length).to.equal(0) + assert.deepEqual( + stateNodes1, []); + + // maxListSize = 0 + const stateNodes2 = node.getChildStateNodeList(0); + expect(stateNodes2.length).to.equal(0) + assert.deepEqual( + stateNodes2, []); + + // maxListSize = 1 + const stateNodes3 = node.getChildStateNodeList(1); + expect(stateNodes3.length).to.equal(1) + assert.deepEqual( + stateNodes3, [ + { + serial: 0, + stateNode: stateNode, + }, + // skip the other nodes + ]); + + // maxListSize = 4 + const stateNodes4 = node.getChildStateNodeList(4); + expect(stateNodes4.length).to.equal(4) + assert.deepEqual( + stateNodes4, [ + { + serial: 0, + stateNode: stateNode, + }, + { + serial: 3, + stateNode: childStateNode1, + }, + { + serial: 4, + stateNode: childStateNode2, + }, + { + serial: 2, + stateNode: childStateNode21, + }, + // skip the node of serial 1 + ]); + + // maxListSize = 5 + const stateNodes5 = node.getChildStateNodeList(5); + expect(stateNodes5.length).to.equal(5) + assert.deepEqual( + stateNodes5, [ + { + serial: 0, + stateNode: stateNode, + }, + { + serial: 3, + stateNode: childStateNode1, + }, + { + serial: 4, + stateNode: childStateNode2, + }, + { + serial: 2, + stateNode: childStateNode21, + }, + { + serial: 1, + stateNode: childStateNode22, + }, + ]); + + // maxListSize = 6 + const stateNodes6 = node.getChildStateNodeList(6); + expect(stateNodes6.length).to.equal(5) + assert.deepEqual( + stateNodes6, [ + { + serial: 0, + stateNode: stateNode, + }, + { + serial: 3, + stateNode: childStateNode1, + }, + { + serial: 4, + stateNode: childStateNode2, + }, + { + serial: 2, + stateNode: childStateNode21, + }, + { + serial: 1, + stateNode: childStateNode22, + }, + ]); + }); + it("deleteRadixTreeVersion", () => { const versionAnother = 'ver_another'; const versionYetAnother = 'ver_yet_another'; diff --git a/test/unit/radix-tree.test.js b/test/unit/radix-tree.test.js index 5292a962c..1a5277f07 100644 --- a/test/unit/radix-tree.test.js +++ b/test/unit/radix-tree.test.js @@ -4,6 +4,7 @@ const expect = chai.expect; const assert = chai.assert; const RadixNode = require('../../db/radix-node'); const StateNode = require('../../db/state-node'); +const { NodeConfigs } = require('../../common/constants'); describe("radix-tree", () => { @@ -2865,6 +2866,28 @@ describe("radix-tree", () => { cloned.getChildStateNodes(), [ stateNode22, newStateNode21, stateNode1, stateNode2, newStateNode23 ]); }); + + it("getChildStateLabels / getChildStateNodes with isPartial = true", () => { + const originalGetRespMaxSiblings = NodeConfigs.GET_RESP_MAX_SIBLINGS; + + NodeConfigs.GET_RESP_MAX_SIBLINGS = 3; + // Insertion order is kept + assert.deepEqual(tree.getChildStateLabels(true), [ + // skip label22 + label21, + label1, + label2 + ]); + assert.deepEqual( + tree.getChildStateNodes(true), [ + // skip stateNode22 + stateNode21, + stateNode1, + stateNode2 + ]); + + NodeConfigs.GET_RESP_MAX_SIBLINGS = originalGetRespMaxSiblings; + }); }); describe("radix info", () => { From 7bd3fba6bd18a68033e87476af409f9540a5920f Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Wed, 18 May 2022 21:10:49 +0900 Subject: [PATCH 065/166] Let radix APIs return endLabel --- db/radix-node.js | 31 +++++++++--- db/radix-tree.js | 24 +++++++--- db/state-node.js | 6 ++- test/unit/radix-node.test.js | 91 ++++++++++++++++++++++++++---------- test/unit/radix-tree.test.js | 55 ++++++++++++++-------- 5 files changed, 149 insertions(+), 58 deletions(-) diff --git a/db/radix-node.js b/db/radix-node.js index 01048b118..615a83a53 100644 --- a/db/radix-node.js +++ b/db/radix-node.js @@ -336,29 +336,48 @@ class RadixNode { return false; } - getChildStateNodeList(maxListSize = null) { + getChildStateNodeListWithEndLabel(maxListSize = null) { const stateNodeList = []; + let endLabel = null; if (CommonUtil.isNumber(maxListSize) && maxListSize <= 0) { - return stateNodeList; + return { + list: stateNodeList, + endLabel, + }; } if (this.hasChildStateNode()) { stateNodeList.push({ serial: this.getSerial(), stateNode: this.getChildStateNode() }); + endLabel = this.getLabel(); } if (CommonUtil.isNumber(maxListSize) && stateNodeList.length === maxListSize) { - return stateNodeList; + return { + list: stateNodeList, + endLabel, + }; } for (const child of this.getChildNodes()) { const maxListSizeForChild = CommonUtil.isNumber(maxListSize) ? maxListSize - stateNodeList.length : null; - stateNodeList.push(...child.getChildStateNodeList(maxListSizeForChild)); + const stateNodeListFromChild = child.getChildStateNodeListWithEndLabel(maxListSizeForChild); + if (stateNodeListFromChild.list.length > 0) { + stateNodeList.push(...stateNodeListFromChild.list); + endLabel = stateNodeListFromChild.endLabel !== null ? + this.getLabel() + stateNodeListFromChild.endLabel : null; + } if (CommonUtil.isNumber(maxListSize) && stateNodeList.length === maxListSize) { - return stateNodeList; + return { + list: stateNodeList, + endLabel, + }; } } - return stateNodeList; + return { + list: stateNodeList, + endLabel, + }; } getProofHash() { diff --git a/db/radix-tree.js b/db/radix-tree.js index 6e92017d0..ae5033332 100644 --- a/db/radix-tree.js +++ b/db/radix-tree.js @@ -378,19 +378,29 @@ class RadixTree { this.numChildStateNodes-- } - getChildStateLabels(isPartial = false, lastEndLabel = null) { + getChildStateLabelsWithEndLabel(isPartial = false, lastEndLabel = null) { + const nodesWithEndLabel = this.getChildStateNodesWithEndLabel(isPartial, lastEndLabel); const labelList = []; - for (const stateNode of this.getChildStateNodes(isPartial, lastEndLabel)) { + for (const stateNode of nodesWithEndLabel.list) { labelList.push(stateNode.getLabel()); } - return labelList; + return { + list: labelList, + endLabel: nodesWithEndLabel.endLabel, + }; } - // TODO(platfowner): Apply lastEndLabel and return endLabel. - getChildStateNodes(isPartial = false, lastEndLabel = null) { + // TODO(platfowner): Apply lastEndLabel. + getChildStateNodesWithEndLabel(isPartial = false, lastEndLabel = null) { const maxListSize = isPartial ? NodeConfigs.GET_RESP_MAX_SIBLINGS : null; - return this.root.getChildStateNodeList(maxListSize).sort((a, b) => a.serial - b.serial) + const nodeListWithEndLabel = this.root.getChildStateNodeListWithEndLabel(maxListSize); + const sortedNodeList = nodeListWithEndLabel.list + .sort((a, b) => a.serial - b.serial) .map(entry => entry.stateNode); + return { + list: sortedNodeList, + endLabel: nodeListWithEndLabel.endLabel, + }; } hasChildStateNodes() { @@ -483,7 +493,7 @@ class RadixTree { tree.setRoot(root); tree.setNextSerial(obj[StateLabelProperties.NEXT_SERIAL]); // NOTE(platfowner): Need to recompute and set numChildStateNodes. - const numChildStateNodes = tree.getChildStateLabels().length; + const numChildStateNodes = tree.getChildStateLabelsWithEndLabel().list.length; tree.setNumChildStateNodes(numChildStateNodes); return tree; } diff --git a/db/state-node.js b/db/state-node.js index 256c6d146..bf2e7216d 100644 --- a/db/state-node.js +++ b/db/state-node.js @@ -355,12 +355,14 @@ class StateNode { } } + // TODO(platfowner): Return endLabel for isPartial = true. getChildLabels(isPartial = false, lastEndLabel = null) { - return [...this.radixTree.getChildStateLabels(isPartial, lastEndLabel)]; + return [...this.radixTree.getChildStateLabelsWithEndLabel(isPartial, lastEndLabel).list]; } + // TODO(platfowner): Return endLabel for isPartial = true. getChildNodes(isPartial = false, lastEndLabel = null) { - return [...this.radixTree.getChildStateNodes(isPartial, lastEndLabel)]; + return [...this.radixTree.getChildStateNodesWithEndLabel(isPartial, lastEndLabel).list]; } hasChildren() { diff --git a/test/unit/radix-node.test.js b/test/unit/radix-node.test.js index 787a9a477..756a63f70 100644 --- a/test/unit/radix-node.test.js +++ b/test/unit/radix-node.test.js @@ -1276,11 +1276,12 @@ describe("radix-node", () => { }); }); - it("getChildStateNodeList", () => { - const stateNodes = node.getChildStateNodeList(); - expect(stateNodes.length).to.equal(5) + it("getChildStateNodeListWithEndLabel", () => { + const nodeListWithEndLabel = node.getChildStateNodeListWithEndLabel(); + expect(nodeListWithEndLabel.endLabel).to.equal('000020022022') + expect(nodeListWithEndLabel.list.length).to.equal(5) assert.deepEqual( - stateNodes, [ + nodeListWithEndLabel.list, [ { serial: 0, stateNode: stateNode, @@ -1306,22 +1307,23 @@ describe("radix-node", () => { it("getChildStateNodeList with non-null maxListSize", () => { // maxListSize = -1 - const stateNodes1 = node.getChildStateNodeList(-1); - expect(stateNodes1.length).to.equal(0) - assert.deepEqual( - stateNodes1, []); + const nodeListWithEndLabel1 = node.getChildStateNodeListWithEndLabel(-1); + expect(nodeListWithEndLabel1.endLabel).to.equal(null) + expect(nodeListWithEndLabel1.list.length).to.equal(0) + assert.deepEqual(nodeListWithEndLabel1.list, []); // maxListSize = 0 - const stateNodes2 = node.getChildStateNodeList(0); - expect(stateNodes2.length).to.equal(0) - assert.deepEqual( - stateNodes2, []); + const nodeListWithEndLabel2 = node.getChildStateNodeListWithEndLabel(0); + expect(nodeListWithEndLabel2.endLabel).to.equal(null) + expect(nodeListWithEndLabel2.list.length).to.equal(0) + assert.deepEqual(nodeListWithEndLabel2.list, []); // maxListSize = 1 - const stateNodes3 = node.getChildStateNodeList(1); - expect(stateNodes3.length).to.equal(1) + const nodeListWithEndLabel3 = node.getChildStateNodeListWithEndLabel(1); + expect(nodeListWithEndLabel3.endLabel).to.equal('0000') + expect(nodeListWithEndLabel3.list.length).to.equal(1) assert.deepEqual( - stateNodes3, [ + nodeListWithEndLabel3.list, [ { serial: 0, stateNode: stateNode, @@ -1329,11 +1331,50 @@ describe("radix-node", () => { // skip the other nodes ]); + // maxListSize = 2 + const nodeListWithEndLabel4 = node.getChildStateNodeListWithEndLabel(2); + expect(nodeListWithEndLabel4.endLabel).to.equal('00001001') + expect(nodeListWithEndLabel4.list.length).to.equal(2) + assert.deepEqual( + nodeListWithEndLabel4.list, [ + { + serial: 0, + stateNode: stateNode, + }, + { + serial: 3, + stateNode: childStateNode1, + }, + // skip the node of serial 1 + ]); + + // maxListSize = 3 + const nodeListWithEndLabel5 = node.getChildStateNodeListWithEndLabel(3); + expect(nodeListWithEndLabel5.endLabel).to.equal('00002002') + expect(nodeListWithEndLabel5.list.length).to.equal(3) + assert.deepEqual( + nodeListWithEndLabel5.list, [ + { + serial: 0, + stateNode: stateNode, + }, + { + serial: 3, + stateNode: childStateNode1, + }, + { + serial: 4, + stateNode: childStateNode2, + }, + // skip the node of serial 1 + ]); + // maxListSize = 4 - const stateNodes4 = node.getChildStateNodeList(4); - expect(stateNodes4.length).to.equal(4) + const nodeListWithEndLabel6 = node.getChildStateNodeListWithEndLabel(4); + expect(nodeListWithEndLabel6.endLabel).to.equal('000020021021') + expect(nodeListWithEndLabel6.list.length).to.equal(4) assert.deepEqual( - stateNodes4, [ + nodeListWithEndLabel6.list, [ { serial: 0, stateNode: stateNode, @@ -1354,10 +1395,11 @@ describe("radix-node", () => { ]); // maxListSize = 5 - const stateNodes5 = node.getChildStateNodeList(5); - expect(stateNodes5.length).to.equal(5) + const nodeListWithEndLabel7 = node.getChildStateNodeListWithEndLabel(5); + expect(nodeListWithEndLabel7.endLabel).to.equal('000020022022') + expect(nodeListWithEndLabel7.list.length).to.equal(5) assert.deepEqual( - stateNodes5, [ + nodeListWithEndLabel7.list, [ { serial: 0, stateNode: stateNode, @@ -1381,10 +1423,11 @@ describe("radix-node", () => { ]); // maxListSize = 6 - const stateNodes6 = node.getChildStateNodeList(6); - expect(stateNodes6.length).to.equal(5) + const nodeListWithEndLabel8 = node.getChildStateNodeListWithEndLabel(6); + expect(nodeListWithEndLabel8.endLabel).to.equal('000020022022') + expect(nodeListWithEndLabel8.list.length).to.equal(5) assert.deepEqual( - stateNodes6, [ + nodeListWithEndLabel8.list, [ { serial: 0, stateNode: stateNode, diff --git a/test/unit/radix-tree.test.js b/test/unit/radix-tree.test.js index 1a5277f07..0826d1fa4 100644 --- a/test/unit/radix-tree.test.js +++ b/test/unit/radix-tree.test.js @@ -2838,18 +2838,26 @@ describe("radix-tree", () => { cloned = tree.clone(version2); }); - it("getChildStateLabels / getChildStateNodes", () => { + it("getChildStateLabelsWithEndLabel / getChildStateNodesWithEndLabel", () => { // Insertion order is kept - assert.deepEqual(tree.getChildStateLabels(), [ label22, label21, label1, label2 ]); + const labelsWithEndLabel = tree.getChildStateLabelsWithEndLabel(); + assert.deepEqual(labelsWithEndLabel.list, [ label22, label21, label1, label2 ]); + expect(labelsWithEndLabel.endLabel).to.equal('000bbb222'); + const nodesWithEndLabel = tree.getChildStateNodesWithEndLabel(); assert.deepEqual( - tree.getChildStateNodes(), [ stateNode22, stateNode21, stateNode1, stateNode2 ]); + nodesWithEndLabel.list, [ stateNode22, stateNode21, stateNode1, stateNode2 ]); + expect(nodesWithEndLabel.endLabel).to.equal('000bbb222'); }); - it("getChildStateLabels / getChildStateNodes with cloned tree", () => { + it("getChildStateLabelsWithEndLabel / getChildStateNodesWithEndLabel with cloned tree", () => { // Insertion order is kept. - assert.deepEqual(cloned.getChildStateLabels(), [ label22, label21, label1, label2 ]); + const labelsWithEndLabelBefore = cloned.getChildStateLabelsWithEndLabel(); + assert.deepEqual(labelsWithEndLabelBefore.list, [ label22, label21, label1, label2 ]); + expect(labelsWithEndLabelBefore.endLabel).to.equal('000bbb222'); + const nodesWithEndLabelBefore = cloned.getChildStateNodesWithEndLabel(); assert.deepEqual( - cloned.getChildStateNodes(), [ stateNode22, stateNode21, stateNode1, stateNode2 ]); + nodesWithEndLabelBefore.list, [ stateNode22, stateNode21, stateNode1, stateNode2 ]); + expect(nodesWithEndLabelBefore.endLabel).to.equal('000bbb222'); const newStateNode21 = new StateNode(version2); newStateNode21.setLabel(label21); @@ -2860,32 +2868,41 @@ describe("radix-tree", () => { cloned.set(label23, newStateNode23); // The order does NOT change. + const labelsWithEndLabelAfter = cloned.getChildStateLabelsWithEndLabel(); assert.deepEqual( - cloned.getChildStateLabels(), [ label22, label21, label1, label2, label23 ]); + labelsWithEndLabelAfter.list, [ label22, label21, label1, label2, label23 ]); + expect(labelsWithEndLabelAfter.endLabel).to.equal('000bbb333'); + const nodesWithEndLabelAfter = cloned.getChildStateNodesWithEndLabel(); assert.deepEqual( - cloned.getChildStateNodes(), + nodesWithEndLabelAfter.list, [ stateNode22, newStateNode21, stateNode1, stateNode2, newStateNode23 ]); + expect(nodesWithEndLabelAfter.endLabel).to.equal('000bbb333'); }); - it("getChildStateLabels / getChildStateNodes with isPartial = true", () => { + it("getChildStateLabelsWithEndLabel / getChildStateNodesWithEndLabel with isPartial = true", () => { + // Change GET_RESP_MAX_SIBLINGS value for testing. const originalGetRespMaxSiblings = NodeConfigs.GET_RESP_MAX_SIBLINGS; - NodeConfigs.GET_RESP_MAX_SIBLINGS = 3; + // Insertion order is kept - assert.deepEqual(tree.getChildStateLabels(true), [ + const labelsWithEndLabel = tree.getChildStateLabelsWithEndLabel(true); + assert.deepEqual(labelsWithEndLabel.list, [ // skip label22 label21, label1, label2 ]); - assert.deepEqual( - tree.getChildStateNodes(true), [ - // skip stateNode22 - stateNode21, - stateNode1, - stateNode2 - ]); + expect(labelsWithEndLabel.endLabel).to.equal('000bbb111'); + const nodesWithEndLabel = tree.getChildStateNodesWithEndLabel(true); + assert.deepEqual(nodesWithEndLabel.list, [ + // skip stateNode22 + stateNode21, + stateNode1, + stateNode2 + ]); + expect(nodesWithEndLabel.endLabel).to.equal('000bbb111'); + // Restore GET_RESP_MAX_SIBLINGS value. NodeConfigs.GET_RESP_MAX_SIBLINGS = originalGetRespMaxSiblings; }); }); @@ -3419,7 +3436,7 @@ describe("radix-tree", () => { "#version": "ver", }); assert.deepEqual(treeRebuilt.toRadixSnapshot(), snapshot); - assert.deepEqual(treeRebuilt.getChildStateLabels(), [ + assert.deepEqual(treeRebuilt.getChildStateLabelsWithEndLabel().list, [ "0x000aaa", "0x000bbb", "0x000bbb111", From cb500b5e13ee7dcfdbc53506eb443d4d11c6607b Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Thu, 19 May 2022 16:22:59 +0900 Subject: [PATCH 066/166] Let get APIs return results with 'end label' for child pagination --- common/constants.js | 1 + db/radix-tree.js | 5 +---- db/state-node.js | 20 +++++++++++++++----- test/unit/db.test.js | 34 +++++++++++++++++++--------------- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/common/constants.js b/common/constants.js index 0b1374a00..699e390f4 100644 --- a/common/constants.js +++ b/common/constants.js @@ -412,6 +412,7 @@ const FunctionTypes = { * @enum {string} */ const StateLabelProperties = { + END_LABEL: '#end_label', HAS_PARENT_STATE_NODE: '#has_parent_state_node', HASH_DELIMITER: '#', // Hash delimiter META_LABEL_PREFIX: '#', // Prefix of all meta labels diff --git a/db/radix-tree.js b/db/radix-tree.js index ae5033332..b66f33c2a 100644 --- a/db/radix-tree.js +++ b/db/radix-tree.js @@ -380,10 +380,7 @@ class RadixTree { getChildStateLabelsWithEndLabel(isPartial = false, lastEndLabel = null) { const nodesWithEndLabel = this.getChildStateNodesWithEndLabel(isPartial, lastEndLabel); - const labelList = []; - for (const stateNode of nodesWithEndLabel.list) { - labelList.push(stateNode.getLabel()); - } + const labelList = nodesWithEndLabel.list.map(entry => entry.getLabel()); return { list: labelList, endLabel: nodesWithEndLabel.endLabel, diff --git a/db/state-node.js b/db/state-node.js index bf2e7216d..c7a86810d 100644 --- a/db/state-node.js +++ b/db/state-node.js @@ -138,7 +138,11 @@ class StateNode { return this.getValue(); } const obj = {}; - for (const label of this.getChildLabels(isPartial, lastEndLabel)) { + const childLabelsWithEndLabel = this.getChildLabelsWithEndLabel(isPartial, lastEndLabel); + if (isPartial) { + obj[`${StateLabelProperties.END_LABEL}`] = childLabelsWithEndLabel.endLabel; + } + for (const label of childLabelsWithEndLabel.list) { const childNode = this.getChild(label); if (childNode.getIsLeaf()) { obj[label] = childNode.toStateSnapshot(options); @@ -355,14 +359,20 @@ class StateNode { } } - // TODO(platfowner): Return endLabel for isPartial = true. getChildLabels(isPartial = false, lastEndLabel = null) { - return [...this.radixTree.getChildStateLabelsWithEndLabel(isPartial, lastEndLabel).list]; + return this.getChildLabelsWithEndLabel(isPartial, lastEndLabel).list; + } + + getChildLabelsWithEndLabel(isPartial = false, lastEndLabel = null) { + return this.radixTree.getChildStateLabelsWithEndLabel(isPartial, lastEndLabel); } - // TODO(platfowner): Return endLabel for isPartial = true. getChildNodes(isPartial = false, lastEndLabel = null) { - return [...this.radixTree.getChildStateNodesWithEndLabel(isPartial, lastEndLabel).list]; + return this.getChildNodesWithEndLabel(isPartial, lastEndLabel).list; + } + + getChildNodesWithEndLabel(isPartial = false, lastEndLabel = null) { + return this.radixTree.getChildStateNodesWithEndLabel(isPartial, lastEndLabel); } hasChildren() { diff --git a/test/unit/db.test.js b/test/unit/db.test.js index 2090816de..2f751c43f 100644 --- a/test/unit/db.test.js +++ b/test/unit/db.test.js @@ -319,7 +319,7 @@ describe("DB operations", () => { node.db.stateManager.finalizeVersion(backupFinalVersion); }) - it('getValue to retrieve value near top of database with is_shallow', () => { + it('getValue to retrieve value near top of database with isShallow = true', () => { assert.deepEqual(node.db.getValue('/apps/test', { isShallow: true }), { 'ai': { "#state_ph": "0x4c6895fec04b40d425d1542b7cfb2f78b0e8cd2dc4d35d0106100f1ecc168cec" @@ -339,7 +339,7 @@ describe("DB operations", () => { }) }); - it('getValue to retrieve value near top of database with is_partial', () => { + it('getValue to retrieve value near top of database with isPartial = true', () => { assert.deepEqual(node.db.getValue('/apps/test', { isPartial: true }), { 'ai': { "#state_ph": "0x4c6895fec04b40d425d1542b7cfb2f78b0e8cd2dc4d35d0106100f1ecc168cec" @@ -356,6 +356,7 @@ describe("DB operations", () => { 'shards': { "#state_ph": "0xbe0fbf9fec28b21de391ebb202517a420f47ee199aece85153e8fb4d9453f223" }, + "#end_label": "736861726473" }) }); @@ -623,7 +624,7 @@ describe("DB operations", () => { expect(node.db.getValue("/apps/test/nested/far/down/to/nowhere")).to.equal(null) }) - it("getValue to fail with value not present with is_shallow", () => { + it("getValue to fail with value not present with isShallow = true", () => { expect(node.db.getValue("/apps/test/nested/far/down/to/nowhere", true, false)).to.equal(null) }) }) @@ -977,19 +978,20 @@ describe("DB operations", () => { }); }) - it("getFunction to retrieve existing function config with is_shallow", () => { + it("getFunction to retrieve existing function config with isShallow = true", () => { assert.deepEqual(node.db.getFunction('/apps/test/test_function', { isShallow: true }), { - some: { + "some": { "#state_ph": "0x637e4fb9edc3f569e3a4bced647d706bf33742bca14b1aae3ca01fd5b44120d5" }, }); }) - it("getFunction to retrieve existing function config with is_partial", () => { + it("getFunction to retrieve existing function config with isPartial = true", () => { assert.deepEqual(node.db.getFunction('/apps/test/test_function', { isPartial: true }), { - some: { - "#state_ph": "0x637e4fb9edc3f569e3a4bced647d706bf33742bca14b1aae3ca01fd5b44120d5" + "some": { + "#state_ph": "0x637e4fb9edc3f569e3a4bced647d706bf33742bca14b1aae3ca01fd5b44120d5", }, + "#end_label": "736f6d65" }); }) }) @@ -1252,14 +1254,15 @@ describe("DB operations", () => { }); }) - it('getRule to retrieve existing rule config with is_partial', () => { + it('getRule to retrieve existing rule config with isPartial = true', () => { assert.deepEqual(node.db.getRule('/apps/test/test_rule', { isPartial: true }), { "some": { "#state_ph": "0x2be40be7d05dfe5a88319f6aa0f1a7eb61691f8f5fae8c7c993f10892cd29038" }, "syntax": { "#state_ph": "0x9bf58fc0d77ba1ec1271522dbaab398ebe0e8ea002bb43f6bd860b665e53b732" - } + }, + "#end_label": "73796e746178" }); }); }) @@ -1792,19 +1795,20 @@ describe("DB operations", () => { }); }) - it("getOwner to retrieve existing owner config with is_shallow", () => { + it("getOwner to retrieve existing owner config with isShallow = true", () => { assert.deepEqual(node.db.getOwner("/apps/test/test_owner", { isShallow: true }), { - some: { + "some": { "#state_ph": "0x6127bafe410040319f8d36b1ec0491e16db32d2d0be00f8fc28015c564582b80" }, }) }) - it("getOwner to retrieve existing owner config with is_partial", () => { + it("getOwner to retrieve existing owner config with isPartial = true", () => { assert.deepEqual(node.db.getOwner("/apps/test/test_owner", { isPartial: true }), { - some: { - "#state_ph": "0x6127bafe410040319f8d36b1ec0491e16db32d2d0be00f8fc28015c564582b80" + "some": { + "#state_ph": "0x6127bafe410040319f8d36b1ec0491e16db32d2d0be00f8fc28015c564582b80", }, + "#end_label": "736f6d65" }) }) }) From 8a382540d934b7d34bbcf57a98267e12817e7bc5 Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Wed, 25 May 2022 10:57:05 +0900 Subject: [PATCH 067/166] Add more test cases for isPartial = true --- db/radix-node.js | 6 ++- db/radix-tree.js | 4 +- db/state-node.js | 8 ++-- test/unit/state-node.test.js | 73 ++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 8 deletions(-) diff --git a/db/radix-node.js b/db/radix-node.js index 615a83a53..cd303e63c 100644 --- a/db/radix-node.js +++ b/db/radix-node.js @@ -336,7 +336,8 @@ class RadixNode { return false; } - getChildStateNodeListWithEndLabel(maxListSize = null) { + // TODO(platfowner): Apply lastEndLabel. + getChildStateNodeListWithEndLabel(maxListSize = null, lastEndLabel = null) { const stateNodeList = []; let endLabel = null; if (CommonUtil.isNumber(maxListSize) && maxListSize <= 0) { @@ -361,7 +362,8 @@ class RadixNode { for (const child of this.getChildNodes()) { const maxListSizeForChild = CommonUtil.isNumber(maxListSize) ? maxListSize - stateNodeList.length : null; - const stateNodeListFromChild = child.getChildStateNodeListWithEndLabel(maxListSizeForChild); + const stateNodeListFromChild = + child.getChildStateNodeListWithEndLabel(maxListSizeForChild, lastEndLabel); if (stateNodeListFromChild.list.length > 0) { stateNodeList.push(...stateNodeListFromChild.list); endLabel = stateNodeListFromChild.endLabel !== null ? diff --git a/db/radix-tree.js b/db/radix-tree.js index b66f33c2a..9beb46fd9 100644 --- a/db/radix-tree.js +++ b/db/radix-tree.js @@ -387,10 +387,10 @@ class RadixTree { }; } - // TODO(platfowner): Apply lastEndLabel. getChildStateNodesWithEndLabel(isPartial = false, lastEndLabel = null) { const maxListSize = isPartial ? NodeConfigs.GET_RESP_MAX_SIBLINGS : null; - const nodeListWithEndLabel = this.root.getChildStateNodeListWithEndLabel(maxListSize); + const nodeListWithEndLabel = + this.root.getChildStateNodeListWithEndLabel(maxListSize, lastEndLabel); const sortedNodeList = nodeListWithEndLabel.list .sort((a, b) => a.serial - b.serial) .map(entry => entry.stateNode); diff --git a/db/state-node.js b/db/state-node.js index c7a86810d..d98b8846a 100644 --- a/db/state-node.js +++ b/db/state-node.js @@ -359,16 +359,16 @@ class StateNode { } } - getChildLabels(isPartial = false, lastEndLabel = null) { - return this.getChildLabelsWithEndLabel(isPartial, lastEndLabel).list; + getChildLabels() { + return this.getChildLabelsWithEndLabel().list; } getChildLabelsWithEndLabel(isPartial = false, lastEndLabel = null) { return this.radixTree.getChildStateLabelsWithEndLabel(isPartial, lastEndLabel); } - getChildNodes(isPartial = false, lastEndLabel = null) { - return this.getChildNodesWithEndLabel(isPartial, lastEndLabel).list; + getChildNodes() { + return this.getChildNodesWithEndLabel().list; } getChildNodesWithEndLabel(isPartial = false, lastEndLabel = null) { diff --git a/test/unit/state-node.test.js b/test/unit/state-node.test.js index 64bfad1ec..afc7eb2a9 100644 --- a/test/unit/state-node.test.js +++ b/test/unit/state-node.test.js @@ -3,6 +3,7 @@ const chai = require('chai'); const expect = chai.expect; const assert = chai.assert; +const { NodeConfigs } = require('../../common/constants'); const CommonUtil = require('../../common/common-util'); const RadixNode = require('../../db/radix-node'); const RadixTree = require('../../db/radix-tree'); @@ -976,11 +977,13 @@ describe("state-node", () => { describe("child", () => { const label1 = 'label1'; const label2 = 'label2'; + const label3 = 'label3'; let parent1; let parent2; let child1; let child2; + let child3; beforeEach(() => { parent1 = new StateNode(); @@ -991,6 +994,9 @@ describe("state-node", () => { child2 = new StateNode(); child2.setValue('value2'); + + child3 = new StateNode(); + child3.setValue('value3'); }); it("get / set / has / delete with single parent", () => { @@ -1234,7 +1240,24 @@ describe("state-node", () => { expect(child2.getIsLeaf()).to.equal(true); expect(parent1.getIsLeaf()).to.equal(false); + parent1.setChild(label3, child3); + assert.deepEqual(parent1.getChildLabels(), ['label1', 'label2', 'label3']); + assert.deepEqual(parent1.getChildNodes(), [child1, child2, child3]); + expect(parent1.numChildren()).to.equal(3); + expect(child1.getIsLeaf()).to.equal(true); + expect(child2.getIsLeaf()).to.equal(true); + expect(child3.getIsLeaf()).to.equal(true); + expect(parent1.getIsLeaf()).to.equal(false); + parent1.deleteChild(label2); + assert.deepEqual(parent1.getChildLabels(), ['label1', 'label3']); + assert.deepEqual(parent1.getChildNodes(), [child1, child3]); + expect(parent1.numChildren()).to.equal(2); + expect(child1.getIsLeaf()).to.equal(true); + expect(child2.getIsLeaf()).to.equal(true); + expect(parent1.getIsLeaf()).to.equal(false); + + parent1.deleteChild(label3); assert.deepEqual(parent1.getChildLabels(), ['label1']); assert.deepEqual(parent1.getChildNodes(), [child1]); expect(parent1.numChildren()).to.equal(1); @@ -1250,6 +1273,56 @@ describe("state-node", () => { expect(child2.getIsLeaf()).to.equal(true); expect(parent1.getIsLeaf()).to.equal(true); }); + + it("getChildLabelsWithEndLabel / getChildNodesWithEndLabel w/ isPartial = true", () => { + // Change GET_RESP_MAX_SIBLINGS value for testing. + const originalGetRespMaxSiblings = NodeConfigs.GET_RESP_MAX_SIBLINGS; + NodeConfigs.GET_RESP_MAX_SIBLINGS = 2; + + const labelsWithEndLabel1 = parent1.getChildLabelsWithEndLabel(true); + assert.deepEqual(labelsWithEndLabel1.list, []); + expect(labelsWithEndLabel1.endLabel).to.equal(null); + const nodesWithEndLabel1 = parent1.getChildNodesWithEndLabel(true); + assert.deepEqual(nodesWithEndLabel1.list, []); + expect(nodesWithEndLabel1.endLabel).to.equal(null); + + parent1.setChild(label1, child1); + parent1.setChild(label2, child2); + const labelsWithEndLabel2 = parent1.getChildLabelsWithEndLabel(true); + assert.deepEqual(labelsWithEndLabel2.list, ['label1', 'label2']); + expect(labelsWithEndLabel2.endLabel).to.equal('6c6162656c32'); + const nodesWithEndLabel2 = parent1.getChildNodesWithEndLabel(true); + assert.deepEqual(nodesWithEndLabel2.list, [child1, child2]); + expect(nodesWithEndLabel2.endLabel).to.equal('6c6162656c32'); + + parent1.setChild(label3, child3); + const labelsWithEndLabel3 = parent1.getChildLabelsWithEndLabel(true); + assert.deepEqual(labelsWithEndLabel3.list, ['label1', 'label2']); + expect(labelsWithEndLabel3.endLabel).to.equal('6c6162656c32'); + const nodesWithEndLabel3 = parent1.getChildNodesWithEndLabel(true); + assert.deepEqual(nodesWithEndLabel3.list, [child1, child2]); + expect(nodesWithEndLabel3.endLabel).to.equal('6c6162656c32'); + + parent1.deleteChild(label2); + const labelsWithEndLabel4 = parent1.getChildLabelsWithEndLabel(true); + assert.deepEqual(labelsWithEndLabel4.list, ['label1', 'label3']); + expect(labelsWithEndLabel4.endLabel).to.equal('6c6162656c33'); + const nodesWithEndLabel4 = parent1.getChildNodesWithEndLabel(true); + assert.deepEqual(nodesWithEndLabel4.list, [child1, child3]); + expect(nodesWithEndLabel4.endLabel).to.equal('6c6162656c33'); + + parent1.deleteChild(label3); + parent1.deleteChild(label1); + const labelsWithEndLabel5 = parent1.getChildLabelsWithEndLabel(true); + assert.deepEqual(labelsWithEndLabel5.list, []); + expect(labelsWithEndLabel5.endLabel).to.equal(null); + const nodesWithEndLabel5 = parent1.getChildNodesWithEndLabel(true); + assert.deepEqual(nodesWithEndLabel5.list, []); + expect(nodesWithEndLabel5.endLabel).to.equal(null); + + // Restore GET_RESP_MAX_SIBLINGS value. + NodeConfigs.GET_RESP_MAX_SIBLINGS = originalGetRespMaxSiblings; + }); }); describe("radix tree", () => { From d788dd0646edff74f2daef42593b55c87f67f96a Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Wed, 25 May 2022 19:08:32 +0900 Subject: [PATCH 068/166] Apply lastEndLabel param to child state node retrieval --- db/radix-node.js | 23 +++- test/unit/db.test.js | 85 +++++++++++++- test/unit/radix-node.test.js | 220 ++++++++++++++++++++++++++++++++++- test/unit/radix-tree.test.js | 24 ++++ test/unit/state-node.test.js | 94 ++++++++++++++- 5 files changed, 433 insertions(+), 13 deletions(-) diff --git a/db/radix-node.js b/db/radix-node.js index cd303e63c..cc399a05b 100644 --- a/db/radix-node.js +++ b/db/radix-node.js @@ -336,7 +336,13 @@ class RadixNode { return false; } - // TODO(platfowner): Apply lastEndLabel. + static compareRadixLabelWithPrefix(label, prefix) { + if (_.startsWith(prefix, label)) { + return 0; + } + return label.localeCompare(prefix); + } + getChildStateNodeListWithEndLabel(maxListSize = null, lastEndLabel = null) { const stateNodeList = []; let endLabel = null; @@ -346,7 +352,7 @@ class RadixNode { endLabel, }; } - if (this.hasChildStateNode()) { + if (this.hasChildStateNode() && !CommonUtil.isString(lastEndLabel)) { stateNodeList.push({ serial: this.getSerial(), stateNode: this.getChildStateNode() @@ -359,11 +365,20 @@ class RadixNode { endLabel, }; } + const lastEndLabelForChild = CommonUtil.isString(lastEndLabel) ? + lastEndLabel.slice(this.getLabel().length) : null; for (const child of this.getChildNodes()) { + // NOTE: Whether the child's label is less than, greater than, or equals to the given last label prefix or not. + const labelComparison = CommonUtil.isString(lastEndLabelForChild) ? + RadixNode.compareRadixLabelWithPrefix(child.getLabel(), lastEndLabelForChild) : 1; + if (labelComparison < 0) { + continue; + } const maxListSizeForChild = CommonUtil.isNumber(maxListSize) ? maxListSize - stateNodeList.length : null; - const stateNodeListFromChild = - child.getChildStateNodeListWithEndLabel(maxListSizeForChild, lastEndLabel); + const stateNodeListFromChild = child.getChildStateNodeListWithEndLabel( + maxListSizeForChild, + labelComparison > 0 ? null : lastEndLabelForChild); if (stateNodeListFromChild.list.length > 0) { stateNodeList.push(...stateNodeListFromChild.list); endLabel = stateNodeListFromChild.endLabel !== null ? diff --git a/test/unit/db.test.js b/test/unit/db.test.js index 2f751c43f..8cfff3887 100644 --- a/test/unit/db.test.js +++ b/test/unit/db.test.js @@ -339,7 +339,7 @@ describe("DB operations", () => { }) }); - it('getValue to retrieve value near top of database with isPartial = true', () => { + it('getValue to retrieve value near top of database with isPartial = true and lastEndLabel', () => { assert.deepEqual(node.db.getValue('/apps/test', { isPartial: true }), { 'ai': { "#state_ph": "0x4c6895fec04b40d425d1542b7cfb2f78b0e8cd2dc4d35d0106100f1ecc168cec" @@ -358,6 +358,53 @@ describe("DB operations", () => { }, "#end_label": "736861726473" }) + // lastEndLabel = "736861726472" ("736861726473" - 1) + assert.deepEqual(node.db.getValue('/apps/test', { isPartial: true, lastEndLabel: "736861726472" }), { + 'shards': { + "#state_ph": "0xbe0fbf9fec28b21de391ebb202517a420f47ee199aece85153e8fb4d9453f223" + }, + "#end_label": "736861726473" + }) + assert.deepEqual(node.db.getValue('/apps/test', { isPartial: true, lastEndLabel: "736861726473" }), { + "#end_label": null + }) + }); + + it('getValue to retrieve value near top of database with isPartial = true and lastEndLabel - chaining', () => { + // Change GET_RESP_MAX_SIBLINGS value for testing. + const originalGetRespMaxSiblings = NodeConfigs.GET_RESP_MAX_SIBLINGS; + NodeConfigs.GET_RESP_MAX_SIBLINGS = 2; + + assert.deepEqual(node.db.getValue('/apps/test', { isPartial: true }), { + "ai": { + "#state_ph": "0x4c6895fec04b40d425d1542b7cfb2f78b0e8cd2dc4d35d0106100f1ecc168cec" + }, + "decrement": { + "#state_ph": "0x11d1aa4946a3e44e3d467d4da85617d56aecd2559fdd6d9e5dd8fb6b5ded71b8" + }, + "#end_label": "64656372656d656e74" + }) + assert.deepEqual(node.db.getValue('/apps/test', { isPartial: true, lastEndLabel: "64656372656d656e74" }), { + "increment": { + "#state_ph": "0x11d1aa4946a3e44e3d467d4da85617d56aecd2559fdd6d9e5dd8fb6b5ded71b8" + }, + "nested": { + "#state_ph": "0x8763e301c728729e38c1f5500a2af7163783bdf0948a7baf7bc87b35f33b347f" + }, + "#end_label": "6e6573746564" + }) + assert.deepEqual(node.db.getValue('/apps/test', { isPartial: true, lastEndLabel: "6e6573746564" }), { + "shards": { + "#state_ph": "0xbe0fbf9fec28b21de391ebb202517a420f47ee199aece85153e8fb4d9453f223" + }, + "#end_label": "736861726473" + }) + assert.deepEqual(node.db.getValue('/apps/test', { isPartial: true, lastEndLabel: "736861726473" }), { + "#end_label": null + }) + + // Restore GET_RESP_MAX_SIBLINGS value. + NodeConfigs.GET_RESP_MAX_SIBLINGS = originalGetRespMaxSiblings; }); it('getValue to retrieve value with include_tree_info', () => { @@ -986,13 +1033,23 @@ describe("DB operations", () => { }); }) - it("getFunction to retrieve existing function config with isPartial = true", () => { + it("getFunction to retrieve existing function config with isPartial = true and lastEndLabel", () => { assert.deepEqual(node.db.getFunction('/apps/test/test_function', { isPartial: true }), { "some": { "#state_ph": "0x637e4fb9edc3f569e3a4bced647d706bf33742bca14b1aae3ca01fd5b44120d5", }, "#end_label": "736f6d65" }); + // lastEndLabel = "736f6d64" ("736f6d65" - 1) + assert.deepEqual(node.db.getFunction('/apps/test/test_function', { isPartial: true, lastEndLabel: "736f6d64" }), { + "some": { + "#state_ph": "0x637e4fb9edc3f569e3a4bced647d706bf33742bca14b1aae3ca01fd5b44120d5", + }, + "#end_label": "736f6d65" + }); + assert.deepEqual(node.db.getFunction('/apps/test/test_function', { isPartial: true, lastEndLabel: "736f6d65" }), { + "#end_label": null + }); }) }) @@ -1254,7 +1311,7 @@ describe("DB operations", () => { }); }) - it('getRule to retrieve existing rule config with isPartial = true', () => { + it('getRule to retrieve existing rule config with isPartial = true and lastEndLabel', () => { assert.deepEqual(node.db.getRule('/apps/test/test_rule', { isPartial: true }), { "some": { "#state_ph": "0x2be40be7d05dfe5a88319f6aa0f1a7eb61691f8f5fae8c7c993f10892cd29038" @@ -1264,6 +1321,16 @@ describe("DB operations", () => { }, "#end_label": "73796e746178" }); + // lastEndLabel = "73796e746177" ("73796e746178" - 1) + assert.deepEqual(node.db.getRule('/apps/test/test_rule', { isPartial: true, lastEndLabel: "73796e746177" }), { + "syntax": { + "#state_ph": "0x9bf58fc0d77ba1ec1271522dbaab398ebe0e8ea002bb43f6bd860b665e53b732" + }, + "#end_label": "73796e746178" + }); + assert.deepEqual(node.db.getRule('/apps/test/test_rule', { isPartial: true, lastEndLabel: "73796e746178" }), { + "#end_label": null + }); }); }) @@ -1803,13 +1870,23 @@ describe("DB operations", () => { }) }) - it("getOwner to retrieve existing owner config with isPartial = true", () => { + it("getOwner to retrieve existing owner config with isPartial = true and lastEndLabel", () => { assert.deepEqual(node.db.getOwner("/apps/test/test_owner", { isPartial: true }), { "some": { "#state_ph": "0x6127bafe410040319f8d36b1ec0491e16db32d2d0be00f8fc28015c564582b80", }, "#end_label": "736f6d65" }) + // lastEndLabel = "736f6d64" ("736f6d65" - 1) + assert.deepEqual(node.db.getOwner("/apps/test/test_owner", { isPartial: true, lastEndLabel: "736f6d64" }), { + "some": { + "#state_ph": "0x6127bafe410040319f8d36b1ec0491e16db32d2d0be00f8fc28015c564582b80", + }, + "#end_label": "736f6d65" + }) + assert.deepEqual(node.db.getOwner("/apps/test/test_owner", { isPartial: true, lastEndLabel: "736f6d65" }), { + "#end_label": null + }) }) }) diff --git a/test/unit/radix-node.test.js b/test/unit/radix-node.test.js index 756a63f70..1358366e2 100644 --- a/test/unit/radix-node.test.js +++ b/test/unit/radix-node.test.js @@ -1276,6 +1276,20 @@ describe("radix-node", () => { }); }); + it("compareRadixLabelWithPrefix", () => { + expect(RadixNode.compareRadixLabelWithPrefix('', '')).to.equal(0); + expect(RadixNode.compareRadixLabelWithPrefix('', 'aabbcc')).to.equal(0); + expect(RadixNode.compareRadixLabelWithPrefix('a', 'aabbcc')).to.equal(0); + expect(RadixNode.compareRadixLabelWithPrefix('aa', 'aabbcc')).to.equal(0); + expect(RadixNode.compareRadixLabelWithPrefix('aab', 'aabbcc')).to.equal(0); + expect(RadixNode.compareRadixLabelWithPrefix('aab9', 'aabbcc')).to.equal(-1); + expect(RadixNode.compareRadixLabelWithPrefix('aab9c', 'aabbcc')).to.equal(-1); + expect(RadixNode.compareRadixLabelWithPrefix('aabc', 'aabbcc')).to.equal(1); + expect(RadixNode.compareRadixLabelWithPrefix('aabcc', 'aabbcc')).to.equal(1); + expect(RadixNode.compareRadixLabelWithPrefix('ab', 'aabbcc')).to.equal(1); + expect(RadixNode.compareRadixLabelWithPrefix('aabbcc', 'aabbcc')).to.equal(0); + }); + it("getChildStateNodeListWithEndLabel", () => { const nodeListWithEndLabel = node.getChildStateNodeListWithEndLabel(); expect(nodeListWithEndLabel.endLabel).to.equal('000020022022') @@ -1305,7 +1319,7 @@ describe("radix-node", () => { ]); }); - it("getChildStateNodeList with non-null maxListSize", () => { + it("getChildStateNodeListWithLabel with non-null maxListSize", () => { // maxListSize = -1 const nodeListWithEndLabel1 = node.getChildStateNodeListWithEndLabel(-1); expect(nodeListWithEndLabel1.endLabel).to.equal(null) @@ -1345,7 +1359,7 @@ describe("radix-node", () => { serial: 3, stateNode: childStateNode1, }, - // skip the node of serial 1 + // skip the other nodes ]); // maxListSize = 3 @@ -1366,7 +1380,7 @@ describe("radix-node", () => { serial: 4, stateNode: childStateNode2, }, - // skip the node of serial 1 + // skip the other nodes ]); // maxListSize = 4 @@ -1391,7 +1405,7 @@ describe("radix-node", () => { serial: 2, stateNode: childStateNode21, }, - // skip the node of serial 1 + // skip the other nodes ]); // maxListSize = 5 @@ -1451,6 +1465,204 @@ describe("radix-node", () => { ]); }); + it("getChildStateNodeListWithLabel with non-null maxListSize and lastEndLabel", () => { + // maxListSize = -1 + // lastEndLabel = '00001001' (childStateNode1) + const nodeListWithEndLabel1 = node.getChildStateNodeListWithEndLabel(-1, '00001001'); + expect(nodeListWithEndLabel1.endLabel).to.equal(null) + expect(nodeListWithEndLabel1.list.length).to.equal(0) + assert.deepEqual(nodeListWithEndLabel1.list, []); + + // maxListSize = 0 + // lastEndLabel = '00001001' (childStateNode1) + const nodeListWithEndLabel2 = node.getChildStateNodeListWithEndLabel(0, '00001001'); + expect(nodeListWithEndLabel2.endLabel).to.equal(null) + expect(nodeListWithEndLabel2.list.length).to.equal(0) + assert.deepEqual(nodeListWithEndLabel2.list, []); + + // maxListSize = 1 + // lastEndLabel = '00001001' (childStateNode1) + const nodeListWithEndLabel3 = node.getChildStateNodeListWithEndLabel(1, '00001001'); + expect(nodeListWithEndLabel3.endLabel).to.equal('00002002') + expect(nodeListWithEndLabel3.list.length).to.equal(1) + assert.deepEqual( + nodeListWithEndLabel3.list, [ + // skip previous nodes + { + serial: 4, + stateNode: childStateNode2, + }, + // skip the other nodes + ]); + + // maxListSize = 2 + // lastEndLabel = '00001001' (childStateNode1) + const nodeListWithEndLabel4 = node.getChildStateNodeListWithEndLabel(2, '00001001'); + expect(nodeListWithEndLabel4.endLabel).to.equal('000020021021') + expect(nodeListWithEndLabel4.list.length).to.equal(2) + assert.deepEqual( + nodeListWithEndLabel4.list, [ + // skip previous nodes + { + serial: 4, + stateNode: childStateNode2, + }, + { + serial: 2, + stateNode: childStateNode21, + }, + // skip the other nodes + ]); + + // maxListSize = 5 + // lastEndLabel = '00001001' (childStateNode1) + const nodeListWithEndLabel5 = node.getChildStateNodeListWithEndLabel(5, '00001001'); + expect(nodeListWithEndLabel5.endLabel).to.equal('000020022022') + expect(nodeListWithEndLabel5.list.length).to.equal(3) + assert.deepEqual( + nodeListWithEndLabel5.list, [ + // skip previous nodes + { + serial: 4, + stateNode: childStateNode2, + }, + { + serial: 2, + stateNode: childStateNode21, + }, + { + serial: 1, + stateNode: childStateNode22, + }, + ]); + + // maxListSize = 6 + // lastEndLabel = '00001001' (childStateNode1) + const nodeListWithEndLabel6 = node.getChildStateNodeListWithEndLabel(6, '00001001'); + expect(nodeListWithEndLabel6.endLabel).to.equal('000020022022') + expect(nodeListWithEndLabel6.list.length).to.equal(3) + assert.deepEqual( + nodeListWithEndLabel6.list, [ + // skip previous nodes + { + serial: 4, + stateNode: childStateNode2, + }, + { + serial: 2, + stateNode: childStateNode21, + }, + { + serial: 1, + stateNode: childStateNode22, + }, + ]); + }); + + it("getChildStateNodeListWithLabel with various lastEndLabel", () => { + // maxListSize = 2 + // lastEndLabel = '00002002' (childStateNode2) + '0' + const nodeListWithEndLabel1 = node.getChildStateNodeListWithEndLabel(2, '00002002' + '0'); + expect(nodeListWithEndLabel1.endLabel).to.equal('000020022022') + expect(nodeListWithEndLabel1.list.length).to.equal(2) + assert.deepEqual( + nodeListWithEndLabel1.list, [ + // skip previous nodes + { + serial: 2, + stateNode: childStateNode21, + }, + { + serial: 1, + stateNode: childStateNode22, + }, + // skip the other nodes + ]); + + // maxListSize = 2 + // lastEndLabel = '00002002' (childStateNode2) + '00' + const nodeListWithEndLabel2 = node.getChildStateNodeListWithEndLabel(2, '00002002' + '00'); + expect(nodeListWithEndLabel2.endLabel).to.equal('000020022022') + expect(nodeListWithEndLabel2.list.length).to.equal(2) + assert.deepEqual( + nodeListWithEndLabel2.list, [ + // skip previous nodes + { + serial: 2, + stateNode: childStateNode21, + }, + { + serial: 1, + stateNode: childStateNode22, + }, + // skip the other nodes + ]); + + // maxListSize = 2 + // lastEndLabel = '000022' (non-existing node) + const nodeListWithEndLabel3 = node.getChildStateNodeListWithEndLabel(2, '000022'); + expect(nodeListWithEndLabel3.endLabel).to.equal(null) + expect(nodeListWithEndLabel3.list.length).to.equal(0) + assert.deepEqual( + nodeListWithEndLabel3.list, []); + }); + + it("getChildStateNodeListWithLabel chaining with endLabel and lastEndLabel", () => { + // maxListSize = 2 + // lastEndLabel = null + const nodeListWithEndLabel1 = node.getChildStateNodeListWithEndLabel(2, null); + expect(nodeListWithEndLabel1.endLabel).to.equal('00001001') + expect(nodeListWithEndLabel1.list.length).to.equal(2) + assert.deepEqual( + nodeListWithEndLabel1.list, [ + { + serial: 0, + stateNode: stateNode, + }, + { + serial: 3, + stateNode: childStateNode1, + }, + ]); + + // maxListSize = 2 + // lastEndLabel = '00001001' + const nodeListWithEndLabel2 = node.getChildStateNodeListWithEndLabel(2, '00001001'); + expect(nodeListWithEndLabel2.endLabel).to.equal('000020021021') + expect(nodeListWithEndLabel2.list.length).to.equal(2) + assert.deepEqual( + nodeListWithEndLabel2.list, [ + { + serial: 4, + stateNode: childStateNode2, + }, + { + serial: 2, + stateNode: childStateNode21, + }, + ]); + + // maxListSize = 2 + // lastEndLabel = '000020021021' + const nodeListWithEndLabel3 = node.getChildStateNodeListWithEndLabel(2, '000020021021'); + expect(nodeListWithEndLabel3.endLabel).to.equal('000020022022') + expect(nodeListWithEndLabel3.list.length).to.equal(1) + assert.deepEqual( + nodeListWithEndLabel3.list, [ + { + serial: 1, + stateNode: childStateNode22, + }, + ]); + + // maxListSize = 2 + // lastEndLabel = '000020022022' + const nodeListWithEndLabel4 = node.getChildStateNodeListWithEndLabel(2, '000020022022'); + expect(nodeListWithEndLabel4.endLabel).to.equal(null) + expect(nodeListWithEndLabel4.list.length).to.equal(0) + assert.deepEqual(nodeListWithEndLabel4.list, []); + }); + it("deleteRadixTreeVersion", () => { const versionAnother = 'ver_another'; const versionYetAnother = 'ver_yet_another'; diff --git a/test/unit/radix-tree.test.js b/test/unit/radix-tree.test.js index 0826d1fa4..412664f8c 100644 --- a/test/unit/radix-tree.test.js +++ b/test/unit/radix-tree.test.js @@ -2905,6 +2905,30 @@ describe("radix-tree", () => { // Restore GET_RESP_MAX_SIBLINGS value. NodeConfigs.GET_RESP_MAX_SIBLINGS = originalGetRespMaxSiblings; }); + + it("getChildStateLabelsWithEndLabel / getChildStateNodesWithEndLabel with isPartial = true and lastEndLabel", () => { + // Change GET_RESP_MAX_SIBLINGS value for testing. + const originalGetRespMaxSiblings = NodeConfigs.GET_RESP_MAX_SIBLINGS; + NodeConfigs.GET_RESP_MAX_SIBLINGS = 2; + + // lastEndLabel = '000bbb' (stateNode2) + + const labelsWithEndLabel = tree.getChildStateLabelsWithEndLabel(true, '000bbb'); + assert.deepEqual(labelsWithEndLabel.list, [ + label22, + label21, + ]); + expect(labelsWithEndLabel.endLabel).to.equal('000bbb222'); + const nodesWithEndLabel = tree.getChildStateNodesWithEndLabel(true, '000bbb'); + assert.deepEqual(nodesWithEndLabel.list, [ + stateNode22, + stateNode21, + ]); + expect(nodesWithEndLabel.endLabel).to.equal('000bbb222'); + + // Restore GET_RESP_MAX_SIBLINGS value. + NodeConfigs.GET_RESP_MAX_SIBLINGS = originalGetRespMaxSiblings; + }); }); describe("radix info", () => { diff --git a/test/unit/state-node.test.js b/test/unit/state-node.test.js index afc7eb2a9..596659d42 100644 --- a/test/unit/state-node.test.js +++ b/test/unit/state-node.test.js @@ -1274,7 +1274,7 @@ describe("state-node", () => { expect(parent1.getIsLeaf()).to.equal(true); }); - it("getChildLabelsWithEndLabel / getChildNodesWithEndLabel w/ isPartial = true", () => { + it("getChildLabelsWithEndLabel / getChildNodesWithEndLabel with isPartial = true", () => { // Change GET_RESP_MAX_SIBLINGS value for testing. const originalGetRespMaxSiblings = NodeConfigs.GET_RESP_MAX_SIBLINGS; NodeConfigs.GET_RESP_MAX_SIBLINGS = 2; @@ -1297,9 +1297,11 @@ describe("state-node", () => { parent1.setChild(label3, child3); const labelsWithEndLabel3 = parent1.getChildLabelsWithEndLabel(true); + // skip label3 assert.deepEqual(labelsWithEndLabel3.list, ['label1', 'label2']); expect(labelsWithEndLabel3.endLabel).to.equal('6c6162656c32'); const nodesWithEndLabel3 = parent1.getChildNodesWithEndLabel(true); + // skip child3 assert.deepEqual(nodesWithEndLabel3.list, [child1, child2]); expect(nodesWithEndLabel3.endLabel).to.equal('6c6162656c32'); @@ -1323,6 +1325,96 @@ describe("state-node", () => { // Restore GET_RESP_MAX_SIBLINGS value. NodeConfigs.GET_RESP_MAX_SIBLINGS = originalGetRespMaxSiblings; }); + + it("getChildLabelsWithEndLabel / getChildNodesWithEndLabel with isPartial = true and lastEndLabel", () => { + // Change GET_RESP_MAX_SIBLINGS value for testing. + const originalGetRespMaxSiblings = NodeConfigs.GET_RESP_MAX_SIBLINGS; + NodeConfigs.GET_RESP_MAX_SIBLINGS = 2; + + // lastEndLabel = ''6c6162656c31' (child1) + + const labelsWithEndLabel1 = parent1.getChildLabelsWithEndLabel(true, '6c6162656c31'); + assert.deepEqual(labelsWithEndLabel1.list, []); + expect(labelsWithEndLabel1.endLabel).to.equal(null); + const nodesWithEndLabel1 = parent1.getChildNodesWithEndLabel(true, '6c6162656c31'); + assert.deepEqual(nodesWithEndLabel1.list, []); + expect(nodesWithEndLabel1.endLabel).to.equal(null); + + parent1.setChild(label1, child1); + parent1.setChild(label2, child2); + const labelsWithEndLabel2 = parent1.getChildLabelsWithEndLabel(true, '6c6162656c31'); + assert.deepEqual(labelsWithEndLabel2.list, ['label2']); + expect(labelsWithEndLabel2.endLabel).to.equal('6c6162656c32'); + const nodesWithEndLabel2 = parent1.getChildNodesWithEndLabel(true, '6c6162656c31'); + assert.deepEqual(nodesWithEndLabel2.list, [child2]); + expect(nodesWithEndLabel2.endLabel).to.equal('6c6162656c32'); + + parent1.setChild(label3, child3); + const labelsWithEndLabel3 = parent1.getChildLabelsWithEndLabel(true, '6c6162656c31'); + // skip label1 + assert.deepEqual(labelsWithEndLabel3.list, ['label2', 'label3']); + expect(labelsWithEndLabel3.endLabel).to.equal('6c6162656c33'); + const nodesWithEndLabel3 = parent1.getChildNodesWithEndLabel(true, '6c6162656c31'); + // skip child1 + assert.deepEqual(nodesWithEndLabel3.list, [child2, child3]); + expect(nodesWithEndLabel3.endLabel).to.equal('6c6162656c33'); + + parent1.deleteChild(label2); + const labelsWithEndLabel4 = parent1.getChildLabelsWithEndLabel(true, '6c6162656c31'); + // skip label1 + assert.deepEqual(labelsWithEndLabel4.list, ['label3']); + expect(labelsWithEndLabel4.endLabel).to.equal('6c6162656c33'); + const nodesWithEndLabel4 = parent1.getChildNodesWithEndLabel(true, '6c6162656c31'); + // skip child1 + assert.deepEqual(nodesWithEndLabel4.list, [child3]); + expect(nodesWithEndLabel4.endLabel).to.equal('6c6162656c33'); + + parent1.deleteChild(label3); + parent1.deleteChild(label1); + const labelsWithEndLabel5 = parent1.getChildLabelsWithEndLabel(true, '6c6162656c31'); + assert.deepEqual(labelsWithEndLabel5.list, []); + expect(labelsWithEndLabel5.endLabel).to.equal(null); + const nodesWithEndLabel5 = parent1.getChildNodesWithEndLabel(true, '6c6162656c31'); + assert.deepEqual(nodesWithEndLabel5.list, []); + expect(nodesWithEndLabel5.endLabel).to.equal(null); + + // Restore GET_RESP_MAX_SIBLINGS value. + NodeConfigs.GET_RESP_MAX_SIBLINGS = originalGetRespMaxSiblings; + }); + + it("getChildLabelsWithEndLabel / getChildNodesWithEndLabel with isPartial = true and lastEndLabel - chaining", () => { + // Change GET_RESP_MAX_SIBLINGS value for testing. + const originalGetRespMaxSiblings = NodeConfigs.GET_RESP_MAX_SIBLINGS; + NodeConfigs.GET_RESP_MAX_SIBLINGS = 2; + + parent1.setChild(label1, child1); + parent1.setChild(label2, child2); + parent1.setChild(label3, child3); + + const labelsWithEndLabel1 = parent1.getChildLabelsWithEndLabel(true); + assert.deepEqual(labelsWithEndLabel1.list, ['label1', 'label2']); + expect(labelsWithEndLabel1.endLabel).to.equal('6c6162656c32'); + const nodesWithEndLabel1 = parent1.getChildNodesWithEndLabel(true); + assert.deepEqual(nodesWithEndLabel1.list, [child1, child2]); + expect(nodesWithEndLabel1.endLabel).to.equal('6c6162656c32'); + + const labelsWithEndLabel2 = parent1.getChildLabelsWithEndLabel(true, '6c6162656c32'); + assert.deepEqual(labelsWithEndLabel2.list, ['label3']); + expect(labelsWithEndLabel2.endLabel).to.equal('6c6162656c33'); + const nodesWithEndLabel2 = parent1.getChildNodesWithEndLabel(true, '6c6162656c32'); + assert.deepEqual(nodesWithEndLabel2.list, [child3]); + expect(nodesWithEndLabel2.endLabel).to.equal('6c6162656c33'); + + const labelsWithEndLabel3 = parent1.getChildLabelsWithEndLabel(true, '6c6162656c33'); + assert.deepEqual(labelsWithEndLabel3.list, []); + expect(labelsWithEndLabel3.endLabel).to.equal(null); + const nodesWithEndLabel3 = parent1.getChildNodesWithEndLabel(true, '6c6162656c33'); + assert.deepEqual(nodesWithEndLabel3.list, []); + expect(nodesWithEndLabel3.endLabel).to.equal(null); + + // Restore GET_RESP_MAX_SIBLINGS value. + NodeConfigs.GET_RESP_MAX_SIBLINGS = originalGetRespMaxSiblings; + }); }); describe("radix tree", () => { From 428ca80566c02a14af5ed016190a65dae6a35755 Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Fri, 27 May 2022 11:37:44 +0900 Subject: [PATCH 069/166] Tweak node list length comparision --- db/radix-node.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/radix-node.js b/db/radix-node.js index cc399a05b..cf6f8f95a 100644 --- a/db/radix-node.js +++ b/db/radix-node.js @@ -359,7 +359,7 @@ class RadixNode { }); endLabel = this.getLabel(); } - if (CommonUtil.isNumber(maxListSize) && stateNodeList.length === maxListSize) { + if (CommonUtil.isNumber(maxListSize) && stateNodeList.length >= maxListSize) { return { list: stateNodeList, endLabel, @@ -384,7 +384,7 @@ class RadixNode { endLabel = stateNodeListFromChild.endLabel !== null ? this.getLabel() + stateNodeListFromChild.endLabel : null; } - if (CommonUtil.isNumber(maxListSize) && stateNodeList.length === maxListSize) { + if (CommonUtil.isNumber(maxListSize) && stateNodeList.length >= maxListSize) { return { list: stateNodeList, endLabel, From 30dc8d443d175bae0a068c764b9c4be8a8c7173a Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Fri, 27 May 2022 11:52:58 +0900 Subject: [PATCH 070/166] Add more test cases for non-existing last end labels --- test/unit/radix-node.test.js | 58 +++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/test/unit/radix-node.test.js b/test/unit/radix-node.test.js index 1358366e2..5cb577867 100644 --- a/test/unit/radix-node.test.js +++ b/test/unit/radix-node.test.js @@ -1559,10 +1559,10 @@ describe("radix-node", () => { ]); }); - it("getChildStateNodeListWithLabel with various lastEndLabel", () => { + it("getChildStateNodeListWithLabel with non-existing lastEndLabel", () => { // maxListSize = 2 - // lastEndLabel = '00002002' (childStateNode2) + '0' - const nodeListWithEndLabel1 = node.getChildStateNodeListWithEndLabel(2, '00002002' + '0'); + // lastEndLabel = '000020020' (= '00002002' (childStateNode2) + '0') + const nodeListWithEndLabel1 = node.getChildStateNodeListWithEndLabel(2, '000020020'); expect(nodeListWithEndLabel1.endLabel).to.equal('000020022022') expect(nodeListWithEndLabel1.list.length).to.equal(2) assert.deepEqual( @@ -1580,8 +1580,8 @@ describe("radix-node", () => { ]); // maxListSize = 2 - // lastEndLabel = '00002002' (childStateNode2) + '00' - const nodeListWithEndLabel2 = node.getChildStateNodeListWithEndLabel(2, '00002002' + '00'); + // lastEndLabel = '0000200200' (= '00002002' (childStateNode2) + '00') + const nodeListWithEndLabel2 = node.getChildStateNodeListWithEndLabel(2, '0000200200'); expect(nodeListWithEndLabel2.endLabel).to.equal('000020022022') expect(nodeListWithEndLabel2.list.length).to.equal(2) assert.deepEqual( @@ -1599,12 +1599,50 @@ describe("radix-node", () => { ]); // maxListSize = 2 - // lastEndLabel = '000022' (non-existing node) - const nodeListWithEndLabel3 = node.getChildStateNodeListWithEndLabel(2, '000022'); - expect(nodeListWithEndLabel3.endLabel).to.equal(null) - expect(nodeListWithEndLabel3.list.length).to.equal(0) + // lastEndLabel = '000020' (= '00002002' (childStateNode2) - '02') + const nodeListWithEndLabel3 = node.getChildStateNodeListWithEndLabel(2, '000020'); + expect(nodeListWithEndLabel3.endLabel).to.equal('000020021021') + expect(nodeListWithEndLabel3.list.length).to.equal(2) assert.deepEqual( - nodeListWithEndLabel3.list, []); + nodeListWithEndLabel3.list, [ + // skip previous nodes + { + serial: 4, + stateNode: childStateNode2, + }, + { + serial: 2, + stateNode: childStateNode21, + }, + // skip the other nodes + ]); + + // maxListSize = 2 + // lastEndLabel = '00002' (= '00002002' (childStateNode2) - '002') + const nodeListWithEndLabel4 = node.getChildStateNodeListWithEndLabel(2, '00002'); + expect(nodeListWithEndLabel4.endLabel).to.equal('000020021021') + expect(nodeListWithEndLabel4.list.length).to.equal(2) + assert.deepEqual( + nodeListWithEndLabel4.list, [ + // skip previous nodes + { + serial: 4, + stateNode: childStateNode2, + }, + { + serial: 2, + stateNode: childStateNode21, + }, + // skip the other nodes + ]); + + // maxListSize = 2 + // lastEndLabel = '000021' (= '00002002' (childStateNode2) - '002' + '1') + const nodeListWithEndLabel5 = node.getChildStateNodeListWithEndLabel(2, '000021'); + expect(nodeListWithEndLabel5.endLabel).to.equal(null) + expect(nodeListWithEndLabel5.list.length).to.equal(0) + assert.deepEqual( + nodeListWithEndLabel5.list, []); }); it("getChildStateNodeListWithLabel chaining with endLabel and lastEndLabel", () => { From 1b7f9e21c3cdde6742f1ff44cd758b823cc84df1 Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Fri, 27 May 2022 12:20:30 +0900 Subject: [PATCH 071/166] Tweak test case titles for readability --- test/integration/node.test.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/integration/node.test.js b/test/integration/node.test.js index d21e5914a..3b3c8ff57 100644 --- a/test/integration/node.test.js +++ b/test/integration/node.test.js @@ -568,7 +568,7 @@ describe('Blockchain Node', () => { }) }) - describe('/get api', () => { + describe('get api', () => { it('get', () => { const request = { op_list: [ @@ -825,7 +825,7 @@ describe('Blockchain Node', () => { }); }); - describe(`${JSON_RPC_METHODS.AIN_GET} api`, () => { + describe('json-rpc: ain_get api', () => { it('returns the correct value', () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); @@ -908,7 +908,7 @@ describe('Blockchain Node', () => { }); }); - describe(`${JSON_RPC_METHODS.AIN_MATCH_FUNCTION} api`, () => { + describe('json-rpc: ain_matchFunction api', () => { it('returns correct value', () => { const ref = "/apps/test/test_function/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; @@ -936,7 +936,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_MATCH_RULE} api`, () => { + describe('json-rpc: ain_matchRule api', () => { it('returns correct value (write)', () => { const ref = "/apps/test/test_rule/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; @@ -1062,7 +1062,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_MATCH_OWNER} api`, () => { + describe('json-rpc api: ain_matchOwner', () => { it('returns correct value', () => { const ref = "/apps/test/test_owner/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; @@ -1091,7 +1091,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_EVAL_RULE} api`, () => { + describe('json-rpc api: ain_evalRule', () => { it('returns true', () => { const ref = "/apps/test/test_rule/some/path"; const value = "value"; @@ -1123,7 +1123,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_EVAL_OWNER} api`, () => { + describe('json-rpc api: ain_evalOwner', () => { it('returns correct value', () => { const ref = "/apps/test/test_owner/some/path"; const address = "abcd"; @@ -1167,7 +1167,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_GET_STATE_PROOF} api`, () => { + describe('json-rpc api: ain_getStateProof', () => { it('returns correct value', () => { const ref = '/values/blockchain_params/token/symbol'; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; @@ -1187,7 +1187,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_GET_PROOF_HASH} api`, () => { + describe('json-rpc api: ain_getProofHash', () => { it('returns correct value', () => { const ref = '/values/blockchain_params/token/symbol'; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; @@ -1198,7 +1198,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_GET_STATE_INFO} api`, () => { + describe('json-rpc api: ain_getStateInfo', () => { it('returns correct value', () => { const ref = '/values/apps/test/test_state_info/some/path'; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; @@ -1221,7 +1221,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_GET_STATE_USAGE} api`, () => { + describe('json-rpc api: ain_getStateUsage', () => { it('with existing app name', () => { const request = { app_name: 'test', protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; return jayson.client.http(server1 + '/json-rpc').request(JSON_RPC_METHODS.AIN_GET_STATE_USAGE, request) @@ -1273,7 +1273,7 @@ describe('Blockchain Node', () => { }) }) - describe(`${JSON_RPC_METHODS.AIN_GET_PROTOCOL_VERSION} api`, () => { + describe('json-rpc api: ain_getProtocolVersion', () => { it('returns the correct version', () => { const client = jayson.client.http(server1 + '/json-rpc'); return client.request(JSON_RPC_METHODS.AIN_GET_PROTOCOL_VERSION, {}) @@ -1283,7 +1283,7 @@ describe('Blockchain Node', () => { }); }); - describe(`${JSON_RPC_METHODS.AIN_CHECK_PROTOCOL_VERSION} api`, () => { + describe('json-rpc api: ain_checkProtocolVersion', () => { it('returns success code', () => { const client = jayson.client.http(server1 + '/json-rpc'); return client.request(JSON_RPC_METHODS.AIN_CHECK_PROTOCOL_VERSION, { protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }) @@ -1330,7 +1330,7 @@ describe('Blockchain Node', () => { }); }) - describe(`${JSON_RPC_METHODS.AIN_GET_ADDRESS} api`, () => { + describe('json-rpc api: ain_getAddress', () => { it('returns the correct node address', () => { const expAddr = '0x01A0980d2D4e418c7F27e1ef539d01A5b5E93204'; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); @@ -2977,7 +2977,7 @@ describe('Blockchain Node', () => { }) }) - describe('ain_sendSignedTransaction api', () => { + describe('json-rpc api: ain_sendSignedTransaction', () => { const account = { address: "0x9534bC7529961E5737a3Dd317BdEeD41AC08a52D", private_key: "e96292ef0676287908fc3461f747f106b7b9336f183b1766f83672fbe893664d", @@ -3914,7 +3914,7 @@ describe('Blockchain Node', () => { }) }) - describe('ain_sendSignedTransactionBatch api', () => { + describe('json-rpc api: ain_sendSignedTransactionBatch', () => { const account = { address: "0x85a620A5A46d01cc1fCF49E73ab00710d4da943E", private_key: "b542fc2ca4a68081b3ba238888d3a8783354c3aa81711340fd69f6ff32798525", From 49715db188ffa361331b7be7a1619ead1127365f Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Fri, 27 May 2022 18:15:19 +0900 Subject: [PATCH 072/166] Add a todo for returning node serials for is_partial = true case --- db/state-node.js | 1 + 1 file changed, 1 insertion(+) diff --git a/db/state-node.js b/db/state-node.js index d98b8846a..b94ec3576 100644 --- a/db/state-node.js +++ b/db/state-node.js @@ -126,6 +126,7 @@ class StateNode { /** * Converts this sub-tree to a js object. */ + // TODO(platfowner): Return node serials with the end label for partial results merging. toStateSnapshot(options) { const isShallow = options && options.isShallow; const isPartial = options && options.isPartial; From ae3dcba9b0cae67af18903a4a2f69f1a93257d6d Mon Sep 17 00:00:00 2001 From: Dongil Seo Date: Fri, 27 May 2022 21:46:51 +0900 Subject: [PATCH 073/166] Make resp limit check work with is_partial and is_shallow options --- common/common-util.js | 7 ++++ db/index.js | 62 +++++++++++++++++++++------------- test/integration/node.test.js | 63 ++++++++++++++++++++++++++++++++--- 3 files changed, 104 insertions(+), 28 deletions(-) diff --git a/common/common-util.js b/common/common-util.js index 6d401b273..ee7880bc7 100644 --- a/common/common-util.js +++ b/common/common-util.js @@ -243,11 +243,15 @@ class CommonUtil { } if (args.is_shallow !== undefined) { options.isShallow = CommonUtil.toBool(args.is_shallow); + } else { + options.isShallow = false; } if (args.is_partial !== undefined) { options.isPartial = CommonUtil.toBool(args.is_partial); options.lastEndLabel = args.last_end_label !== undefined ? CommonUtil.toString(args.last_end_label) : null; + } else { + options.isPartial = false; } if (args.include_version !== undefined) { options.includeVersion = CommonUtil.toBool(args.include_version); @@ -266,6 +270,9 @@ class CommonUtil { static toMatchOrEvalOptions(args, fromApi = false) { const options = {}; + // NOTE: Not allowed true values of isShallow or isPartial options in match/eval requests. + options.isShallow = false; + options.isPartial = false; if (args.is_global !== undefined) { options.isGlobal = CommonUtil.toBool(args.is_global); } diff --git a/db/index.js b/db/index.js index cc0372784..ff97fb51e 100644 --- a/db/index.js +++ b/db/index.js @@ -510,7 +510,7 @@ class DB { return null; } if (options && options.fromApi) { - const limitChecked = DB.checkRespTreeLimits(stateNode); + const limitChecked = DB.checkRespTreeLimits(stateNode, options); if (limitChecked !== true) { return limitChecked; } @@ -618,7 +618,9 @@ class DB { } const limitChecked = this.checkRespTreeLimitsForEvalOrMatch( PredefinedDbPaths.FUNCTIONS_ROOT, localPath, options); - if (limitChecked !== true) return limitChecked; + if (limitChecked !== true) { + return limitChecked; + } return this.convertFunctionMatch( this.matchFunctionForParsedPath(localPath), isGlobal); } @@ -633,7 +635,9 @@ class DB { } const limitChecked = this.checkRespTreeLimitsForEvalOrMatch( PredefinedDbPaths.RULES_ROOT, localPath, options); - if (limitChecked !== true) return limitChecked; + if (limitChecked !== true) { + return limitChecked; + } const matched = this.matchRuleForParsedPath(localPath); return { write: this.convertRuleMatch(matched.write, isGlobal), @@ -651,7 +655,9 @@ class DB { } const limitChecked = this.checkRespTreeLimitsForEvalOrMatch( PredefinedDbPaths.OWNERS_ROOT, localPath, options); - if (limitChecked !== true) return limitChecked; + if (limitChecked !== true) { + return limitChecked; + } return this.convertOwnerMatch(this.matchOwnerForParsedPath(localPath), isGlobal); } @@ -665,7 +671,9 @@ class DB { } const limitChecked = this.checkRespTreeLimitsForEvalOrMatch( PredefinedDbPaths.RULES_ROOT, localPath, options); - if (limitChecked !== true) return limitChecked; + if (limitChecked !== true) { + return limitChecked; + } return this.getPermissionForValue(localPath, value, auth, options); } @@ -680,7 +688,9 @@ class DB { } const limitChecked = this.checkRespTreeLimitsForEvalOrMatch( PredefinedDbPaths.OWNERS_ROOT, localPath, options); - if (limitChecked !== true) return limitChecked; + if (limitChecked !== true) { + return limitChecked; + } if (permission === OwnerProperties.WRITE_RULE) { return this.getPermissionForRule(localPath, auth, options && options.isMerge); } else if (permission === OwnerProperties.WRITE_FUNCTION) { @@ -700,20 +710,26 @@ class DB { } // TODO(liayoo): Apply stricter limits to rule/function/owner state budgets - static checkRespTreeLimits(stateNode) { - if (stateNode.numChildren() > NodeConfigs.GET_RESP_MAX_SIBLINGS) { - return { - code: JsonRpcApiResultCode.GET_EXCEEDS_MAX_SIBLINGS, - message: `The data exceeds the max sibling limit of the requested node: ` + - `${stateNode.numChildren()} > ${NodeConfigs.GET_RESP_MAX_SIBLINGS}` - }; + static checkRespTreeLimits(stateNode, options) { + // NOTE: Skip sibling number limit check for isPartial = true cases. + if (!(options && options.isPartial)) { + if (stateNode.numChildren() > NodeConfigs.GET_RESP_MAX_SIBLINGS) { + return { + code: JsonRpcApiResultCode.GET_EXCEEDS_MAX_SIBLINGS, + message: `The data exceeds the max sibling limit of the requested node: ` + + `${stateNode.numChildren()} > ${NodeConfigs.GET_RESP_MAX_SIBLINGS}` + }; + } } - if (stateNode.getTreeBytes() > NodeConfigs.GET_RESP_BYTES_LIMIT) { - return { - code: JsonRpcApiResultCode.GET_EXCEEDS_MAX_BYTES, - message: `The data exceeds the max byte limit of the requested node: ` + - `${stateNode.getTreeBytes()} > ${NodeConfigs.GET_RESP_BYTES_LIMIT}` - }; + // NOTE: Skip bytes limit check for isShallow = true or isPartial = true cases. + if (!(options && (options.isShallow || options.isPartial))) { + if (stateNode.getTreeBytes() > NodeConfigs.GET_RESP_BYTES_LIMIT) { + return { + code: JsonRpcApiResultCode.GET_EXCEEDS_MAX_BYTES, + message: `The data exceeds the max byte limit of the requested node: ` + + `${stateNode.getTreeBytes()} > ${NodeConfigs.GET_RESP_BYTES_LIMIT}` + }; + } } return true; } @@ -724,7 +740,7 @@ class DB { const fullPath = DB.getFullPath(localPath, rootLabel); const stateNode = DB.getRefForReadingFromStateRoot(targetStateRoot, fullPath); if (stateNode !== null) { - const limitChecked = DB.checkRespTreeLimits(stateNode); + const limitChecked = DB.checkRespTreeLimits(stateNode, options); if (limitChecked !== true) { return limitChecked; } @@ -2270,11 +2286,11 @@ class DB { return funcs; } - // TODO(platfowner): Consider optimizing subtree function retrieval logic. getSubtreeFunctions(funcNode) { return this.getSubtreeFunctionsRecursive(0, funcNode); } + // TODO(platfowner): Consider optimizing subtree function retrieval for many children. matchFunctionForParsedPath(parsedValuePath) { const matched = this.matchFunctionPath(parsedValuePath); const subtreeFunctions = this.getSubtreeFunctions(matched.matchedFunctionNode); @@ -2418,11 +2434,11 @@ class DB { return rules; } - // TODO(platfowner): Consider optimizing subtree rule retrieval logic. getSubtreeRules(ruleNode, ruleProp) { return this.getSubtreeRulesRecursive(0, ruleNode, ruleProp); } + // TODO(platfowner): Consider optimizing subtree rule retrieval for many children. matchRuleForParsedPath(parsedValuePath) { const matchedWriteRule = this.matchRulePath(parsedValuePath, RuleProperties.WRITE); const matchedStateRule = this.matchRulePath(parsedValuePath, RuleProperties.STATE); @@ -2684,11 +2700,11 @@ class DB { return owners; } - // TODO(platfowner): Consider optimizing subtree owner retrieval logic. getSubtreeOwners(ownerNode) { return this.getSubtreeOwnersRecursive(0, ownerNode); } + // TODO(platfowner): Consider optimizing subtree owner retrieval for many children. matchOwnerForParsedPath(parsedRefPath) { const matched = this.matchOwnerPath(parsedRefPath); const subtreeOwners = matched.matchedOwnerNode ? diff --git a/test/integration/node.test.js b/test/integration/node.test.js index 3b3c8ff57..938c711d8 100644 --- a/test/integration/node.test.js +++ b/test/integration/node.test.js @@ -825,7 +825,7 @@ describe('Blockchain Node', () => { }); }); - describe('json-rpc: ain_get api', () => { + describe('json-rpc api: ain_get', () => { it('returns the correct value', () => { const expected = 100; const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); @@ -852,7 +852,7 @@ describe('Blockchain Node', () => { }); }); - it('returns error when requested data exceeds the get response limits (bytes)', async () => { + it('returns error when requested data exceeds the get response limits - bytes', async () => { const bigTree = {}; for (let i = 0; i < 10; i++) { bigTree[i] = {}; @@ -881,7 +881,35 @@ describe('Blockchain Node', () => { }); }); - it('returns error when requested data exceeds the get response limits (siblings)', async () => { + // the same as the previous test case but is_shallow option + it('returns a correct value with is_shallow option', async () => { + const bigTree = {}; + for (let i = 0; i < 10; i++) { + bigTree[i] = {}; + for (let j = 0; j < 1000; j++) { + bigTree[i][j] = 'a'; + } + } + const body = parseOrLog(syncRequest('POST', server1 + '/set_value', {json: { + ref: '/apps/test/test_value/some/path', + value: bigTree, // 77820 bytes (using object-sizeof) + }}).body.toString('utf-8')); + if (!(await waitUntilTxFinalized(serverList, _.get(body, 'result.tx_hash')))) { + console.error(`Failed to check finalization of tx.`); + } + const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET, { + protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, + type: 'GET_VALUE', + ref: "/apps/test/test_value/some/path", + is_shallow: true // w/ is_shallow option + }) + .then(res => { + expect(res.result.result.code).to.equal(undefined); + }); + }); + + it('returns error when requested data exceeds the get response limits - siblings', async () => { const wideTree = {}; for (let i = 0; i < 1000; i++) { wideTree[i] = 'a'; @@ -906,9 +934,34 @@ describe('Blockchain Node', () => { .includes('The data exceeds the max sibling limit of the requested node'), true); }); }); + + // the same as the previous test case but is_partial option + it('returns a correct value with is_partial option', async () => { + const wideTree = {}; + for (let i = 0; i < 1000; i++) { + wideTree[i] = 'a'; + } + const body = parseOrLog(syncRequest('POST', server1 + '/set_value', {json: { + ref: '/apps/test/test_value/some/path', + value: wideTree, // 1000 siblings + }}).body.toString('utf-8')); + if (!(await waitUntilTxFinalized(serverList, _.get(body, 'result.tx_hash')))) { + console.error(`Failed to check finalization of tx.`); + } + const jsonRpcClient = jayson.client.http(server2 + '/json-rpc'); + return jsonRpcClient.request(JSON_RPC_METHODS.AIN_GET, { + protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, + type: 'GET_VALUE', + ref: "/apps/test/test_value/some/path", + is_partial: true // w/ is_partial option + }) + .then(res => { + expect(res.result.result['#end_label']).to.equal('393938'); + }); + }); }); - describe('json-rpc: ain_matchFunction api', () => { + describe('json-rpc api: ain_matchFunction', () => { it('returns correct value', () => { const ref = "/apps/test/test_function/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; @@ -936,7 +989,7 @@ describe('Blockchain Node', () => { }) }) - describe('json-rpc: ain_matchRule api', () => { + describe('json-rpc api: ain_matchRule', () => { it('returns correct value (write)', () => { const ref = "/apps/test/test_rule/some/path"; const request = { ref, protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION }; From da411352cd1a28a9faa3e25de589cdc08116f578 Mon Sep 17 00:00:00 2001 From: kriii Date: Fri, 27 May 2022 23:19:43 +0900 Subject: [PATCH 074/166] Fix integration tests --- test/unit/db.test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/unit/db.test.js b/test/unit/db.test.js index cf1e19b56..c13487af9 100644 --- a/test/unit/db.test.js +++ b/test/unit/db.test.js @@ -6303,7 +6303,7 @@ describe("Util methods", () => { const appName = 'app/path'; assert.deepEqual(node.db.validateAppName(appName, 2, stateLabelLengthLimit), { "is_valid": false, - "code": 30701, + "code": 30601, "message": `Invalid app name for state label: ${appName}`, }); }); @@ -6315,7 +6315,7 @@ describe("Util methods", () => { } assert.deepEqual(node.db.validateAppName(appName, 2, stateLabelLengthLimit), { "is_valid": false, - "code": 30701, + "code": 30601, "message": `Invalid app name for state label: ${appName}`, }); }); @@ -6324,7 +6324,7 @@ describe("Util methods", () => { const appName = 'balance_total_sum'; assert.deepEqual(node.db.validateAppName(appName, 2, stateLabelLengthLimit), { "is_valid": false, - "code": 30702, + "code": 30602, "message": `Invalid app name for service name: ${appName}`, }); }); @@ -6333,21 +6333,21 @@ describe("Util methods", () => { const appName = 'appName'; assert.deepEqual(node.db.validateAppName(appName, 2, stateLabelLengthLimit), { "is_valid": false, - "code": 30702, + "code": 30602, "message": `Invalid app name for service name: ${appName}`, }); const appName2 = 'app-name'; assert.deepEqual(node.db.validateAppName(appName2, 2, stateLabelLengthLimit), { "is_valid": false, - "code": 30702, + "code": 30602, "message": `Invalid app name for service name: ${appName2}`, }); const appName3 = '0app'; assert.deepEqual(node.db.validateAppName(appName3, 2, stateLabelLengthLimit), { "is_valid": false, - "code": 30702, + "code": 30602, "message": `Invalid app name for service name: ${appName3}`, }); }); @@ -6355,7 +6355,7 @@ describe("Util methods", () => { it("app name in use", () => { assert.deepEqual(node.db.validateAppName(appNameInUse, 2, stateLabelLengthLimit), { "is_valid": false, - "code": 30703, + "code": 30603, "message": `App name already in use: ${appNameInUse}`, }); }); From ce3348664e4844c94f795a45b591a92be49a9b38 Mon Sep 17 00:00:00 2001 From: kriii Date: Fri, 27 May 2022 23:20:01 +0900 Subject: [PATCH 075/166] Fix json prc set method set --- json_rpc/constants.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/json_rpc/constants.js b/json_rpc/constants.js index f3bc541c4..73e8fd99b 100644 --- a/json_rpc/constants.js +++ b/json_rpc/constants.js @@ -58,13 +58,14 @@ const JSON_RPC_METHODS = { } const JSON_RPC_SET_METHOD_SET = new Set([ - JSON_RPC_METHODS.AIN_ADD_TO_DEV_CLIENT_API_IP_WHITELIST, + JSON_RPC_METHODS.AIN_ADD_TO_WHITELIST_NODE_PARAM, JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_HD_WALLET, JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_KEYSTORE, JSON_RPC_METHODS.AIN_INJECT_ACCOUNT_FROM_PRIVATE_KEY, - JSON_RPC_METHODS.AIN_REMOVE_FROM_DEV_CLIENT_API_IP_WHITELIST, + JSON_RPC_METHODS.AIN_REMOVE_FROM_WHITELIST_NODE_PARAM, JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION, - JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH + JSON_RPC_METHODS.AIN_SEND_SIGNED_TRANSACTION_BATCH, + JSON_RPC_METHODS.AIN_SET_NODE_PARAM ]); module.exports = { From 9cea149f5faeed7619fb1a1c0c240ec37e4cdfcb Mon Sep 17 00:00:00 2001 From: kriii Date: Mon, 30 May 2022 10:19:25 +0900 Subject: [PATCH 076/166] Fix results --- common/result-code.js | 9 +++---- json_rpc/admin.js | 55 +++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/common/result-code.js b/common/result-code.js index f1606edf5..b79639312 100644 --- a/common/result-code.js +++ b/common/result-code.js @@ -174,10 +174,11 @@ const JsonRpcApiResultCode = { BATCH_TX_INVALID_FORMAT: 30405, BATCH_TX_INVALID_SIGNATURE: 30406, // Admin APIs - PARAM_INVALID: 30501, - VALUE_INCOMPATIBLE: 30502, - ALREADY_IN_WHITELIST: 30503, - NOT_IN_WHITELIST: 30504, + ADMIN_FORBIDDEN_REQUEST: 30501, + ADMIN_PARAM_INVALID: 30502, + ADMIN_VALUE_INCOMPATIBLE: 30503, + ADMIN_ALREADY_IN_WHITELIST: 30504, + ADMIN_NOT_IN_WHITELIST: 30505, // ain_validateAppName INVALID_APP_NAME_FOR_STATE_LABEL: 30601, INVALID_APP_NAME_FOR_SERVICE_NAME: 30602, diff --git a/json_rpc/admin.js b/json_rpc/admin.js index 325492434..c220a82c9 100644 --- a/json_rpc/admin.js +++ b/json_rpc/admin.js @@ -29,19 +29,24 @@ module.exports = function getAdminApis(node) { const verified = node.verifyNodeAccountSignature(args.message, args.signature); if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_GET_NODE_PARAM || !verified) { const latency = Date.now() - beginTime; - trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); - done({ code: 403, message: 'Forbidden' }); + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_GET, latency); + done(null, JsonRpcUtil.addProtocolVersion({ + result: { + code: JsonRpcApiResultCode.ADMIN_FORBIDDEN_REQUEST, + message: `Forbidden request.` + } + })); return; } const param = args.message.param; const latency = Date.now() - beginTime; - trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_GET, latency); if (NodeConfigs[param] === undefined) { done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.PARAM_INVALID, - message: `Param [${param}] is not exist.` + code: JsonRpcApiResultCode.ADMIN_PARAM_INVALID, + message: `Param [${param}] does not exist.` } })); } else { @@ -55,7 +60,12 @@ module.exports = function getAdminApis(node) { if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_SET_NODE_PARAM || !verified) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); - done({ code: 403, message: 'Forbidden' }); + done(null, JsonRpcUtil.addProtocolVersion({ + result: { + code: JsonRpcApiResultCode.ADMIN_FORBIDDEN_REQUEST, + message: `Forbidden request.` + } + })); return; } @@ -65,8 +75,8 @@ module.exports = function getAdminApis(node) { trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.PARAM_INVALID, - message: `Param [${param}] is not exists.` + code: JsonRpcApiResultCode.ADMIN_PARAM_INVALID, + message: `Param [${param}] does not exists.` } })); return; @@ -77,7 +87,7 @@ module.exports = function getAdminApis(node) { trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.VALUE_INCOMPATIBLE, + code: JsonRpcApiResultCode.ADMIN_VALUE_INCOMPATIBLE, message: `(${args.message.value}) is incompatible with param [${param}]: ${JSON.stringify(NodeConfigs[param])}` } })); @@ -101,7 +111,12 @@ module.exports = function getAdminApis(node) { if (_.get(args.message, 'method') !== JSON_RPC_METHODS.AIN_ADD_TO_WHITELIST_NODE_PARAM || !verified) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); - done({ code: 403, message: 'Forbidden' }); + done(null, JsonRpcUtil.addProtocolVersion({ + result: { + code: JsonRpcApiResultCode.ADMIN_FORBIDDEN_REQUEST, + message: `Forbidden request.` + } + })); return; } @@ -111,8 +126,8 @@ module.exports = function getAdminApis(node) { trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.PARAM_INVALID, - message: `Param [${param}] is not exists.` + code: JsonRpcApiResultCode.ADMIN_PARAM_INVALID, + message: `Param [${param}] does not exists.` } })); return; @@ -123,8 +138,8 @@ module.exports = function getAdminApis(node) { trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.PARAM_INVALID, - message: `Param [${param}] is not whitelist` + code: JsonRpcApiResultCode.ADMIN_PARAM_INVALID, + message: `Param [${param}] is not a whitelist` } })); return; @@ -139,7 +154,7 @@ module.exports = function getAdminApis(node) { trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.ALREADY_IN_WHITELIST, + code: JsonRpcApiResultCode.ADMIN_ALREADY_IN_WHITELIST, message: `(${args.message.value}) already in whitelist [${param}]: ${JSON.stringify(NodeConfigs[param])}` } })); @@ -177,8 +192,8 @@ module.exports = function getAdminApis(node) { trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.PARAM_INVALID, - message: `Param [${param}] is not exists.` + code: JsonRpcApiResultCode.ADMIN_PARAM_INVALID, + message: `Param [${param}] does not exists.` } })); return; @@ -189,8 +204,8 @@ module.exports = function getAdminApis(node) { trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.PARAM_INVALID, - message: `Param [${param}] is not whitelist` + code: JsonRpcApiResultCode.ADMIN_PARAM_INVALID, + message: `Param [${param}] is not a whitelist` } })); return; @@ -204,7 +219,7 @@ module.exports = function getAdminApis(node) { trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.NOT_IN_WHITELIST, + code: JsonRpcApiResultCode.ADMIN_NOT_IN_WHITELIST, message: `(${args.message.value}) not in whitelist [${param}]: ${JSON.stringify(NodeConfigs[param])}` } })); From a1611e674c857d43dcc0da94f8dbbab18f7cadd9 Mon Sep 17 00:00:00 2001 From: kriii Date: Mon, 30 May 2022 10:38:19 +0900 Subject: [PATCH 077/166] Add wildcard example for tools --- tools/api-access/addToDevClientApiIpWhitelist.js | 1 + tools/api-access/getDevClientApiIpWhitelist.js | 1 + tools/api-access/removeFromDevClientApiIpWhitelist.js | 1 + 3 files changed, 3 insertions(+) diff --git a/tools/api-access/addToDevClientApiIpWhitelist.js b/tools/api-access/addToDevClientApiIpWhitelist.js index f75a2ee8b..f2e64e7a3 100644 --- a/tools/api-access/addToDevClientApiIpWhitelist.js +++ b/tools/api-access/addToDevClientApiIpWhitelist.js @@ -65,6 +65,7 @@ function usage() { console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 private_key 127.0.0.1'); console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 mnemonic 127.0.0.1'); console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file 127.0.0.1'); + console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file "*"'); process.exit(0); } diff --git a/tools/api-access/getDevClientApiIpWhitelist.js b/tools/api-access/getDevClientApiIpWhitelist.js index 7fb66a2f5..afcf1589b 100644 --- a/tools/api-access/getDevClientApiIpWhitelist.js +++ b/tools/api-access/getDevClientApiIpWhitelist.js @@ -53,6 +53,7 @@ function usage() { console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 private_key'); console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 mnemonic'); console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file'); + console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 keystore "*"'); process.exit(0); } diff --git a/tools/api-access/removeFromDevClientApiIpWhitelist.js b/tools/api-access/removeFromDevClientApiIpWhitelist.js index d974eb6e0..e23252f12 100644 --- a/tools/api-access/removeFromDevClientApiIpWhitelist.js +++ b/tools/api-access/removeFromDevClientApiIpWhitelist.js @@ -65,6 +65,7 @@ function usage() { console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 private_key 127.0.0.1'); console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 mnemonic 127.0.0.1'); console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file 127.0.0.1'); + console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file "*"'); process.exit(0); } From bb9dad0531b0cc69ba4c882b4fa24fe0a5e16d0d Mon Sep 17 00:00:00 2001 From: kriii Date: Tue, 31 May 2022 02:02:47 +0900 Subject: [PATCH 078/166] Change set methods value handling as like evironment variable-ish --- common/result-code.js | 2 +- json_rpc/admin.js | 49 +++++++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/common/result-code.js b/common/result-code.js index b79639312..f2cad530b 100644 --- a/common/result-code.js +++ b/common/result-code.js @@ -176,7 +176,7 @@ const JsonRpcApiResultCode = { // Admin APIs ADMIN_FORBIDDEN_REQUEST: 30501, ADMIN_PARAM_INVALID: 30502, - ADMIN_VALUE_INCOMPATIBLE: 30503, + ADMIN_VALUE_NOT_A_STRING_TYPE: 30503, ADMIN_ALREADY_IN_WHITELIST: 30504, ADMIN_NOT_IN_WHITELIST: 30505, // ain_validateAppName diff --git a/json_rpc/admin.js b/json_rpc/admin.js index c220a82c9..90aa7f14a 100644 --- a/json_rpc/admin.js +++ b/json_rpc/admin.js @@ -9,16 +9,15 @@ const CommonUtil = require('../common/common-util'); const JsonRpcUtil = require('./json-rpc-util'); const { JSON_RPC_METHODS } = require('./constants'); -function checkCompatibility(valueA, valueB) { - if (CommonUtil.isBool(valueA)) { - return CommonUtil.isBool(valueB); - } else if (CommonUtil.isIntegerString(valueA) || CommonUtil.isFloatString(valueA)) { - return CommonUtil.isIntegerString(valueB) || CommonUtil.isFloatString(valueB); - } else if (CommonUtil.isArray(valueA) || CommonUtil.isWildcard(valueA)) { - return CommonUtil.isArray(valueB) || CommonUtil.isWildcard(valueB); +function convertValue(valueFromNodeParam, value) { + if (CommonUtil.isBool(valueFromNodeParam)) { + return CommonUtil.convertEnvVarInputToBool(value); + } else if (CommonUtil.isIntegerString(valueFromNodeParam) || CommonUtil.isFloatString(valueFromNodeParam)) { + return Number(value); + } else if (CommonUtil.isArray(valueFromNodeParam) || CommonUtil.isWildcard(valueFromNodeParam)) { + return CommonUtil.getWhitelistFromString(value); } else { - // TODO(kriii): Decide how to work on the object (e.g. TRAFFIC_STATS_PERIOD_SECS_LIST). - return false; + return value; } } @@ -82,19 +81,19 @@ module.exports = function getAdminApis(node) { return; } - if (!checkCompatibility(NodeConfigs[param], args.message.value)) { + if (!CommonUtil.isString(args.message.value)) { const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ result: { - code: JsonRpcApiResultCode.ADMIN_VALUE_INCOMPATIBLE, - message: `(${args.message.value}) is incompatible with param [${param}]: ${JSON.stringify(NodeConfigs[param])}` + code: JsonRpcApiResultCode.ADMIN_VALUE_NOT_A_STRING_TYPE, + message: `(${args.message.value}) is not a string type.)}` } })); return; } - NodeConfigs[param] = args.message.value; + NodeConfigs[param] = convertValue(NodeConfigs[param], args.message.value); const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ @@ -145,6 +144,18 @@ module.exports = function getAdminApis(node) { return; } + if (!CommonUtil.isString(args.message.value)) { + const latency = Date.now() - beginTime; + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); + done(null, JsonRpcUtil.addProtocolVersion({ + result: { + code: JsonRpcApiResultCode.ADMIN_VALUE_NOT_A_STRING_TYPE, + message: `(${args.message.value}) is not a string type.)}` + } + })); + return; + } + if (!CommonUtil.isArray(NodeConfigs[param])) { // NOTE(liayoo): if the whitelist was "*" previously, adding an IP will no longer "allow-all". NodeConfigs[param] = []; @@ -211,6 +222,18 @@ module.exports = function getAdminApis(node) { return; } + if (!CommonUtil.isString(args.message.value)) { + const latency = Date.now() - beginTime; + trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); + done(null, JsonRpcUtil.addProtocolVersion({ + result: { + code: JsonRpcApiResultCode.ADMIN_VALUE_NOT_A_STRING_TYPE, + message: `(${args.message.value}) is not a string type.)}` + } + })); + return; + } + if (CommonUtil.isWildcard(args.message.value)) { NodeConfigs[param] = []; } else if (!CommonUtil.isArray(NodeConfigs[param]) || From 5d140ef9729c29970d487d26e5583c728abe5507 Mon Sep 17 00:00:00 2001 From: kriii Date: Tue, 31 May 2022 02:03:10 +0900 Subject: [PATCH 079/166] Fix tool usages --- tools/api-access/addToDevClientApiIpWhitelist.js | 1 - tools/api-access/getDevClientApiIpWhitelist.js | 1 - tools/api-access/removeFromDevClientApiIpWhitelist.js | 1 - 3 files changed, 3 deletions(-) diff --git a/tools/api-access/addToDevClientApiIpWhitelist.js b/tools/api-access/addToDevClientApiIpWhitelist.js index f2e64e7a3..281e1a9cd 100644 --- a/tools/api-access/addToDevClientApiIpWhitelist.js +++ b/tools/api-access/addToDevClientApiIpWhitelist.js @@ -64,7 +64,6 @@ function usage() { console.log('\nExamples:'); console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 private_key 127.0.0.1'); console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 mnemonic 127.0.0.1'); - console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file 127.0.0.1'); console.log('node tools/api-access/addToDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file "*"'); process.exit(0); } diff --git a/tools/api-access/getDevClientApiIpWhitelist.js b/tools/api-access/getDevClientApiIpWhitelist.js index afcf1589b..7fb66a2f5 100644 --- a/tools/api-access/getDevClientApiIpWhitelist.js +++ b/tools/api-access/getDevClientApiIpWhitelist.js @@ -53,7 +53,6 @@ function usage() { console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 private_key'); console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 mnemonic'); console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file'); - console.log('node tools/api-access/getDevClientApiIpWhitelist.js http://localhost:8081 0 keystore "*"'); process.exit(0); } diff --git a/tools/api-access/removeFromDevClientApiIpWhitelist.js b/tools/api-access/removeFromDevClientApiIpWhitelist.js index e23252f12..275c34b74 100644 --- a/tools/api-access/removeFromDevClientApiIpWhitelist.js +++ b/tools/api-access/removeFromDevClientApiIpWhitelist.js @@ -64,7 +64,6 @@ function usage() { console.log('\nExamples:'); console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 private_key 127.0.0.1'); console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 mnemonic 127.0.0.1'); - console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file 127.0.0.1'); console.log('node tools/api-access/removeFromDevClientApiIpWhitelist.js http://localhost:8081 0 keystore /path/to/keystore/file "*"'); process.exit(0); } From b02b08de1af9dd453e402c112018a42a7eea20fb Mon Sep 17 00:00:00 2001 From: kriii Date: Tue, 31 May 2022 02:03:28 +0900 Subject: [PATCH 080/166] Add tools for new APIs --- tools/api-access/addToWhiteListNodeParam.js | 74 +++++++++++++++++++ tools/api-access/getNodeParam.js | 66 +++++++++++++++++ .../removeFromWhitelistNodeParam.js | 74 +++++++++++++++++++ tools/api-access/setNodeParam.js | 74 +++++++++++++++++++ 4 files changed, 288 insertions(+) create mode 100644 tools/api-access/addToWhiteListNodeParam.js create mode 100644 tools/api-access/getNodeParam.js create mode 100644 tools/api-access/removeFromWhitelistNodeParam.js create mode 100644 tools/api-access/setNodeParam.js diff --git a/tools/api-access/addToWhiteListNodeParam.js b/tools/api-access/addToWhiteListNodeParam.js new file mode 100644 index 000000000..0a824d64c --- /dev/null +++ b/tools/api-access/addToWhiteListNodeParam.js @@ -0,0 +1,74 @@ +const axios = require('axios'); +const _ = require('lodash'); +const ainUtil = require('@ainblockchain/ain-util'); +const stringify = require('fast-json-stable-stringify'); +const { BlockchainConsts } = require('../../common/constants'); +const { getAccountPrivateKey } = require('./util'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); + +async function sendAddToWhiteListNodeParamRequest(endpointUrl, privateKey, chainId, param, value) { + const message = { + timestamp: Date.now(), + method: JSON_RPC_METHODS.AIN_ADD_TO_WHITELIST_NODE_PARAM, + param, + value, + }; + const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); + return await axios.post( + `${endpointUrl}/json-rpc`, + { + method: JSON_RPC_METHODS.AIN_ADD_TO_WHITELIST_NODE_PARAM, + params: { + protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, + message, + signature, + }, + jsonrpc: '2.0', + id: 0 + } + ).then(function(resp) { + return _.get(resp, 'data.result.result'); + }); +} + +async function addToWhiteListNodeParam(endpointUrl, chainId, type, keystoreFilePath, param, value) { + const privateKey = await getAccountPrivateKey(type, keystoreFilePath); + const res = await sendAddToWhiteListNodeParamRequest(endpointUrl, privateKey, chainId, param, value); + console.log('Result:', res); +} + +async function processArguments() { + if (process.argv.length !== 7 && process.argv.length !== 8) { + usage(); + } + const endpointUrl = process.argv[2]; + const chainId = Number(process.argv[3]); + const accountType = process.argv[4]; + let keystoreFilePath = null; + let param = null; + let value = null; + if (accountType === 'keystore') { + keystoreFilePath = process.argv[5]; + param = process.argv[6]; + value = process.argv[7]; + } else { + param = process.argv[5]; + value = process.argv[6]; + } + if (!value) { + console.error('Please specify a value'); + usage(); + } + await addToWhiteListNodeParam(endpointUrl, chainId, accountType, keystoreFilePath, param, value); +} + +function usage() { + console.log('\nUsage:\n node addToWhiteListNodeParam.js [] \n'); + console.log('\nExamples:'); + console.log('node tools/api-access/addToWhiteListNodeParam.js http://localhost:8081 0 private_key DEV_CLIENT_API_IP_WHITELIST 127.0.0.1'); + console.log('node tools/api-access/addToWhiteListNodeParam.js http://localhost:8081 0 mnemonic DEV_CLIENT_API_IP_WHITELIST "*"'); + console.log('node tools/api-access/addToWhiteListNodeParam.js http://localhost:8081 0 keystore /path/to/kezystore/file CORS_WHITELIST "https://ainetwork\\.ai"'); + process.exit(0); +} + +processArguments(); diff --git a/tools/api-access/getNodeParam.js b/tools/api-access/getNodeParam.js new file mode 100644 index 000000000..44e026ac2 --- /dev/null +++ b/tools/api-access/getNodeParam.js @@ -0,0 +1,66 @@ +const axios = require('axios'); +const _ = require('lodash'); +const ainUtil = require('@ainblockchain/ain-util'); +const stringify = require('fast-json-stable-stringify'); +const { BlockchainConsts } = require('../../common/constants'); +const { getAccountPrivateKey } = require('./util'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); + +async function sendGetNodeParamRequest(endpointUrl, privateKey, chainId, param) { + const message = { + timestamp: Date.now(), + method: JSON_RPC_METHODS.AIN_GET_NODE_PARAM, + param, + }; + const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); + return await axios.post( + `${endpointUrl}/json-rpc`, + { + method: JSON_RPC_METHODS.AIN_GET_NODE_PARAM, + params: { + protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, + message, + signature, + }, + jsonrpc: '2.0', + id: 0 + } + ).then(function(resp) { + return _.get(resp, 'data.result.result'); + }); +} + +async function getNodeParam(endpointUrl, chainId, type, keystoreFilePath, param) { + const privateKey = await getAccountPrivateKey(type, keystoreFilePath); + const res = await sendGetNodeParamRequest(endpointUrl, privateKey, chainId, param); + console.log('Result:', res); +} + +async function processArguments() { + if (process.argv.length !== 6 && process.argv.length !== 7) { + usage(); + } + const endpointUrl = process.argv[2]; + const chainId = Number(process.argv[3]); + const accountType = process.argv[4]; + let keystoreFilePath = null; + let param = null; + if (accountType === 'keystore') { + keystoreFilePath = process.argv[5]; + param = process.argv[6]; + } else { + param = process.argv[5]; + } + await getNodeParam(endpointUrl, chainId, accountType, keystoreFilePath, param); +} + +function usage() { + console.log('\nUsage:\n node getNodeParam.js [] \n'); + console.log('\nExamples:'); + console.log('node tools/api-access/getNodeParam.js http://localhost:8081 0 private_key DEV_CLIENT_API_IP_WHITELIST'); + console.log('node tools/api-access/getNodeParam.js http://localhost:8081 0 mnemonic P2P_MESSAGE_TIMEOUT_MS'); + console.log('node tools/api-access/getNodeParam.js http://localhost:8081 0 keystore /path/to/keystore/file SYNC_MODE'); + process.exit(0); +} + +processArguments(); diff --git a/tools/api-access/removeFromWhitelistNodeParam.js b/tools/api-access/removeFromWhitelistNodeParam.js new file mode 100644 index 000000000..042a5f9dc --- /dev/null +++ b/tools/api-access/removeFromWhitelistNodeParam.js @@ -0,0 +1,74 @@ +const axios = require('axios'); +const _ = require('lodash'); +const ainUtil = require('@ainblockchain/ain-util'); +const stringify = require('fast-json-stable-stringify'); +const { BlockchainConsts } = require('../../common/constants'); +const { getAccountPrivateKey } = require('./util'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); + +async function sendRemoveFromWhiteListNodeParamRequest(endpointUrl, privateKey, chainId, param, value) { + const message = { + timestamp: Date.now(), + method: JSON_RPC_METHODS.AIN_REMOVE_FROM_WHITELIST_NODE_PARAM, + param, + value, + }; + const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); + return await axios.post( + `${endpointUrl}/json-rpc`, + { + method: JSON_RPC_METHODS.AIN_REMOVE_FROM_WHITELIST_NODE_PARAM, + params: { + protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, + message, + signature, + }, + jsonrpc: '2.0', + id: 0 + } + ).then(function(resp) { + return _.get(resp, 'data.result.result'); + }); +} + +async function removeFromWhiteListNodeParam(endpointUrl, chainId, type, keystoreFilePath, param, value) { + const privateKey = await getAccountPrivateKey(type, keystoreFilePath); + const res = await sendRemoveFromWhiteListNodeParamRequest(endpointUrl, privateKey, chainId, param, value); + console.log('Result:', res); +} + +async function processArguments() { + if (process.argv.length !== 7 && process.argv.length !== 8) { + usage(); + } + const endpointUrl = process.argv[2]; + const chainId = Number(process.argv[3]); + const accountType = process.argv[4]; + let keystoreFilePath = null; + let param = null; + let value = null; + if (accountType === 'keystore') { + keystoreFilePath = process.argv[5]; + param = process.argv[6]; + value = process.argv[7]; + } else { + param = process.argv[5]; + value = process.argv[6]; + } + if (!value) { + console.error('Please specify a value'); + usage(); + } + await removeFromWhiteListNodeParam(endpointUrl, chainId, accountType, keystoreFilePath, param, value); +} + +function usage() { + console.log('\nUsage:\n node removeFromWhiteListNodeParam.js [] \n'); + console.log('\nExamples:'); + console.log('node tools/api-access/removeFromWhiteListNodeParam.js http://localhost:8081 0 private_key DEV_CLIENT_API_IP_WHITELIST 127.0.0.1'); + console.log('node tools/api-access/removeFromWhiteListNodeParam.js http://localhost:8081 0 mnemonic DEV_CLIENT_API_IP_WHITELIST "*"'); + console.log('node tools/api-access/removeFromWhiteListNodeParam.js http://localhost:8081 0 keystore /path/to/kezystore/file CORS_WHITELIST "https://ainetwork\\.ai"'); + process.exit(0); +} + +processArguments(); diff --git a/tools/api-access/setNodeParam.js b/tools/api-access/setNodeParam.js new file mode 100644 index 000000000..5424a5b47 --- /dev/null +++ b/tools/api-access/setNodeParam.js @@ -0,0 +1,74 @@ +const axios = require('axios'); +const _ = require('lodash'); +const ainUtil = require('@ainblockchain/ain-util'); +const stringify = require('fast-json-stable-stringify'); +const { BlockchainConsts } = require('../../common/constants'); +const { getAccountPrivateKey } = require('./util'); +const { JSON_RPC_METHODS } = require('../../json_rpc/constants'); + +async function sendSetNodeParamRequest(endpointUrl, privateKey, chainId, param, value) { + const message = { + timestamp: Date.now(), + method: JSON_RPC_METHODS.AIN_REMOVE_FROM_WHITELIST_NODE_PARAM, + param, + value, + }; + const signature = ainUtil.ecSignMessage(stringify(message), Buffer.from(privateKey, 'hex'), chainId); + return await axios.post( + `${endpointUrl}/json-rpc`, + { + method: JSON_RPC_METHODS.AIN_REMOVE_FROM_WHITELIST_NODE_PARAM, + params: { + protoVer: BlockchainConsts.CURRENT_PROTOCOL_VERSION, + message, + signature, + }, + jsonrpc: '2.0', + id: 0 + } + ).then(function(resp) { + return _.get(resp, 'data.result.result'); + }); +} + +async function setNodeParam(endpointUrl, chainId, type, keystoreFilePath, param, value) { + const privateKey = await getAccountPrivateKey(type, keystoreFilePath); + const res = await sendSetNodeParamRequest(endpointUrl, privateKey, chainId, param, value); + console.log('Result:', res); +} + +async function processArguments() { + if (process.argv.length !== 7 && process.argv.length !== 8) { + usage(); + } + const endpointUrl = process.argv[2]; + const chainId = Number(process.argv[3]); + const accountType = process.argv[4]; + let keystoreFilePath = null; + let param = null; + let value = null; + if (accountType === 'keystore') { + keystoreFilePath = process.argv[5]; + param = process.argv[6]; + value = process.argv[7]; + } else { + param = process.argv[5]; + value = process.argv[6]; + } + if (!value) { + console.error('Please specify a value'); + usage(); + } + await setNodeParam(endpointUrl, chainId, accountType, keystoreFilePath, param, value); +} + +function usage() { + console.log('\nUsage:\n node setNodeParam.js [] \n'); + console.log('\nExamples:'); + console.log('node tools/api-access/setNodeParam.js http://localhost:8081 0 private_key DEV_CLIENT_API_IP_WHITELIST "*"'); + console.log('node tools/api-access/setNodeParam.js http://localhost:8081 0 mnemonic P2P_MESSAGE_TIMEOUT_MS 200000'); + console.log('node tools/api-access/setNodeParam.js http://localhost:8081 0 keystore /path/to/kezystore/file SYNC_MODE peer'); + process.exit(0); +} + +processArguments(); From 43434cdcc7ed5c98c280051da7ddeadeba4b590c Mon Sep 17 00:00:00 2001 From: kriii Date: Tue, 31 May 2022 11:38:22 +0900 Subject: [PATCH 081/166] Fix typo --- json_rpc/admin.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/json_rpc/admin.js b/json_rpc/admin.js index 90aa7f14a..b1636fcb6 100644 --- a/json_rpc/admin.js +++ b/json_rpc/admin.js @@ -75,7 +75,7 @@ module.exports = function getAdminApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: { code: JsonRpcApiResultCode.ADMIN_PARAM_INVALID, - message: `Param [${param}] does not exists.` + message: `Param [${param}] does not exist.` } })); return; @@ -126,7 +126,7 @@ module.exports = function getAdminApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: { code: JsonRpcApiResultCode.ADMIN_PARAM_INVALID, - message: `Param [${param}] does not exists.` + message: `Param [${param}] does not exist.` } })); return; @@ -204,7 +204,7 @@ module.exports = function getAdminApis(node) { done(null, JsonRpcUtil.addProtocolVersion({ result: { code: JsonRpcApiResultCode.ADMIN_PARAM_INVALID, - message: `Param [${param}] does not exists.` + message: `Param [${param}] does not exist.` } })); return; From cea30fd8874e1189124dcf546c2eb37a89fa6664 Mon Sep 17 00:00:00 2001 From: kriii Date: Tue, 31 May 2022 12:38:45 +0900 Subject: [PATCH 082/166] Add TODO --- json_rpc/admin.js | 1 + 1 file changed, 1 insertion(+) diff --git a/json_rpc/admin.js b/json_rpc/admin.js index b1636fcb6..716ac5907 100644 --- a/json_rpc/admin.js +++ b/json_rpc/admin.js @@ -94,6 +94,7 @@ module.exports = function getAdminApis(node) { } NodeConfigs[param] = convertValue(NodeConfigs[param], args.message.value); + // TODO(kriii): Add a refresher for some params. const latency = Date.now() - beginTime; trafficStatsManager.addEvent(TrafficEventTypes.ACCESS_CONTROL_SET, latency); done(null, JsonRpcUtil.addProtocolVersion({ From 903cdaccbb992c4220e97caee6745251545ddbb4 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Tue, 31 May 2022 16:22:06 +0900 Subject: [PATCH 083/166] Add refresh button on topology --- tracker-server/index.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tracker-server/index.html b/tracker-server/index.html index 8feb4343d..b3e25d7f7 100644 --- a/tracker-server/index.html +++ b/tracker-server/index.html @@ -5,6 +5,12 @@ +
+ +
+
+ +
- +
- \ No newline at end of file From a86849b50d228e5b50d288392aadc85d48a892e0 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Fri, 3 Jun 2022 11:52:08 +0900 Subject: [PATCH 087/166] Pass data from server to html fancier --- tracker-server/index.html | 8 +++++++- tracker-server/index.js | 7 +++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tracker-server/index.html b/tracker-server/index.html index c07385d33..ce960b5e0 100644 --- a/tracker-server/index.html +++ b/tracker-server/index.html @@ -82,7 +82,13 @@ }); } - // draw({ /* replace this */ }); + function sanitizeInitData() { + return initData.replace(/"/g, '"'); + } + + const initData = "<%= data %>"; + const sanitized = sanitizeInitData(initData); + draw(JSON.parse(sanitized)); document.getElementById('json-file').addEventListener('change', () => { const jsonFile = document.getElementById("json-file").files[0]; diff --git a/tracker-server/index.js b/tracker-server/index.js index 06d551990..06adac533 100755 --- a/tracker-server/index.js +++ b/tracker-server/index.js @@ -57,10 +57,9 @@ app.get('/network_status', (req, res, next) => { }); app.get('/network_topology', (req, res) => { - res.render(__dirname + '/index.html', {}, (err, html) => { - const networkStatus = tracker.getNetworkStatus(); - const graphData = getGraphData(networkStatus); - html = html.replace(/{ \/\* replace this \*\/ };/g, JSON.stringify(graphData)); + const networkStatus = tracker.getNetworkStatus(); + const graphData = getGraphData(networkStatus); + res.render(__dirname + '/index.html', { data: JSON.stringify(graphData) }, (err, html) => { res.send(html); }); }); From 54f63a81bdafea228ef121051470455d0f533379 Mon Sep 17 00:00:00 2001 From: minsulee2 Date: Fri, 3 Jun 2022 12:42:10 +0900 Subject: [PATCH 088/166] Add example json file --- tracker-server/example.json | 49 +++++++++++++++++++++++++++++++++++++ tracker-server/index.html | 3 --- 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 tracker-server/example.json diff --git a/tracker-server/example.json b/tracker-server/example.json new file mode 100644 index 000000000..3ddc0eb2f --- /dev/null +++ b/tracker-server/example.json @@ -0,0 +1,49 @@ +{ + "nodes": [ + { + "address": "t" + }, + { + "address": "e" + }, + { + "address": "s" + }, + { + "address": "t" + }, + { + "address": "foo" + }, + { + "address": "bar" + } + ], + "links": [ + { + "source": 0, + "target": 1, + "weight": 1 + }, + { + "source": 1, + "target": 2, + "weight": 1 + }, + { + "source": 2, + "target": 3, + "weight": 1 + }, + { + "source": 4, + "target": 2, + "weight": 1 + }, + { + "source": 5, + "target": 1, + "weight": 1 + } + ] +} \ No newline at end of file diff --git a/tracker-server/index.html b/tracker-server/index.html index ce960b5e0..d0ab0de38 100644 --- a/tracker-server/index.html +++ b/tracker-server/index.html @@ -8,9 +8,6 @@
-
- -