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

Fixed json deserialization of array fuel types from the file #643

Merged
merged 8 commits into from
Nov 28, 2023
Merged
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed

- [#643](https://github.com/FuelLabs/fuel-vm/pull/643): Fixed json deserialization of array fuel types from the file.

## [Version 0.43.0]

### Changed
Expand Down
3 changes: 2 additions & 1 deletion fuel-tx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ strum_macros = { version = "0.24", optional = true }
[dev-dependencies]
bincode = { workspace = true }
fuel-crypto = { workspace = true, default-features = false, features = ["random"] }
fuel-tx = { path = ".", features = ["builder", "random"] }
fuel-tx = { path = ".", features = ["builder", "random", "serde"] }
fuel-tx-test-helpers = { path = "test-helpers" }
fuel-types = { workspace = true, default-features = false, features = ["random"] }
hex = { version = "0.4", default-features = false }
Expand All @@ -38,6 +38,7 @@ quickcheck = "1.0"
quickcheck_macros = "1.0"
rand = { version = "0.8", default-features = false, features = ["std_rng"] }
rstest = "0.15"
serde_json = { version = "1.0" }

[features]
default = ["fuel-asm/default", "fuel-crypto/default", "fuel-merkle/default", "fuel-types/default", "std"]
Expand Down
40 changes: 40 additions & 0 deletions fuel-tx/src/transaction/types/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,43 @@ impl Ord for StorageSlot {
self.key.cmp(&other.key)
}
}

#[cfg(test)]
mod tests {
use super::*;
use rand::SeedableRng;
use std::{
fs::File,
path::PathBuf,
};

const FILE_PATH: &str = "storage-slots.json";

#[test]
fn test_storage_slot_serialization() {
let rng = &mut rand::rngs::StdRng::seed_from_u64(8586);
let key: Bytes32 = rng.gen();
let value: Bytes32 = rng.gen();

let slot = StorageSlot::new(key, value);
let slots = vec![slot.clone()];

// `from_str` works
let slot_str = serde_json::to_string(&slots).expect("to string");
let storage_slots: Vec<StorageSlot> =
serde_json::from_str(&slot_str).expect("read from string");
assert_eq!(storage_slots.len(), 1);

let path = std::env::temp_dir().join(PathBuf::from(FILE_PATH));

// writes to file works
let storage_slots_file = File::create(&path).expect("create file");
serde_json::to_writer(&storage_slots_file, &slots).expect("write file");

// `from_reader` works
let storage_slots_file = File::open(&path).expect("open file");
let storage_slots: Vec<StorageSlot> =
serde_json::from_reader(storage_slots_file).expect("read file");
assert_eq!(storage_slots.len(), 1);
}
}
3 changes: 2 additions & 1 deletion fuel-types/src/array_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ macro_rules! key_methods {
{
use serde::de::Error;
if deserializer.is_human_readable() {
let s: &str = serde::Deserialize::deserialize(deserializer)?;
let s: alloc::string::String =
serde::Deserialize::deserialize(deserializer)?;
s.parse().map_err(D::Error::custom)
} else {
let bytes = deserializer.deserialize_bytes(BytesVisitor {})?;
Expand Down
Loading