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

feat: add block header to responses in block-related queries #3092

Merged
merged 10 commits into from
Sep 4, 2024
5 changes: 5 additions & 0 deletions .changeset/fifty-roses-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

feat: add block header to responses in block-related queries
8 changes: 8 additions & 0 deletions packages/account/src/providers/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ fragment blockFragment on Block {
height
header {
time
daHeight
stateTransitionBytecodeVersion
transactionsCount
transactionsRoot
messageOutboxRoot
eventInboxRoot
prevRoot
applicationHash
}
Torres-ssf marked this conversation as resolved.
Show resolved Hide resolved
transactions {
id
Expand Down
72 changes: 64 additions & 8 deletions packages/account/src/providers/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,29 @@ describe('Provider', () => {
);
});

it('can getBlock', async () => {
using launched = await setupTestProviderAndWallets();
const { provider } = launched;
await provider.produceBlocks(1);
const block = await provider.getBlock('latest');
expect(block).toStrictEqual({
id: expect.any(String),
height: expect.any(BN),
time: expect.any(String),
header: {
applicationHash: expect.any(String),
daHeight: expect.any(BN),
eventInboxRoot: expect.any(String),
messageOutboxRoot: expect.any(String),
prevRoot: expect.any(String),
stateTransitionBytecodeVersion: expect.any(String),
transactionsCount: expect.any(String),
transactionsRoot: expect.any(String),
},
transactionIds: expect.any(Array<string>),
});
});

it('can getBlocks', async () => {
using launched = await setupTestProviderAndWallets();
const blocksLenght = 5;
Expand All @@ -856,14 +879,47 @@ describe('Provider', () => {
});
expect(blocks.length).toBe(blocksLenght);
blocks.forEach((block) => {
expect(block).toEqual(
expect.objectContaining({
id: expect.any(String),
height: expect.any(BN),
time: expect.any(String),
transactionIds: expect.any(Array<string>),
})
);
expect(block).toStrictEqual({
id: expect.any(String),
height: expect.any(BN),
time: expect.any(String),
header: {
applicationHash: expect.any(String),
daHeight: expect.any(BN),
eventInboxRoot: expect.any(String),
messageOutboxRoot: expect.any(String),
prevRoot: expect.any(String),
stateTransitionBytecodeVersion: expect.any(String),
transactionsCount: expect.any(String),
transactionsRoot: expect.any(String),
},
transactionIds: expect.any(Array<string>),
});
});
});

it('can getBlockWithTransactions', async () => {
using launched = await setupTestProviderAndWallets();
const { provider } = launched;
await provider.produceBlocks(1);
const block = await provider.getBlockWithTransactions('latest');
const { transactions } = await provider.getTransactions({ first: 100 });
expect(block).toStrictEqual({
id: expect.any(String),
height: expect.any(BN),
time: expect.any(String),
header: {
applicationHash: expect.any(String),
daHeight: expect.any(BN),
eventInboxRoot: expect.any(String),
messageOutboxRoot: expect.any(String),
prevRoot: expect.any(String),
stateTransitionBytecodeVersion: expect.any(String),
transactionsCount: expect.any(String),
transactionsRoot: expect.any(String),
},
transactionIds: expect.any(Array<string>),
transactions,
});
});

Expand Down
50 changes: 46 additions & 4 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ export type Block = {
height: BN;
time: string;
transactionIds: string[];
header: {
daHeight: BN;
stateTransitionBytecodeVersion: string;
transactionsCount: string;
transactionsRoot: string;
messageOutboxRoot: string;
eventInboxRoot: string;
prevRoot: string;
applicationHash: string;
};
};

export type GetCoinsResponse = {
Expand Down Expand Up @@ -1428,11 +1438,23 @@ Supported fuel-core version: ${supportedVersion}.`
return null;
}

const { header, height, id, transactions } = block;

return {
id: block.id,
height: bn(block.height),
time: block.header.time,
transactionIds: block.transactions.map((tx) => tx.id),
id,
height: bn(height),
time: header.time,
header: {
applicationHash: header.applicationHash,
daHeight: bn(header.daHeight),
eventInboxRoot: header.eventInboxRoot,
messageOutboxRoot: header.messageOutboxRoot,
prevRoot: header.prevRoot,
stateTransitionBytecodeVersion: header.stateTransitionBytecodeVersion,
transactionsCount: header.transactionsCount,
transactionsRoot: header.transactionsRoot,
},
transactionIds: transactions.map((tx) => tx.id),
};
}

Expand All @@ -1456,6 +1478,16 @@ Supported fuel-core version: ${supportedVersion}.`
id: block.id,
height: bn(block.height),
time: block.header.time,
header: {
applicationHash: block.header.applicationHash,
daHeight: bn(block.header.daHeight),
eventInboxRoot: block.header.eventInboxRoot,
messageOutboxRoot: block.header.messageOutboxRoot,
prevRoot: block.header.prevRoot,
stateTransitionBytecodeVersion: block.header.stateTransitionBytecodeVersion,
transactionsCount: block.header.transactionsCount,
transactionsRoot: block.header.transactionsRoot,
},
transactionIds: block.transactions.map((tx) => tx.id),
}));

Expand Down Expand Up @@ -1491,6 +1523,16 @@ Supported fuel-core version: ${supportedVersion}.`
id: block.id,
height: bn(block.height, 10),
time: block.header.time,
header: {
applicationHash: block.header.applicationHash,
daHeight: bn(block.header.daHeight),
eventInboxRoot: block.header.eventInboxRoot,
messageOutboxRoot: block.header.messageOutboxRoot,
prevRoot: block.header.prevRoot,
stateTransitionBytecodeVersion: block.header.stateTransitionBytecodeVersion,
transactionsCount: block.header.transactionsCount,
transactionsRoot: block.header.transactionsRoot,
},
transactionIds: block.transactions.map((tx) => tx.id),
transactions: block.transactions.map(
(tx) => new TransactionCoder().decode(arrayify(tx.rawPayload), 0)?.[0]
Expand Down
12 changes: 11 additions & 1 deletion packages/account/test/fixtures/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,17 @@ export const MOCK_CHAIN: GqlChainInfoFragment = {
latestBlock: {
id: '0xb9e55ced368c8d8f1aa487d33e97043e5891406792ea5d61f7807d0441d34722',
height: '234',
header: { time: '4611686020122537935' },
header: {
time: '4611686020152758037',
applicationHash: '0x9b4b3f3021fec42b2d946b2c6547841e379716122f78a6f22d65f51d6e1a2746',
daHeight: '0',
eventInboxRoot: '0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
messageOutboxRoot: '0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
prevRoot: '0x68a08304aa13a972e471d3b15e9a59d18a50301c3990840ed4282b2e7b6a9008',
stateTransitionBytecodeVersion: '0',
transactionsCount: '1',
transactionsRoot: '0x4d9e0e3cda1af0c5bbf3dff9af9025ee2e264cc88bdef9dd103fb3a96d0a21ba',
},
transactions: [
{
id: '0x304fb90a1a9897d839dcd9a5b93739ca6045638fc6520e2cf5735dd84b2de4a7',
Expand Down
Loading