Skip to content

Commit df215b8

Browse files
committed
Fix getBlockHeight
1 parent 6471a02 commit df215b8

8 files changed

Lines changed: 61 additions & 20 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ exclude = []
1010
resolver = "2"
1111

1212
[workspace.package]
13-
version = "2.0.5"
13+
version = "2.0.6"
1414
authors = ["Dexterlab <support@dexterlab.com>"]
1515
repository = "https://github.com/solana-labs/solana"
1616
homepage = "https://dexterlab.com/"
@@ -75,10 +75,10 @@ solana-metrics = "=2.0.5"
7575
solana-net-utils = "=2.0.5"
7676
solana-perf = "=2.0.5"
7777
solana-rayon-threadlimit = "=2.0.5"
78-
block-meta-rpc = { path = "rpc", version = "=2.0.5" }
78+
block-meta-rpc = { path = "rpc", version = "=2.0.6" }
7979
solana-rpc-client-api = "=2.0.5"
8080
solana-sdk = "=2.0.5"
81-
solana-storage-mysql = { path = "storage-mysql", version = "=2.0.5" }
81+
solana-storage-mysql = { path = "storage-mysql", version = "=2.0.6" }
8282
solana-version = "=2.0.5"
8383
spl-memo = "=3.0.1"
8484
symlink = "0.1.0"

rpc/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ pub struct Config {
2323

2424
/// MySQL database name
2525
pub mysql_name: String,
26+
27+
/// MySQL table name for blocks
28+
#[serde(default = "default_mysql_table")]
29+
pub mysql_table: String,
2630
}
2731

2832
impl Config {
@@ -40,3 +44,7 @@ impl Config {
4044
}
4145
}
4246
}
47+
48+
fn default_mysql_table() -> String {
49+
"sol_mainnet_block".to_string()
50+
}

rpc/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ fn main() {
112112

113113
let mut rpc_config = JsonRpcConfig::default_for_storage_rpc();
114114
rpc_config.rpc_mysql_config = rpc_mysql_config;
115+
rpc_config.mysql_table = app_config.mysql_table.clone();
115116
rpc_config.obsolete_v1_7_api = matches.is_present("obsolete_v1_7_rpc_api");
116117
rpc_config.rpc_threads = value_t_or_exit!(matches, "rpc_threads", usize);
117118
rpc_config.rpc_niceness_adj = value_t_or_exit!(matches, "rpc_niceness_adj", i8);

rpc/src/request_processor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub struct JsonRpcConfig {
6060
pub full_api: bool,
6161
pub obsolete_v1_7_api: bool,
6262
pub max_request_body_size: Option<usize>,
63+
pub mysql_table: String,
6364
}
6465

6566
impl JsonRpcConfig {

rpc/src/rpc_service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl JsonRpcService {
8585
username: username.clone(),
8686
password: password.clone(),
8787
db_name: db_name.clone(),
88+
table_name: config.mysql_table.clone(),
8889
};
8990
runtime
9091
.block_on(solana_storage_mysql::meta_storage::MetaStorage::new_with_config(mysql_config))

storage-mysql/src/meta_storage.rs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub struct MetaStorageConfig {
8181
pub username: String,
8282
pub password: String,
8383
pub db_name: String,
84+
pub table_name: String,
8485
}
8586

8687
impl Default for MetaStorageConfig {
@@ -93,13 +94,15 @@ impl Default for MetaStorageConfig {
9394
username: String::new(),
9495
password: String::new(),
9596
db_name: String::new(),
97+
table_name: "sol_mainnet_block".to_string(),
9698
}
9799
}
98100
}
99101

100102
#[derive(Clone)]
101103
pub struct MetaStorage {
102104
connection: MySQLConnection,
105+
table_name: String,
103106
}
104107

105108
impl MetaStorage {
@@ -124,6 +127,7 @@ impl MetaStorage {
124127
username,
125128
password,
126129
db_name,
130+
table_name,
127131
} = config;
128132
let dsn = format!("mysql://{}:{}@{}:{}/{}", username, password, host, port, db_name);
129133
let connection = MySQLConnection::new(
@@ -135,6 +139,7 @@ impl MetaStorage {
135139

136140
Ok(Self {
137141
connection,
142+
table_name,
138143
})
139144
}
140145

@@ -147,7 +152,7 @@ impl MetaStorage {
147152

148153
// Use `get_first_key` to get the smallest slot
149154
let first_block: Option<u64> = mysql
150-
.get_first_key("sol_mainnet_block", "id")
155+
.get_first_key(&self.table_name, "id")
151156
.await
152157
.map_err(|e| Error::StorageBackendError(Box::new(e)))?;
153158

@@ -162,7 +167,7 @@ impl MetaStorage {
162167

163168
// Use `get_last_key` to get the largest slot
164169
let last_block: Option<u64> = mysql
165-
.get_last_key("sol_mainnet_block", "id")
170+
.get_last_key(&self.table_name, "id")
166171
.await
167172
.map_err(|e| Error::StorageBackendError(Box::new(e)))?;
168173

@@ -187,7 +192,7 @@ impl MetaStorage {
187192
let limit_i64 = limit.map(|l| l as i64);
188193

189194
let blocks: Vec<u64> = mysql
190-
.get_row_keys("sol_mainnet_block", Some(&start_key), end_key.as_deref(), limit_i64)
195+
.get_row_keys(&self.table_name, Some(&start_key), end_key.as_deref(), limit_i64)
191196
.await?;
192197
Ok(blocks.into_iter().map(|block| block as Slot).collect())
193198
}
@@ -200,7 +205,7 @@ impl MetaStorage {
200205

201206
// Fetch `PrimitiveDateTime` directly from MySQL
202207
let block_time_primitive: PrimitiveDateTime = mysql
203-
.get_single_value::<PrimitiveDateTime>("sol_mainnet_block", "block_time", "id", &key)
208+
.get_single_value::<PrimitiveDateTime>(&self.table_name, "block_time", "id", &key)
204209
.await
205210
.map_err(|e| match e {
206211
crate::mysql::Error::RowNotFound => Error::BlockNotFound(slot),
@@ -227,22 +232,47 @@ impl MetaStorage {
227232
let mysql = self.connection.client();
228233

229234
// Fetch the ID of the latest block
230-
let latest_block_id: u64 = mysql
231-
.get_last_key("solana_blocks", "id")
235+
let latest_block_id: u64 = match mysql
236+
.get_last_key(&self.table_name, "id")
232237
.await
233-
.map_err(|e| Error::StorageBackendError(Box::new(e)))?
234-
.ok_or_else(|| Error::BlockNotFound(0))?; // Handle case where no blocks exist
238+
{
239+
Ok(Some(id)) => id,
240+
Ok(None) => {
241+
warn!("No rows in table '{}'; cannot determine latest block id", self.table_name);
242+
return Err(Error::BlockNotFound(0));
243+
}
244+
Err(e) => {
245+
error!("Failed to fetch latest block id from '{}': {}", self.table_name, e);
246+
return Err(Error::StorageBackendError(Box::new(e)));
247+
}
248+
};
235249

236250
debug!("Latest block ID fetched: {}", latest_block_id);
237251

238252
// Fetch the block height using the latest block ID
239-
let block_height: u64 = mysql
240-
.get_single_value::<u64>("solana_blocks", "block_height", "id", &latest_block_id.to_string())
253+
let block_height: u64 = match mysql
254+
.get_single_value::<u64>(&self.table_name, "block_height", "id", &latest_block_id.to_string())
241255
.await
242-
.map_err(|e| match e {
243-
crate::mysql::Error::RowNotFound => Error::BlockNotFound(latest_block_id),
244-
other => Error::StorageBackendError(Box::new(other)),
245-
})?;
256+
{
257+
Ok(height) => height,
258+
Err(crate::mysql::Error::RowNotFound) => {
259+
warn!(
260+
"Block height not found for latest block id {} in '{}'",
261+
latest_block_id,
262+
self.table_name
263+
);
264+
return Err(Error::BlockNotFound(latest_block_id));
265+
}
266+
Err(other) => {
267+
error!(
268+
"Failed to fetch block_height for id {} from '{}': {}",
269+
latest_block_id,
270+
self.table_name,
271+
other
272+
);
273+
return Err(Error::StorageBackendError(Box::new(other)));
274+
}
275+
};
246276

247277
debug!("Latest block Height fetched: {}", block_height);
248278

storage-mysql/src/mysql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum Error {
1717
#[error("Timeout")]
1818
Timeout,
1919

20-
#[error("MySQL")]
20+
#[error("MySQL: {0}")]
2121
MySQL(mysql::Error),
2222
}
2323

0 commit comments

Comments
 (0)