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

Better .valueOf() #2419

Closed
dapplion opened this issue Apr 25, 2021 · 3 comments · Fixed by #3760
Closed

Better .valueOf() #2419

dapplion opened this issue Apr 25, 2021 · 3 comments · Fixed by #3760
Labels
prio-low This is nice to have.

Comments

@dapplion
Copy link
Contributor

dapplion commented Apr 25, 2021

Describe the issue

Using .valueOf() as SomeType is not type-safe at all and can become dangerous an unmaintainable.

We are using it for a very specific purpose which is to drop the tree and get the actual internal value without wrappers. This meaning is not easy to get at first glance.

Proposed solution

Create a helper function that clearly shows what it does that AND is type safe.

function dropTree<T>(treeBackedValue: T): T extends ByteVector ? Uint8Array : T {
  return treeBackedValue.valueOf()
}

With this code would read like

const randaoReveal = dropTree(block.body.randaoReveal)

instead of

const randaoReveal = block.body.randaoReveal.valueOf() as Uint8Array;

Related to #2408

@wemeetagain
Copy link
Member

👍 A typesafe wrapper would be really nice. I suggest something like this exported from @chainsafe/ssz:

function toStruct<T extends CompositeValue>(value: T): T extends ByteVector ? Uint8Array : T {
  if (isTreeBacked(value)) {
    return value.toStruct();
  }
  return value;
}

A note about the name, its not that we're dropping the tree but rather that we're converting the tree to a "struct", ie a primitive javascript datatype. (our valueOf method is an alias for toStruct)

@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 15 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the bot:stale label Jun 26, 2021
@dapplion dapplion added the prio-low This is nice to have. label Jun 27, 2021
@stale stale bot removed the bot:stale label Jun 27, 2021
@stale
Copy link

stale bot commented Sep 19, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 15 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the meta-stale label Sep 19, 2021
@dapplion dapplion mentioned this issue Apr 26, 2022
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
prio-low This is nice to have.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants