Skip to content

Commit d208d08

Browse files
authored
feat: updating ZOS over the chain (#385)
1 parent 36addb5 commit d208d08

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

substrate-node/pallets/pallet-tfgrid/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ pub mod pallet {
180180
#[pallet::getter(fn pallet_version)]
181181
pub type PalletVersion<T> = StorageValue<_, types::StorageVersion, ValueQuery>;
182182

183+
#[pallet::storage]
184+
#[pallet::getter(fn zos_version)]
185+
pub type ZosVersion<T> = StorageValue<_, Vec<u8>, ValueQuery>;
186+
183187
#[pallet::config]
184188
pub trait Config: frame_system::Config + pallet_timestamp::Config {
185189
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
@@ -250,6 +254,8 @@ pub mod pallet {
250254
FarmingPolicyUpdated(types::FarmingPolicy<T::BlockNumber>),
251255
FarmingPolicySet(u32, Option<FarmingPolicyLimit>),
252256
FarmCertificationSet(u32, FarmCertification),
257+
258+
ZosVersionUpdated(Vec<u8>),
253259
}
254260

255261
#[pallet::error]
@@ -315,6 +321,8 @@ pub mod pallet {
315321
FarmNameTooShort,
316322
FarmNameTooLong,
317323
MethodIsDeprecated,
324+
325+
InvalidZosVersion,
318326
}
319327

320328
#[pallet::genesis_config]
@@ -1724,6 +1732,25 @@ pub mod pallet {
17241732

17251733
Ok(().into())
17261734
}
1735+
1736+
#[pallet::weight(100_000_000 + T::DbWeight::get().writes(1) + T::DbWeight::get().reads(1))]
1737+
pub fn set_zos_version(
1738+
origin: OriginFor<T>,
1739+
zos_version: Vec<u8>,
1740+
) -> DispatchResultWithPostInfo {
1741+
T::RestrictedOrigin::ensure_origin(origin)?;
1742+
1743+
ensure!(
1744+
ZosVersion::<T>::get() != zos_version,
1745+
Error::<T>::InvalidZosVersion
1746+
);
1747+
1748+
ZosVersion::<T>::put(&zos_version);
1749+
1750+
Self::deposit_event(Event::ZosVersionUpdated(zos_version));
1751+
1752+
Ok(().into())
1753+
}
17271754
}
17281755
}
17291756

substrate-node/pallets/pallet-tfgrid/src/tests.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use crate::{mock::*, Error};
1+
use super::Event as TfgridEvent;
2+
use crate::{mock::Event as MockEvent, mock::*, Error};
23
use frame_support::{assert_noop, assert_ok, BoundedVec};
3-
use frame_system::RawOrigin;
4+
use frame_system::{EventRecord, Phase, RawOrigin};
5+
use sp_core::H256;
46
use tfchain_support::types::{
57
FarmCertification, FarmingPolicyLimit, Location, NodeCertification, PublicConfig, PublicIP,
68
Resources,
@@ -1308,6 +1310,28 @@ fn test_create_and_update_policy() {
13081310
});
13091311
}
13101312

1313+
#[test]
1314+
fn test_set_zos_version() {
1315+
ExternalityBuilder::build().execute_with(|| {
1316+
let zos_version = "1.0.0".as_bytes().to_vec();
1317+
assert_ok!(TfgridModule::set_zos_version(
1318+
RawOrigin::Root.into(),
1319+
zos_version.clone(),
1320+
));
1321+
1322+
let saved_zos_version = TfgridModule::zos_version();
1323+
assert_eq!(saved_zos_version, zos_version);
1324+
1325+
let our_events = System::events();
1326+
assert_eq!(
1327+
our_events.contains(&record(MockEvent::TfgridModule(
1328+
TfgridEvent::<TestRuntime>::ZosVersionUpdated(zos_version)
1329+
))),
1330+
true
1331+
);
1332+
})
1333+
}
1334+
13111335
fn create_entity() {
13121336
let name = "foobar".as_bytes().to_vec();
13131337
let country = "Belgium".as_bytes().to_vec();
@@ -1488,3 +1512,11 @@ fn create_farming_policies() {
14881512
FarmCertification::NotCertified,
14891513
));
14901514
}
1515+
1516+
fn record(event: Event) -> EventRecord<Event, H256> {
1517+
EventRecord {
1518+
phase: Phase::Initialization,
1519+
event,
1520+
topics: vec![],
1521+
}
1522+
}

0 commit comments

Comments
 (0)