Skip to content

Commit

Permalink
removing update graffit and batch update graffiti (#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
patnir authored Oct 13, 2023
1 parent 4c96133 commit 7b6310d
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 524 deletions.
237 changes: 0 additions & 237 deletions src/blocks/blocks.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { GraphileWorkerService } from '../graphile-worker/graphile-worker.servic
import { PrismaService } from '../prisma/prisma.service';
import { bootstrapTestApp } from '../test/test-app';
import { BlocksService } from './blocks.service';
import { BatchUpdateGraffitiDto } from './dto/batch-update-graffiti.dto';
import { UpsertBlocksDto } from './dto/upsert-blocks.dto';
import { BlockOperation } from './enums/block-operation';
import { SerializedBlockWithTransactions } from './interfaces/serialized-block-with-transactions';
Expand Down Expand Up @@ -209,242 +208,6 @@ describe('BlocksController', () => {
});
});

describe('POST /blocks/batch_update_graffiti', () => {
it('throws error when too many update block objects provided', async () => {
const blockUpdate = {
hash: 'hash',
graffiti:
'676d702f202020202020202020202036356264346432632d6433373237383130',
};
// array with 201 elements
const updates = new Array(201).fill(blockUpdate);

await request(app.getHttpServer())
.post('/blocks/batch_update_graffiti')
.set('Authorization', `Bearer ${API_KEY}`)
.send({
updates,
})
.expect(HttpStatus.UNPROCESSABLE_ENTITY);
});

it('throws error when too many update objects', async () => {
await request(app.getHttpServer())
.post('/blocks/batch_update_graffiti')
.expect(HttpStatus.UNAUTHORIZED);
});

it('throw error when graffiti is not formatted correctly', async () => {
// graffiti is not a list
await request(app.getHttpServer())
.post('/blocks/batch_update_graffiti')
.set('Authorization', `Bearer ${API_KEY}`)
.send({
hash: 'hash',
graffiti:
'676d702f202020202020202020202036356264346432632d6433373237383130',
})
.expect(HttpStatus.UNPROCESSABLE_ENTITY);

await request(app.getHttpServer())
.post('/blocks/batch_update_graffiti')
.set('Authorization', `Bearer ${API_KEY}`)
.send([
{
hash: 'hash',
graffiti:
'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0SSSS',
},
])
.expect(HttpStatus.UNPROCESSABLE_ENTITY);

await request(app.getHttpServer())
.post('/blocks/batch_update_graffiti')
.set('Authorization', `Bearer ${API_KEY}`)
.send({
updates: [
{
hash: 'hash',
graffiti:
'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0SSSS',
},
],
})
.expect(HttpStatus.UNPROCESSABLE_ENTITY);
});

it('returns the block graffiti', async () => {
const updateGraffiti = jest
.spyOn(blocksService, 'batchUpdateGrafitti')
.mockImplementationOnce(
jest.fn(async (input: BatchUpdateGraffitiDto) => {
const updates = input.updates;

const block1: Block = {
id: faker.datatype.number(),
created_at: new Date(),
updated_at: new Date(),
main: true,
network_version: 0,
time_since_last_block_ms: faker.datatype.number(),
hash: updates[0].hash,
difficulty: null,
work: null,
sequence: faker.datatype.number(),
timestamp: new Date(),
transactions_count: 0,
graffiti: updates[0].graffiti,
previous_block_hash: uuid(),
size: faker.datatype.number({ min: 1 }),
};

const block2: Block = {
id: faker.datatype.number(),
created_at: new Date(),
updated_at: new Date(),
main: true,
network_version: 0,
time_since_last_block_ms: faker.datatype.number(),
hash: updates[1].hash,
difficulty: null,
work: null,
sequence: faker.datatype.number(),
timestamp: new Date(),
transactions_count: 0,
graffiti: updates[1].graffiti,
previous_block_hash: uuid(),
size: faker.datatype.number({ min: 1 }),
};

await Promise.resolve([block1, block2]);

return [block1, block2];
}),
);

await request(app.getHttpServer())
.post('/blocks/batch_update_graffiti')
.set('Authorization', `Bearer ${API_KEY}`)
.send({
updates: [
{
hash: 'hash1',
graffiti:
'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9',
},
{
hash: 'hash2',
graffiti:
'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e0',
},
],
})
.expect(HttpStatus.CREATED);

expect(updateGraffiti).toHaveBeenCalledWith({
updates: [
{
hash: 'hash1',
graffiti:
'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9',
},
{
hash: 'hash2',
graffiti:
'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e0',
},
],
});
});
});

