-
Notifications
You must be signed in to change notification settings - Fork 778
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
Common: Chain Refactor #3545
Conversation
…onality, adjust/simplify createCustomCommon(), remove setChain(), fix tests
bc9ee48
to
c5e696f
Compare
@@ -27,7 +27,7 @@ type ConsensusConfig = { | |||
|
|||
export interface ChainConfig { | |||
name: string | |||
chainId: number | bigint | |||
chainId: number | string |
There was a problem hiding this comment.
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.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files
Flags with carried forward coverage won't be shown. Click here to find out more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great teamwork, LGTM 🙏 😄
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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))
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 | ||
}) |
There was a problem hiding this comment.
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?
Very nice! |
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 thechain
, 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 forgetInitializedChains
since we no longer allow multiple chain configurations in a single instance ofcommon
. (note from @acolytec3)