From 44659baab23ffef9017b5cc7197ca352730d23c4 Mon Sep 17 00:00:00 2001 From: zhounan <2627159529@qq.com> Date: Thu, 17 Oct 2019 11:41:43 +0800 Subject: [PATCH] flatten RawTransaction fixed_codec and impl action::call result::call --- protocol/src/fixed_codec/macro.rs | 1 - protocol/src/fixed_codec/receipt.rs | 19 +++- protocol/src/fixed_codec/tests/fixed_codec.rs | 8 +- protocol/src/fixed_codec/transaction.rs | 95 +++++++++---------- 4 files changed, 62 insertions(+), 61 deletions(-) diff --git a/protocol/src/fixed_codec/macro.rs b/protocol/src/fixed_codec/macro.rs index e6f13e64b..935e7e0e0 100644 --- a/protocol/src/fixed_codec/macro.rs +++ b/protocol/src/fixed_codec/macro.rs @@ -16,4 +16,3 @@ macro_rules! impl_default_fixed_codec_for { )+ ) } - diff --git a/protocol/src/fixed_codec/receipt.rs b/protocol/src/fixed_codec/receipt.rs index b53448fa0..019c070c4 100644 --- a/protocol/src/fixed_codec/receipt.rs +++ b/protocol/src/fixed_codec/receipt.rs @@ -83,8 +83,11 @@ impl rlp::Encodable for ReceiptResult { return_value, logs_bloom, } => { - // TODO(@yejiayu): The interface for `call` is about to be modified. - unimplemented!() + s.begin_list(4) + .append(&CALL_RESULT_FLAG) + .append(contract) + .append(logs_bloom.as_ref()) + .append(&return_value.to_vec()); } ReceiptResult::Fail { system, user } => { s.begin_list(3) @@ -132,8 +135,16 @@ impl rlp::Decodable for ReceiptResult { }) } CALL_RESULT_FLAG => { - // TODO(@yejiayu): The interface for `call` is about to be modified. - unimplemented!() + let contract = rlp::decode(r.at(1)?.as_raw())?; + let bloom = rlp::decode(r.at(2)?.as_raw())?; + let logs_bloom = Box::new(bloom); + let return_value = Bytes::from(r.at(3)?.data()?); + + Ok(ReceiptResult::Call { + contract, + return_value, + logs_bloom, + }) } FAIL_RESULT_FLAG => { let system = String::from_utf8(r.at(1)?.data()?.to_vec()) diff --git a/protocol/src/fixed_codec/tests/fixed_codec.rs b/protocol/src/fixed_codec/tests/fixed_codec.rs index e200fb50e..5d58e6afd 100644 --- a/protocol/src/fixed_codec/tests/fixed_codec.rs +++ b/protocol/src/fixed_codec/tests/fixed_codec.rs @@ -36,12 +36,7 @@ fn test_fixed_codec() { mock_sign_tx, AType::Transfer ); - test_eq!( - transaction, - SignedTransaction, - mock_sign_tx, - AType::Deploy - ); + test_eq!(transaction, SignedTransaction, mock_sign_tx, AType::Deploy); test_eq!(transaction, SignedTransaction, mock_sign_tx, AType::Call); test_eq!(epoch, Proof, mock_proof); @@ -53,6 +48,7 @@ fn test_fixed_codec() { test_eq!(receipt, Receipt, mock_receipt, ReceiptType::Transfer); test_eq!(receipt, Receipt, mock_receipt, ReceiptType::Deploy); + test_eq!(receipt, Receipt, mock_receipt, ReceiptType::Call); test_eq!(receipt, Receipt, mock_receipt, ReceiptType::Fail); test_eq!(genesis, Genesis, mock_genesis); diff --git a/protocol/src/fixed_codec/transaction.rs b/protocol/src/fixed_codec/transaction.rs index 2b3859cd7..27dd04f20 100644 --- a/protocol/src/fixed_codec/transaction.rs +++ b/protocol/src/fixed_codec/transaction.rs @@ -2,7 +2,7 @@ use bytes::Bytes; use crate::fixed_codec::{FixedCodecError, ProtocolFixedCodec}; use crate::types::{ - primitive::{Balance, ContractType, Fee, Hash, UserAddress, ContractAddress}, + primitive::{Balance, ContractAddress, ContractType, Fee, Hash, UserAddress}, transaction::{CarryingAsset, RawTransaction, SignedTransaction, TransactionAction}, }; use crate::{impl_default_fixed_codec_for, ProtocolResult}; @@ -20,7 +20,7 @@ impl rlp::Encodable for RawTransaction { match &self.action { TransactionAction::Transfer { receiver, - carrying_asset + carrying_asset, } => { s.begin_list(9); s.append(&TRANSFER_ACTION_FLAG); @@ -39,7 +39,7 @@ impl rlp::Encodable for RawTransaction { } TransactionAction::Deploy { code, - contract_type + contract_type, } => { s.begin_list(8); s.append(&DEPLOY_ACTION_FLAG); @@ -66,7 +66,7 @@ impl rlp::Encodable for RawTransaction { contract, method, args, - carrying_asset + carrying_asset, } => { match &carrying_asset { Some(_) => { @@ -87,7 +87,8 @@ impl rlp::Encodable for RawTransaction { s.append(&self.timeout); // Append tx action fields - let args = args.iter() + let args = args + .iter() .map(|arg| hex::encode(arg.to_vec())) .collect::>(); @@ -117,13 +118,13 @@ impl rlp::Decodable for RawTransaction { // Decode tx action fields let action = TransactionAction::Transfer { - receiver: UserAddress::from_bytes(Bytes::from(r.at(8)?.data()?)) + receiver: UserAddress::from_bytes(Bytes::from(r.at(8)?.data()?)) .map_err(|_| rlp::DecoderError::RlpInvalidLength)?, carrying_asset: CarryingAsset { asset_id: Hash::from_bytes(Bytes::from(r.at(7)?.data()?)) .map_err(|_| rlp::DecoderError::RlpInvalidLength)?, - amount: Balance::from_bytes_be(r.at(6)?.data()?) - } + amount: Balance::from_bytes_be(r.at(6)?.data()?), + }, }; Ok(RawTransaction { @@ -131,7 +132,7 @@ impl rlp::Decodable for RawTransaction { nonce, timeout, fee, - action + action, }) } DEPLOY_ACTION_FLAG => { @@ -152,7 +153,7 @@ impl rlp::Decodable for RawTransaction { let action = TransactionAction::Deploy { code, - contract_type + contract_type, }; Ok(RawTransaction { @@ -160,7 +161,7 @@ impl rlp::Decodable for RawTransaction { nonce, timeout, fee, - action + action, }) } CALL_ACTION_WITH_ASSET_FLAG | CALL_ACTION_WITHOUT_ASSET_FLAG => { @@ -169,16 +170,14 @@ impl rlp::Decodable for RawTransaction { // Decode tx action fields let args: Vec = rlp::decode_list(r.at(6)?.as_raw()); - let args: Result, _> = args.iter() - .map(|arg|hex_to_bytes(&arg)) - .collect(); + let args: Result, _> = args.iter().map(|arg| hex_to_bytes(&arg)).collect(); let args = args?; if let CALL_ACTION_WITH_ASSET_FLAG = flag { let carrying_asset = CarryingAsset { asset_id: Hash::from_bytes(Bytes::from(r.at(8)?.data()?)) .map_err(|_| rlp::DecoderError::RlpInvalidLength)?, - amount: Balance::from_bytes_be(r.at(7)?.data()?) + amount: Balance::from_bytes_be(r.at(7)?.data()?), }; let contract = ContractAddress::from_bytes(Bytes::from(r.at(9)?.data()?)) @@ -187,10 +186,10 @@ impl rlp::Decodable for RawTransaction { .map_err(|_| rlp::DecoderError::RlpInvalidLength)?; let action = TransactionAction::Call { - contract: contract, - method: method, - args: args, - carrying_asset: Some(carrying_asset) + contract, + method, + args, + carrying_asset: Some(carrying_asset), }; Ok(RawTransaction { @@ -198,7 +197,7 @@ impl rlp::Decodable for RawTransaction { nonce, timeout, fee, - action + action, }) } else { let contract = ContractAddress::from_bytes(Bytes::from(r.at(7)?.data()?)) @@ -207,10 +206,10 @@ impl rlp::Decodable for RawTransaction { .map_err(|_| rlp::DecoderError::RlpInvalidLength)?; let action = TransactionAction::Call { - contract: contract, - method: method, - args: args, - carrying_asset: None + contract, + method, + args, + carrying_asset: None, }; Ok(RawTransaction { @@ -218,7 +217,7 @@ impl rlp::Decodable for RawTransaction { nonce, timeout, fee, - action + action, }) } } @@ -227,26 +226,6 @@ impl rlp::Decodable for RawTransaction { } } - - -fn help_decode_raw_tx(r: &rlp::Rlp) -> Result<(Hash, Fee, Hash, u64), rlp::DecoderError> { - let chain_id = Hash::from_bytes(Bytes::from(r.at(1)?.data()?)) - .map_err(|_| rlp::DecoderError::RlpInvalidLength)?; - - let fee = Fee { - asset_id: Hash::from_bytes(Bytes::from(r.at(2)?.data()?)) - .map_err(|_| rlp::DecoderError::RlpInvalidLength)?, - cycle: r.at(3)?.as_val()? - }; - - let nonce = Hash::from_bytes(Bytes::from(r.at(4)?.data()?)) - .map_err(|_| rlp::DecoderError::RlpInvalidLength)?; - - let timeout = r.at(5)?.as_val()?; - - Ok((chain_id, fee, nonce, timeout)) -} - impl rlp::Encodable for SignedTransaction { fn rlp_append(&self, s: &mut rlp::RlpStream) { s.begin_list(4) @@ -277,6 +256,24 @@ impl rlp::Decodable for SignedTransaction { } } +fn help_decode_raw_tx(r: &rlp::Rlp) -> Result<(Hash, Fee, Hash, u64), rlp::DecoderError> { + let chain_id = Hash::from_bytes(Bytes::from(r.at(1)?.data()?)) + .map_err(|_| rlp::DecoderError::RlpInvalidLength)?; + + let fee = Fee { + asset_id: Hash::from_bytes(Bytes::from(r.at(2)?.data()?)) + .map_err(|_| rlp::DecoderError::RlpInvalidLength)?, + cycle: r.at(3)?.as_val()?, + }; + + let nonce = Hash::from_bytes(Bytes::from(r.at(4)?.data()?)) + .map_err(|_| rlp::DecoderError::RlpInvalidLength)?; + + let timeout = r.at(5)?.as_val()?; + + Ok((chain_id, fee, nonce, timeout)) +} + fn clean_0x(s: &str) -> &str { if s.starts_with("0x") { &s[2..] @@ -288,9 +285,7 @@ fn clean_0x(s: &str) -> &str { fn hex_to_bytes(s: &str) -> Result { let s = clean_0x(s); let bytes = hex::decode(s) - .map_err(|_|rlp::DecoderError::Custom("hex to bytes err when decode raw tx"))?; + .map_err(|_| rlp::DecoderError::Custom("hex to bytes err when decode raw tx"))?; - Ok( - Bytes::from(bytes) - ) -} \ No newline at end of file + Ok(Bytes::from(bytes)) +}