Skip to content

Commit

Permalink
different approach for checking emulator api statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
bartolomej committed Nov 2, 2023
1 parent cff992f commit 6471d9f
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 127 deletions.
1 change: 1 addition & 0 deletions apps/electron/src/services/flowser-app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class FlowserAppService {
this.flowCliService = new FlowCliService(this.processManagerService);
this.flowEmulatorService = new FlowEmulatorService(
this.processManagerService,
this.flowGatewayService,
);
this.flowSnapshotsStorageService = new FileStorageService();
this.flowSnapshotsService = new FlowSnapshotsService(
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/flow-gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,21 @@ export class FlowGatewayService {
return { ...account, balance: account.balance / Math.pow(10, 8), address };
}

public async getApiStatus(): Promise<FlowApiStatus> {
public async getRestApiStatus(): Promise<FlowApiStatus> {
const restServerUrl = await fcl.config.get("accessNode.api");

if (!restServerUrl) {
throw new Error("accessNode.api not configured");
}

return this.getApiStatus(restServerUrl);
}

public async getApiStatus(url: string) {
try {
await axios.request({
method: "GET",
url: restServerUrl,
url,
// Prevent axios from throwing on certain http response codes
// https://github.com/axios/axios/issues/41
validateStatus: () => true,
Expand Down
113 changes: 56 additions & 57 deletions packages/core/src/flow-indexer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export class FlowIndexerService {
private accountStorageIndex: IResourceIndex<flowserResource.FlowAccountStorage>,
private flowStorageService: FlowAccountStorageService,
private flowGatewayService: FlowGatewayService,
private flowInteractionsService: IFlowInteractions
private flowInteractionsService: IFlowInteractions,
) {}

async processBlockchainData(): Promise<void> {
const gatewayStatus = await this.flowGatewayService.getApiStatus();
const gatewayStatus = await this.flowGatewayService.getRestApiStatus();

if (gatewayStatus !== FlowApiStatus.SERVICE_STATUS_ONLINE) {
this.logger.debug("Gateway offline, pausing processing.");
Expand Down Expand Up @@ -125,15 +125,15 @@ export class FlowIndexerService {

try {
blockData.transactions.map((transaction) =>
this.subscribeToTransactionStatusUpdates(transaction.id)
this.subscribeToTransactionStatusUpdates(transaction.id),
);
} catch (e) {
this.logger.error("Transaction status update failed", e);
}
}

private async subscribeToTransactionStatusUpdates(
transactionId: string
transactionId: string,
): Promise<void> {
const unsubscribe = this.flowGatewayService
.getTxStatusSubscription(transactionId)
Expand All @@ -145,7 +145,7 @@ export class FlowIndexerService {
grcpStatus: this.reMapGrcpStatus(newStatus.statusCode),
executionStatus: newStatus.status,
},
})
}),
);
try {
await this.flowGatewayService
Expand All @@ -170,29 +170,29 @@ export class FlowIndexerService {
transaction: transaction,
transactionStatus: transaction.status,
}).catch((e: any) =>
this.logger.error(`transaction save error: ${e.message}`, e.stack)
)
)
this.logger.error(`transaction save error: ${e.message}`, e.stack),
),
),
);
const eventPromises = Promise.all(
data.events.map((flowEvent) =>
this.eventIndex
.create(this.createEventEntity(flowEvent))
.catch((e: any) =>
this.logger.error(`event save error: ${e.message}`, e.stack)
)
)
this.logger.error(`event save error: ${e.message}`, e.stack),
),
),
);

const eventProcessingPromises = Promise.all(
data.events.map((event) =>
this.processEvent(event).catch((e) => {
this.logger.error(
`Event handling error: ${e.message} (${JSON.stringify(event)})`,
e.stack
e.stack,
);
})
)
}),
),
);

