Skip to content

Commit

Permalink
Merge pull request #12 from polymerdao/giuseppe/no-panic-on-decode
Browse files Browse the repository at this point in the history
fix: do not panic decoding hex
  • Loading branch information
7AC committed Dec 14, 2022
2 parents 822c8f8 + 67d3896 commit 8cd875d
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 168 deletions.
46 changes: 20 additions & 26 deletions beacon-chain/lightclient/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prysmaticlabs/prysm/v3/config/params"
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
)

type ConfigJSON struct {
Expand Down Expand Up @@ -79,53 +78,48 @@ func (c *Config) MarshalJSON() ([]byte, error) {
}

func (c *Config) UnmarshalJSON(input []byte) error {
var config ConfigJSON
if err := json.Unmarshal(input, &config); err != nil {
var configJSON ConfigJSON
if err := json.Unmarshal(input, &configJSON); err != nil {
return err
}
capellaForkEpoch, err := strconv.ParseUint(config.CapellaForkEpoch, 10, 64)
config := Config{}
capellaForkEpoch, err := strconv.ParseUint(configJSON.CapellaForkEpoch, 10, 64)
if err != nil {
return err
}
bellatrixForkEpoch, err := strconv.ParseUint(config.BellatrixForkEpoch, 10, 64)
config.CapellaForkEpoch = types.Epoch(capellaForkEpoch)
bellatrixForkEpoch, err := strconv.ParseUint(configJSON.BellatrixForkEpoch, 10, 64)
if err != nil {
return err
}
altairForkEpoch, err := strconv.ParseUint(config.AltairForkEpoch, 10, 64)
config.BellatrixForkEpoch = types.Epoch(bellatrixForkEpoch)
altairForkEpoch, err := strconv.ParseUint(configJSON.AltairForkEpoch, 10, 64)
if err != nil {
return err
}
minSyncCommitteeParticipants, err := strconv.ParseUint(config.MinSyncCommitteeParticipants, 10, 64)
if err != nil {
config.AltairForkEpoch = types.Epoch(altairForkEpoch)
if config.MinSyncCommitteeParticipants, err = strconv.ParseUint(configJSON.MinSyncCommitteeParticipants, 10,
64); err != nil {
return err
}
genesisSlot, err := strconv.ParseUint(config.GenesisSlot, 10, 64)
genesisSlot, err := strconv.ParseUint(configJSON.GenesisSlot, 10, 64)
if err != nil {
return err
}
slotsPerEpoch, err := strconv.ParseUint(config.SlotsPerEpoch, 10, 64)
config.GenesisSlot = types.Slot(genesisSlot)
slotsPerEpoch, err := strconv.ParseUint(configJSON.SlotsPerEpoch, 10, 64)
if err != nil {
return err
}
epochsPerSyncCommitteePeriod, err := strconv.ParseUint(config.EpochsPerSyncCommitteePeriod, 10, 64)
config.SlotsPerEpoch = types.Slot(slotsPerEpoch)
epochsPerSyncCommitteePeriod, err := strconv.ParseUint(configJSON.EpochsPerSyncCommitteePeriod, 10, 64)
if err != nil {
return err
}
secondsPerSlot, err := strconv.ParseUint(config.SecondsPerSlot, 10, 64)
*c = Config{
CapellaForkEpoch: types.Epoch(capellaForkEpoch),
CapellaForkVersion: hexutil.MustDecode(config.CapellaForkVersion),
BellatrixForkEpoch: types.Epoch(bellatrixForkEpoch),
BellatrixForkVersion: hexutil.MustDecode(config.BellatrixForkVersion),
AltairForkEpoch: types.Epoch(altairForkEpoch),
AltairForkVersion: hexutil.MustDecode(config.AltairForkVersion),
GenesisForkVersion: hexutil.MustDecode(config.GenesisForkVersion),
MinSyncCommitteeParticipants: minSyncCommitteeParticipants,
GenesisSlot: types.Slot(genesisSlot),
DomainSyncCommittee: bytesutil.ToBytes4(hexutil.MustDecode(config.DomainSyncCommittee)),
SlotsPerEpoch: types.Slot(slotsPerEpoch),
EpochsPerSyncCommitteePeriod: types.Epoch(epochsPerSyncCommitteePeriod),
SecondsPerSlot: secondsPerSlot,
config.EpochsPerSyncCommitteePeriod = types.Epoch(epochsPerSyncCommitteePeriod)
if config.SecondsPerSlot, err = strconv.ParseUint(configJSON.SecondsPerSlot, 10, 64); err != nil {
return err
}
*c = config
return nil
}
Loading

0 comments on commit 8cd875d

Please sign in to comment.