Skip to content

Commit

Permalink
Implement bootstrap components for reproducible tests (#43)
Browse files Browse the repository at this point in the history
* Add starknet-test-components crate

* Implement ChainFullNodeStarter for Starknet devnet

* Implement ChainBootstrapper for devnet

* Implement ProvideStarknetTestTypes

* Use address and amount type from hermes-test-components

* Implement ChainDriverBuilder for StarknetBootstrap

* Use StarknetBootstrap to bootstrap chain in test

* Implement HasChainId for StarknetChain

* Initialize test data directory

* Use parameters from bootstrap for test

* Fix formatting
  • Loading branch information
soareschen authored Aug 5, 2024
1 parent dea1dab commit 65d87d8
Show file tree
Hide file tree
Showing 47 changed files with 670 additions and 84 deletions.
3 changes: 2 additions & 1 deletion relayer/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/target
test-data/
21 changes: 21 additions & 0 deletions relayer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ resolver = "2"
members = [
"crates/starknet-chain-components",
"crates/starknet-chain-context",
"crates/starknet-test-components",
"crates/starknet-integration-tests",
]

Expand Down Expand Up @@ -32,6 +33,7 @@ clap = { version = "4.5.8" }
starknet = { version = "0.11.0" }
url = { version = "2.4.0" }
eyre = { version = "0.6.12" }
tokio = { version = "1.38" }

hermes-runtime-components = { version = "0.1.0" }
hermes-async-runtime-components = { version = "0.1.0" }
Expand All @@ -57,10 +59,12 @@ hermes-any-counterparty = { version = "0.1.0" }

hermes-cli-components = { version = "0.1.0" }

hermes-test-components = { version = "0.1.0" }

hermes-starknet-chain-components = { version = "0.1.0" }
hermes-starknet-test-components = { version = "0.1.0" }
hermes-starknet-chain-context = { version = "0.1.0" }


[patch.crates-io]
ibc = { git = "https://github.com/cosmos/ibc-rs.git", rev = "5dc0b09" }
ibc-core = { git = "https://github.com/cosmos/ibc-rs.git", rev = "5dc0b09" }
Expand Down Expand Up @@ -121,4 +125,5 @@ hermes-wasm-client-components = { git = "https://github.com/informalsystems/herm
hermes-wasm-test-components = { git = "https://github.com/informalsystems/hermes-sdk.git" }

hermes-starknet-chain-components = { path = "./crates/starknet-chain-components" }
hermes-starknet-test-components = { path = "./crates/starknet-test-components" }
hermes-starknet-chain-context = { path = "./crates/starknet-chain-context" }
1 change: 1 addition & 0 deletions relayer/crates/starknet-chain-components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ readme = "README.md"
[dependencies]
cgp-core = { workspace = true }
hermes-relayer-components = { workspace = true }
hermes-test-components = { workspace = true }

starknet = { workspace = true }
12 changes: 10 additions & 2 deletions relayer/crates/starknet-chain-components/src/components.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cgp_core::prelude::*;
pub use hermes_relayer_components::chain::traits::send_message::MessageSenderComponent;
pub use hermes_relayer_components::chain::traits::types::chain_id::ChainIdTypeComponent;
pub use hermes_relayer_components::chain::traits::types::event::EventTypeComponent;
pub use hermes_relayer_components::chain::traits::types::message::MessageTypeComponent;
use hermes_relayer_components::error::impls::retry::ReturnRetryable;
Expand All @@ -12,6 +13,9 @@ pub use hermes_relayer_components::transaction::traits::submit_tx::TxSubmitterCo
pub use hermes_relayer_components::transaction::traits::types::transaction::TransactionTypeComponent;
pub use hermes_relayer_components::transaction::traits::types::tx_hash::TransactionHashTypeComponent;
pub use hermes_relayer_components::transaction::traits::types::tx_response::TxResponseTypeComponent;
pub use hermes_test_components::chain::traits::types::address::AddressTypeComponent;
pub use hermes_test_components::chain::traits::types::amount::AmountTypeComponent;
pub use hermes_test_components::chain::traits::types::denom::DenomTypeComponent;

use crate::impls::contract::call::CallStarknetContract;
use crate::impls::contract::invoke::InvokeStarknetContract;
Expand All @@ -25,6 +29,8 @@ use crate::impls::tx_response::{DefaultPollTimeout, QueryTransactionReceipt};
use crate::impls::types::address::ProvideFeltAddressType;
use crate::impls::types::amount::ProvideU256Amount;
use crate::impls::types::blob::ProvideFeltBlobType;
use crate::impls::types::chain_id::ProvideFeltChainId;
use crate::impls::types::denom::ProvideTokenAddressDenom;
use crate::impls::types::event::ProvideStarknetEvent;
use crate::impls::types::message::ProvideCallMessage;
use crate::impls::types::method::ProvideFeltMethodSelector;
Expand All @@ -37,13 +43,13 @@ pub use crate::traits::contract::message::InvokeContractMessageBuilderComponent;
pub use crate::traits::messages::transfer::TransferTokenMessageBuilderComponent;
pub use crate::traits::queries::token_balance::TokenBalanceQuerierComponent;
pub use crate::traits::transfer::TokenTransferComponent;
pub use crate::traits::types::address::AddressTypeComponent;
pub use crate::traits::types::amount::AmountTypeComponent;
pub use crate::traits::types::blob::BlobTypeComponent;
pub use crate::traits::types::method::MethodSelectorTypeComponent;

define_components! {
StarknetChainComponents {
ChainIdTypeComponent:
ProvideFeltChainId,
AddressTypeComponent:
ProvideFeltAddressType,
BlobTypeComponent:
Expand All @@ -54,6 +60,8 @@ define_components! {
ProvideStarknetEvent,
AmountTypeComponent:
ProvideU256Amount,
DenomTypeComponent:
ProvideTokenAddressDenom,
TransactionTypeComponent:
ProvideCallTransaction,
TransactionHashTypeComponent:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use cgp_core::error::CanRaiseError;
use hermes_test_components::chain::traits::types::address::HasAddressType;
use starknet::core::types::{BlockId, BlockTag, Felt, FunctionCall};
use starknet::providers::{Provider, ProviderError};

use crate::traits::contract::call::ContractCaller;
use crate::traits::provider::HasStarknetProvider;
use crate::traits::types::address::HasAddressType;
use crate::traits::types::blob::HasBlobType;
use crate::traits::types::method::HasMethodSelectorType;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use hermes_relayer_components::chain::traits::types::message::HasMessageType;
use hermes_test_components::chain::traits::types::address::HasAddressType;
use starknet::accounts::Call;
use starknet::core::types::Felt;

use crate::traits::contract::message::InvokeContractMessageBuilder;
use crate::traits::types::address::HasAddressType;
use crate::traits::types::blob::HasBlobType;
use crate::traits::types::method::HasMethodSelectorType;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use hermes_relayer_components::chain::traits::types::message::HasMessageType;
use hermes_test_components::chain::traits::types::address::HasAddressType;
use hermes_test_components::chain::traits::types::amount::HasAmountType;
use starknet::accounts::Call;
use starknet::core::types::{Felt, U256};
use starknet::core::types::Felt;
use starknet::macros::selector;

use crate::traits::messages::transfer::TransferTokenMessageBuilder;
use crate::traits::types::address::HasAddressType;
use crate::traits::types::amount::HasAmountType;
use crate::traits::types::blob::HasBlobType;
use crate::traits::types::method::HasMethodSelectorType;
use crate::types::amount::StarknetAmount;

pub const TRANSFER_SELECTOR: Felt = selector!("transfer");

Expand All @@ -16,21 +17,22 @@ pub struct BuildTransferErc20TokenMessage;
impl<Chain> TransferTokenMessageBuilder<Chain> for BuildTransferErc20TokenMessage
where
Chain: HasAddressType<Address = Felt>
+ HasAmountType<Amount = U256>
+ HasAmountType<Amount = StarknetAmount>
+ HasBlobType<Blob = Vec<Felt>>
+ HasMethodSelectorType<MethodSelector = Felt>
+ HasMessageType<Message = Call>,
{
fn build_transfer_token_message(
_chain: &Chain,
token_address: &Felt,
recipient: &Felt,
amount: &U256,
amount: &StarknetAmount,
) -> Call {
let quantity = amount.quantity;

Call {
to: *token_address,
to: amount.token_address,
selector: TRANSFER_SELECTOR,
calldata: vec![*recipient, amount.low().into(), amount.high().into()],
calldata: vec![*recipient, quantity.low().into(), quantity.high().into()],
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use cgp_core::error::CanRaiseError;
use hermes_test_components::chain::traits::types::address::HasAddressType;
use hermes_test_components::chain::traits::types::amount::HasAmountType;
use starknet::core::types::{Felt, U256};
use starknet::macros::selector;

use crate::traits::contract::call::CanCallContract;
use crate::traits::queries::token_balance::TokenBalanceQuerier;
use crate::traits::types::address::HasAddressType;
use crate::traits::types::amount::HasAmountType;
use crate::traits::types::blob::HasBlobType;
use crate::traits::types::method::HasMethodSelectorType;
use crate::types::amount::StarknetAmount;

pub struct QueryErc20TokenBalance;

Expand All @@ -16,7 +17,7 @@ pub const BALANCE_SELECTOR: Felt = selector!("balance_of");
impl<Chain> TokenBalanceQuerier<Chain> for QueryErc20TokenBalance
where
Chain: HasAddressType<Address = Felt>
+ HasAmountType<Amount = U256>
+ HasAmountType<Amount = StarknetAmount>
+ HasBlobType<Blob = Vec<Felt>>
+ HasMethodSelectorType<MethodSelector = Felt>
+ CanCallContract
Expand All @@ -26,7 +27,7 @@ where
chain: &Chain,
token_address: &Felt,
account_address: &Felt,
) -> Result<U256, Chain::Error> {
) -> Result<StarknetAmount, Chain::Error> {
let output = chain
.call_contract(token_address, &BALANCE_SELECTOR, &vec![*account_address])
.await?;
Expand All @@ -40,8 +41,11 @@ where
let low = u128::from_be_bytes(e1.to_bytes_be()[16..].try_into().unwrap());
let high = u128::from_be_bytes(e2.to_bytes_be()[16..].try_into().unwrap());

let amount = U256::from_words(low, high);
let quantity = U256::from_words(low, high);

Ok(amount)
Ok(StarknetAmount {
quantity,
token_address: *token_address,
})
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use hermes_relayer_components::chain::traits::send_message::CanSendSingleMessage;
use hermes_test_components::chain::traits::types::address::HasAddressType;
use hermes_test_components::chain::traits::types::amount::HasAmountType;
use starknet::core::types::Felt;
use starknet::macros::selector;

use crate::traits::messages::transfer::CanBuildTransferTokenMessage;
use crate::traits::transfer::TokenTransferer;
use crate::traits::types::address::HasAddressType;
use crate::traits::types::amount::HasAmountType;
use crate::traits::types::blob::HasBlobType;
use crate::traits::types::method::HasMethodSelectorType;

Expand All @@ -24,11 +24,10 @@ where
{
async fn transfer_token(
chain: &Chain,
token_address: &Chain::Address,
recipient: &Chain::Address,
amount: &Chain::Amount,
) -> Result<(), Chain::Error> {
let message = chain.build_transfer_token_message(token_address, recipient, amount);
let message = chain.build_transfer_token_message(recipient, amount);

chain.send_message(message).await?;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use cgp_core::Async;
use hermes_test_components::chain::traits::types::address::ProvideAddressType;
use starknet::core::types::Felt;

use crate::traits::types::address::ProvideAddressType;

pub struct ProvideFeltAddressType;

impl<Chain: Async> ProvideAddressType<Chain> for ProvideFeltAddressType {
Expand Down
17 changes: 13 additions & 4 deletions relayer/crates/starknet-chain-components/src/impls/types/amount.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use cgp_core::Async;
use starknet::core::types::U256;
use hermes_test_components::chain::traits::types::amount::ProvideAmountType;
use hermes_test_components::chain::traits::types::denom::HasDenomType;
use starknet::core::types::Felt;

use crate::traits::types::amount::ProvideAmountType;
use crate::types::amount::StarknetAmount;

pub struct ProvideU256Amount;

impl<Chain: Async> ProvideAmountType<Chain> for ProvideU256Amount {
type Amount = U256;
impl<Chain: Async> ProvideAmountType<Chain> for ProvideU256Amount
where
Chain: HasDenomType<Denom = Felt>,
{
type Amount = StarknetAmount;

fn amount_denom(amount: &StarknetAmount) -> &Felt {
&amount.token_address
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use cgp_core::Async;
use hermes_relayer_components::chain::traits::types::chain_id::ProvideChainIdType;
use starknet::core::types::Felt;

pub struct ProvideFeltChainId;

impl<Chain: Async> ProvideChainIdType<Chain> for ProvideFeltChainId {
type ChainId = Felt;
}
12 changes: 12 additions & 0 deletions relayer/crates/starknet-chain-components/src/impls/types/denom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use hermes_test_components::chain::traits::types::address::HasAddressType;
use hermes_test_components::chain::traits::types::denom::ProvideDenomType;

pub struct ProvideTokenAddressDenom;

impl<Chain> ProvideDenomType<Chain> for ProvideTokenAddressDenom
where
Chain: HasAddressType,
Chain::Address: Clone,
{
type Denom = Chain::Address;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod address;
pub mod amount;
pub mod blob;
pub mod chain_id;
pub mod denom;
pub mod event;
pub mod message;
pub mod method;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cgp_core::prelude::*;
use hermes_test_components::chain::traits::types::address::HasAddressType;

use crate::traits::types::address::HasAddressType;
use crate::traits::types::blob::HasBlobType;
use crate::traits::types::method::HasMethodSelectorType;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cgp_core::prelude::*;
use hermes_relayer_components::chain::traits::types::event::HasEventType;
use hermes_test_components::chain::traits::types::address::HasAddressType;

use crate::traits::types::address::HasAddressType;
use crate::traits::types::blob::HasBlobType;
use crate::traits::types::method::HasMethodSelectorType;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cgp_core::prelude::*;
use hermes_relayer_components::chain::traits::types::message::HasMessageType;
use hermes_test_components::chain::traits::types::address::HasAddressType;

use crate::traits::types::address::HasAddressType;
use crate::traits::types::blob::HasBlobType;
use crate::traits::types::method::HasMethodSelectorType;

Expand Down
Loading

0 comments on commit 65d87d8

Please sign in to comment.