return Promise.all([
Expand All @@ -207,22 +207,22 @@ export class FlowIndexerService {
const block = await this.flowGatewayService.getBlockByHeight(height);
const collections = await Promise.all(
block.collectionGuarantees.map(async (guarantee) =>
this.flowGatewayService.getCollectionById(guarantee.collectionId)
)
this.flowGatewayService.getCollectionById(guarantee.collectionId),
),
);
const transactionIds = collections
.map((collection) => collection.transactionIds)
.flat();

const transactionFutures = Promise.all(
transactionIds.map((txId) =>
this.flowGatewayService.getTransactionById(txId)
)
this.flowGatewayService.getTransactionById(txId),
),
);
const transactionStatusesFutures = Promise.all(
transactionIds.map((txId) =>
this.flowGatewayService.getTransactionStatusById(txId)
)
this.flowGatewayService.getTransactionStatusById(txId),
),
);

const [transactions, statuses] = await Promise.all([
Expand All @@ -241,7 +241,7 @@ export class FlowIndexerService {
...event,
transactionId: tx.id,
blockId: tx.referenceBlockId,
}))
})),
)
.flat();

Expand Down Expand Up @@ -296,47 +296,47 @@ export class FlowIndexerService {

private async onAccountContractAdded(event: FlowAccountContractEvent) {
const account = await this.flowGatewayService.getAccount(
event.data.address
event.data.address,
);

await this.contractIndex.create(
this.createContractEntity({
account,
name: event.data.contract,
})
}),
);
}

private async onAccountContractUpdated(event: FlowAccountContractEvent) {
const account = await this.flowGatewayService.getAccount(
event.data.address
event.data.address,
);

await this.contractIndex.update(
this.createContractEntity({
account,
name: event.data.contract,
})
}),
);
}

private async onAccountContractRemoved(event: FlowAccountContractEvent) {
const account = await this.flowGatewayService.getAccount(
event.data.address
event.data.address,
);

await this.contractIndex.delete(
this.createContractEntity({
account,
name: event.data.contract,
})
}),
);
}

private async onAccountKeyAddedEvent(event: FlowAccountKeyEvent) {
const publicKey = this.decodeUint8PublicKey(event.data.publicKey.publicKey);
const account = await this.flowGatewayService.getAccount(
event.data.address
event.data.address,
);
const key = account.keys.find((key) => key.publicKey === publicKey);

Expand All @@ -348,7 +348,7 @@ export class FlowIndexerService {
this.createKeyEntity({
key,
address: account.address,
})
}),
);
}

Expand All @@ -371,15 +371,15 @@ export class FlowIndexerService {
transactionStatus: flowResource.FlowTransactionStatus;
}) {
const parsedInteraction = await this.flowInteractionsService.parse(
options.transaction.script
options.transaction.script,
);
if (parsedInteraction.error) {
this.logger.error(
`Unexpected interaction parsing error: ${parsedInteraction.error}`
`Unexpected interaction parsing error: ${parsedInteraction.error}`,
);
}
return this.transactionIndex.create(
this.createTransactionEntity({ ...options, parsedInteraction })
this.createTransactionEntity({ ...options, parsedInteraction }),
);
}

