Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rpc): custom JSON ExtendedHeader marshal/unmarshaling using amino wrapper #1292

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func TestAllReturnValuesAreMarshalable(t *testing.T) {
}

func implementsMarshaler(t *testing.T, typ reflect.Type) { //nolint:unused
if typ.Implements(reflect.TypeOf(new(json.Marshaler)).Elem()) {
return
}

switch typ.Kind() {
case reflect.Struct:
for i := 0; i < typ.NumField(); i++ {
Expand Down
48 changes: 45 additions & 3 deletions header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package header
import (
"bytes"
"context"
"encoding/json"
"fmt"

"github.com/celestiaorg/celestia-node/share"

"github.com/ipfs/go-blockservice"
logging "github.com/ipfs/go-log/v2"

bts "github.com/tendermint/tendermint/libs/bytes"
amino "github.com/tendermint/tendermint/libs/json"
core "github.com/tendermint/tendermint/types"

"github.com/celestiaorg/celestia-app/pkg/da"
appshares "github.com/celestiaorg/celestia-app/pkg/shares"

"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/celestia-node/share"
)

var log = logging.Logger("header")
Expand Down Expand Up @@ -144,3 +146,43 @@ func (eh *ExtendedHeader) UnmarshalBinary(data []byte) error {
*eh = *out
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.
func (eh *ExtendedHeader) MarshalJSON() ([]byte, error) {
type Alias ExtendedHeader
validatorSet, err := amino.Marshal(eh.ValidatorSet)
if err != nil {
return nil, err
}
return json.Marshal(&struct {
ValidatorSet json.RawMessage `json:"validator_set"`
*Alias
}{
ValidatorSet: validatorSet,
Alias: (*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.
func (eh *ExtendedHeader) UnmarshalJSON(data []byte) error {
type Alias ExtendedHeader
aux := &struct {
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 := amino.Unmarshal(aux.ValidatorSet, valSet); err != nil {
return err
}

eh.ValidatorSet = valSet
return nil
}
22 changes: 19 additions & 3 deletions header/serde_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,29 @@ import (

func TestMarshalUnmarshalExtendedHeader(t *testing.T) {
in := RandExtendedHeader(t)
data, err := in.MarshalBinary()
binaryData, err := in.MarshalBinary()
distractedm1nd marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)

out := &ExtendedHeader{}
err = out.UnmarshalBinary(data)
err = out.UnmarshalBinary(binaryData)
require.NoError(t, err)
assert.Equal(t, in.ValidatorSet, out.ValidatorSet)
equalExtendedHeader(t, in, out)

// A custom JSON marshal/unmarshal is necessary which wraps the ValidatorSet with amino
// encoding, to be able to marshal the crypto.PubKey type back from JSON.
jsonData, err := in.MarshalJSON()
require.NoError(t, err)

out = &ExtendedHeader{}
err = out.UnmarshalJSON(jsonData)
require.NoError(t, err)
equalExtendedHeader(t, in, out)
}

func equalExtendedHeader(t *testing.T, in, out *ExtendedHeader) {
// ValidatorSet.totalVotingPower is not set (is a cached value that can be recomputed client side)
assert.Equal(t, in.ValidatorSet.Validators, out.ValidatorSet.Validators)
assert.Equal(t, in.ValidatorSet.Proposer, out.ValidatorSet.Proposer)
assert.True(t, in.DAH.Equals(out.DAH))
// not the check for equality as time.Time is not serialized exactly 1:1
assert.NotZero(t, out.RawHeader)
Expand Down