Skip to content
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
37 changes: 24 additions & 13 deletions yarn-project/p2p/src/services/libp2p/libp2p_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import { reqGoodbyeHandler } from '../reqresp/protocols/goodbye.js';
import { pingHandler, reqRespBlockHandler, reqRespTxHandler, statusHandler } from '../reqresp/protocols/index.js';
import { ReqResp } from '../reqresp/reqresp.js';
import type { P2PBlockReceivedCallback, P2PService, PeerDiscoveryService } from '../service.js';
import { P2PInstrumentation } from './instrumentation.js';

interface ValidationResult {
name: string;
Expand Down Expand Up @@ -125,6 +126,8 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
) {
super(telemetry, 'LibP2PService');

this.instrumentation = new P2PInstrumentation(telemetry, 'LibP2PService');

this.msgIdSeenValidators[TopicType.tx] = new MessageSeenValidator(config.seenMessageCacheSize);
this.msgIdSeenValidators[TopicType.block_proposal] = new MessageSeenValidator(config.seenMessageCacheSize);
this.msgIdSeenValidators[TopicType.block_attestation] = new MessageSeenValidator(config.seenMessageCacheSize);
Expand Down Expand Up @@ -504,25 +507,33 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
}

protected preValidateReceivedMessage(msg: Message, msgId: string, source: PeerId) {
const getValidator = () => {
if (msg.topic === this.topicStrings[TopicType.tx]) {
return this.msgIdSeenValidators[TopicType.tx];
}
if (msg.topic === this.topicStrings[TopicType.block_attestation]) {
return this.msgIdSeenValidators[TopicType.block_attestation];
}
if (msg.topic === this.topicStrings[TopicType.block_proposal]) {
return this.msgIdSeenValidators[TopicType.block_proposal];
}
this.logger.error(`Received message on unknown topic: ${msg.topic}`);
};
let topicType: TopicType | undefined;

switch (msg.topic) {
case this.topicStrings[TopicType.tx]:
topicType = TopicType.tx;
break;
case this.topicStrings[TopicType.block_attestation]:
topicType = TopicType.block_attestation;
break;
case this.topicStrings[TopicType.block_proposal]:
topicType = TopicType.block_proposal;
break;
default:
this.logger.error(`Received message on unknown topic: ${msg.topic}`);
break;
}

const validator = getValidator();
const validator = topicType ? this.msgIdSeenValidators[topicType] : undefined;

if (!validator || !validator.addMessage(msgId)) {
this.instrumentation.incMessagePrevalidationStatus(false, topicType);
this.node.services.pubsub.reportMessageValidationResult(msgId, source.toString(), TopicValidatorResult.Ignore);
return false;
}

this.instrumentation.incMessagePrevalidationStatus(true, topicType);

return true;
}

Expand Down
17 changes: 0 additions & 17 deletions yarn-project/validator-client/src/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,6 @@ describe('ValidatorClient', () => {
expect(p2pClient.getTxsByHash).toHaveBeenCalledWith(proposal.payload.txHashes, sender);
});

it('should request txs if missing for attesting', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Duplicate test (see line 239 of same file)

p2pClient.hasTxsInPool.mockImplementation(txHashes => Promise.resolve(times(txHashes.length, i => i === 0)));

const attestation = await validatorClient.attestToProposal(proposal, sender);
expect(attestation).toBeDefined();
expect(p2pClient.getTxsByHash).toHaveBeenCalledWith(proposal.payload.txHashes, sender);
});

it('should request txs even if not attestor in this slot', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Duplicate test (see line 247 of same file)

p2pClient.hasTxsInPool.mockImplementation(txHashes => Promise.resolve(times(txHashes.length, () => false)));
epochCache.isInCommittee.mockResolvedValue(false);

const attestation = await validatorClient.attestToProposal(proposal, sender);
expect(attestation).toBeUndefined();
expect(p2pClient.getTxsByHash).toHaveBeenCalledWith(proposal.payload.txHashes, sender);
});

it('should throw an error if the transactions are not available', async () => {
// Mock the p2pClient.getTxStatus to return undefined for all transactions
p2pClient.getTxStatus.mockResolvedValue(undefined);
Expand Down