Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ledger connections happen on agent init in background #580

Merged
9 changes: 8 additions & 1 deletion packages/core/src/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class Agent {
}

public async initialize() {
const { publicDidSeed, walletConfig, mediatorConnectionsInvite } = this.agentConfig
const { connectToIndyLedgersOnStartup, publicDidSeed, walletConfig, mediatorConnectionsInvite } = this.agentConfig

if (this._isInitialized) {
throw new AriesFrameworkError(
Expand All @@ -161,6 +161,13 @@ export class Agent {
await this.wallet.initPublicDid({ seed: publicDidSeed })
}

// As long as value isn't false we will async connect to all genesis pools on startup
if (connectToIndyLedgersOnStartup) {
this.ledger.connectToPools().catch((error) => {
this.logger.warn('Error connecting to ledger, will try to reconnect when needed.', { error })
})
}

for (const transport of this.inboundTransports) {
transport.start(this)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/agent/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export class AgentConfig {
}
}

public get connectToIndyLedgersOnStartup() {
return this.initConfig.connectToIndyLedgersOnStartup ?? true
}

public get publicDidSeed() {
return this.initConfig.publicDidSeed
}
Expand Down
25 changes: 25 additions & 0 deletions packages/core/src/modules/ledger/IndyPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class IndyPool {
private fileSystem: FileSystem
private poolConfig: IndyPoolConfig
private _poolHandle?: number
private poolConnected?: Promise<number>
public authorAgreement?: AuthorAgreement | null

public constructor(agentConfig: AgentConfig, poolConfig: IndyPoolConfig) {
Expand Down Expand Up @@ -74,6 +75,21 @@ export class IndyPool {
}

public async connect() {
if (!this.poolConnected) {
// Save the promise of connectToLedger to determine if we are done connecting
this.poolConnected = this.connectToLedger()
this.poolConnected.catch((error) => {
// Set poolConnected to undefined so we can retry connection upon failure
this.poolConnected = undefined
this.logger.error('Connection to pool: ' + this.poolConfig.genesisPath + ' failed.', { error })
})
return this.poolConnected
} else {
throw new AriesFrameworkError('Cannot attempt connection to ledger, already connecting.')
}
}

private async connectToLedger() {
const poolName = this.poolConfig.id
const genesisPath = await this.getGenesisPath()

Expand Down Expand Up @@ -130,6 +146,15 @@ export class IndyPool {
}

private async getPoolHandle() {
if (this.poolConnected) {
// If we have tried to already connect to pool wait for it
try {
await this.poolConnected
} catch (error) {
this.logger.error('Connection to pool: ' + this.poolConfig.genesisPath + ' failed.', { error })
}
}

if (!this._poolHandle) {
return this.connect()
}
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/modules/ledger/LedgerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export class LedgerModule {
this.wallet = wallet
}

/**
* Connect to all the ledger pools
*/
public async connectToPools() {
await this.ledgerService.connectToPools()
}

public async registerPublicDid(did: string, verkey: string, alias: string, role?: NymRole) {
const myPublicDid = this.wallet.publicDid?.did

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class IndyLedgerService {
this.indyPoolService = indyPoolService
}

public async connectToPools() {
return this.indyPoolService.connectToPools()
}

public async registerPublicDid(
submitterDid: string,
targetDid: string,
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/modules/ledger/services/IndyPoolService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ export class IndyPoolService {
this.didCache = new PersistedLruCache(DID_POOL_CACHE_ID, DID_POOL_CACHE_LIMIT, cacheRepository)
}

/**
* Create connections to all ledger pools
*/
public async connectToPools() {
const poolsPromises = this.pools.map((pool) => {
return pool.connect()
})
return Promise.all(poolsPromises)
}

/**
* Get the pool used for writing to the ledger. For now we always use the first pool
* as the pool that writes to the ledger
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface InitConfig {
didCommMimeType?: DidCommMimeType

indyLedgers?: IndyPoolConfig[]
connectToIndyLedgersOnStartup?: boolean

autoAcceptMediationRequests?: boolean
mediatorConnectionsInvite?: string
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"devDependencies": {
"@types/indy-sdk-react-native": "npm:@types/indy-sdk@^1.16.6",
"@types/react-native": "^0.64.10",
"indy-sdk-react-native": "^0.1.13",
"indy-sdk-react-native": "^0.1.16",
"react": "17.0.1",
"react-native": "0.64.2",
"react-native-fs": "^2.18.0",
Expand All @@ -40,7 +40,7 @@
"typescript": "~4.3.0"
},
"peerDependencies": {
"indy-sdk-react-native": "^0.1.13",
"indy-sdk-react-native": "^0.1.16",
"react-native-fs": "^2.18.0",
"react-native-get-random-values": "^1.7.0"
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5229,10 +5229,10 @@ indent-string@^4.0.0:
resolved "https://registry.yarnpkg.com/indent-string/-/indent-string-4.0.0.tgz#624f8f4497d619b2d9768531d58f4122854d7251"
integrity sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg==

indy-sdk-react-native@^0.1.13:
version "0.1.13"
resolved "https://registry.yarnpkg.com/indy-sdk-react-native/-/indy-sdk-react-native-0.1.13.tgz#7a131541f21d4fa5091d214ed75f2235977512e9"
integrity sha512-V0vfR6y8rOy9efc0An5FqSxeiynYFXrOd2tcXYVAA2iX5Y6OuOmbzAGHTTMfiagZaLyE+/1j7DSSaYlXo76VYA==
indy-sdk-react-native@^0.1.16:
version "0.1.16"
resolved "https://registry.yarnpkg.com/indy-sdk-react-native/-/indy-sdk-react-native-0.1.16.tgz#db956168cdd27e569c45ac88e8c12baa7a6ac85e"
integrity sha512-vsYfq/TNZbF3AYALySNrhtONnSKIHsPdFMo9LSlUNrNZEBQnnR0aZKjAnRSLSYXUo6hF5Njc+AJFpwawWLZqkg==
dependencies:
buffer "^6.0.2"

Expand Down