Skip to content

Commit 2e612dd

Browse files
cyberphysic4lkodemartin
authored andcommitted
feat(protocol): PCR-2 iota-json-rpc* and iota-open-rpc (#5277)
* iota-json-rpc* and iota-open-rpc changes * refactor(protocol): pcr2 more base changes (#5448) * feat: implement conversions between v1 and v2 for system-state summary * refactor(types): add non_exhaustive attribute to IotaSystemStateSummary * refactor(core): use system-state summary v2 * doc(type): fix typo in system-state summary v2 doc * refactor(json-rpc): support new system state summary (#5449) * feat(json-rpc-api): support all versions of system-state summary * refactor(json-rpc): support v2 of system-state summary * chore(open-rpc): update specification --------- Co-authored-by: Konstantinos Demartinos <[email protected]>
1 parent ce760a4 commit 2e612dd

File tree

8 files changed

+361
-44
lines changed

8 files changed

+361
-44
lines changed

crates/iota-core/src/authority.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use iota_types::{
6868
TransactionEvents, VerifiedCertifiedTransactionEffects, VerifiedSignedTransactionEffects,
6969
},
7070
error::{ExecutionError, IotaError, IotaResult, UserInputError},
71-
event::{Event, EventID, SystemEpochInfoEvent, SystemEpochInfoEventV1, SystemEpochInfoEventV2},
71+
event::{Event, EventID, SystemEpochInfoEvent},
7272
executable_transaction::VerifiedExecutableTransaction,
7373
execution_config_utils::to_binary_config,
7474
execution_status::ExecutionStatus,
@@ -4714,9 +4714,9 @@ impl AuthorityState {
47144714
let system_epoch_info_event = temporary_store
47154715
.events
47164716
.data
4717-
.iter()
4717+
.into_iter()
47184718
.find(|event| event.is_system_epoch_info_event())
4719-
.map(|event| SystemEpochInfoEvent::from(event));
4719+
.map(SystemEpochInfoEvent::from);
47204720
// The system epoch info event can be `None` in case if the `advance_epoch`
47214721
// Move function call failed and was executed in the safe mode.
47224722
assert!(system_epoch_info_event.is_some() || system_obj.safe_mode());

crates/iota-core/src/authority/authority_store.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ use iota_types::{
1919
error::UserInputError,
2020
execution::TypeLayoutStore,
2121
fp_bail, fp_ensure,
22-
iota_system_state::get_iota_system_state,
22+
iota_system_state::{
23+
get_iota_system_state, iota_system_state_summary::IotaSystemStateSummaryV2,
24+
},
2325
message_envelope::Message,
2426
storage::{
2527
BackingPackageStore, MarkerValue, ObjectKey, ObjectOrTombstone, ObjectStore, get_module,
@@ -1592,10 +1594,11 @@ impl AuthorityStore {
15921594

15931595
// It is safe to call this function because we are in the middle of
15941596
// reconfiguration.
1595-
let system_state = self
1597+
let system_state: IotaSystemStateSummaryV2 = self
15961598
.get_iota_system_state_object_unsafe()
15971599
.expect("Reading iota system state object cannot fail")
1598-
.into_iota_system_state_summary();
1600+
.into_iota_system_state_summary()
1601+
.try_into()?;
15991602
let storage_fund_balance = system_state.storage_fund_total_object_storage_rebates;
16001603
info!(
16011604
"Total IOTA amount in the network: {}, storage fund balance: {}, total storage rebate: {} at beginning of epoch {}",

crates/iota-json-rpc-api/src/governance.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use iota_open_rpc_macros::open_rpc;
77
use iota_types::{
88
base_types::{IotaAddress, ObjectID},
99
iota_serde::BigInt,
10-
iota_system_state::iota_system_state_summary::IotaSystemStateSummary,
10+
iota_system_state::iota_system_state_summary::{
11+
IotaSystemStateSummaryV1, IotaSystemStateSummaryV2,
12+
},
1113
};
1214
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
1315

@@ -53,7 +55,13 @@ pub trait GovernanceReadApi {
5355

5456
/// Return the latest IOTA system state object on-chain.
5557
#[method(name = "getLatestIotaSystemState")]
56-
async fn get_latest_iota_system_state(&self) -> RpcResult<IotaSystemStateSummary>;
58+
async fn get_latest_iota_system_state(&self) -> RpcResult<IotaSystemStateSummaryV2>;
59+
60+
/// Return the latest IOTA system state object on-chain (version 1).
61+
/// Requires the `client-target-api-version` header to be set into
62+
/// a value `< 0.11` during requests.
63+
#[method(name = "getLatestIotaSystemState", version <= "0.10.99")]
64+
async fn get_latest_iota_system_state_v1(&self) -> RpcResult<IotaSystemStateSummaryV1>;
5765

5866
/// Return the reference gas price for the network
5967
#[method(name = "getReferenceGasPrice")]

crates/iota-json-rpc-types/src/iota_transaction.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ use iota_types::{
3535
signature::GenericSignature,
3636
storage::{DeleteKind, WriteKind},
3737
transaction::{
38-
Argument, CallArg, ChangeEpoch, Command, EndOfEpochTransactionKind, GenesisObject,
39-
InputObjectKind, ObjectArg, ProgrammableMoveCall, ProgrammableTransaction,
38+
Argument, CallArg, ChangeEpoch, ChangeEpochV2, Command, EndOfEpochTransactionKind,
39+
GenesisObject, InputObjectKind, ObjectArg, ProgrammableMoveCall, ProgrammableTransaction,
4040
SenderSignedData, TransactionData, TransactionDataAPI, TransactionKind,
4141
},
4242
};
@@ -517,6 +517,9 @@ impl IotaTransactionBlockKind {
517517
EndOfEpochTransactionKind::ChangeEpoch(e) => {
518518
IotaEndOfEpochTransactionKind::ChangeEpoch(e.into())
519519
}
520+
EndOfEpochTransactionKind::ChangeEpochV2(e) => {
521+
IotaEndOfEpochTransactionKind::ChangeEpochV2(e.into())
522+
}
520523
EndOfEpochTransactionKind::AuthenticatorStateCreate => {
521524
IotaEndOfEpochTransactionKind::AuthenticatorStateCreate
522525
}
@@ -603,6 +606,9 @@ impl IotaTransactionBlockKind {
603606
EndOfEpochTransactionKind::ChangeEpoch(e) => {
604607
IotaEndOfEpochTransactionKind::ChangeEpoch(e.into())
605608
}
609+
EndOfEpochTransactionKind::ChangeEpochV2(e) => {
610+
IotaEndOfEpochTransactionKind::ChangeEpochV2(e.into())
611+
}
606612
EndOfEpochTransactionKind::AuthenticatorStateCreate => {
607613
IotaEndOfEpochTransactionKind::AuthenticatorStateCreate
608614
}
@@ -679,6 +685,42 @@ impl From<ChangeEpoch> for IotaChangeEpoch {
679685
}
680686
}
681687

688+
#[serde_as]
689+
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
690+
pub struct IotaChangeEpochV2 {
691+
#[schemars(with = "BigInt<u64>")]
692+
#[serde_as(as = "BigInt<u64>")]
693+
pub epoch: EpochId,
694+
#[schemars(with = "BigInt<u64>")]
695+
#[serde_as(as = "BigInt<u64>")]
696+
pub storage_charge: u64,
697+
#[schemars(with = "BigInt<u64>")]
698+
#[serde_as(as = "BigInt<u64>")]
699+
pub computation_charge: u64,
700+
#[schemars(with = "BigInt<u64>")]
701+
#[serde_as(as = "BigInt<u64>")]
702+
pub computation_charge_burned: u64,
703+
#[schemars(with = "BigInt<u64>")]
704+
#[serde_as(as = "BigInt<u64>")]
705+
pub storage_rebate: u64,
706+
#[schemars(with = "BigInt<u64>")]
707+
#[serde_as(as = "BigInt<u64>")]
708+
pub epoch_start_timestamp_ms: u64,
709+
}
710+
711+
impl From<ChangeEpochV2> for IotaChangeEpochV2 {
712+
fn from(e: ChangeEpochV2) -> Self {
713+
Self {
714+
epoch: e.epoch,
715+
storage_charge: e.storage_charge,
716+
computation_charge: e.computation_charge,
717+
computation_charge_burned: e.computation_charge_burned,
718+
storage_rebate: e.storage_rebate,
719+
epoch_start_timestamp_ms: e.epoch_start_timestamp_ms,
720+
}
721+
}
722+
}
723+
682724
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, PartialEq, Eq)]
683725
#[enum_dispatch(IotaTransactionBlockEffectsAPI)]
684726
#[serde(
@@ -1035,10 +1077,12 @@ impl Display for IotaTransactionBlockEffects {
10351077
"Gas Cost Summary:\n \
10361078
Storage Cost: {} NANOS\n \
10371079
Computation Cost: {} NANOS\n \
1080+
Computation Cost Burned: {} NANOS\n \
10381081
Storage Rebate: {} NANOS\n \
10391082
Non-refundable Storage Fee: {} NANOS",
10401083
gas_cost_summary.storage_cost,
10411084
gas_cost_summary.computation_cost,
1085+
gas_cost_summary.computation_cost_burned,
10421086
gas_cost_summary.storage_rebate,
10431087
gas_cost_summary.non_refundable_storage_fee,
10441088
)]);
@@ -1622,6 +1666,7 @@ pub struct IotaEndOfEpochTransaction {
16221666
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
16231667
pub enum IotaEndOfEpochTransactionKind {
16241668
ChangeEpoch(IotaChangeEpoch),
1669+
ChangeEpochV2(IotaChangeEpochV2),
16251670
AuthenticatorStateCreate,
16261671
AuthenticatorStateExpire(IotaAuthenticatorStateExpire),
16271672
BridgeStateCreate(CheckpointDigest),

crates/iota-json-rpc/src/coin_api.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use iota_types::{
1818
coin::{CoinMetadata, TreasuryCap},
1919
effects::TransactionEffectsAPI,
2020
gas_coin::GAS,
21-
iota_system_state::IotaSystemStateTrait,
21+
iota_system_state::{
22+
IotaSystemStateTrait, iota_system_state_summary::IotaSystemStateSummaryV2,
23+
},
2224
object::Object,
2325
parse_iota_struct_tag,
2426
};
@@ -224,11 +226,12 @@ impl CoinReadApiServer for CoinReadApi {
224226
async move {
225227
let coin_struct = parse_to_struct_tag(&coin_type)?;
226228
Ok(if GAS::is_gas(&coin_struct) {
227-
let system_state_summary = self
228-
.internal
229-
.get_state()
230-
.get_system_state()?
231-
.into_iota_system_state_summary();
229+
let system_state_summary = IotaSystemStateSummaryV2::try_from(
230+
self.internal
231+
.get_state()
232+
.get_system_state()?
233+
.into_iota_system_state_summary(),
234+
)?;
232235
Supply {
233236
value: system_state_summary.iota_total_supply,
234237
}
@@ -1287,9 +1290,8 @@ mod tests {
12871290
id::UID,
12881291
iota_system_state::{
12891292
IotaSystemState,
1290-
iota_system_state_inner_v1::{
1291-
IotaSystemStateV1, StorageFundV1, SystemParametersV1, ValidatorSetV1,
1292-
},
1293+
iota_system_state_inner_v1::{StorageFundV1, SystemParametersV1, ValidatorSetV1},
1294+
iota_system_state_inner_v2::IotaSystemStateV2,
12931295
},
12941296
};
12951297
use mockall::predicate;
@@ -1305,7 +1307,7 @@ mod tests {
13051307
let mut state = default_system_state();
13061308
state.iota_treasury_cap.inner.total_supply.value = 42;
13071309

1308-
Ok(IotaSystemState::V1(state))
1310+
Ok(IotaSystemState::V2(state))
13091311
});
13101312

13111313
let coin_read_api = CoinReadApi::new_for_tests(Arc::new(mock_state), None);
@@ -1415,8 +1417,8 @@ mod tests {
14151417
expected.assert_eq(error_result.message());
14161418
}
14171419

1418-
fn default_system_state() -> IotaSystemStateV1 {
1419-
IotaSystemStateV1 {
1420+
fn default_system_state() -> IotaSystemStateV2 {
1421+
IotaSystemStateV2 {
14201422
epoch: Default::default(),
14211423
protocol_version: Default::default(),
14221424
system_state_version: Default::default(),
@@ -1464,7 +1466,8 @@ mod tests {
14641466
},
14651467
safe_mode: Default::default(),
14661468
safe_mode_storage_charges: iota_types::balance::Balance::new(Default::default()),
1467-
safe_mode_computation_rewards: iota_types::balance::Balance::new(Default::default()),
1469+
safe_mode_computation_charges: iota_types::balance::Balance::new(Default::default()),
1470+
safe_mode_computation_charges_burned: Default::default(),
14681471
safe_mode_storage_rebates: Default::default(),
14691472
safe_mode_non_refundable_storage_fee: Default::default(),
14701473
epoch_start_timestamp_ms: Default::default(),

crates/iota-json-rpc/src/governance_api.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use iota_types::{
2727
iota_serde::BigInt,
2828
iota_system_state::{
2929
IotaSystemState, IotaSystemStateTrait, PoolTokenExchangeRate, get_validator_from_table,
30-
iota_system_state_summary::IotaSystemStateSummary,
30+
iota_system_state_summary::{IotaSystemStateSummaryV1, IotaSystemStateSummaryV2},
3131
},
3232
object::{Object, ObjectRead},
3333
timelock::timelocked_staked_iota::TimelockedStakedIota,
@@ -198,7 +198,9 @@ impl GovernanceReadApi {
198198
);
199199

200200
let system_state = self.get_system_state()?;
201-
let system_state_summary = system_state.clone().into_iota_system_state_summary();
201+
let system_state_summary = IotaSystemStateSummaryV2::try_from(
202+
system_state.clone().into_iota_system_state_summary(),
203+
)?;
202204

203205
let rates = exchange_rates(&self.state, system_state_summary.epoch)
204206
.await?
@@ -264,8 +266,9 @@ impl GovernanceReadApi {
264266
);
265267

266268
let system_state = self.get_system_state()?;
267-
let system_state_summary: IotaSystemStateSummary =
268-
system_state.clone().into_iota_system_state_summary();
269+
let system_state_summary = IotaSystemStateSummaryV2::try_from(
270+
system_state.clone().into_iota_system_state_summary(),
271+
)?;
269272

270273
let rates = exchange_rates(&self.state, system_state_summary.epoch)
271274
.await?
@@ -400,13 +403,26 @@ impl GovernanceReadApiServer for GovernanceReadApi {
400403
}
401404

402405
#[instrument(skip(self))]
403-
async fn get_latest_iota_system_state(&self) -> RpcResult<IotaSystemStateSummary> {
406+
async fn get_latest_iota_system_state(&self) -> RpcResult<IotaSystemStateSummaryV2> {
404407
async move {
405408
Ok(self
406409
.state
407-
.get_system_state()
408-
.map_err(Error::from)?
409-
.into_iota_system_state_summary())
410+
.get_system_state()?
411+
.into_iota_system_state_summary()
412+
.try_into()?)
413+
}
414+
.trace()
415+
.await
416+
}
417+
418+
#[instrument(skip(self))]
419+
async fn get_latest_iota_system_state_v1(&self) -> RpcResult<IotaSystemStateSummaryV1> {
420+
async move {
421+
Ok(self
422+
.state
423+
.get_system_state()?
424+
.into_iota_system_state_summary()
425+
.try_into()?)
410426
}
411427
.trace()
412428
.await
@@ -425,8 +441,7 @@ impl GovernanceReadApiServer for GovernanceReadApi {
425441
#[instrument(skip(self))]
426442
async fn get_validators_apy(&self) -> RpcResult<ValidatorApys> {
427443
info!("get_validator_apy");
428-
let system_state_summary: IotaSystemStateSummary =
429-
self.get_latest_iota_system_state().await?;
444+
let system_state_summary = self.get_latest_iota_system_state().await?;
430445

431446
let exchange_rate_table = exchange_rates(&self.state, system_state_summary.epoch)
432447
.await
@@ -592,7 +607,9 @@ fn validator_exchange_rates(
592607
fn active_validators_exchange_rates(
593608
state: &Arc<dyn StateRead>,
594609
) -> RpcInterimResult<Vec<ValidatorExchangeRates>> {
595-
let system_state_summary = state.get_system_state()?.into_iota_system_state_summary();
610+
let system_state_summary = IotaSystemStateSummaryV2::try_from(
611+
state.get_system_state()?.into_iota_system_state_summary(),
612+
)?;
596613

597614
let tables = system_state_summary
598615
.active_validators
@@ -615,7 +632,9 @@ fn active_validators_exchange_rates(
615632
fn inactive_validators_exchange_rates(
616633
state: &Arc<dyn StateRead>,
617634
) -> RpcInterimResult<Vec<ValidatorExchangeRates>> {
618-
let system_state_summary = state.get_system_state()?.into_iota_system_state_summary();
635+
let system_state_summary = IotaSystemStateSummaryV2::try_from(
636+
state.get_system_state()?.into_iota_system_state_summary(),
637+
)?;
619638

620639
let tables = validator_summary_from_system_state(
621640
state,
@@ -664,7 +683,9 @@ fn pending_validators_exchange_rate(
664683
fn candidate_validators_exchange_rate(
665684
state: &Arc<dyn StateRead>,
666685
) -> RpcInterimResult<Vec<ValidatorExchangeRates>> {
667-
let system_state_summary = state.get_system_state()?.into_iota_system_state_summary();
686+
let system_state_summary = IotaSystemStateSummaryV2::try_from(
687+
state.get_system_state()?.into_iota_system_state_summary(),
688+
)?;
668689

669690
// From validator_candidates_id table get validator info using as key its
670691
// IotaAddress

0 commit comments

Comments
 (0)