Skip to content

Commit

Permalink
Merge pull request #2169 from OriginTrail/v6/prerelease/testnet
Browse files Browse the repository at this point in the history
OriginTrail 6.0.0-beta.3.0.0 Testnet Release
  • Loading branch information
zeroxbt authored Nov 3, 2022
2 parents 6c69f10 + 925193a commit b039042
Show file tree
Hide file tree
Showing 44 changed files with 1,530 additions and 452 deletions.
13 changes: 11 additions & 2 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"config": {
"blockchainTitle": "ganache",
"networkId": "ganache::testnet",
"identityFileName": "ganache",
"hubContractAddress": "0x209679fA3B658Cd0fC74473aF28243bfe78a9b12",
"rpcEndpoints": ["http://localhost:7545"],
"evmManagementPublicKey": "0x1B420da5f7Be66567526E32bc68ab29F1A63765A"
Expand All @@ -134,14 +135,16 @@
"package": "./blockchain/implementation/ot-parachain/ot-parachain-service.js",
"config": {
"networkId": "otp::testnet",
"hubContractAddress": "0x256736AEb3f19AC6738E9F4D10C9B61da71CEB9F",
"identityFileName": "otp",
"hubContractAddress": "0x707233a55bD035C6Bc732196CA4dbffa63CbA169",
"rpcEndpoints": ["wss://lofar.origin-trail.network"]
}
},
"polygon": {
"package": "./blockchain/implementation/polygon/polygon-service.js",
"config": {
"networkId": "polygon::testnet",
"identityFileName": "polygon",
"hubContractAddress": "0xdaa16AC171CfE8Df6F79C06E7EEAb2249E2C9Ec8",
"gasPriceOracleLink": "https://gasstation-mumbai.matic.today/v2",
"rpcEndpoints": [
Expand All @@ -156,6 +159,7 @@
"package": "./blockchain/implementation/polygon/eth-service.js",
"config": {
"networkId": "eth::rinkeby",
"identityFileName": "rinkeby",
"hubContractAddress": "",
"gasPriceOracleLink": "",
"rpcEndpoints": []
Expand Down Expand Up @@ -261,6 +265,7 @@
"package": "./blockchain/implementation/ganache/ganache-service.js",
"config": {
"blockchainTitle": "ganache",
"identityFileName": "ganache",
"networkId": "ganache::testnet",
"hubContractAddress": "0x209679fA3B658Cd0fC74473aF28243bfe78a9b12",
"rpcEndpoints": ["http://localhost:7545"]
Expand Down Expand Up @@ -409,14 +414,16 @@
"package": "./blockchain/implementation/ot-parachain/ot-parachain-service.js",
"config": {
"networkId": "parachain::testnet",
"hubContractAddress": "0x256736AEb3f19AC6738E9F4D10C9B61da71CEB9F",
"hubContractAddress": "0xc9184C1A0CE150a882DC3151Def25075bdAf069C",
"identityFileName": "otp",
"rpcEndpoints": ["wss://lofar.origin-trail.network"]
}
},
"polygon": {
"package": "./blockchain/implementation/polygon/polygon-service.js",
"config": {
"networkId": "polygon::testnet",
"identityFileName": "polygon",
"hubContractAddress": "0xdaa16AC171CfE8Df6F79C06E7EEAb2249E2C9Ec8",
"gasPriceOracleLink": "https://gasstation-mumbai.matic.today/v2",
"rpcEndpoints": [
Expand Down Expand Up @@ -588,6 +595,7 @@
"package": "./blockchain/implementation/ot-parachain/ot-parachain-service.js",
"config": {
"networkId": "otp::mainnet",
"identityFileName": "otp",
"hubContractAddress": "",
"rpcEndpoints": []
}
Expand All @@ -596,6 +604,7 @@
"package": "./blockchain/implementation/polygon/polygon-service.js",
"config": {
"networkId": "polygon::mainnet",
"identityFileName": "polygon",
"hubContractAddress": "",
"gasPriceOracleLink": "",
"rpcEndpoints": []
Expand Down
188 changes: 104 additions & 84 deletions ot-node.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import DeepExtend from 'deep-extend';
import rc from 'rc';
import fs from 'fs';
import appRootPath from 'app-root-path';
import path from 'path';
import EventEmitter from 'events';
import { createRequire } from 'module';
import DependencyInjection from './src/service/dependency-injection.js';
import Logger from './src/logger/logger.js';
import { MIN_NODE_VERSION } from './src/constants/constants.js';
import { CONTRACTS, MIN_NODE_VERSION } from './src/constants/constants.js';
import FileService from './src/service/file-service.js';
import NetworkPrivateKeyMigration from './src/migration/network-private-key-migration.js';
import OtnodeUpdateCommand from './src/commands/common/otnode-update-command.js';
import OtAutoUpdater from './src/modules/auto-updater/implementation/ot-auto-updater.js';
import BlockchainIdentityMigration from './src/migration/blockchain-identity-migration.js';

const require = createRequire(import.meta.url);
const pjson = require('./package.json');
Expand Down Expand Up @@ -47,14 +45,14 @@ class OTNode {
this.initializeEventEmitter();

await this.initializeModules();
await this.saveNetworkModulePeerIdAndPrivKey();
await this.createProfiles();

await this.initializeCommandExecutor();
await this.initializeShardingTableService();
await this.initializeTelemetryInjectionService();

await this.initializeRouters();

await this.startListeningOnBlockchainEvents();
this.logger.info('Node is up and running!');
}

Expand Down Expand Up @@ -162,56 +160,39 @@ class OTNode {
async createProfiles() {
const blockchainModuleManager = this.container.resolve('blockchainModuleManager');
const createProfilesPromises = blockchainModuleManager
.getImplementationsNames()
.getImplementationNames()
.map(async (blockchain) => {
try {
if (!blockchainModuleManager.identityExists(blockchain)) {
this.logger.info(`Creating blockchain identity on network: ${blockchain}`);
const networkModuleManager = this.container.resolve('networkModuleManager');
const peerId = networkModuleManager.getPeerId();
await blockchainModuleManager.deployIdentity(blockchain);
await blockchainModuleManager.saveIdentityInFile();
}
const identity = blockchainModuleManager.getIdentity(blockchain);
this.logger.info(`${blockchain} blockchain identity is ${identity}`);

if (!(await blockchainModuleManager.profileExists(blockchain, identity))) {
this.logger.info(`Creating profile on network: ${blockchain}`);
const networkModuleManager = this.container.resolve('networkModuleManager');
const peerId = networkModuleManager.getPeerId().toB58String();
await blockchainModuleManager.createProfile(blockchain, peerId);
if (
process.env.NODE_ENV !== 'development' &&
process.env.NODE_ENV !== 'test'
) {
await this.saveIdentityInUserConfigurationFile(
blockchainModuleManager.getIdentity(blockchain),
blockchain,
);
}
}
this.logger.info(
`${blockchain} blockchain identity is ${blockchainModuleManager.getIdentity(
blockchain,
)}`,
);
} catch (error) {
this.logger.warn(
`Unable to create ${blockchain} blockchain profile. Removing implementation.`,
`Unable to create ${blockchain} blockchain profile. Removing implementation. Error: ${error.message}`,
);
blockchainModuleManager.removeImplementation(blockchain);
}
});

await Promise.all(createProfilesPromises);

if (!blockchainModuleManager.getImplementationsNames().length) {
this.logger.info(`Unable to create blockchain profiles. OT-node shutting down...`);
if (!blockchainModuleManager.getImplementationNames().length) {
this.logger.error(`Unable to create blockchain profiles. OT-node shutting down...`);
this.stop(1);
}
}

async saveNetworkModulePeerIdAndPrivKey() {
const networkModuleManager = this.container.resolve('networkModuleManager');
const privateKey = networkModuleManager.getPrivateKey();

if (process.env.NODE_ENV !== 'development' && process.env.NODE_ENV !== 'test') {
await this.savePrivateKeyAndPeerIdInUserConfigurationFile(privateKey);
}
}

async initializeCommandExecutor() {
try {
const commandExecutor = this.container.resolve('commandExecutor');
Expand All @@ -226,6 +207,85 @@ class OTNode {
}
}

async initializeShardingTableService() {
const blockchainModuleManager = this.container.resolve('blockchainModuleManager');
const initShardingServices = blockchainModuleManager
.getImplementationNames()
.map(async (blockchain) => {
try {
const shardingTableService = this.container.resolve('shardingTableService');
shardingTableService.initialize(blockchain);
this.logger.info(
`Sharding Table Service initialized successfully for '${blockchain}' blockchain`,
);
} catch (e) {
this.logger.error(
`Sharding table service initialization for '${blockchain}' blockchain failed.
Error message: ${e.message}`,
);
blockchainModuleManager.removeImplementation(blockchain);
}
});
await Promise.all(initShardingServices);

if (!blockchainModuleManager.getImplementationNames().length) {
this.logger.error(
`Unable to initialize sharding table service. OT-node shutting down...`,
);
this.stop(1);
}
}

async startListeningOnBlockchainEvents() {
this.logger.info('Starting blockchain event listener');
const blockchainModuleManager = this.container.resolve('blockchainModuleManager');
const repositoryModuleManager = this.container.resolve('repositoryModuleManager');
const eventEmitter = this.container.resolve('eventEmitter');

const onEventsReceived = async (events) => {
if (events.length > 0) {
const insertedEvents = await repositoryModuleManager.insertBlockchainEvents(events);
insertedEvents.forEach((event) => {
if (event) {
const eventName = `${event.blockchain_id}-${event.event}`;
eventEmitter.emit(eventName, event);
}
});
}
};

const getLastCheckedBlock = async (blockchainId, contract) =>
repositoryModuleManager.getLastCheckedBlock(blockchainId, contract);

const updateLastCheckedBlock = async (blockchainId, currentBlock, timestamp, contract) =>
repositoryModuleManager.updateLastCheckedBlock(
blockchainId,
currentBlock,
timestamp,
contract,
);

let working = false;

setInterval(async () => {
if (!working) {
try {
working = true;
await blockchainModuleManager.getAllPastEvents(
CONTRACTS.SHARDING_TABLE_CONTRACT,
onEventsReceived,
getLastCheckedBlock,
updateLastCheckedBlock,
);
} catch (e) {
this.logger.error(`Failed to get blockchain events. Error: ${e}`);
} finally {
working = false;
}
}
}, 10 * 1000);
}

async initializeTelemetryInjectionService() {
if (this.config.telemetry.enabled) {
try {
Expand All @@ -242,55 +302,6 @@ class OTNode {
}
}

async savePrivateKeyAndPeerIdInUserConfigurationFile(privateKey) {
const configurationFilePath = path.join(appRootPath.path, '..', this.config.configFilename);
const configFile = JSON.parse(await fs.promises.readFile(configurationFilePath));

if (!configFile.modules.network) {
configFile.modules.network = {
implementation: {
'libp2p-service': {
config: {},
},
},
};
} else if (!configFile.modules.network.implementation) {
configFile.modules.network.implementation = {
'libp2p-service': {
config: {},
},
};
} else if (!configFile.modules.network.implementation['libp2p-service']) {
configFile.modules.network.implementation['libp2p-service'] = {
config: {},
};
}
if (!configFile.modules.network.implementation['libp2p-service'].config.privateKey) {
configFile.modules.network.implementation['libp2p-service'].config.privateKey =
privateKey;
await fs.promises.writeFile(configurationFilePath, JSON.stringify(configFile, null, 2));
}
}

async saveIdentityInUserConfigurationFile(identity, blockchain) {
const configurationFilePath = path.join(appRootPath.path, '..', this.config.configFilename);
const configFile = JSON.parse(await fs.promises.readFile(configurationFilePath));
if (
configFile.modules.blockchain &&
configFile.modules.blockchain.implementation &&
configFile.modules.blockchain.implementation[blockchain] &&
configFile.modules.blockchain.implementation[blockchain].config
) {
if (!configFile.modules.blockchain.implementation[blockchain].config.identity) {
configFile.modules.blockchain.implementation[blockchain].config.identity = identity;
await fs.promises.writeFile(
configurationFilePath,
JSON.stringify(configFile, null, 2),
);
}
}
}

async removeUpdateFile() {
const updateFilePath = this.fileService.getUpdateFilePath();
await this.fileService.removeFile(updateFilePath).catch((error) => {
Expand All @@ -308,6 +319,15 @@ class OTNode {
if (!(await networkPrivateKeyMigration.migrationAlreadyExecuted())) {
await networkPrivateKeyMigration.migrate();
}

const blockchainIdentityMigration = new BlockchainIdentityMigration(
'BlockchainIdentityMigration',
this.logger,
this.config,
);
if (!(await blockchainIdentityMigration.migrationAlreadyExecuted())) {
await blockchainIdentityMigration.migrate();
}
}

async checkForUpdate() {
Expand Down
19 changes: 9 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b039042

Please sign in to comment.