-
Notifications
You must be signed in to change notification settings - Fork 61
feat: add ChainId type #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tdelabro
wants to merge
8
commits into
starknet-io:main
Choose a base branch
from
tdelabro:feat-chain-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8901471
feat: add ChainId type
tdelabro fbc3b5a
better feat split for not(alloc)
tdelabro b352e1b
fix pub inner fields
tdelabro c548871
replace TryFrom<&str> by FromStr
tdelabro ef2ad22
rebased
tdelabro d8e659e
don't use string for str test
tdelabro d5b2972
ci: reduce depth of feature combination testing
tdelabro b2be0d1
silence warnings unused
tdelabro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,191 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(not(feature = "std"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub extern crate alloc; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(not(feature = "std"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use alloc::string::{String, ToString}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use crate::short_string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use crate::short_string::ShortString; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use super::{ChainId, SN_MAIN_STR, SN_SEPOLIA_STR}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl From<ChainId> for ShortString { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn from(value: ChainId) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match value { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Mainnet => short_string!("SN_MAIN"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Sepolia => short_string!("SN_SEPOLIA"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "devnet")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Devnet(ss) => ss, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "devnet")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl From<ShortString> for ChainId { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn from(value: ShortString) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if value.as_ref() == SN_MAIN_STR { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Mainnet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if value.as_ref() == SN_SEPOLIA_STR { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Sepolia | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Devnet(value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(not(feature = "devnet"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mod try_chain_id_from_short_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use crate::chain_id::{SN_MAIN_STR, SN_SEPOLIA_STR}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use super::*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Debug, Clone)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub struct TryChainIdFromShortStringError(ShortString); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl core::fmt::Display for TryChainIdFromShortStringError { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
write!(f, "unknown chain id: {}", self.0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "std")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl std::error::Error for TryChainIdFromShortStringError {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl TryFrom<ShortString> for ChainId { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type Error = TryChainIdFromShortStringError; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn try_from(value: ShortString) -> Result<Self, Self::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if value.as_ref() == SN_MAIN_STR { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(ChainId::Mainnet) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if value.as_ref() == SN_SEPOLIA_STR { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(ChainId::Sepolia) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(TryChainIdFromShortStringError(value)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(not(feature = "devnet"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub use try_chain_id_from_short_string::*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// String | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl From<ChainId> for String { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn from(value: ChainId) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match value { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Mainnet => SN_MAIN_STR.to_string(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Sepolia => SN_SEPOLIA_STR.to_string(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "devnet")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Devnet(ss) => ss.to_string(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(not(feature = "devnet"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl From<ChainId> for &str { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn from(value: ChainId) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match value { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Mainnet => SN_MAIN_STR, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ChainId::Sepolia => SN_SEPOLIA_STR, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+83
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why conversion from |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Debug, Clone, Copy)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "devnet")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub struct TryChainIdFromStringError(pub(super) crate::short_string::TryShortStringFromStringError); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Debug, Clone)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(not(feature = "devnet"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pub struct TryChainIdFromStringError(pub(super) String); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl core::fmt::Display for TryChainIdFromStringError { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "devnet")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
write!(f, "failed to parse string as ShortString: {}", self.0)?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(not(feature = "devnet"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
write!(f, "unknown chain id: {}", self.0)?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "std")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl std::error::Error for TryChainIdFromStringError {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl TryFrom<String> for ChainId { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type Error = TryChainIdFromStringError; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn try_from(value: String) -> Result<Self, Self::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if value == SN_MAIN_STR { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return Ok(ChainId::Mainnet); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if value == SN_SEPOLIA_STR { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return Ok(ChainId::Sepolia); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "devnet")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match ShortString::try_from(value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(ss) => Ok(ChainId::Devnet(ss)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(e) => Err(TryChainIdFromStringError(e)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(not(feature = "devnet"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Err(TryChainIdFromStringError(value)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+120
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(test)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mod tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use super::*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn short_string_and_chain_id_round_trip() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let ss = short_string!("SN_MAIN"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let chain_id = ChainId::try_from(ss.clone()).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert_eq!(chain_id.to_string(), ss.to_string()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let ss = short_string!("SN_SEPOLIA"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let chain_id = ChainId::try_from(ss.clone()).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert_eq!(chain_id.to_string(), ss.to_string()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(not(feature = "devnet"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let ss = short_string!("SN_DEVNET"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert!(ChainId::try_from(ss).is_err()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "devnet")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let ss = short_string!("SN_DEVNET"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let chain_id = ChainId::try_from(ss.clone()).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert_eq!(ss.to_string(), chain_id.to_string()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fn string_and_chain_id_round_trip() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let s = String::from(SN_MAIN_STR); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let chain_id = ChainId::try_from(s.clone()).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert_eq!(chain_id.to_string(), s.to_string()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let s = String::from(SN_SEPOLIA_STR); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let chain_id = ChainId::try_from(s.clone()).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert_eq!(chain_id.to_string(), s.to_string()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(not(feature = "devnet"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let s = String::from("SN_DEVNET"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert!(ChainId::try_from(s).is_err()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "devnet")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let s = String::from("SN_DEVNET"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let chain_id = ChainId::try_from(s.clone()).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert_eq!(s, chain_id.to_string()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let s = String::from("SN_DEVNET_LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert!(ChainId::try_from(s).is_err()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let s = String::from("SN_DEVNET_🌟"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert!(ChainId::try_from(s).is_err()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, why does conversion from
ChainId
toShortString
is possible but not the other way around if featuredevnet
is disabled?