-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate mesh and contract state update out
- Loading branch information
Xiangyi Zheng
committed
Nov 9, 2024
1 parent
144fae5
commit 04125bd
Showing
9 changed files
with
161 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::protocol::ProtocolState; | ||
use crate::rpc_client; | ||
use near_account_id::AccountId; | ||
use std::sync::Arc; | ||
use std::time::{Duration, Instant}; | ||
use tokio::sync::RwLock; | ||
|
||
pub struct ContractStateUpdater { | ||
rpc_client: near_fetch::Client, | ||
mpc_contract_id: AccountId, | ||
} | ||
|
||
impl ContractStateUpdater { | ||
pub fn init( | ||
rpc_client: near_fetch::Client, | ||
mpc_contract_id: AccountId, | ||
) -> (Self, Arc<RwLock<Option<ProtocolState>>>) { | ||
let updater = Self { | ||
rpc_client, | ||
mpc_contract_id: mpc_contract_id.clone(), | ||
}; | ||
let contract_state = Arc::new(RwLock::new(None)); | ||
(updater, contract_state) | ||
} | ||
|
||
pub async fn run( | ||
&self, | ||
contract_state: Arc<RwLock<Option<ProtocolState>>>, | ||
) -> anyhow::Result<()> { | ||
let mut last_update = Instant::now(); | ||
loop { | ||
if last_update.elapsed() > Duration::from_millis(1000) { | ||
let mut contract_state = contract_state.write().await; | ||
*contract_state = | ||
rpc_client::fetch_mpc_contract_state(&self.rpc_client, &self.mpc_contract_id) | ||
.await | ||
.ok(); | ||
last_update = Instant::now(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod cli; | ||
pub mod config; | ||
pub mod contract_updater; | ||
pub mod gcp; | ||
pub mod http_client; | ||
pub mod indexer; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.