github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/rust/src/consensus/service.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 18 use consensus::engine::{Block, BlockId, Error, PeerId}; 19 use std::collections::HashMap; 20 21 /// Provides methods that allow the consensus engine to issue commands and requests. 22 pub trait Service { 23 // -- P2P -- 24 25 /// Send a consensus message to a specific, connected peer 26 fn send_to(&mut self, peer: &PeerId, message_type: &str, payload: Vec<u8>) 27 -> Result<(), Error>; 28 29 /// Broadcast a message to all connected peers 30 fn broadcast(&mut self, message_type: &str, payload: Vec<u8>) -> Result<(), Error>; 31 32 // -- Block Creation -- 33 34 /// Initialize a new block built on the block with the given previous id and 35 /// begin adding batches to it. If no previous id is specified, the current 36 /// head will be used. 37 fn initialize_block(&mut self, previous_id: Option<BlockId>) -> Result<(), Error>; 38 39 /// Stop adding batches to the current block and return a summary of its 40 /// contents. 41 fn summarize_block(&mut self) -> Result<Vec<u8>, Error>; 42 43 /// Insert the given consensus data into the block and sign it. If this call is successful, the 44 /// consensus engine will receive the block afterwards. 45 fn finalize_block(&mut self, data: Vec<u8>) -> Result<BlockId, Error>; 46 47 /// Stop adding batches to the current block and abandon it. 48 fn cancel_block(&mut self) -> Result<(), Error>; 49 50 // -- Block Directives -- 51 52 /// Update the prioritization of blocks to check 53 fn check_blocks(&mut self, priority: Vec<BlockId>) -> Result<(), Error>; 54 55 /// Update the block that should be committed 56 fn commit_block(&mut self, block_id: BlockId) -> Result<(), Error>; 57 58 /// Signal that this block is no longer being committed 59 fn ignore_block(&mut self, block_id: BlockId) -> Result<(), Error>; 60 61 /// Mark this block as invalid from the perspective of consensus 62 fn fail_block(&mut self, block_id: BlockId) -> Result<(), Error>; 63 64 // -- Queries -- 65 66 /// Retrieve consensus-related information about blocks 67 fn get_blocks(&mut self, block_ids: Vec<BlockId>) -> Result<HashMap<BlockId, Block>, Error>; 68 69 /// Get the chain head block. 70 fn get_chain_head(&mut self) -> Result<Block, Error>; 71 72 /// Read the value of settings as of the given block 73 fn get_settings( 74 &mut self, 75 block_id: BlockId, 76 keys: Vec<String>, 77 ) -> Result<HashMap<String, String>, Error>; 78 79 /// Read values in state as of the given block 80 fn get_state( 81 &mut self, 82 block_id: BlockId, 83 addresses: Vec<String>, 84 ) -> Result<HashMap<String, Vec<u8>>, Error>; 85 } 86 87 #[cfg(test)] 88 pub mod tests { 89 use super::*; 90 use std::default::Default; 91 92 pub struct MockService {} 93 94 impl Service for MockService { 95 fn send_to( 96 &mut self, 97 _peer: &PeerId, 98 _message_type: &str, 99 _payload: Vec<u8>, 100 ) -> Result<(), Error> { 101 Ok(()) 102 } 103 fn broadcast(&mut self, _message_type: &str, _payload: Vec<u8>) -> Result<(), Error> { 104 Ok(()) 105 } 106 fn initialize_block(&mut self, _previous_id: Option<BlockId>) -> Result<(), Error> { 107 Ok(()) 108 } 109 fn summarize_block(&mut self) -> Result<Vec<u8>, Error> { 110 Ok(Default::default()) 111 } 112 fn finalize_block(&mut self, _data: Vec<u8>) -> Result<BlockId, Error> { 113 Ok(Default::default()) 114 } 115 fn cancel_block(&mut self) -> Result<(), Error> { 116 Ok(()) 117 } 118 fn check_blocks(&mut self, _priority: Vec<BlockId>) -> Result<(), Error> { 119 Ok(()) 120 } 121 fn commit_block(&mut self, _block_id: BlockId) -> Result<(), Error> { 122 Ok(()) 123 } 124 fn ignore_block(&mut self, _block_id: BlockId) -> Result<(), Error> { 125 Ok(()) 126 } 127 fn fail_block(&mut self, _block_id: BlockId) -> Result<(), Error> { 128 Ok(()) 129 } 130 fn get_blocks( 131 &mut self, 132 _block_ids: Vec<BlockId>, 133 ) -> Result<HashMap<BlockId, Block>, Error> { 134 Ok(Default::default()) 135 } 136 fn get_chain_head(&mut self) -> Result<Block, Error> { 137 Ok(Default::default()) 138 } 139 fn get_settings( 140 &mut self, 141 _block_id: BlockId, 142 _settings: Vec<String>, 143 ) -> Result<HashMap<String, String>, Error> { 144 Ok(Default::default()) 145 } 146 fn get_state( 147 &mut self, 148 _block_id: BlockId, 149 _addresses: Vec<String>, 150 ) -> Result<HashMap<String, Vec<u8>>, Error> { 151 Ok(Default::default()) 152 } 153 } 154 }