|
| 1 | +package blob |
| 2 | + |
| 3 | +// NOTE: This file is a trimmed copy of celestia-node's blob/blob.go |
| 4 | +// at release v0.28.4 (commit tag v0.28.4). We keep only the JSON- |
| 5 | +// compatible surface used by ev-node to avoid pulling celestia-app / |
| 6 | +// Cosmos-SDK dependencies. See pkg/blob/README.md for update guidance. |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "encoding/binary" |
| 11 | + "encoding/json" |
| 12 | + "fmt" |
| 13 | + |
| 14 | + "github.com/celestiaorg/go-square/merkle" |
| 15 | + "github.com/celestiaorg/go-square/v3/inclusion" |
| 16 | + libshare "github.com/celestiaorg/go-square/v3/share" |
| 17 | + "github.com/celestiaorg/nmt" |
| 18 | +) |
| 19 | + |
| 20 | +// Commitment is the Merkle subtree commitment for a blob. |
| 21 | +type Commitment []byte |
| 22 | + |
| 23 | +// Proof is a set of NMT proofs used to verify a blob inclusion. |
| 24 | +// This mirrors celestia-node's blob.Proof shape. |
| 25 | +type Proof []*nmt.Proof |
| 26 | + |
| 27 | +// DefaultMaxBlobSize is the default maximum blob size used by celestia-app (32 MiB). |
| 28 | +const DefaultMaxBlobSize = 32 * 1_048_576 // bytes |
| 29 | + |
| 30 | +// subtreeRootThreshold is copied from celestia-app/v6 appconsts.SubtreeRootThreshold. |
| 31 | +// It controls the branching factor when generating commitments. |
| 32 | +const subtreeRootThreshold = 64 |
| 33 | + |
| 34 | +// Blob represents application-specific binary data that can be submitted to Celestia. |
| 35 | +// It is intentionally compatible with celestia-node's blob.Blob JSON shape. |
| 36 | +type Blob struct { |
| 37 | + *libshare.Blob `json:"blob"` |
| 38 | + |
| 39 | + Commitment Commitment `json:"commitment"` |
| 40 | + |
| 41 | + // index is the index of the blob's first share in the EDS. |
| 42 | + // Only blobs retrieved from the chain will have this set; default is -1. |
| 43 | + index int |
| 44 | +} |
| 45 | + |
| 46 | +// NewBlobV0 builds a version 0 blob (the only version we currently need). |
| 47 | +func NewBlobV0(namespace libshare.Namespace, data []byte) (*Blob, error) { |
| 48 | + return NewBlob(libshare.ShareVersionZero, namespace, data, nil) |
| 49 | +} |
| 50 | + |
| 51 | +// NewBlob constructs a new blob from the provided namespace, data, signer, and share version. |
| 52 | +// This is a lightly adapted copy of celestia-node/blob.NewBlob. |
| 53 | +func NewBlob(shareVersion uint8, namespace libshare.Namespace, data, signer []byte) (*Blob, error) { |
| 54 | + if err := namespace.ValidateForBlob(); err != nil { |
| 55 | + return nil, fmt.Errorf("invalid namespace: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + libBlob, err := libshare.NewBlob(namespace, data, shareVersion, signer) |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("build blob: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + com, err := inclusion.CreateCommitment(libBlob, merkle.HashFromByteSlices, subtreeRootThreshold) |
| 64 | + if err != nil { |
| 65 | + return nil, fmt.Errorf("create commitment: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + return &Blob{ |
| 69 | + Blob: libBlob, |
| 70 | + Commitment: com, |
| 71 | + index: -1, |
| 72 | + }, nil |
| 73 | +} |
| 74 | + |
| 75 | +// Namespace returns the blob namespace. |
| 76 | +func (b *Blob) Namespace() libshare.Namespace { |
| 77 | + return b.Blob.Namespace() |
| 78 | +} |
| 79 | + |
| 80 | +// Index returns the blob's first share index in the EDS (or -1 if unknown). |
| 81 | +func (b *Blob) Index() int { |
| 82 | + return b.index |
| 83 | +} |
| 84 | + |
| 85 | +// MarshalJSON matches celestia-node's blob JSON encoding. |
| 86 | +func (b *Blob) MarshalJSON() ([]byte, error) { |
| 87 | + type jsonBlob struct { |
| 88 | + Namespace []byte `json:"namespace"` |
| 89 | + Data []byte `json:"data"` |
| 90 | + ShareVersion uint8 `json:"share_version"` |
| 91 | + Commitment Commitment `json:"commitment"` |
| 92 | + Signer []byte `json:"signer,omitempty"` |
| 93 | + Index int `json:"index"` |
| 94 | + } |
| 95 | + |
| 96 | + jb := &jsonBlob{ |
| 97 | + Namespace: b.Namespace().Bytes(), |
| 98 | + Data: b.Data(), |
| 99 | + ShareVersion: b.ShareVersion(), |
| 100 | + Commitment: b.Commitment, |
| 101 | + Signer: b.Signer(), |
| 102 | + Index: b.index, |
| 103 | + } |
| 104 | + return json.Marshal(jb) |
| 105 | +} |
| 106 | + |
| 107 | +// UnmarshalJSON matches celestia-node's blob JSON decoding. |
| 108 | +func (b *Blob) UnmarshalJSON(data []byte) error { |
| 109 | + type jsonBlob struct { |
| 110 | + Namespace []byte `json:"namespace"` |
| 111 | + Data []byte `json:"data"` |
| 112 | + ShareVersion uint8 `json:"share_version"` |
| 113 | + Commitment Commitment `json:"commitment"` |
| 114 | + Signer []byte `json:"signer,omitempty"` |
| 115 | + Index int `json:"index"` |
| 116 | + } |
| 117 | + |
| 118 | + var jb jsonBlob |
| 119 | + if err := json.Unmarshal(data, &jb); err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + ns, err := libshare.NewNamespaceFromBytes(jb.Namespace) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + blob, err := NewBlob(jb.ShareVersion, ns, jb.Data, jb.Signer) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + blob.Commitment = jb.Commitment |
| 134 | + blob.index = jb.Index |
| 135 | + *b = *blob |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +// MakeID constructs a blob ID by prefixing the commitment with the height (little endian). |
| 140 | +func MakeID(height uint64, commitment Commitment) []byte { |
| 141 | + id := make([]byte, 8+len(commitment)) |
| 142 | + binary.LittleEndian.PutUint64(id, height) |
| 143 | + copy(id[8:], commitment) |
| 144 | + return id |
| 145 | +} |
| 146 | + |
| 147 | +// SplitID splits a blob ID into height and commitment. |
| 148 | +// If the ID is malformed, it returns height 0 and nil commitment. |
| 149 | +func SplitID(id []byte) (uint64, Commitment) { |
| 150 | + if len(id) <= 8 { |
| 151 | + return 0, nil |
| 152 | + } |
| 153 | + return binary.LittleEndian.Uint64(id[:8]), id[8:] |
| 154 | +} |
| 155 | + |
| 156 | +// EqualCommitment compares the blob's commitment with the provided one. |
| 157 | +func (b *Blob) EqualCommitment(com Commitment) bool { |
| 158 | + return bytes.Equal(b.Commitment, com) |
| 159 | +} |
0 commit comments