Skip to content

Commit 37a1aca

Browse files
committed
Validate that operator account has positive balance
Signed-off-by: Luis Mastrangelo <[email protected]>
1 parent 34ce7f7 commit 37a1aca

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

packages/relay/src/lib/relay.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,14 @@ export class Relay {
329329
mirrorClient(): MirrorNodeClient {
330330
return this.mirrorNodeClient;
331331
}
332+
333+
async ensureOperatorHasBalance() {
334+
if (!ConfigService.get('READ_ONLY')) {
335+
const operator = this.clientMain.operatorAccountId!.toString();
336+
const balance = await this.ethImpl.getBalance(operator, 'latest', {} as RequestDetails);
337+
if (BigInt(balance) === BigInt(0)) {
338+
throw new Error(`Operator account \`${operator}\` has no balance`);
339+
}
340+
}
341+
}
332342
}

packages/server/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3-
import app from './server';
43
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
4+
55
import { setServerTimeout } from './koaJsonRpc/lib/utils'; // Import the 'setServerTimeout' function from the correct location
6+
import app, { logger, relay } from './server';
67

78
async function main() {
9+
try {
10+
await relay.ensureOperatorHasBalance();
11+
} catch (error) {
12+
logger.fatal(error);
13+
process.exit(1);
14+
}
15+
816
const server = app.listen({ port: ConfigService.get('SERVER_PORT'), host: ConfigService.get('SERVER_HOST') });
917

1018
// set request timeout to ensure sockets are closed after specified time of inactivity

packages/server/src/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ const mainLogger = pino({
2626
},
2727
});
2828

29-
const logger = mainLogger.child({ name: 'rpc-server' });
29+
export const logger = mainLogger.child({ name: 'rpc-server' });
3030
const register = new Registry();
31-
const relay: Relay = new Relay(logger.child({ name: 'relay' }), register);
31+
export const relay: Relay = new Relay(logger.child({ name: 'relay' }), register);
3232
const app = new KoaJsonRpc(logger.child({ name: 'koa-rpc' }), register, relay, {
3333
limit: ConfigService.get('INPUT_SIZE_LIMIT') + 'mb',
3434
});

packages/ws-server/src/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3-
import { app, httpApp } from './webSocketServer';
4-
import constants from '@hashgraph/json-rpc-relay/dist/lib/constants';
53
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
4+
import constants from '@hashgraph/json-rpc-relay/dist/lib/constants';
5+
6+
import { app, httpApp, logger, relay } from './webSocketServer';
67

78
async function main() {
9+
try {
10+
await relay.ensureOperatorHasBalance();
11+
} catch (error) {
12+
logger.fatal(error);
13+
process.exit(1);
14+
}
15+
816
const host = ConfigService.get('SERVER_HOST');
917
app.listen({ port: constants.WEB_SOCKET_PORT, host });
1018
httpApp.listen({ port: constants.WEB_SOCKET_HTTP_PORT, host });
1119
}
1220

13-
(async () => {
14-
await main();
15-
})();
21+
main();

packages/ws-server/src/webSocketServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,4 @@ process.on('uncaughtException', (err) => {
237237
logger.error(err, 'Uncaught Exception!');
238238
});
239239

240-
export { app, httpApp };
240+
export { app, httpApp, relay, logger };

0 commit comments

Comments
 (0)