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

Use custom types in data structures and constants #695

Closed
wants to merge 16 commits into from

Conversation

Nashatyrev
Copy link
Member

Original issue: #667

The goal is to improve Spec readability by using alias custom types in data structures and as constant units instead of generic types.

What was done

  • Add Hash and Bitfield custom types. The Hash type is treated as a result of any 256-bit cryptographic hash function or its derivative preserving hash properties (like xoring two hashes)
  • Replace generic SSZ types in data structures with custom types where appropriate
  • Add/update constant units according to custom types

What else can be done

  • Add more custom types (like Time, ForkVersion, Bitfield64) which are now referred at 1-2 places in the Spec
  • As per @hwwhww suggestion in Use custom types in data structures #667 cover all elements with custom types
  • In some way describe array index types (like 'validator_balances': [ValidatorIndex => Gwei])

# Withdrawal credentials
'withdrawal_credentials': 'bytes32',
'withdrawal_credentials': Hash,
Copy link
Contributor

Choose a reason for hiding this comment

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

@JustinDrake
Should it be Bytes32?

Copy link
Member Author

Choose a reason for hiding this comment

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

Already returned it back to Bytes32 in this commit a693737

Copy link
Contributor

Choose a reason for hiding this comment

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

But there are two withdrawal_credentialss: one is in DepositInput and another one is in Validator.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, my bad.
Sure this one should also be Bytes32
Thanks for spotting this!

Copy link
Collaborator

@JustinDrake JustinDrake Feb 27, 2019

Choose a reason for hiding this comment

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

I'm tempted to nuke Bytes32 (which is semantically redundant with bytes32) and use Hash everywhere. After all, withdrawal_crendentials fits with the definition: "result of hashing and its derivatives"

Copy link
Member Author

@Nashatyrev Nashatyrev Feb 27, 2019

Choose a reason for hiding this comment

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

I'm fine with it. Committed

Copy link
Contributor

Choose a reason for hiding this comment

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

After all, withdrawal_crendentials fits with the definition: "result of hashing and its derivatives"

Technically the first byte is a version number, but hashes have had built-in version numbers before (see ipfs multi hash) so that's not too terrible...

Copy link
Contributor

@djrtwo djrtwo left a comment

Choose a reason for hiding this comment

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

I think this is a net positive. Is there any hangup on merge at this point?

@hwwhww
Copy link
Contributor

hwwhww commented Mar 1, 2019

I’m inclined to have custom types cover all elements. Compromise: define a general Uint64?

@Nashatyrev
Copy link
Member Author

IMHO, defining aliases for types which are used just in a couple of places in the spec wouldn't make the spec clearer.
As well I see no big reason to make UInt64 => uint64 alias.
We probably need someone else opinion here. @djrtwo ?

@hwwhww
Copy link
Contributor

hwwhww commented Mar 20, 2019

Hmm, I think this PR would break CI tests...
Basically, it requires (i) defining a custom type in Python and also (ii) converting Python types to SSZ types when defining SSZ classes.

@Nashatyrev could you rebase it and then let's see how to fix it in function_puller.py?

# Conflicts:
#	specs/core/0_beacon-chain.md
@Nashatyrev
Copy link
Member Author

@hwwhww rebased. Let me know if I can help you with CI tests

JustinDrake added a commit that referenced this pull request Apr 14, 2019
* Remove unnecessary per-field comments, focus on field grouping
* Group `Validator` fields relevant to light-clients together (small optimisation for light clients)
* Rework grouping of `BeaconState` fields
* `deposit_index` => `eth1_deposit_index`

Do not merge before:

* #695 which specifies custom types for individual container fields, offsetting the removal of comments in some instances
* #896 and #843 so that we don't have to continuously maintain the genesis `BeaconState`
* #874 which introduces `current_crosslinks` and `previous_crosslinks`
@JustinDrake JustinDrake mentioned this pull request Apr 14, 2019
@protolambda
Copy link
Collaborator

  1. Is this still up to date? (I don't think so)
  2. Do we still plan to merge this?
  3. FYI: this breaks the typing syntax in many of the SSZ utilities in the pyspec. I.e. the default zero values, random instantiation of ssz, and minimal_ssz.py itself. These basic types are declared with strings, not types: they instruct what the typing looks like, they are not the type itself. I recommend to add an alias functionality, where we just lookup the "raw" type for any given type used in the SSZ container type declarations, and only then serialize/hash it etc.

@JustinDrake
Copy link
Collaborator

Do we still plan to merge this?

I'm very pro adding aliases for readability. Do you want to go ahead @protolambda and fix whatever needs fixing? We can then merge.

@JustinDrake JustinDrake added the scope:SSZ Simple Serialize label May 3, 2019
@protolambda
Copy link
Collaborator

I think we addressed most of the typing ideas with #1077. We can go further with it and use NewType here and there to describe some things in addition to typing them (e.g. Slot), but I think it's clean enough right now with the current data-types. So I'm closing this PR. Anyone who is interested is welcome to open a new PR for typing improvements, if anything can be improved on the current dev branch (this PR is quite out of date now).

👏 for bringing this up so early, and apologies from us all that typing didn't make it into the spec so quick, I hope the new more complete typing support makes up for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope:SSZ Simple Serialize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants