Skip to content

Commit

Permalink
fix(share): tendermint compatible get range result json marshall
Browse files Browse the repository at this point in the history
  • Loading branch information
zvolin committed Nov 11, 2024
1 parent 353141f commit 363eb67
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions nodebuilder/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package share

import (
"context"
"encoding/json"

tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/types"

libshare "github.com/celestiaorg/go-square/v2/share"
Expand All @@ -23,6 +25,44 @@ type GetRangeResult struct {
Proof *types.ShareProof
}

// MarshalJSON marshals an GetRangeResult to JSON. Uses tendermint encoder for proof for compatibility.
func (getRangeResult *GetRangeResult) MarshalJSON() ([]byte, error) {
type Alias GetRangeResult
proof, err := tmjson.Marshal(getRangeResult.Proof)
if err != nil {
return nil, err
}
return json.Marshal(&struct {
Proof json.RawMessage `json:"Proof"`
*Alias
}{
Proof: proof,
Alias: (*Alias)(getRangeResult),
})
}

// UnmarshalJSON unmarshals an GetRangeResult from JSON. Uses tendermint decoder for proof for compatibility.
func (getRangeResult *GetRangeResult) UnmarshalJSON(data []byte) error {
type Alias GetRangeResult
aux := &struct {
Proof json.RawMessage `json:"Proof"`
*Alias
}{
Alias: (*Alias)(getRangeResult),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
proof := new(types.ShareProof)
if err := tmjson.Unmarshal(aux.Proof, proof); err != nil {
return err
}

getRangeResult.Proof = proof

return nil
}

// Module provides access to any data square or block share on the network.
//
// All Get methods provided on Module follow the following flow:
Expand Down

0 comments on commit 363eb67

Please sign in to comment.