Skip to content

Commit

Permalink
Merge pull request #57 from logion-network/feature/avoid-sync-error-log
Browse files Browse the repository at this point in the history
Avoid syncing extrinsics with errors; add logging.
  • Loading branch information
benoitdevos authored Dec 6, 2021
2 parents 8529789 + 3b3b25f commit 21f51c0
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/logion/services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class BlockExtrinsicsService {
const api = await this.polkadotService.readyApi();
const hash = await api.rpc.chain.getBlockHash(blockNumber);
const { block, events } = await this.blockAndEvents(hash);
const extrinsics = await this.successfulExtrinsics(block, events);
const extrinsics = await this.allExtrinsics(block, events);
return {
number: BigInt(block.header.number.toString()),
extrinsics
Expand All @@ -65,7 +65,7 @@ export class BlockExtrinsicsService {
};
}

private async successfulExtrinsics(
private async allExtrinsics(
block: Block,
events: Vec<EventRecord>,
): Promise<JsonExtrinsic[]> {
Expand Down
23 changes: 18 additions & 5 deletions src/logion/services/blockconsumption.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { LocSynchronizer } from "./locsynchronization.service";
import { Log } from "../util/Log";
import { TransactionSynchronizer } from "./transactionsync.service";
import { ProtectionSynchronizer } from "./protectionsynchronization.service";
import { ExtrinsicDataExtractor } from "./extrinsic.data.extractor";
import { toString } from "./types/responses/Extrinsic";

const { logger } = Log;

Expand All @@ -19,6 +21,7 @@ export class BlockConsumer {
private transactionSynchronizer: TransactionSynchronizer,
private locSynchronizer: LocSynchronizer,
private protectionSynchronizer: ProtectionSynchronizer,
private extrinsicDataExtractor: ExtrinsicDataExtractor,
) {}

async consumeNewBlocks(now: Moment): Promise<void> {
Expand Down Expand Up @@ -56,13 +59,23 @@ export class BlockConsumer {
updatedOn: now
});
}
this.syncPointRepository.save(lastSyncPoint);
await this.syncPointRepository.save(lastSyncPoint);
}

private async processBlock(blockNumber: bigint): Promise<void> {
const block = await this.blockService.getBlockExtrinsics(blockNumber);
await this.transactionSynchronizer.addTransactions(block);
await this.locSynchronizer.updateLocRequests(block);
await this.protectionSynchronizer.updateProtectionRequests(block);
const block = await this.blockService.getBlockExtrinsics(blockNumber);
const timestamp = this.extrinsicDataExtractor.getBlockTimestamp(block);
if (timestamp === undefined) {
throw Error("Block has no timestamp");
}
await this.transactionSynchronizer.addTransactions(block);
for (let i = 0; i < block.extrinsics.length; ++i) {
const extrinsic = block.extrinsics[i];
if (extrinsic.method.pallet !== "timestamp") {
logger.info("Processing extrinsic: %s", toString(extrinsic))
await this.locSynchronizer.updateLocRequests(extrinsic, timestamp);
await this.protectionSynchronizer.updateProtectionRequests(extrinsic);
}
}
}
}
81 changes: 41 additions & 40 deletions src/logion/services/locsynchronization.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { injectable } from 'inversify';
import { LocRequestAggregateRoot, LocRequestRepository } from '../model/locrequest.model';
import { ExtrinsicDataExtractor } from "../services/extrinsic.data.extractor";
import { ExtrinsicDataExtractor } from "./extrinsic.data.extractor";
import { decimalToUuid } from '../lib/uuid';

import { BlockExtrinsics } from './types/responses/Block';
import { JsonArgs } from './call';
import { JsonExtrinsic, toString } from "./types/responses/Extrinsic";
import { Moment } from "moment";
import { Log } from "../util/Log";

const { logger } = Log;

