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
use hex;
use protobuf::Message;
use std;
use std::error::Error as StdError;

use crate::protos;
use crate::protos::{
    FromBytes, FromNative, FromProto, IntoBytes, IntoNative, IntoProto, ProtoConversionError,
};
use crate::signing;

use super::transaction::Transaction;

#[derive(Clone)]
pub struct BatchHeader {
    signer_public_key: Vec<u8>,
    transaction_ids: Vec<Vec<u8>>,
}

impl BatchHeader {
    pub fn signer_public_key(&self) -> &[u8] {
        &self.signer_public_key
    }

    pub fn transaction_ids(&self) -> &[Vec<u8>] {
        &self.transaction_ids
    }
}

impl FromProto<protos::batch::BatchHeader> for BatchHeader {
    fn from_proto(header: protos::batch::BatchHeader) -> Result<Self, ProtoConversionError> {
        Ok(BatchHeader {
            signer_public_key: hex::decode(header.get_signer_public_key())?,
            transaction_ids: header
                .get_transaction_ids()
                .to_vec()
                .into_iter()
                .map(|t| hex::decode(t).map_err(ProtoConversionError::from))
                .collect::<Result<_, _>>()?,
        })
    }
}

impl FromNative<BatchHeader> for protos::batch::BatchHeader {
    fn from_native(header: BatchHeader) -> Result<Self, ProtoConversionError> {
        let mut proto_header = protos::batch::BatchHeader::new();
        proto_header.set_signer_public_key(hex::encode(header.signer_public_key));
        proto_header.set_transaction_ids(
            header
                .transaction_ids
                .iter()
                .map(hex::encode)
                .collect::<protobuf::RepeatedField<String>>(),
        );
        Ok(proto_header)
    }
}

impl FromBytes<BatchHeader> for BatchHeader {
    fn from_bytes(bytes: &[u8]) -> Result<BatchHeader, ProtoConversionError> {
        let proto: protos::batch::BatchHeader =
            protobuf::parse_from_bytes(bytes).map_err(|_| {
                ProtoConversionError::SerializationError(
                    "Unable to get BatchHeader from bytes".to_string(),
                )
            })?;
        proto.into_native()
    }
}

impl IntoBytes for BatchHeader {
    fn into_bytes(self) -> Result<Vec<u8>, ProtoConversionError> {
        let proto = self.into_proto()?;
        let bytes = proto.write_to_bytes().map_err(|_| {
            ProtoConversionError::SerializationError(
                "Unable to get bytes from BatchHeader".to_string(),
            )
        })?;
        Ok(bytes)
    }
}

impl IntoProto<protos::batch::BatchHeader> for BatchHeader {}
impl IntoNative<BatchHeader> for protos::batch::BatchHeader {}

pub struct Batch {
    header: Vec<u8>,
    header_signature: String,
    transactions: Vec<Transaction>,
    trace: bool,
}

impl Batch {
    pub fn header(&self) -> &[u8] {
        &self.header
    }

    pub fn header_signature(&self) -> &str {
        &self.header_signature
    }

    pub fn transactions(&self) -> &[Transaction] {
        &self.transactions
    }

    pub fn trace(&self) -> bool {
        self.trace
    }

    pub fn into_pair(self) -> Result<BatchPair, BatchBuildError> {
        let header = BatchHeader::from_bytes(&self.header)?;

        Ok(BatchPair {
            batch: self,
            header,
        })
    }
}

pub struct BatchPair {
    batch: Batch,
    header: BatchHeader,
}

impl BatchPair {
    pub fn batch(&self) -> &Batch {
        &self.batch
    }

    pub fn header(&self) -> &BatchHeader {
        &self.header
    }

    pub fn take(self) -> (Batch, BatchHeader) {
        (self.batch, self.header)
    }
}

impl From<protos::batch::Batch> for Batch {
    fn from(batch: protos::batch::Batch) -> Self {
        Batch {
            header: batch.get_header().to_vec(),
            header_signature: batch.get_header_signature().to_string(),
            transactions: batch
                .get_transactions()
                .to_vec()
                .into_iter()
                .map(Transaction::from)
                .collect(),
            trace: batch.get_trace(),
        }
    }
}