Expand All @@ -403,19 +403,19 @@ export class FlowIndexerService {
this.createKeyEntity({
address,
key,
})
)
)
}),
),
),
),
Promise.all(
Object.keys(account.contracts).map((name) =>
this.contractIndex.create(
this.createContractEntity({
account,
name,
})
)
)
}),
),
),
),
]);
}
Expand All @@ -425,36 +425,35 @@ export class FlowIndexerService {
this.logger.debug(
`Processing storages for accounts: ${allAccounts
.map((e) => e.address)
.join(", ")}`
.join(", ")}`,
);
await Promise.all(
allAccounts.map((account: flowserResource.FlowAccount) =>
this.reIndexAccountStorage(account.address)
)
this.reIndexAccountStorage(account.address),
),
);
}

private async reIndexAccountStorage(address: string) {
const storages = await this.flowStorageService.getAccountStorageItems(
address
);
const storages =
await this.flowStorageService.getAccountStorageItems(address);
return Promise.all(
storages.map((storage) => this.accountStorageIndex.upsert(storage))
storages.map((storage) => this.accountStorageIndex.upsert(storage)),
);
}

private async maybeProcessWellKnownAccounts() {
await Promise.all(
this.getAllWellKnownAddresses()
.filter(
(address) => this.accountIndex.findOneById(address) !== undefined
(address) => this.accountIndex.findOneById(address) !== undefined,
)
.map((address) =>
this.createAccount(address).catch((error) => {
// Most likely an account not found error monotonic/non-monotonic addresses.
// Can be safely ignored.
})
)
}),
),
);
}

Expand Down Expand Up @@ -488,7 +487,7 @@ export class FlowIndexerService {
}

private createAccountEntity(
account: flowResource.FlowAccount
account: flowResource.FlowAccount,
): OmitTimestamps<flowserResource.FlowAccount> {
const address = ensurePrefixedAddress(account.address);

Expand Down Expand Up @@ -564,7 +563,7 @@ export class FlowIndexerService {
}

private createEventEntity(
event: flowResource.FlowEvent
event: flowResource.FlowEvent,
): OmitTimestamps<flowserResource.FlowEvent> {
return {
id: `${event.transactionId}.${event.eventIndex}`,
Expand Down Expand Up @@ -623,7 +622,7 @@ export class FlowIndexerService {
// But we don't need the type info since we already have
// our own system of representing types with `CadenceType` message.
function fromTypeAnnotatedFclArguments(
object: flowResource.FlowTypeAnnotatedValue
object: flowResource.FlowTypeAnnotatedValue,
): FclValue {
const { type, value } = object;
// Available type values are listed here:
Expand All @@ -640,7 +639,7 @@ export class FlowIndexerService {
// TODO(restructure): Double check this
// @ts-ignore
return value?.map((element: any) =>
fromTypeAnnotatedFclArguments(element)
fromTypeAnnotatedFclArguments(element),
);
case "Path":
case "PublicPath":
Expand All @@ -660,25 +659,25 @@ export class FlowIndexerService {
referenceBlockId: transaction.referenceBlockId,
gasLimit: transaction.gasLimit,
authorizers: transaction.authorizers.map((address) =>
ensurePrefixedAddress(address)
ensurePrefixedAddress(address),
),
arguments:
parsedInteraction.interaction?.parameters.map(
(parameter, index): flowserResource.FclArgumentWithMetadata => ({
identifier: parameter.identifier,
type: parameter.type,
value: fromTypeAnnotatedFclArguments(transaction.args[index]),
})
}),
) ?? [],
proposalKey: {
...transaction.proposalKey,
address: ensurePrefixedAddress(transaction.proposalKey.address),
},
envelopeSignatures: this.deserializeSignableObjects(
transaction.envelopeSignatures
transaction.envelopeSignatures,
),
payloadSignatures: this.deserializeSignableObjects(
transaction.payloadSignatures
transaction.payloadSignatures,
),
status: {
errorMessage: transactionStatus.errorMessage,
Expand All @@ -695,7 +694,7 @@ export class FlowIndexerService {
}

private deserializeSignableObjects(
signableObjects: flowResource.FlowSignableObject[]
signableObjects: flowResource.FlowSignableObject[],
): flowserResource.SignableObject[] {
return signableObjects.map((signable) => ({
...signable,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/flow-snapshots.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export class FlowSnapshotsService extends EventEmitter {
// Ensure we exclude the ones that were deleted without using Flowser.
await this.persistAll(
persistedSnapshots.filter((snapshot) =>
validSnapshotIdLookup.has(snapshot.id)
)
validSnapshotIdLookup.has(snapshot.id),
),
);
}

Expand Down
Loading

0 comments on commit 6471d9f

Please sign in to comment.