// ParseBatch parses and performs sanity checks on the payload of a batch transaction.
ParseBatch parses and performs sanity checks on the payload of a batch transaction.
// ParseBatch parses and performs sanity checks on the payload of a batch transaction .
func ParseBatch(payload []byte) (Batch, error) {
	r := bytes.NewReader(payload)
	b := make([]byte, 4)
	var batch Batch
	if _, err := io.ReadFull(r, b[:1]); err != nil {
		return batch, errors.Wrap(err, "batch: failed to decode number of transactions in batch")
	}
	batch.Size = b[0]
	if batch.Size == 0 {
		return batch, errors.New("batch: size must be greater than zero")
	}
	batch.Tags = make([]uint8, batch.Size)
	batch.Payloads = make([][]byte, batch.Size)
	for i := uint8(0); i < batch.Size; i++ {
		if _, err := io.ReadFull(r, b[:1]); err != nil {
			return batch, errors.Wrap(err, "batch: could not read tag")
		}
		if sys.Tag(b[0]) == sys.TagBatch {
			return batch, errors.New("batch: entries inside batch cannot be batch transactions themselves")
		}
		batch.Tags[i] = b[0]
		if _, err := io.ReadFull(r, b[:4]); err != nil {
			return batch, errors.Wrap(err, "batch: could not read payload size")
		}
		size := binary.BigEndian.Uint32(b[:4])
		if size > 2*1024*1024 {
			return batch, errors.New("batch: payload size exceeds 2MB")
		}
		batch.Payloads[i] = make([]byte, size)
		if _, err := io.ReadFull(r, batch.Payloads[i]); err != nil {
			return batch, errors.Wrap(err, "batch: could not read payload")
		}
	}
	return batch, nil
}
func ParseBatch ( payload [ ] byte ) ( Batch , error ) { r := bytes . NewReader ( payload ) b := make ( [ ] byte , 4 ) var batch Batch if _ , err := io . ReadFull ( r , b [ : 1 ] ) ; err != nil { return batch , errors . Wrap ( err , " " ) } batch . Size = b [ 0 ] if batch . Size == 0 { return batch , errors . New ( " " ) } batch . Tags = make ( [ ] uint8 , batch . Size ) batch . Payloads = make ( [ ] [ ] byte , batch . Size ) for i := uint8 ( 0 ) ; i < batch . Size ; i ++ { if _ , err := io . ReadFull ( r , b [ : 1 ] ) ; err != nil { return batch , errors . Wrap ( err , " " ) } if sys . Tag ( b [ 0 ] ) == sys . TagBatch { return batch , errors . New ( " " ) } batch . Tags [ i ] = b [ 0 ] if _ , err := io . ReadFull ( r , b [ : 4 ] ) ; err != nil { return batch , errors . Wrap ( err , " " ) } size := binary . BigEndian . Uint32 ( b [ : 4 ] ) if size > 2 * 1024 * 1024 { return batch , errors . New ( " " ) } batch . Payloads [ i ] = make ( [ ] byte , size ) if _ , err := io . ReadFull ( r , batch . Payloads [ i ] ) ; err != nil { return batch , errors . Wrap ( err , " " ) } } return batch , nil }
ParseBatch parses and performs sanity checks on the payload of a batch transaction.
ParseBatch parses and performs sanity checks on the payload of a batch transaction .