Skip to content

Commit

Permalink
[WIP]: support timeout_height from proto (#3684)
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius authored Feb 9, 2024
1 parent c08a407 commit 7523c98
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
22 changes: 22 additions & 0 deletions rust/tw_cosmos_sdk/src/modules/serializer/json_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub struct SignedTxJson {
pub memo: String,
pub msg: Vec<AnyMsg<Json>>,
pub signatures: Vec<SignatureJson>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub timeout_height: Option<String>,
}

#[derive(Serialize)]
Expand All @@ -28,6 +31,9 @@ pub struct UnsignedTxJson {
pub memo: String,
pub msgs: Vec<AnyMsg<Json>>,
pub sequence: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub timeout_height: Option<String>,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -69,11 +75,19 @@ where
let signature =
Self::serialize_signature(&signed.signer.public_key, signed.signature.clone());

let convert = |value: u64| {
if value == 0 {
None
} else {
Some(value.to_string())
}
};
Ok(SignedTxJson {
fee: Self::build_fee(&signed.fee),
memo: signed.tx_body.memo.clone(),
msg,
signatures: vec![signature],
timeout_height: convert(signed.tx_body.timeout_height),
})
}

Expand All @@ -87,13 +101,21 @@ where
.map(|msg| msg.to_json())
.collect::<SigningResult<_>>()?;

let convert = |value: u64| {
if value == 0 {
None
} else {
Some(value.to_string())
}
};
Ok(UnsignedTxJson {
account_number: unsigned.account_number.to_string(),
chain_id: unsigned.chain_id.clone(),
fee: Self::build_fee(&unsigned.fee),
memo: unsigned.tx_body.memo.clone(),
msgs,
sequence: unsigned.signer.sequence.to_string(),
timeout_height: convert(unsigned.tx_body.timeout_height),
})
}

Expand Down
4 changes: 1 addition & 3 deletions rust/tw_cosmos_sdk/src/modules/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use tw_number::U256;
use tw_proto::Cosmos::Proto;
use tw_proto::{google, serialize};

const DEFAULT_TIMEOUT_HEIGHT: u64 = 0;

pub struct TxBuilder<Context> {
_phantom: PhantomData<Context>,
}
Expand Down Expand Up @@ -126,7 +124,7 @@ where
Ok(TxBody {
messages,
memo: input.memo.to_string(),
timeout_height: DEFAULT_TIMEOUT_HEIGHT,
timeout_height: input.timeout_height,
})
}

Expand Down
64 changes: 64 additions & 0 deletions rust/tw_cosmos_sdk/tests/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,70 @@ fn test_sign_raw_json() {
});
}

#[test]
fn test_sign_raw_json_with_timeout() {
let coin = TestCoinContext::default()
.with_public_key_type(PublicKeyType::Secp256k1)
.with_hrp("cosmos");

let raw_json_msg = Proto::mod_Message::RawJSON {
type_pb: "osmosis/poolmanager/split-amount-in".into(),
value: r#"{
"routes": [
{
"pools": [
{
"pool_id": "463",
"token_out_denom": "ibc/1DC495FCEFDA068A3820F903EDBD78B942FBD204D7E93D3BA2B432E9669D1A59"
},
{
"pool_id": "916",
"token_out_denom": "ibc/573FCD90FACEE750F55A8864EF7D38265F07E5A9273FA0E8DAFD39951332B580"
}
],
"token_in_amount": "70000"
},
{
"pools": [
{
"pool_id": "907",
"token_out_denom": "ibc/573FCD90FACEE750F55A8864EF7D38265F07E5A9273FA0E8DAFD39951332B580"
}
],
"token_in_amount": "30000"
}
],
"sender": "osmo1qr7dhmvcqm4fnleaqel3gel4u20nk5rp9rwsae",
"token_in_denom": "uosmo",
"token_out_min_amount": "885297"
}"#.into(),
};
let input = Proto::SigningInput {
account_number: 24139,
chain_id: "osmosis-1".into(),
sequence: 191,
fee: Some(make_fee(617438, make_amount("uosmo", "1853"))),
private_key: account_1037_private_key(),
messages: vec![make_message(MessageEnum::raw_json_message(raw_json_msg))],
timeout_height: 13692007,
..Proto::SigningInput::default()
};

// `RawJSON` doesn't support Protobuf serialization and signing.
test_sign_protobuf_error::<StandardCosmosContext>(TestErrorInput {
coin: &coin,
input: input.clone(),
error: SigningError::Error_not_supported,
});
test_sign_json::<StandardCosmosContext>(TestInput {
coin: &coin,
input,
tx: r#"{"mode":"block","tx":{"fee":{"amount":[{"amount":"1853","denom":"uosmo"}],"gas":"617438"},"memo":"","msg":[{"type":"osmosis/poolmanager/split-amount-in","value":{"routes":[{"pools":[{"pool_id":"463","token_out_denom":"ibc/1DC495FCEFDA068A3820F903EDBD78B942FBD204D7E93D3BA2B432E9669D1A59"},{"pool_id":"916","token_out_denom":"ibc/573FCD90FACEE750F55A8864EF7D38265F07E5A9273FA0E8DAFD39951332B580"}],"token_in_amount":"70000"},{"pools":[{"pool_id":"907","token_out_denom":"ibc/573FCD90FACEE750F55A8864EF7D38265F07E5A9273FA0E8DAFD39951332B580"}],"token_in_amount":"30000"}],"sender":"osmo1qr7dhmvcqm4fnleaqel3gel4u20nk5rp9rwsae","token_in_denom":"uosmo","token_out_min_amount":"885297"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"7gMxXwqzZDe5+h1i16q7A7CgGUtLl2+Q8/YaUZCeYvp8kISbBwD2SlNTpJtz1RLskzF2uNcDebo61HbcVn9dAw=="}],"timeout_height":"13692007"}}"#,
signature: "ee03315f0ab36437b9fa1d62d7aabb03b0a0194b4b976f90f3f61a51909e62fa7c90849b0700f64a5353a49b73d512ec933176b8d70379ba3ad476dc567f5d03",
signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"7gMxXwqzZDe5+h1i16q7A7CgGUtLl2+Q8/YaUZCeYvp8kISbBwD2SlNTpJtz1RLskzF2uNcDebo61HbcVn9dAw=="}]"#,
});
}

#[test]
fn test_sign_ibc_transfer() {
let coin = TestCoinContext::default()
Expand Down
3 changes: 3 additions & 0 deletions src/proto/Cosmos.proto
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ message SigningInput {

// Optional. If set, use a different Signer info when signing the transaction.
SignerInfo signer_info = 12;

// Optional timeout_height
uint64 timeout_height = 13;
}

// Result containing the signed and encoded transaction.
Expand Down

0 comments on commit 7523c98

Please sign in to comment.