Skip to content

Commit

Permalink
fix: allow /block to be able to be queried by block === 0 (cardano-…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanVerbner committed Jul 27, 2020
1 parent 0e850da commit ef79708
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/server/services/block-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ const configure = (
networkRepository: NetworkRepository
): BlockService => ({
async findBlock(blockIdentifier) {
// cardano doesn't have block zero but we need to map it to genesis
const searchBlockZero = blockIdentifier.index === 0; // We need to manually check for the block hash if sent to server
if (searchBlockZero) {
const genesis = await repository.findGenesisBlock();
const isHashInvalidIfGiven = blockIdentifier.hash && genesis?.hash !== blockIdentifier.hash;
if (isHashInvalidIfGiven) {
throw ErrorFactory.blockNotFoundError();
}
return repository.findBlock(undefined, genesis?.hash);
}

const searchLatestBlock = blockIdentifier.hash === undefined && blockIdentifier.index === undefined;
const blockNumber = searchLatestBlock ? await repository.findLatestBlockNumber() : blockIdentifier.index;
return repository.findBlock(blockNumber, blockIdentifier.hash);
Expand All @@ -138,14 +149,14 @@ const configure = (
const block = await this.findBlock(request.block_identifier);
if (block !== null) {
const { number } = block;
const transactionsHashes = await repository.findBlockTransactionHashes(number, request.block_identifier.hash);
const transactionsHashes = await repository.findBlockTransactionHashes(number, block.hash);
if (transactionsHashes.length > PAGE_SIZE) {
return {
block: mapToRosettaBlock(block, []),
other_transactions: transactionsHashes
};
}
const transactions = await repository.findTransactionsByBlock(number, request.block_identifier.hash);
const transactions = await repository.findTransactionsByBlock(number, block.hash);
return {
block: mapToRosettaBlock(block, transactions)
};
Expand Down
23 changes: 22 additions & 1 deletion test/e2e/block-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
block23236WithTransactions,
latestBlock,
block7134WithTxs,
blockWith8Txs
blockWith8Txs,
GENESIS_HASH
} from './fixture-data';
import { setupDatabase, setupServer } from './utils/test-utils';

Expand Down Expand Up @@ -125,6 +126,26 @@ describe('Block API', () => {
expect(response.statusCode).toEqual(StatusCodes.OK);
expect(response.json()).toEqual(blockWith8Txs);
});

test('should return an error if hash sent in the request does not match the one in block 0', async () => {
const response = await server.inject({
method: 'post',
url: '/block',
payload: generatePayload(0, '0x7a8dbe66c6a1b41bdbf4f3865ea20aebbf93b9697bf39024d5d08ffad10ab1e8')
});
expect(response.statusCode).toEqual(StatusCodes.INTERNAL_SERVER_ERROR);
expect(response.json()).toEqual({ code: 4001, message: 'Block not found', retriable: false });
});

test('should be able to return block 0', async () => {
const response = await server.inject({
method: 'post',
url: '/block',
payload: generatePayload(0)
});
expect(response.statusCode).toEqual(StatusCodes.OK);
expect(response.json().block.block_identifier.hash).toEqual(GENESIS_HASH);
});
});
describe('/block/transactions endpoint', () => {
const BLOCK_TRANSACTION_ENDPOINT = '/block/transaction';
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/fixture-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,5 @@ export const blockWith8Txs = {
}
]
};

export const GENESIS_HASH = '0x5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb';

0 comments on commit ef79708

Please sign in to comment.