diff --git a/Cargo.lock b/Cargo.lock index d38e343f2388..f6dd18461cd4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14307,7 +14307,7 @@ dependencies = [ [[package]] name = "sui-crypto" version = "0.4.0" -source = "git+https://github.com/MystenLabs/sui-rust-sdk.git?rev=cfae7c5966bf5d7b7a4af8db61ba6f3fed8e3c4e#cfae7c5966bf5d7b7a4af8db61ba6f3fed8e3c4e" +source = "git+https://github.com/MystenLabs/sui-rust-sdk.git?rev=8ec4d2f792a70660965ac2d12001c3d23a703b36#8ec4d2f792a70660965ac2d12001c3d23a703b36" dependencies = [ "ark-bn254", "ark-ff 0.4.2", @@ -16141,7 +16141,7 @@ dependencies = [ [[package]] name = "sui-rpc" version = "0.4.0" -source = "git+https://github.com/MystenLabs/sui-rust-sdk.git?rev=cfae7c5966bf5d7b7a4af8db61ba6f3fed8e3c4e#cfae7c5966bf5d7b7a4af8db61ba6f3fed8e3c4e" +source = "git+https://github.com/MystenLabs/sui-rust-sdk.git?rev=8ec4d2f792a70660965ac2d12001c3d23a703b36#8ec4d2f792a70660965ac2d12001c3d23a703b36" dependencies = [ "base64 0.22.1", "bcs", @@ -16326,7 +16326,7 @@ dependencies = [ [[package]] name = "sui-sdk-types" version = "0.4.0" -source = "git+https://github.com/MystenLabs/sui-rust-sdk.git?rev=cfae7c5966bf5d7b7a4af8db61ba6f3fed8e3c4e#cfae7c5966bf5d7b7a4af8db61ba6f3fed8e3c4e" +source = "git+https://github.com/MystenLabs/sui-rust-sdk.git?rev=8ec4d2f792a70660965ac2d12001c3d23a703b36#8ec4d2f792a70660965ac2d12001c3d23a703b36" dependencies = [ "base64ct", "bcs", diff --git a/Cargo.toml b/Cargo.toml index 144eea3d1ddc..6bf2ea27bed3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -727,9 +727,9 @@ anemo-cli = { git = "https://github.com/mystenlabs/anemo.git", rev = "845a9b5aab anemo-tower = { git = "https://github.com/mystenlabs/anemo.git", rev = "845a9b5aab1f8b78fb67c0e8e1998cb2e6a4d72e" } # core-types from the new sdk for use in gRPC -sui-sdk-types = { git = "https://github.com/MystenLabs/sui-rust-sdk.git", rev = "cfae7c5966bf5d7b7a4af8db61ba6f3fed8e3c4e", features = [ "hash", "serde" ] } -sui-crypto = { git = "https://github.com/MystenLabs/sui-rust-sdk.git", rev = "cfae7c5966bf5d7b7a4af8db61ba6f3fed8e3c4e", features = [ "ed25519", "secp256r1", "secp256k1", "passkey", "zklogin", "mldsa65" ] } -sui-rpc = { git = "https://github.com/MystenLabs/sui-rust-sdk.git", rev = "cfae7c5966bf5d7b7a4af8db61ba6f3fed8e3c4e", features = [ "unstable" ] } +sui-sdk-types = { git = "https://github.com/MystenLabs/sui-rust-sdk.git", rev = "8ec4d2f792a70660965ac2d12001c3d23a703b36", features = [ "hash", "serde" ] } +sui-crypto = { git = "https://github.com/MystenLabs/sui-rust-sdk.git", rev = "8ec4d2f792a70660965ac2d12001c3d23a703b36", features = [ "ed25519", "secp256r1", "secp256k1", "passkey", "zklogin", "mldsa65" ] } +sui-rpc = { git = "https://github.com/MystenLabs/sui-rust-sdk.git", rev = "8ec4d2f792a70660965ac2d12001c3d23a703b36", features = [ "unstable" ] } ### Workspace Members ### anemo-benchmark = { path = "crates/anemo-benchmark" } diff --git a/crates/sui-e2e-tests/tests/rpc/v2/ledger_service/get_epoch.rs b/crates/sui-e2e-tests/tests/rpc/v2/ledger_service/get_epoch.rs index a536de88cba2..966efd8d3fcc 100644 --- a/crates/sui-e2e-tests/tests/rpc/v2/ledger_service/get_epoch.rs +++ b/crates/sui-e2e-tests/tests/rpc/v2/ledger_service/get_epoch.rs @@ -7,9 +7,12 @@ use sui_macros::sim_test; use sui_protocol_config::ProtocolConfig; use sui_rpc::field::FieldMask; use sui_rpc::field::FieldMaskUtil; +use sui_rpc::proto::sui::rpc::v2::Epoch; +use sui_rpc::proto::sui::rpc::v2::GetCheckpointRequest; use sui_rpc::proto::sui::rpc::v2::GetEpochRequest; use sui_rpc::proto::sui::rpc::v2::ledger_service_client::LedgerServiceClient; use test_cluster::TestClusterBuilder; +use tonic::transport::Channel; #[sim_test] async fn get_epoch() { @@ -207,3 +210,84 @@ async fn get_epoch_protocol_config_exposes_gasless_allowlist() { serde_json::json!([["0xa::usdc::USDC", "10000"], ["0xb::usdt::USDT", "0"],]); assert_eq!(parsed, expected_json); } + +async fn read_epoch( + client: &mut LedgerServiceClient, + epoch: u64, + paths: &[&str], +) -> Epoch { + let request = + GetEpochRequest::new(epoch).with_read_mask(FieldMask::from_paths(paths.iter().copied())); + client + .get_epoch(request) + .await + .unwrap_or_else(|status| panic!("GetEpoch {epoch}: {status:?}")) + .into_inner() + .epoch + .unwrap() +} + +/// After an epoch change, epoch 0's end-of-epoch fields match its last checkpoint and epoch 1's +/// are unset. +#[sim_test] +async fn get_epoch_end_fields() { + let cluster = TestClusterBuilder::new() + .with_num_validators(1) + .disable_fullnode_pruning() + .build() + .await; + cluster.trigger_reconfiguration().await; + // Epoch 0's end and epoch 1's start are indexed from epoch 0's last checkpoint, which the + // fullnode executed before it entered epoch 1. + cluster.wait_for_rpc_index_ready().await; + let mut client = LedgerServiceClient::connect(cluster.rpc_url().to_owned()) + .await + .unwrap(); + + let end_mask = [ + "first_checkpoint", + "last_checkpoint", + "transaction_start_inclusive", + "transaction_end_exclusive", + "ended_in_safe_mode", + "system_epoch_info", + "epoch_commitments", + ]; + let epoch_0 = read_epoch(&mut client, 0, &end_mask).await; + let last_of_epoch_0 = epoch_0.last_checkpoint.unwrap(); + let summary = client + .get_checkpoint( + GetCheckpointRequest::by_sequence_number(last_of_epoch_0) + .with_read_mask(FieldMask::from_paths(["summary"])), + ) + .await + .unwrap() + .into_inner() + .checkpoint + .unwrap() + .summary + .unwrap(); + assert_eq!(epoch_0.transaction_start_inclusive, Some(0)); + assert_eq!( + epoch_0.transaction_end_exclusive, + summary.total_network_transactions + ); + assert_eq!(epoch_0.ended_in_safe_mode, Some(false)); + assert!(epoch_0.system_epoch_info.unwrap().total_stake.unwrap() > 0); + assert_eq!( + epoch_0.epoch_commitments, + summary.end_of_epoch_data.unwrap().epoch_commitments + ); + + let epoch_1 = read_epoch(&mut client, 1, &end_mask).await; + assert_eq!(epoch_1.first_checkpoint, Some(last_of_epoch_0 + 1)); + assert_eq!( + epoch_1.transaction_start_inclusive, + epoch_0.transaction_end_exclusive + ); + assert_eq!(epoch_1.last_checkpoint, None); + assert_eq!(epoch_1.transaction_end_exclusive, None); + assert_eq!(epoch_1.ended_in_safe_mode, None); + assert_eq!(epoch_1.system_epoch_info, None); + assert!(epoch_1.epoch_commitments.is_empty()); +} diff --git a/crates/sui-indexer-alt-e2e-tests/tests/get_epoch_parity_tests.rs b/crates/sui-indexer-alt-e2e-tests/tests/get_epoch_parity_tests.rs new file mode 100644 index 000000000000..25afc4868b8d --- /dev/null +++ b/crates/sui-indexer-alt-e2e-tests/tests/get_epoch_parity_tests.rs @@ -0,0 +1,238 @@ +// Copyright (c) Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +//! `LedgerService/GetEpoch` on a fullnode and on kv-rpc indexing the same chain. + +use std::path::Path; +use std::time::Duration; + +use prometheus::Registry; +use sui_indexer_alt_e2e_tests::OffchainCluster; +use sui_indexer_alt_e2e_tests::OffchainClusterConfig; +use sui_indexer_alt_framework::ingestion::ClientArgs; +use sui_indexer_alt_framework::ingestion::ingestion_client::IngestionClientArgs; +use sui_kv_rpc::KvRpcConfig; +use sui_rpc::field::FieldMask; +use sui_rpc::field::FieldMaskUtil; +use sui_rpc::proto::sui::rpc::v2::Epoch; +use sui_rpc::proto::sui::rpc::v2::GetCheckpointRequest; +use sui_rpc::proto::sui::rpc::v2::GetEpochRequest; +use sui_rpc::proto::sui::rpc::v2::ledger_service_client::LedgerServiceClient; +use sui_test_transaction_builder::make_staking_transaction; +use tempfile::TempDir; +use test_cluster::TestClusterBuilder; +use tonic::transport::Channel; + +/// Every `Epoch` field both servers read from their stored epoch rows. +const STORED_FIELDS: &[&str] = &[ + "epoch", + "first_checkpoint", + "last_checkpoint", + "start", + "end", + "reference_gas_price", + "protocol_config.protocol_version", + "committee", + "transaction_start_inclusive", + "transaction_end_exclusive", + "ended_in_safe_mode", + "system_epoch_info", + "epoch_commitments", +]; + +/// What a client polling the latest epoch's state reads. kv-rpc derives the running epoch's +/// `committee` from the `system_state` it reads when both are asked for. +const LATEST_STATE_FIELDS: &[&str] = &["epoch", "last_checkpoint", "committee", "system_state"]; + +async fn get_epoch(client: &mut LedgerServiceClient, epoch: u64, paths: &[&str]) -> Epoch { + let request = + GetEpochRequest::new(epoch).with_read_mask(FieldMask::from_paths(paths.iter().copied())); + client + .get_epoch(request) + .await + .unwrap_or_else(|status| panic!("GetEpoch {epoch} {paths:?}: {status:?}")) + .into_inner() + .epoch + .unwrap_or_else(|| panic!("GetEpoch {epoch} {paths:?} found no epoch")) +} + +async fn get_latest_epoch(client: &mut LedgerServiceClient, paths: &[&str]) -> Epoch { + let request = + GetEpochRequest::latest().with_read_mask(FieldMask::from_paths(paths.iter().copied())); + client + .get_epoch(request) + .await + .unwrap_or_else(|status| panic!("GetEpoch latest {paths:?}: {status:?}")) + .into_inner() + .epoch + .unwrap_or_else(|| panic!("GetEpoch latest {paths:?} found no epoch")) +} + +async fn latest_checkpoint(client: &mut LedgerServiceClient) -> u64 { + client + .get_checkpoint( + GetCheckpointRequest::latest() + .with_read_mask(FieldMask::from_paths(["sequence_number"])), + ) + .await + .unwrap() + .into_inner() + .checkpoint + .unwrap() + .sequence_number + .unwrap() +} + +/// Copy the fullnode's checkpoint files `*next..=up_to` into kv-rpc's ingestion directory, so +/// kv-rpc's served height stops at `up_to`. Each file is written under a temporary name and then +/// renamed, so the indexer never reads a partial file. +fn release_checkpoints(fullnode_dir: &Path, kv_rpc_dir: &Path, next: &mut u64, up_to: u64) { + while *next <= up_to { + let name = format!("{next}.binpb.zst"); + let partial = kv_rpc_dir.join(format!("{name}.partial")); + std::fs::copy(fullnode_dir.join(&name), &partial) + .unwrap_or_else(|e| panic!("copy checkpoint {next}: {e}")); + std::fs::rename(&partial, kv_rpc_dir.join(&name)).unwrap(); + *next += 1; + } +} + +/// A fullnode and kv-rpc serve the same stored and end-of-epoch fields and the same system state +/// for an ended epoch. A running epoch is not compared: the fullnode answers for it from its live +/// state and kv-rpc at its served height, so the two answers align only on a stopped chain once +/// both servers reach the same height. +/// +/// Around the end of epoch 0 at its last checkpoint L, kv-rpc's latest epoch is epoch 0 at its +/// state at L - 1, both while kv-rpc serves L - 1 and once it serves L, since the change-epoch +/// transaction in L has already written epoch 1's state. kv-rpc is held at L - 1, L and then +/// epoch 1's first checkpoint by releasing checkpoints to it one step at a time. +#[tokio::test] +async fn test_get_epoch_matches_between_fullnode_and_kv_rpc() { + let fullnode_dir = TempDir::new().unwrap(); + let kv_rpc_dir = TempDir::new().unwrap(); + let onchain = TestClusterBuilder::new() + .with_num_validators(1) + .with_data_ingestion_dir(fullnode_dir.path().to_path_buf()) + .with_epoch_duration_ms(300_000) + .disable_fullnode_pruning() + .build() + .await; + onchain.wait_for_rpc_index_ready().await; + let mut fullnode = LedgerServiceClient::connect(onchain.rpc_url().to_owned()) + .await + .unwrap(); + + let mut next_to_release = 0; + let genesis_tip = latest_checkpoint(&mut fullnode).await; + release_checkpoints( + fullnode_dir.path(), + kv_rpc_dir.path(), + &mut next_to_release, + genesis_tip, + ); + let offchain = OffchainCluster::new( + ClientArgs { + ingestion: IngestionClientArgs { + local_ingestion_path: Some(kv_rpc_dir.path().to_path_buf()), + ..Default::default() + }, + ..Default::default() + }, + OffchainClusterConfig { + kv_rpc_config: KvRpcConfig { + enable_object_at_checkpoint: Some(true), + ..Default::default() + }, + ..Default::default() + }, + &Registry::new(), + ) + .await + .unwrap(); + let mut kv_rpc = LedgerServiceClient::connect(offchain.kv_rpc_url().to_string()) + .await + .unwrap(); + + // Stake mid-epoch, so epoch 0's state at its end differs from its start snapshot. + let validator = onchain.swarm.config().validator_configs()[0].sui_address(); + let stake = make_staking_transaction(&onchain.wallet, validator).await; + onchain.wallet.execute_transaction_must_succeed(stake).await; + + onchain.trigger_reconfiguration().await; + // Epoch 0's end is indexed from its last checkpoint, which the fullnode executed, and wrote to + // its ingestion directory, before it entered epoch 1. + onchain.wait_for_rpc_index_ready().await; + let last_of_epoch_0 = get_epoch(&mut fullnode, 0, &["last_checkpoint"]) + .await + .last_checkpoint + .unwrap(); + + let mut serve_kv_rpc_through = async |checkpoint| { + release_checkpoints( + fullnode_dir.path(), + kv_rpc_dir.path(), + &mut next_to_release, + checkpoint, + ); + offchain + .wait_for_kv_rpc(checkpoint, Duration::from_secs(60)) + .await + .unwrap_or_else(|_| panic!("kv-rpc serves checkpoint {checkpoint}")); + }; + + // kv-rpc at L - 1: epoch 0 is running, at its state there. + serve_kv_rpc_through(last_of_epoch_0 - 1).await; + let before_end = get_latest_epoch(&mut kv_rpc, LATEST_STATE_FIELDS).await; + assert_eq!(before_end.epoch, Some(0)); + assert_eq!(before_end.last_checkpoint, None); + let state_before_end = before_end.system_state.expect("system_state"); + assert_eq!(state_before_end.epoch, Some(0)); + let start_snapshot = get_epoch(&mut fullnode, 0, &["system_state"]) + .await + .system_state + .expect("system_state"); + assert_ne!( + state_before_end, start_snapshot, + "the stake changes epoch 0's state after its start", + ); + let committee_0 = get_epoch(&mut fullnode, 0, &["committee"]).await.committee; + assert!(committee_0.is_some()); + assert_eq!(before_end.committee, committee_0); + + // kv-rpc at L: epoch 0 has ended, and its state is still the one at L - 1. + serve_kv_rpc_through(last_of_epoch_0).await; + let at_end = get_latest_epoch(&mut kv_rpc, LATEST_STATE_FIELDS).await; + assert_eq!(at_end.epoch, Some(0)); + assert_eq!(at_end.last_checkpoint, Some(last_of_epoch_0)); + assert_eq!(at_end.system_state.as_ref(), Some(&state_before_end)); + assert_eq!(at_end.committee, committee_0); + + // kv-rpc at epoch 1's first checkpoint: its latest epoch is epoch 1, at epoch 1's state, and + // epoch 0 is past and serves its start snapshot, as on the fullnode. + let first_of_epoch_1 = last_of_epoch_0 + 1; + tokio::time::timeout(Duration::from_secs(60), async { + while latest_checkpoint(&mut fullnode).await < first_of_epoch_1 { + tokio::time::sleep(Duration::from_millis(100)).await; + } + }) + .await + .expect("epoch 1 has a checkpoint"); + serve_kv_rpc_through(first_of_epoch_1).await; + let in_epoch_1 = get_latest_epoch(&mut kv_rpc, LATEST_STATE_FIELDS).await; + assert_eq!(in_epoch_1.epoch, Some(1)); + assert_eq!(in_epoch_1.last_checkpoint, None); + assert_eq!(in_epoch_1.system_state.unwrap().epoch, Some(1)); + assert_eq!(in_epoch_1.committee.unwrap().epoch, Some(1)); + + let served = get_epoch(&mut fullnode, 0, STORED_FIELDS).await; + // The comparison only means something if the ended epoch's end fields are set. + assert!(served.transaction_end_exclusive.is_some()); + assert!(served.system_epoch_info.is_some()); + assert_eq!(get_epoch(&mut kv_rpc, 0, STORED_FIELDS).await, served); + let ended_state = get_epoch(&mut fullnode, 0, &["system_state"]).await; + assert_eq!(ended_state.system_state.as_ref().unwrap().epoch, Some(0)); + assert_eq!( + get_epoch(&mut kv_rpc, 0, &["system_state"]).await, + ended_state + ); +} diff --git a/crates/sui-indexer-alt-e2e-tests/tests/kv_rpc_tests.rs b/crates/sui-indexer-alt-e2e-tests/tests/kv_rpc_tests.rs index fb011daddea0..6134f4ed45eb 100644 --- a/crates/sui-indexer-alt-e2e-tests/tests/kv_rpc_tests.rs +++ b/crates/sui-indexer-alt-e2e-tests/tests/kv_rpc_tests.rs @@ -7662,3 +7662,144 @@ async fn test_list_events_between_checkpoint_positions() { request.start_event_index = Some(1); expect_invalid_list_events(client, request).await; } + +/// A `GetEpoch` request for the latest epoch, reading `paths`. +fn latest_epoch_request(paths: &[&str]) -> sui_rpc::proto::sui::rpc::v2::GetEpochRequest { + sui_rpc::proto::sui::rpc::v2::GetEpochRequest::latest() + .with_read_mask(FieldMask::from_paths(paths.iter().copied())) +} + +/// kv-rpc serves the running epoch's `system_state` as of the served checkpoint rather than the +/// epoch-start snapshot: stake added mid-epoch shows in the validator's `next_epoch_stake`. +/// Simulacrum's genesis system state is V1, so the stake is also the first mutable access that +/// migrates it to V2 mid-epoch. +#[tokio::test] +async fn test_get_epoch_serves_the_running_epochs_system_state() { + /// The least `request_add_stake` accepts: 1 SUI. + const STAKE: u64 = 1_000_000_000; + + let mut cluster = list_api_cluster().await; + let (sender, kp, gas) = cluster.funded_account(10 * DEFAULT_GAS_BUDGET).unwrap(); + cluster.create_checkpoint().await; + let mut client = LedgerServiceClient::connect(cluster.kv_rpc_url().to_string()) + .await + .unwrap(); + let mask = ["system_state"]; + + let before = client + .get_epoch(latest_epoch_request(&mask)) + .await + .expect("GetEpoch") + .into_inner() + .epoch + .unwrap(); + let validator = before + .system_state + .as_ref() + .unwrap() + .validators + .as_ref() + .unwrap() + .active_validators[0] + .clone(); + let validator_address: SuiAddress = validator.address.as_deref().unwrap().parse().unwrap(); + + let mut builder = ProgrammableTransactionBuilder::new(); + let amount = builder.pure(STAKE).unwrap(); + let stake = builder.command(sui_types::transaction::Command::SplitCoins( + sui_types::transaction::Argument::GasCoin, + vec![amount], + )); + let system_state = builder + .obj(sui_types::transaction::ObjectArg::SUI_SYSTEM_MUT) + .unwrap(); + let validator_arg = builder.pure(validator_address).unwrap(); + builder.programmable_move_call( + sui_types::SUI_SYSTEM_PACKAGE_ID, + sui_types::sui_system_state::SUI_SYSTEM_MODULE_NAME.to_owned(), + sui_types::governance::ADD_STAKE_FUN_NAME.to_owned(), + vec![], + vec![system_state, stake, validator_arg], + ); + let data = TransactionData::new_programmable( + sender, + vec![gas], + builder.finish(), + DEFAULT_GAS_BUDGET, + cluster.reference_gas_price(), + ); + let (_, err) = cluster + .execute_transaction(Transaction::from_data_and_signer(data, vec![&kp])) + .expect("add stake"); + assert!(err.is_none(), "add stake failed: {err:?}"); + cluster.create_checkpoint().await; + + let after = client + .get_epoch(latest_epoch_request(&mask)) + .await + .expect("GetEpoch") + .into_inner() + .epoch + .unwrap(); + let validator_after = after + .system_state + .as_ref() + .unwrap() + .validators + .as_ref() + .unwrap() + .active_validators + .iter() + .find(|v| v.address == validator.address) + .expect("validator still active"); + assert_eq!( + validator_after.next_epoch_stake, + Some(validator.next_epoch_stake.unwrap() + STAKE), + ); +} + +/// Without `at_checkpoint` object reads (off by default), the running epoch's `system_state` is +/// `UNIMPLEMENTED` / `NOT_SERVED_HERE` with cause `disabled_by_config`. Its `committee` and the +/// default read mask come from stored rows and are still served. +#[tokio::test] +async fn test_get_epoch_running_system_state_needs_object_at_checkpoint_reads() { + let mut cluster = FullCluster::new().await.expect("Failed to create cluster"); + cluster.advance_epoch(); + cluster.create_checkpoint().await; + let mut client = LedgerServiceClient::connect(cluster.kv_rpc_url().to_string()) + .await + .unwrap(); + + let status = client + .get_epoch(latest_epoch_request(&["system_state"])) + .await + .expect_err("the running epoch's system_state needs at_checkpoint reads"); + assert_eq!(status.code(), tonic::Code::Unimplemented, "{status:?}"); + let error_info = sui_rpc::proto::google::rpc::Status::decode(status.details()) + .unwrap() + .details + .iter() + .find_map(|detail| detail.to_msg::().ok()) + .unwrap_or_else(|| panic!("no ErrorInfo in {status:?}")); + assert_eq!(error_info.reason, ErrorReason::NotServedHere.as_str_name()); + assert_eq!(error_info.domain, ErrorReason::DOMAIN); + assert_eq!( + error_info.metadata, + [("cause".to_owned(), "disabled_by_config".to_owned())].into(), + "{status:?}" + ); + + let running = client + .get_epoch(latest_epoch_request(&["epoch", "committee"])) + .await + .expect("committee is served without at_checkpoint reads") + .into_inner() + .epoch + .unwrap(); + assert_eq!(running.epoch, Some(1)); + assert!(!running.committee.unwrap().members.is_empty()); + client + .get_epoch(sui_rpc::proto::sui::rpc::v2::GetEpochRequest::latest()) + .await + .expect("the default read mask is served without at_checkpoint reads"); +} diff --git a/crates/sui-kv-rpc/src/bigtable_client.rs b/crates/sui-kv-rpc/src/bigtable_client.rs index 0fe7dff3f064..2462128b0f23 100644 --- a/crates/sui-kv-rpc/src/bigtable_client.rs +++ b/crates/sui-kv-rpc/src/bigtable_client.rs @@ -52,6 +52,7 @@ use sui_inverted_index::eval_bitmap_query_stream; use sui_kvstore::BigTableBitmapSource; use sui_kvstore::BitmapIndexSpec; use sui_kvstore::CheckpointData; +use sui_kvstore::EpochData; use sui_kvstore::KeyValueStoreReader; use sui_kvstore::ObjectStatus; use sui_kvstore::RowFilter; @@ -65,6 +66,7 @@ use sui_rpc_api::ledger_history::query_options::IntraTxCoordinate; use sui_rpc_api::ledger_history::row_placement::RowPlacement; use sui_types::base_types::ObjectID; use sui_types::base_types::SequenceNumber; +use sui_types::committee::EpochId; use sui_types::digests::TransactionDigest; use sui_types::messages_checkpoint::CheckpointSequenceNumber; use sui_types::object::Object; @@ -81,11 +83,12 @@ pub(crate) mod stage { pub const TX_SEQ_DIGEST: &str = "tx_seq_digest"; pub const TX_CP_RESOLVE: &str = "tx_cp_resolve"; pub const CHECKPOINTS: &str = "checkpoints"; + pub const EPOCHS: &str = "epochs"; } /// Metrics for the request-scoped `BigTableClient` limiter and the reads it -/// gates. Cardinality is bounded: per method, 6 stages for `permit_wait_ms`, -/// `permit_hold_ms`, and `reads_started`, 6 stages × 3 outcomes for +/// gates. Cardinality is bounded: per method, 7 stages for `permit_wait_ms`, +/// `permit_hold_ms`, and `reads_started`, 7 stages × 3 outcomes for /// `reads_finished`, and one series each for the request-end `permits_peak` /// and `ops_total` histograms. #[derive(Clone)] @@ -419,6 +422,18 @@ impl BigTableClient { .await } + /// The `epochs` rows for `epoch_ids`, reading only `columns` (every column if `None`). One + /// multi-get, under one permit. + pub(crate) async fn get_epochs_filtered( + &self, + epoch_ids: &[EpochId], + columns: Option<&[&str]>, + ) -> Result, RpcError> { + let mut inner = self.inner.clone(); + self.call_gated(stage::EPOCHS, inner.get_epochs_filtered(epoch_ids, columns)) + .await + } + /// Stream the checkpoint of each of `tx_sequence_numbers`, reading only the /// checkpoint-number column of `tx_seq_digest`. The permit is held for the /// stream's lifetime (see `gate_stream`); rows arrive in BigTable order. diff --git a/crates/sui-kv-rpc/src/config.rs b/crates/sui-kv-rpc/src/config.rs index 74683975fc8c..2a6df29abaac 100644 --- a/crates/sui-kv-rpc/src/config.rs +++ b/crates/sui-kv-rpc/src/config.rs @@ -670,11 +670,11 @@ pub struct KvRpcConfig { pub watermark_pipeline: Option>, /// Serve `GetObject` and `BatchGetObjects` requests that set `at_checkpoint` or - /// `max_version`, and every `ListObjectVersions` request (bounded by `at_checkpoint`, or by - /// the served height when it is unset), and fold the alpha - /// `kvstore_object_versions_by_checkpoint` and `kvstore_object_tombstones` pipelines into the - /// `GetServiceInfo` checkpoint height. Defaults to `false`, which rejects those requests with - /// `Unimplemented` (`NOT_SERVED_HERE`, cause `disabled_by_config`). + /// `max_version`, every `ListObjectVersions` request (bounded by `at_checkpoint`, or by the + /// served height when it is unset), and the running epoch's `GetEpoch.system_state`, and fold + /// the alpha `kvstore_object_versions_by_checkpoint` and `kvstore_object_tombstones` pipelines + /// into the `GetServiceInfo` checkpoint height. Defaults to `false`, which rejects those + /// requests with `Unimplemented` (`NOT_SERVED_HERE`, cause `disabled_by_config`). /// /// Enable this only after both pipelines have been backfilled from genesis. The versions /// index has no restore floor, so an object unchanged since before the pipeline's first diff --git a/crates/sui-kv-rpc/src/lib.rs b/crates/sui-kv-rpc/src/lib.rs index ae5183a0c59c..6abbd8621be7 100644 --- a/crates/sui-kv-rpc/src/lib.rs +++ b/crates/sui-kv-rpc/src/lib.rs @@ -298,8 +298,9 @@ pub struct KvRpcServer { pub(crate) request_bigtable_concurrency: usize, pub(crate) monotonic_read_wait_timeout: Duration, pub(crate) stages: StagesConfig, - /// `at_checkpoint` and `max_version` object reads, and every `ListObjectVersions` - /// request (bounded by `at_checkpoint` or the served height), need the alpha + /// `at_checkpoint` and `max_version` object reads, which also serve the running epoch's + /// `GetEpoch.system_state`, and every `ListObjectVersions` request (bounded by + /// `at_checkpoint` or the served height), need the alpha /// [`OBJECT_VERSIONS_BY_CHECKPOINT_PIPELINE`] and [`OBJECT_TOMBSTONES_PIPELINE`], /// backfilled from genesis (see [`KvRpcConfig::enable_object_at_checkpoint`]). /// Instances without them reject those requests with `Unimplemented` (`NOT_SERVED_HERE`, diff --git a/crates/sui-kv-rpc/src/v2/get_epoch.rs b/crates/sui-kv-rpc/src/v2/get_epoch.rs index f977af4853a0..0f5123fec260 100644 --- a/crates/sui-kv-rpc/src/v2/get_epoch.rs +++ b/crates/sui-kv-rpc/src/v2/get_epoch.rs @@ -1,7 +1,9 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -use sui_kvstore::BigTableClient; +use futures::TryStreamExt; +use sui_kvstore::EpochData; +use sui_kvstore::ObjectStatus; use sui_kvstore::tables::epochs::col; use sui_protocol_config::{Chain, ProtocolConfig}; use sui_rpc::field::{FieldMask, FieldMaskTree, FieldMaskUtil}; @@ -9,13 +11,24 @@ use sui_rpc::merge::Merge; use sui_rpc::proto::sui::rpc::v2::Epoch; use sui_rpc::proto::sui::rpc::v2::ProtocolConfig as RpcProtocolConfig; use sui_rpc::proto::sui::rpc::v2::{GetEpochRequest, GetEpochResponse}; +use sui_rpc_api::grpc::v2::ledger_service::set_epoch_end_fields; use sui_rpc_api::{ - ErrorReason, + ErrorReason, NotServedHereCause, RpcError, grpc::v2::protocol_config_to_proto, + not_served_here, proto::{google::rpc::bad_request::FieldViolation, timestamp_ms_to_proto}, }; -use sui_types::sui_system_state::SuiSystemStateTrait; +use sui_types::SUI_SYSTEM_STATE_OBJECT_ID; +use sui_types::base_types::ObjectID; +use sui_types::messages_checkpoint::CheckpointCommitment; +use sui_types::object::Object; +use sui_types::storage::{EpochInfo, ObjectKey}; +use sui_types::sui_system_state::{ + SuiSystemState, SuiSystemStateTrait, get_sui_system_state, get_sui_system_state_wrapper, + sui_system_state_inner_id, +}; +use crate::bigtable_client::BigTableClient; use crate::served_height::ServedHeight; pub const READ_MASK_DEFAULT: &str = sui_rpc_api::read_mask_defaults::EPOCH; @@ -23,8 +36,16 @@ pub const READ_MASK_DEFAULT: &str = sui_rpc_api::read_mask_defaults::EPOCH; /// Serve the epoch the request names, or the latest one, as of `served_height`: an epoch that /// starts above it is not found, and an epoch that ends above it has not ended yet, so its end /// fields are left out even when the indexer has already written them. +/// +/// `system_state` is the epoch-start snapshot for an epoch below the served one. For the served +/// epoch it is the state at the served checkpoint, read from the object versions index when +/// `object_at_checkpoint_enabled`, or at the checkpoint before it when the served checkpoint is the +/// epoch's last: the change-epoch transaction there has already turned the state into the next +/// epoch's. `committee` comes from the same state when `system_state` is read too, and from the +/// epoch-start snapshot otherwise; the committee does not change within an epoch. pub async fn get_epoch( - mut client: BigTableClient, + client: BigTableClient, + object_at_checkpoint_enabled: bool, served_height: ServedHeight, request: GetEpochRequest, chain: Chain, @@ -43,16 +64,35 @@ pub async fn get_epoch( let mut message = Epoch::default(); - let columns = epoch_columns(&read_mask); let epoch = request.epoch.unwrap_or(served_height.epoch); if epoch > served_height.epoch { return Ok(GetEpochResponse::default()); } - let Some(mut epoch_info) = client - .get_epochs_filtered(&[epoch], Some(&columns)) - .await? - .pop() - else { + // The served epoch's `system_state` is read at the served height, and `committee` comes from + // it, so the first read fetches the snapshot only for an epoch below the served one, or for + // `committee` without `system_state`. + let reads_state_at_served_height = + epoch == served_height.epoch && read_mask.contains(Epoch::SYSTEM_STATE_FIELD.name); + let columns = epoch_columns(&read_mask, !reads_state_at_served_height); + let epoch_future = async { client.get_epochs_filtered(&[epoch], Some(&columns)).await }; + let transaction_start_future = async { + if !read_mask.contains(Epoch::TRANSACTION_START_INCLUSIVE_FIELD.name) { + return Ok(None); + } + let Some(previous) = epoch.checked_sub(1) else { + return Ok(Some(0)); + }; + // The previous epoch ended below this epoch's first checkpoint, so at or below the served + // height. + let previous = client + .get_epochs_filtered(&[previous], Some(&[col::EPOCH, col::TX_HI])) + .await? + .pop(); + Ok::<_, RpcError>(previous.and_then(|previous| previous.tx_hi)) + }; + let (mut epochs, transaction_start_inclusive) = + tokio::try_join!(epoch_future, transaction_start_future)?; + let Some(mut epoch_info) = epochs.pop() else { return Ok(GetEpochResponse::default()); }; if epoch_info @@ -62,7 +102,6 @@ pub async fn get_epoch( epoch_info.end_checkpoint = None; epoch_info.end_timestamp_ms = None; } - if read_mask.contains(Epoch::EPOCH_FIELD.name) { message.epoch = epoch_info.epoch; } @@ -90,8 +129,23 @@ pub async fn get_epoch( RpcProtocolConfig::merge_from(protocol_config_to_proto(config), &submask) }); } + if epoch_info.end_checkpoint.is_some() { + set_epoch_end_fields(&mut message, &read_mask, &epoch_end(&epoch_info)?); + } + message.transaction_start_inclusive = transaction_start_inclusive; + let system_state = if reads_state_at_served_height { + // Epoch 0's last checkpoint is never the genesis checkpoint. + let checkpoint = if epoch_info.end_checkpoint == Some(served_height.checkpoint) { + served_height.checkpoint - 1 + } else { + served_height.checkpoint + }; + Some(live_system_state(&client, object_at_checkpoint_enabled, checkpoint).await?) + } else { + epoch_info.system_state.take() + }; if read_mask.contains(Epoch::COMMITTEE_FIELD.name) { - message.committee = epoch_info.system_state.as_ref().map(|system_state| { + message.committee = system_state.as_ref().map(|system_state| { system_state .get_current_epoch_committee() .committee() @@ -100,24 +154,145 @@ pub async fn get_epoch( }); } if read_mask.contains(Epoch::SYSTEM_STATE_FIELD.name) { - message.system_state = epoch_info - .system_state - .map(|system_state| Box::new(system_state.into())); + message.system_state = system_state.map(|system_state| Box::new(system_state.into())); } Ok(GetEpochResponse::new(message)) } -/// Compute the set of BigTable columns needed for the given read mask. -/// Always includes `ep` (epoch number, small). -fn epoch_columns(mask: &FieldMaskTree) -> Vec<&'static str> { - let mut columns = vec![col::EPOCH]; +/// The end-of-epoch columns of `epoch_info`, decoded into the shape `set_epoch_end_fields` reads. +fn epoch_end(epoch_info: &EpochData) -> Result { + let epoch_commitments = epoch_info + .epoch_commitments + .as_deref() + .map(bcs::from_bytes::>) + .transpose() + .map_err(|e| { + RpcError::new( + tonic::Code::Internal, + format!("decode epoch commitments: {e}"), + ) + })?; + Ok(EpochInfo { + tx_hi: epoch_info.tx_hi, + safe_mode: epoch_info.safe_mode, + epoch_commitments, + total_stake: epoch_info.total_stake, + storage_fund_balance: epoch_info.storage_fund_balance, + storage_fund_reinvestment: epoch_info.storage_fund_reinvestment, + storage_charge: epoch_info.storage_charge, + storage_rebate: epoch_info.storage_rebate, + stake_subsidy_amount: epoch_info.stake_subsidy_amount, + total_gas_fees: epoch_info.total_gas_fees, + total_stake_rewards_distributed: epoch_info.total_stake_rewards_distributed, + leftover_storage_fund_inflow: epoch_info.leftover_storage_fund_inflow, + ..EpochInfo::default() + }) +} + +/// The system state at the end of `checkpoint`: the `0x5` wrapper and the inner object its +/// `version` keys, each read at that checkpoint. The version is read too, not taken from the +/// epoch-start snapshot, because the first mutable access to the system state after genesis or a +/// framework upgrade migrates the inner object to a new version mid-epoch. `Unimplemented` / +/// `NOT_SERVED_HERE` (cause `disabled_by_config`) unless `object_at_checkpoint_enabled`. +async fn live_system_state( + client: &BigTableClient, + object_at_checkpoint_enabled: bool, + checkpoint: u64, +) -> Result { + if !object_at_checkpoint_enabled { + return Err(not_served_here( + NotServedHereCause::DisabledByConfig, + "the running epoch's system_state needs at_checkpoint object reads, which are not \ + enabled on this instance", + )); + } + let internal = |message: String| RpcError::new(tonic::Code::Internal, message); + let mut objects = + vec![object_at_checkpoint(client, SUI_SYSTEM_STATE_OBJECT_ID, checkpoint).await?]; + let version = get_sui_system_state_wrapper(&objects.as_slice()) + .map_err(|e| internal(format!("decode system state wrapper: {e}")))? + .version; + objects + .push(object_at_checkpoint(client, sui_system_state_inner_id(version), checkpoint).await?); + get_sui_system_state(&objects.as_slice()).map_err(|e| { + internal(format!( + "decode system state at checkpoint {checkpoint}: {e}" + )) + }) +} + +/// The version of `id` live at the end of `checkpoint`, from the version index (a single-row +/// forward read), read from the `objects` table. `Internal` if it is not live there: callers read +/// only objects that always exist. +async fn object_at_checkpoint( + client: &BigTableClient, + id: ObjectID, + checkpoint: u64, +) -> Result { + let internal = |message: String| RpcError::new(tonic::Code::Internal, message); + let version = match client + .get_object_version_at_checkpoint(id, checkpoint) + .await? + { + Some((version, ObjectStatus::Live)) => version, + other => { + return Err(internal(format!( + "object {id} is not live at checkpoint {checkpoint}: {other:?}" + ))); + } + }; + // The read runs to its end, so it finishes as completed rather than cut off. + let rows: Vec = client + .get_objects_stream(vec![ObjectKey(id, version)]) + .await? + .try_collect() + .await?; + rows.into_iter().next().ok_or_else(|| { + internal(format!( + "object {id} version {version} is not in the objects table" + )) + }) +} + +/// Compute the set of BigTable columns needed for the given read mask. Always includes `ep` and +/// `ec`: the end checkpoint decides whether the epoch has ended at the served height, which every +/// end field and `system_state` depend on. The epoch-start snapshot is read for `committee` or +/// `system_state` only if `reads_snapshot`. +fn epoch_columns(mask: &FieldMaskTree, reads_snapshot: bool) -> Vec<&'static str> { + let mut columns = vec![col::EPOCH, col::END_CHECKPOINT]; if mask.contains(Epoch::FIRST_CHECKPOINT_FIELD.name) { columns.push(col::START_CHECKPOINT); } - // `end_checkpoint` also decides whether the end timestamp is served yet. - if mask.contains(Epoch::LAST_CHECKPOINT_FIELD.name) || mask.contains(Epoch::END_FIELD.name) { - columns.push(col::END_CHECKPOINT); + let end_fields = [ + ( + Epoch::TRANSACTION_END_EXCLUSIVE_FIELD.name, + &[col::TX_HI][..], + ), + (Epoch::ENDED_IN_SAFE_MODE_FIELD.name, &[col::SAFE_MODE][..]), + ( + Epoch::SYSTEM_EPOCH_INFO_FIELD.name, + &[ + col::TOTAL_STAKE, + col::STORAGE_FUND_REINVESTMENT, + col::STORAGE_CHARGE, + col::STORAGE_REBATE, + col::STORAGE_FUND_BALANCE, + col::STAKE_SUBSIDY_AMOUNT, + col::TOTAL_GAS_FEES, + col::TOTAL_STAKE_REWARDS_DISTRIBUTED, + col::LEFTOVER_STORAGE_FUND_INFLOW, + ][..], + ), + ( + Epoch::EPOCH_COMMITMENTS_FIELD.name, + &[col::EPOCH_COMMITMENTS][..], + ), + ]; + for (field, field_columns) in end_fields { + if mask.contains(field) { + columns.extend_from_slice(field_columns); + } } if mask.contains(Epoch::START_FIELD.name) { columns.push(col::START_TIMESTAMP); @@ -131,7 +306,10 @@ fn epoch_columns(mask: &FieldMaskTree) -> Vec<&'static str> { if mask.subtree(Epoch::PROTOCOL_CONFIG_FIELD.name).is_some() { columns.push(col::PROTOCOL_VERSION); } - if mask.contains(Epoch::COMMITTEE_FIELD.name) || mask.contains(Epoch::SYSTEM_STATE_FIELD.name) { + if reads_snapshot + && (mask.contains(Epoch::COMMITTEE_FIELD.name) + || mask.contains(Epoch::SYSTEM_STATE_FIELD.name)) + { columns.push(col::SYSTEM_STATE); } @@ -141,8 +319,11 @@ fn epoch_columns(mask: &FieldMaskTree) -> Vec<&'static str> { #[cfg(test)] mod tests { use bytes::Bytes; - use sui_kvstore::tables::epochs; + use sui_kvstore::BigTableClient as KvStoreClient; + use sui_kvstore::tables::{epochs, object_versions_by_checkpoint, objects}; use sui_kvstore::testing::MockBigtableServer; + use sui_types::base_types::SequenceNumber; + use sui_types::sui_system_state::mock; use super::*; @@ -152,27 +333,219 @@ mod tests { const BOUNDS_MASK: &[&str] = &["epoch", "first_checkpoint", "last_checkpoint", "end"]; - async fn get( - client: &BigTableClient, + async fn try_get( + client: &KvStoreClient, + object_at_checkpoint_enabled: bool, served_height: ServedHeight, epoch: Option, paths: &[&str], - ) -> Epoch { + ) -> sui_rpc_api::Result { let mut request = GetEpochRequest::default(); request.epoch = epoch; request.read_mask = Some(FieldMask::from_paths(paths.iter().copied())); - get_epoch(client.clone(), served_height, request, Chain::Unknown) + let client = BigTableClient::new( + client.clone(), + 8, + crate::bigtable_client::Metrics::for_testing().0, + "GetEpoch", + ); + get_epoch( + client, + object_at_checkpoint_enabled, + served_height, + request, + Chain::Unknown, + ) + .await + .map(|response| response.epoch.unwrap_or_default()) + } + + async fn get( + client: &KvStoreClient, + served_height: ServedHeight, + epoch: Option, + paths: &[&str], + ) -> Epoch { + try_get(client, false, served_height, epoch, paths) .await .expect("get_epoch succeeds") - .epoch - .unwrap_or_default() + } + + fn v1(reference_gas_price: u64) -> SuiSystemState { + let mut inner = mock::sui_system_state_inner_v1(); + inner.reference_gas_price = reference_gas_price; + SuiSystemState::V1(inner) + } + + fn v2(reference_gas_price: u64) -> SuiSystemState { + let mut inner = mock::sui_system_state_inner_v2(); + inner.reference_gas_price = reference_gas_price; + SuiSystemState::V2(inner) + } + + /// Record `id` at `version` with `status` in the version index, from `checkpoint`. + async fn insert_version( + mock: &MockBigtableServer, + id: ObjectID, + version: SequenceNumber, + status: ObjectStatus, + checkpoint: u64, + ) { + mock.insert_row( + object_versions_by_checkpoint::NAME, + object_versions_by_checkpoint::encode_key(&id, checkpoint), + object_versions_by_checkpoint::encode(version, status), + ) + .await; + } + + /// Write `state`'s `0x5` wrapper and inner object, both live from `checkpoint` at an object + /// version equal to it. + async fn insert_system_state( + mock: &MockBigtableServer, + state: SuiSystemState, + checkpoint: u64, + ) { + let version = SequenceNumber::from_u64(checkpoint); + for mut object in mock::system_state_output_objects(state) { + object + .data + .try_as_move_mut() + .expect("move object") + .increment_version_to(version); + mock.insert_row( + objects::NAME, + objects::encode_key(&ObjectKey(object.id(), version)), + objects::encode(&object).unwrap(), + ) + .await; + insert_version(mock, object.id(), version, ObjectStatus::Live, checkpoint).await; + } + } + + /// The served epoch's `system_state` is the state at the served checkpoint, read through the + /// version index, never a version written above it, and following the inner object when it + /// migrates to a new `system_state_version` mid-epoch. At the epoch's last checkpoint it is the + /// state at the checkpoint before, since the change-epoch transaction there has already + /// written the next epoch's state. A past epoch's is its epoch-start snapshot. Without + /// checkpoint-pinned reads the served epoch's `system_state` is `Unimplemented` / + /// `NOT_SERVED_HERE` with cause `disabled_by_config`. + #[tokio::test] + async fn served_epoch_system_state_is_read_at_the_served_checkpoint() { + let mock = MockBigtableServer::new(); + let (addr, _server) = mock.start().await.expect("start mock BigTable"); + let client = KvStoreClient::new_local(addr.to_string(), "test".to_string()) + .await + .expect("connect to mock BigTable"); + // Epoch 0 starts on a V1 inner object with reference gas price 1 and ends at checkpoint + // 15. The system state changes at checkpoint 3, and at checkpoint 12 it migrates to V2: + // the V1 inner object is deleted and a V2 one created under the new version's key. The + // change-epoch transaction at checkpoint 15 writes epoch 1's state. + mock.insert_row( + epochs::NAME, + epochs::encode_key(0), + [ + u64_cell(col::EPOCH, 0), + u64_cell(col::START_CHECKPOINT, 0), + u64_cell(col::END_CHECKPOINT, 15), + ( + col::SYSTEM_STATE, + Bytes::from(bcs::to_bytes(&v1(1)).unwrap()), + ), + ], + ) + .await; + insert_system_state(&mock, v1(100), 3).await; + insert_system_state(&mock, v2(200), 12).await; + insert_version( + &mock, + sui_system_state_inner_id(1), + SequenceNumber::from_u64(12), + ObjectStatus::Deleted, + 12, + ) + .await; + insert_system_state(&mock, v2(300), 15).await; + + let at = |checkpoint| ServedHeight { + checkpoint, + epoch: 0, + }; + let reference_gas_price = |epoch: Epoch| { + epoch + .system_state + .expect("system_state") + .reference_gas_price + }; + + for (checkpoint, expected) in [(10, 100), (12, 200), (14, 200), (15, 200)] { + let epoch = try_get(&client, true, at(checkpoint), None, &["system_state"]) + .await + .expect("get_epoch succeeds"); + assert_eq!( + reference_gas_price(epoch), + Some(expected), + "served at checkpoint {checkpoint}", + ); + } + + // Once epoch 1 is served, epoch 0 is past: its snapshot, without touching the index, and + // without checkpoint-pinned reads. + mock.clear_read_rows_calls().await; + let inside_epoch_1 = ServedHeight { + checkpoint: 20, + epoch: 1, + }; + let past = try_get(&client, true, inside_epoch_1, Some(0), &["system_state"]) + .await + .expect("get_epoch succeeds"); + assert_eq!(reference_gas_price(past), Some(1)); + assert!( + mock.read_rows_calls() + .await + .iter() + .all(|call| call.table != object_versions_by_checkpoint::NAME), + "a past epoch's system_state reads no object versions", + ); + let past = get(&client, inside_epoch_1, Some(0), &["system_state"]).await; + assert_eq!(reference_gas_price(past), Some(1)); + + for checkpoint in [10, 15] { + let status = try_get(&client, false, at(checkpoint), None, &["system_state"]) + .await + .expect_err("system_state needs checkpoint-pinned reads"); + let status = tonic::Status::from(status); + assert_eq!( + status.code(), + tonic::Code::Unimplemented, + "served at checkpoint {checkpoint}", + ); + let error_info = + ::decode(status.details()) + .expect("error details decode as google.rpc.Status") + .details + .iter() + .find_map(|detail| { + detail + .to_msg::() + .ok() + }) + .unwrap_or_else(|| panic!("no ErrorInfo in {status:?}")); + assert_eq!(error_info.reason, ErrorReason::NotServedHere.as_str_name()); + assert_eq!(error_info.domain, ErrorReason::DOMAIN); + assert_eq!( + error_info.metadata, + [("cause".to_owned(), "disabled_by_config".to_owned())].into(), + "served at checkpoint {checkpoint}", + ); + } } #[tokio::test] async fn epochs_are_served_as_of_the_served_height() { let mock = MockBigtableServer::new(); let (addr, _server) = mock.start().await.expect("start mock BigTable"); - let client = BigTableClient::new_local(addr.to_string(), "test".to_string()) + let client = KvStoreClient::new_local(addr.to_string(), "test".to_string()) .await .expect("connect to mock BigTable"); // Epoch 0 covers checkpoints 0..=9 and its end row is written; epoch 1 starts at 10. @@ -235,4 +608,115 @@ mod tests { assert_eq!(latest.epoch, Some(1)); assert_eq!(latest.first_checkpoint, Some(10)); } + + const END_MASK: &[&str] = &[ + "transaction_start_inclusive", + "transaction_end_exclusive", + "ended_in_safe_mode", + "system_epoch_info", + "epoch_commitments", + ]; + + /// The end-of-epoch fields appear once the epoch has ended at or below the served height, + /// with the counters unset for an epoch that ended in safe mode. The first transaction comes + /// from the previous epoch's end and is served as soon as the epoch is. + #[tokio::test] + async fn end_fields_are_served_once_the_epoch_has_ended_at_the_served_height() { + let mock = MockBigtableServer::new(); + let (addr, _server) = mock.start().await.expect("start mock BigTable"); + let client = KvStoreClient::new_local(addr.to_string(), "test".to_string()) + .await + .expect("connect to mock BigTable"); + let commitments = vec![CheckpointCommitment::CheckpointArtifactsDigest( + sui_types::digests::CheckpointArtifactsDigest::new([7; 32]), + )]; + // Epoch 0 ends normally at checkpoint 9 after transaction 49; epoch 1 ends in safe mode at + // checkpoint 19 after transaction 79. + mock.insert_row( + epochs::NAME, + epochs::encode_key(0), + [ + u64_cell(col::EPOCH, 0), + u64_cell(col::START_CHECKPOINT, 0), + u64_cell(col::END_CHECKPOINT, 9), + u64_cell(col::TX_HI, 50), + (col::SAFE_MODE, Bytes::from_static(&[0])), + u64_cell(col::TOTAL_STAKE, 1), + u64_cell(col::STORAGE_FUND_REINVESTMENT, 2), + u64_cell(col::STORAGE_CHARGE, 3), + u64_cell(col::STORAGE_REBATE, 4), + u64_cell(col::STORAGE_FUND_BALANCE, 5), + u64_cell(col::STAKE_SUBSIDY_AMOUNT, 6), + u64_cell(col::TOTAL_GAS_FEES, 7), + u64_cell(col::TOTAL_STAKE_REWARDS_DISTRIBUTED, 8), + u64_cell(col::LEFTOVER_STORAGE_FUND_INFLOW, 9), + ( + col::EPOCH_COMMITMENTS, + Bytes::from(bcs::to_bytes(&commitments).unwrap()), + ), + ], + ) + .await; + mock.insert_row( + epochs::NAME, + epochs::encode_key(1), + [ + u64_cell(col::EPOCH, 1), + u64_cell(col::START_CHECKPOINT, 10), + u64_cell(col::END_CHECKPOINT, 19), + u64_cell(col::TX_HI, 80), + (col::SAFE_MODE, Bytes::from_static(&[1])), + ( + col::EPOCH_COMMITMENTS, + Bytes::from(bcs::to_bytes(&Vec::::new()).unwrap()), + ), + ], + ) + .await; + let at = |checkpoint, epoch| ServedHeight { checkpoint, epoch }; + + let running = get(&client, at(8, 0), Some(0), END_MASK).await; + let mut expected = Epoch::default(); + expected.transaction_start_inclusive = Some(0); + assert_eq!(running, expected); + + let ended = get(&client, at(9, 0), Some(0), END_MASK).await; + assert_eq!(ended.transaction_start_inclusive, Some(0)); + assert_eq!(ended.transaction_end_exclusive, Some(50)); + assert_eq!(ended.ended_in_safe_mode, Some(false)); + let info = ended.system_epoch_info.expect("system_epoch_info"); + assert_eq!( + [ + info.total_stake, + info.storage_fund_reinvestment, + info.storage_charge, + info.storage_rebate, + info.storage_fund_balance, + info.stake_subsidy_amount, + info.total_gas_fees, + info.total_stake_rewards_distributed, + info.leftover_storage_fund_inflow, + ], + [1, 2, 3, 4, 5, 6, 7, 8, 9].map(Some), + ); + assert_eq!( + ended.epoch_commitments, + commitments + .into_iter() + .map(Into::into) + .collect::>(), + ); + + let running = get(&client, at(15, 1), Some(1), END_MASK).await; + let mut expected = Epoch::default(); + expected.transaction_start_inclusive = Some(50); + assert_eq!(running, expected); + + let safe_mode = get(&client, at(19, 1), Some(1), END_MASK).await; + assert_eq!(safe_mode.transaction_start_inclusive, Some(50)); + assert_eq!(safe_mode.transaction_end_exclusive, Some(80)); + assert_eq!(safe_mode.ended_in_safe_mode, Some(true)); + assert_eq!(safe_mode.system_epoch_info, None); + assert!(safe_mode.epoch_commitments.is_empty()); + } } diff --git a/crates/sui-kv-rpc/src/v2/mod.rs b/crates/sui-kv-rpc/src/v2/mod.rs index d282dfcd36e6..3842f0efcf30 100644 --- a/crates/sui-kv-rpc/src/v2/mod.rs +++ b/crates/sui-kv-rpc/src/v2/mod.rs @@ -133,7 +133,8 @@ impl LedgerService for KvRpcServer { request: tonic::Request, ) -> Result, tonic::Status> { get_epoch::get_epoch( - self.client.clone(), + self.limited_client("GetEpoch"), + self.object_at_checkpoint_enabled, ServedHeight::of(&request)?, request.into_inner(), self.chain_id.chain(), diff --git a/crates/sui-rpc-api/src/grpc/v2/ledger_service/get_epoch.rs b/crates/sui-rpc-api/src/grpc/v2/ledger_service/get_epoch.rs index 9006b9cb2fb9..49a7a6b65111 100644 --- a/crates/sui-rpc-api/src/grpc/v2/ledger_service/get_epoch.rs +++ b/crates/sui-rpc-api/src/grpc/v2/ledger_service/get_epoch.rs @@ -15,12 +15,20 @@ use sui_rpc::proto::sui::rpc::v2::Epoch; use sui_rpc::proto::sui::rpc::v2::GetEpochRequest; use sui_rpc::proto::sui::rpc::v2::GetEpochResponse; use sui_rpc::proto::sui::rpc::v2::ProtocolConfig; +use sui_rpc::proto::sui::rpc::v2::SystemEpochInfo; use sui_rpc::proto::timestamp_ms_to_proto; use sui_sdk_types::EpochId; +use sui_types::storage::EpochInfo; +use sui_types::sui_system_state::SuiSystemState; use sui_types::sui_system_state::SuiSystemStateTrait; pub const READ_MASK_DEFAULT: &str = crate::read_mask_defaults::EPOCH; +/// The latest epoch is the live system state's. The live epoch's `protocol_config`, `start`, +/// `reference_gas_price` and `system_state` come from the live state; any other epoch's come from +/// its rpc-store row, with `system_state` the epoch-start snapshot. The end-of-epoch fields are +/// set exactly when `last_checkpoint` is, from the same rpc-store row, as soon as the row has +/// them. #[tracing::instrument(skip(service))] pub fn get_epoch(service: &RpcService, request: GetEpochRequest) -> Result { let read_mask = { @@ -38,22 +46,24 @@ pub fn get_epoch(service: &RpcService, request: GetEpochRequest) -> Result Result Result Result Some(0), + Some(previous) => indexes + .map(|indexes| indexes.get_epoch_info(previous)) + .transpose()? + .flatten() + .and_then(|info| info.tx_hi), + }; } if read_mask.contains(Epoch::COMMITTEE_FIELD.name) { @@ -139,9 +155,65 @@ pub fn get_epoch(service: &RpcService, request: GetEpochRequest) -> Result Option { + epoch_info.start_checkpoint?; + epoch_info.system_state +} + +/// Set the end-of-epoch fields `read_mask` selects from `epoch_info`. Call it exactly when the +/// response serves the epoch's `last_checkpoint`, so the end fields appear together with it. +pub fn set_epoch_end_fields( + message: &mut Epoch, + read_mask: &FieldMaskTree, + epoch_info: &EpochInfo, +) { + if read_mask.contains(Epoch::TRANSACTION_END_EXCLUSIVE_FIELD) { + message.transaction_end_exclusive = epoch_info.tx_hi; + } + if read_mask.contains(Epoch::ENDED_IN_SAFE_MODE_FIELD) { + message.ended_in_safe_mode = epoch_info.safe_mode; + } + if read_mask.contains(Epoch::SYSTEM_EPOCH_INFO_FIELD) { + let mut info = SystemEpochInfo::default(); + info.total_stake = epoch_info.total_stake; + info.storage_fund_reinvestment = epoch_info.storage_fund_reinvestment; + info.storage_charge = epoch_info.storage_charge; + info.storage_rebate = epoch_info.storage_rebate; + info.storage_fund_balance = epoch_info.storage_fund_balance; + info.stake_subsidy_amount = epoch_info.stake_subsidy_amount; + info.total_gas_fees = epoch_info.total_gas_fees; + info.total_stake_rewards_distributed = epoch_info.total_stake_rewards_distributed; + info.leftover_storage_fund_inflow = epoch_info.leftover_storage_fund_inflow; + // An epoch that ended in safe mode emitted no `SystemEpochInfoEvent`, so every counter + // is unset and so is the message. + message.system_epoch_info = (info != SystemEpochInfo::default()).then_some(info); + } + if read_mask.contains(Epoch::EPOCH_COMMITMENTS_FIELD) { + message.epoch_commitments = epoch_info + .epoch_commitments + .iter() + .flatten() + .cloned() + .map(Into::into) + .collect(); + } +} + #[derive(Debug)] pub struct CommitteeNotFoundError { epoch: EpochId, diff --git a/crates/sui-rpc-api/src/grpc/v2/ledger_service/mod.rs b/crates/sui-rpc-api/src/grpc/v2/ledger_service/mod.rs index 408d9bd648bb..b79d8b48c130 100644 --- a/crates/sui-rpc-api/src/grpc/v2/ledger_service/mod.rs +++ b/crates/sui-rpc-api/src/grpc/v2/ledger_service/mod.rs @@ -44,7 +44,7 @@ mod list_transactions; mod object_set; mod query_end; mod stream; -pub use get_epoch::protocol_config_to_proto; +pub use get_epoch::{protocol_config_to_proto, set_epoch_end_fields}; pub use get_object::ObjectSelector; pub use get_object::validate_get_object_requests; diff --git a/crates/sui-rpc-store/src/indexer/epochs.rs b/crates/sui-rpc-store/src/indexer/epochs.rs index 4d7bb766ba30..f2869cf77716 100644 --- a/crates/sui-rpc-store/src/indexer/epochs.rs +++ b/crates/sui-rpc-store/src/indexer/epochs.rs @@ -118,10 +118,14 @@ fn epoch_end(checkpoint: &Checkpoint) -> anyhow::Result { let epoch_commitments = summary .end_of_epoch_data .as_ref() - .map(|data| bcs::to_bytes(&data.epoch_commitments)) - .transpose() - .map_err(|e| anyhow::anyhow!("bcs encode epoch_commitments: {e}"))? - .unwrap_or_default(); + .ok_or_else(|| { + anyhow::anyhow!( + "end-of-epoch checkpoint {} has no end_of_epoch_data", + summary.sequence_number + ) + })? + .epoch_commitments + .clone(); // The `SystemEpochInfoEvent` is emitted only by the change-epoch // transaction, so scanning every transaction's events finds it @@ -214,9 +218,6 @@ mod tests { assert_eq!(end.total_gas_fees, Some(0)); assert_eq!(end.total_stake, Some(0)); assert_eq!(end.storage_charge, Some(0)); - // Commitments are always recorded (BCS of the vec, never - // empty bytes even for an empty vec). - assert!(!end.epoch_commitments.is_empty()); } #[test] diff --git a/crates/sui-rpc-store/src/schema/epochs.rs b/crates/sui-rpc-store/src/schema/epochs.rs index 5464928aea56..0aed3176dd62 100644 --- a/crates/sui-rpc-store/src/schema/epochs.rs +++ b/crates/sui-rpc-store/src/schema/epochs.rs @@ -16,6 +16,7 @@ use sui_consistent_store::error::Error; use sui_consistent_store::reader::Reader; use sui_types::committee::Committee; use sui_types::committee::EpochId; +use sui_types::messages_checkpoint::CheckpointCommitment; use sui_types::storage::EpochInfo; use sui_types::sui_system_state::SuiSystemState; use sui_types::sui_system_state::SuiSystemStateTrait; @@ -78,9 +79,8 @@ pub struct EpochEnd { /// `network_total_transactions` at the epoch's final checkpoint. pub tx_hi: u64, pub safe_mode: bool, - /// BCS-encoded `Vec` from the checkpoint's - /// end-of-epoch data. - pub epoch_commitments: Vec, + /// The checkpoint's end-of-epoch commitments, stored BCS-encoded. + pub epoch_commitments: Vec, pub total_stake: Option, pub storage_fund_balance: Option, pub storage_fund_reinvestment: Option, @@ -101,7 +101,11 @@ pub fn end(end: EpochEnd) -> Value { end_checkpoint: Some(end.end_checkpoint), tx_hi: Some(end.tx_hi), safe_mode: Some(end.safe_mode), - epoch_commitments: Some(end.epoch_commitments.into()), + epoch_commitments: Some( + bcs::to_bytes(&end.epoch_commitments) + .expect("bcs encode epoch commitments") + .into(), + ), total_stake: end.total_stake, storage_fund_balance: end.storage_fund_balance, storage_fund_reinvestment: end.storage_fund_reinvestment, @@ -151,7 +155,12 @@ fn merge( if next.end_checkpoint.is_some() { merged.end_checkpoint = next.end_checkpoint; } - if next.system_state_bcs.is_some() { + // A record without `start_checkpoint` was seeded from the state at a mid-epoch restore + // point, so it never replaces the state of a record that knows the epoch's first + // checkpoint: that one is the epoch-start state. + if next.system_state_bcs.is_some() + && (next.start_checkpoint.is_some() || merged.start_checkpoint.is_none()) + { merged.system_state_bcs = next.system_state_bcs; } if next.tx_hi.is_some() { @@ -196,7 +205,7 @@ fn merge( impl super::RpcStoreSchema { /// Look up the metadata for an epoch, decoding the embedded - /// BCS `SuiSystemState` into the canonical + /// BCS `SuiSystemState` and epoch commitments into the canonical /// [`sui_types::storage::EpochInfo`]. pub fn get_epoch(&self, epoch: EpochId) -> Result, Error> { let Some(stored) = self.epochs.get(&U64Be(epoch))? else { @@ -211,6 +220,14 @@ impl super::RpcStoreSchema { .map_err(|e| DecodeError::with_source("bcs decode SuiSystemState", e)) }) .transpose()?; + let epoch_commitments = stored + .epoch_commitments + .as_ref() + .map(|bytes| { + bcs::from_bytes::>(bytes) + .map_err(|e| DecodeError::with_source("bcs decode epoch commitments", e)) + }) + .transpose()?; Ok(Some(EpochInfo { epoch, protocol_version: stored.protocol_version, @@ -220,6 +237,18 @@ impl super::RpcStoreSchema { end_checkpoint: stored.end_checkpoint, reference_gas_price: stored.reference_gas_price, system_state, + tx_hi: stored.tx_hi, + safe_mode: stored.safe_mode, + epoch_commitments, + total_stake: stored.total_stake, + storage_fund_balance: stored.storage_fund_balance, + storage_fund_reinvestment: stored.storage_fund_reinvestment, + storage_charge: stored.storage_charge, + storage_rebate: stored.storage_rebate, + stake_subsidy_amount: stored.stake_subsidy_amount, + total_gas_fees: stored.total_gas_fees, + total_stake_rewards_distributed: stored.total_stake_rewards_distributed, + leftover_storage_fund_inflow: stored.leftover_storage_fund_inflow, })) } @@ -416,25 +445,31 @@ mod tests { } #[test] - fn partial_seed_then_backfill_fills_start_checkpoint() { + fn partial_seed_then_backfill_fills_start_record() { // The embedded restore seeds a partial start record from the // mid-epoch system state with no `start_checkpoint`; a later - // full start record from the upward backfill supplies it. The - // merge must combine them rather than clobber. + // full start record from the upward backfill supplies it, and + // its epoch-start state replaces the seeded one. + let state = |reference_gas_price| { + let mut inner = sui_types::sui_system_state::mock::sui_system_state_inner_v1(); + inner.reference_gas_price = reference_gas_price; + SuiSystemState::V1(inner) + }; + let bytes = |state: &SuiSystemState| bcs::to_bytes(state).unwrap(); let (_dir, db, schema) = fresh_db(); let mut batch = db.batch(); batch .merge( &schema.epochs, &U64Be(42), - &start(73, 1000, 111, None, None), + &start(73, 1000, 111, None, Some(bytes(&state(2)))), ) .unwrap(); batch .merge( &schema.epochs, &U64Be(42), - &start(73, 1000, 111, Some(500), None), + &start(73, 1000, 111, Some(500), Some(bytes(&state(1)))), ) .unwrap(); batch.commit().unwrap(); @@ -442,39 +477,49 @@ mod tests { let info = schema.get_epoch(42).unwrap().expect("epoch present"); assert_eq!(info.start_checkpoint, Some(500)); assert_eq!(info.protocol_version, Some(73)); + assert_eq!(info.system_state, Some(state(1))); } #[test] - fn partial_seed_does_not_clobber_known_start_checkpoint() { + fn partial_seed_does_not_clobber_known_start_record() { // Reverse order: a full start record then a partial re-seed - // (no `start_checkpoint`) must NOT erase the known value. This - // is the crux of the presence-tracked merge — a `None` operand - // leaves the field untouched. + // (no `start_checkpoint`) from a mid-epoch state must NOT erase + // the known start checkpoint, nor replace the epoch-start state. + let state = |reference_gas_price| { + let mut inner = sui_types::sui_system_state::mock::sui_system_state_inner_v1(); + inner.reference_gas_price = reference_gas_price; + SuiSystemState::V1(inner) + }; + let bytes = |state: &SuiSystemState| bcs::to_bytes(state).unwrap(); let (_dir, db, schema) = fresh_db(); let mut batch = db.batch(); batch .merge( &schema.epochs, &U64Be(42), - &start(73, 1000, 111, Some(500), None), + &start(73, 1000, 111, Some(500), Some(bytes(&state(1)))), ) .unwrap(); batch .merge( &schema.epochs, &U64Be(42), - &start(73, 1000, 111, None, None), + &start(73, 1000, 111, None, Some(bytes(&state(2)))), ) .unwrap(); batch.commit().unwrap(); let info = schema.get_epoch(42).unwrap().expect("epoch present"); assert_eq!(info.start_checkpoint, Some(500)); + assert_eq!(info.system_state, Some(state(1))); } #[test] - fn end_record_stores_system_epoch_info_fields() { + fn end_record_surfaces_system_epoch_info_fields() { let (_dir, db, schema) = fresh_db(); + let commitments = vec![CheckpointCommitment::CheckpointArtifactsDigest( + sui_types::digests::CheckpointArtifactsDigest::new([7; 32]), + )]; let mut batch = db.batch(); batch .merge( @@ -485,7 +530,7 @@ mod tests { end_checkpoint: 600, tx_hi: 12_345, safe_mode: false, - epoch_commitments: vec![1, 2, 3], + epoch_commitments: commitments.clone(), total_stake: Some(1_000), storage_fund_balance: Some(2_000), storage_fund_reinvestment: Some(3_000), @@ -500,26 +545,19 @@ mod tests { .unwrap(); batch.commit().unwrap(); - // `get_epoch` deliberately does not surface these fields yet, - // so read the raw stored record to confirm they were written. - let stored = schema - .epochs - .get(&U64Be(42)) - .unwrap() - .expect("epoch present") - .into_inner(); - assert_eq!(stored.tx_hi, Some(12_345)); - assert_eq!(stored.safe_mode, Some(false)); - assert_eq!(stored.epoch_commitments.as_deref(), Some(&[1, 2, 3][..])); - assert_eq!(stored.total_stake, Some(1_000)); - assert_eq!(stored.storage_fund_balance, Some(2_000)); - assert_eq!(stored.storage_fund_reinvestment, Some(3_000)); - assert_eq!(stored.storage_charge, Some(4_000)); - assert_eq!(stored.storage_rebate, Some(5_000)); - assert_eq!(stored.stake_subsidy_amount, Some(6_000)); - assert_eq!(stored.total_gas_fees, Some(7_000)); - assert_eq!(stored.total_stake_rewards_distributed, Some(8_000)); - assert_eq!(stored.leftover_storage_fund_inflow, Some(9_000)); + let info = schema.get_epoch(42).unwrap().expect("epoch present"); + assert_eq!(info.tx_hi, Some(12_345)); + assert_eq!(info.safe_mode, Some(false)); + assert_eq!(info.epoch_commitments, Some(commitments)); + assert_eq!(info.total_stake, Some(1_000)); + assert_eq!(info.storage_fund_balance, Some(2_000)); + assert_eq!(info.storage_fund_reinvestment, Some(3_000)); + assert_eq!(info.storage_charge, Some(4_000)); + assert_eq!(info.storage_rebate, Some(5_000)); + assert_eq!(info.stake_subsidy_amount, Some(6_000)); + assert_eq!(info.total_gas_fees, Some(7_000)); + assert_eq!(info.total_stake_rewards_distributed, Some(8_000)); + assert_eq!(info.leftover_storage_fund_inflow, Some(9_000)); } #[test] @@ -538,23 +576,19 @@ mod tests { end_checkpoint: 600, tx_hi: 7, safe_mode: true, - epoch_commitments: vec![0], + epoch_commitments: vec![], ..Default::default() }), ) .unwrap(); batch.commit().unwrap(); - let stored = schema - .epochs - .get(&U64Be(42)) - .unwrap() - .expect("epoch present") - .into_inner(); - assert_eq!(stored.safe_mode, Some(true)); - assert_eq!(stored.tx_hi, Some(7)); - assert_eq!(stored.total_stake, None); - assert_eq!(stored.total_gas_fees, None); - assert_eq!(stored.storage_charge, None); + let info = schema.get_epoch(42).unwrap().expect("epoch present"); + assert_eq!(info.safe_mode, Some(true)); + assert_eq!(info.tx_hi, Some(7)); + assert_eq!(info.epoch_commitments, Some(vec![])); + assert_eq!(info.total_stake, None); + assert_eq!(info.total_gas_fees, None); + assert_eq!(info.storage_charge, None); } } diff --git a/crates/sui-types/src/full_checkpoint_content.rs b/crates/sui-types/src/full_checkpoint_content.rs index cdc5d343ca0d..1aef46c3ae47 100644 --- a/crates/sui-types/src/full_checkpoint_content.rs +++ b/crates/sui-types/src/full_checkpoint_content.rs @@ -60,46 +60,6 @@ impl CheckpointData { .chain(self.transactions.iter().flat_map(|tx| &tx.output_objects)) .collect() } - - pub fn epoch_info(&self) -> Result, StorageError> { - if self.checkpoint_summary.end_of_epoch_data.is_none() - && self.checkpoint_summary.sequence_number != 0 - { - return Ok(None); - } - let (start_checkpoint, transaction) = if self.checkpoint_summary.sequence_number == 0 { - (0, &self.transactions[0]) - } else { - let Some(transaction) = self.transactions.iter().find(|tx| { - matches!( - tx.transaction.intent_message().value.kind(), - TransactionKind::ChangeEpoch(_) | TransactionKind::EndOfEpochTransaction(_) - ) - }) else { - return Err(StorageError::custom(format!( - "Failed to get end of epoch transaction in checkpoint {} with EndOfEpochData", - self.checkpoint_summary.sequence_number, - ))); - }; - (self.checkpoint_summary.sequence_number + 1, transaction) - }; - let system_state = - get_sui_system_state(&transaction.output_objects.as_slice()).map_err(|e| { - StorageError::custom(format!( - "Failed to find system state object output from end of epoch transaction: {e}" - )) - })?; - Ok(Some(EpochInfo { - epoch: system_state.epoch(), - protocol_version: Some(system_state.protocol_version()), - start_timestamp_ms: Some(system_state.epoch_start_timestamp_ms()), - end_timestamp_ms: None, - start_checkpoint: Some(start_checkpoint), - end_checkpoint: None, - reference_gas_price: Some(system_state.reference_gas_price()), - system_state: Some(system_state), - })) - } } #[derive(Clone, Debug, Serialize, Deserialize)] @@ -288,6 +248,7 @@ impl Checkpoint { end_checkpoint: None, reference_gas_price: Some(system_state.reference_gas_price()), system_state: Some(system_state), + ..Default::default() })) } diff --git a/crates/sui-types/src/storage/read_store.rs b/crates/sui-types/src/storage/read_store.rs index 431dae94ad5b..5d23565d0ad7 100644 --- a/crates/sui-types/src/storage/read_store.rs +++ b/crates/sui-types/src/storage/read_store.rs @@ -1086,7 +1086,7 @@ impl TransactionInfo { } } -#[derive(Clone, Default, Serialize, Deserialize, Eq, PartialEq, Debug)] +#[derive(Clone, Default, Eq, PartialEq, Debug)] pub struct EpochInfo { pub epoch: u64, pub protocol_version: Option, @@ -1095,8 +1095,22 @@ pub struct EpochInfo { pub start_checkpoint: Option, pub end_checkpoint: Option, pub reference_gas_price: Option, - // System State as of the start of the epoch + /// System state as of the start of the epoch. pub system_state: Option, - // pub end_of_epoch_transaction: Option, - // pub epoch_commitments: Vec, + /// `network_total_transactions` at the epoch's last checkpoint: one past the sequence number + /// of the epoch's last transaction. + pub tx_hi: Option, + pub safe_mode: Option, + pub epoch_commitments: Option>, + // Counters from the epoch's `SystemEpochInfoEvent`, left `None` when the epoch ended in safe + // mode, which emits no such event. + pub total_stake: Option, + pub storage_fund_balance: Option, + pub storage_fund_reinvestment: Option, + pub storage_charge: Option, + pub storage_rebate: Option, + pub stake_subsidy_amount: Option, + pub total_gas_fees: Option, + pub total_stake_rewards_distributed: Option, + pub leftover_storage_fund_inflow: Option, } diff --git a/crates/sui-types/src/sui_system_state/mod.rs b/crates/sui-types/src/sui_system_state/mod.rs index 47627de950f4..c8fdde5f003e 100644 --- a/crates/sui-types/src/sui_system_state/mod.rs +++ b/crates/sui-types/src/sui_system_state/mod.rs @@ -251,6 +251,17 @@ pub fn get_sui_system_state_wrapper( Ok(result) } +/// The ID of the system state's inner object for `version`: the dynamic field of the +/// `SuiSystemState` wrapper (`0x5`) keyed by the wrapper's `version`. +pub fn sui_system_state_inner_id(version: u64) -> ObjectID { + crate::dynamic_field::derive_dynamic_field_id( + SUI_SYSTEM_STATE_OBJECT_ID, + &move_core_types::language_storage::TypeTag::U64, + &version.to_le_bytes(), + ) + .expect("a u64 key type tag always serializes") +} + pub fn get_sui_system_state(object_store: &dyn ObjectStore) -> Result { let wrapper = get_sui_system_state_wrapper(object_store)?; let id = wrapper.id.id.bytes;