-
Notifications
You must be signed in to change notification settings - Fork 86
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
pallet-collection-data-feed
implementation
#1324
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a1addfb
add price trait & mock
lemunozm 591f924
add pallet-pool-price-feed basic structure
lemunozm e0534db
add OnNewData trait
lemunozm 0d2f18d
add PriceRegistry trait implementation
lemunozm 1702c37
finish implementation
lemunozm c0c6026
using a BTreeMap
lemunozm f8c5249
counter to Listening storage
lemunozm 81391c4
clippy
lemunozm 448a506
rename cache to collection
lemunozm 6e21fe8
rename pallet and price by data
lemunozm c80e940
add tests
lemunozm 80e75ff
test simplification
lemunozm 553288c
fix clippy
lemunozm 3f9dade
simplify trait
lemunozm dbeb481
Add doc intro to lib
lemunozm 6e49ab8
minor function rename
lemunozm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use cfg_traits::data::{DataCollection, DataRegistry}; | ||
use frame_support::pallet_prelude::*; | ||
use mock_builder::{execute_call, register_call}; | ||
|
||
#[pallet::config] | ||
pub trait Config: frame_system::Config { | ||
type DataId; | ||
type CollectionId; | ||
type Collection: DataCollection<Self::DataId, Self::Data, Self::Moment>; | ||
type Data; | ||
type Moment; | ||
} | ||
|
||
#[pallet::pallet] | ||
#[pallet::generate_store(pub(super) trait Store)] | ||
pub struct Pallet<T>(_); | ||
|
||
#[pallet::storage] | ||
pub(super) type CallIds<T: Config> = StorageMap< | ||
_, | ||
Blake2_128Concat, | ||
<Blake2_128 as frame_support::StorageHasher>::Output, | ||
mock_builder::CallId, | ||
>; | ||
|
||
impl<T: Config> Pallet<T> { | ||
pub fn mock_get(f: impl Fn(&T::DataId) -> Option<(T::Data, T::Moment)> + 'static) { | ||
register_call!(f); | ||
} | ||
|
||
pub fn mock_cache(f: impl Fn(&T::CollectionId) -> T::Collection + 'static) { | ||
register_call!(f); | ||
} | ||
|
||
pub fn mock_register_data_id( | ||
f: impl Fn(&T::DataId, &T::CollectionId) -> DispatchResult + 'static, | ||
) { | ||
register_call!(move |(a, b)| f(a, b)); | ||
} | ||
|
||
pub fn mock_unregister_data_id( | ||
f: impl Fn(&T::DataId, &T::CollectionId) -> DispatchResult + 'static, | ||
) { | ||
register_call!(move |(a, b)| f(a, b)); | ||
} | ||
} | ||
|
||
impl<T: Config> DataRegistry for Pallet<T> { | ||
type Collection = T::Collection; | ||
type CollectionId = T::CollectionId; | ||
type Data = T::Data; | ||
type DataId = T::DataId; | ||
type Moment = T::Moment; | ||
|
||
fn get(a: &T::DataId) -> Option<(T::Data, T::Moment)> { | ||
let a = unsafe { std::mem::transmute::<_, &'static T::DataId>(a) }; | ||
execute_call!(a) | ||
} | ||
|
||
fn collection(a: &T::CollectionId) -> T::Collection { | ||
let a = unsafe { std::mem::transmute::<_, &'static T::CollectionId>(a) }; | ||
execute_call!(a) | ||
} | ||
|
||
fn register_data_id(a: &T::DataId, b: &T::CollectionId) -> DispatchResult { | ||
let a = unsafe { std::mem::transmute::<_, &'static T::DataId>(a) }; | ||
let b = unsafe { std::mem::transmute::<_, &'static T::CollectionId>(b) }; | ||
execute_call!((a, b)) | ||
} | ||
|
||
fn unregister_data_id(a: &T::DataId, b: &T::CollectionId) -> DispatchResult { | ||
let a = unsafe { std::mem::transmute::<_, &'static T::DataId>(a) }; | ||
let b = unsafe { std::mem::transmute::<_, &'static T::CollectionId>(b) }; | ||
execute_call!((a, b)) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
pub mod util { | ||
use std::collections::HashMap; | ||
|
||
use super::*; | ||
|
||
pub type Value<T> = (<T as Config>::Data, <T as Config>::Moment); | ||
pub struct MockDataCollection<T: Config>(pub HashMap<T::DataId, Option<Value<T>>>); | ||
|
||
impl<T: Config> DataCollection<T::DataId, T::Data, T::Moment> for MockDataCollection<T> | ||
where | ||
T::DataId: std::hash::Hash + Eq, | ||
T::Data: Clone, | ||
T::Moment: Clone, | ||
{ | ||
fn get(&self, data_id: &T::DataId) -> Result<Option<Value<T>>, DispatchError> { | ||
Ok(self | ||
.0 | ||
.get(data_id) | ||
.ok_or(DispatchError::CannotLookup)? | ||
.clone()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use sp_runtime::{DispatchError, DispatchResult}; | ||
|
||
/// Abstraction that represents a storage where | ||
/// you can subscribe to data updates and collect them | ||
pub trait DataRegistry { | ||
/// A data identification | ||
type DataId; | ||
|
||
/// A collection identification | ||
type CollectionId; | ||
|
||
/// A collection of datas | ||
type Collection: DataCollection<Self::DataId, Self::Data, Self::Moment>; | ||
|
||
/// Represents a data | ||
type Data; | ||
|
||
/// Represents a timestamp | ||
type Moment; | ||
|
||
/// Return the last data value for a data id along with the moment it was updated last time | ||
fn get(data_id: &Self::DataId) -> Option<(Self::Data, Self::Moment)>; | ||
|
||
/// Retrives a collection of datas with all datas associated to a collection id | ||
fn collection(collection_id: &Self::CollectionId) -> Self::Collection; | ||
|
||
/// Start listening data changes for a data id in a collection id | ||
fn register_data_id( | ||
data_id: &Self::DataId, | ||
collection_id: &Self::CollectionId, | ||
) -> DispatchResult; | ||
|
||
/// Start listening data changes for a data id in a collection id | ||
fn unregister_data_id( | ||
data_id: &Self::DataId, | ||
collection_id: &Self::CollectionId, | ||
) -> DispatchResult; | ||
} | ||
|
||
/// Abstration to represent a collection of datas in memory | ||
pub trait DataCollection<DataId, Data, Moment> { | ||
/// Return the last data value for a data id along with the moment it was updated last time | ||
fn get(&self, data_id: &DataId) -> Result<Option<(Data, Moment)>, DispatchError>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
[package] | ||
authors = ["Centrifuge <admin@centrifuge.io>"] | ||
description = "Pallet to collect data from a feeder entity" | ||
edition = "2021" | ||
license = "LGPL-3.0" | ||
name = "pallet-collection-data-feed" | ||
repository = "https://github.com/centrifuge/centrifuge-chain" | ||
version = "1.0.0" | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", default-features = false, version = "3.0.0", features = ["derive"] } | ||
scale-info = { version = "2.3.0", default-features = false, features = ["derive"] } | ||
|
||
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.37" } | ||
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.37" } | ||
sp-arithmetic = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.37" } | ||
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.37" } | ||
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.37" } | ||
|
||
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", default-features = true, branch = "polkadot-v0.9.37" } | ||
|
||
cfg-traits = { path = "../../libs/traits", default-features = false } | ||
|
||
[dev-dependencies] | ||
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" } | ||
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" } | ||
|
||
orml-oracle = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.37" } | ||
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"scale-info/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"sp-arithmetic/std", | ||
"sp-runtime/std", | ||
"sp-std/std", | ||
"cfg-traits/std", | ||
"orml-traits/std", | ||
] | ||
runtime-benchmarks = [ | ||
"frame-support/runtime-benchmarks", | ||
"frame-system/runtime-benchmarks", | ||
"sp-runtime/runtime-benchmarks", | ||
"cfg-traits/runtime-benchmarks", | ||
] | ||
try-runtime = [ | ||
"frame-support/try-runtime", | ||
"frame-system/try-runtime", | ||
"cfg-traits/try-runtime", | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have the same for interest rate right?
Would it make sense to merge them into a common collection trait?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be great, but the parameters differ a little bit, the
RateCollection
uses two:interest_rate
andnormalized_debt
, and in this case we only need adata_id
. Maybe in a future refactor once #1310 is used instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've simplified the trait to allow in the future mixing
DataCollection
for this and interest accrual.