-
Notifications
You must be signed in to change notification settings - Fork 257
/
payforblob.go
325 lines (280 loc) · 9.79 KB
/
payforblob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package types
import (
"crypto/sha256"
fmt "fmt"
"cosmossdk.io/errors"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
appns "github.com/celestiaorg/celestia-app/pkg/namespace"
appshares "github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/nmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
"github.com/tendermint/tendermint/crypto/merkle"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
coretypes "github.com/tendermint/tendermint/types"
"golang.org/x/exp/slices"
)
const (
URLMsgPayForBlobs = "/celestia.blob.v1.MsgPayForBlobs"
ShareSize = appconsts.ShareSize
)
// MsgPayForBlobs implements the `LegacyMsg` interface.
// See: https://docs.cosmos.network/v0.46/building-modules/messages-and-queries.html#legacy-amino-legacymsgs
var _ legacytx.LegacyMsg = &MsgPayForBlobs{}
func NewMsgPayForBlobs(signer string, blobs ...*Blob) (*MsgPayForBlobs, error) {
err := ValidateBlobs(blobs...)
if err != nil {
return nil, err
}
commitments, err := CreateCommitments(blobs)
if err != nil {
return nil, err
}
namespaceVersions, namespaceIds, sizes, shareVersions := extractBlobComponents(blobs)
namespaces := []appns.Namespace{}
for i := range namespaceVersions {
if namespaceVersions[i] > appconsts.NamespaceVersionMaxValue {
return nil, fmt.Errorf("namespace version %d is too large (max %d)", namespaceVersions[i], appconsts.NamespaceVersionMaxValue)
}
namespace, err := appns.New(uint8(namespaceVersions[i]), namespaceIds[i])
if err != nil {
return nil, err
}
namespaces = append(namespaces, namespace)
}
msg := &MsgPayForBlobs{
Signer: signer,
Namespaces: namespacesToBytes(namespaces),
ShareCommitments: commitments,
BlobSizes: sizes,
ShareVersions: shareVersions,
}
return msg, msg.ValidateBasic()
}
func namespacesToBytes(namespaces []appns.Namespace) (result [][]byte) {
for _, namespace := range namespaces {
result = append(result, namespace.Bytes())
}
return result
}
// Route fulfills the legacytx.LegacyMsg interface
func (msg *MsgPayForBlobs) Route() string { return RouterKey }
// Type fulfills the legacytx.LegacyMsg interface
func (msg *MsgPayForBlobs) Type() string {
return URLMsgPayForBlobs
}
// ValidateBasic fulfills the sdk.Msg interface by performing stateless
// validity checks on the msg that also don't require having the actual blob(s)
func (msg *MsgPayForBlobs) ValidateBasic() error {
if len(msg.Namespaces) == 0 {
return ErrNoNamespaces
}
if len(msg.ShareVersions) == 0 {
return ErrNoShareVersions
}
if len(msg.BlobSizes) == 0 {
return ErrNoBlobSizes
}
if len(msg.ShareCommitments) == 0 {
return ErrNoShareCommitments
}
if len(msg.Namespaces) != len(msg.ShareVersions) || len(msg.Namespaces) != len(msg.BlobSizes) || len(msg.Namespaces) != len(msg.ShareCommitments) {
return ErrMismatchedNumberOfPFBComponent.Wrapf(
"namespaces %d blob sizes %d share versions %d share commitments %d",
len(msg.Namespaces), len(msg.BlobSizes), len(msg.ShareVersions), len(msg.ShareCommitments),
)
}
for _, namespace := range msg.Namespaces {
ns, err := appns.From(namespace)
if err != nil {
return errors.Wrap(ErrInvalidNamespace, err.Error())
}
err = ValidateBlobNamespace(ns)
if err != nil {
return err
}
}
for _, v := range msg.ShareVersions {
if v != uint32(appconsts.ShareVersionZero) {
return ErrUnsupportedShareVersion
}
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return err
}
for _, commitment := range msg.ShareCommitments {
if len(commitment) != appconsts.HashLength() {
return ErrInvalidShareCommitment
}
}
return nil
}
// ValidateBlobNamespace returns an error if the provided namespace is reserved,
// parity shares, or tail padding.
func ValidateBlobNamespace(ns appns.Namespace) error {
if ns.IsReserved() {
return ErrReservedNamespace.Wrapf("got namespace: %x, want: > %x", ns, appns.MaxReservedNamespace)
}
if ns.IsParityShares() {
return ErrParitySharesNamespace
}
if ns.IsTailPadding() {
return ErrTailPaddingNamespace
}
return nil
}
// GetSignBytes fulfills the legacytx.LegacyMsg interface by returning a deterministic set
// of bytes to sign over
func (msg *MsgPayForBlobs) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners fulfills the sdk.Msg interface by returning the signer's address
func (msg *MsgPayForBlobs) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{address}
}
// CreateCommitment generates the share commitment for a given blob.
// See [Message layout rationale] and [Non-interactive default rules].
//
// [Message layout rationale]: https://github.com/celestiaorg/celestia-specs/blob/e59efd63a2165866584833e91e1cb8a6ed8c8203/src/rationale/message_block_layout.md?plain=1#L12
// [Non-interactive default rules]: https://github.com/celestiaorg/celestia-specs/blob/e59efd63a2165866584833e91e1cb8a6ed8c8203/src/rationale/message_block_layout.md?plain=1#L36
func CreateCommitment(blob *Blob) ([]byte, error) {
coreblob := coretypes.Blob{
NamespaceID: blob.NamespaceId,
Data: blob.Data,
ShareVersion: uint8(blob.ShareVersion),
NamespaceVersion: uint8(blob.NamespaceVersion),
}
shares, err := appshares.SplitBlobs(0, nil, []coretypes.Blob{coreblob}, false)
if err != nil {
return nil, err
}
// the commitment is the root of a merkle mountain range with max tree size
// determined by the number of roots required to create a share commitment
// over that blob. The size of the tree is only increased if the number of
// subtree roots surpasses a constant threshold.
subTreeWidth := appshares.SubTreeWidth(len(shares), appconsts.DefaultSubtreeRootThreshold)
treeSizes, err := merkleMountainRangeSizes(uint64(len(shares)), uint64(subTreeWidth))
if err != nil {
return nil, err
}
leafSets := make([][][]byte, len(treeSizes))
cursor := uint64(0)
for i, treeSize := range treeSizes {
leafSets[i] = appshares.ToBytes(shares[cursor : cursor+treeSize])
cursor = cursor + treeSize
}
// create the commitments by pushing each leaf set onto an nmt
subTreeRoots := make([][]byte, len(leafSets))
for i, set := range leafSets {
// create the nmt todo(evan) use nmt wrapper
tree := nmt.New(sha256.New(), nmt.NamespaceIDSize(appns.NamespaceSize), nmt.IgnoreMaxNamespace(true))
for _, leaf := range set {
namespace, err := appns.New(uint8(blob.NamespaceVersion), blob.NamespaceId)
if err != nil {
return nil, err
}
// the namespace must be added again here even though it is already
// included in the leaf to ensure that the hash will match that of
// the nmt wrapper (pkg/wrapper). Each namespace is added to keep
// the namespace in the share, and therefore the parity data, while
// also allowing for the manual addition of the parity namespace to
// the parity data.
nsLeaf := make([]byte, 0)
nsLeaf = append(nsLeaf, namespace.Bytes()...)
nsLeaf = append(nsLeaf, leaf...)
err = tree.Push(nsLeaf)
if err != nil {
return nil, err
}
}
// add the root
root, err := tree.Root()
if err != nil {
return nil, err
}
subTreeRoots[i] = root
}
return merkle.HashFromByteSlices(subTreeRoots), nil
}
func CreateCommitments(blobs []*Blob) ([][]byte, error) {
commitments := make([][]byte, len(blobs))
for i, blob := range blobs {
commitment, err := CreateCommitment(blob)
if err != nil {
return nil, err
}
commitments[i] = commitment
}
return commitments, nil
}
// ValidateBlobs performs basic checks over the components of one or more PFBs.
func ValidateBlobs(blobs ...*Blob) error {
if len(blobs) == 0 {
return ErrNoBlobs
}
for _, blob := range blobs {
if blob.NamespaceVersion > appconsts.NamespaceVersionMaxValue {
return fmt.Errorf("namespace version %d is too large", blob.NamespaceVersion)
}
ns, err := appns.New(uint8(blob.NamespaceVersion), blob.NamespaceId)
if err != nil {
return err
}
err = ns.ValidateBlobNamespace()
if err != nil {
return err
}
if len(blob.Data) == 0 {
return ErrZeroBlobSize
}
if !slices.Contains(appconsts.SupportedShareVersions, uint8(blob.ShareVersion)) {
return ErrUnsupportedShareVersion
}
}
return nil
}
// extractBlobComponents separates and returns the components of a slice of
// blobs.
func extractBlobComponents(pblobs []*tmproto.Blob) (namespaceVersions []uint32, namespaceIds [][]byte, sizes []uint32, shareVersions []uint32) {
namespaceVersions = make([]uint32, len(pblobs))
namespaceIds = make([][]byte, len(pblobs))
sizes = make([]uint32, len(pblobs))
shareVersions = make([]uint32, len(pblobs))
for i, pblob := range pblobs {
namespaceVersions[i] = pblob.NamespaceVersion
namespaceIds[i] = pblob.NamespaceId
sizes[i] = uint32(len(pblob.Data))
shareVersions[i] = pblob.ShareVersion
}
return namespaceVersions, namespaceIds, sizes, shareVersions
}
// merkleMountainRangeSizes returns the sizes (number of leaf nodes) of the
// trees in a merkle mountain range constructed for a given totalSize and
// maxTreeSize.
//
// https://docs.grin.mw/wiki/chain-state/merkle-mountain-range/
// https://github.com/opentimestamps/opentimestamps-server/blob/master/doc/merkle-mountain-range.md
// TODO: potentially rename function because this doesn't return heights
func merkleMountainRangeSizes(totalSize, maxTreeSize uint64) ([]uint64, error) {
var treeSizes []uint64
for totalSize != 0 {
switch {
case totalSize >= maxTreeSize:
treeSizes = append(treeSizes, maxTreeSize)
totalSize = totalSize - maxTreeSize
case totalSize < maxTreeSize:
treeSize, err := appshares.RoundDownPowerOfTwo(totalSize)
if err != nil {
return treeSizes, err
}
treeSizes = append(treeSizes, treeSize)
totalSize = totalSize - treeSize
}
}
return treeSizes, nil
}