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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
//! Types related to creation and submission of blobs.
use std::iter;
#[cfg(feature = "uniffi")]
use std::sync::Arc;
use serde::{Deserialize, Serialize};
#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
use wasm_bindgen::prelude::*;
mod commitment;
mod msg_pay_for_blobs;
use crate::consts::appconsts;
#[cfg(feature = "uniffi")]
use crate::error::UniffiResult;
use crate::nmt::Namespace;
use crate::state::{AccAddress, AddressTrait};
use crate::{Error, Result, Share, bail_validation};
pub use self::commitment::Commitment;
pub use self::msg_pay_for_blobs::MsgPayForBlobs;
pub use celestia_proto::celestia::blob::v1::MsgPayForBlobs as RawMsgPayForBlobs;
pub use celestia_proto::proto::blob::v2::BlobProto as RawBlob;
pub use celestia_proto::proto::blob::v2::BlobTx as RawBlobTx;
/// Arbitrary data that can be stored in the network within certain [`Namespace`].
// NOTE: We don't use the `serde(try_from)` pattern for this type
// becase JSON representation needs to have `commitment` field but
// Protobuf definition doesn't.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "custom_serde::SerdeBlob", into = "custom_serde::SerdeBlob")]
#[cfg_attr(
all(feature = "wasm-bindgen", target_arch = "wasm32"),
wasm_bindgen(getter_with_clone, inspectable)
)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
pub struct Blob {
/// A [`Namespace`] the [`Blob`] belongs to.
pub namespace: Namespace,
/// Data stored within the [`Blob`].
pub data: Vec<u8>,
/// Version indicating the format in which [`Share`]s should be created from this [`Blob`].
pub share_version: u8,
/// A [`Commitment`] computed from the [`Blob`]s data.
pub commitment: Commitment,
/// Index of the blob's first share in the EDS. Only set for blobs retrieved from chain.
pub index: Option<u64>,
/// A signer of the blob, i.e. address of the account which submitted the blob.
///
/// Must be present in `share_version 1` and absent otherwise.
pub signer: Option<AccAddress>,
}
/// Params defines the parameters for the blob module.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[cfg_attr(
all(feature = "wasm-bindgen", target_arch = "wasm32"),
wasm_bindgen(getter_with_clone, inspectable)
)]
pub struct BlobParams {
/// Gas cost per blob byte
pub gas_per_blob_byte: u32,
/// Max square size
pub gov_max_square_size: u64,
}
/// List of blobs together with height they were published at
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[cfg_attr(
all(feature = "wasm-bindgen", target_arch = "wasm32"),
wasm_bindgen(getter_with_clone, inspectable)
)]
pub struct BlobsAtHeight {
/// Height the blobs were published at
pub height: u64,
/// Published blobs
pub blobs: Vec<Blob>,
}
impl Blob {
/// Create a new blob with the given data within the [`Namespace`], with optional signer.
///
/// # Notes
///
/// If present onchain, `signer` was verified by consensus node on blob submission.
///
/// # Errors
///
/// This function propagates any error from the [`Commitment`] creation.
///
/// # Example
///
/// ```
/// use celestia_types::{Blob, nmt::Namespace, state::AccAddress};
///
/// let my_namespace = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
/// let signer: AccAddress = "celestia1377k5an3f94v6wyaceu0cf4nq6gk2jtpc46g7h"
/// .parse()
/// .unwrap();
/// let blob = Blob::new(
/// my_namespace,
/// b"some data to store on blockchain".to_vec(),
/// Some(signer),
/// )
/// .expect("Failed to create a blob");
///
/// assert_eq!(
/// serde_json::to_string_pretty(&blob).unwrap(),
/// indoc::indoc! {r#"{
/// "namespace": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQIDBAU=",
/// "data": "c29tZSBkYXRhIHRvIHN0b3JlIG9uIGJsb2NrY2hhaW4=",
/// "share_version": 1,
/// "commitment": "AUpLKHYnlrfK0cX6DcGyryFMld1nJia+cjkCwXFTFgA=",
/// "index": -1,
/// "signer": "j71qdnFJas04ncZ4/CazBpFlSWE="
/// }"#},
/// );
/// ```
pub fn new(namespace: Namespace, data: Vec<u8>, signer: Option<AccAddress>) -> Result<Blob> {
let share_version = if signer.is_none() {
appconsts::SHARE_VERSION_ZERO
} else {
appconsts::SHARE_VERSION_ONE
};
let commitment =
Commitment::from_blob(namespace, &data[..], share_version, signer.as_ref())?;
Ok(Blob {
namespace,
data,
share_version,
commitment,
index: None,
signer,
})
}
/// Creates a `Blob` from [`RawBlob`].
pub fn from_raw(raw: RawBlob) -> Result<Blob> {
let namespace = Namespace::new(raw.namespace_version as u8, &raw.namespace_id)?;
let share_version =
u8::try_from(raw.share_version).map_err(|_| Error::UnsupportedShareVersion(u8::MAX))?;
let signer = raw.signer.try_into().map(AccAddress::new).ok();
let commitment =
Commitment::from_blob(namespace, &raw.data[..], share_version, signer.as_ref())?;
Ok(Blob {
namespace,
data: raw.data,
share_version,
commitment,
index: None,
signer,
})
}
/// Validate [`Blob`]s data with the [`Commitment`] it has.
///
/// # Errors
///
/// If validation fails, this function will return an error with a reason of failure.
///
/// # Example
///
/// ```
/// use celestia_types::Blob;
/// # use celestia_types::nmt::Namespace;
/// #
/// # let namespace = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
///
/// let mut blob = Blob::new(namespace, b"foo".to_vec(), None).unwrap();
///
/// assert!(blob.validate().is_ok());
///
/// let other_blob = Blob::new(namespace, b"bar".to_vec(), None).unwrap();
/// blob.commitment = other_blob.commitment;
///
/// assert!(blob.validate().is_err());
/// ```
pub fn validate(&self) -> Result<()> {
let computed_commitment = Commitment::from_blob(
self.namespace,
&self.data,
self.share_version,
self.signer.as_ref(),
)?;
if self.commitment != computed_commitment {
bail_validation!("blob commitment != localy computed commitment")
}
Ok(())
}
/// Validate [`Blob`]s data with a [`Commitment`].
///
/// # Errors
///
/// If validation fails, this function will return an error with a reason of failure.
///
/// # Example
///
/// ```
/// use celestia_types::Blob;
/// # use celestia_types::nmt::Namespace;
/// #
/// # let namespace = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
/// #
/// # let commitment = Blob::new(namespace, b"foo".to_vec(), None)
/// # .unwrap()
/// # .commitment;
///
/// let blob = Blob::new(namespace, b"foo".to_vec(), None).unwrap();
///
/// assert!(blob.validate_with_commitment(&commitment).is_ok());
///
/// let other_commitment = Blob::new(namespace, b"bar".to_vec(), None)
/// .unwrap()
/// .commitment;
///
/// assert!(blob.validate_with_commitment(&other_commitment).is_err());
/// ```
pub fn validate_with_commitment(&self, commitment: &Commitment) -> Result<()> {
self.validate()?;
if self.commitment != *commitment {
bail_validation!("blob commitment != commitment");
}
Ok(())
}
/// Encode the blob into a sequence of shares.
///
/// Check the [`Share`] documentation for more information about the share format.
///
/// # Errors
///
/// This function will return an error if [`InfoByte`] creation fails
/// or the data length overflows [`u32`].
///
/// # Example
///
/// ```
/// use celestia_types::Blob;
/// # use celestia_types::nmt::Namespace;
/// # let namespace = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
///
/// let blob = Blob::new(namespace, b"foo".to_vec(), None).unwrap();
/// let shares = blob.to_shares().unwrap();
///
/// assert_eq!(shares.len(), 1);
/// ```
///
/// [`Share`]: crate::share::Share
/// [`InfoByte`]: crate::share::InfoByte
pub fn to_shares(&self) -> Result<Vec<Share>> {
commitment::split_blob_to_shares(
self.namespace,
self.share_version,
&self.data,
self.signer.as_ref(),
)
}
/// Reconstructs a blob from shares.
///
/// # Errors
///
/// This function will return an error if:
/// - there is not enough shares to reconstruct the blob
/// - blob doesn't start with the first share
/// - shares are from any reserved namespace
/// - shares for the blob have different namespaces / share version
///
/// # Example
///
/// ```
/// use celestia_types::Blob;
/// # use celestia_types::nmt::Namespace;
/// # let namespace = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
///
/// let blob = Blob::new(namespace, b"foo".to_vec(), None).unwrap();
/// let shares = blob.to_shares().unwrap();
///
/// let reconstructed = Blob::reconstruct(&shares).unwrap();
///
/// assert_eq!(blob, reconstructed);
/// ```
pub fn reconstruct<'a, I>(shares: I) -> Result<Self>
where
I: IntoIterator<Item = &'a Share>,
{
let mut shares = shares.into_iter();
let first_share = shares.next().ok_or(Error::MissingShares)?;
let blob_len = first_share
.sequence_length()
.ok_or(Error::ExpectedShareWithSequenceStart)?;
let namespace = first_share.namespace();
if namespace.is_reserved() {
return Err(Error::UnexpectedReservedNamespace);
}
let share_version = first_share.info_byte().expect("non parity").version();
let signer = first_share.signer();
let shares_needed = shares_needed_for_blob(blob_len as usize, signer.is_some());
let mut data =
Vec::with_capacity(shares_needed * appconsts::CONTINUATION_SPARSE_SHARE_CONTENT_SIZE);
data.extend_from_slice(first_share.payload().expect("non parity"));
for _ in 1..shares_needed {
let share = shares.next().ok_or(Error::MissingShares)?;
if share.namespace() != namespace {
return Err(Error::BlobSharesMetadataMismatch(format!(
"expected namespace ({:?}) got ({:?})",
namespace,
share.namespace()
)));
}
let version = share.info_byte().expect("non parity").version();
if version != share_version {
return Err(Error::BlobSharesMetadataMismatch(format!(
"expected share version ({share_version}) got ({version})"
)));
}
if share.sequence_length().is_some() {
return Err(Error::UnexpectedSequenceStart);
}
data.extend_from_slice(share.payload().expect("non parity"));
}
// remove padding
data.truncate(blob_len as usize);
if share_version == appconsts::SHARE_VERSION_ZERO {
Self::new(namespace, data, None)
} else if share_version == appconsts::SHARE_VERSION_ONE {
// shouldn't happen as we have user namespace, seq start, and share v1
let signer = signer.ok_or(Error::MissingSigner)?;
Self::new(namespace, data, Some(signer))
} else {
Err(Error::UnsupportedShareVersion(share_version))
}
}
/// Reconstructs all the blobs from shares.
///
/// This function will seek shares that indicate start of the next blob (with
/// [`Share::sequence_length`]) and pass them to [`Blob::reconstruct`].
/// It will automatically ignore all shares that are within reserved namespaces
/// e.g. it is completely fine to pass whole [`ExtendedDataSquare`] to this
/// function and get all blobs in the block.
///
/// # Errors
///
/// This function propagates any errors from [`Blob::reconstruct`].
///
/// # Example
///
/// ```
/// use celestia_types::Blob;
/// # use celestia_types::nmt::Namespace;
/// # let namespace1 = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
/// # let namespace2 = Namespace::new_v0(&[2, 3, 4, 5, 6]).expect("Invalid namespace");
///
/// let blobs = vec![
/// Blob::new(namespace1, b"foo".to_vec(), None).unwrap(),
/// Blob::new(namespace2, b"bar".to_vec(), None).unwrap(),
/// ];
/// let shares: Vec<_> = blobs.iter().flat_map(|blob| blob.to_shares().unwrap()).collect();
///
/// let reconstructed = Blob::reconstruct_all(&shares).unwrap();
///
/// assert_eq!(blobs, reconstructed);
/// ```
///
/// [`ExtendedDataSquare`]: crate::ExtendedDataSquare
pub fn reconstruct_all<'a, I>(shares: I) -> Result<Vec<Self>>
where
I: IntoIterator<Item = &'a Share>,
{
let mut shares = shares
.into_iter()
.filter(|shr| !shr.namespace().is_reserved());
let mut blobs = Vec::with_capacity(2);
loop {
let mut blob = {
// find next share from blobs namespace that is sequence start
let Some(start) = shares.find(|&shr| shr.sequence_length().is_some()) else {
break;
};
iter::once(start).chain(&mut shares)
};
blobs.push(Blob::reconstruct(&mut blob)?);
}
Ok(blobs)
}
/// Get the amount of shares needed to encode this blob.
///
/// # Example
///
/// ```
/// use celestia_types::Blob;
/// # use celestia_types::nmt::Namespace;
/// # let namespace = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
///
/// let blob = Blob::new(namespace, b"foo".to_vec(), None).unwrap();
/// let shares_len = blob.shares_len();
///
/// let blob_shares = blob.to_shares().unwrap();
///
/// assert_eq!(shares_len, blob_shares.len());
/// ```
pub fn shares_len(&self) -> usize {
let Some(without_first_share) = self
.data
.len()
.checked_sub(appconsts::FIRST_SPARSE_SHARE_CONTENT_SIZE)
else {
return 1;
};
1 + without_first_share.div_ceil(appconsts::CONTINUATION_SPARSE_SHARE_CONTENT_SIZE)
}
}
#[cfg(feature = "uniffi")]
#[uniffi::export]
impl Blob {
/// Create a new blob with the given data within the [`Namespace`].
///
/// # Errors
///
/// This function propagates any error from the [`Commitment`] creation.
// constructor cannot be named `new`, otherwise it doesn't show up in Kotlin ¯\_(ツ)_/¯
#[uniffi::constructor(name = "create")]
pub fn uniffi_new(namespace: Arc<Namespace>, data: Vec<u8>) -> UniffiResult<Self> {
let namespace = Arc::unwrap_or_clone(namespace);
Ok(Blob::new(namespace, data, None)?)
}
/// Create a new blob with the given data within the [`Namespace`] and with given signer.
///
/// # Errors
///
/// This function propagates any error from the [`Commitment`] creation.
#[uniffi::constructor(name = "create_with_signer")]
pub fn uniffi_new_with_signer(
namespace: Arc<Namespace>,
data: Vec<u8>,
signer: AccAddress,
) -> UniffiResult<Blob> {
let namespace = Arc::unwrap_or_clone(namespace);
Ok(Blob::new(namespace, data, Some(signer))?)
}
/// A [`Namespace`] the [`Blob`] belongs to.
#[uniffi::method(name = "namespace")]
pub fn get_namespace(&self) -> Namespace {
self.namespace
}
/// Data stored within the [`Blob`].
#[uniffi::method(name = "data")]
pub fn get_data(&self) -> Vec<u8> {
self.data.clone()
}
/// Version indicating the format in which [`Share`]s should be created from this [`Blob`].
#[uniffi::method(name = "share_version")]
pub fn get_share_version(&self) -> u8 {
self.share_version
}
/// A [`Commitment`] computed from the [`Blob`]s data.
#[uniffi::method(name = "commitment")]
pub fn get_commitment(&self) -> Commitment {
self.commitment
}
/// Index of the blob's first share in the EDS. Only set for blobs retrieved from chain.
#[uniffi::method(name = "index")]
pub fn get_index(&self) -> Option<u64> {
self.index
}
/// A signer of the blob, i.e. address of the account which submitted the blob.
///
/// Must be present in `share_version 1` and absent otherwise.
#[uniffi::method(name = "signer")]
pub fn get_signer(&self) -> Option<AccAddress> {
self.signer
}
}
#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
#[wasm_bindgen]
impl Blob {
/// Create a new blob with the given data within the [`Namespace`].
#[wasm_bindgen(constructor)]
pub fn js_new(namespace: &Namespace, data: Vec<u8>) -> Result<Blob> {
Self::new(*namespace, data, None)
}
/// Clone a blob creating a new deep copy of it.
#[wasm_bindgen(js_name = clone)]
pub fn js_clone(&self) -> Blob {
self.clone()
}
}
impl From<Blob> for RawBlob {
fn from(value: Blob) -> RawBlob {
RawBlob {
namespace_id: value.namespace.id().to_vec(),
namespace_version: value.namespace.version() as u32,
data: value.data,
share_version: value.share_version as u32,
signer: value
.signer
.map(|addr| addr.as_bytes().to_vec())
.unwrap_or_default(),
}
}
}
fn shares_needed_for_blob(blob_len: usize, has_signer: bool) -> usize {
let mut first_share_content = appconsts::FIRST_SPARSE_SHARE_CONTENT_SIZE;
if has_signer {
first_share_content -= appconsts::SIGNER_SIZE;
}
let Some(without_first_share) = blob_len.checked_sub(first_share_content) else {
return 1;
};
1 + without_first_share.div_ceil(appconsts::CONTINUATION_SPARSE_SHARE_CONTENT_SIZE)
}
mod custom_serde {
use serde::de::Error as _;
use serde::ser::Error as _;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use tendermint_proto::serializers::bytes::base64string;
use crate::nmt::Namespace;
use crate::state::{AccAddress, AddressTrait};
use crate::{Error, Result};
use super::{Blob, Commitment, commitment};
mod index_serde {
use super::*;
/// Serialize [`Option<u64>`] as `i64` with `None` represented as `-1`.
pub fn serialize<S>(value: &Option<u64>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let x = value
.map(i64::try_from)
.transpose()
.map_err(S::Error::custom)?
.unwrap_or(-1);
serializer.serialize_i64(x)
}
/// Deserialize [`Option<u64>`] from `i64` with negative values as `None`.
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<u64>, D::Error>
where
D: Deserializer<'de>,
{
i64::deserialize(deserializer).map(|val| if val >= 0 { Some(val as u64) } else { None })
}
}
mod signer_serde {
use super::*;
/// Serialize signer as optional base64 string
pub fn serialize<S>(value: &Option<AccAddress>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if let Some(ref addr) = value.as_ref().map(|addr| addr.as_bytes()) {
base64string::serialize(addr, serializer)
} else {
serializer.serialize_none()
}
}
/// Deserialize signer from optional base64 string
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<AccAddress>, D::Error>
where
D: Deserializer<'de>,
{
let bytes: Vec<u8> = base64string::deserialize(deserializer)?;
if bytes.is_empty() {
Ok(None)
} else {
let addr = AccAddress::new(bytes.try_into().map_err(D::Error::custom)?);
Ok(Some(addr))
}
}
}
/// This is the copy of the `Blob` struct, to perform additional checks during deserialization
#[derive(Serialize, Deserialize)]
pub(super) struct SerdeBlob {
namespace: Namespace,
#[serde(with = "base64string")]
data: Vec<u8>,
share_version: u8,
commitment: Commitment,
// NOTE: celestia supports deserializing blobs without index, so we should too
#[serde(default, with = "index_serde")]
index: Option<u64>,
#[serde(default, with = "signer_serde")]
signer: Option<AccAddress>,
}
impl From<Blob> for SerdeBlob {
fn from(value: Blob) -> Self {
Self {
namespace: value.namespace,
data: value.data,
share_version: value.share_version,
commitment: value.commitment,
index: value.index,
signer: value.signer,
}
}
}
impl TryFrom<SerdeBlob> for Blob {
type Error = Error;
fn try_from(value: SerdeBlob) -> Result<Self> {
commitment::validate_blob(value.share_version, value.signer.is_some())?;
Ok(Blob {
namespace: value.namespace,
data: value.data,
share_version: value.share_version,
commitment: value.commitment,
index: value.index,
signer: value.signer,
})
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::nmt::{NS_ID_SIZE, NS_SIZE};
use crate::test_utils::random_bytes;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;
fn sample_blob() -> Blob {
serde_json::from_str(
r#"{
"namespace": "AAAAAAAAAAAAAAAAAAAAAAAAAAAADCBNOWAP3dM=",
"data": "8fIMqAB+kQo7+LLmHaDya8oH73hxem6lQWX1",
"share_version": 0,
"commitment": "D6YGsPWdxR8ju2OcOspnkgPG2abD30pSHxsFdiPqnVk=",
"index": -1
}"#,
)
.unwrap()
}
fn sample_blob_with_signer() -> Blob {
serde_json::from_str(
r#"{
"namespace": "AAAAAAAAAAAAAAAAAAAAAAAAALwwSWpxCuQb5+A=",
"data": "lQnnMKE=",
"share_version": 1,
"commitment": "dujykaNN+Ey7ET3QNdPG0g2uveriBvZusA3fLSOdMKU=",
"index": -1,
"signer": "Yjc3XldhbdYke5i8aSlggYxCCLE="
}"#,
)
.unwrap()
}
#[test]
fn create_new_blob_unsigned() {
use crate::consts::appconsts;
let ns = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
let blob = Blob::new(ns, b"some data to store on blockchain".to_vec(), None)
.expect("Failed to create blob");
let shares = blob.to_shares().expect("to_shares failed");
assert!(!shares.is_empty(), "expected at least one share");
let first = &shares[0];
// first share should be a sequence start
assert!(
first.sequence_length().is_some(),
"first share must be a sequence start"
);
// check share version and signer
let version = first.info_byte().expect("non parity share").version();
assert_eq!(
version,
appconsts::SHARE_VERSION_ZERO,
"unexpected share version for unsigned blob"
);
assert_eq!(
first.signer(),
None,
"unsigned blob must not carry a signer"
);
}
#[test]
fn create_new_blob_signed() {
use crate::consts::appconsts;
let ns = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
let signer: AccAddress = "celestia1377k5an3f94v6wyaceu0cf4nq6gk2jtpc46g7h"
.parse()
.expect("invalid signer");
let blob = Blob::new(
ns,
b"some data to store on blockchain".to_vec(),
Some(signer),
)
.expect("Failed to create signed blob");
let shares = blob.to_shares().expect("to_shares failed");
assert!(!shares.is_empty(), "expected at least one share");
let first = &shares[0];
// first share should be a sequence start
assert!(
first.sequence_length().is_some(),
"first share must be a sequence start"
);
// check share version and signer
let version = first.info_byte().expect("non parity share").version();
assert_eq!(
version,
appconsts::SHARE_VERSION_ONE,
"unexpected share version for signed blob"
);
assert_eq!(
first.signer(),
Some(signer),
"first share must carry the expected signer"
);
}
#[test]
fn create_from_raw() {
let expected = sample_blob();
let raw = RawBlob::from(expected.clone());
let created = Blob::from_raw(raw).unwrap();
assert_eq!(created, expected);
}
#[test]
fn create_from_raw_with_signer() {
let expected = sample_blob_with_signer();
let raw = RawBlob::from(expected.clone());
let created = Blob::from_raw(raw).unwrap();
assert_eq!(created, expected);
}
#[test]
fn validate_blob() {
sample_blob().validate().unwrap();
}
#[test]
fn validate_blob_with_signer() {
sample_blob_with_signer().validate().unwrap();
}
#[test]
fn validate_blob_commitment_mismatch() {
let mut blob = sample_blob();
blob.commitment = Commitment::new([7; 32]);
blob.validate().unwrap_err();
}
#[test]
fn deserialize_blob_with_missing_index() {
serde_json::from_str::<Blob>(
r#"{
"namespace": "AAAAAAAAAAAAAAAAAAAAAAAAAAAADCBNOWAP3dM=",
"data": "8fIMqAB+kQo7+LLmHaDya8oH73hxem6lQWX1",
"share_version": 0,
"commitment": "D6YGsPWdxR8ju2OcOspnkgPG2abD30pSHxsFdiPqnVk="
}"#,
)
.unwrap();
}
#[test]
fn deserialize_blob_with_share_version_and_signer_mismatch() {
// signer in v0
serde_json::from_str::<Blob>(
r#"{
"namespace": "AAAAAAAAAAAAAAAAAAAAAAAAALwwSWpxCuQb5+A=",
"data": "lQnnMKE=",
"share_version": 0,
"commitment": "dujykaNN+Ey7ET3QNdPG0g2uveriBvZusA3fLSOdMKU=",
"signer": "Yjc3XldhbdYke5i8aSlggYxCCLE="
}"#,
)
.unwrap_err();
// no signer in v1
serde_json::from_str::<Blob>(
r#"{
"namespace": "AAAAAAAAAAAAAAAAAAAAAAAAALwwSWpxCuQb5+A=",
"data": "lQnnMKE=",
"share_version": 1,
"commitment": "dujykaNN+Ey7ET3QNdPG0g2uveriBvZusA3fLSOdMKU=",
}"#,
)
.unwrap_err();
}
#[test]
fn reconstruct() {
for _ in 0..10 {
let len = rand::random::<usize>() % (1024 * 1024) + 1;
let data = random_bytes(len);
let ns = Namespace::const_v0(rand::random());
let blob = Blob::new(ns, data, None).unwrap();
let shares = blob.to_shares().unwrap();
assert_eq!(blob, Blob::reconstruct(&shares).unwrap());
}
}
#[test]
fn reconstruct_with_signer() {
for _ in 0..10 {
let len = rand::random::<usize>() % (1024 * 1024) + 1;
let data = random_bytes(len);
let ns = Namespace::const_v0(rand::random());
let signer = rand::random::<[u8; 20]>().into();
let blob = Blob::new(ns, data, Some(signer)).unwrap();
let shares = blob.to_shares().unwrap();
assert_eq!(blob, Blob::reconstruct(&shares).unwrap());
}
}
#[test]
fn reconstruct_empty() {
assert!(matches!(
Blob::reconstruct(&Vec::<Share>::new()),
Err(Error::MissingShares)
));
}
#[test]
fn reconstruct_not_sequence_start() {
let len = rand::random::<usize>() % (1024 * 1024) + 1;
let data = random_bytes(len);
let ns = Namespace::const_v0(rand::random());
let mut shares = Blob::new(ns, data, None).unwrap().to_shares().unwrap();
// modify info byte to remove sequence start bit
shares[0].as_mut()[NS_SIZE] &= 0b11111110;
assert!(matches!(
Blob::reconstruct(&shares),
Err(Error::ExpectedShareWithSequenceStart)
));
}
#[test]
fn reconstruct_reserved_namespace() {
for ns in (0..255).flat_map(|n| {
let mut v0 = [0; NS_ID_SIZE];
*v0.last_mut().unwrap() = n;
let mut v255 = [0xff; NS_ID_SIZE];
*v255.last_mut().unwrap() = n;
[Namespace::new_v0(&v0), Namespace::new_v255(&v255)]
}) {
let len = (rand::random::<usize>() % 1023 + 1) * 2;
let data = random_bytes(len);
let shares = Blob::new(ns.unwrap(), data, None)
.unwrap()
.to_shares()
.unwrap();
assert!(matches!(
Blob::reconstruct(&shares),
Err(Error::UnexpectedReservedNamespace)
));
}
}
#[test]
fn reconstruct_not_enough_shares() {
let len = rand::random::<usize>() % 1024 * 1024 + 2048;
let data = random_bytes(len);
let ns = Namespace::const_v0(rand::random());
let shares = Blob::new(ns, data, None).unwrap().to_shares().unwrap();
assert!(matches!(
// minimum for len is 4 so 3 will break stuff
Blob::reconstruct(&shares[..2]),
Err(Error::MissingShares)
));
}
#[test]
fn reconstruct_inconsistent_share_version() {
let len = rand::random::<usize>() % (1024 * 1024) + 512;
let data = random_bytes(len);
let ns = Namespace::const_v0(rand::random());
let mut shares = Blob::new(ns, data, None).unwrap().to_shares().unwrap();
// change share version in second share
shares[1].as_mut()[NS_SIZE] = 0b11111110;
assert!(matches!(
Blob::reconstruct(&shares),
Err(Error::BlobSharesMetadataMismatch(..))
));
}
#[test]
fn reconstruct_inconsistent_namespace() {
let len = rand::random::<usize>() % (1024 * 1024) + 512;
let data = random_bytes(len);
let ns = Namespace::const_v0(rand::random());
let ns2 = Namespace::const_v0(rand::random());
let mut shares = Blob::new(ns, data, None).unwrap().to_shares().unwrap();
// change namespace in second share
shares[1].as_mut()[..NS_SIZE].copy_from_slice(ns2.as_bytes());
assert!(matches!(
Blob::reconstruct(&shares),
Err(Error::BlobSharesMetadataMismatch(..))
));
}
#[test]
fn reconstruct_unexpected_sequence_start() {
let len = rand::random::<usize>() % (1024 * 1024) + 512;
let data = random_bytes(len);
let ns = Namespace::const_v0(rand::random());
let mut shares = Blob::new(ns, data, None).unwrap().to_shares().unwrap();
// modify info byte to add sequence start bit
shares[1].as_mut()[NS_SIZE] |= 0b00000001;
assert!(matches!(
Blob::reconstruct(&shares),
Err(Error::UnexpectedSequenceStart)
));
}
#[test]
fn reconstruct_all() {
let blobs: Vec<_> = (0..rand::random::<usize>() % 16 + 3)
.map(|_| {
let len = rand::random::<usize>() % (1024 * 1024) + 512;
let data = random_bytes(len);
let ns = Namespace::const_v0(rand::random());
Blob::new(ns, data, None).unwrap()
})
.collect();
let shares: Vec<_> = blobs
.iter()
.flat_map(|blob| blob.to_shares().unwrap())
.collect();
let reconstructed = Blob::reconstruct_all(&shares).unwrap();
assert_eq!(blobs, reconstructed);
}
}