Skip to content

Commit

Permalink
Merge pull request #242 from logion-network/feature/improve-chain-time
Browse files Browse the repository at this point in the history
More robust chain time
  • Loading branch information
gdethier authored Mar 28, 2024
2 parents 26de868 + d2f2b17 commit dda1c41
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
9 changes: 9 additions & 0 deletions packages/node-api/integration/ChainTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { setup } from "./Util.js";

export async function handleTime() {
const { api } = await setup();
const time = await api.time.now();
const block = time.currentBlock;
const nextTime = await time.atBlock(block + 1n);
expect(nextTime.currentTime - time.currentTime).toBe(Number(time.slotDuration));
}
3 changes: 3 additions & 0 deletions packages/node-api/integration/Main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { storageFees, legalFees, certificateFees, ensureEnoughFunds } from "./Fe
import { toPalletLogionLocOtherAccountId, toSponsorshipId, toPalletLogionLocMetadataItem, toPalletLogionLocFile, toCollectionItemToken, toCollectionItemFile } from "./Adapters.js";
import { badOriginError, moduleError } from "./Error.js";
import { createIdentityLocTest } from "./IdentityLoc.js";
import { handleTime } from "./ChainTime.js";

describe("Logion Node API", () => {

Expand Down Expand Up @@ -74,4 +75,6 @@ describe("Logion Node API", () => {

it("supports verified issuers", verifiedIssuers);
it("supports invited contributors", invitedContributors);

it("handles time", handleTime);
});
2 changes: 1 addition & 1 deletion packages/node-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/node-api",
"version": "0.28.4-2",
"version": "0.28.4-3",
"description": "logion API",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
21 changes: 13 additions & 8 deletions packages/node-api/src/ChainTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ export class ChainTime {

static async now(api: ApiPromise): Promise<ChainTime> {
const currentBlock = await api.rpc.chain.getBlock();
return new ChainTime(api, Date.now(), currentBlock.block.header.number.toBigInt());
const slotDuration = (await api.call.auraApi.slotDuration()).toBigInt();
return new ChainTime(api, Date.now(), currentBlock.block.header.number.toBigInt(), slotDuration);
}

constructor(api: ApiPromise, now: number, currentBlock: bigint) {
private constructor(api: ApiPromise, now: number, currentBlock: bigint, slotDuration: bigint) {
this._api = api;
this._currentTime = now;
this._currentBlock = currentBlock;
this._slotDuration = slotDuration;
}

private _api: ApiPromise;
private _currentTime: number;
private _currentBlock: bigint;
private _slotDuration: bigint;

get currentTime(): number {
return this._currentTime;
Expand All @@ -25,19 +28,21 @@ export class ChainTime {
return this._currentBlock;
}

get slotDuration() {
return this._slotDuration;
}

async atDate(date: Date): Promise<ChainTime> {
const diffInMs = BigInt(date.getTime() - this._currentTime);
const expectedBlockTimeInMs = this._api.consts.timestamp.minimumPeriod.toBigInt() * BigInt(2);
const deltaInBlocks = diffInMs / expectedBlockTimeInMs;
const deltaInBlocks = diffInMs / this.slotDuration;
const atBlock = this._currentBlock + deltaInBlocks;
return new ChainTime(this._api, date.getTime(), atBlock);
return new ChainTime(this._api, date.getTime(), atBlock, this._slotDuration);
}

async atBlock(blockNumber: bigint): Promise<ChainTime> {
const diffInBlocks = blockNumber - this._currentBlock;
const expectedBlockTimeInMs = this._api.consts.timestamp.minimumPeriod.toBigInt() * BigInt(2);
const deltaInMs = diffInBlocks * expectedBlockTimeInMs;
const deltaInMs = diffInBlocks * this.slotDuration;
const atTime = this._currentTime + Number(deltaInMs);
return new ChainTime(this._api, atTime, blockNumber);
return new ChainTime(this._api, atTime, blockNumber, this._slotDuration);
}
}
8 changes: 4 additions & 4 deletions packages/node-api/src/Queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface CoinBalance {
level: number,
}

export const ARTIFICIAL_MAX_BALANCE = Currency.toPrefixedNumberAmount(100n);
export const ARTIFICIAL_MAX_BALANCE = Currency.Lgnt.fromCanonical(100n).toCanonicalPrefixedNumber();

export class Queries {

Expand Down Expand Up @@ -72,9 +72,9 @@ export class Queries {
const accountInfo = await this.api.query.system.account(accountId);
const data = this.adapters.fromFrameSystemAccountInfo(accountInfo);

const logAvailable = Currency.toPrefixedNumberAmount(BigInt(data.available)).optimizeScale(3);
const logReserved = Currency.toPrefixedNumberAmount(BigInt(data.reserved)).optimizeScale(3);
const logTotal = Currency.toPrefixedNumberAmount(BigInt(data.total)).optimizeScale(3);
const logAvailable = Currency.Lgnt.fromCanonical(BigInt(data.available)).toCanonicalPrefixedNumber().optimizeScale(3);
const logReserved = Currency.Lgnt.fromCanonical(BigInt(data.reserved)).toCanonicalPrefixedNumber().optimizeScale(3);
const logTotal = Currency.Lgnt.fromCanonical(BigInt(data.total)).toCanonicalPrefixedNumber().optimizeScale(3);
const logLevel = logTotal.scientificNumber.divideBy(ARTIFICIAL_MAX_BALANCE.convertTo(logTotal.prefix).scientificNumber).toNumber();

return [
Expand Down
1 change: 1 addition & 0 deletions packages/node-api/src/VaultClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface CancelVaultOutTransferParameters extends RequestVaultOutTransfe

export class Vault {

// Should be taken from runtime, requires https://github.com/logion-network/logion-internal/issues/1193
static readonly THRESHOLD = 2;

constructor(
Expand Down
10 changes: 5 additions & 5 deletions packages/node-api/test/ChainTime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ describe("ChainTime", () => {

function buildPolkadotApiForTime() {
return {
consts: {
timestamp: {
minimumPeriod: {
toBigInt: () => BigInt(3000)
}
call: {
auraApi: {
slotDuration: () => Promise.resolve({
toBigInt: () => 6000n
}),
}
},

Expand Down

0 comments on commit dda1c41

Please sign in to comment.