|
| 1 | +// Copyright 2019-2020 Kodebox, Inc. |
| 2 | +// This file is part of CodeChain. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use super::super::errors; |
| 18 | +use super::super::traits::IBC; |
| 19 | +use super::super::types::IBCQueryResult; |
| 20 | +use ccore::ibc; |
| 21 | +use ccore::{BlockChainClient, BlockId, StateInfo}; |
| 22 | +use jsonrpc_core::Result; |
| 23 | +use rustc_serialize::hex::ToHex; |
| 24 | +use std::sync::Arc; |
| 25 | + |
| 26 | +pub struct IBCClient<C> |
| 27 | +where |
| 28 | + C: StateInfo + BlockChainClient, { |
| 29 | + client: Arc<C>, |
| 30 | +} |
| 31 | + |
| 32 | +impl<C> IBCClient<C> |
| 33 | +where |
| 34 | + C: StateInfo + BlockChainClient, |
| 35 | +{ |
| 36 | + pub fn new(client: Arc<C>) -> Self { |
| 37 | + Self { |
| 38 | + client, |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl<C> IBC for IBCClient<C> |
| 44 | +where |
| 45 | + C: StateInfo + 'static + Send + Sync + BlockChainClient, |
| 46 | +{ |
| 47 | + fn query_client_consensus_state( |
| 48 | + &self, |
| 49 | + client_id: String, |
| 50 | + block_number: Option<u64>, |
| 51 | + ) -> Result<Option<IBCQueryResult>> { |
| 52 | + let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest); |
| 53 | + let mut state = self.client.state_at(block_id).ok_or_else(errors::state_not_exist)?; |
| 54 | + let block_number = match self.client.block_number(&block_id) { |
| 55 | + None => return Ok(None), |
| 56 | + Some(block_number) => block_number, |
| 57 | + }; |
| 58 | + |
| 59 | + let mut context = ibc::context::TopLevelContext::new(&mut state); |
| 60 | + let client_manager = ibc::client::Manager::new(); |
| 61 | + let client_state = |
| 62 | + client_manager.query(&mut context, &client_id).map_err(|_| errors::ibc_client_not_exist())?; |
| 63 | + |
| 64 | + let consensus_state = client_state.get_consensus_state(&mut context); |
| 65 | + |
| 66 | + let rlp_encoded_consensus_state = consensus_state.encode(); |
| 67 | + |
| 68 | + Ok(Some(IBCQueryResult { |
| 69 | + block_number, |
| 70 | + raw: rlp_encoded_consensus_state.to_hex(), |
| 71 | + // FIXME |
| 72 | + proof: "".to_string(), |
| 73 | + })) |
| 74 | + } |
| 75 | + |
| 76 | + fn query_header(&self, block_number: Option<u64>) -> Result<Option<String>> { |
| 77 | + let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest); |
| 78 | + let header = match self.client.block_header(&block_id) { |
| 79 | + None => return Ok(None), |
| 80 | + Some(header) => header, |
| 81 | + }; |
| 82 | + |
| 83 | + Ok(Some(header.into_inner().to_hex())) |
| 84 | + } |
| 85 | + |
| 86 | + fn query_client_root( |
| 87 | + &self, |
| 88 | + client_id: String, |
| 89 | + other_block_number: u64, |
| 90 | + this_block_number: Option<u64>, |
| 91 | + ) -> Result<Option<IBCQueryResult>> { |
| 92 | + let block_id = this_block_number.map(BlockId::Number).unwrap_or(BlockId::Latest); |
| 93 | + let mut state = self.client.state_at(block_id).ok_or_else(errors::state_not_exist)?; |
| 94 | + let block_number = match self.client.block_number(&block_id) { |
| 95 | + None => return Ok(None), |
| 96 | + Some(block_number) => block_number, |
| 97 | + }; |
| 98 | + |
| 99 | + let mut context = ibc::context::TopLevelContext::new(&mut state); |
| 100 | + let client_manager = ibc::client::Manager::new(); |
| 101 | + let client_state = |
| 102 | + client_manager.query(&mut context, &client_id).map_err(|_| errors::ibc_client_not_exist())?; |
| 103 | + |
| 104 | + let root = |
| 105 | + client_state.get_root(&mut context, other_block_number).map_err(|_| errors::ibc_client_root_not_exist())?; |
| 106 | + let rlp_encoded_root = root.encode(); |
| 107 | + |
| 108 | + Ok(Some(IBCQueryResult { |
| 109 | + block_number, |
| 110 | + raw: rlp_encoded_root.to_hex(), |
| 111 | + // FIXME |
| 112 | + proof: "".to_string(), |
| 113 | + })) |
| 114 | + } |
| 115 | +} |
0 commit comments