Skip to content

Commit

Permalink
fix(chain): calculation of relative ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 17, 2023
1 parent ca661d7 commit 651ba1a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/actions/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@ export async function getNetworkId(options) {
}

// ## Retrieve `ttl` version
export async function ttl(absoluteTtl, options) {
const { json } = options;
export async function ttl(_absoluteTtl, { json, ...options }) {
// Initialize `Ae`
const sdk = initSdk(options);
const height = await sdk.getHeight();
const absoluteTtl = +_absoluteTtl;
const relativeTtl = absoluteTtl - height;
if (json) {
print({ absoluteTtl, relativeTtl: +height + +absoluteTtl });
print({ absoluteTtl, relativeTtl });
} else {
printUnderscored('Absolute TTL', absoluteTtl);
printUnderscored('Relative TTL', +height + +absoluteTtl);
printUnderscored('Relative TTL', relativeTtl);
}
}

Expand Down
14 changes: 11 additions & 3 deletions test/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,18 @@ Syncing _________________________________ false
}).timeout(10000);

it('calculates ttl', async () => {
const { relativeTtl } = await executeChain(['ttl', 10, '--json']);
const height = await sdk.getHeight();
const isValid = [relativeTtl + 1, relativeTtl, relativeTtl - 1].includes(height + 10);
isValid.should.equal(true);
const resJson = await executeChain(['ttl', 10, '--json']);
expect(resJson).to.eql({
absoluteTtl: 10,
relativeTtl: 10 - height,
});

const res = await executeChain(['ttl', 10]);
expect(res).to.equal(`
Absolute TTL ____________________________ 10
Relative TTL ____________________________ ${resJson.relativeTtl}
`.trim());
});

it('prints network id', async () => {
Expand Down

0 comments on commit 651ba1a

Please sign in to comment.