Skip to content

Commit

Permalink
Ensure slice values are not nil.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Mar 2, 2024
1 parent 499e866 commit 595a7a6
Show file tree
Hide file tree
Showing 27 changed files with 557 additions and 28 deletions.
27 changes: 26 additions & 1 deletion api/v1/bellatrix/blindedbeaconblockbody_json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Attestant Limited.
// Copyright © 2022, 2024 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -95,23 +95,48 @@ func (b *BlindedBeaconBlockBody) unpack(data *blindedBeaconBlockBodyJSON) error
if data.ProposerSlashings == nil {
return errors.New("proposer slashings missing")
}
for i := range data.ProposerSlashings {
if data.ProposerSlashings[i] == nil {
return fmt.Errorf("proposer slashings entry %d missing", i)
}
}
b.ProposerSlashings = data.ProposerSlashings
if data.AttesterSlashings == nil {
return errors.New("attester slashings missing")
}
for i := range data.AttesterSlashings {
if data.AttesterSlashings[i] == nil {
return fmt.Errorf("attester slashings entry %d missing", i)
}
}
b.AttesterSlashings = data.AttesterSlashings
if data.Attestations == nil {
return errors.New("attestations missing")
}
for i := range data.Attestations {
if data.Attestations[i] == nil {
return fmt.Errorf("attestations entry %d missing", i)
}
}
b.Attestations = data.Attestations
if data.Deposits == nil {
return errors.New("deposits missing")
}
for i := range data.Deposits {
if data.Deposits[i] == nil {
return fmt.Errorf("deposits entry %d missing", i)
}
}
b.Deposits = data.Deposits
if data.VoluntaryExits == nil {
return errors.New("voluntary exits missing")
}
b.VoluntaryExits = data.VoluntaryExits
for i := range data.VoluntaryExits {
if data.VoluntaryExits[i] == nil {
return fmt.Errorf("voluntary exits entry %d missing", i)
}
}
if data.SyncAggregate == nil {
return errors.New("sync aggregate missing")
}
Expand Down
27 changes: 26 additions & 1 deletion api/v1/bellatrix/blindedbeaconblockbody_test.go

Large diffs are not rendered by default.

32 changes: 31 additions & 1 deletion api/v1/capella/blindedbeaconblockbody_json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Attestant Limited.
// Copyright © 2022, 2024 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -97,22 +97,47 @@ func (b *BlindedBeaconBlockBody) unpack(data *blindedBeaconBlockBodyJSON) error
if data.ProposerSlashings == nil {
return errors.New("proposer slashings missing")
}
for i := range data.ProposerSlashings {
if data.ProposerSlashings[i] == nil {
return fmt.Errorf("proposer slashings entry %d missing", i)
}
}
b.ProposerSlashings = data.ProposerSlashings
if data.AttesterSlashings == nil {
return errors.New("attester slashings missing")
}
for i := range data.AttesterSlashings {
if data.AttesterSlashings[i] == nil {
return fmt.Errorf("attester slashings entry %d missing", i)
}
}
b.AttesterSlashings = data.AttesterSlashings
if data.Attestations == nil {
return errors.New("attestations missing")
}
for i := range data.Attestations {
if data.Attestations[i] == nil {
return fmt.Errorf("attestations entry %d missing", i)
}
}
b.Attestations = data.Attestations
if data.Deposits == nil {
return errors.New("deposits missing")
}
for i := range data.Deposits {
if data.Deposits[i] == nil {
return fmt.Errorf("deposits entry %d missing", i)
}
}
b.Deposits = data.Deposits
if data.VoluntaryExits == nil {
return errors.New("voluntary exits missing")
}
for i := range data.VoluntaryExits {
if data.VoluntaryExits[i] == nil {
return fmt.Errorf("voluntary exits entry %d missing", i)
}
}
b.VoluntaryExits = data.VoluntaryExits
if data.SyncAggregate == nil {
return errors.New("sync aggregate missing")
Expand All @@ -125,6 +150,11 @@ func (b *BlindedBeaconBlockBody) unpack(data *blindedBeaconBlockBodyJSON) error
if data.BLSToExecutionChanges == nil {
b.BLSToExecutionChanges = make([]*capella.SignedBLSToExecutionChange, 0)
} else {
for i := range data.BLSToExecutionChanges {
if data.BLSToExecutionChanges[i] == nil {
return fmt.Errorf("bls to execution changes entry %d missing", i)
}
}
b.BLSToExecutionChanges = data.BLSToExecutionChanges
}

Expand Down
36 changes: 33 additions & 3 deletions api/v1/capella/blindedbeaconblockbody_test.go

Large diffs are not rendered by default.

38 changes: 37 additions & 1 deletion api/v1/deneb/blindedbeaconblockbody_json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Attestant Limited.
// Copyright © 2023, 2024 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -75,6 +75,7 @@ func (b *BlindedBeaconBlockBody) UnmarshalJSON(input []byte) error {
return b.unpack(&data)
}