#[derive(Debug)]
pub enum BatchBuildError {
    MissingField(String),
    SerializationError(String),
    DeserializationError(String),
    SigningError(String),
}

impl StdError for BatchBuildError {
    fn description(&self) -> &str {
        match *self {
            BatchBuildError::MissingField(ref msg) => msg,
            BatchBuildError::SerializationError(ref msg) => msg,
            BatchBuildError::DeserializationError(ref msg) => msg,
            BatchBuildError::SigningError(ref msg) => msg,
        }
    }
}

impl std::fmt::Display for BatchBuildError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            BatchBuildError::MissingField(ref s) => write!(f, "MissingField: {}", s),
            BatchBuildError::SerializationError(ref s) => write!(f, "SerializationError: {}", s),
            BatchBuildError::DeserializationError(ref s) => {
                write!(f, "DeserializationError: {}", s)
            }
            BatchBuildError::SigningError(ref s) => write!(f, "SigningError: {}", s),
        }
    }
}

impl From<ProtoConversionError> for BatchBuildError {
    fn from(e: ProtoConversionError) -> Self {
        BatchBuildError::DeserializationError(format!("{}", e))
    }
}

#[derive(Default, Clone)]
pub struct BatchBuilder {
    transactions: Option<Vec<Transaction>>,
    trace: Option<bool>,
}

impl BatchBuilder {
    pub fn new() -> Self {
        BatchBuilder::default()
    }

    pub fn with_transactions(mut self, transactions: Vec<Transaction>) -> BatchBuilder {
        self.transactions = Some(transactions);
        self
    }

    pub fn with_trace(mut self, trace: bool) -> BatchBuilder {
        self.trace = Some(trace);
        self
    }

    pub fn build_pair(self, signer: &signing::Signer) -> Result<BatchPair, BatchBuildError> {
        let transactions = self.transactions.ok_or_else(|| {
            BatchBuildError::MissingField("'transactions' field is required".to_string())
        })?;
        let trace = self.trace.unwrap_or(false);
        let transaction_ids = transactions
            .iter()
            .flat_map(|t| {
                vec![hex::decode(t.header_signature())
                    .map_err(|e| BatchBuildError::SerializationError(format!("{}", e)))]
            })
            .collect::<Result<_, _>>()?;

        let signer_public_key = signer.public_key().to_vec();

        let header = BatchHeader {
            signer_public_key,
            transaction_ids,
        };

        let header_proto: protos::batch::BatchHeader = header
            .clone()
            .into_proto()
            .map_err(|e| BatchBuildError::SerializationError(format!("{}", e)))?;
        let header_bytes = header_proto
            .write_to_bytes()
            .map_err(|e| BatchBuildError::SerializationError(format!("{}", e)))?;

        let header_signature = hex::encode(
            signer
                .sign(&header_bytes)
                .map_err(|e| BatchBuildError::SigningError(format!("{}", e)))?,
        );

        let batch = Batch {
            header: header_bytes,
            header_signature,
            transactions,
            trace,
        };

        Ok(BatchPair { batch, header })
    }