@injectable()
export class LocSynchronizer {
Expand All @@ -14,43 +17,40 @@ export class LocSynchronizer {
private locRequestRepository: LocRequestRepository,
) {}

async updateLocRequests(block: BlockExtrinsics): Promise<void> {
const timestamp = this.extrinsicDataExtractor.getBlockTimestamp(block);
if(timestamp === undefined) {
throw Error("Block has no timestamp");
}
for(let i = 0; i < block.extrinsics.length; ++i) {
const extrinsic = block.extrinsics[i];
if(extrinsic.method.pallet === "logionLoc") {
if(extrinsic.method.method === "createLoc") {
const locId = this.extractLocId(extrinsic.args);
await this.mutateLoc(locId, loc => loc.setLocCreatedDate(timestamp));
} else if(extrinsic.method.method === "addMetadata") {
const locId = this.extractLocId(extrinsic.args);
const item = {
name: extrinsic.args['item'].name.toUtf8(),
value: extrinsic.args['item'].value.toUtf8(),
addedOn: timestamp,
};
await this.mutateLoc(locId, loc => loc.addMetadataItem(item));
} else if(extrinsic.method.method === "addFile") {
const locId = this.extractLocId(extrinsic.args);
const hash = extrinsic.args['file'].get('hash').toHex();
await this.mutateLoc(locId, loc => loc.setFileAddedOn(hash, timestamp));
} else if(extrinsic.method.method === "addLink") {
const locId = this.extractLocId(extrinsic.args);
const link = {
target: decimalToUuid(extrinsic.args['link'].id.toString()),
addedOn: timestamp,
};
await this.mutateLoc(locId, loc => loc.addLink(link));
} else if(extrinsic.method.method === "close") {
const locId = this.extractLocId(extrinsic.args);
await this.mutateLoc(locId, loc => loc.close(timestamp));
} else if(extrinsic.method.method === "makeVoid" || extrinsic.method.method === "makeVoidAndReplace") {
const locId = this.extractLocId(extrinsic.args);
await this.mutateLoc(locId, loc => loc.voidLoc(timestamp));
}
async updateLocRequests(extrinsic: JsonExtrinsic, timestamp: Moment) {
if (extrinsic.method.pallet === "logionLoc") {
if (extrinsic.error) {
logger.info("updateLocRequests() - Skipping extrinsic with error: %s", toString(extrinsic))
return
}
if (extrinsic.method.method === "createLoc") {
const locId = this.extractLocId(extrinsic.args);
await this.mutateLoc(locId, loc => loc.setLocCreatedDate(timestamp));
} else if (extrinsic.method.method === "addMetadata") {
const locId = this.extractLocId(extrinsic.args);
const item = {
name: extrinsic.args['item'].name.toUtf8(),
value: extrinsic.args['item'].value.toUtf8(),
addedOn: timestamp,
};
await this.mutateLoc(locId, loc => loc.addMetadataItem(item));
} else if (extrinsic.method.method === "addFile") {
const locId = this.extractLocId(extrinsic.args);
const hash = extrinsic.args['file'].get('hash').toHex();
await this.mutateLoc(locId, loc => loc.setFileAddedOn(hash, timestamp));
} else if (extrinsic.method.method === "addLink") {
const locId = this.extractLocId(extrinsic.args);
const link = {
target: decimalToUuid(extrinsic.args['link'].id.toString()),
addedOn: timestamp,
};
await this.mutateLoc(locId, loc => loc.addLink(link));
} else if (extrinsic.method.method === "close") {
const locId = this.extractLocId(extrinsic.args);
await this.mutateLoc(locId, loc => loc.close(timestamp));
} else if (extrinsic.method.method === "makeVoid" || extrinsic.method.method === "makeVoidAndReplace") {
const locId = this.extractLocId(extrinsic.args);
await this.mutateLoc(locId, loc => loc.voidLoc(timestamp));
}
}
}
Expand All @@ -62,6 +62,7 @@ export class LocSynchronizer {
private async mutateLoc(locId: string, mutator: (loc: LocRequestAggregateRoot) => void) {
const loc = await this.locRequestRepository.findById(locId);
if(loc !== undefined) {
logger.info("Mutating LOC %s : %s", locId, mutator)
mutator(loc);
await this.locRequestRepository.save(loc);
}
Expand Down
44 changes: 22 additions & 22 deletions src/logion/services/protectionsynchronization.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { injectable } from 'inversify';
import { ProtectionRequestRepository, FetchProtectionRequestsSpecification } from '../model/protectionrequest.model';
import { ExtrinsicDataExtractor } from "../services/extrinsic.data.extractor";

import { BlockExtrinsics } from './types/responses/Block';
import { ExtrinsicDataExtractor } from "./extrinsic.data.extractor";
import { JsonArgs } from './call';
import { JsonExtrinsic, toString } from "./types/responses/Extrinsic";
import { Log } from "../util/Log";

const { logger } = Log;

@injectable()
export class ProtectionSynchronizer {
Expand All @@ -20,25 +22,23 @@ export class ProtectionSynchronizer {

readonly nodeOwner: string;

async updateProtectionRequests(block: BlockExtrinsics): Promise<void> {
const timestamp = this.extrinsicDataExtractor.getBlockTimestamp(block);
if(timestamp === undefined) {
throw Error("Block has no timestamp");
}
for(let i = 0; i < block.extrinsics.length; ++i) {
const extrinsic = block.extrinsics[i];
if(extrinsic.method.pallet === "verifiedRecovery") {
if(extrinsic.method.method === "createRecovery" && this.nodeOwnerInFriends(extrinsic.args)) {
const signer = extrinsic.signer!;
const requests = await this.protectionRequestRepository.findBy(new FetchProtectionRequestsSpecification({
expectedRequesterAddress: signer,
expectedStatuses: [ 'ACCEPTED' ],
}));
for(let j = 0; j < requests.length; ++j) {
const request = requests[j];
request.setActivated();
await this.protectionRequestRepository.save(request);
}
async updateProtectionRequests(extrinsic: JsonExtrinsic): Promise<void> {
if (extrinsic.method.pallet === "verifiedRecovery") {
if (extrinsic.error) {
logger.info("updateProtectionRequests() - Skipping extrinsic with error: %s", toString(extrinsic))
return
}
if (extrinsic.method.method === "createRecovery" && this.nodeOwnerInFriends(extrinsic.args)) {
const signer = extrinsic.signer!;
const requests = await this.protectionRequestRepository.findBy(new FetchProtectionRequestsSpecification({
expectedRequesterAddress: signer,
expectedStatuses: [ 'ACCEPTED' ],
}));
for (let j = 0; j < requests.length; ++j) {
const request = requests[j];
logger.info("Setting protection %s activated", request.id)
request.setActivated();
await this.protectionRequestRepository.save(request);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/logion/services/transaction.extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class TransactionExtractor {
}
const blockBuilder = BlockWithTransactions.builder()
.blockNumber(block.number);
logger.debug("Looking at block %d", block.number);
logger.info("Looking at block %d", block.number);
const transactions: Transaction[] = [];
for (let index = 0; index < block.extrinsics.length; index++) {
const extrinsic = block.extrinsics[index];
Expand Down
16 changes: 16 additions & 0 deletions src/logion/services/types/responses/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,29 @@ export interface JsonExtrinsic {
error: ExtrinsicError | null;
}

export function toString(extrinsic: JsonExtrinsic): string {
return `extrinsic ${ methodToString(extrinsic.method) } ${ errorToString(extrinsic.error) }`
}

export interface JsonEvent {
method: JsonMethod;
data: string[];
}

function methodToString(method: JsonMethod): string {
return `method [${method.pallet}.${method.method}]`
}

export interface ExtrinsicError {
section: string
name: string
details: string
}

function errorToString(error: ExtrinsicError | null): string {
if (error) {
return `error [${ error.section }.${ error.name }]`
} else {
return "";
}
}
Loading

0 comments on commit 21f51c0

Please sign in to comment.