diff --git a/api/docgen/exampledata/extendedHeader.json b/api/docgen/exampledata/extendedHeader.json index 5da16246c1..d7a43824a0 100644 --- a/api/docgen/exampledata/extendedHeader.json +++ b/api/docgen/exampledata/extendedHeader.json @@ -46,7 +46,7 @@ } }, "commit": { - "height": 67374, + "height": "67374", "round": 0, "block_id": { "hash": "A7F6B1CF33313121539206754A73FDC22ADA48C4AA8C4BB4F707ED2E089E59D3", diff --git a/header/header.go b/header/header.go index 4db80d0ac3..b44447ce42 100644 --- a/header/header.go +++ b/header/header.go @@ -2,7 +2,6 @@ package header import ( "bytes" - "encoding/json" "errors" "fmt" "time" @@ -227,56 +226,22 @@ func (eh *ExtendedHeader) UnmarshalBinary(data []byte) error { return nil } -// MarshalJSON marshals an ExtendedHeader to JSON. The ValidatorSet is wrapped with amino encoding, -// to be able to unmarshal the crypto.PubKey type back from JSON. +// MarshalJSON marshals an ExtendedHeader to JSON. +// Uses tendermint encoder for tendermint compatibility. func (eh *ExtendedHeader) MarshalJSON() ([]byte, error) { + // alias the type to avoid going into recursion loop + // because tmjson.Marshal invokes custom json marshalling type Alias ExtendedHeader - validatorSet, err := tmjson.Marshal(eh.ValidatorSet) - if err != nil { - return nil, err - } - rawHeader, err := tmjson.Marshal(eh.RawHeader) - if err != nil { - return nil, err - } - return json.Marshal(&struct { - RawHeader json.RawMessage `json:"header"` - ValidatorSet json.RawMessage `json:"validator_set"` - *Alias - }{ - ValidatorSet: validatorSet, - RawHeader: rawHeader, - Alias: (*Alias)(eh), - }) + return tmjson.Marshal((*Alias)(eh)) } -// UnmarshalJSON unmarshals an ExtendedHeader from JSON. The ValidatorSet is wrapped with amino -// encoding, to be able to unmarshal the crypto.PubKey type back from JSON. +// UnmarshalJSON unmarshals an ExtendedHeader from JSON. +// Uses tendermint decoder for tendermint compatibility. func (eh *ExtendedHeader) UnmarshalJSON(data []byte) error { + // alias the type to avoid going into recursion loop + // because tmjson.Unmarshal invokes custom json unmarshalling type Alias ExtendedHeader - aux := &struct { - RawHeader json.RawMessage `json:"header"` - ValidatorSet json.RawMessage `json:"validator_set"` - *Alias - }{ - Alias: (*Alias)(eh), - } - if err := json.Unmarshal(data, &aux); err != nil { - return err - } - - valSet := new(core.ValidatorSet) - if err := tmjson.Unmarshal(aux.ValidatorSet, valSet); err != nil { - return err - } - rawHeader := new(RawHeader) - if err := tmjson.Unmarshal(aux.RawHeader, rawHeader); err != nil { - return err - } - - eh.ValidatorSet = valSet - eh.RawHeader = *rawHeader - return nil + return tmjson.Unmarshal(data, (*Alias)(eh)) } var _ libhead.Header[*ExtendedHeader] = &ExtendedHeader{}