Skip to content

Commit cfb707e

Browse files
Veetaharnbguy
andauthored
refactor(ibc-testkit): use bon instead of typed-builder (#1326)
* refactor(ibc-testkit): use `bon` instead of `typed-builder` * Bump `bon` requirement * Bump `bon` requirement * Fix some places that I missed to update * Update `LightClientBuilder` to `dummy_light_client()` * Remove dummy line comments from function args * default tags at function args * revert builder pattern * nits * pass required args in start_fn * fix docs build * use default call instead of build --------- Co-authored-by: Ranadeep Biswas <[email protected]> Co-authored-by: Rano | Ranadeep <[email protected]>
1 parent ede380f commit cfb707e

File tree

18 files changed

+272
-397
lines changed

18 files changed

+272
-397
lines changed

ibc-testkit/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ description = """
1717

1818
[dependencies]
1919
# external dependencies
20+
bon = { version = "2.3.0" }
2021
borsh = { workspace = true, optional = true }
2122
derive_more = { workspace = true }
2223
displaydoc = { workspace = true }
@@ -25,7 +26,6 @@ schemars = { workspace = true, optional = true }
2526
serde = { workspace = true, optional = true }
2627
serde-json = { workspace = true, optional = true }
2728
subtle-encoding = { workspace = true }
28-
typed-builder = { version = "0.19.1" }
2929

3030
# ibc dependencies
3131
ibc = { workspace = true, features = [ "std" ] }

ibc-testkit/src/context.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use ibc::primitives::prelude::*;
2323
use ibc::primitives::Timestamp;
2424

2525
use super::testapp::ibc::core::types::{LightClientState, MockIbcStore};
26-
use crate::fixtures::core::context::TestContextConfig;
26+
use crate::fixtures::core::context::dummy_store_generic_test_context;
2727
use crate::hosts::{HostClientState, MockHost, TendermintHost, TestBlock, TestHeader, TestHost};
2828
use crate::testapp::ibc::clients::{AnyClientState, AnyConsensusState};
2929
use crate::testapp::ibc::core::router::MockRouter;
@@ -70,7 +70,7 @@ where
7070
HostClientState<H>: ClientStateValidation<MockIbcStore<S>>,
7171
{
7272
fn default() -> Self {
73-
TestContextConfig::builder().build()
73+
dummy_store_generic_test_context().call()
7474
}
7575
}
7676

@@ -529,35 +529,35 @@ mod tests {
529529
let tests: Vec<Test<H>> = vec![
530530
Test {
531531
name: "Empty history, small pruning window".to_string(),
532-
ctx: TestContextConfig::builder()
532+
ctx: dummy_store_generic_test_context()
533533
.latest_height(Height::new(cv, 1).expect("Never fails"))
534-
.build(),
534+
.call(),
535535
},
536536
Test {
537537
name: "Large pruning window".to_string(),
538-
ctx: TestContextConfig::builder()
538+
ctx: dummy_store_generic_test_context()
539539
.latest_height(Height::new(cv, 2).expect("Never fails"))
540-
.build(),
540+
.call(),
541541
},
542542
Test {
543543
name: "Small pruning window".to_string(),
544-
ctx: TestContextConfig::builder()
544+
ctx: dummy_store_generic_test_context()
545545
.latest_height(Height::new(cv, 30).expect("Never fails"))
546-
.build(),
546+
.call(),
547547
},
548548
Test {
549549
name: "Small pruning window, small starting height".to_string(),
550-
ctx: TestContextConfig::builder()
550+
ctx: dummy_store_generic_test_context()
551551
.latest_height(Height::new(cv, 2).expect("Never fails"))
552-
.build(),
552+
.call(),
553553
},
554554
// This is disabled, as now we generate all the blocks till latest_height
555555
// Generating 2000 Tendermint blocks is slow.
556556
// Test {
557557
// name: "Large pruning window, large starting height".to_string(),
558-
// ctx: TestContextConfig::builder()
558+
// ctx: dummy_store_generic_test_context()
559559
// .latest_height(Height::new(cv, 2000).expect("Never fails"))
560-
// .build(),
560+
// .call(),
561561
// },
562562
];
563563

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
1+
use bon::builder;
12
use ibc::apps::transfer::types::msgs::transfer::MsgTransfer;
23
use ibc::apps::transfer::types::packet::PacketData;
34
use ibc::apps::transfer::types::{Memo, PrefixedCoin};
45
use ibc::core::channel::types::packet::Packet;
56
use ibc::core::channel::types::timeout::{TimeoutHeight, TimeoutTimestamp};
67
use ibc::core::host::types::identifiers::{ChannelId, PortId, Sequence};
78
use ibc::core::primitives::Signer;
8-
use typed_builder::TypedBuilder;
99

1010
use crate::fixtures::core::signer::dummy_account_id;
1111

12-
/// Configuration of the `MsgTransfer` message for building dummy messages.
13-
#[derive(TypedBuilder, Debug)]
14-
#[builder(build_method(into = MsgTransfer))]
15-
pub struct MsgTransferConfig {
16-
#[builder(default = PortId::transfer())]
17-
pub port_id_on_a: PortId,
18-
#[builder(default = ChannelId::zero())]
19-
pub chan_id_on_a: ChannelId,
20-
pub packet_data: PacketData,
21-
#[builder(default = TimeoutHeight::Never)]
22-
pub timeout_height_on_b: TimeoutHeight,
23-
#[builder(default = TimeoutTimestamp::Never)]
24-
pub timeout_timestamp_on_b: TimeoutTimestamp,
25-
}
26-
27-
impl From<MsgTransferConfig> for MsgTransfer {
28-
fn from(config: MsgTransferConfig) -> Self {
29-
Self {
30-
port_id_on_a: config.port_id_on_a,
31-
chan_id_on_a: config.chan_id_on_a,
32-
packet_data: config.packet_data,
33-
timeout_height_on_b: config.timeout_height_on_b,
34-
timeout_timestamp_on_b: config.timeout_timestamp_on_b,
35-
}
12+
/// Returns a dummy [`MsgTransfer`], for testing purposes only!
13+
#[builder]
14+
pub fn dummy_msg_transfer(
15+
#[builder(start_fn)] packet_data: PacketData,
16+
#[builder(default = PortId::transfer())] port_id_on_a: PortId,
17+
#[builder(default = ChannelId::zero())] chan_id_on_a: ChannelId,
18+
#[builder(default = TimeoutHeight::Never)] timeout_height_on_b: TimeoutHeight,
19+
#[builder(default = TimeoutTimestamp::Never)] timeout_timestamp_on_b: TimeoutTimestamp,
20+
) -> MsgTransfer {
21+
MsgTransfer {
22+
port_id_on_a,
23+
chan_id_on_a,
24+
packet_data,
25+
timeout_height_on_b,
26+
timeout_timestamp_on_b,
3627
}
3728
}
3829

@@ -52,26 +43,18 @@ pub fn extract_transfer_packet(msg: &MsgTransfer, sequence: Sequence) -> Packet
5243
}
5344
}
5445

55-
/// Configuration of the `PacketData` type for building dummy packets.
56-
#[derive(TypedBuilder, Debug)]
57-
#[builder(build_method(into = PacketData))]
58-
pub struct PacketDataConfig {
59-
pub token: PrefixedCoin,
60-
#[builder(default = dummy_account_id())]
61-
pub sender: Signer,
62-
#[builder(default = dummy_account_id())]
63-
pub receiver: Signer,
64-
#[builder(default = "".into())]
65-
pub memo: Memo,
66-
}
67-
68-
impl From<PacketDataConfig> for PacketData {
69-
fn from(config: PacketDataConfig) -> Self {
70-
Self {
71-
token: config.token,
72-
sender: config.sender,
73-
receiver: config.receiver,
74-
memo: config.memo,
75-
}
46+
/// Returns a dummy [`PacketData`], for testing purposes only!
47+
#[builder]
48+
pub fn dummy_packet_data(
49+
#[builder(start_fn)] token: PrefixedCoin,
50+
#[builder(default = dummy_account_id())] sender: Signer,
51+
#[builder(default = dummy_account_id())] receiver: Signer,
52+
#[builder(default = "".into())] memo: Memo,
53+
) -> PacketData {
54+
PacketData {
55+
token,
56+
sender,
57+
receiver,
58+
memo,
7659
}
7760
}

ibc-testkit/src/fixtures/clients/tendermint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::str::FromStr;
22
use core::time::Duration;
33

44
use basecoin_store::avl::get_proof_spec as basecoin_proof_spec;
5+
use bon::Builder;
56
use ibc::clients::tendermint::client_state::ClientState as TmClientState;
67
use ibc::clients::tendermint::types::error::TendermintClientError;
78
use ibc::clients::tendermint::types::proto::v1::{ClientState as RawTmClientState, Fraction};
@@ -17,7 +18,6 @@ use ibc::core::host::types::error::DecodingError;
1718
use ibc::core::host::types::identifiers::ChainId;
1819
use ibc::core::primitives::prelude::*;
1920
use tendermint::block::Header as TmHeader;
20-
use typed_builder::TypedBuilder;
2121

2222
/// Returns a dummy tendermint `ClientState` by given `frozen_height`, for testing purposes only!
2323
pub fn dummy_tm_client_state_from_raw(
@@ -69,7 +69,7 @@ pub fn dummy_raw_tm_client_state(frozen_height: RawHeight) -> RawTmClientState {
6969
}
7070
}
7171

72-
#[derive(TypedBuilder, Debug)]
72+
#[derive(Debug, Builder)]
7373
pub struct ClientStateConfig {
7474
#[builder(default = TrustThreshold::ONE_THIRD)]
7575
pub trust_level: TrustThreshold,

ibc-testkit/src/fixtures/core/channel/packet.rs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
1+
use bon::builder;
12
use ibc::core::channel::types::packet::Packet;
23
use ibc::core::channel::types::proto::v1::Packet as RawPacket;
34
use ibc::core::channel::types::timeout::{TimeoutHeight, TimeoutTimestamp};
45
use ibc::core::client::types::proto::v1::Height as RawHeight;
56
use ibc::core::host::types::identifiers::{ChannelId, PortId, Sequence};
67
use ibc::core::primitives::prelude::*;
7-
use typed_builder::TypedBuilder;
88

9-
/// Configuration of the `PacketData` type for building dummy packets.
10-
#[derive(TypedBuilder, Debug)]
11-
#[builder(build_method(into = Packet))]
12-
pub struct PacketConfig {
13-
#[builder(default = Sequence::from(0))]
14-
pub seq_on_a: Sequence,
15-
#[builder(default = PortId::transfer())]
16-
pub port_id_on_a: PortId,
17-
#[builder(default = ChannelId::zero())]
18-
pub chan_id_on_a: ChannelId,
19-
#[builder(default = PortId::transfer())]
20-
pub port_id_on_b: PortId,
21-
#[builder(default = ChannelId::zero())]
22-
pub chan_id_on_b: ChannelId,
23-
#[builder(default)]
24-
pub data: Vec<u8>,
25-
#[builder(default = TimeoutHeight::Never)]
26-
pub timeout_height_on_b: TimeoutHeight,
27-
#[builder(default = TimeoutTimestamp::Never)]
28-
pub timeout_timestamp_on_b: TimeoutTimestamp,
29-
}
30-
31-
impl From<PacketConfig> for Packet {
32-
fn from(config: PacketConfig) -> Self {
33-
Self {
34-
seq_on_a: config.seq_on_a,
35-
port_id_on_a: config.port_id_on_a,
36-
chan_id_on_a: config.chan_id_on_a,
37-
port_id_on_b: config.port_id_on_b,
38-
chan_id_on_b: config.chan_id_on_b,
39-
data: config.data,
40-
timeout_height_on_b: config.timeout_height_on_b,
41-
timeout_timestamp_on_b: config.timeout_timestamp_on_b,
42-
}
9+
/// Returns a dummy [`Packet`], for testing purposes only!
10+
#[builder]
11+
pub fn dummy_packet(
12+
#[builder(default = Sequence::from(0))] seq_on_a: Sequence,
13+
#[builder(default = PortId::transfer())] port_id_on_a: PortId,
14+
#[builder(default = ChannelId::zero())] chan_id_on_a: ChannelId,
15+
#[builder(default = PortId::transfer())] port_id_on_b: PortId,
16+
#[builder(default = ChannelId::zero())] chan_id_on_b: ChannelId,
17+
#[builder(default)] data: Vec<u8>,
18+
#[builder(default = TimeoutHeight::Never)] timeout_height_on_b: TimeoutHeight,
19+
#[builder(default = TimeoutTimestamp::Never)] timeout_timestamp_on_b: TimeoutTimestamp,
20+
) -> Packet {
21+
Packet {
22+
seq_on_a,
23+
port_id_on_a,
24+
chan_id_on_a,
25+
port_id_on_b,
26+
chan_id_on_b,
27+
data,
28+
timeout_height_on_b,
29+
timeout_timestamp_on_b,
4330
}
4431
}
4532

ibc-testkit/src/fixtures/core/connection/mod.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,12 @@ use ibc::core::commitment_types::proto::v1::MerklePrefix;
77
use ibc::core::connection::types::proto::v1::Counterparty as RawCounterparty;
88
use ibc::core::host::types::identifiers::ConnectionId;
99
use ibc::core::primitives::prelude::*;
10-
use typed_builder::TypedBuilder;
1110

1211
pub use self::conn_open_ack::*;
1312
pub use self::conn_open_confirm::*;
1413
pub use self::conn_open_init::*;
1514
pub use self::conn_open_try::*;
1615

17-
#[derive(TypedBuilder, Debug)]
18-
#[builder(build_method(into = RawCounterparty))]
19-
pub struct CounterpartyConfig {
20-
#[builder(default = "07-tendermint-0")]
21-
client_id: &'static str,
22-
#[builder(default = "connection-0")]
23-
connection_id: &'static str,
24-
#[builder(default = Some(MerklePrefix {
25-
key_prefix: b"ibc".to_vec()
26-
}))]
27-
prefix: Option<MerklePrefix>,
28-
}
29-
30-
impl From<CounterpartyConfig> for RawCounterparty {
31-
fn from(config: CounterpartyConfig) -> Self {
32-
Self {
33-
client_id: config.client_id.to_string(),
34-
connection_id: config.connection_id.to_string(),
35-
prefix: config.prefix,
36-
}
37-
}
38-
}
39-
4016
pub fn dummy_raw_counterparty_conn(conn_id: Option<u64>) -> RawCounterparty {
4117
let connection_id = match conn_id {
4218
Some(id) => ConnectionId::new(id).to_string(),

0 commit comments

Comments
 (0)