    pub fn build(self, signer: &signing::Signer) -> Result<Batch, BatchBuildError> {
        Ok(self.build_pair(signer)?.batch)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::signing::hash::HashSigner;
    use crate::signing::Signer;
    use protobuf::Message;
    use sawtooth_sdk;

    static KEY1: &str = "111111111111111111111111111111111111111111111111111111111111111111";
    static KEY2: &str = "222222222222222222222222222222222222222222222222222222222222222222";
    static KEY3: &str = "333333333333333333333333333333333333333333333333333333333333333333";
    static BYTES1: [u8; 4] = [0x01, 0x02, 0x03, 0x04];
    static BYTES2: [u8; 4] = [0x05, 0x06, 0x07, 0x08];
    static BYTES3: [u8; 4] = [0x09, 0x0a, 0x0b, 0x0c];
    static BYTES4: [u8; 4] = [0x0d, 0x0e, 0x0f, 0x10];
    static BYTES5: [u8; 4] = [0x11, 0x12, 0x13, 0x14];
    static SIGNATURE1: &str =
        "sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1";
    static SIGNATURE2: &str =
        "sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2";
    static SIGNATURE3: &str =
        "sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3";

    fn check_builder_batch(signer: &Signer, pair: &BatchPair) {
        assert_eq!(
            vec![
                SIGNATURE2.as_bytes().to_vec(),
                SIGNATURE3.as_bytes().to_vec()
            ],
            pair.header().transaction_ids()
        );
        assert_eq!(signer.public_key(), pair.header().signer_public_key());
        assert_eq!(
            vec![
                Transaction::new(
                    BYTES2.to_vec(),
                    hex::encode(SIGNATURE2.to_string()),
                    BYTES3.to_vec()
                ),
                Transaction::new(
                    BYTES4.to_vec(),
                    hex::encode(SIGNATURE3.to_string()),
                    BYTES5.to_vec()
                ),
            ],
            pair.batch().transactions()
        );
        assert_eq!(true, pair.batch().trace());
    }

    #[test]
    fn batch_builder_chain() {
        let signer = HashSigner::new();

        let pair = BatchBuilder::new()
            .with_transactions(vec![
                Transaction::new(
                    BYTES2.to_vec(),
                    hex::encode(SIGNATURE2.to_string()),
                    BYTES3.to_vec(),
                ),
                Transaction::new(
                    BYTES4.to_vec(),
                    hex::encode(SIGNATURE3.to_string()),
                    BYTES5.to_vec(),
                ),
            ])
            .with_trace(true)
            .build_pair(&signer)
            .unwrap();

        check_builder_batch(&signer, &pair);
    }

    #[test]
    fn batch_builder_separate() {
        let signer = HashSigner::new();

        let mut builder = BatchBuilder::new();
        builder = builder.with_transactions(vec![
            Transaction::new(
                BYTES2.to_vec(),
                hex::encode(SIGNATURE2.to_string()),
                BYTES3.to_vec(),
            ),
            Transaction::new(
                BYTES4.to_vec(),
                hex::encode(SIGNATURE3.to_string()),
                BYTES5.to_vec(),
            ),
        ]);
        builder = builder.with_trace(true);
        let pair = builder.build_pair(&signer).unwrap();

        check_builder_batch(&signer, &pair);
    }

    #[test]
    fn batch_header_fields() {
        let header = BatchHeader {
            signer_public_key: hex::decode(KEY1).unwrap(),
            transaction_ids: vec![hex::decode(KEY2).unwrap(), hex::decode(KEY3).unwrap()],
        };

        assert_eq!(KEY1, hex::encode(header.signer_public_key()));
        assert_eq!(
            vec![hex::decode(KEY2).unwrap(), hex::decode(KEY3).unwrap(),],
            header.transaction_ids()
        );
    }

    #[test]
    // test that the batch header can be converted into bytes and back correctly
    fn batch_header_bytes() {
        let original = BatchHeader {
            signer_public_key: hex::decode(KEY1).unwrap(),
            transaction_ids: vec![hex::decode(KEY2).unwrap(), hex::decode(KEY3).unwrap()],
        };

        let header_bytes = original.clone().into_bytes().unwrap();
        let header = BatchHeader::from_bytes(&header_bytes).unwrap();

        assert_eq!(
            hex::encode(original.signer_public_key()),
            hex::encode(header.signer_public_key())
        );
        assert_eq!(original.transaction_ids(), header.transaction_ids());
    }

    #[test]
    fn batch_header_sawtooth10_compatibility() {
        // Create protobuf bytes using the Sawtooth SDK
        let mut proto = sawtooth_sdk::messages::batch::BatchHeader::new();
        proto.set_signer_public_key(KEY1.to_string());
        proto.set_transaction_ids(protobuf::RepeatedField::from_vec(vec![
            KEY2.to_string(),
            KEY3.to_string(),
        ]));
        let header_bytes = proto.write_to_bytes().unwrap();

        // Deserialize the header bytes into our protobuf
        let header_proto: protos::batch::BatchHeader =
            protobuf::parse_from_bytes(&header_bytes).unwrap();

        // Convert to a BatchHeader
        let header: BatchHeader = header_proto.into_native().unwrap();

        assert_eq!(KEY1, hex::encode(header.signer_public_key()));
        assert_eq!(
            vec![hex::decode(KEY2).unwrap(), hex::decode(KEY3).unwrap(),],
            header.transaction_ids(),
        );
    }

    #[test]
    fn batch_fields() {
        let batch = Batch {
            header: BYTES1.to_vec(),
            header_signature: SIGNATURE1.to_string(),
            transactions: vec![
                Transaction::new(BYTES2.to_vec(), SIGNATURE2.to_string(), BYTES3.to_vec()),
                Transaction::new(BYTES4.to_vec(), SIGNATURE3.to_string(), BYTES5.to_vec()),
            ],
            trace: true,
        };

        assert_eq!(BYTES1.to_vec(), batch.header());
        assert_eq!(SIGNATURE1, batch.header_signature());
        assert_eq!(
            vec![
                Transaction::new(BYTES2.to_vec(), SIGNATURE2.to_string(), BYTES3.to_vec()),
                Transaction::new(BYTES4.to_vec(), SIGNATURE3.to_string(), BYTES5.to_vec()),
            ],
            batch.transactions()
        );
        assert_eq!(true, batch.trace());
    }

    #[test]
    fn batch_sawtooth10_compatibility() {}
}

#[cfg(all(feature = "nightly", test))]
mod benchmarks {
    extern crate test;
    use super::*;
    use crate::signing::hash::HashSigner;
    use test::Bencher;

