Skip to content

Commit

Permalink
setting record test
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Mar 27, 2024
1 parent e750d68 commit 8d14b73
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ futures = { version = "0.3.29" }
jsonrpsee = { version = "0.16.3" }
color-print = "0.3.4"
parity-scale-codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
hex = { version = "0.4.3" }
hex-literal = { version = "0.4.1" }
log = { version = "0.4.20", default-features = false }
scale-info = { version = "2.9.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions pallets/regions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sp-core = { workspace = true, default-features = false }
sp-runtime = { workspace = true, default-features = false }

[dev-dependencies]
hex = { workspace = true }
serde = { workspace = true }
pallet-balances = { workspace = true, default-features = false }
ismp-testsuite = { workspace = true }
Expand Down
6 changes: 5 additions & 1 deletion pallets/regions/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]

use core::cmp::max;
use frame_support::PalletId;
use ismp::{
consensus::StateMachineId,
Expand Down Expand Up @@ -262,7 +263,10 @@ impl<T: Config> IsmpModule for IsmpModuleCallback<T> {
res.get.keys.iter().try_for_each(|key| {
let value = Self::read_value(&res.values, &key)?;

let region_id = RegionId::decode(&mut key.as_slice()).map_err(|_| {
// The last 16 bytes represent the region id.
let mut region_id_encoded = &key[max(0, key.len() as usize - 16) as usize..];

let region_id = RegionId::decode(&mut region_id_encoded).map_err(|_| {
IsmpError::ImplementationSpecific("Failed to decode region_id".to_string())
})?;

Expand Down
20 changes: 15 additions & 5 deletions pallets/regions/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ismp::{
module::IsmpModule,
router::{DispatchRequest, Get, GetResponse, Response},
};
use pallet_broker::{CoreMask, RegionId};
use pallet_broker::{CoreMask, RegionId, RegionRecord};
use std::collections::BTreeMap;

#[test]
Expand Down Expand Up @@ -48,22 +48,32 @@ fn setting_record_works() {

assert_eq!(request.who, 2);

let mock_record: RegionRecord<u64, u64> = RegionRecord { end: 42, owner: 1, paid: None };

let mock_response = Response::Get(GetResponse {
get: Get {
source: <Test as Config>::CoretimeChain::get(),
dest: get.dest,
nonce: 0,
from: get.from,
keys: get.keys,
keys: get.keys.clone(),
height: get.height,
timeout_timestamp: <Test as Config>::Timeout::get(),
gas_limit: 0,
},
// TODO: value
values: BTreeMap::from([(vec![], None)]),
values: BTreeMap::from([(get.keys[0].clone(), Some(mock_record.encode()))]),
});

let module: IsmpModuleCallback<Test> = IsmpModuleCallback::default();
let _ = module.on_response(mock_response);
assert_ok!(module.on_response(mock_response));

assert_eq!(
Regions::regions(&region_id).unwrap(),
Region {
owner: 2,
record: Some(mock_record),
request_status: RequestStatus::Successful
}
);
});
}

0 comments on commit 8d14b73

Please sign in to comment.