|
| 1 | +/* |
| 2 | + * Copyright 2021 NEM |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { ChronoUnit, Duration, Instant } from '@js-joda/core'; |
| 18 | +import { RepositoryFactory } from '../infrastructure'; |
| 19 | +import { Deadline, defaultChronoUnit, defaultDeadline } from '../model/transaction'; |
| 20 | + |
| 21 | +/** |
| 22 | + * A factory service that allows the client to generate Deadline objects based on different strategies. |
| 23 | + * |
| 24 | + * The main issue is that sometimes the local computer time is not in sync, the created deadlines may be too old or too in the future and rejected by the server. |
| 25 | + */ |
| 26 | +export class DeadlineService { |
| 27 | + /** |
| 28 | + * The difference in milliseconds between the server and the local time. It used to create "server" deadline without asking for the server time every time a new deadline is created. |
| 29 | + */ |
| 30 | + private localTimeOffset: number; |
| 31 | + |
| 32 | + /** |
| 33 | + * Private constructor, use the create static method |
| 34 | + * |
| 35 | + * @param repositoryFactory the repository factory to call the rest servers. |
| 36 | + * @param epochAdjustment the server epochAdjustment |
| 37 | + * @param serverTime the latest known server time to calculate the remote and local time difference. |
| 38 | + */ |
| 39 | + private constructor( |
| 40 | + private readonly repositoryFactory: RepositoryFactory, |
| 41 | + private readonly epochAdjustment: number, |
| 42 | + serverTime: number, |
| 43 | + ) { |
| 44 | + this.localTimeOffset = Instant.now().minusSeconds(epochAdjustment).toEpochMilli() - serverTime; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * It creates a deadline by querying the current time to the server each time. This is the most accurate but less efficient way. |
| 49 | + * |
| 50 | + * @param deadline the deadline value |
| 51 | + * @param chronoUnit the unit of the value. |
| 52 | + */ |
| 53 | + public async createDeadlineUsingServerTime(deadline = defaultDeadline, chronoUnit: ChronoUnit = defaultChronoUnit): Promise<Deadline> { |
| 54 | + const serverTime = (await this.repositoryFactory.createNodeRepository().getNodeTime().toPromise()).receiveTimeStamp.compact(); |
| 55 | + return Deadline.createFromAdjustedValue(Duration.ofMillis(serverTime).plus(deadline, chronoUnit).toMillis()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * It creates a deadline using the known difference between the local and server time. |
| 60 | + * |
| 61 | + * @param deadline the deadline value |
| 62 | + * @param chronoUnit the unit of the value. |
| 63 | + */ |
| 64 | + public createDeadlineUsingOffset(deadline = defaultDeadline, chronoUnit: ChronoUnit = defaultChronoUnit): Deadline { |
| 65 | + return Deadline.createFromAdjustedValue( |
| 66 | + Instant.now().plus(deadline, chronoUnit).minusMillis(this.localTimeOffset).minusSeconds(this.epochAdjustment).toEpochMilli(), |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * It creates a deadline using the local time. If the local system time is not in sync, the Deadline may be rejected by the server. |
| 72 | + * |
| 73 | + * @param deadline the deadline value |
| 74 | + * @param chronoUnit the unit of the value. |
| 75 | + */ |
| 76 | + public createDeadlineUsingLocalTime(deadline = defaultDeadline, chronoUnit: ChronoUnit = defaultChronoUnit): Deadline { |
| 77 | + return Deadline.create(this.epochAdjustment, deadline, chronoUnit); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Factory method of this object. |
| 82 | + * |
| 83 | + * @param repositoryFactory the repository factory to call the rest servers. |
| 84 | + */ |
| 85 | + public static async create(repositoryFactory: RepositoryFactory): Promise<DeadlineService> { |
| 86 | + const epochAdjustment = await repositoryFactory.getEpochAdjustment().toPromise(); |
| 87 | + const serverTime = (await repositoryFactory.createNodeRepository().getNodeTime().toPromise()).receiveTimeStamp.compact(); |
| 88 | + return new DeadlineService(repositoryFactory, epochAdjustment, serverTime); |
| 89 | + } |
| 90 | +} |
0 commit comments