-
Notifications
You must be signed in to change notification settings - Fork 20.1k
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
go.mod: upgrade gencodec #27288
Conversation
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
} |
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'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
This reverts commit 1142f55.
This reverts commit 1142f55.
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.