Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(rosetta): Nakamoto timestamps #2132

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/api/controllers/db-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,17 @@ export async function getRosettaBlockFromDataStore(
}
}

// In epoch2.x, only the burn_block_time is consensus-level. Starting in epoch3, Stacks blocks include a consensus-level timestamp.
// Use `signer_bitvec` field to determine if the block is from epoch3.
let timestamp = dbBlock.burn_block_time * 1000;
if (dbBlock.signer_bitvec) {
timestamp = dbBlock.block_time * 1000;
}

const apiBlock: RosettaBlock = {
block_identifier: { index: dbBlock.block_height, hash: dbBlock.block_hash },
parent_block_identifier,
timestamp: dbBlock.burn_block_time * 1000,
timestamp: timestamp,
transactions: blockTxs.found ? blockTxs.result : [],
metadata: {
burn_block_height: dbBlock.burn_block_height,
Expand Down
88 changes: 88 additions & 0 deletions tests/rosetta/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,94 @@ describe('Rosetta API', () => {
});
});

test('block - Nakamoto timestamps', async () => {
const parentData = new TestBlockBuilder({
block_height: 0,
}).build();

// Epoch2.x block
const block1 = new TestBlockBuilder({
block_height: 1,
block_hash: '0x1234',
index_block_hash: '0x123456',
parent_block_hash: parentData.block.block_hash,
parent_index_block_hash: parentData.block.index_block_hash,
}).build();
block1.block.burn_block_time = 1222;
block1.block.block_time = 1333;
block1.block.signer_bitvec = null;

// Epoch3 block
const block2 = new TestBlockBuilder({
block_height: 2,
block_hash: '0x2234',
index_block_hash: '0x223456',
parent_block_hash: block1.block.block_hash,
parent_index_block_hash: block1.block.index_block_hash,
}).build();
block2.block.burn_block_time = 2222;
block2.block.block_time = 2333;
block2.block.signer_bitvec = '1111';

await db.update(parentData);
await db.update(block1);
await db.update(block2);

const query1 = await supertest(api.address)
.post(`/rosetta/v1/block`)
.send({
network_identifier: { blockchain: 'stacks', network: 'testnet' },
block_identifier: { index: block1.block.block_height },
});
expect(query1.status).toBe(200);
expect(query1.type).toBe('application/json');
const expected1: RosettaBlockResponse = {
block: {
block_identifier: {
index: block1.block.block_height,
hash: block1.block.block_hash,
},
parent_block_identifier: {
index: 1,
hash: '0x1234',
},
timestamp: block1.block.burn_block_time * 1000, // epoch2.x, should be burn-block-time
transactions: [],
metadata: {
burn_block_height: block1.block.burn_block_height,
},
},
};
expect(query1.body).toEqual(expected1);

const query2 = await supertest(api.address)
.post(`/rosetta/v1/block`)
.send({
network_identifier: { blockchain: 'stacks', network: 'testnet' },
block_identifier: { index: block2.block.block_height },
});
expect(query2.status).toBe(200);
expect(query2.type).toBe('application/json');
const expected2: RosettaBlockResponse = {
block: {
block_identifier: {
index: block2.block.block_height,
hash: block2.block.block_hash,
},
parent_block_identifier: {
index: block2.block.block_height - 1,
hash: block2.block.parent_block_hash,
},
timestamp: block2.block.block_time * 1000, // epoch3, should be Stacks-block-time
transactions: [],
metadata: {
burn_block_height: block2.block.burn_block_height,
},
},
};
expect(query2.body).toEqual(expected2);
});

test('stx-transfer-memo block/transaction', async () => {
const parentData = new TestBlockBuilder().addTx().build();
const block: TestBlockArgs = {
Expand Down
Loading