From 9f815f518efe2cb10e0daef38c2cde514e945345 Mon Sep 17 00:00:00 2001 From: VeerChaurasia Date: Sun, 25 Aug 2024 22:29:10 +0530 Subject: [PATCH] Implement Config/types.go --- config/types.go | 81 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/config/types.go b/config/types.go index 1ff2cd6..aa8d5b7 100644 --- a/config/types.go +++ b/config/types.go @@ -1,16 +1,71 @@ package config - -type ChainConfig struct{ - +import ( + "encoding/hex" + "encoding/json" +) +type ChainConfig struct { + ChainID uint64 `json:"chain_id"` + GenesisTime uint64 `json:"genesis_time"` + GenesisRoot []byte `json:"genesis_root"` } -type Fork struct{ - epoch uint64 - fork_version []uint8 +type Fork struct { + Epoch uint64 `json:"epoch"` + ForkVersion []byte `json:"fork_version"` +} +type Forks struct { + Genesis Fork `json:"genesis"` + Altair Fork `json:"altair"` + Bellatrix Fork `json:"bellatrix"` + Capella Fork `json:"capella"` + Deneb Fork `json:"deneb"` +} +func (c ChainConfig) MarshalJSON() ([]byte, error) { + type Alias ChainConfig + return json.Marshal(&struct { + Alias + GenesisRoot string `json:"genesis_root"` + }{ + Alias: (Alias)(c), + GenesisRoot: hex.EncodeToString(c.GenesisRoot), + }) +} +func (c *ChainConfig) UnmarshalJSON(data []byte) error { + type Alias ChainConfig + aux := &struct { + *Alias + GenesisRoot string `json:"genesis_root"` + }{ + Alias: (*Alias)(c), + } + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + var err error + c.GenesisRoot, err = hex.DecodeString(aux.GenesisRoot) + return err +} +func (f Fork) MarshalJSON() ([]byte, error) { + type Alias Fork + return json.Marshal(&struct { + Alias + ForkVersion string `json:"fork_version"` + }{ + Alias: (Alias)(f), + ForkVersion: hex.EncodeToString(f.ForkVersion), + }) +} +func (f *Fork) UnmarshalJSON(data []byte) error { + type Alias Fork + aux := &struct { + *Alias + ForkVersion string `json:"fork_version"` + }{ + Alias: (*Alias)(f), + } + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + var err error + f.ForkVersion, err = hex.DecodeString(aux.ForkVersion) + return err } -type Forks struct{ - genesis Fork - altair Fork - bellatrix Fork - capella Fork - deneb Fork -} \ No newline at end of file