Skip to content

Commit a18083e

Browse files
authored
refactor getConfiguration (#1344)
* refactor getConfiguration
1 parent 6d899ea commit a18083e

98 files changed

Lines changed: 1245 additions & 1031 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.

.github/workflows/ci.yml

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ jobs:
119119
repository: 'oceanprotocol/barge'
120120
path: 'barge'
121121
- name: Login to Docker Hub
122-
if: ${{ env.DOCKERHUB_PASSWORD && env.DOCKERHUB_USERNAME }}
122+
if: ${{ env.DOCKERHUB_PASSWORDNONO && env.DOCKERHUB_USERNAMENONO }}
123123
run: |
124124
echo "Login to Docker Hub";echo "$DOCKERHUB_PASSWORD" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
125125
env:
@@ -226,12 +226,44 @@ jobs:
226226
working-directory: ${{ github.workspace }}/barge
227227
run: |
228228
bash -x start_ocean.sh --no-node --with-typesense 2>&1 > start_ocean.log &
229-
- run: npm ci
230-
- run: npm run build
231229
- run: docker image ls
232230
- name: Delete default runner images
233231
run: |
234232
rm -rf /usr/share/swift/
233+
- name: Checkout Ocean CLI
234+
uses: actions/checkout@v4
235+
with:
236+
repository: 'oceanprotocol/ocean-cli'
237+
path: 'ocean-cli'
238+
- name: Checkout Ocean-js
239+
uses: actions/checkout@v4
240+
with:
241+
repository: 'oceanprotocol/ocean.js'
242+
path: 'ocean.js'
243+
ref: main
244+
- name: Build ocean-js
245+
working-directory: ${{ github.workspace }}/ocean.js
246+
run: |
247+
npm ci
248+
npm run build
249+
npm link
250+
- name: Setup Ocean CLI
251+
working-directory: ${{ github.workspace }}/ocean-cli
252+
run: |
253+
npm ci
254+
npm link @oceanprotocol/lib
255+
npm run build
256+
- name: Checkout Ocean Node
257+
uses: actions/checkout@v4
258+
with:
259+
repository: 'oceanprotocol/ocean-node'
260+
path: 'ocean-node'
261+
ref: ${{ github.event_name == 'pull_request' && github.head_ref || 'main' }}
262+
- name: Build Ocean Node
263+
working-directory: ${{ github.workspace }}/ocean-node
264+
run: |
265+
npm ci
266+
npm run build
235267
236268
- name: Wait for contracts deployment and C2D cluster to be ready
237269
working-directory: ${{ github.workspace }}/barge
@@ -245,12 +277,6 @@ jobs:
245277
run: docker logs ocean-contracts-1 && docker logs ocean-typesense-1
246278
if: ${{ failure() }}
247279

248-
- name: Checkout Ocean Node
249-
uses: actions/checkout@v4
250-
with:
251-
repository: 'oceanprotocol/ocean-node'
252-
path: 'ocean-node'
253-
ref: ${{ github.event_name == 'pull_request' && github.head_ref || 'main' }}
254280
- name: Set DOCKER_REGISTRY_AUTHS from Docker Hub secrets
255281
if: env.DOCKERHUB_USERNAME && env.DOCKERHUB_PASSWORD
256282
run: |
@@ -267,8 +293,6 @@ jobs:
267293
- name: Start Ocean Node
268294
working-directory: ${{ github.workspace }}/ocean-node
269295
run: |
270-
npm ci
271-
npm run build
272296
npm run start > ocean-node.log 2>&1 &
273297
env:
274298
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
@@ -295,7 +319,7 @@ jobs:
295319
PERSISTENT_STORAGE: '{"enabled": true, "type": "localfs", "options": {"folder": "/tmp/ocean-persistent-storage"}}'
296320
- name: Check Ocean Node is running
297321
run: |
298-
for i in $(seq 1 90); do
322+
for i in $(seq 1 12); do
299323
if curl --output /dev/null --silent --head --fail "http://localhost:8001"; then
300324
echo "Ocean Node is up"
301325
exit 0
@@ -304,33 +328,6 @@ jobs:
304328
done
305329
echo "Ocean Node did not start in time"
306330
exit 1
307-
- name: Checkout Ocean CLI
308-
uses: actions/checkout@v4
309-
with:
310-
repository: 'oceanprotocol/ocean-cli'
311-
path: 'ocean-cli'
312-
- name: Set up Node.js
313-
uses: actions/setup-node@v4
314-
with:
315-
node-version: 'v20.19.0'
316-
- name: Checkout Ocean-js
317-
uses: actions/checkout@v4
318-
with:
319-
repository: 'oceanprotocol/ocean.js'
320-
path: 'ocean.js'
321-
ref: main
322-
- name: Build ocean-js
323-
working-directory: ${{ github.workspace }}/ocean.js
324-
run: |
325-
npm ci
326-
npm run build
327-
npm link
328-
- name: Setup Ocean CLI
329-
working-directory: ${{ github.workspace }}/ocean-cli
330-
run: |
331-
npm ci
332-
npm link @oceanprotocol/lib
333-
npm run build
334331
- name: Run system tests
335332
working-directory: ${{ github.workspace }}/ocean-cli
336333
run: npm run test:system

src/OceanNode.ts

Lines changed: 143 additions & 21 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(
@@ -51,21 +60,15 @@ export class OceanNode {
5160
public keyManager?: KeyManager,
5261
public blockchainRegistry?: BlockchainRegistry
5362
) {
54-
if (keyManager) {
55-
this.keyManager = keyManager
56-
} else {
57-
this.keyManager = new KeyManager(config)
58-
}
59-
if (blockchainRegistry) {
60-
this.blockchainRegistry = blockchainRegistry
61-
} else {
62-
this.blockchainRegistry = new BlockchainRegistry(this.keyManager, config)
63-
}
64-
this.coreHandlers = CoreHandlersRegistry.getInstance(this)
63+
this.keyManager = keyManager
64+
this.blockchainRegistry = blockchainRegistry
65+
this.coreHandlers = CoreHandlersRegistry.getInstance(this, true)
6566
this.requestMap = new Map<string, RequestLimiter>()
6667
this.config = config
68+
this.database = db
69+
6770
if (this.db && this.db?.authToken) {
68-
this.auth = new Auth(this.db.authToken)
71+
this.auth = new Auth(this.db.authToken, config)
6972
}
7073
if (node) {
7174
node.setCoreHandlers(this.coreHandlers)
@@ -86,6 +89,7 @@ export class OceanNode {
8689
this.persistentStorage = null
8790
}
8891
}
92+
this.addIndexer(indexer)
8993
}
9094

9195
// Singleton instance
@@ -104,10 +108,19 @@ export class OceanNode {
104108
if (!config) {
105109
throw new Error('KeyManager and BlockchainRegistry are required')
106110
}
107-
keyManager = new KeyManager(config)
108-
blockchainRegistry = new BlockchainRegistry(keyManager, config)
111+
if (!keyManager) keyManager = new KeyManager(config)
112+
if (!blockchainRegistry)
113+
blockchainRegistry = new BlockchainRegistry(keyManager, config)
109114
}
110-
// prepare compute engines
115+
// teardown old instance if needed
116+
this.instance?.tearDownAll().catch((err: unknown) => {
117+
OCEAN_NODE_LOGGER.warn(
118+
`Failed to tear down previous OceanNode instance: ${
119+
err instanceof Error ? err.message : String(err)
120+
}`
121+
)
122+
})
123+
OCEAN_NODE_LOGGER.debug('Creating new OceanNode instance')
111124
this.instance = new OceanNode(
112125
config,
113126
db,
@@ -117,6 +130,8 @@ export class OceanNode {
117130
keyManager,
118131
blockchainRegistry
119132
)
133+
} else {
134+
OCEAN_NODE_LOGGER.debug('Return cached OceanNode instance')
120135
}
121136
return this.instance
122137
}
@@ -127,7 +142,34 @@ export class OceanNode {
127142
}
128143

129144
public addIndexer(_indexer: OceanIndexer) {
145+
const previous = this.indexer
130146
this.indexer = _indexer
147+
if (previous) {
148+
previous.stop().catch((err: unknown) => {
149+
OCEAN_NODE_LOGGER.warn(
150+
`Failed to stop replaced indexer: ${err instanceof Error ? err.message : String(err)}`
151+
)
152+
})
153+
}
154+
}
155+
156+
public async tearDownAll() {
157+
if (this.c2dEngines) {
158+
await this.c2dEngines.stopAllEngines()
159+
this.c2dEngines = null
160+
}
161+
if (this.indexer) {
162+
await this.indexer.stop()
163+
this.indexer = null
164+
}
165+
if (this.blockchainRegistry) {
166+
this.blockchainRegistry.stop()
167+
this.blockchainRegistry = null
168+
}
169+
if (OceanNode.instance === this) {
170+
OceanNode.instance = null
171+
}
172+
OCEAN_NODE_LOGGER.debug('OceanNode instance stopped & cleared')
131173
}
132174

133175
public async addC2DEngines() {
@@ -161,11 +203,7 @@ export class OceanNode {
161203
return this.indexer
162204
}
163205

164-
public getDatabase(): Database {
165-
return this.db
166-
}
167-
168-
public getC2DEngines(): C2DEngines {
206+
public getC2DEngines(): C2DEngines | undefined {
169207
return this.c2dEngines
170208
}
171209

@@ -259,4 +297,88 @@ export class OceanNode {
259297
}
260298
}
261299
}
300+
301+
getAdminAddresses(): { addresses: string[]; accessLists: any } {
302+
const ret = {
303+
addresses: [] as string[],
304+
accessLists: undefined as AccessListContract | undefined
305+
}
306+
307+
if (this.config.allowedAdmins && this.config.allowedAdmins.length > 0) {
308+
for (const admin of this.config.allowedAdmins) {
309+
if (isAddress(admin) === true) {
310+
ret.addresses.push(admin)
311+
}
312+
}
313+
}
314+
ret.accessLists = this.config.allowedAdminsList
315+
return ret
316+
}
317+
318+
checkSupportedChainId(chainId: number): ValidateChainId {
319+
if (!chainId || !(`${chainId.toString()}` in this.config.supportedNetworks)) {
320+
OCEAN_NODE_LOGGER.error(`Chain ID ${chainId} is not supported`)
321+
return {
322+
validation: false,
323+
networkRpc: ''
324+
}
325+
}
326+
return {
327+
validation: true,
328+
networkRpc: this.config.supportedNetworks[chainId.toString()].rpc
329+
}
330+
}
331+
332+
async getJsonRpcProvider(chainId: number): Promise<FallbackProvider> {
333+
const checkResult = this.checkSupportedChainId(chainId)
334+
if (!checkResult.validation) {
335+
return null
336+
}
337+
const blockchain = this.getBlockchain(chainId)
338+
if (!blockchain) return null
339+
return await blockchain.getProvider()
340+
}
341+
342+
hasP2PInterface() {
343+
return this.config.hasP2P || false
344+
}
345+
346+
private dbInitPromise: Promise<Database> | null = null
347+
async getDatabase(forceReload: boolean = false): Promise<Database> {
348+
if (!this.database || forceReload) {
349+
if (!this.dbInitPromise || forceReload) {
350+
const { dbConfig } = this.config
351+
if (dbConfig && dbConfig.url) {
352+
this.dbInitPromise = Database.init(dbConfig).then((db) => {
353+
this.database = db
354+
return db
355+
})
356+
}
357+
}
358+
return await this.dbInitPromise
359+
}
360+
return this.database
361+
}
362+
363+
async getValidationSignature(ddo: string): Promise<any> {
364+
try {
365+
const hashedDDO = create256Hash(ddo)
366+
const providerWallet = await this.keyManager.getEthWallet()
367+
const messageHash = ethers.solidityPackedKeccak256(
368+
['bytes'],
369+
[ethers.hexlify(ethers.toUtf8Bytes(hashedDDO))]
370+
)
371+
const signed32Bytes = await providerWallet.signMessage(
372+
new Uint8Array(ethers.toBeArray(messageHash))
373+
)
374+
const signatureSplitted = ethers.Signature.from(signed32Bytes)
375+
const v = signatureSplitted.v <= 1 ? signatureSplitted.v + 27 : signatureSplitted.v
376+
const r = ethers.hexlify(signatureSplitted.r) // 32 bytes
377+
const s = ethers.hexlify(signatureSplitted.s)
378+
return { hash: hashedDDO, publicKey: providerWallet.address, r, s, v }
379+
} catch (error) {
380+
OCEAN_NODE_LOGGER.logMessage(`Validation signature error: ${error}`, true)
381+
return { hash: '', publicKey: '', r: '', s: '', v: '' }
382+
}
383+
}
262384
}

0 commit comments

Comments
 (0)