Skip to content

Commit

Permalink
Fixed json deserialization of array fuel types from the file (#643)
Browse files Browse the repository at this point in the history
* Repro of storage slot deserialization bug

* from string works

* Fix compilation

* Fix compilation

* Fix the issue with deserialization

* Fixed the issue with deseriaklization form the file

* clean up after test

* We don't need to clean in temporary folder

---------

Co-authored-by: xgreenx <xgreenx9999@gmail.com>
  • Loading branch information
sdankel and xgreenx authored Nov 28, 2023
1 parent b560a55 commit 80fb4b1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
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

0 comments on commit 80fb4b1

Please sign in to comment.