1use cita_cloud_proto::blockchain::raw_transaction::Tx;
16use cita_cloud_proto::blockchain::{
17 Block, CompactBlock, CompactBlockBody, RawTransaction, RawTransactions,
18};
19use cita_cloud_proto::common::Address;
20use cita_cloud_proto::status_code::StatusCodeEnum;
21use std::fs;
22use std::path::Path;
23use toml::macros::Deserialize;
24use toml::Value;
25
26pub const ADDR_BYTES_LEN: usize = 20;
27pub const HASH_BYTES_LEN: usize = 32;
28
29pub fn h160_address_check(address: Option<&Address>) -> Result<(), StatusCodeEnum> {
30 match address {
31 Some(addr) => {
32 if addr.address.len() == ADDR_BYTES_LEN {
33 Ok(())
34 } else {
35 Err(StatusCodeEnum::ProvideAddressError)
36 }
37 }
38 None => Err(StatusCodeEnum::NoProvideAddress),
39 }
40}
41
42pub fn get_tx_hash(raw_tx: &RawTransaction) -> Result<&[u8], StatusCodeEnum> {
43 match raw_tx.tx {
44 Some(Tx::NormalTx(ref normal_tx)) => Ok(&normal_tx.transaction_hash),
45 Some(Tx::UtxoTx(ref utxo_tx)) => Ok(&utxo_tx.transaction_hash),
46 None => Err(StatusCodeEnum::NoTransaction),
47 }
48}
49
50pub fn get_tx_hash_list(raw_txs: &RawTransactions) -> Result<Vec<Vec<u8>>, StatusCodeEnum> {
51 let mut hashes = Vec::new();
52 for raw_tx in &raw_txs.body {
53 hashes.push(get_tx_hash(raw_tx)?.to_vec())
54 }
55 Ok(hashes)
56}
57
58pub fn extract_compact(block: Block) -> CompactBlock {
59 let mut compact_body = CompactBlockBody { tx_hashes: vec![] };
60
61 if let Some(body) = block.body {
62 for raw_tx in body.body {
63 match raw_tx.tx {
64 Some(Tx::NormalTx(normal_tx)) => {
65 compact_body.tx_hashes.push(normal_tx.transaction_hash)
66 }
67 Some(Tx::UtxoTx(utxo_tx)) => compact_body.tx_hashes.push(utxo_tx.transaction_hash),
68 None => {}
69 }
70 }
71 }
72
73 CompactBlock {
74 version: block.version,
75 header: block.header,
76 body: Some(compact_body),
77 }
78}
79
80pub fn read_toml<'a, T: Deserialize<'a>>(path: impl AsRef<Path>, name: &'a str) -> T {
81 let s = fs::read_to_string(path)
82 .map_err(|e| println!("read_to_string err: {e}"))
83 .unwrap();
84 let config: Value = s
85 .parse()
86 .map_err(|e| println!("toml parse err: {e}"))
87 .unwrap();
88 T::deserialize(config[name].clone())
89 .map_err(|e| println!("config deserialize err: {e}"))
90 .unwrap()
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96 use serde_derive::{Deserialize, Serialize};
97
98 #[derive(Debug, Deserialize, Serialize)]
99 struct ExecutorConfig {
100 port: u16,
101 }
102
103 #[test]
104 fn it_works() {
105 let config: ExecutorConfig = read_toml("src/example/sample.toml", "executor");
106 assert_eq!(config.port, 50002);
107 }
108}