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: Chain Refactor #3545

Merged
merged 47 commits into from
Jul 31, 2024
Merged

Common: Chain Refactor #3545

merged 47 commits into from
Jul 31, 2024

Conversation

holgerd77
Copy link
Member

@holgerd77 holgerd77 commented Jul 26, 2024

Ok, this is a first PoC on the Common simplification with directly passing in (and only allowing) a ChainConfig object (so, simply: Mainnet e.g.) to the chain, as discussed with @acolytec3 some time ago!

This is for tree shaking, but also leads to a dramatic simplification of code! Conversion helpers, all gone, lots of typing simplifications, also a strongly streamlined creatCustomCommon.

I also killed setChain() along the way, since we never had any use case for runtime-chain switching, as some side note.

This is absurdly intrusive though regarding the code changes, hope I get this under control! 😝 Not so intrusive luckily regarding the final outer API changes.

Uff. 🤯

Ah, and the Common tests are already passing, that's something. None of the upstream tests though yet.

-- Also added getPresetChainConfig as a reasonable replacement for getInitializedChains since we no longer allow multiple chain configurations in a single instance of common. (note from @acolytec3)

…onality, adjust/simplify createCustomCommon(), remove setChain(), fix tests
@holgerd77 holgerd77 force-pushed the common-chain-refactor branch from bc9ee48 to c5e696f Compare July 26, 2024 19:11
@@ -27,7 +27,7 @@ type ConsensusConfig = {

export interface ChainConfig {
name: string
chainId: number | bigint
chainId: number | string
Copy link
Member Author

Choose a reason for hiding this comment

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

This is also a change for serialization and is not yet reflected in the code base.

Copy link

codecov bot commented Jul 29, 2024

Codecov Report

Attention: Patch coverage is 56.62651% with 36 lines in your changes missing coverage. Please review.

Project coverage is 6.27%. Comparing base (fb50628) to head (e301f04).
Report is 16 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

Flag Coverage Δ
client 0.00% <0.00%> (ø)
tx 77.77% <59.49%> (-0.67%) ⬇️

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.

Great teamwork, LGTM 🙏 😄

@acolytec3 acolytec3 merged commit 361f4ed into master Jul 31, 2024
34 checks passed
@holgerd77 holgerd77 deleted the common-chain-refactor branch August 5, 2024 07:56
Copy link
Member Author

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

@acolytec3 @jochem-brouwer thanks a lot for working through this 🙏, some additional post-merge comments, but nothing grave!

One-step-back question: are you guys generally satisfied/happy with the direction of the changes within this PR, now that you have worked your way through this?

@@ -50,10 +50,10 @@ const genesisState: GenesisState = {
[contractAddress]: accountState,
}

const common = new Common({
// @ts-ignore PrefixedHesString type is too strict
Copy link
Member Author

Choose a reason for hiding this comment

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

Was this PrefixedHexString comment something one could not "fix on the fly"? 🤔

If something is "structurally" too strict, we should maybe un-stricten it?

Copy link
Contributor

Choose a reason for hiding this comment

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

When we take JSON and pass it directly to the various constructors that have the PrefixedHexString type, Typescript now complains because we're passing in string s. We either need to loosen the type on the opts that can be passed to the various *Data constructors or else type check all these strings ahead of time to confirm they're prefixed correctly.

const genesis = createBlockFromRLPSerializedBlock(genesisRlp, { common })

const blockRlp = hexToBytes(testData.blocks[0].rlp as PrefixedHexString)
const blockRlp = hexToBytes(testData.default.blocks[0].rlp as PrefixedHexString)
Copy link
Member Author

Choose a reason for hiding this comment

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

Are all these now commonly applied default accesses future proof? (so: not only work in our specific current tooling setup)

Copy link
Member Author

Choose a reason for hiding this comment

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

@acolytec3 maybe a final comment on this one?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's fine, though honestly not 100% sure. I think it was a knock-on effect of switching our dev setup to module in our package.json across the monorepo.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, thanks, then let's keep/take it, though I find all these kind of things honestly a bit spooky

@@ -60,7 +60,7 @@ export class Common {
constructor(opts: CommonOpts) {
this.events = new EventEmitter()

this._chainParams = JSON.parse(JSON.stringify(opts.chain))
this._chainParams = opts.chain
Copy link
Member Author

Choose a reason for hiding this comment

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

This JSON.parse(... stuff was to do a deep copy of the original structure, I otherwise had the cases where the original data got modified when being modified in the Common object.

So, this e.g. concretely played out somewhere in the EVM/VM tests where a test broke when simply Mainnet or so was used as in input two times in a row to a Common object and in the first round the associated _chainParams got modified.

(I am doing a commit-by-commit review, bear with me if some of the stuff is getting fixed on later commits)

Copy link
Member Author

Choose a reason for hiding this comment

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

( @jochem-brouwer even mentioned that we should do this in yet another place, can't ad hoc remember where (maybe along the copy() method))

Copy link
Collaborator

Choose a reason for hiding this comment

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

Btw, I learned just this week there is a better way to deep copy an object in js called structuredClone https://developer.mozilla.org/en-US/docs/Web/API/structuredClone

Copy link
Contributor

Choose a reason for hiding this comment

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

That'll be a nice thing to switch to if/when NodeJS brings it out from behind the experimental flag. Looks like it's in Node 22 but not fully supported yet. https://nodejs.org/api/globals.html#structuredclonevalue-options

.filter((hf) => hf.timestamp !== undefined)
.map((hf) => {
hf.timestamp = undefined
})
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not fully seeing through this change, but do we really need this hacky stuff here? 🤔

Or is otherwise something wrong with our typing/data structure/whatever?

@holgerd77
Copy link
Member Author

Also, to complete here:

This DOES have its desired effect on the bundle size, so Common is slowly getting to sane(r) levels.

Before (on tx/examples/londonTx.ts):

grafik grafik

After:

grafik grafik

Or simply by "looking at the box sizes" if the above is too noisy:

Before:

grafik

After:

grafik

Slowly getting there.

@roninjin10
Copy link
Collaborator

Very 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