fix: use string instead of Vec<u8> for stageId. (#1495)

Signed-off-by: Chen Kai <281165273grape@gmail.com>
This commit is contained in:
Chen Kai
2023-03-15 00:57:19 +08:00
committed by GitHub
parent c78effcb3f
commit 06db495d96
4 changed files with 17 additions and 4 deletions

View File

@ -27,7 +27,7 @@ impl StageId {
/// Get the last committed progress of this stage. /// Get the last committed progress of this stage.
pub fn get_progress<'db>(&self, tx: &impl DbTx<'db>) -> Result<Option<BlockNumber>, DbError> { pub fn get_progress<'db>(&self, tx: &impl DbTx<'db>) -> Result<Option<BlockNumber>, DbError> {
tx.get::<SyncStage>(self.0.as_bytes().to_vec()) tx.get::<SyncStage>(self.0.to_string())
} }
/// Save the progress of this stage. /// Save the progress of this stage.
@ -36,7 +36,7 @@ impl StageId {
tx: &impl DbTxMut<'db>, tx: &impl DbTxMut<'db>,
block: BlockNumber, block: BlockNumber,
) -> Result<(), DbError> { ) -> Result<(), DbError> {
tx.put::<SyncStage>(self.0.as_bytes().to_vec(), block) tx.put::<SyncStage>(self.0.to_string(), block)
} }
} }

View File

@ -305,7 +305,7 @@ table!(
/// List with transaction numbers. /// List with transaction numbers.
pub type TransitionList = IntegerList; pub type TransitionList = IntegerList;
/// Encoded stage id. /// Encoded stage id.
pub type StageId = Vec<u8>; pub type StageId = String;
// //
// TODO: Temporary types, until they're properly defined alongside with the Encode and Decode Trait // TODO: Temporary types, until they're properly defined alongside with the Encode and Decode Trait

View File

@ -83,3 +83,16 @@ impl Decode for H256 {
Ok(H256::from_slice(&value.into()[..])) Ok(H256::from_slice(&value.into()[..]))
} }
} }
impl Encode for String {
type Encoded = Vec<u8>;
fn encode(self) -> Self::Encoded {
self.into_bytes()
}
}
impl Decode for String {
fn decode<B: Into<Bytes>>(value: B) -> Result<Self, Error> {
String::from_utf8(value.into().to_vec()).map_err(|_| Error::DecodeError)
}
}

View File

@ -106,7 +106,7 @@ impl<DB: Database> BlockIdProvider for ShareableDatabase<DB> {
fn chain_info(&self) -> Result<ChainInfo> { fn chain_info(&self) -> Result<ChainInfo> {
let best_number = self let best_number = self
.db .db
.view(|tx| tx.get::<tables::SyncStage>("Finish".as_bytes().to_vec()))? .view(|tx| tx.get::<tables::SyncStage>("Finish".to_string()))?
.map_err(Into::<reth_interfaces::db::Error>::into)? .map_err(Into::<reth_interfaces::db::Error>::into)?
.unwrap_or_default(); .unwrap_or_default();
let best_hash = self.block_hash(U256::from(best_number))?.unwrap_or_default(); let best_hash = self.block_hash(U256::from(best_number))?.unwrap_or_default();