diff --git a/src/logion/services/block.service.ts b/src/logion/services/block.service.ts index 5b680589..34a4331c 100644 --- a/src/logion/services/block.service.ts +++ b/src/logion/services/block.service.ts @@ -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 @@ -65,7 +65,7 @@ export class BlockExtrinsicsService { }; } - private async successfulExtrinsics( + private async allExtrinsics( block: Block, events: Vec, ): Promise { diff --git a/src/logion/services/blockconsumption.service.ts b/src/logion/services/blockconsumption.service.ts index aadc2766..1390cc53 100644 --- a/src/logion/services/blockconsumption.service.ts +++ b/src/logion/services/blockconsumption.service.ts @@ -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; @@ -19,6 +21,7 @@ export class BlockConsumer { private transactionSynchronizer: TransactionSynchronizer, private locSynchronizer: LocSynchronizer, private protectionSynchronizer: ProtectionSynchronizer, + private extrinsicDataExtractor: ExtrinsicDataExtractor, ) {} async consumeNewBlocks(now: Moment): Promise { @@ -56,13 +59,23 @@ export class BlockConsumer { updatedOn: now }); } - this.syncPointRepository.save(lastSyncPoint); + await this.syncPointRepository.save(lastSyncPoint); } private async processBlock(blockNumber: bigint): Promise { - 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); + } + } } } diff --git a/src/logion/services/locsynchronization.service.ts b/src/logion/services/locsynchronization.service.ts index 36581e7a..dbc08b45 100644 --- a/src/logion/services/locsynchronization.service.ts +++ b/src/logion/services/locsynchronization.service.ts @@ -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 { @@ -14,43 +17,40 @@ export class LocSynchronizer { private locRequestRepository: LocRequestRepository, ) {} - async updateLocRequests(block: BlockExtrinsics): Promise { - 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)); } } } @@ -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); } diff --git a/src/logion/services/protectionsynchronization.service.ts b/src/logion/services/protectionsynchronization.service.ts index e0306885..70793920 100644 --- a/src/logion/services/protectionsynchronization.service.ts +++ b/src/logion/services/protectionsynchronization.service.ts @@ -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 { @@ -20,25 +22,23 @@ export class ProtectionSynchronizer { readonly nodeOwner: string; - async updateProtectionRequests(block: BlockExtrinsics): Promise { - 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 { + 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); } } } diff --git a/src/logion/services/transaction.extractor.ts b/src/logion/services/transaction.extractor.ts index 8932d57a..99a6cb76 100644 --- a/src/logion/services/transaction.extractor.ts +++ b/src/logion/services/transaction.extractor.ts @@ -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]; diff --git a/src/logion/services/types/responses/Extrinsic.ts b/src/logion/services/types/responses/Extrinsic.ts index f1fbf3cd..d1ab810a 100644 --- a/src/logion/services/types/responses/Extrinsic.ts +++ b/src/logion/services/types/responses/Extrinsic.ts @@ -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 ""; + } +} diff --git a/test/unit/services/blockconsumption.service.spec.ts b/test/unit/services/blockconsumption.service.spec.ts index a534dde4..f9269afd 100644 --- a/test/unit/services/blockconsumption.service.spec.ts +++ b/test/unit/services/blockconsumption.service.spec.ts @@ -8,6 +8,8 @@ import { BlockConsumer } from "../../../src/logion/services/blockconsumption.ser import { LocSynchronizer } from "../../../src/logion/services/locsynchronization.service"; import { TransactionSynchronizer } from "../../../src/logion/services/transactionsync.service"; import { ProtectionSynchronizer } from "../../../src/logion/services/protectionsynchronization.service"; +import { ExtrinsicDataExtractor } from "../../../src/logion/services/extrinsic.data.extractor"; +import { JsonExtrinsic } from "../../../src/logion/services/types/responses/Extrinsic"; describe("BlockConsumer", () => { @@ -18,6 +20,7 @@ describe("BlockConsumer", () => { transactionSynchronizer = new Mock(); locSynchronizer = new Mock(); protectionSynchronizer = new Mock(); + extrinsicDataExtractor = new Mock(); }); let blockService: Mock; @@ -26,6 +29,7 @@ describe("BlockConsumer", () => { let transactionSynchronizer: Mock; let locSynchronizer: Mock; let protectionSynchronizer: Mock; + let extrinsicDataExtractor: Mock; it("does nothing given up to date", async () => { // Given @@ -55,6 +59,7 @@ describe("BlockConsumer", () => { transactionSynchronizer.object(), locSynchronizer.object(), protectionSynchronizer.object(), + extrinsicDataExtractor.object(), ); await transactionSync.consumeNewBlocks(moment()); } @@ -68,6 +73,11 @@ describe("BlockConsumer", () => { const block = new Mock(); blockService.setup(instance => instance.getBlockExtrinsics(It.IsAny())) .returns(Promise.resolve(block.object())); + const extrinsic = new Mock(); + extrinsic.setup(instance => instance.method).returns({ method: "method", pallet: "pallet" }) + block.setup(instance => instance.extrinsics).returns([ extrinsic.object() ]); + const timestamp = moment(); + extrinsicDataExtractor.setup(instance => instance.getBlockTimestamp(block.object())).returns(timestamp) const syncPoint = new Mock(); syncPoint.setup(instance => instance.latestHeadBlockNumber).returns((head - n).toString()); @@ -78,8 +88,8 @@ describe("BlockConsumer", () => { .returns(Promise.resolve()); transactionSynchronizer.setup(instance => instance.addTransactions(block.object())).returns(Promise.resolve()); - locSynchronizer.setup(instance => instance.updateLocRequests(block.object())).returns(Promise.resolve()); - protectionSynchronizer.setup(instance => instance.updateProtectionRequests(block.object())).returns(Promise.resolve()); + locSynchronizer.setup(instance => instance.updateLocRequests(extrinsic.object(), timestamp)).returns(Promise.resolve()); + protectionSynchronizer.setup(instance => instance.updateProtectionRequests(extrinsic.object())).returns(Promise.resolve()); // When await consumeNewBlocks(); @@ -93,8 +103,51 @@ describe("BlockConsumer", () => { syncPointRepository.verify(instance => instance.save(syncPoint.object())); transactionSynchronizer.verify(instance => instance.addTransactions(block.object()), Times.Exactly(5)); - locSynchronizer.verify(instance => instance.updateLocRequests(block.object()), Times.Exactly(5)); - protectionSynchronizer.verify(instance => instance.updateProtectionRequests(block.object()), Times.Exactly(5)); + locSynchronizer.verify(instance => instance.updateLocRequests(extrinsic.object(), timestamp), Times.Exactly(5)); + protectionSynchronizer.verify(instance => instance.updateProtectionRequests(extrinsic.object()), Times.Exactly(5)); + }); + + it("consumes extrinsics with errors", async () => { + // Given + const head = 10002n; + blockService.setup(instance => instance.getHeadBlockNumber()).returns(Promise.resolve(head)); + + const block = new Mock(); + blockService.setup(instance => instance.getBlockExtrinsics(It.IsAny())) + .returns(Promise.resolve(block.object())); + const extrinsic = new Mock(); + extrinsic.setup(instance => instance.method).returns({ method: "method", pallet: "pallet" }) + extrinsic.setup(instance => instance.error).returns({ section: "errorSection", name: "error", details: "An error occurred." }) + block.setup(instance => instance.extrinsics).returns([ extrinsic.object() ]); + const timestamp = moment(); + extrinsicDataExtractor.setup(instance => instance.getBlockTimestamp(block.object())).returns(timestamp) + + const syncPoint = new Mock(); + syncPoint.setup(instance => instance.latestHeadBlockNumber).returns((head - 1n).toString()); + syncPoint.setup(instance => instance.update(It.IsAny())).returns(); + syncPointRepository.setup(instance => instance.findByName(TRANSACTIONS_SYNC_POINT_NAME)) + .returns(Promise.resolve(syncPoint.object())); + syncPointRepository.setup(instance => instance.save(syncPoint.object())) + .returns(Promise.resolve()); + + transactionSynchronizer.setup(instance => instance.addTransactions(block.object())).returns(Promise.resolve()); + locSynchronizer.setup(instance => instance.updateLocRequests(extrinsic.object(), timestamp)).returns(Promise.resolve()); + protectionSynchronizer.setup(instance => instance.updateProtectionRequests(extrinsic.object())).returns(Promise.resolve()); + + // When + await consumeNewBlocks(); + + // Then + blockService.verify(instance => instance.getHeadBlockNumber()); + syncPointRepository.verify(instance => instance.findByName(TRANSACTIONS_SYNC_POINT_NAME)); + blockService.verify(instance => instance.getBlockExtrinsics(It.Is(_ => true)), Times.Exactly(1)); + syncPoint.verify(instance => instance.update( + It.Is<{blockNumber: bigint}>(value => value.blockNumber === head))); + syncPointRepository.verify(instance => instance.save(syncPoint.object())); + + transactionSynchronizer.verify(instance => instance.addTransactions(block.object()), Times.Exactly(1)); + locSynchronizer.verify(instance => instance.updateLocRequests(extrinsic.object(), timestamp), Times.Exactly(1)); + protectionSynchronizer.verify(instance => instance.updateProtectionRequests(extrinsic.object()), Times.Exactly(1)); }); it("deletes all and restarts given out of sync", async () => { @@ -105,6 +158,11 @@ describe("BlockConsumer", () => { const block = new Mock(); blockService.setup(instance => instance.getBlockExtrinsics(It.IsAny())) .returns(Promise.resolve(block.object())); + const extrinsic = new Mock(); + extrinsic.setup(instance => instance.method).returns({ method: "method", pallet: "pallet" }) + block.setup(instance => instance.extrinsics).returns([ extrinsic.object() ]); + const timestamp = moment(); + extrinsicDataExtractor.setup(instance => instance.getBlockTimestamp(block.object())).returns(timestamp) const syncPoint = new Mock(); syncPoint.setup(instance => instance.latestHeadBlockNumber).returns(789789n.toString()); @@ -121,8 +179,8 @@ describe("BlockConsumer", () => { transactionSynchronizer.setup(instance => instance.reset()).returns(Promise.resolve()); transactionSynchronizer.setup(instance => instance.addTransactions(block.object())).returns(Promise.resolve()); - locSynchronizer.setup(instance => instance.updateLocRequests(block.object())).returns(Promise.resolve()); - protectionSynchronizer.setup(instance => instance.updateProtectionRequests(block.object())).returns(Promise.resolve()); + locSynchronizer.setup(instance => instance.updateLocRequests(extrinsic.object(), timestamp)).returns(Promise.resolve()); + protectionSynchronizer.setup(instance => instance.updateProtectionRequests(extrinsic.object())).returns(Promise.resolve()); // When await consumeNewBlocks(); diff --git a/test/unit/services/locsynchronization.service.spec.ts b/test/unit/services/locsynchronization.service.spec.ts index a4311c48..fa1227a2 100644 --- a/test/unit/services/locsynchronization.service.spec.ts +++ b/test/unit/services/locsynchronization.service.spec.ts @@ -1,10 +1,13 @@ import moment, { Moment } from 'moment'; import { It, Mock } from 'moq.ts'; - -import { BlockExtrinsics } from '../../../src/logion/services/types/responses/Block'; import { LocSynchronizer } from "../../../src/logion/services/locsynchronization.service"; import { ExtrinsicDataExtractor } from '../../../src/logion/services/extrinsic.data.extractor'; -import { LinkDescription, LocRequestAggregateRoot, LocRequestRepository, MetadataItemDescription } from '../../../src/logion/model/locrequest.model'; +import { + LinkDescription, + LocRequestAggregateRoot, + LocRequestRepository, + MetadataItemDescription +} from '../../../src/logion/model/locrequest.model'; import { JsonExtrinsic } from '../../../src/logion/services/types/responses/Extrinsic'; import { JsonArgs } from '../../../src/logion/services/call'; import { decimalToUuid } from '../../../src/logion/lib/uuid'; @@ -18,7 +21,6 @@ describe("LocSynchronizer", () => { it("sets LOC created date", async () => { givenLocExtrinsic("createLoc", { loc_id: locId }); - givenBlock(); givenLocRequest(); givenLocRequestExpectsLocCreationDate(); await whenConsumingBlock(); @@ -38,7 +40,6 @@ describe("LocSynchronizer", () => { }, } }); - givenBlock(); givenLocRequest(); givenLocRequestExpectsMetadata(); await whenConsumingBlock(); @@ -48,7 +49,6 @@ describe("LocSynchronizer", () => { it("closes LOC", async () => { givenLocExtrinsic("close", { loc_id: locId }); - givenBlock(); givenLocRequest(); givenLocRequestExpectsClose(); await whenConsumingBlock(); @@ -68,7 +68,6 @@ describe("LocSynchronizer", () => { }, } }); - givenBlock(); givenLocRequest(); givenLocRequestExpectsLink(); await whenConsumingBlock(); @@ -78,7 +77,6 @@ describe("LocSynchronizer", () => { it("voids LOC on makeVoid", async () => { givenLocExtrinsic("makeVoid", { loc_id: locId }); - givenBlock(); givenLocRequest(); givenLocRequestExpectsVoid(); await whenConsumingBlock(); @@ -88,7 +86,6 @@ describe("LocSynchronizer", () => { it("voids LOC on makeVoidAndReplace", async () => { givenLocExtrinsic("makeVoidAndReplace", { loc_id: locId }); - givenBlock(); givenLocRequest(); givenLocRequestExpectsVoid(); await whenConsumingBlock(); @@ -116,15 +113,6 @@ function givenLocExtrinsic(method: string, args: JsonArgs) { let locExtrinsic: Mock; -function givenBlock() { - block = new Mock(); - extrinsicDataExtractor.setup(instance => instance.getBlockTimestamp(block.object())).returns(blockTimestamp); - const extrinsics: JsonExtrinsic[] = [ locExtrinsic.object() ]; - block.setup(instance => instance.extrinsics).returns(extrinsics); -} - -let block: Mock; - function givenLocRequest() { locRequest = new Mock(); locRequestRepository.setup(instance => instance.findById(decimalToUuid(locDecimalUuid))).returns(Promise.resolve(locRequest.object())); @@ -141,7 +129,7 @@ function givenLocRequestExpectsLocCreationDate() { const IS_BLOCK_TIME = It.Is(time => time.isSame(blockTimestamp)); async function whenConsumingBlock() { - await locSynchronizer().updateLocRequests(block.object()); + await locSynchronizer().updateLocRequests(locExtrinsic.object(), blockTimestamp); } function locSynchronizer(): LocSynchronizer { diff --git a/test/unit/services/protectionsynchronization.service.spec.ts b/test/unit/services/protectionsynchronization.service.spec.ts index 01cda2ec..2c6f1966 100644 --- a/test/unit/services/protectionsynchronization.service.spec.ts +++ b/test/unit/services/protectionsynchronization.service.spec.ts @@ -1,9 +1,10 @@ -import moment, { Moment } from 'moment'; import { It, Mock } from 'moq.ts'; - -import { BlockExtrinsics } from '../../../src/logion/services/types/responses/Block'; import { ExtrinsicDataExtractor } from '../../../src/logion/services/extrinsic.data.extractor'; -import { FetchProtectionRequestsSpecification, ProtectionRequestAggregateRoot, ProtectionRequestRepository } from '../../../src/logion/model/protectionrequest.model'; +import { + FetchProtectionRequestsSpecification, + ProtectionRequestAggregateRoot, + ProtectionRequestRepository +} from '../../../src/logion/model/protectionrequest.model'; import { JsonExtrinsic } from '../../../src/logion/services/types/responses/Extrinsic'; import { ProtectionSynchronizer } from '../../../src/logion/services/protectionsynchronization.service'; import { ALICE, BOB } from '../../../src/logion/model/addresses.model'; @@ -19,7 +20,6 @@ describe("ProtectionSynchronizer", () => { it("activates protection", async () => { givenCreateRecoveryExtrinsic(); - givenBlock(); givenProtectionRequest(); await whenConsumingBlock(); thenRequestActivated(); @@ -27,7 +27,6 @@ describe("ProtectionSynchronizer", () => { }); }); -const blockTimestamp = moment(); let extrinsicDataExtractor: Mock; let protectionRequestRepository: Mock; @@ -51,20 +50,11 @@ function givenCreateRecoveryExtrinsic() { let locExtrinsic: Mock; -function givenBlock() { - block = new Mock(); - extrinsicDataExtractor.setup(instance => instance.getBlockTimestamp(block.object())).returns(blockTimestamp); - const extrinsics: JsonExtrinsic[] = [ locExtrinsic.object() ]; - block.setup(instance => instance.extrinsics).returns(extrinsics); -} - -let block: Mock; - function givenProtectionRequest() { locRequest = new Mock(); locRequest.setup(instance => instance.setActivated()).returns(undefined); - protectionRequestRepository.setup(instance => instance.findBy(It.Is(spec => + protectionRequestRepository.setup(instance => instance.findBy(It.Is(spec => spec.expectedRequesterAddress === SIGNER ))).returns(Promise.resolve([locRequest.object()])); protectionRequestRepository.setup(instance => instance.save(locRequest.object())).returns(Promise.resolve()); @@ -75,7 +65,7 @@ const SIGNER: string = "signer"; let locRequest: Mock; async function whenConsumingBlock() { - await synchronizer().updateProtectionRequests(block.object()); + await synchronizer().updateProtectionRequests(locExtrinsic.object()); } function synchronizer(): ProtectionSynchronizer {