describe('POST /blocks/update_graffiti', () => {
it('throws unauthorized error when no api key is provided', async () => {
await request(app.getHttpServer())
.post('/blocks/update_graffiti')
.expect(HttpStatus.UNAUTHORIZED);
});

it('throw error when graffiti is not formatted correctly', async () => {
// graffiti is not hex
await request(app.getHttpServer())
.post('/blocks/update_graffiti')
.set('Authorization', `Bearer ${API_KEY}`)
.send({
hash: 'hash',
graffiti:
'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0SSSS',
})
.expect(HttpStatus.UNPROCESSABLE_ENTITY);

// graffiti is not 64 characters long
await request(app.getHttpServer())
.post('/blocks/update_graffiti')
.set('Authorization', `Bearer ${API_KEY}`)
.send({
hash: 'hash',
graffiti: 'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c',
})
.expect(HttpStatus.UNPROCESSABLE_ENTITY);

// graffiti is not 64 characters long
await request(app.getHttpServer())
.post('/blocks/update_graffiti')
.set('Authorization', `Bearer ${API_KEY}`)
.send({
hash: 'hash',
graffiti:
'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2ca1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2ca1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c',
})
.expect(HttpStatus.UNPROCESSABLE_ENTITY);
});

it('returns the block graffiti', async () => {
const updateGraffiti = jest
.spyOn(blocksService, 'updateGraffiti')
.mockImplementationOnce(
jest.fn(async (hash: string, graffiti: string) => {
const block: Block = {
id: faker.datatype.number(),
created_at: new Date(),
updated_at: new Date(),
main: true,
network_version: 0,
time_since_last_block_ms: faker.datatype.number(),
hash: hash,
difficulty: null,
work: null,
sequence: faker.datatype.number(),
timestamp: new Date(),
transactions_count: 0,
graffiti: graffiti,
previous_block_hash: uuid(),
size: faker.datatype.number({ min: 1 }),
};

await Promise.resolve(block);

return block;
}),
);

await request(app.getHttpServer())
.post('/blocks/update_graffiti')
.set('Authorization', `Bearer ${API_KEY}`)
.send({
hash: 'hash',
graffiti:
'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9',
})
.expect(HttpStatus.CREATED);

expect(updateGraffiti).toHaveBeenCalledWith(
'hash',
'a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9a1b3e4f2c8d0b7e9',
);
});
});

describe('GET /blocks', () => {
describe('with no query parameters', () => {
it('returns a list of blocks in descending order', async () => {
Expand Down
45 changes: 1 addition & 44 deletions src/blocks/blocks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ import { PaginatedList } from '../common/interfaces/paginated-list';
import { divide } from '../common/utils/bigint';
import { serializedTransactionFromRecord } from '../transactions/utils/transaction-translator';
import { BlocksService } from './blocks.service';
import { BatchUpdateGraffitiDto } from './dto/batch-update-graffiti.dto';
import { BlockQueryDto } from './dto/block-query.dto';
import { BlocksMetricsQueryDto } from './dto/blocks-metrics-query.dto';
import { BlocksQueryDto } from './dto/blocks-query.dto';
import { DisconnectBlocksDto } from './dto/disconnect-blocks.dto';
import { UpdateGraffitiDto } from './dto/update-graffiti.dto';
import { UpsertBlocksDto } from './dto/upsert-blocks.dto';
import { SerializedBlock } from './interfaces/serialized-block';
import { SerializedBlockHead } from './interfaces/serialized-block-head';
Expand All @@ -47,7 +45,7 @@ import {
} from './utils/block-translator';
import { serializedBlockMetricsFromRecord } from './utils/blocks-metrics-translator';
import { serializedBlocksStatusFromRecord } from './utils/blocks-status-translator';
import { Asset, AssetDescription, Block, Transaction } from '.prisma/client';
import { Asset, AssetDescription, Transaction } from '.prisma/client';

const MAX_SUPPORTED_TIME_RANGE_IN_DAYS = 90;

Expand All @@ -62,47 +60,6 @@ export class BlocksController {
private readonly blocksTransactionsLoader: BlocksTransactionsLoader,
) {}

@ApiExcludeEndpoint()
@Post('update_graffiti')
@UseGuards(ApiKeyGuard)
async updateGraffiti(
@Body(
new ValidationPipe({
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
transform: true,
}),
)
updateGraffitiDto: UpdateGraffitiDto,
): Promise<SerializedBlock> {
const block: Block = await this.blocksService.updateGraffiti(
updateGraffitiDto.hash,
updateGraffitiDto.graffiti,
);
return serializedBlockFromRecord(block);
}

@ApiExcludeEndpoint()
@Post('batch_update_graffiti')
@UseGuards(ApiKeyGuard)
async batchUpdateGraffiti(
@Body(
new ValidationPipe({
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
transform: true,
}),
)
batchUpdateGraffitiDto: BatchUpdateGraffitiDto,
): Promise<List<SerializedBlock>> {
const blocks = await this.blocksService.batchUpdateGrafitti(
batchUpdateGraffitiDto,
);

return {
object: 'list',
data: blocks.map(serializedBlockFromRecord),
};
}

@ApiExcludeEndpoint()
@Post()
@UseGuards(ApiKeyGuard)
Expand Down
Loading

0 comments on commit 7b6310d

Please sign in to comment.