Skip to content

Commit

Permalink
refactor(ethexe): Move WVara eth getter to Router instance (#4153)
Browse files Browse the repository at this point in the history
  • Loading branch information
breathx authored Aug 21, 2024
1 parent 3e9ae98 commit 3311e06
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 42 deletions.
12 changes: 7 additions & 5 deletions ethexe/cli/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ impl TestEnv {
Ethereum::deploy(&rpc_url, validators, signer.clone(), sender_address).await?
};

let router_address = ethereum.router().address();
let router = ethereum.router();
let router_query = router.query();

let router_query = RouterQuery::new(&rpc_url, router_address).await?;
let genesis_block_hash = router_query.genesis_block_hash().await?;

let blob_reader = blob_reader.unwrap_or_else(|| Arc::new(MockBlobReader::new(block_time)));

let router_address = router.address();

let query = Query::new(
Arc::new(db.clone()),
&rpc_url,
Expand Down Expand Up @@ -449,7 +451,7 @@ async fn ping() {

let program_address = ethexe_signer::Address::try_from(program_id).unwrap();

let wvara = env.ethereum.wvara();
let wvara = env.ethereum.router().wvara();

log::info!("📗 Approving WVara to mirror");
wvara.approve_all(program_address.0.into()).await.unwrap();
Expand Down Expand Up @@ -613,7 +615,7 @@ async fn ping_reorg() {

let program_address = ethexe_signer::Address::try_from(program_id).unwrap();

let wvara = env.ethereum.wvara();
let wvara = env.ethereum.router().wvara();

log::info!("📗 Approving WVara to mirror");
wvara.approve_all(program_address.0.into()).await.unwrap();
Expand Down Expand Up @@ -874,7 +876,7 @@ async fn ping_deep_sync() {
// Send message in between.
let program_address = ethexe_signer::Address::try_from(program_id).unwrap();

let wvara = env.ethereum.wvara();
let wvara = env.ethereum.router().wvara();

log::info!("📗 Approving WVara to mirror");
wvara.approve_all(program_address.0.into()).await.unwrap();
Expand Down
24 changes: 11 additions & 13 deletions ethexe/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use ethexe_signer::{Address as LocalAddress, PublicKey, Signer as LocalSigner};
use mirror::Mirror;
use router::{Router, RouterQuery};
use std::sync::Arc;
use wvara::WVara;

mod abi;
mod eip1167;
Expand All @@ -72,7 +71,7 @@ pub(crate) fn decode_log<E: SolEvent>(log: Log) -> Result<E> {

pub struct Ethereum {
router_address: Address,
wrapped_vara_address: Address,
wvara_address: Address,
provider: Arc<AlloyProvider>,
}

Expand All @@ -84,15 +83,15 @@ impl Ethereum {
sender_address: LocalAddress,
) -> Result<Self> {
let router_query = RouterQuery::new(rpc_url, router_address).await?;
let wrapped_vara_address = router_query.wrapped_vara_address().await?;
let wvara_address = router_query.wvara_address().await?;

let router_address = Address::new(router_address.0);

let provider = create_provider(rpc_url, signer, sender_address).await?;

Ok(Self {
router_address,
wrapped_vara_address,
wvara_address,
provider,
})
}
Expand Down Expand Up @@ -126,7 +125,7 @@ impl Ethereum {
)
.await?;
let wrapped_vara = IWrappedVara::new(*proxy.address(), provider.clone());
let wrapped_vara_address = *wrapped_vara.address();
let wvara_address = *wrapped_vara.address();

let nonce = provider.get_transaction_count(deployer_address).await?;
let mirror_address = deployer_address.create(
Expand All @@ -150,7 +149,7 @@ impl Ethereum {
initialOwner: deployer_address,
_mirror: mirror_address,
_mirrorProxy: mirror_proxy_address,
_wrappedVara: wrapped_vara_address,
_wrappedVara: wvara_address,
_validatorsKeys: validators,
}
.abi_encode(),
Expand All @@ -174,7 +173,7 @@ impl Ethereum {

Ok(Self {
router_address,
wrapped_vara_address,
wvara_address,
provider,
})
}
Expand All @@ -184,17 +183,16 @@ impl Ethereum {
}

pub fn router(&self) -> Router {
Router::new(self.router_address, self.provider.clone())
Router::new(
self.router_address,
self.wvara_address,
self.provider.clone(),
)
}

pub fn mirror(&self, address: LocalAddress) -> Mirror {
Mirror::new(address.0.into(), self.provider.clone())
}

// TODO (breathx): move in router.
pub fn wvara(&self) -> WVara {
WVara::new(self.wrapped_vara_address, self.provider.clone())
}
}

async fn create_provider(
Expand Down
9 changes: 8 additions & 1 deletion ethexe/ethereum/src/mirror/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crate::{abi::IMirror, AlloyProvider, AlloyTransport};
use alloy::{
primitives::Address,
providers::{ProviderBuilder, RootProvider},
providers::{Provider, ProviderBuilder, RootProvider},
transports::BoxTransport,
};
use anyhow::{anyhow, Result};
Expand All @@ -46,6 +46,13 @@ impl Mirror {
LocalAddress(*self.0.address().0)
}

pub fn query(&self) -> MirrorQuery {
MirrorQuery(QueryInstance::new(
*self.0.address(),
Arc::new(self.0.provider().root().clone()),
))
}

pub async fn send_message(
&self,
payload: impl AsRef<[u8]>,
Expand Down
68 changes: 46 additions & 22 deletions ethexe/ethereum/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{abi::IRouter, AlloyProvider, AlloyTransport};
use crate::{abi::IRouter, wvara::WVara, AlloyProvider, AlloyTransport};
use alloy::{
consensus::{SidecarBuilder, SimpleCoder},
primitives::{Address, Bytes, B256},
Expand All @@ -40,15 +40,38 @@ type Instance = IRouter::IRouterInstance<AlloyTransport, InstanceProvider>;

type QueryInstance = IRouter::IRouterInstance<AlloyTransport, Arc<RootProvider<BoxTransport>>>;

pub struct Router(Instance);
pub struct Router {
instance: Instance,
wvara_address: Address,
}

impl Router {
pub(crate) fn new(address: Address, provider: InstanceProvider) -> Self {
Self(Instance::new(address, provider))
pub(crate) fn new(
address: Address,
wvara_address: Address,
provider: InstanceProvider,
) -> Self {
Self {
instance: Instance::new(address, provider),
wvara_address,
}
}

pub fn address(&self) -> LocalAddress {
LocalAddress(*self.0.address().0)
LocalAddress(*self.instance.address().0)
}

pub fn query(&self) -> RouterQuery {
RouterQuery {
instance: QueryInstance::new(
*self.instance.address(),
Arc::new(self.instance.provider().root().clone()),
),
}
}

pub fn wvara(&self) -> WVara {
WVara::new(self.wvara_address, self.instance.provider().clone())
}

pub async fn update_validators(&self, validators: Vec<H160>) -> Result<H256> {
Expand All @@ -57,7 +80,7 @@ impl Router {
.map(|v| v.to_fixed_bytes().into())
.collect();

let builder = self.0.updateValidators(validators);
let builder = self.instance.updateValidators(validators);
let tx = builder.send().await?;

let receipt = tx.get_receipt().await?;
Expand All @@ -70,7 +93,7 @@ impl Router {
code_id: CodeId,
blob_tx_hash: H256,
) -> Result<H256> {
let builder = self.0.requestCodeValidation(
let builder = self.instance.requestCodeValidation(
code_id.into_bytes().into(),
blob_tx_hash.to_fixed_bytes().into(),
);
Expand All @@ -88,7 +111,7 @@ impl Router {
let code_id = CodeId::generate(code);

let builder = self
.0
.instance
.requestCodeValidation(code_id.into_bytes().into(), B256::ZERO)
.sidecar(SidecarBuilder::<SimpleCoder>::from_slice(code).build()?);
let tx = builder.send().await?;
Expand All @@ -99,9 +122,9 @@ impl Router {
}

pub async fn wait_code_validation(&self, code_id: CodeId) -> Result<bool> {
let filter = Filter::new().address(*self.0.address());
let filter = Filter::new().address(*self.instance.address());
let mut router_events = self
.0
.instance
.provider()
.subscribe_logs(&filter)
.await?
Expand Down Expand Up @@ -132,7 +155,7 @@ impl Router {
payload: impl AsRef<[u8]>,
value: u128,
) -> Result<(H256, ActorId)> {
let builder = self.0.createProgram(
let builder = self.instance.createProgram(
code_id.into_bytes().into(),
salt.to_fixed_bytes().into(),
payload.as_ref().to_vec().into(),
Expand Down Expand Up @@ -165,7 +188,7 @@ impl Router {
commitments: Vec<CodeCommitment>,
signatures: Vec<LocalSignature>,
) -> Result<H256> {
let builder = self.0.commitCodes(
let builder = self.instance.commitCodes(
commitments.into_iter().map(Into::into).collect(),
signatures
.into_iter()
Expand All @@ -183,7 +206,7 @@ impl Router {
signatures: Vec<LocalSignature>,
) -> Result<H256> {
let builder = self
.0
.instance
.commitBlocks(
commitments.into_iter().map(Into::into).collect(),
signatures
Expand All @@ -198,20 +221,21 @@ impl Router {
}
}

pub struct RouterQuery(QueryInstance);
pub struct RouterQuery {
instance: QueryInstance,
}

impl RouterQuery {
pub async fn new(rpc_url: &str, router_address: LocalAddress) -> Result<Self> {
let provider = Arc::new(ProviderBuilder::new().on_builtin(rpc_url).await?);

Ok(Self(QueryInstance::new(
Address::new(router_address.0),
provider,
)))
Ok(Self {
instance: QueryInstance::new(Address::new(router_address.0), provider),
})
}

pub async fn wrapped_vara_address(&self) -> Result<Address> {
self.0
pub async fn wvara_address(&self) -> Result<Address> {
self.instance
.wrappedVara()
.call()
.await
Expand All @@ -220,7 +244,7 @@ impl RouterQuery {
}

pub async fn last_commitment_block_hash(&self) -> Result<H256> {
self.0
self.instance
.lastBlockCommitmentHash()
.call()
.await
Expand All @@ -229,7 +253,7 @@ impl RouterQuery {
}

pub async fn genesis_block_hash(&self) -> Result<H256> {
self.0
self.instance
.genesisBlockHash()
.call()
.await
Expand Down
9 changes: 8 additions & 1 deletion ethexe/ethereum/src/wvara/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crate::{abi::IWrappedVara, AlloyProvider, AlloyTransport};
use alloy::{
primitives::{Address, Uint},
providers::{ProviderBuilder, RootProvider},
providers::{Provider, ProviderBuilder, RootProvider},
transports::BoxTransport,
};
use anyhow::Result;
Expand All @@ -44,6 +44,13 @@ impl WVara {
LocalAddress(*self.0.address().0)
}

pub fn query(&self) -> WVaraQuery {
WVaraQuery(QueryInstance::new(
*self.0.address(),
Arc::new(self.0.provider().root().clone()),
))
}

pub async fn approve(&self, address: Address, value: u128) -> Result<H256> {
let value = Uint::<256, 4>::from(value);

Expand Down

0 comments on commit 3311e06

Please sign in to comment.