    static KEY1: &str = "111111111111111111111111111111111111111111111111111111111111111111";
    static KEY2: &str = "222222222222222222222222222222222222222222222222222222222222222222";
    static KEY3: &str = "333333333333333333333333333333333333333333333333333333333333333333";
    static BYTES1: [u8; 4] = [0x01, 0x02, 0x03, 0x04];
    static BYTES2: [u8; 4] = [0x05, 0x06, 0x07, 0x08];
    static BYTES3: [u8; 4] = [0x09, 0x0a, 0x0b, 0x0c];
    static BYTES4: [u8; 4] = [0x0d, 0x0e, 0x0f, 0x10];
    static BYTES5: [u8; 4] = [0x11, 0x12, 0x13, 0x14];
    static SIGNATURE1: &str =
        "sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1sig1";
    static SIGNATURE2: &str =
        "sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2sig2";
    static SIGNATURE3: &str =
        "sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3sig3";

    #[bench]
    fn bench_batch_creation(b: &mut Bencher) {
        b.iter(|| Batch {
            header: BYTES1.to_vec(),
            header_signature: SIGNATURE1.to_string(),
            transactions: vec![
                Transaction::new(BYTES2.to_vec(), SIGNATURE2.to_string(), BYTES3.to_vec()),
                Transaction::new(BYTES4.to_vec(), SIGNATURE3.to_string(), BYTES5.to_vec()),
            ],
            trace: true,
        });
    }

    #[bench]
    fn bench_batch_builder(b: &mut Bencher) {
        let signer = HashSigner::new();
        let batch = BatchBuilder::new()
            .with_transactions(vec![
                Transaction::new(
                    BYTES2.to_vec(),
                    hex::encode(SIGNATURE2.to_string()),
                    BYTES3.to_vec(),
                ),
                Transaction::new(
                    BYTES4.to_vec(),
                    hex::encode(SIGNATURE3.to_string()),
                    BYTES5.to_vec(),
                ),
            ])
            .with_trace(true);
        b.iter(|| batch.clone().build_pair(&signer));
    }

    #[bench]
    fn bench_batch_header_into_native(b: &mut Bencher) {
        let mut proto_header = protos::batch::BatchHeader::new();
        proto_header.set_signer_public_key(KEY1.to_string());
        proto_header.set_transaction_ids(protobuf::RepeatedField::from_vec(vec![
            KEY2.to_string(),
            KEY3.to_string(),
        ]));
        b.iter(|| proto_header.clone().into_native());
    }

    #[bench]
    fn bench_batch_header_into_proto(b: &mut Bencher) {
        let native_header = BatchHeader {
            signer_public_key: hex::decode(KEY1).unwrap(),
            transaction_ids: vec![hex::decode(KEY2).unwrap(), hex::decode(KEY3).unwrap()],
        };
        b.iter(|| native_header.clone().into_proto());
    }
}