Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
flatten RawTransaction fixed_codec and impl action::call result::call
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanxyg committed Oct 23, 2019
1 parent 39fe0c9 commit 44659ba
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 61 deletions.
1 change: 0 additions & 1 deletion protocol/src/fixed_codec/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ macro_rules! impl_default_fixed_codec_for {
)+
)
}

19 changes: 15 additions & 4 deletions protocol/src/fixed_codec/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
8 changes: 2 additions & 6 deletions protocol/src/fixed_codec/tests/fixed_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
95 changes: 45 additions & 50 deletions protocol/src/fixed_codec/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -66,7 +66,7 @@ impl rlp::Encodable for RawTransaction {
contract,
method,
args,
carrying_asset
carrying_asset,
} => {
match &carrying_asset {
Some(_) => {
Expand All @@ -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::<Vec<_>>();

Expand Down Expand Up @@ -117,21 +118,21 @@ 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 {
chain_id,
nonce,
timeout,
fee,
action
action,
})
}
DEPLOY_ACTION_FLAG => {
Expand All @@ -152,15 +153,15 @@ impl rlp::Decodable for RawTransaction {

let action = TransactionAction::Deploy {
code,
contract_type
contract_type,
};

Ok(RawTransaction {
chain_id,
nonce,
timeout,
fee,
action
action,
})
}
CALL_ACTION_WITH_ASSET_FLAG | CALL_ACTION_WITHOUT_ASSET_FLAG => {
Expand All @@ -169,16 +170,14 @@ impl rlp::Decodable for RawTransaction {

// Decode tx action fields
let args: Vec<String> = rlp::decode_list(r.at(6)?.as_raw());
let args: Result<Vec<_>, _> = args.iter()
.map(|arg|hex_to_bytes(&arg))
.collect();
let args: Result<Vec<_>, _> = 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()?))
Expand All @@ -187,18 +186,18 @@ 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 {
chain_id,
nonce,
timeout,
fee,
action
action,
})
} else {
let contract = ContractAddress::from_bytes(Bytes::from(r.at(7)?.data()?))
Expand All @@ -207,18 +206,18 @@ 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 {
chain_id,
nonce,
timeout,
fee,
action
action,
})
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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..]
Expand All @@ -288,9 +285,7 @@ fn clean_0x(s: &str) -> &str {
fn hex_to_bytes(s: &str) -> Result<Bytes, rlp::DecoderError> {
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)
)
}
Ok(Bytes::from(bytes))
}

0 comments on commit 44659ba

Please sign in to comment.