From 06db495d969ad3aeefc9577d1e50f58f18670223 Mon Sep 17 00:00:00 2001 From: Chen Kai <281165273grape@gmail.com> Date: Wed, 15 Mar 2023 00:57:19 +0800 Subject: [PATCH] fix: use string instead of Vec for stageId. (#1495) Signed-off-by: Chen Kai <281165273grape@gmail.com> --- crates/stages/src/id.rs | 4 ++-- crates/storage/db/src/tables/mod.rs | 2 +- crates/storage/db/src/tables/models/mod.rs | 13 +++++++++++++ crates/storage/provider/src/providers/mod.rs | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/stages/src/id.rs b/crates/stages/src/id.rs index 8feb02221..2d4bfc971 100644 --- a/crates/stages/src/id.rs +++ b/crates/stages/src/id.rs @@ -27,7 +27,7 @@ impl StageId { /// Get the last committed progress of this stage. pub fn get_progress<'db>(&self, tx: &impl DbTx<'db>) -> Result, DbError> { - tx.get::(self.0.as_bytes().to_vec()) + tx.get::(self.0.to_string()) } /// Save the progress of this stage. @@ -36,7 +36,7 @@ impl StageId { tx: &impl DbTxMut<'db>, block: BlockNumber, ) -> Result<(), DbError> { - tx.put::(self.0.as_bytes().to_vec(), block) + tx.put::(self.0.to_string(), block) } } diff --git a/crates/storage/db/src/tables/mod.rs b/crates/storage/db/src/tables/mod.rs index bb45b8bf4..7d058403d 100644 --- a/crates/storage/db/src/tables/mod.rs +++ b/crates/storage/db/src/tables/mod.rs @@ -305,7 +305,7 @@ table!( /// List with transaction numbers. pub type TransitionList = IntegerList; /// Encoded stage id. -pub type StageId = Vec; +pub type StageId = String; // // TODO: Temporary types, until they're properly defined alongside with the Encode and Decode Trait diff --git a/crates/storage/db/src/tables/models/mod.rs b/crates/storage/db/src/tables/models/mod.rs index 545eca636..5d98e7025 100644 --- a/crates/storage/db/src/tables/models/mod.rs +++ b/crates/storage/db/src/tables/models/mod.rs @@ -83,3 +83,16 @@ impl Decode for H256 { Ok(H256::from_slice(&value.into()[..])) } } + +impl Encode for String { + type Encoded = Vec; + fn encode(self) -> Self::Encoded { + self.into_bytes() + } +} + +impl Decode for String { + fn decode>(value: B) -> Result { + String::from_utf8(value.into().to_vec()).map_err(|_| Error::DecodeError) + } +} diff --git a/crates/storage/provider/src/providers/mod.rs b/crates/storage/provider/src/providers/mod.rs index a7539fa73..ce50dba0c 100644 --- a/crates/storage/provider/src/providers/mod.rs +++ b/crates/storage/provider/src/providers/mod.rs @@ -106,7 +106,7 @@ impl BlockIdProvider for ShareableDatabase { fn chain_info(&self) -> Result { let best_number = self .db - .view(|tx| tx.get::("Finish".as_bytes().to_vec()))? + .view(|tx| tx.get::("Finish".to_string()))? .map_err(Into::::into)? .unwrap_or_default(); let best_hash = self.block_hash(U256::from(best_number))?.unwrap_or_default();