Skip to content

Conversation

acolytec3
Copy link
Contributor

@acolytec3 acolytec3 commented Oct 17, 2024

After failed attempt after attempt, I'm determined to get this right.

eventemitter3 is the right choice for us on a performance basis since it's 50% faster than native Nodejs events.

Now, I just need to take on the final boss of async events with eventemitter3.

Final boss has been defeated with the help of the robots.

@acolytec3 acolytec3 changed the title Switch events to eventemitter3 Switch events to eventemitter3 - the saga Oct 17, 2024
Copy link

codecov bot commented Oct 17, 2024

Codecov Report

Attention: Patch coverage is 50.98039% with 25 lines in your changes missing coverage. Please review.

Project coverage is 27.44%. Comparing base (d14a5ed) to head (39a7b63).
Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

Flag Coverage Δ
block 67.57% <ø> (ø)
blockchain 83.50% <100.00%> (+0.01%) ⬆️
client 0.00% <0.00%> (ø)
common 89.85% <100.00%> (ø)
devp2p 0.00% <0.00%> (ø)
evm 65.21% <75.00%> (+0.02%) ⬆️
genesis 0.00% <ø> (ø)
mpt 52.09% <ø> (+0.03%) ⬆️
statemanager 67.41% <ø> (ø)
tx 76.70% <ø> (?)
util 73.46% <ø> (+2.23%) ⬆️
vm 58.31% <78.57%> (+0.02%) ⬆️
wallet 0.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Copy link
Member

@jochem-brouwer jochem-brouwer left a comment

Choose a reason for hiding this comment

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

Looks clean and good, however, I see that for VM and EVM there are now changes where it seems that even on sync events resolve() is added and called (in the tests). These should be removed.

Also, the "breaking change" here is that blockchain events do now not support async events. Not sure if we want to put that back in, but if anyone is listening async on deletedCanonicalBlocks we definitely should.

I will also test this ASAP :)

this.config.events.once(Event.SYNC_SYNCHRONIZED, (height?: number) => {
this.resolveSync(height)
this.config.events.once(Event.SYNC_SYNCHRONIZED, (chainHeight?: bigint) => {
this.resolveSync(Number(chainHeight))
Copy link
Member

Choose a reason for hiding this comment

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

We could also propagate the bigint here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will make this change. Not really sure why we even propagate this other than for debug logging.

[Event.POOL_PEER_BANNED]: [bannedPeer: Peer]
[Event.PROTOCOL_ERROR]: [boundProtocolError: Error, peerCausingError: Peer]
[Event.PROTOCOL_MESSAGE]: [messageDetails: any, protocolName: string, sendingPeer: Peer]
}
Copy link
Member

Choose a reason for hiding this comment

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

How does the deletion of this event bus change the typing of the client types?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

eventEmitter3 doesn't need this additional layer of type expansions and we can just use the basic EventParams interface keyed by the properties of the Event enum.

assert.throws(() => sender.sendStatus({ id: 5 }), /not a function/, 'sendStatus error')
})
it('throws sendMessage error', () => {
assert.throws(() => sender.sendMessage(1, 5), /not a function/, 'sendMessage error')
Copy link
Member

Choose a reason for hiding this comment

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

These events are deleted and do not seem related (?) to events, is this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, they aren't directly related but they're pointless tests and don't actually test anything other than that when you try to access a non-existent property on an object that Javascript throws. I just stumbled across it because it referenced EventEmitter.

describe('should receive message', async () => {
const rlpxProtocol = { events: new EventEmitter() }
const sender = new RlpxSender(rlpxProtocol as Devp2pETH)
const sender = new RlpxSender(rlpxProtocol as never as Devp2pETH)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we have to cast it as never first now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've reversed this edit. For some reason, Typescript wasn't paying nicely with it before.

listener(data, resolve)
})
} else {
listener(data)
Copy link
Member

Choose a reason for hiding this comment

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

This is the async logic handler now, right? This is not part of eventemitter3?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, if the listener has 2 arguments, we assume it's looking for the event object and then the resolve function (and therefore is async). Otherwise, we just call the listener.

// Setup an event listener on the `afterTx` event
vm.events.on('afterTx', (event, resolve) => {
console.log('asynchronous listener to afterTx', bytesToHex(event.transaction.hash()))
resolve?.()
Copy link
Member

Choose a reason for hiding this comment

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

An explicit note that resolve must be called would be nice here :)

Copy link
Contributor

@am1r021 am1r021 left a comment

Choose a reason for hiding this comment

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

LGTM

@acolytec3 acolytec3 merged commit dc154fd into master Oct 18, 2024
41 checks passed
@acolytec3 acolytec3 deleted the eventEmitter-migration-take-4 branch October 18, 2024 01:08
resolveSync(height?: bigint) {
this.clearFetcher()
const heightStr = typeof height === 'number' && height !== 0 ? ` height=${height}` : ''
const heightStr = typeof height === 'bigint' && height !== BIGINT_0 ? ` height=${height}` : ''
Copy link
Member

Choose a reason for hiding this comment

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

Do these things even solve any bugs?? Or did you just also change the type coming in?

@holgerd77
Copy link
Member

Really really really nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants