Skip to content

Commit eae04a1

Browse files
committed
fix state height check before returning reward proofs
1 parent 8b9fc47 commit eae04a1

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

sequencer/src/api/sql.rs

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,33 @@ impl RewardAccountProofDataSource for SqlStorage {
120120
"cannot get accounts for height {height}: no blocks available"
121121
);
122122

123-
// Check if we have the desired state snapshot. If so, we can load the desired accounts
124-
// directly.
125-
if height < block_height {
126-
let (tree, _) = load_v1_reward_accounts(self, height, &[account])
127-
.await
128-
.with_context(|| {
129-
format!("failed to load v1 reward account {account:?} at height {height}")
130-
})?;
123+
ensure!(
124+
height < block_height,
125+
"requested height {height} is not yet available. latest block height: {block_height}"
126+
);
131127

132-
let (proof, balance) = RewardAccountProofV1::prove(&tree, account.into())
133-
.with_context(|| {
134-
format!("reward account {account:?} not available at height {height}")
135-
})?;
128+
let merklized_state_height = tx
129+
.get_last_state_height()
130+
.await
131+
.context("getting merklized state height")? as u64;
132+
ensure!(
133+
height <= merklized_state_height,
134+
"requested height {height} is not yet available. latest merklized state height: \
135+
{merklized_state_height}"
136+
);
136137

137-
Ok(RewardAccountQueryDataV1 { balance, proof })
138-
} else {
139-
bail!(
140-
"requested height {height} is not yet available (latest block height: \
141-
{block_height})"
142-
);
143-
}
138+
let (tree, _) = load_v1_reward_accounts(self, height, &[account])
139+
.await
140+
.with_context(|| {
141+
format!("failed to load v1 reward account {account:?} at height {height}")
142+
})?;
143+
144+
let (proof, balance) =
145+
RewardAccountProofV1::prove(&tree, account.into()).with_context(|| {
146+
format!("reward account {account:?} not available at height {height}")
147+
})?;
148+
149+
Ok(RewardAccountQueryDataV1 { balance, proof })
144150
}
145151

146152
async fn load_v2_reward_account_proof(
@@ -160,27 +166,33 @@ impl RewardAccountProofDataSource for SqlStorage {
160166
"cannot get accounts for height {height}: no blocks available"
161167
);
162168

163-
// Check if we have the desired state snapshot. If so, we can load the desired accounts
164-
// directly.
165-
if height < block_height {
166-
let (tree, _) = load_v2_reward_accounts(self, height, &[account])
167-
.await
168-
.with_context(|| {
169-
format!("failed to load v2 reward account {account:?} at height {height}")
170-
})?;
169+
ensure!(
170+
height < block_height,
171+
"requested height {height} is not yet available. latest block height: {block_height}"
172+
);
171173

172-
let (proof, balance) = RewardAccountProofV2::prove(&tree, account.into())
173-
.with_context(|| {
174-
format!("reward account {account:?} not available at height {height}")
175-
})?;
174+
let merklized_state_height = tx
175+
.get_last_state_height()
176+
.await
177+
.context("getting merklized state height")? as u64;
178+
ensure!(
179+
height <= merklized_state_height,
180+
"requested height {height} is not yet available. latest merklized state height: \
181+
{merklized_state_height}"
182+
);
176183

177-
Ok(RewardAccountQueryDataV2 { balance, proof })
178-
} else {
179-
bail!(
180-
"requested height {height} is not yet available (latest block height: \
181-
{block_height})"
182-
);
183-
}
184+
let (tree, _) = load_v2_reward_accounts(self, height, &[account])
185+
.await
186+
.with_context(|| {
187+
format!("failed to load v2 reward account {account:?} at height {height}")
188+
})?;
189+
190+
let (proof, balance) =
191+
RewardAccountProofV2::prove(&tree, account.into()).with_context(|| {
192+
format!("reward account {account:?} not available at height {height}")
193+
})?;
194+
195+
Ok(RewardAccountQueryDataV2 { balance, proof })
184196
}
185197
}
186198
impl CatchupStorage for SqlStorage {

0 commit comments

Comments
 (0)