Skip to content

Commit

Permalink
fix(chain): padding of top output
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 17, 2023
1 parent a5fa358 commit ca661d7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
6 changes: 2 additions & 4 deletions src/actions/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ export async function ttl(absoluteTtl, options) {
}

// ## Retrieve `TOP` block
export async function top(options) {
const { json } = options;
export async function top({ json, ...options }) {
// Initialize `Ae`
const sdk = initSdk(options);
// Call `getTopBlock` API and print it
// TODO: shouldn't be padded
printBlock(await sdk.api.getTopHeader(), json);
printBlock(await sdk.api.getTopHeader(), json, true);
}

// # Play by `limit`
Expand Down
2 changes: 1 addition & 1 deletion src/actions/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function getBlockByHash(hash, options) {
try {
checkPref(hash, [Encoding.KeyBlockHash, Encoding.MicroBlockHash]);
const sdk = initSdk(options);
printBlock(await getBlock(hash, sdk), json);
printBlock(await getBlock(hash, sdk), json, true);
} catch (e) {
printError(e.message);
}
Expand Down
13 changes: 7 additions & 6 deletions src/utils/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,32 +303,33 @@ export function printBlockTransactions(ts, json, tabs = 0) {
);
}

export function printBlock(block, json) {
export function printBlock(block, json, isRoot = false) {
if (json) {
print(block);
return;
}
const encoding = block.hash.split('_')[0];
const tabs = encoding === Encoding.MicroBlockHash ? 1 : 0;
const tabs = !isRoot && encoding === Encoding.MicroBlockHash ? 1 : 0;
const tabString = getTabs(tabs);

print(`${tabString}<<--------------- ${Encoding[encoding]} --------------->>`);
const reverseEncoding = Object.fromEntries(Object.entries(Encoding).map(([k, v]) => [v, k]));
print(`${tabString}<<--------------- ${reverseEncoding[encoding]} --------------->>`);

printUnderscored(`${tabString}Block hash`, block.hash);
printUnderscored(`${tabString}Block height`, block.height);
printUnderscored(`${tabString}State hash`, block.stateHash);
printUnderscored(`${tabString}Nonce`, block.nonce ?? 'N/A');
printUnderscored(`${tabString}Miner`, block.miner ?? 'N/A');
printUnderscored(`${tabString}Time`, new Date(block.time));
printUnderscored(`${tabString}Time`, new Date(block.time).toString());
printUnderscored(`${tabString}Previous block hash`, block.prevHash);
printUnderscored(`${tabString}Previous key block hash`, block.prevKeyHash);
printUnderscored(`${tabString}Version`, block.version);
printUnderscored(`${tabString}Target`, block.target ?? 'N/A');
const txCount = block?.transactions?.length ?? 0;
const txCount = block.transactions?.length ?? 0;
printUnderscored(`${tabString}Transactions`, txCount);
if (txCount) printBlockTransactions(block.transactions, false, tabs + 1);

print('<<------------------------------------->>');
print(`${tabString}<<------------------------------------->>`);
}

// ##OTHER
Expand Down
24 changes: 20 additions & 4 deletions test/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,26 @@ describe('Chain Module', () => {
});

it('prints top', async () => {
const res = await executeChain(['top', '--json']);
res.should.be.a('object');
res.hash.should.be.a('string');
res.height.should.be.a('number');
const resJson = await executeChain(['top', '--json']);
expect(resJson.hash).to.be.a('string');
expect(resJson.height).to.be.a('number');

const res = await executeChain(['top']);
expect(res).to.equal(`
<<--------------- ${resJson.hash.startsWith('mh_') ? 'MicroBlockHash' : 'KeyBlockHash'} --------------->>
Block hash ______________________________ ${resJson.hash}
Block height ____________________________ ${resJson.height}
State hash ______________________________ ${resJson.stateHash}
Nonce ___________________________________ ${resJson.nonce ?? 'N/A'}
Miner ___________________________________ ${resJson.miner ?? 'N/A'}
Time ____________________________________ ${new Date(resJson.time).toString()}
Previous block hash _____________________ ${resJson.prevHash}
Previous key block hash _________________ ${resJson.prevKeyHash}
Version _________________________________ 5
Target __________________________________ ${resJson.target ?? 'N/A'}
Transactions ____________________________ 0
<<------------------------------------->>
`.trim());
});

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

0 comments on commit ca661d7

Please sign in to comment.