469 lines
16 KiB
JavaScript
469 lines
16 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.NodeRevenueService = exports.NodeRegistryService = exports.NodeGovernanceService = exports.NodeEncryptedNotesService = exports.NodeEchoService = exports.NodeTornadoService = exports.NodeEventsService = void 0;
|
||
|
const path_1 = __importDefault(require("path"));
|
||
|
const promises_1 = require("fs/promises");
|
||
|
const core_1 = require("@tornado/core");
|
||
|
const data_1 = require("./data");
|
||
|
class NodeEventsService extends core_1.BatchEventsService {
|
||
|
netId;
|
||
|
logger;
|
||
|
getInstanceName;
|
||
|
constructor(serviceConstructor) {
|
||
|
super(serviceConstructor);
|
||
|
this.netId = serviceConstructor.netId;
|
||
|
this.logger = serviceConstructor.logger;
|
||
|
this.getInstanceName = serviceConstructor.getInstanceName;
|
||
|
}
|
||
|
}
|
||
|
exports.NodeEventsService = NodeEventsService;
|
||
|
class NodeTornadoService extends core_1.BaseTornadoService {
|
||
|
cacheDirectory;
|
||
|
userDirectory;
|
||
|
nativeCurrency;
|
||
|
logger;
|
||
|
treeCache;
|
||
|
constructor(serviceConstructor) {
|
||
|
super(serviceConstructor);
|
||
|
const { netId, provider, Tornado, type, amount, currency, cacheDirectory, userDirectory, nativeCurrency, logger, treeCache, } = serviceConstructor;
|
||
|
this.cacheDirectory = cacheDirectory;
|
||
|
this.userDirectory = userDirectory;
|
||
|
this.nativeCurrency = nativeCurrency;
|
||
|
this.logger = logger;
|
||
|
this.batchEventsService = new NodeEventsService({
|
||
|
netId,
|
||
|
provider,
|
||
|
contract: Tornado,
|
||
|
onProgress: this.updateEventProgress,
|
||
|
logger,
|
||
|
getInstanceName: () => `${type.toLowerCase()}s_${netId}_${currency}_${amount}`,
|
||
|
});
|
||
|
this.treeCache = treeCache;
|
||
|
}
|
||
|
updateEventProgress({ fromBlock, toBlock, count }) {
|
||
|
if (toBlock) {
|
||
|
this.logger.debug(`${this.getInstanceName()}: Fetched ${count} events from ${fromBlock} to ${toBlock}`);
|
||
|
}
|
||
|
}
|
||
|
async getEventsFromDB() {
|
||
|
return await (0, data_1.loadSavedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
});
|
||
|
}
|
||
|
async getEventsFromCache() {
|
||
|
return await (0, data_1.loadCachedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
cacheDirectory: this.cacheDirectory,
|
||
|
deployedBlock: this.deployedBlock,
|
||
|
});
|
||
|
}
|
||
|
async validateEvents({ events, lastBlock, hasNewEvents, }) {
|
||
|
const tree = await super.validateEvents({
|
||
|
events,
|
||
|
lastBlock,
|
||
|
hasNewEvents,
|
||
|
});
|
||
|
if (tree && this.currency === this.nativeCurrency && this.treeCache) {
|
||
|
const merkleTree = tree;
|
||
|
await this.treeCache.createTree(events, merkleTree);
|
||
|
console.log(`${this.getInstanceName()}: Updated tree cache with root ${(0, core_1.toFixedHex)(BigInt(merkleTree.root))}\n`);
|
||
|
}
|
||
|
return tree;
|
||
|
}
|
||
|
async saveEvents({ events, lastBlock }) {
|
||
|
await (0, data_1.saveUserFile)({
|
||
|
fileName: this.getInstanceName() + '.json',
|
||
|
userDirectory: this.userDirectory,
|
||
|
dataString: JSON.stringify(events, null, 2) + '\n',
|
||
|
lastBlock,
|
||
|
});
|
||
|
}
|
||
|
async updateEvents() {
|
||
|
const { events, lastBlock, validateResult } = await super.updateEvents();
|
||
|
await (0, data_1.saveLastBlock)({
|
||
|
fileName: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
lastBlock,
|
||
|
});
|
||
|
return {
|
||
|
events,
|
||
|
lastBlock,
|
||
|
validateResult,
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
exports.NodeTornadoService = NodeTornadoService;
|
||
|
class NodeEchoService extends core_1.BaseEchoService {
|
||
|
cacheDirectory;
|
||
|
userDirectory;
|
||
|
logger;
|
||
|
constructor(serviceConstructor) {
|
||
|
super(serviceConstructor);
|
||
|
const { netId, provider, Echoer, cacheDirectory, userDirectory, logger } = serviceConstructor;
|
||
|
this.cacheDirectory = cacheDirectory;
|
||
|
this.userDirectory = userDirectory;
|
||
|
this.logger = logger;
|
||
|
this.batchEventsService = new NodeEventsService({
|
||
|
netId,
|
||
|
provider,
|
||
|
contract: Echoer,
|
||
|
onProgress: this.updateEventProgress,
|
||
|
logger,
|
||
|
getInstanceName: this.getInstanceName,
|
||
|
});
|
||
|
}
|
||
|
updateEventProgress({ fromBlock, toBlock, count }) {
|
||
|
if (toBlock) {
|
||
|
this.logger.debug(`${this.getInstanceName()}: Fetched ${count} events from ${fromBlock} to ${toBlock}`);
|
||
|
}
|
||
|
}
|
||
|
async getEventsFromDB() {
|
||
|
return await (0, data_1.loadSavedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
});
|
||
|
}
|
||
|
async getEventsFromCache() {
|
||
|
return await (0, data_1.loadCachedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
cacheDirectory: this.cacheDirectory,
|
||
|
deployedBlock: this.deployedBlock,
|
||
|
});
|
||
|
}
|
||
|
async saveEvents({ events, lastBlock }) {
|
||
|
const instanceName = this.getInstanceName();
|
||
|
await (0, data_1.saveUserFile)({
|
||
|
fileName: instanceName + '.json',
|
||
|
userDirectory: this.userDirectory,
|
||
|
dataString: JSON.stringify(events, null, 2) + '\n',
|
||
|
lastBlock,
|
||
|
});
|
||
|
}
|
||
|
async updateEvents() {
|
||
|
const { events, lastBlock, validateResult } = await super.updateEvents();
|
||
|
await (0, data_1.saveLastBlock)({
|
||
|
fileName: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
lastBlock,
|
||
|
});
|
||
|
return {
|
||
|
events,
|
||
|
lastBlock,
|
||
|
validateResult,
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
exports.NodeEchoService = NodeEchoService;
|
||
|
class NodeEncryptedNotesService extends core_1.BaseEncryptedNotesService {
|
||
|
cacheDirectory;
|
||
|
userDirectory;
|
||
|
logger;
|
||
|
constructor(serviceConstructor) {
|
||
|
super(serviceConstructor);
|
||
|
const { netId, provider, Router, cacheDirectory, userDirectory, logger } = serviceConstructor;
|
||
|
this.cacheDirectory = cacheDirectory;
|
||
|
this.userDirectory = userDirectory;
|
||
|
this.logger = logger;
|
||
|
this.batchEventsService = new NodeEventsService({
|
||
|
netId,
|
||
|
provider,
|
||
|
contract: Router,
|
||
|
onProgress: this.updateEventProgress,
|
||
|
logger,
|
||
|
getInstanceName: this.getInstanceName,
|
||
|
});
|
||
|
}
|
||
|
updateEventProgress({ fromBlock, toBlock, count }) {
|
||
|
if (toBlock) {
|
||
|
this.logger.debug(`${this.getInstanceName()}: Fetched ${count} events from ${fromBlock} to ${toBlock}`);
|
||
|
}
|
||
|
}
|
||
|
async getEventsFromDB() {
|
||
|
return await (0, data_1.loadSavedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
});
|
||
|
}
|
||
|
async getEventsFromCache() {
|
||
|
return await (0, data_1.loadCachedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
cacheDirectory: this.cacheDirectory,
|
||
|
deployedBlock: this.deployedBlock,
|
||
|
});
|
||
|
}
|
||
|
async saveEvents({ events, lastBlock }) {
|
||
|
const instanceName = this.getInstanceName();
|
||
|
await (0, data_1.saveUserFile)({
|
||
|
fileName: instanceName + '.json',
|
||
|
userDirectory: this.userDirectory,
|
||
|
dataString: JSON.stringify(events, null, 2) + '\n',
|
||
|
lastBlock,
|
||
|
});
|
||
|
}
|
||
|
async updateEvents() {
|
||
|
const { events, lastBlock, validateResult } = await super.updateEvents();
|
||
|
await (0, data_1.saveLastBlock)({
|
||
|
fileName: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
lastBlock,
|
||
|
});
|
||
|
return {
|
||
|
events,
|
||
|
lastBlock,
|
||
|
validateResult,
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
exports.NodeEncryptedNotesService = NodeEncryptedNotesService;
|
||
|
class NodeGovernanceService extends core_1.BaseGovernanceService {
|
||
|
cacheDirectory;
|
||
|
userDirectory;
|
||
|
logger;
|
||
|
constructor(serviceConstructor) {
|
||
|
super(serviceConstructor);
|
||
|
const { netId, provider, Governance, cacheDirectory, userDirectory, logger } = serviceConstructor;
|
||
|
this.cacheDirectory = cacheDirectory;
|
||
|
this.userDirectory = userDirectory;
|
||
|
this.logger = logger;
|
||
|
this.batchEventsService = new NodeEventsService({
|
||
|
netId,
|
||
|
provider,
|
||
|
contract: Governance,
|
||
|
onProgress: this.updateEventProgress,
|
||
|
logger,
|
||
|
getInstanceName: this.getInstanceName,
|
||
|
});
|
||
|
}
|
||
|
updateEventProgress({ fromBlock, toBlock, count }) {
|
||
|
if (toBlock) {
|
||
|
this.logger.debug(`${this.getInstanceName()}: Fetched ${count} events from ${fromBlock} to ${toBlock}`);
|
||
|
}
|
||
|
}
|
||
|
async getEventsFromDB() {
|
||
|
return await (0, data_1.loadSavedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
});
|
||
|
}
|
||
|
async getEventsFromCache() {
|
||
|
return await (0, data_1.loadCachedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
cacheDirectory: this.cacheDirectory,
|
||
|
deployedBlock: this.deployedBlock,
|
||
|
});
|
||
|
}
|
||
|
async saveEvents({ events, lastBlock }) {
|
||
|
const instanceName = this.getInstanceName();
|
||
|
await (0, data_1.saveUserFile)({
|
||
|
fileName: instanceName + '.json',
|
||
|
userDirectory: this.userDirectory,
|
||
|
dataString: JSON.stringify(events, null, 2) + '\n',
|
||
|
lastBlock,
|
||
|
});
|
||
|
}
|
||
|
async updateEvents() {
|
||
|
const { events, lastBlock, validateResult } = await super.updateEvents();
|
||
|
await (0, data_1.saveLastBlock)({
|
||
|
fileName: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
lastBlock,
|
||
|
});
|
||
|
return {
|
||
|
events,
|
||
|
lastBlock,
|
||
|
validateResult,
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
exports.NodeGovernanceService = NodeGovernanceService;
|
||
|
class NodeRegistryService extends core_1.BaseRegistryService {
|
||
|
cacheDirectory;
|
||
|
userDirectory;
|
||
|
logger;
|
||
|
constructor(serviceConstructor) {
|
||
|
super(serviceConstructor);
|
||
|
const { netId, provider, RelayerRegistry, cacheDirectory, userDirectory, logger } = serviceConstructor;
|
||
|
this.cacheDirectory = cacheDirectory;
|
||
|
this.userDirectory = userDirectory;
|
||
|
this.logger = logger;
|
||
|
this.batchEventsService = new NodeEventsService({
|
||
|
netId,
|
||
|
provider,
|
||
|
contract: RelayerRegistry,
|
||
|
onProgress: this.updateEventProgress,
|
||
|
logger,
|
||
|
getInstanceName: this.getInstanceName,
|
||
|
});
|
||
|
}
|
||
|
updateEventProgress({ fromBlock, toBlock, count }) {
|
||
|
if (toBlock) {
|
||
|
this.logger.debug(`${this.getInstanceName()}: Fetched ${count} events from ${fromBlock} to ${toBlock}`);
|
||
|
}
|
||
|
}
|
||
|
async getEventsFromDB() {
|
||
|
return await (0, data_1.loadSavedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
});
|
||
|
}
|
||
|
async getEventsFromCache() {
|
||
|
return await (0, data_1.loadCachedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
cacheDirectory: this.cacheDirectory,
|
||
|
deployedBlock: this.deployedBlock,
|
||
|
});
|
||
|
}
|
||
|
async saveEvents({ events, lastBlock }) {
|
||
|
const instanceName = this.getInstanceName();
|
||
|
await (0, data_1.saveUserFile)({
|
||
|
fileName: instanceName + '.json',
|
||
|
userDirectory: this.userDirectory,
|
||
|
dataString: JSON.stringify(events, null, 2) + '\n',
|
||
|
lastBlock,
|
||
|
});
|
||
|
}
|
||
|
async updateEvents() {
|
||
|
const { events, lastBlock, validateResult } = await super.updateEvents();
|
||
|
await (0, data_1.saveLastBlock)({
|
||
|
fileName: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
lastBlock,
|
||
|
});
|
||
|
return {
|
||
|
events,
|
||
|
lastBlock,
|
||
|
validateResult,
|
||
|
};
|
||
|
}
|
||
|
async getRelayersFromDB() {
|
||
|
const filePath = path_1.default.join(this.userDirectory || '', 'relayers.json');
|
||
|
if (!this.userDirectory || !(await (0, data_1.existsAsync)(filePath))) {
|
||
|
return {
|
||
|
lastBlock: 0,
|
||
|
timestamp: 0,
|
||
|
relayers: [],
|
||
|
};
|
||
|
}
|
||
|
try {
|
||
|
const { lastBlock, timestamp, relayers } = JSON.parse(await (0, promises_1.readFile)(filePath, { encoding: 'utf8' }));
|
||
|
return {
|
||
|
lastBlock,
|
||
|
timestamp,
|
||
|
relayers,
|
||
|
};
|
||
|
}
|
||
|
catch (err) {
|
||
|
console.log('Method getRelayersFromDB has error');
|
||
|
console.log(err);
|
||
|
return {
|
||
|
lastBlock: 0,
|
||
|
timestamp: 0,
|
||
|
relayers: [],
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
async getRelayersFromCache() {
|
||
|
const filePath = path_1.default.join(this.cacheDirectory || '', 'relayers.json');
|
||
|
if (!this.cacheDirectory || !(await (0, data_1.existsAsync)(filePath))) {
|
||
|
return {
|
||
|
lastBlock: 0,
|
||
|
timestamp: 0,
|
||
|
relayers: [],
|
||
|
fromCache: true,
|
||
|
};
|
||
|
}
|
||
|
try {
|
||
|
const { lastBlock, timestamp, relayers } = JSON.parse(await (0, promises_1.readFile)(filePath, { encoding: 'utf8' }));
|
||
|
return {
|
||
|
lastBlock,
|
||
|
timestamp,
|
||
|
relayers,
|
||
|
fromCache: true,
|
||
|
};
|
||
|
}
|
||
|
catch (err) {
|
||
|
console.log('Method getRelayersFromDB has error');
|
||
|
console.log(err);
|
||
|
return {
|
||
|
lastBlock: 0,
|
||
|
timestamp: 0,
|
||
|
relayers: [],
|
||
|
fromCache: true,
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
async saveRelayers({ lastBlock, timestamp, relayers }) {
|
||
|
await (0, data_1.saveUserFile)({
|
||
|
fileName: 'relayers.json',
|
||
|
userDirectory: this.userDirectory,
|
||
|
dataString: JSON.stringify({ lastBlock, timestamp, relayers }, null, 2) + '\n',
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
exports.NodeRegistryService = NodeRegistryService;
|
||
|
class NodeRevenueService extends core_1.BaseRevenueService {
|
||
|
cacheDirectory;
|
||
|
userDirectory;
|
||
|
logger;
|
||
|
constructor(serviceConstructor) {
|
||
|
super(serviceConstructor);
|
||
|
const { netId, provider, RelayerRegistry, cacheDirectory, userDirectory, logger } = serviceConstructor;
|
||
|
this.cacheDirectory = cacheDirectory;
|
||
|
this.userDirectory = userDirectory;
|
||
|
this.logger = logger;
|
||
|
this.batchEventsService = new NodeEventsService({
|
||
|
netId,
|
||
|
provider,
|
||
|
contract: RelayerRegistry,
|
||
|
onProgress: this.updateEventProgress,
|
||
|
logger,
|
||
|
getInstanceName: this.getInstanceName,
|
||
|
});
|
||
|
}
|
||
|
updateEventProgress({ fromBlock, toBlock, count }) {
|
||
|
if (toBlock) {
|
||
|
this.logger.debug(`${this.getInstanceName()}: Fetched ${count} events from ${fromBlock} to ${toBlock}`);
|
||
|
}
|
||
|
}
|
||
|
async getEventsFromDB() {
|
||
|
return await (0, data_1.loadSavedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
});
|
||
|
}
|
||
|
async getEventsFromCache() {
|
||
|
return await (0, data_1.loadCachedEvents)({
|
||
|
name: this.getInstanceName(),
|
||
|
cacheDirectory: this.cacheDirectory,
|
||
|
deployedBlock: this.deployedBlock,
|
||
|
});
|
||
|
}
|
||
|
async saveEvents({ events, lastBlock }) {
|
||
|
const instanceName = this.getInstanceName();
|
||
|
await (0, data_1.saveUserFile)({
|
||
|
fileName: instanceName + '.json',
|
||
|
userDirectory: this.userDirectory,
|
||
|
dataString: JSON.stringify(events, null, 2) + '\n',
|
||
|
lastBlock,
|
||
|
});
|
||
|
}
|
||
|
async updateEvents() {
|
||
|
const { events, lastBlock, validateResult } = await super.updateEvents();
|
||
|
await (0, data_1.saveLastBlock)({
|
||
|
fileName: this.getInstanceName(),
|
||
|
userDirectory: this.userDirectory,
|
||
|
lastBlock,
|
||
|
});
|
||
|
return {
|
||
|
events,
|
||
|
lastBlock,
|
||
|
validateResult,
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
exports.NodeRevenueService = NodeRevenueService;
|