//nolint:gocyclo
func (b *BlindedBeaconBlockBody) unpack(data *blindedBeaconBlockBodyJSON) error {
if data.RANDAOReveal == "" {
return errors.New("RANDAO reveal missing")
Expand Down Expand Up @@ -105,22 +106,47 @@ func (b *BlindedBeaconBlockBody) unpack(data *blindedBeaconBlockBodyJSON) error
if data.ProposerSlashings == nil {
return errors.New("proposer slashings missing")
}
for i := range data.ProposerSlashings {
if data.ProposerSlashings[i] == nil {
return fmt.Errorf("proposer slashings entry %d missing", i)
}
}
b.ProposerSlashings = data.ProposerSlashings
if data.AttesterSlashings == nil {
return errors.New("attester slashings missing")
}
for i := range data.AttesterSlashings {
if data.AttesterSlashings[i] == nil {
return fmt.Errorf("attester slashings entry %d missing", i)
}
}
b.AttesterSlashings = data.AttesterSlashings
if data.Attestations == nil {
return errors.New("attestations missing")
}
for i := range data.Attestations {
if data.Attestations[i] == nil {
return fmt.Errorf("attestations entry %d missing", i)
}
}
b.Attestations = data.Attestations
if data.Deposits == nil {
return errors.New("deposits missing")
}
for i := range data.Deposits {
if data.Deposits[i] == nil {
return fmt.Errorf("deposits entry %d missing", i)
}
}
b.Deposits = data.Deposits
if data.VoluntaryExits == nil {
return errors.New("voluntary exits missing")
}
for i := range data.VoluntaryExits {
if data.VoluntaryExits[i] == nil {
return fmt.Errorf("voluntary exits entry %d missing", i)
}
}
b.VoluntaryExits = data.VoluntaryExits
if data.SyncAggregate == nil {
return errors.New("sync aggregate missing")
Expand All @@ -133,11 +159,21 @@ func (b *BlindedBeaconBlockBody) unpack(data *blindedBeaconBlockBodyJSON) error
if data.BLSToExecutionChanges == nil {
b.BLSToExecutionChanges = make([]*capella.SignedBLSToExecutionChange, 0)
} else {
for i := range data.BLSToExecutionChanges {
if data.BLSToExecutionChanges[i] == nil {
return fmt.Errorf("bls to execution changes entry %d missing", i)
}
}
b.BLSToExecutionChanges = data.BLSToExecutionChanges
}
if data.BlobKZGCommitments == nil {
return errors.New("blob KZG commitments missing")
}
for i := range data.BlobKZGCommitments {
if data.BlobKZGCommitments[i] == "" {
return fmt.Errorf("blob KZG commitments entry %d missing", i)
}
}
b.BlobKZGCommitments = make([]deneb.KZGCommitment, len(data.BlobKZGCommitments))
for i := range data.BlobKZGCommitments {
data, err := hex.DecodeString(strings.TrimPrefix(data.BlobKZGCommitments[i], "0x"))
Expand Down
Loading

0 comments on commit 595a7a6

Please sign in to comment.