Skip to content

Commit c39a68c

Browse files
committed
refactor getConfiguration
1 parent 92576e8 commit c39a68c

77 files changed

Lines changed: 799 additions & 755 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/OceanNode.ts

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { OceanP2P } from './components/P2P/index.js'
22
import { OceanProvider } from './components/Provider/index.js'
33
import { OceanIndexer } from './components/Indexer/index.js'
4-
import { OceanNodeConfig, P2PCommandResponse } from './@types/OceanNode.js'
4+
import {
5+
AccessListContract,
6+
OceanNodeConfig,
7+
P2PCommandResponse
8+
} from './@types/OceanNode.js'
9+
import { ValidateChainId } from './@types/commands.js'
10+
511
import { Database } from './components/database/index.js'
612
import { Escrow } from './components/core/utils/escrow.js'
713
import { CoreHandlersRegistry } from './components/core/handler/coreHandlersRegistry.js'
@@ -15,6 +21,8 @@ import { BlockchainRegistry } from './components/BlockchainRegistry/index.js'
1521
import { Blockchain } from './utils/blockchain.js'
1622
import { createPersistentStorage } from './components/persistentStorage/createPersistentStorage.js'
1723
import { PersistentStorageFactory } from './components/persistentStorage/PersistentStorageFactory.js'
24+
import { isAddress, FallbackProvider, ethers } from 'ethers'
25+
import { create256Hash } from './utils/crypt.js'
1826

