Skip to content

Commit 35b797d

Browse files
committed
fix(bridge): prevent ignoring data requests
fix #2388
1 parent bd6c130 commit 35b797d

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

bridges/centralized-ethereum/src/actors/dr_database.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ pub struct DrInfoBridge {
5353
}
5454

5555
/// Data request state
56-
#[derive(Serialize, Deserialize, Clone)]
56+
#[derive(Clone, Default, Serialize, Deserialize)]
5757
pub enum DrState {
5858
/// New: the data request has just been posted to the smart contract.
59+
#[default]
5960
New,
6061
/// Pending: the data request has been created and broadcast to witnet, but it has not been
6162
/// included in a witnet block yet.
@@ -77,12 +78,6 @@ impl fmt::Display for DrState {
7778
}
7879
}
7980

80-
impl Default for DrState {
81-
fn default() -> Self {
82-
Self::New
83-
}
84-
}
85-
8681
/// Data request states in Witnet Request Board contract
8782
#[derive(Serialize, Deserialize, Clone)]
8883
pub enum WitnetQueryStatus {

bridges/centralized-ethereum/src/actors/eth_poller.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
actors::dr_database::{
3-
DrDatabase, DrInfoBridge, DrState, GetLastDrId, SetDrInfoBridge, WitnetQueryStatus,
3+
DrDatabase, DrInfoBridge, GetLastDrId, SetDrInfoBridge, WitnetQueryStatus,
44
},
55
config::Config,
66
};
@@ -127,18 +127,22 @@ impl EthPoller {
127127
}
128128
WitnetQueryStatus::Posted => {
129129
log::info!("[{}] new dr in wrb", i);
130-
if let Some(set_dr_info_bridge) =
130+
if let Ok(set_dr_info_bridge) =
131131
process_posted_request(i.into(), &wrb_contract).await
132132
{
133133
dr_database_addr.do_send(set_dr_info_bridge);
134+
} else {
135+
break;
134136
}
135137
}
136138
WitnetQueryStatus::Reported => {
137139
log::debug!("[{}] already reported", i);
138-
if let Some(set_dr_info_bridge) =
140+
if let Ok(set_dr_info_bridge) =
139141
process_posted_request(i.into(), &wrb_contract).await
140142
{
141143
dr_database_addr.do_send(set_dr_info_bridge);
144+
} else {
145+
break;
142146
}
143147
}
144148
WitnetQueryStatus::Deleted => {
@@ -175,7 +179,7 @@ impl EthPoller {
175179
async fn process_posted_request(
176180
query_id: U256,
177181
wrb_contract: &Contract<web3::transports::Http>,
178-
) -> Option<SetDrInfoBridge> {
182+
) -> Result<SetDrInfoBridge, web3::contract::Error> {
179183
let dr_bytes: Result<Bytes, web3::contract::Error> = wrb_contract
180184
.query(
181185
"readRequestBytecode",
@@ -186,20 +190,32 @@ async fn process_posted_request(
186190
)
187191
.await;
188192

193+
// Re-route some errors as success (explanation below)
189194
match dr_bytes {
190-
Ok(dr_bytes) => Some(SetDrInfoBridge(
191-
query_id,
192-
DrInfoBridge {
193-
dr_bytes,
194-
dr_state: DrState::New,
195-
dr_tx_hash: None,
196-
dr_tx_creation_timestamp: None,
197-
},
198-
)),
195+
Ok(dr_bytes) => Ok(dr_bytes),
199196
Err(err) => {
200197
log::error!("Fail to read dr bytes from contract: {}", err.to_string());
201198

202-
None
199+
// In some versions of the bridge contracts (those based on
200+
// `WitnetRequestBoardTrustableBase`), we may get a revert when trying to fetch the dr
201+
// bytes for a deleted query.
202+
// If that's the case, we can return a success here, with empty bytes, so that the
203+
// request can locally marked as complete, and we can move on.
204+
if err.to_string().contains("WitnetRequestBoardTrustableBase") {
205+
log::error!("Wait! This is an instance of `WitnetRequestBoardTrustableBase`. Let's assume we got a revert because the dr bytes were deleted, and simply move on.");
206+
207+
Ok(Default::default())
208+
// Otherwise, handle the error normally
209+
} else {
210+
Err(err)
211+
}
203212
}
204-
}
213+
// Wrap the dr bytes in a `SetDrInfoBridge` structure
214+
}.map(|dr_bytes| SetDrInfoBridge(
215+
query_id,
216+
DrInfoBridge {
217+
dr_bytes,
218+
..Default::default()
219+
},
220+
))
205221
}

0 commit comments

Comments
 (0)