github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/adm/src/wrappers.rs (about) 1 /* 2 * Copyright 2018 Intel Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * ------------------------------------------------------------------------------ 16 */ 17 use std; 18 19 use protobuf; 20 21 use sawtooth_sdk::messages; 22 23 #[derive(Debug)] 24 pub enum Error { 25 ParseError(String), 26 } 27 28 impl std::fmt::Display for Error { 29 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 30 match *self { 31 Error::ParseError(ref msg) => write!(f, "ParseError: {}", msg), 32 } 33 } 34 } 35 36 impl std::error::Error for Error { 37 fn description(&self) -> &str { 38 match *self { 39 Error::ParseError(ref msg) => msg, 40 } 41 } 42 43 fn cause(&self) -> Option<&std::error::Error> { 44 match *self { 45 Error::ParseError(_) => None, 46 } 47 } 48 } 49 50 #[derive(Serialize)] 51 pub struct Block { 52 pub batches: Vec<Batch>, 53 pub block_num: u64, 54 #[serde(skip)] 55 pub consensus: Vec<u8>, 56 pub header_signature: String, 57 pub previous_block_id: String, 58 pub state_root_hash: String, 59 } 60 61 impl Block { 62 pub fn try_from(block: messages::block::Block) -> Result<Self, Error> { 63 protobuf::parse_from_bytes(&block.header) 64 .map_err(|err| { 65 Error::ParseError(format!( 66 "Invalid BlockHeader {}: {}", 67 block.header_signature, err 68 )) 69 }) 70 .and_then(|block_header: messages::block::BlockHeader| { 71 block 72 .get_batches() 73 .iter() 74 .map(|batch| Batch::try_from(batch.clone())) 75 .collect::<Result<Vec<Batch>, Error>>() 76 .map(move |batches| Block { 77 batches: batches, 78 block_num: block_header.get_block_num(), 79 consensus: Vec::from(block_header.get_consensus()), 80 header_signature: String::from(block.get_header_signature()), 81 previous_block_id: String::from(block_header.get_previous_block_id()), 82 state_root_hash: String::from(block_header.get_state_root_hash()), 83 }) 84 }) 85 } 86 } 87 88 #[derive(Serialize)] 89 pub struct Batch { 90 pub header_signature: String, 91 pub signer_public_key: String, 92 pub transactions: Vec<Transaction>, 93 } 94 95 impl Batch { 96 pub fn try_from(batch: messages::batch::Batch) -> Result<Self, Error> { 97 protobuf::parse_from_bytes(&batch.header) 98 .map_err(|err| { 99 Error::ParseError(format!( 100 "Invalid BatchHeader {}: {}", 101 batch.header_signature, err 102 )) 103 }) 104 .and_then(|batch_header: messages::batch::BatchHeader| { 105 batch 106 .get_transactions() 107 .iter() 108 .map(|transaction| Transaction::try_from(&transaction.clone())) 109 .collect::<Result<Vec<Transaction>, Error>>() 110 .map(move |transactions| Batch { 111 header_signature: String::from(batch.get_header_signature()), 112 signer_public_key: String::from(batch_header.get_signer_public_key()), 113 transactions: transactions, 114 }) 115 }) 116 } 117 } 118 119 #[derive(Serialize)] 120 pub struct Transaction { 121 pub batcher_public_key: String, 122 pub dependencies: Vec<String>, 123 pub family_name: String, 124 pub family_version: String, 125 pub header_signature: String, 126 pub inputs: Vec<String>, 127 pub nonce: String, 128 pub outputs: Vec<String>, 129 #[serde(skip)] 130 pub payload: Vec<u8>, 131 pub payload_sha512: String, 132 pub signer_public_key: String, 133 } 134 135 impl Transaction { 136 pub fn try_from(transaction: &messages::transaction::Transaction) -> Result<Self, Error> { 137 protobuf::parse_from_bytes(&transaction.header) 138 .map_err(|err| { 139 Error::ParseError(format!( 140 "Invalid TransactionHeader {}: {}", 141 transaction.header_signature, err 142 )) 143 }) 144 .map( 145 |transaction_header: messages::transaction::TransactionHeader| Transaction { 146 batcher_public_key: String::from(transaction_header.get_batcher_public_key()), 147 dependencies: transaction_header.get_dependencies().to_vec(), 148 family_name: String::from(transaction_header.get_family_name()), 149 family_version: String::from(transaction_header.get_family_version()), 150 header_signature: String::from(transaction.get_header_signature()), 151 inputs: transaction_header.get_inputs().to_vec(), 152 nonce: String::from(transaction_header.get_nonce()), 153 outputs: transaction_header.get_outputs().to_vec(), 154 payload: Vec::from(transaction.get_payload()), 155 payload_sha512: String::from(transaction_header.get_payload_sha512()), 156 signer_public_key: String::from(transaction_header.get_signer_public_key()), 157 }, 158 ) 159 } 160 }