Skip to content
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

fix state_key and access_path_str tests #4334

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions consensus/src/consensus_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ fn verify_header_test_barnard_block3_ubuntu22() {
.unwrap()
}
// see https://stcscan.io/barnard/address/0x01/resources
// 0x00000000000000000000000000000001::Config::Config<0x00000000000000000000000000000001::ConsensusConfig::ConsensusConfig> "strategy":1
// 0x1::Config::Config<0x1::ConsensusConfig::ConsensusConfig> "strategy":1
// see https://stcscan.io/main/address/0x01/resources
// 0x00000000000000000000000000000001::Config::Config<0x00000000000000000000000000000001::ConsensusConfig::ConsensusConfig> "strategy":3
// 0x01::Config::Config<0x1::ConsensusConfig::ConsensusConfig> "strategy":3
#[stest::test]
fn verify_header_test_barnard_block5061847_ubuntu20() {
let str = r#"{"header":{"parent_hash":"0x45efb7914f0fe6bcd6baf4ed7a67da076953cf18128a25227e575a689236942f","timestamp":1654174610768,"number":5061847,"author":"0x7eec55ea1bafa8c4919101135b90b17b","author_auth_key":null,"txn_accumulator_root":"0x8c35c3d34ee16ca395c2a17668b61894b347c037d49d30477e7d2506dc69e936","block_accumulator_root":"0x0e602da30ea3f2f91b505f8a788dfc1c2b73619f328661a80ec6d59197527936","state_root":"0x42c63a367951160b01ce4e2c1e4559f08e786cf1e56a5e423da6db459f9507f7","gas_used":0,"difficulty":"0x0bee","body_hash":"0xc01e0329de6d899348a8ef4bd51db56175b3fa0988e57c3dcec8eaf13a164d97","chain_id":{"id":251},"nonce":2771673659,"extra":"0x00000000"},"body":{"transactions":[],"uncles":null}}"#;
Expand Down
1 change: 1 addition & 0 deletions scripts/nextest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cargo nextest run \
-p starcoin-storage \
-p starcoin-types \
-p starcoin-sync \
-p starcoin-vm-types \
--retries 2 --build-jobs 8 --test-threads 12 --no-fail-fast --failure-output immediate-final


Expand Down
8 changes: 6 additions & 2 deletions vm/types/src/access_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ impl fmt::Debug for AccessPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
let path = match self.path {
DataPath::Code(ref module_name) => {
format!("\"Code({}::{})\"", &self.address, module_name)
format!(
"\"Code(0x{}::{})\"",
&self.address.short_str_lossless(),
module_name
)
}
DataPath::Resource(ref struct_tag) => {
format!("\"Resource({})\"", struct_tag)
Expand All @@ -210,7 +214,7 @@ impl fmt::Debug for AccessPath {

impl fmt::Display for AccessPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/{}", self.address, self.path)
write!(f, "0x{}/{}", self.address.short_str_lossless(), self.path)
}
}

Expand Down
12 changes: 6 additions & 6 deletions vm/types/src/state_store/state_key/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ fn assert_crypto_hash(key: &StateKey, expected_hash: &str) {
fn test_resource_hash() {
assert_crypto_hash(
&StateKey::resource_typed::<AccountResource>(&AccountAddress::TWO).unwrap(),
"8f9ab5d5e3c9f5b885fcceea388fecd16bdb490da08aac9d4f026ddc66733def",
"fdec56915926115cd094939bf5ef500157dd63a56e1a0e7521600adacdc50b90",
);
}

#[test]
fn test_resource_group_hash() {
assert_crypto_hash(
&StateKey::resource_group(&AccountAddress::TWO, &ObjectGroupResource::struct_tag()),
"87973d52189ac6a25ea543214305c4c8fb3bc2ceea8c34600361b03527578133",
"0x235dbcd87b398a707c229dbe02d3a42b118128a38ac266ff2d77c729c8c24d42",
);
}

#[test]
fn test_module_hash() {
assert_crypto_hash(
&StateKey::module(&AccountAddress::TWO, ident_str!("mymodule")),
"83d33b345c5e4b25d8f4dfe2b98b492024313b3b6e4febea6bfa844dbd850200",
"2b1fbcded1092bfbd49306ed6f39766c6235b3c52d412965668b600bc874e119",
);
}

Expand All @@ -58,21 +58,21 @@ fn test_debug() {
let key = StateKey::module(&AccountAddress::ONE, ident_str!("account"));
assert_eq!(
&format!("{:?}", key),
"StateKey::AccessPath { address: 0x1, path: \"Code(0x00000000000000000000000000000001::account)\" }",
"StateKey::AccessPath { address: 0x1, path: \"Code(0x1::account)\" }",
);

// resource
let key = StateKey::resource_typed::<AccountResource>(&AccountAddress::FOUR).unwrap();
assert_eq!(
&format!("{:?}", key),
"StateKey::AccessPath { address: 0x4, path: \"Resource(0x00000000000000000000000000000001::account::Account)\" }",
"StateKey::AccessPath { address: 0x4, path: \"Resource(0x1::account::Account)\" }",
);

// resource group
let key = StateKey::resource_group(&AccountAddress::THREE, &ObjectGroupResource::struct_tag());
assert_eq!(
&format!("{:?}", key),
"StateKey::AccessPath { address: 0x3, path: \"ResourceGroup(0x00000000000000000000000000000001::object::ObjectGroup)\" }",
"StateKey::AccessPath { address: 0x3, path: \"ResourceGroup(0x1::object::ObjectGroup)\" }",
);

// table item
Expand Down
38 changes: 17 additions & 21 deletions vm/types/src/unit_tests/access_path_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ fn test_data_type() {

#[test]
fn test_access_path_str_valid() {
let r1 = format!(
"{}/1/0x00000000000000000000000000000001::Account::Account",
AccountAddress::random()
);
let test_cases = vec!["0x00000000000000000000000000000000/0/Account",
"0x00000000000000000000000000000001/0/Account",
"0x00000000000000000000000000000001/1/0x00000000000000000000000000000001::Account::Account",
"0x00000000000000000000000000000001/1/0x00000000000000000000000000000001::Account::Balance<0x00000000000000000000000000000001::STC::STC>",
r1.as_str()];
let r1 = format!("{}/1/0x1::Account::Account", AccountAddress::random());
let test_cases = vec![
"0x0/0/Account",
"0x1/0/Account",
"0x1/1/0x1::Account::Account",
"0x1/1/0x1::Account::Balance<0x1::STC::STC>",
r1.as_str(),
];
for case in test_cases {
let access_path = AccessPath::from_str(case).unwrap();
assert_eq!(case.to_owned(), access_path.to_string())
Expand All @@ -33,22 +32,19 @@ fn test_access_path_str_valid() {
#[test]
fn test_access_path_str_invalid() {
//invalid address
let r1 = format!(
"{}00/1/0x00000000000000000000000000000001::Account::Account",
AccountAddress::random()
);
let r1 = format!("{}00/1/0x1::Account::Account", AccountAddress::random());
let test_cases = vec![
// invalid struct tag
"0x00000000000000000000000000000001/1/Account",
"0x1/1/Account",
// invalid module name
"0x00000000000000000000000000000001/0/0x00000000000000000000000000000001::Account::Account",
"0x1/0/0x1::Account::Account",
//invalid data type
"0x00000000000000000000000000000001/3/Account",
"0x1/3/Account",
//too many `/`
"0x00000000000000000000000000000001/0/Account/xxx",
"0x00000000000000000000000000000001/0//Account",
"0x1/0/Account/xxx",
"0x1/0//Account",
//too less '`'
"0x00000000000000000000000000000001/1",
"0x1/1",
r1.as_str(),
];
for case in test_cases {
Expand All @@ -63,13 +59,13 @@ fn test_access_path_str_invalid() {

#[test]
fn test_bad_case_from_protest() {
let raw_path = "0x00000000000000000000000000000001/1/0x00000000000000000000000000000001::a::A_";
let raw_path = "0x1/1/0x1::a::A_";
let access_path = AccessPath::from_str(raw_path);
assert!(access_path.is_ok());

//The module name start with '_' will encounter parse error
//This may be the parser error, or the identity's arbitrary error
let raw_path = "0x00000000000000000000000000000001/1/0x00000000000000000000000000000001::_a::A";
let raw_path = "0x1/1/0x1::_a::A";
let access_path = AccessPath::from_str(raw_path);
assert!(access_path.is_err());
}
Expand Down
Loading