Skip to content

Commit

Permalink
include move_resource_v5
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Oct 6, 2024
1 parent ac5c688 commit 9ed6ef0
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 96 deletions.
33 changes: 6 additions & 27 deletions compatibility/src/version_five/account_blob_v5.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0
#![allow(dead_code)]

#[allow(dead_code)]
use crate::version_five::move_resource_v5::MoveResourceV5;
use crate::version_five::{
core_account_v5::AccountResourceV5, diem_account_v5::DiemAccountResourceV5,
language_storage_v5::StructTagV5, legacy_address_v5::LegacyAddressV5,
};
use crate::version_five::move_resource_v5::MoveResourceV5;
use anyhow::{bail, Context, Result};
use diem_crypto::{
hash::{CryptoHash, CryptoHasher},
HashValue,
};

use diem_crypto_derive::CryptoHasher;
// use move_core_types::language_storage::StructTag;
// use move_core_types::move_resource::MoveResource;
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::BTreeMap;

Expand All @@ -24,26 +22,18 @@ pub struct AccountStateV5(pub BTreeMap<Vec<u8>, Vec<u8>>);

impl AccountStateV5 {
pub fn get_resource_data<T: MoveResourceV5>(&self) -> Result<&[u8]> {
// NOTE: don't forget access_vector: has a byte prepended
let struct_tag = T::struct_tag();
// NOTE: when encoding you should use access_vector: since there's a magic a bit prepended to flag resources.

// let legacy_struct_tag = convert_to_legacy(&struct_tag)?;
let key = struct_tag.access_vector();
let key = T::struct_tag().access_vector();

let errmsg = format!(
"could not find in btree type {}",
T::struct_tag().to_canonical_string()
"could not find in btree type {:?}",
&T::struct_tag().module_id()
);

Ok(self.0.get(&key).context(errmsg)?)
}

// pub fn find_bytes_struct_tag(&self, s: &StructTagV5) -> Result<&[u8]> {
// let errmsg = format!("could not find in btree type {}", s.to_canonical_string());
// let key = s.access_vector();
// Ok(self.0.get(&key).context(errmsg)?)
// }

pub fn find_bytes_legacy_struct_tag_v5(
&self,
legacy_struct_tag: &StructTagV5,
Expand Down Expand Up @@ -127,14 +117,3 @@ impl CryptoHash for AccountStateBlob {
self.hash
}
}

// pub fn convert_to_legacy(s: &StructTag) -> Result<StructTagV5> {
// let legacy_address = LegacyAddressV5::from_hex_literal(&s.address.to_hex_literal())?;

// Ok(StructTagV5 {
// address: legacy_address,
// module: s.module.clone(),
// name: s.name.clone(),
// type_params: vec![], // TODO // s.type_params.clone(),
// })
// }
23 changes: 4 additions & 19 deletions compatibility/src/version_five/balance_v5.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use move_core_types::account_address::AccountAddress;
use move_core_types::{
ident_str,
identifier::IdentStr,
// language_storage::{StructTagV5, TypeTag},
move_resource::{MoveResource, MoveStructType},
};
use serde::{Deserialize, Serialize};
use crate::version_five::language_storage_v5::{StructTagV5, TypeTagV5};
use crate::version_five::legacy_address_v5::{LegacyAddressV5, LEGACY_CORE_CODE_ADDRESS};
use crate::version_five::move_resource_v5::MoveStructTypeV5;
use crate::version_five::legacy_address_v5::LEGACY_CORE_CODE_ADDRESS;
use crate::version_five::move_resource_v5::MoveResourceV5;
use crate::version_five::move_resource_v5::MoveStructTypeV5;
use move_core_types::{ident_str, identifier::IdentStr};
use serde::{Deserialize, Serialize};
/// The balance resource held under an account.
#[derive(Debug, Serialize, Deserialize)]
pub struct BalanceResourceV5 {
Expand Down Expand Up @@ -44,12 +38,3 @@ impl MoveStructTypeV5 for BalanceResourceV5 {
}

impl MoveResourceV5 for BalanceResourceV5 {}

// pub fn xus_tag() -> TypeTag {
// TypeTag::Struct(Box::new(StructTagV5 {
// address: AccountAddress::ONE,
// module: ident_str!("GAS").into(),
// name: ident_str!("GAS").into(),
// type_params: vec![],
// }))
// }
10 changes: 4 additions & 6 deletions compatibility/src/version_five/core_account_v5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use move_core_types::ident_str;
use move_core_types::{
identifier::IdentStr,
move_resource::{MoveResource, MoveStructType},
};
use move_core_types::identifier::IdentStr;
use serde::{Deserialize, Serialize};

use super::legacy_address_v5::LegacyAddressV5;
use super::move_resource_v5::{MoveResourceV5, MoveStructTypeV5};

pub const CORE_ACCOUNT_MODULE_IDENTIFIER: &IdentStr = ident_str!("Account");

Expand Down Expand Up @@ -61,9 +59,9 @@ impl AccountResourceV5 {
// }
}

impl MoveStructType for AccountResourceV5 {
impl MoveStructTypeV5 for AccountResourceV5 {
const MODULE_NAME: &'static IdentStr = CORE_ACCOUNT_MODULE_IDENTIFIER;
const STRUCT_NAME: &'static IdentStr = CORE_ACCOUNT_MODULE_IDENTIFIER;
}

impl MoveResource for AccountResourceV5 {}
impl MoveResourceV5 for AccountResourceV5 {}
7 changes: 2 additions & 5 deletions compatibility/src/version_five/diem_account_v5.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::version_five::event_v5::EventHandleV5;
use crate::version_five::language_storage_v5::StructTagV5;
use crate::version_five::legacy_address_v5::LegacyAddressV5;
use crate::version_five::event_v5::EventHandleV5;

use move_core_types::ident_str;
use move_core_types::{
identifier::IdentStr,
};
use move_core_types::identifier::IdentStr;
use serde::{Deserialize, Serialize};

use super::legacy_address_v5::LEGACY_CORE_CODE_ADDRESS;
use super::move_resource_v5::{MoveResourceV5, MoveStructTypeV5};


/// The Identifier for the Account module.
pub const DIEM_ACCOUNT_MODULE_IDENTIFIER: &IdentStr = ident_str!("DiemAccount");
/// A Rust representation of an Account resource.
Expand Down
4 changes: 2 additions & 2 deletions compatibility/src/version_five/hash_value_v5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ impl HashValue {

/// Full hex representation of a given hash value.
pub fn to_hex(&self) -> String {

Check failure on line 252 in compatibility/src/version_five/hash_value_v5.rs

View workflow job for this annotation

GitHub Actions / clippy

methods with the following characteristics: (`to_*` and `self` type is `Copy`) usually take `self` by value
format!("{:x}", self)
format!("{:x}", &self)
}

/// Full hex representation of a given hash value with `0x` prefix.
pub fn to_hex_literal(&self) -> String {

Check failure on line 257 in compatibility/src/version_five/hash_value_v5.rs

View workflow job for this annotation

GitHub Actions / clippy

methods with the following characteristics: (`to_*` and `self` type is `Copy`) usually take `self` by value
format!("{:#x}", self)
format!("{:#x}", &self)
}

/// Parse a given hex string to a hash value.
Expand Down
2 changes: 2 additions & 0 deletions compatibility/src/version_five/language_storage_v5.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0
#![allow(dead_code)]

use crate::version_five::{legacy_address_v5::LegacyAddressV5, safe_serialize_v5};
use move_core_types::identifier::{IdentStr, Identifier};
use serde::{Deserialize, Serialize};
Expand Down
18 changes: 13 additions & 5 deletions compatibility/src/version_five/state_snapshot_v5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ fn decode_record_from_string() {

let acc_state: AccountStateV5 = bcs::from_bytes(&b.blob).unwrap();

// We should be able to get the resource directly using a method on AccountStateV5.
// We should be able to get the resource directly using a method on AccountStateV5.
// Among other things, a FreezingBit resource would be found on a backup
// record. Note it is the simplest structure did not use an AccountAddress
// for example, which makes it easier to check.

let s = acc_state.get_resource::<FreezingBit>().unwrap();
assert!(s.is_frozen() == false);
assert!(!s.is_frozen());

// Sanity check that our access_path_vector we generate
// can be used to access a value in the K-V store
Expand All @@ -135,14 +135,11 @@ fn decode_record_from_string() {
let res: BalanceResourceV5 = bcs::from_bytes(b).unwrap();
assert!(res.coin() == 1000000);



// Finally a DiemAccount resource should be found in this payload.
// This is the most complex structure, since involves some
// nested types like EventHandle and WithdrawCapability
let ar = acc_state.get_account_resource().unwrap();
assert!(ar.sequence_number() == 0);

}

#[test]
Expand Down Expand Up @@ -206,3 +203,14 @@ fn decode_encode_v5_struct_tag() {
// "resource keys" have prepended 01
assert!(hex::encode(s.access_vector()) == patch_expected_key);
}

#[test]
fn nested_generic_structs() {
use crate::version_five::balance_v5::BalanceResourceV5;
use crate::version_five::move_resource_v5::MoveStructTypeV5;
// This is the balance resource access_path as vector in storage
let balance_key = hex::decode("01000000000000000000000000000000010b4469656d4163636f756e740742616c616e6365010700000000000000000000000000000001034741530347415300").unwrap();

let vec = BalanceResourceV5::struct_tag().access_vector();
assert!(balance_key == vec);
}
58 changes: 26 additions & 32 deletions compatibility/tests/version_five_read_snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
use std::path::PathBuf;

use libra_backwards_compatibility::version_five::{
freezing_v5::FreezingBit,
balance_v5::BalanceResourceV5,
state_snapshot_v5::{
v5_read_from_snapshot_manifest,
v5_accounts_from_snapshot_backup
}};
use libra_types::move_resource::cumulative_deposits::LegacyBalanceResourceV6;
balance_v5::BalanceResourceV5,
freezing_v5::FreezingBit,
state_snapshot_v5::{v5_accounts_from_snapshot_backup, v5_read_from_snapshot_manifest},
};

fn fixtures_path() -> PathBuf {
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
p.push("fixtures/v5/state_ver_119757649.17a8");
assert!(p.exists());
p
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
p.push("fixtures/v5/state_ver_119757649.17a8");
assert!(p.exists());
p
}



#[test]
fn read_snapshot_manifest() {
let mut p = fixtures_path();
p.push("state.manifest");
assert!(p.exists());
let mut p = fixtures_path();
p.push("state.manifest");
assert!(p.exists());

let res = v5_read_from_snapshot_manifest(&p).unwrap();
let res = v5_read_from_snapshot_manifest(&p).unwrap();

assert!(res.version == 119757649);
assert!(res.version == 119757649);
}

#[tokio::test]
async fn read_full_snapshot() -> anyhow::Result<()>{
let mut p = fixtures_path();
p.push("state.manifest");
async fn read_full_snapshot() -> anyhow::Result<()> {
let mut p = fixtures_path();
p.push("state.manifest");

let man = v5_read_from_snapshot_manifest(&p)?;
let archive_path = fixtures_path();
let accts = v5_accounts_from_snapshot_backup(man, &archive_path).await?;
let man = v5_read_from_snapshot_manifest(&p)?;
let archive_path = fixtures_path();
let accts = v5_accounts_from_snapshot_backup(man, &archive_path).await?;

assert!(accts.len() == 17339);
assert!(accts.len() == 17339);

let first_account = accts[0].to_account_state()?;
let f = first_account.get_resource::<FreezingBit>()?;
assert!(f.is_frozen() == false);
let b = first_account.get_resource::<BalanceResourceV5>()?;
// assert!(f.is_frozen() == false);
dbg!(&b);
let first_account = accts[0].to_account_state()?;
let f = first_account.get_resource::<FreezingBit>()?;
assert!(!f.is_frozen());
let b = first_account.get_resource::<BalanceResourceV5>()?;
assert!(b.coin() == 100135989588);

Ok(())
Ok(())
}

0 comments on commit 9ed6ef0

Please sign in to comment.