Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Add Incremental Snapshot support to RPC #19559

Merged
merged 11 commits into from
Sep 2, 2021
9 changes: 7 additions & 2 deletions client/src/mock_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use {
rpc_response::{
Response, RpcAccountBalance, RpcBlockProduction, RpcBlockProductionRange, RpcBlockhash,
RpcConfirmedTransactionStatusWithSignature, RpcContactInfo, RpcFees, RpcPerfSample,
RpcResponseContext, RpcSimulateTransactionResult, RpcStakeActivation, RpcSupply,
RpcVersionInfo, RpcVoteAccountInfo, RpcVoteAccountStatus, StakeActivationState,
RpcResponseContext, RpcSimulateTransactionResult, RpcSnapshotSlotInfo,
RpcStakeActivation, RpcSupply, RpcVersionInfo, RpcVoteAccountInfo,
RpcVoteAccountStatus, StakeActivationState,
},
rpc_sender::RpcSender,
},
Expand Down Expand Up @@ -222,6 +223,10 @@ impl RpcSender for MockSender {
"getMaxShredInsertSlot" => json![0],
"requestAirdrop" => Value::String(Signature::new(&[8; 64]).to_string()),
"getSnapshotSlot" => Value::Number(Number::from(0)),
"getHighestSnapshotSlot" => json!(RpcSnapshotSlotInfo {
full: 100,
incremental: Some(110),
}),
"getBlockHeight" => Value::Number(Number::from(1234)),
"getSlotLeaders" => json!([PUBKEY]),
"getBlockProduction" => {
Expand Down
20 changes: 16 additions & 4 deletions client/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,13 +1018,16 @@ impl RpcClient {
)
}

/// Returns the highest slot that the node has a snapshot for.
/// Returns the highest slot information that the node has snapshots for.
///
/// This will find the highest full snapshot slot, and the highest incremental snapshot slot
/// _based on_ the full snapshot slot, if there is one.
///
/// # RPC Reference
///
/// This method corresponds directly to the [`getSnapshotSlot`] RPC method.
/// This method corresponds directly to the [`getHighestSnapshotSlot`] RPC method.
///
/// [`getSnapshotSlot`]: https://docs.solana.com/developing/clients/jsonrpc-api#getsnapshotslot
/// [`getHighestSnapshotSlot`]: https://docs.solana.com/developing/clients/jsonrpc-api#gethighestsnapshotslot
///
/// # Examples
///
Expand All @@ -1034,9 +1037,18 @@ impl RpcClient {
/// # client_error::ClientError,
/// # };
/// # let rpc_client = RpcClient::new_mock("succeeds".to_string());
/// let slot = rpc_client.get_snapshot_slot()?;
/// let snapshot_slot_info = rpc_client.get_highest_snapshot_slot()?;
/// # Ok::<(), ClientError>(())
/// ```
pub fn get_highest_snapshot_slot(&self) -> ClientResult<RpcSnapshotSlotInfo> {
self.send(RpcRequest::GetHighestSnapshotSlot, Value::Null)
}

#[deprecated(
since = "1.8.0",
note = "Please use RpcClient::get_highest_snapshot_slot() instead"
)]
#[allow(deprecated)]
pub fn get_snapshot_slot(&self) -> ClientResult<Slot> {
self.send(RpcRequest::GetSnapshotSlot, Value::Null)
}
Expand Down
6 changes: 6 additions & 0 deletions client/src/rpc_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ pub enum RpcRequest {
)]
GetRecentBlockhash,
GetRecentPerformanceSamples,
GetHighestSnapshotSlot,
#[deprecated(
since = "1.8.0",
note = "Please use RpcRequest::GetHighestSnapshotSlot instead"
)]
GetSnapshotSlot,
GetSignaturesForAddress,
GetSignatureStatuses,
Expand Down Expand Up @@ -151,6 +156,7 @@ impl fmt::Display for RpcRequest {
RpcRequest::GetProgramAccounts => "getProgramAccounts",
RpcRequest::GetRecentBlockhash => "getRecentBlockhash",
RpcRequest::GetRecentPerformanceSamples => "getRecentPerformanceSamples",
RpcRequest::GetHighestSnapshotSlot => "getHighestSnapshotSlot",
RpcRequest::GetSnapshotSlot => "getSnapshotSlot",
RpcRequest::GetSignaturesForAddress => "getSignaturesForAddress",
RpcRequest::GetSignatureStatuses => "getSignatureStatuses",
Expand Down
6 changes: 6 additions & 0 deletions client/src/rpc_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,9 @@ impl From<ConfirmedTransactionStatusWithSignature> for RpcConfirmedTransactionSt
}
}
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
pub struct RpcSnapshotSlotInfo {
pub full: Slot,
pub incremental: Option<Slot>,
}
109 changes: 76 additions & 33 deletions docs/src/developing/clients/jsonrpc-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ gives a convenient interface for the RPC methods.
- [getFirstAvailableBlock](jsonrpc-api.md#getfirstavailableblock)
- [getGenesisHash](jsonrpc-api.md#getgenesishash)
- [getHealth](jsonrpc-api.md#gethealth)
- [getHighestSnapshotSlot](jsonrpc-api.md#gethighestsnapshotslot)
- [getIdentity](jsonrpc-api.md#getidentity)
- [getInflationGovernor](jsonrpc-api.md#getinflationgovernor)
- [getInflationRate](jsonrpc-api.md#getinflationrate)
Expand All @@ -53,7 +54,6 @@ gives a convenient interface for the RPC methods.
- [getSlotLeader](jsonrpc-api.md#getslotleader)
- [getSlotLeaders](jsonrpc-api.md#getslotleaders)
- [getStakeActivation](jsonrpc-api.md#getstakeactivation)
- [getSnapshotSlot](jsonrpc-api.md#getsnapshotslot)
- [getSupply](jsonrpc-api.md#getsupply)
- [getTokenAccountBalance](jsonrpc-api.md#gettokenaccountbalance)
- [getTokenAccountsByDelegate](jsonrpc-api.md#gettokenaccountsbydelegate)
Expand Down Expand Up @@ -101,6 +101,7 @@ Unstable methods may see breaking changes in patch releases and may not be suppo
- [getFeeRateGovernor](jsonrpc-api.md#getfeerategovernor)
- [getFees](jsonrpc-api.md#getfees)
- [getRecentBlockhash](jsonrpc-api.md#getrecentblockhash)
- [getSnapshotSlot](jsonrpc-api.md#getsnapshotslot)

## Request Formatting

Expand Down Expand Up @@ -1296,6 +1297,46 @@ Unhealthy Result (if additional information is available)
}
```

### getHighestSnapshotSlot

**NEW: This method is only available in solana-core v1.8 or newer. Please use
[getSnapshotSlot](jsonrpc-api.md#getsnapshotslot) for solana-core v1.7**

Returns the highest slot information that the node has snapshots for.

This will find the highest full snapshot slot, and the highest incremental
snapshot slot _based on_ the full snapshot slot, if there is one.

#### Parameters:

None

#### Results:

- `<object>`
- `full: <u64>` - Highest full snapshot slot
- `incremental: <u64 | undefined>` - Highest incremental snapshot slot _based on_ `full`


#### Example:

Request:
```bash
curl http://localhost:8899 -X POST -H "Content-Type: application/json" -d '
{"jsonrpc":"2.0","id":1,"method":"getHighestSnapshotSlot"}
'
```

Result:
```json
{"jsonrpc":"2.0","result":{"full":100,"incremental":110},"id":1}
```

Result when the node has no snapshot:
```json
{"jsonrpc":"2.0","error":{"code":-32008,"message":"No snapshot"},"id":1}
```

### getIdentity

Returns the identity pubkey for the current node
Expand Down Expand Up @@ -2121,38 +2162,6 @@ Result:
}
```


### getSnapshotSlot

Returns the highest slot that the node has a snapshot for

#### Parameters:

None

#### Results:

- `<u64>` - Snapshot slot

#### Example:

Request:
```bash
curl http://localhost:8899 -X POST -H "Content-Type: application/json" -d '
{"jsonrpc":"2.0","id":1, "method":"getSnapshotSlot"}
'
```

Result:
```json
{"jsonrpc":"2.0","result":100,"id":1}
```

Result when the node has no snapshot:
```json
{"jsonrpc":"2.0","error":{"code":-32008,"message":"No snapshot"},"id":1}
```

### getSignaturesForAddress

**NEW: This method is only available in solana-core v1.7 or newer. Please use
Expand Down Expand Up @@ -4778,3 +4787,37 @@ Result:
"id": 1
}
```

### getSnapshotSlot

**DEPRECATED: Please use [getHighestSnapshotSlot](jsonrpc-api.md#gethighestsnapshotslot) instead**
This method is expected to be removed in solana-core v1.9

Returns the highest slot that the node has a snapshot for

#### Parameters:

None

#### Results:

- `<u64>` - Snapshot slot

#### Example:

Request:
```bash
curl http://localhost:8899 -X POST -H "Content-Type: application/json" -d '
{"jsonrpc":"2.0","id":1, "method":"getSnapshotSlot"}
'
```

Result:
```json
{"jsonrpc":"2.0","result":100,"id":1}
```

Result when the node has no snapshot:
```json
{"jsonrpc":"2.0","error":{"code":-32008,"message":"No snapshot"},"id":1}
```
52 changes: 41 additions & 11 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2276,8 +2276,8 @@ pub mod rpc_minimal {
commitment: Option<CommitmentConfig>,
) -> Result<u64>;

#[rpc(meta, name = "getSnapshotSlot")]
fn get_snapshot_slot(&self, meta: Self::Metadata) -> Result<Slot>;
#[rpc(meta, name = "getHighestSnapshotSlot")]
fn get_highest_snapshot_slot(&self, meta: Self::Metadata) -> Result<RpcSnapshotSlotInfo>;

#[rpc(meta, name = "getTransactionCount")]
fn get_transaction_count(
Expand Down Expand Up @@ -2373,16 +2373,31 @@ pub mod rpc_minimal {
Ok(meta.get_block_height(commitment))
}

fn get_snapshot_slot(&self, meta: Self::Metadata) -> Result<Slot> {
debug!("get_snapshot_slot rpc request received");
fn get_highest_snapshot_slot(&self, meta: Self::Metadata) -> Result<RpcSnapshotSlotInfo> {
debug!("get_highest_snapshot_slot rpc request received");

meta.snapshot_config
.and_then(|snapshot_config| {
snapshot_utils::get_highest_full_snapshot_archive_slot(
&snapshot_config.snapshot_archives_dir,
)
})
.ok_or_else(|| RpcCustomError::NoSnapshot.into())
if meta.snapshot_config.is_none() {
return Err(RpcCustomError::NoSnapshot.into());
}

let snapshot_archives_dir = meta
.snapshot_config
.map(|snapshot_config| snapshot_config.snapshot_archives_dir)
.unwrap();

let full_snapshot_slot =
snapshot_utils::get_highest_full_snapshot_archive_slot(&snapshot_archives_dir)
.ok_or(RpcCustomError::NoSnapshot)?;
let incremental_snapshot_slot =
snapshot_utils::get_highest_incremental_snapshot_archive_slot(
&snapshot_archives_dir,
full_snapshot_slot,
);

Ok(RpcSnapshotSlotInfo {
full: full_snapshot_slot,
incremental: incremental_snapshot_slot,
})
}

fn get_transaction_count(
Expand Down Expand Up @@ -3686,6 +3701,9 @@ pub mod rpc_deprecated_v1_8 {
&self,
meta: Self::Metadata,
) -> Result<RpcResponse<RpcFeeRateGovernor>>;

#[rpc(meta, name = "getSnapshotSlot")]
fn get_snapshot_slot(&self, meta: Self::Metadata) -> Result<Slot>;
}

pub struct DeprecatedV1_8Impl;
Expand Down Expand Up @@ -3729,6 +3747,18 @@ pub mod rpc_deprecated_v1_8 {
debug!("get_fee_rate_governor rpc request received");
Ok(meta.get_fee_rate_governor())
}

fn get_snapshot_slot(&self, meta: Self::Metadata) -> Result<Slot> {
debug!("get_snapshot_slot rpc request received");

meta.snapshot_config
.and_then(|snapshot_config| {
snapshot_utils::get_highest_full_snapshot_archive_slot(
&snapshot_config.snapshot_archives_dir,
)
})
.ok_or_else(|| RpcCustomError::NoSnapshot.into())
}
}
}

Expand Down
Loading