Skip to content

Commit

Permalink
sc-4550 fix issue when from block is greater than to block number (#14)
Browse files Browse the repository at this point in the history
* Reworked logging
* Stability changes and some code cleanup
* Fixed issue with from block bigger than to_block
  • Loading branch information
konstantinzolotarev authored Apr 9, 2024
1 parent ccfb981 commit 9b8536f
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 145 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ challenger-lib = { path = "challenger" }

ethers = "2.0"
tokio = { version = "1", features = ["full"] }
tokio-util = "0.7.10"
hex-literal = "0.4.1"
env_logger = "0.11.3"
futures = "0.3.28"
eyre = "0.6.8"
log = "0.4.20"
log = { version = "0.4.20", features = ["kv"] }
hex = "0.4.3"
chrono = "0.4.26"
lazy_static = "1.4.0"
tokio-util = "0.7.9"
async-trait = "0.1.73"
warp = "0.3"

[dependencies]
challenger-lib.workspace = true
tokio = { workspace = true, features = ["full"] }
env_logger = { workspace = true }
log = { workspace = true }
log = { workspace = true, features = ["kv"] }
eyre = { workspace = true }
ethers = { workspace = true }
warp = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion challenger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ hex-literal = { workspace = true }
env_logger = { workspace = true }
futures = { workspace = true }
eyre = { workspace = true }
log = { workspace = true }
log = { workspace = true, features = ["kv"] }
hex = { workspace = true }
chrono = { workspace = true }
lazy_static = { workspace = true }
Expand Down
34 changes: 15 additions & 19 deletions challenger/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ pub trait ScribeOptimisticProvider: Send + Sync {
async fn get_block(&self, block_number: U64) -> Result<Option<Block<H256>>>;

/// Returns challenge period from ScribeOptimistic smart contract deployed to `address`.
async fn get_challenge_period(&self, address: Address) -> Result<u16>;
async fn get_challenge_period(&self) -> Result<u16>;

/// Returns list of `OpPokeChallengedSuccessfully` events and log metadata in between `from_block` and `to_block`
/// from smart contract deployed to `address`.
async fn get_successful_challenges(
&self,
address: Address,
from_block: U64,
to_block: U64,
) -> Result<Vec<(OpPokeChallengedSuccessfullyFilter, LogMeta)>>;
Expand All @@ -51,7 +50,6 @@ pub trait ScribeOptimisticProvider: Send + Sync {
/// deployed to `address`.
async fn get_op_pokes(
&self,
address: Address,
from_block: U64,
to_block: U64,
) -> Result<Vec<(OpPokedFilter, LogMeta)>>;
Expand Down Expand Up @@ -117,8 +115,8 @@ where
}

/// Returns challenge period from ScribeOptimistic smart contract deployed to `address`.
async fn get_challenge_period(&self, address: Address) -> Result<u16> {
debug!("Address {:?}: Getting challenge period", address);
async fn get_challenge_period(&self) -> Result<u16> {
debug!("[{:?}] Getting challenge period", self.address);

self.contract
.op_challenge_period()
Expand All @@ -131,46 +129,44 @@ where
/// from smart contract deployed to `address`.
async fn get_successful_challenges(
&self,
address: Address,
from_block: U64,
to_block: U64,
) -> Result<Vec<(OpPokeChallengedSuccessfullyFilter, LogMeta)>> {
debug!(
"Address {:?}, searching OpPokeChallengedSuccessfully events from block {:?} to block {:?}",
address, from_block, to_block
"[{:?}] Searching OpPokeChallengedSuccessfully events from block {:?} to block {:?}",
self.address, from_block, to_block
);
let event =
Contract::event_of_type::<OpPokeChallengedSuccessfullyFilter>(self.client.clone())
.address(ValueOrArray::Array(vec![address]))
.address(ValueOrArray::Array(vec![self.address]))
.from_block(from_block)
.to_block(to_block);

event.query_with_meta().await.wrap_err(format!(
"Address {:?}: Failed to get OpPokeChallengedSuccessfully events from block {:?} to block {:?}",
address, from_block, to_block
"Failed to get OpPokeChallengedSuccessfully events from block {:?} to block {:?}",
from_block, to_block
))
}

/// Returns list of `OpPoked` events and log metadata in between `from_block` and `to_block` from smart contract
/// deployed to `address`.
async fn get_op_pokes(
&self,
address: Address,
from_block: U64,
to_block: U64,
) -> Result<Vec<(OpPokedFilter, LogMeta)>> {
debug!(
"Address {:?}, searching OpPoked events from block {:?} to block {:?}",
address, from_block, to_block
"[{:?}] Searching OpPoked events from block {:?} to block {:?}",
self.address, from_block, to_block
);
let event = Contract::event_of_type::<OpPokedFilter>(self.client.clone())
.address(ValueOrArray::Array(vec![address]))
.address(ValueOrArray::Array(vec![self.address]))
.from_block(from_block)
.to_block(to_block);

event.query_with_meta().await.wrap_err(format!(
"Address {:?}: Failed to get OpPoked events from block {:?} to block {:?}",
address, from_block, to_block
"Failed to get OpPoked events from block {:?} to block {:?}",
from_block, to_block
))
}

Expand All @@ -179,7 +175,7 @@ where
/// Validation logic described in here: https://github.com/chronicleprotocol/scribe/blob/main/docs/Scribe.md#verifying-optimistic-pokes
async fn is_schnorr_signature_valid(&self, op_poked: OpPokedFilter) -> Result<bool> {
debug!(
"Address {:?}: Validating schnorr signature for {:?}",
"[{:?}] Validating schnorr signature for {:?}",
self.address, op_poked
);

Expand All @@ -202,7 +198,7 @@ where
/// NOTE: You have to validate if schnorr signature is INVALID before calling this function !
async fn challenge(&self, schnorr_data: SchnorrData) -> Result<Option<TransactionReceipt>> {
debug!(
"Address {:?}: Challenging schnorr data {:?}",
"[{:?}] Challenging schnorr data {:?}",
self.address, schnorr_data
);

Expand Down
Loading

0 comments on commit 9b8536f

Please sign in to comment.