Skip to content

Commit

Permalink
fix(header)!: tendermint compatible json marshall (celestiaorg#3928)
Browse files Browse the repository at this point in the history
  • Loading branch information
zvolin authored and vgonkivs committed Nov 25, 2024
1 parent eff19e8 commit 699fb52
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 46 deletions.
2 changes: 1 addition & 1 deletion api/docgen/exampledata/extendedHeader.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
}
},
"commit": {
"height": 67374,
"height": "67374",
"round": 0,
"block_id": {
"hash": "A7F6B1CF33313121539206754A73FDC22ADA48C4AA8C4BB4F707ED2E089E59D3",
Expand Down
55 changes: 10 additions & 45 deletions header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package header

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -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{}

0 comments on commit 699fb52

Please sign in to comment.