Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example tests to deserialzie asset unlock #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ impl Decodable for AssetUnlockPayload {

#[cfg(test)]
mod tests {
use hashes::Hash;
use core::str::FromStr;
use hex::FromHex;
use hashes::{Hash};
use crate::bls_sig_utils::BLSSignature;
use crate::consensus::Encodable;
use crate::consensus;
use crate::consensus::{ Encodable};
use crate::hash_types::QuorumHash;
use crate::transaction::special_transaction::asset_unlock::qualified_asset_unlock::AssetUnlockPayload;
use crate::transaction::special_transaction::asset_unlock::request_info::AssetUnlockRequestInfo;
Expand All @@ -125,4 +128,40 @@ mod tests {
assert_eq!(payload.size(), want);
assert_eq!(actual, want);
}

#[test]
fn deserialize() {
let payload_bytes = Vec::from_hex("012d0100000000000070110100250500004acfa5c6d92071d206da5b767039d42f24e7ab1a694a5b8014cddc088311e448aee468c03feec7caada0599457136ef0dfe9365657a42ef81bb4aa53af383d05d90552b2cd23480cae24036b953ba8480d2f98291271a338e4235265dea94feacb54d1fd96083151001eff4156e7475e998154a8e6082575e2ee461b394d24f7")
.unwrap();

let payload: AssetUnlockPayload = consensus::encode::deserialize(&payload_bytes).unwrap();
assert_eq!(payload.base.version, 1);
assert_eq!(payload.base.index, 301);
assert_eq!(payload.base.fee, 70000);
assert_eq!(payload.request_info.request_height, 1317);
assert_eq!(payload.request_info.quorum_hash, QuorumHash::from_str("4acfa5c6d92071d206da5b767039d42f24e7ab1a694a5b8014cddc088311e448").unwrap());
assert_eq!(payload.quorum_sig, BLSSignature::from_str("aee468c03feec7caada0599457136ef0dfe9365657a42ef81bb4aa53af383d05d90552b2cd23480cae24036b953ba8480d2f98291271a338e4235265dea94feacb54d1fd96083151001eff4156e7475e998154a8e6082575e2ee461b394d24f7").unwrap());
}

#[test]
fn serialize() {
let payload = AssetUnlockPayload {
base: AssetUnlockBasePayload {
version: 1,
index: 301,
fee: 70000,
},
request_info: AssetUnlockRequestInfo {
request_height: 1317,
quorum_hash: QuorumHash::from_str("4acfa5c6d92071d206da5b767039d42f24e7ab1a694a5b8014cddc088311e448").unwrap(),
},
quorum_sig: BLSSignature::from_str("aee468c03feec7caada0599457136ef0dfe9365657a42ef81bb4aa53af383d05d90552b2cd23480cae24036b953ba8480d2f98291271a338e4235265dea94feacb54d1fd96083151001eff4156e7475e998154a8e6082575e2ee461b394d24f7").unwrap()
};

let serialized_bytes = consensus::serialize(&payload);

let expected_payload_bytes = Vec::from_hex("012d0100000000000070110100250500004acfa5c6d92071d206da5b767039d42f24e7ab1a694a5b8014cddc088311e448aee468c03feec7caada0599457136ef0dfe9365657a42ef81bb4aa53af383d05d90552b2cd23480cae24036b953ba8480d2f98291271a338e4235265dea94feacb54d1fd96083151001eff4156e7475e998154a8e6082575e2ee461b394d24f7")
.unwrap();
assert_eq!(serialized_bytes, expected_payload_bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,13 @@ impl Decodable for AssetUnlockBaseTransactionInfo {

#[cfg(test)]
mod tests {
use core::str::FromStr;
use hashes::Hash;
use hashes::hex::FromHex;
use crate::consensus::Encodable;
use crate::transaction::special_transaction::asset_unlock::unqualified_asset_unlock::{AssetUnlockBasePayload, AssetUnlockBaseTransactionInfo};
use crate::{ScriptBuf, TxOut};
use crate::{consensus, PublicKey, QuorumHash, ScriptBuf, TxOut};
use crate::transaction::special_transaction::asset_unlock::request_info::AssetUnlockRequestInfo;

#[test]
fn size() {
Expand All @@ -190,4 +193,83 @@ mod tests {
let actual = payload.consensus_encode(&mut Vec::new()).unwrap();
assert_eq!(actual, want);
}

#[test]
fn test() {
// let private_key
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
let pubkey = PublicKey::from_str(pubkey_string).expect("pubkey");


let output_script = ScriptBuf::new_p2pkh(&pubkey.pubkey_hash());

let tx_out = TxOut {
value: 1000,
script_pubkey: output_script,
};

let withdrawal_transaction = AssetUnlockBaseTransactionInfo {
version: 1,
lock_time: 0,
output: vec![tx_out],
base_payload: AssetUnlockBasePayload {
version: 1,
index: 0,
fee: 1300
},
};

let mut asset_unlock_info_bytes_serialized: Vec<u8> = vec![];
withdrawal_transaction.consensus_encode(&mut asset_unlock_info_bytes_serialized).unwrap();

let request_info = AssetUnlockRequestInfo {
request_height: 0,
quorum_hash: QuorumHash::all_zeros(),
};

let mut request_info_bytes_serialized: Vec<u8> = vec![];
request_info.consensus_encode(&mut request_info_bytes_serialized).unwrap();

let mut asset_unlock_tx_with_request_info_bytes = vec![];
request_info.consensus_append_to_base_encode(
asset_unlock_info_bytes_serialized.clone(),
&mut asset_unlock_tx_with_request_info_bytes,
).unwrap();

let len = asset_unlock_tx_with_request_info_bytes.len();
let (asset_unlock_info_bytes, request_info_bytes) = asset_unlock_tx_with_request_info_bytes.split_at_mut(len - 36);
// let (request_info_bytes, asset_unlock_info_bytes) = asset_unlock_tx_with_request_info_bytes.split_at_mut(36);

let deserializaed_withdrawal_transaction: AssetUnlockBaseTransactionInfo = consensus::encode::deserialize(&asset_unlock_info_bytes).unwrap();
let request_info: AssetUnlockRequestInfo = consensus::encode::deserialize(&request_info_bytes).unwrap();

println!("deserializaed_withdrawal_transaction: {:?}", deserializaed_withdrawal_transaction);
println!("deserializaed_request_info: {:?}", request_info);

let mut tx_bytes = Vec::from_hex("010009000001c8000000000000001976a914c35b782432294088e354bc28aa56d95736cb630288ac0000000001000000000000000070f915129f05000053c006055af6d0ae9aa9627df8615a71c312421a28c4712c8add83c8e1bfdadd").unwrap();
let len = tx_bytes.len();
let (asset_unlock_info_bytes, request_info_bytes) = tx_bytes.split_at_mut(len - 36);
let deserializaed_withdrawal_transaction: AssetUnlockBaseTransactionInfo = consensus::encode::deserialize(&asset_unlock_info_bytes).unwrap();
let request_info: AssetUnlockRequestInfo = consensus::encode::deserialize(&request_info_bytes).unwrap();
println!("deserializaed_withdrawal_transaction 2: {:?}", deserializaed_withdrawal_transaction);
println!("deserializaed_request_info: {:?}", request_info);
// let request_info: AssetUnlockRequestInfo = consensus::encode::deserialize(&request_info_bytes).unwrap();
// println!("{:?}", request_info);

// [1, 0, 9, 0, 0, 1, 200, 0, 0, 0, 0, 0, 0, 0, 25, 118, 169, 20, 37, 71, 51, 84, 63, 142, 114, 112, 122, 75, 72, 144, 114, 138, 210, 149, 75, 161, 203, 249, 136, 172, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 70, 161, 4, 0]
// [1, 0, 9, 0, 0, 1, 232, 3, 0, 0, 0, 0, 0, 0, 25, 118, 169, 20, 187, 27, 168, 113, 128, 20, 177, 194, 132, 210, 184, 221, 117, 143, 242, 61, 38, 72, 205, 91, 136, 172, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 20, 5, 0, 0]
// [1, 0, 9, 0, 0, 1, 232, 3, 0, 0, 0, 0, 0, 0, 7, 106, 5, 1, 2, 3, 4, 5, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 20, 5, 0, 0]
// let deserializaed_withdrawal_transaction: AssetUnlockBaseTransactionInfo = consensus::encode::deserialize(&bytes).unwrap();

// println!("bytes: {:?}", bytes);
}

#[test]
fn test_deserialize_info() {
let bytes = Vec::from_hex("010009000001c8000000000000001976a914c35b782432294088e354bc28aa56d95736cb630288ac0000000001000000000000000070f915129f05000053c006055af6d0ae9aa9627df8615a71c312421a28c4712c8add83c8e1bfdadd")
.unwrap();

let decoded_info: AssetUnlockRequestInfo = consensus::encode::deserialize(&bytes).unwrap();
println!("decoded_info: {:?}", decoded_info);
}
}
Loading