Skip to content

Commit 1b98c13

Browse files
cyberphysic4lkodemartinThoralf-M
authored andcommitted
feat(protocol): PCR-2 iota-indexer (#5278)
* iota-indexer changes * refactor(indexer): use new system state summary (#5477) * refactor(indexer): add burned computation cost to schema * refactor(indexer)!: use new system-state summary type * Update crates/iota-indexer/src/handlers/checkpoint_handler.rs Co-authored-by: Thoralf-M <[email protected]> --------- Co-authored-by: Konstantinos Demartinos <[email protected]> Co-authored-by: Thoralf-M <[email protected]>
1 parent b7ecadd commit 1b98c13

File tree

9 files changed

+234
-64
lines changed

9 files changed

+234
-64
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE checkpoints
2+
DROP COLUMN computation_cost_burned;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Your SQL goes here
2+
ALTER TABLE checkpoints
3+
ADD COLUMN computation_cost_burned BIGINT;

crates/iota-indexer/src/apis/coin_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use iota_types::{
1717
};
1818
use jsonrpsee::{RpcModule, core::RpcResult};
1919

20-
use crate::indexer_reader::IndexerReader;
20+
use crate::{indexer_reader::IndexerReader, types::IotaSystemStateSummaryView};
2121

2222
pub(crate) struct CoinReadApi {
2323
inner: IndexerReader,
@@ -142,7 +142,7 @@ impl CoinReadApiServer for CoinReadApi {
142142
.inner
143143
.spawn_blocking(|this| this.get_latest_iota_system_state())
144144
.await?
145-
.iota_total_supply,
145+
.iota_total_supply(),
146146
})
147147
} else {
148148
self.inner

crates/iota-indexer/src/apis/governance_api.rs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@ use iota_types::{
2121
governance::StakedIota,
2222
id::ID,
2323
iota_serde::BigInt,
24-
iota_system_state::{PoolTokenExchangeRate, iota_system_state_summary::IotaSystemStateSummary},
24+
iota_system_state::{
25+
PoolTokenExchangeRate,
26+
iota_system_state_summary::{
27+
IotaSystemStateSummary, IotaSystemStateSummaryV1, IotaSystemStateSummaryV2,
28+
},
29+
},
2530
timelock::timelocked_staked_iota::TimelockedStakedIota,
2631
};
2732
use jsonrpsee::{RpcModule, core::RpcResult};
2833
use serde::{Serialize, de::DeserializeOwned};
2934
use tokio::sync::Mutex;
3035

31-
use crate::{errors::IndexerError, indexer_reader::IndexerReader};
36+
use crate::{
37+
errors::IndexerError, indexer_reader::IndexerReader, types::IotaSystemStateSummaryView,
38+
};
3239

3340
/// Maximum amount of staked objects for querying.
3441
const MAX_QUERY_STAKED_OBJECTS: usize = 1000;
@@ -63,9 +70,8 @@ impl GovernanceReadApi {
6370
}
6471

6572
async fn get_validators_apy(&self) -> Result<ValidatorApys, IndexerError> {
66-
let system_state_summary: IotaSystemStateSummary =
67-
self.get_latest_iota_system_state().await?;
68-
let epoch = system_state_summary.epoch;
73+
let system_state_summary = self.get_latest_iota_system_state().await?;
74+
let epoch = system_state_summary.epoch();
6975

7076
let exchange_rate_table = self.exchange_rates(&system_state_summary).await?;
7177

@@ -170,7 +176,7 @@ impl GovernanceReadApi {
170176
});
171177

172178
let system_state_summary = self.get_latest_iota_system_state().await?;
173-
let epoch = system_state_summary.epoch;
179+
let epoch = system_state_summary.epoch();
174180

175181
let (candidate_rates, pending_rates) = tokio::try_join!(
176182
self.candidate_validators_exchange_rate(&system_state_summary),
@@ -236,7 +242,7 @@ impl GovernanceReadApi {
236242
});
237243

238244
let system_state_summary = self.get_latest_iota_system_state().await?;
239-
let epoch = system_state_summary.epoch;
245+
let epoch = system_state_summary.epoch();
240246

241247
let rates = self
242248
.exchange_rates(&system_state_summary)
@@ -359,7 +365,7 @@ impl GovernanceReadApi {
359365
&self,
360366
system_state_summary: &IotaSystemStateSummary,
361367
) -> Result<Vec<ValidatorExchangeRates>, IndexerError> {
362-
let epoch = system_state_summary.epoch;
368+
let epoch = system_state_summary.epoch();
363369

364370
let mut cache = self.exchange_rates_cache.lock().await;
365371

@@ -399,7 +405,7 @@ impl GovernanceReadApi {
399405
system_state_summary: &IotaSystemStateSummary,
400406
) -> Result<Vec<ValidatorExchangeRates>, IndexerError> {
401407
let tables = system_state_summary
402-
.active_validators
408+
.active_validators()
403409
.iter()
404410
.map(|validator| {
405411
(
@@ -422,8 +428,8 @@ impl GovernanceReadApi {
422428
) -> Result<Vec<ValidatorExchangeRates>, IndexerError> {
423429
let tables = self
424430
.validator_summary_from_system_state(
425-
system_state_summary.inactive_pools_id,
426-
system_state_summary.inactive_pools_size,
431+
system_state_summary.inactive_pools_id(),
432+
system_state_summary.inactive_pools_size(),
427433
|df| bcs::from_bytes::<ID>(&df.bcs_name).map_err(Into::into),
428434
)
429435
.await?;
@@ -470,8 +476,8 @@ impl GovernanceReadApi {
470476
) -> Result<Vec<ValidatorExchangeRates>, IndexerError> {
471477
let tables = self
472478
.validator_summary_from_system_state(
473-
system_state_summary.validator_candidates_id,
474-
system_state_summary.validator_candidates_size,
479+
system_state_summary.validator_candidates_id(),
480+
system_state_summary.validator_candidates_size(),
475481
|df| bcs::from_bytes::<IotaAddress>(&df.bcs_name).map_err(Into::into),
476482
)
477483
.await?;
@@ -502,9 +508,9 @@ impl GovernanceReadApi {
502508
/// let system_state_summary = self.get_latest_iota_system_state().await?;
503509
/// let _ = self.validator_summary_from_system_state(
504510
/// // ID of the object that maps from a staking pool ID to the inactive validator that has that pool as its staking pool
505-
/// system_state_summary.inactive_pools_id,
511+
/// system_state_summary.inactive_pools_id(),
506512
/// // Number of inactive staking pools
507-
/// system_state_summary.inactive_pools_size,
513+
/// system_state_summary.inactive_pools_size(),
508514
/// // Extract the `ID` of the `Inactive` validator from the `DynamicFieldInfo` in the `system_state_summary.inactive_pools_id` table
509515
/// |df| bcs::from_bytes::<ID>(&df.bcs_name).map_err(Into::into),
510516
/// ).await?;
@@ -517,9 +523,9 @@ impl GovernanceReadApi {
517523
/// let system_state_summary = self.get_latest_iota_system_state().await?;
518524
/// let _ = self.validator_summary_from_system_state(
519525
/// // ID of the object that stores preactive validators, mapping their addresses to their Validator structs
520-
/// system_state_summary.validator_candidates_id,
526+
/// system_state_summary.validator_candidates_id(),
521527
/// // Number of preactive validators
522-
/// system_state_summary.validator_candidates_size,
528+
/// system_state_summary.validator_candidates_size(),
523529
/// // Extract the `IotaAddress` of the `Candidate` validator from the `DynamicFieldInfo` in the `system_state_summary.validator_candidates_id` table
524530
/// |df| bcs::from_bytes::<IotaAddress>(&df.bcs_name).map_err(Into::into),
525531
/// ).await?;
@@ -683,10 +689,20 @@ impl GovernanceReadApiServer for GovernanceReadApi {
683689
Ok(epoch.committee().map_err(IndexerError::from)?.into())
684690
}
685691

686-
async fn get_latest_iota_system_state(&self) -> RpcResult<IotaSystemStateSummary> {
687-
self.get_latest_iota_system_state()
688-
.await
689-
.map_err(Into::into)
692+
async fn get_latest_iota_system_state(&self) -> RpcResult<IotaSystemStateSummaryV2> {
693+
Ok(self
694+
.get_latest_iota_system_state()
695+
.await?
696+
.try_into()
697+
.map_err(IndexerError::from)?)
698+
}
699+
700+
async fn get_latest_iota_system_state_v1(&self) -> RpcResult<IotaSystemStateSummaryV1> {
701+
Ok(self
702+
.get_latest_iota_system_state()
703+
.await?
704+
.try_into()
705+
.map_err(IndexerError::from)?)
690706
}
691707

692708
async fn get_reference_gas_price(&self) -> RpcResult<BigInt<u64>> {

crates/iota-indexer/src/handlers/checkpoint_handler.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ use iota_types::{
1818
base_types::ObjectID,
1919
dynamic_field::{DynamicFieldInfo, DynamicFieldName, DynamicFieldType},
2020
effects::TransactionEffectsAPI,
21-
event::SystemEpochInfoEventV1,
22-
iota_system_state::{
23-
IotaSystemStateTrait, get_iota_system_state,
24-
iota_system_state_summary::IotaSystemStateSummary,
25-
},
21+
event::{SystemEpochInfoEvent, SystemEpochInfoEventV1, SystemEpochInfoEventV2},
22+
iota_system_state::{IotaSystemStateTrait, get_iota_system_state},
2623
messages_checkpoint::{
2724
CertifiedCheckpointSummary, CheckpointContents, CheckpointSequenceNumber,
2825
},
@@ -55,7 +52,8 @@ use crate::{
5552
},
5653
types::{
5754
EventIndex, IndexedCheckpoint, IndexedDeletedObject, IndexedEpochInfo, IndexedEvent,
58-
IndexedObject, IndexedPackage, IndexedTransaction, IndexerResult, TransactionKind, TxIndex,
55+
IndexedObject, IndexedPackage, IndexedTransaction, IndexerResult,
56+
IotaSystemStateSummaryView, TransactionKind, TxIndex,
5957
},
6058
};
6159

@@ -205,14 +203,14 @@ impl CheckpointHandler {
205203
} = data;
206204

207205
// Genesis epoch
206+
let system_state =
207+
get_iota_system_state(&checkpoint_object_store)?.into_iota_system_state_summary();
208208
if *checkpoint_summary.sequence_number() == 0 {
209209
info!("Processing genesis epoch");
210-
let system_state: IotaSystemStateSummary =
211-
get_iota_system_state(&checkpoint_object_store)?.into_iota_system_state_summary();
212210
return Ok(Some(EpochToCommit {
213211
last_epoch: None,
214212
new_epoch: IndexedEpochInfo::from_new_system_state_summary(
215-
system_state,
213+
&system_state,
216214
0, // first_checkpoint_id
217215
None,
218216
),
@@ -225,23 +223,33 @@ impl CheckpointHandler {
225223
return Ok(None);
226224
}
227225

228-
let system_state: IotaSystemStateSummary =
229-
get_iota_system_state(&checkpoint_object_store)?.into_iota_system_state_summary();
230-
231-
let epoch_event = transactions
226+
let event = transactions
232227
.iter()
233228
.flat_map(|t| t.events.as_ref().map(|e| &e.data))
234229
.flatten()
235-
.find(|ev| ev.is_system_epoch_info_event())
230+
.find(|ev| ev.is_system_epoch_info_event_v1() || ev.is_system_epoch_info_event_v2())
231+
.map(|ev| {
232+
if ev.is_system_epoch_info_event_v2() {
233+
SystemEpochInfoEvent::V2(
234+
bcs::from_bytes::<SystemEpochInfoEventV2>(&ev.contents).expect(
235+
"event deserialization should succeed as type was pre-validated",
236+
),
237+
)
238+
} else {
239+
SystemEpochInfoEvent::V1(
240+
bcs::from_bytes::<SystemEpochInfoEventV1>(&ev.contents).expect(
241+
"event deserialization should succeed as type was pre-validated",
242+
),
243+
)
244+
}
245+
})
236246
.unwrap_or_else(|| {
237247
panic!(
238-
"Can't find SystemEpochInfoEventV1 in epoch end checkpoint {}",
248+
"Can't find SystemEpochInfoEvent in epoch end checkpoint {}",
239249
checkpoint_summary.sequence_number()
240250
)
241251
});
242252

243-
let event = bcs::from_bytes::<SystemEpochInfoEventV1>(&epoch_event.contents)?;
244-
245253
// Now we just entered epoch X, we want to calculate the diff between
246254
// TotalTransactionsByEndOfEpoch(X-1) and TotalTransactionsByEndOfEpoch(X-2).
247255
// Note that on the indexer's chain-reading side, this is not guaranteed
@@ -250,11 +258,12 @@ impl CheckpointHandler {
250258
// guarantee that the previous epoch's checkpoints have been written to
251259
// db.
252260

253-
let network_tx_count_prev_epoch = match system_state.epoch {
261+
let epoch = system_state.epoch();
262+
let network_tx_count_prev_epoch = match epoch {
254263
// If first epoch change, this number is 0
255264
1 => Ok(0),
256265
_ => {
257-
let last_epoch = system_state.epoch - 2;
266+
let last_epoch = epoch - 2;
258267
state
259268
.get_network_total_transactions_by_end_of_epoch(last_epoch)
260269
.await
@@ -269,7 +278,7 @@ impl CheckpointHandler {
269278
network_tx_count_prev_epoch,
270279
)),
271280
new_epoch: IndexedEpochInfo::from_new_system_state_summary(
272-
system_state,
281+
&system_state,
273282
checkpoint_summary.sequence_number + 1, // first_checkpoint_id
274283
Some(&event),
275284
),

crates/iota-indexer/src/models/checkpoints.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct StoredCheckpoint {
3939
pub end_of_epoch_data: Option<Vec<u8>>,
4040
pub min_tx_sequence_number: Option<i64>,
4141
pub max_tx_sequence_number: Option<i64>,
42+
pub computation_cost_burned: Option<i64>,
4243
}
4344

4445
impl From<&IndexedCheckpoint> for StoredCheckpoint {
@@ -60,6 +61,7 @@ impl From<&IndexedCheckpoint> for StoredCheckpoint {
6061
timestamp_ms: c.timestamp_ms as i64,
6162
total_gas_cost: c.total_gas_cost,
6263
computation_cost: c.computation_cost as i64,
64+
computation_cost_burned: Some(c.computation_cost_burned as i64),
6365
storage_cost: c.storage_cost as i64,
6466
storage_rebate: c.storage_rebate as i64,
6567
non_refundable_storage_fee: c.non_refundable_storage_fee as i64,
@@ -147,6 +149,9 @@ impl TryFrom<StoredCheckpoint> for RpcCheckpoint {
147149
})
148150
.transpose()?;
149151

152+
let computation_cost_burned = checkpoint
153+
.computation_cost_burned
154+
.unwrap_or(checkpoint.computation_cost) as u64;
150155
Ok(RpcCheckpoint {
151156
epoch: checkpoint.epoch as u64,
152157
sequence_number: checkpoint.sequence_number as u64,
@@ -155,9 +160,7 @@ impl TryFrom<StoredCheckpoint> for RpcCheckpoint {
155160
end_of_epoch_data,
156161
epoch_rolling_gas_cost_summary: GasCostSummary {
157162
computation_cost: checkpoint.computation_cost as u64,
158-
// TODO_FIXED_BASE_FEE: update computation cost burned in checkpoint to be used
159-
// here in issue #3122
160-
computation_cost_burned: checkpoint.computation_cost as u64,
163+
computation_cost_burned,
161164
storage_cost: checkpoint.storage_cost as u64,
162165
storage_rebate: checkpoint.storage_rebate as u64,
163166
non_refundable_storage_fee: checkpoint.non_refundable_storage_fee as u64,

crates/iota-indexer/src/models/epoch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use iota_types::iota_system_state::iota_system_state_summary::IotaSystemStateSum
99
use crate::{
1010
errors::IndexerError,
1111
schema::{epochs, feature_flags, protocol_configs},
12-
types::IndexedEpochInfo,
12+
types::{IndexedEpochInfo, IotaSystemStateSummaryView},
1313
};
1414

1515
#[derive(Queryable, Insertable, Debug, Clone, Default)]
@@ -165,7 +165,7 @@ impl TryFrom<StoredEpochInfo> for EpochInfo {
165165
Ok(EpochInfo {
166166
epoch: value.epoch as u64,
167167
validators: system_state
168-
.map(|s| s.active_validators)
168+
.map(|s| s.active_validators().to_vec())
169169
.unwrap_or_default(),
170170
epoch_total_transactions: value.epoch_total_transactions.unwrap_or(0) as u64,
171171
first_checkpoint_id: value.first_checkpoint_id as u64,

crates/iota-indexer/src/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ diesel::table! {
6060
end_of_epoch_data -> Nullable<Bytea>,
6161
min_tx_sequence_number -> Nullable<Int8>,
6262
max_tx_sequence_number -> Nullable<Int8>,
63+
computation_cost_burned -> Nullable<Int8>,
6364
}
6465
}
6566

0 commit comments

Comments
 (0)