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

go.mod: upgrade gencodec #27288

Merged
merged 1 commit into from
May 17, 2023
Merged

Conversation

fjl
Copy link
Contributor

@fjl fjl commented May 17, 2023

This updates the gencodec tool version to latest. The newer version can generate conversions between slices and arrays. This feature is not used yet, we plan to start using it for blsync.

@fjl
Copy link
Contributor Author

fjl commented May 17, 2023

Example:

//go:generate go run github.com/fjl/gencodec@latest -type SyncAggregate -field-override syncAggregateMarshaling -out gen_syncaggregate_json.go

type SyncAggregate struct {
	Signers   [params.SyncCommitteeBitmaskSize]byte `gencodec:"required" json:"sync_committee_bits"`
	Signature [params.BLSSignatureSize]byte         `gencodec:"required" json:"sync_committee_signature"`
}

type syncAggregateMarshaling struct {
	Signers   hexutil.Bytes
	Signature hexutil.Bytes
}

gen_syncaggregate.go

// MarshalJSON marshals as JSON.
func (s SyncAggregate) MarshalJSON() ([]byte, error) {
	type SyncAggregate struct {
		Signers   hexutil.Bytes `gencodec:"required" json:"sync_committee_bits"`
		Signature hexutil.Bytes `gencodec:"required" json:"sync_committee_signature"`
	}
	var enc SyncAggregate
	enc.Signers = s.Signers[:]
	enc.Signature = s.Signature[:]
	return json.Marshal(&enc)
}

// UnmarshalJSON unmarshals from JSON.
func (s *SyncAggregate) UnmarshalJSON(input []byte) error {
	type SyncAggregate struct {
		Signers   *hexutil.Bytes `gencodec:"required" json:"sync_committee_bits"`
		Signature *hexutil.Bytes `gencodec:"required" json:"sync_committee_signature"`
	}
	var dec SyncAggregate
	if err := json.Unmarshal(input, &dec); err != nil {
		return err
	}
	if dec.Signers == nil {
		return errors.New("missing required field 'sync_committee_bits' for SyncAggregate")
	}
	if len(*dec.Signers) != len(s.Signers) {
		return errors.New("field 'sync_committee_bits' has wrong length, need 64 items")
	}
	copy(s.Signers[:], *dec.Signers)
	if dec.Signature == nil {
		return errors.New("missing required field 'sync_committee_signature' for SyncAggregate")
	}
	if len(*dec.Signature) != len(s.Signature) {
		return errors.New("field 'sync_committee_signature' has wrong length, need 96 items")
	}
	copy(s.Signature[:], *dec.Signature)
	return nil
}

Copy link
Contributor

@holiman holiman left a comment

Choose a reason for hiding this comment

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

.... that's great I suppose.. :)

EDIT: Have reviewed the code example above now, looks good to me. A bit strange that you use if len(*dec.Signature) != len(s.Signature), but return errors.New("field 'sync_committee_signature' has wrong length, need 96 items").

Instead of, say, consistently use 96, or consistently use len(s.Signature).

a:

if len(*dec.Signature) != 96 {
		return errors.New("field 'sync_committee_signature' has wrong length, need 96 items")
	}

b:

if len(*dec.Signature) != len(s.Signature) {
		return fmt.Errorf("field 'sync_committee_signature' has wrong length, need %d items", len(s.Signature))
	}

Doesn't matter though, looks good to me

@karalabe karalabe added this to the 1.11.7 milestone May 17, 2023
@karalabe karalabe merged commit 41fafa4 into ethereum:master May 17, 2023
@vinioleg2015zem vinioleg2015zem linked an issue May 21, 2023 that may be closed by this pull request
devopsbo3 pushed a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
devopsbo3 added a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
devopsbo3 added a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

> content://media/external/downloads/1000000694
4 participants