1927
export interface RequestLimiter {
2028
requester: string | string[] // IP address or peer ID
@@ -40,6 +48,7 @@ export class OceanNode {
4048
private requestMap: Map<string, RequestLimiter>
4149
private auth: Auth
4250
private persistentStorage: PersistentStorageFactory
51+
private database: Database
4352

4453
// eslint-disable-next-line no-useless-constructor
4554
private constructor(
@@ -64,8 +73,9 @@ export class OceanNode {
6473
this.coreHandlers = CoreHandlersRegistry.getInstance(this)
6574
this.requestMap = new Map<string, RequestLimiter>()
6675
this.config = config
76+
this.database = db
6777
if (this.db && this.db?.authToken) {
68-
this.auth = new Auth(this.db.authToken)
78+
this.auth = new Auth(this.db.authToken, config)
6979
}
7080
if (node) {
7181
node.setCoreHandlers(this.coreHandlers)
@@ -161,10 +171,6 @@ export class OceanNode {
161171
return this.indexer
162172
}
163173

164-
public getDatabase(): Database {
165-
return this.db
166-
}
167-
168174
public getC2DEngines(): C2DEngines {
169175
return this.c2dEngines
170176
}
@@ -259,4 +265,81 @@ export class OceanNode {
259265
}
260266
}
261267
}
268+
269+
getAdminAddresses(): { addresses: string[]; accessLists: any } {
270+
const ret = {
271+
addresses: [] as string[],
272+
accessLists: undefined as AccessListContract | undefined
273+
}
274+
275+
if (this.config.allowedAdmins && this.config.allowedAdmins.length > 0) {
276+
for (const admin of this.config.allowedAdmins) {
277+
if (isAddress(admin) === true) {
278+
ret.addresses.push(admin)
279+
}
280+
}
281+
}
282+
ret.accessLists = this.config.allowedAdminsList
283+
return ret
284+
}
285+
286+
checkSupportedChainId(chainId: number): ValidateChainId {
287+
if (!chainId || !(`${chainId.toString()}` in this.config.supportedNetworks)) {
288+
OCEAN_NODE_LOGGER.error(`Chain ID ${chainId} is not supported`)
289+
return {
290+
validation: false,
291+
networkRpc: ''
292+
}
293+
}
294+
return {
295+
validation: true,
296+
networkRpc: this.config.supportedNetworks[chainId.toString()].rpc
297+
}
298+
}
299+
300+
async getJsonRpcProvider(chainId: number): Promise<FallbackProvider> {
301+
const checkResult = this.checkSupportedChainId(chainId)
302+
if (!checkResult.validation) {
303+
return null
304+
}
305+
const blockchain = this.getBlockchain(chainId)
306+
if (!blockchain) return null
307+
return await blockchain.getProvider()
308+
}
309+
310+
hasP2PInterface() {
311+
return this.config.hasP2P || false
312+
}
313+
314+
async getDatabase(forceReload: boolean = false): Promise<Database> {
315+
if (!this.database || forceReload) {
316+
const { dbConfig } = this.config
317+
if (dbConfig && dbConfig.url) {
318+
this.database = await Database.init(dbConfig)
319+
}
320+
}
321+
return this.database
322+
}
323+
324+
async getValidationSignature(ddo: string): Promise<any> {
325+
try {
326+
const hashedDDO = create256Hash(ddo)
327+
const providerWallet = await this.keyManager.getEthWallet()
328+
const messageHash = ethers.solidityPackedKeccak256(
329+
['bytes'],
330+
[ethers.hexlify(ethers.toUtf8Bytes(hashedDDO))]
331+
)
332+
const signed32Bytes = await providerWallet.signMessage(
333+
new Uint8Array(ethers.toBeArray(messageHash))
334+
)
335+
const signatureSplitted = ethers.Signature.from(signed32Bytes)
336+
const v = signatureSplitted.v <= 1 ? signatureSplitted.v + 27 : signatureSplitted.v
337+
const r = ethers.hexlify(signatureSplitted.r) // 32 bytes
338+
const s = ethers.hexlify(signatureSplitted.s)
339+
return { hash: hashedDDO, publicKey: providerWallet.address, r, s, v }
340+
} catch (error) {
341+
OCEAN_NODE_LOGGER.logMessage(`Validation signature error: ${error}`, true)
342+
return { hash: '', publicKey: '', r: '', s: '', v: '' }
343+
}
344+
}
262345
}

src/components/Auth/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { AuthToken, AuthTokenDatabase } from '../database/AuthTokenDatabase.js'
22
import jwt from 'jsonwebtoken'
33
import { checkNonce, NonceResponse } from '../core/utils/nonceHandler.js'
44
import { OceanNode } from '../../OceanNode.js'
5-
import { getConfiguration } from '../../utils/index.js'
65
import { CommonValidation } from '../../utils/validators.js'
7-
6+
import { OceanNodeConfig } from '../../@types/OceanNode.js'
87
export interface AuthValidation {
98
token?: string
109
address?: string
@@ -16,24 +15,26 @@ export interface AuthValidation {
1615

1716
export class Auth {
1817
private authTokenDatabase: AuthTokenDatabase
18+
private jwtSecret: string
1919

20-
public constructor(authTokenDatabase: AuthTokenDatabase) {
20+
public constructor(authTokenDatabase: AuthTokenDatabase, config: OceanNodeConfig) {
2121
this.authTokenDatabase = authTokenDatabase
22+
this.jwtSecret = config.jwtSecret
2223
}
2324

24-
public async getJwtSecret(): Promise<string> {
25-
const config = await getConfiguration()
26-
return config.jwtSecret
25+
public getJwtSecret(): string {
26+
return this.jwtSecret
2727
}
2828

29+
// eslint-disable-next-line require-await
2930
async getJWTToken(address: string, nonce: string, createdAt: number): Promise<string> {
3031
const jwtToken = jwt.sign(
3132
{
3233
address,
3334
nonce,
3435
createdAt
3536
},
36-
await this.getJwtSecret()
37+
this.getJwtSecret()
3738
)
3839

3940
return jwtToken
@@ -84,7 +85,8 @@ export class Auth {
8485
if (signature && address && nonce) {
8586
const oceanNode = OceanNode.getInstance()
8687
const nonceCheckResult: NonceResponse = await checkNonce(
87-
oceanNode.getDatabase().nonce,
88+
oceanNode.getConfig(),
89+
(await oceanNode.getDatabase()).nonce,
8890
address,
8991
parseInt(nonce),
9092
signature,

src/components/Indexer/ChainIndexer.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { LOG_LEVELS_STR } from '../../utils/logging/Logger.js'
55
import { isDefined, sleep } from '../../utils/util.js'
66
import { EVENTS, INDEXER_CRAWLING_EVENTS } from '../../utils/index.js'
77
import { INDEXER_LOGGER } from '../../utils/logging/common.js'
8-
import { getDatabase } from '../../utils/database.js'
98
import { DEVELOPMENT_CHAIN_ID } from '../../utils/address.js'
109
import { processBlocks, processChunkLogs } from './processor.js'
1110
import { Blockchain } from '../../utils/blockchain.js'
@@ -16,6 +15,7 @@ import {
1615
retrieveChunkEvents
1716
} from './utils.js'
1817
import { OceanNodeConfig } from '../../@types/OceanNode.js'
18+
import { Database } from '../database/index.js'
1919

2020
export interface ReindexTask {
2121
txId: string
@@ -36,15 +36,18 @@ export class ChainIndexer {
3636
private reindexQueue: ReindexTask[] = []
3737
private eventEmitter: EventEmitter
3838
private blockchain: Blockchain
39+
private db: Database
3940

4041
constructor(
4142
blockchain: Blockchain,
4243
rpcDetails: SupportedNetwork,
43-
eventEmitter: EventEmitter
44+
eventEmitter: EventEmitter,
45+
database: Database
4446
) {
4547
this.blockchain = blockchain
4648
this.eventEmitter = eventEmitter
4749
this.rpcDetails = rpcDetails
50+
this.db = database
4851
}
4952

5053
/**
@@ -237,7 +240,8 @@ export class ChainIndexer {
237240
provider,
238241
this.blockchain.getSupportedChain(),
239242
startBlock,
240-
blocksToProcess
243+
blocksToProcess,
244+
this.config
241245
)
242246

243247
INDEXER_LOGGER.debug(
@@ -320,7 +324,7 @@ export class ChainIndexer {
320324
* Get the last indexed block from database
321325
*/
322326
private async getLastIndexedBlock(): Promise<number | null> {
323-
const { indexer } = await getDatabase()
327+
const { indexer } = this.db
324328
try {
325329
const networkDetails = await indexer.retrieve(this.blockchain.getSupportedChain())
326330
if (networkDetails && networkDetails.lastIndexedBlock) {
@@ -352,7 +356,7 @@ export class ChainIndexer {
352356
return -1
353357
}
354358

355-
const { indexer } = await getDatabase()
359+
const { indexer } = this.db
356360
const updatedIndex = await indexer.update(
357361
this.blockchain.getSupportedChain(),
358362
block
@@ -383,7 +387,7 @@ export class ChainIndexer {
383387
* Delete all assets from this chain
384388
*/
385389
private async deleteAllAssetsFromChain(): Promise<number> {
386-
const { ddo } = await getDatabase()
390+
const { ddo } = this.db
387391
try {
388392
const numDeleted = await ddo.deleteAllAssetsFromChain(
389393
this.blockchain.getSupportedChain()
@@ -449,7 +453,8 @@ export class ChainIndexer {
449453
logs,
450454
signer,
451455
provider,
452-
this.blockchain.getSupportedChain()
456+
this.blockchain.getSupportedChain(),
457+
this.config
453458
)
454459

455460
// Emit event to clear from parent queue

src/components/Indexer/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { BlockchainRegistry } from '../BlockchainRegistry/index.js'
3535
import { CommandStatus, JobStatus } from '../../@types/commands.js'
3636
import { buildJobIdentifier, getDeployedContractBlock } from './utils.js'
3737
import { create256Hash } from '../../utils/crypt.js'
38-
import { getDatabase, isReachableConnection } from '../../utils/database.js'
38+
import { isReachableConnection } from '../../utils/database.js'
3939
import { sleep } from '../../utils/util.js'
4040
import { isReindexingNeeded } from './version.js'
4141
import { getPackageVersion } from '../../utils/version.js'
@@ -145,10 +145,10 @@ export class OceanIndexer {
145145
INDEXER_LOGGER.info(
146146
'Database connection stable - reinitialising DB and restarting all chain indexers'
147147
)
148-
const freshDb = await getDatabase(true)
149-
if (freshDb) {
150-
this.db = freshDb
151-
}
148+
// const freshDb = await getDatabase(true)
149+
// if (freshDb) {
150+
// this.db = freshDb
151+
// }
152152

153153
await this.startAllChainIndexers()
154154
}, 5000)
@@ -292,7 +292,8 @@ export class OceanIndexer {
292292
const indexer = new ChainIndexer(
293293
blockchain,
294294
rpcDetails,
295-
INDEXER_CRAWLING_EVENT_EMITTER
295+
INDEXER_CRAWLING_EVENT_EMITTER,
296+
this.db
296297
)
297298

298299
INDEXER_LOGGER.log(

src/components/Indexer/processor.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ethers, Signer, FallbackProvider, Interface, getAddress } from 'ethers'
22
import { BlocksEvents, ProcessingEvents } from '../../@types/blockchain.js'
33
import { EVENTS } from '../../utils/constants.js'
4-
import { getConfiguration } from '../../utils/config.js'
54
import { INDEXER_LOGGER } from '../../utils/logging/common.js'
65
import { LOG_LEVELS_STR } from '../../utils/logging/Logger.js'
76
import { fetchEventFromTransaction } from '../../utils/util.js'
@@ -23,6 +22,7 @@ import {
2322
import { findEventByKey } from './utils.js'
2423
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20TemplateEnterprise.sol/ERC20TemplateEnterprise.json' with { type: 'json' }
2524
import AccessListContract from '@oceanprotocol/contracts/artifacts/contracts/accesslists/AccessList.sol/AccessList.json' with { type: 'json' }
25+
import { OceanNodeConfig } from '../../@types/OceanNode.js'
2626

2727
const EVENT_PROCESSOR_MAP: Record<string, ProcessorConstructor> = {
2828
[EVENTS.METADATA_CREATED]: MetadataEventProcessor,
@@ -41,15 +41,19 @@ const EVENT_PROCESSOR_MAP: Record<string, ProcessorConstructor> = {
4141

4242
const processorInstances = new Map<string, BaseEventProcessor>()
4343

44-
function getEventProcessor(eventType: string, chainId: number): BaseEventProcessor {
44+
function getEventProcessor(
45+
eventType: string,
46+
chainId: number,
47+
config: OceanNodeConfig
48+
): BaseEventProcessor {
4549
const cacheKey = `${eventType}-${chainId}`
4650

4751
if (!processorInstances.has(cacheKey)) {
4852
const ProcessorClass = EVENT_PROCESSOR_MAP[eventType]
4953
if (!ProcessorClass) {
5054
throw new Error(`No processor found for event type: ${eventType}`)
5155
}
52-
processorInstances.set(cacheKey, new ProcessorClass(chainId))
56+
processorInstances.set(cacheKey, new ProcessorClass(chainId, config))
5357
}
5458

5559
return processorInstances.get(cacheKey)
@@ -59,11 +63,12 @@ export const processChunkLogs = async (
5963
logs: readonly ethers.Log[],
6064
signer: Signer,
6165
provider: FallbackProvider,
62-
chainId: number
66+
chainId: number,
67+
config: OceanNodeConfig
6368
): Promise<BlocksEvents> => {
6469
const storeEvents: BlocksEvents = {}
6570
if (logs.length > 0) {
66-
const { allowedValidators, allowedValidatorsList } = await getConfiguration() // getAllowedValidators()
71+
const { allowedValidators, allowedValidatorsList } = config // getAllowedValidators()
6772
const checkMetadataValidated =
6873
allowedValidators.length > 0 ||
6974
(allowedValidatorsList && Object.keys(allowedValidatorsList).length > 0)
@@ -164,7 +169,7 @@ export const processChunkLogs = async (
164169
if (event.type === EVENTS.TOKEN_URI_UPDATE) {
165170
storeEvents[event.type] = 'TOKEN_URI_UPDATE'
166171
} else {
167-
const processor = getEventProcessor(event.type, chainId)
172+
const processor = getEventProcessor(event.type, chainId, config)
168173
storeEvents[event.type] = await processor.processEvent(
169174
log,
170175
chainId,
@@ -188,12 +193,13 @@ export const processBlocks = async (
188193
provider: FallbackProvider,
189194
network: number,
190195
lastIndexedBlock: number,
191-
count: number
196+
count: number,
197+
config: OceanNodeConfig
192198
): Promise<ProcessingEvents> => {
193199
try {
194200
const events: any[] | BlocksEvents =
195201
blockLogs && blockLogs.length > 0
196-
? await processChunkLogs(blockLogs, signer, provider, network)
202+
? await processChunkLogs(blockLogs, signer, provider, network, config)
197203
: []
198204

199205
return {

0 commit comments

Comments
 (0)