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: do not publish empty contract data #4022

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 49 additions & 1 deletion yarn-project/sequencer-client/src/sequencer/sequencer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { L1ToL2MessageSource, L2Block, L2BlockSource, MerkleTreeId, Tx, TxHash, mockTx } from '@aztec/circuit-types';
import {
ExtendedContractData,
L1ToL2MessageSource,
L2Block,
L2BlockSource,
MerkleTreeId,
Tx,
TxHash,
mockTx,
} from '@aztec/circuit-types';
import {
BlockHeader,
Fr,
Expand Down Expand Up @@ -209,6 +218,45 @@ describe('sequencer', () => {

expect(publisher.processL2Block).not.toHaveBeenCalled();
});

it('publishes contract data', async () => {
const txWithContract = mockTx(0x10000);
(txWithContract.newContracts as Array<ExtendedContractData>) = [ExtendedContractData.random()];
txWithContract.data.constants.txContext.chainId = chainId;

const txWithEmptyContract = mockTx(0x20000);
(txWithEmptyContract.newContracts as Array<ExtendedContractData>) = [ExtendedContractData.empty()];
txWithEmptyContract.data.constants.txContext.chainId = chainId;

const block = L2Block.random(lastBlockNumber + 1);
const proof = makeEmptyProof();

p2p.getTxs.mockResolvedValueOnce([txWithContract, txWithEmptyContract]);
blockBuilder.buildL2Block.mockResolvedValueOnce([block, proof]);
publisher.processL2Block.mockResolvedValueOnce(true);
publisher.processNewContractData.mockResolvedValueOnce(true);
globalVariableBuilder.buildGlobalVariables.mockResolvedValueOnce(
new GlobalVariables(chainId, version, new Fr(lastBlockNumber + 1), Fr.ZERO),
);

await sequencer.initialSync();
await sequencer.work();

// check that the block was built with both transactions
expect(blockBuilder.buildL2Block).toHaveBeenCalledWith(
expect.anything(),
expect.arrayContaining([
expect.objectContaining({ hash: await txWithContract.getTxHash() }),
expect.objectContaining({ hash: await txWithEmptyContract.getTxHash() }),
]),
expect.any(Array),
);

// check that the empty contract did not get published
expect(publisher.processNewContractData).toHaveBeenCalledWith(block.number, block.getCalldataHash(), [
txWithContract.newContracts[0],
]);
});
});

class TestSubject extends Sequencer {
Expand Down
27 changes: 12 additions & 15 deletions yarn-project/sequencer-client/src/sequencer/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,18 @@ export class Sequencer {
protected async publishExtendedContractData(validTxs: ProcessedTx[], block: L2Block) {
// Publishes contract data for txs to the network and awaits the tx to be mined
this.state = SequencerState.PUBLISHING_CONTRACT_DATA;
const newContractData = validTxs
.map(tx => {
// Currently can only have 1 new contract per tx
return tx.newContracts[0];
})
.filter((cd): cd is Exclude<typeof cd, undefined> => cd !== undefined);

const blockHash = block.getCalldataHash();
this.log(`Publishing extended contract data with block hash ${blockHash.toString('hex')}`);

const publishedContractData = await this.publisher.processNewContractData(block.number, blockHash, newContractData);
if (publishedContractData) {
this.log(`Successfully published new contract data for block ${block.number}`);
} else if (!publishedContractData && newContractData.length) {
this.log(`Failed to publish new contract data for block ${block.number}`);
const newContracts = validTxs.flatMap(tx => tx.newContracts).filter(cd => !cd.isEmpty());

if (newContracts.length > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't suppose we could invert the check, early return and reduce the nesting?

const blockHash = block.getCalldataHash();
this.log.info(`Publishing ${newContracts.length} contracts in block hash ${blockHash.toString('hex')}`);

const publishedContractData = await this.publisher.processNewContractData(block.number, blockHash, newContracts);
if (publishedContractData) {
this.log(`Successfully published new contract data for block ${block.number}`);
} else if (!publishedContractData && newContracts.length) {
this.log(`Failed to publish new contract data for block ${block.number}`);
}
}
}

Expand Down