Skip to content

Commit

Permalink
refactor(blob): removing jsonProof (celestiaorg#2652)
Browse files Browse the repository at this point in the history
Closes celestiaorg#2634 . nmt.Proof has json serialization built in now so
jsonProof is unnecessary and results in missing fields from the original
struct.

Only adds additional fields to the JSON response so not breaking
  • Loading branch information
distractedm1nd authored Sep 5, 2023
1 parent f9cf180 commit da8244e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 39 deletions.
37 changes: 0 additions & 37 deletions blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/celestiaorg/nmt"

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

// Commitment is a Merkle Root of the subtree built from shares of the Blob.
Expand Down Expand Up @@ -62,42 +61,6 @@ func (p Proof) equal(input Proof) error {
return nil
}

type jsonProof struct {
Start int `json:"start"`
End int `json:"end"`
Nodes [][]byte `json:"nodes"`
}

func (p *Proof) MarshalJSON() ([]byte, error) {
proofs := make([]jsonProof, 0, p.Len())
for _, pp := range *p {
proofs = append(proofs, jsonProof{
Start: pp.Start(),
End: pp.End(),
Nodes: pp.Nodes(),
})
}

return json.Marshal(proofs)
}

func (p *Proof) UnmarshalJSON(data []byte) error {
var proofs []jsonProof
err := json.Unmarshal(data, &proofs)
if err != nil {
return err
}

nmtProofs := make([]*nmt.Proof, len(proofs))
for i, jProof := range proofs {
nmtProof := nmt.NewInclusionProof(jProof.Start, jProof.End, jProof.Nodes, ipld.NMTIgnoreMaxNamespace)
nmtProofs[i] = &nmtProof
}

*p = nmtProofs
return nil
}

// Blob represents any application-specific binary data that anyone can submit to Celestia.
type Blob struct {
types.Blob `json:"blob"`
Expand Down
5 changes: 3 additions & 2 deletions blob/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"testing"
"time"

Expand Down Expand Up @@ -272,14 +273,14 @@ func TestBlobService_Get(t *testing.T) {
doFn: func() (interface{}, error) {
proof, err := service.GetProof(ctx, 1, blobs0[1].Namespace(), blobs0[1].Commitment)
require.NoError(t, err)
return proof.MarshalJSON()
return json.Marshal(proof)
},
expectedResult: func(i interface{}, err error) {
require.NoError(t, err)
jsonData, ok := i.([]byte)
require.True(t, ok)
var proof Proof
require.NoError(t, proof.UnmarshalJSON(jsonData))
require.NoError(t, json.Unmarshal(jsonData, &proof))

newProof, err := service.GetProof(ctx, 1, blobs0[1].Namespace(), blobs0[1].Commitment)
require.NoError(t, err)
Expand Down

0 comments on commit da8244e

Please sign in to comment.