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

Common deep copy refactor #3608

Closed
wants to merge 8 commits into from
Closed

Conversation

scorbajio
Copy link
Contributor

This change refactors the Common class to deep copy parameters on calls to copy. It aims to address issue #3593 and #3572.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 4010916 Previous: 9856f66 Ratio
Block 9422905 2207 ops/sec (±1.09%) 37877 ops/sec (±1.73%) 17.16
Block 9422906 2223 ops/sec (±0.59%) 35937 ops/sec (±3.35%) 16.17
Block 9422907 2203 ops/sec (±0.78%) 36656 ops/sec (±1.46%) 16.64
Block 9422908 2170 ops/sec (±0.74%) 35733 ops/sec (±1.60%) 16.47
Block 9422910 2201 ops/sec (±0.47%) 34971 ops/sec (±1.78%) 15.89

This comment was automatically generated by workflow using github-action-benchmark.

acolytec3
acolytec3 previously approved these changes Aug 22, 2024
Copy link
Contributor

@acolytec3 acolytec3 left a comment

Choose a reason for hiding this comment

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

LGTM

customCrypto: this.customCrypto,
})
copy.updateParams(this._params)
copy.setHardfork(this.hardfork())
Copy link
Member

Choose a reason for hiding this comment

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

This code has never been touched by any refactor, I am really not seeing why we should change this now.

Also, side note, the spread operator ... has issues with nested copying, see e.g. this example:

grafik

That's why spread is generally not so suitable.

Also to note: side adding setHardfork() can have massive side effects here, I remember similar experiments in the past.

await setCommon()
await resetCommon()
})

Copy link
Member

Choose a reason for hiding this comment

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

I am honestly very confused by this whole setup, so these tests trigger the issue but were later removed?? 😂

I will copy (cherry-pick) them over in a new PR and add the changes I initially intended.

Copy link
Member

Choose a reason for hiding this comment

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

(and see if tests are passing)

@@ -69,7 +71,7 @@ export class Common {
(this._chainParams.customHardforks && this._chainParams.customHardforks[hf.name]),
])
this._hardfork = this.DEFAULT_HARDFORK
this._params = { ...(opts.params ?? {}) } // copy
this._params = opts.params ? JSON.parse(JSON.stringify(opts.params)) : {} // copy
Copy link
Member

Choose a reason for hiding this comment

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

Ok, so this is actually the single line change I initially intended, likely with the ones from updateParams() and resetParams() (good catch!), no changes intended for copy() whatsoever.

Will tests these lines in a new PR (maybe again, eventually you've been to this point already), then this should already be enough for this (or a replacing) PR!

(we also should not mix this with #3593 , it's absolutely not clear that these are related)

@@ -58,6 +59,7 @@ export class Common {
public events: EventEmitter

constructor(opts: CommonOpts) {
this._opts = opts
this.events = new EventEmitter()

this._chainParams = opts.chain
Copy link
Member

Choose a reason for hiding this comment

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

Ah, but you are right, we need additional treatment for chain. But we should do here, then we can avoid doing any copy() changes.

Also (more important): if we do not here, then the initial Mainnet (or whatever chain) structure gets modified in the first place. That's bad and we should very much avoid (same for the params structs, I stumbled upon this kind of failure).

let chainConfig: ChainConfig
let c: Common
const setCommon = async () => {
chainConfig = Mainnet
Copy link
Member

Choose a reason for hiding this comment

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

Haha, so funny, I am pretty sure (or: could very much imagine) that this is where you turned wrong: so, by associating chainConfig = Mainnet here and then manipulating this further down the line with chainConfig.chainId = 2 this then also manipulates Mainnet itself.

And that causes another test below in line 55 to fail. 😂 And the error looks pretty much like a "still failing" message if one is not looking pretty closely. 🙂

@holgerd77 holgerd77 mentioned this pull request Aug 26, 2024
@holgerd77
Copy link
Member

Have replaced with #3613, will close here.

Ok, but this work was definitely not "wasted", since this discovered various additional necessary deep-copy cases + could re-use the test setup! 🙏

@holgerd77 holgerd77 closed this Aug 26, 2024
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.

3 participants