Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
open-runtime-module-library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Noxim
open-runtime-module-library
Commits
4c914772
Unverified
Commit
4c914772
authored
5 years ago
by
Xiliang Chen
Committed by
GitHub
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
make default combine data configurable (#58)
parent
5221fdbb
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
oracle/src/default_combine_data.rs
+15
-27
15 additions, 27 deletions
oracle/src/default_combine_data.rs
oracle/src/mock.rs
+6
-1
6 additions, 1 deletion
oracle/src/mock.rs
with
21 additions
and
28 deletions
oracle/src/default_combine_data.rs
+
15
−
27
View file @
4c914772
use
frame_support
::{
parameter_types
,
traits
::{
Get
,
Time
},
};
use
frame_support
::
traits
::{
Get
,
Time
};
use
orml_traits
::
CombineData
;
use
rstd
::
prelude
::
Vec
;
use
rstd
::
prelude
::
*
;
use
crate
::{
MomentOf
,
TimestampedValueOf
,
Trait
};
/// Sort by value and returns median timestamped value.
/// Returns prev_value if not enough valid values.
pub
struct
DefaultCombineData
<
T
:
Trait
>
(
rstd
::
marker
::
PhantomData
<
T
>
);
impl
<
T
:
Trait
>
CombineData
<
T
::
OracleKey
,
TimestampedValueOf
<
T
>>
for
DefaultCombineData
<
T
>
{
pub
struct
DefaultCombineData
<
T
,
MinimumCount
,
ExpiresIn
>
(
rstd
::
marker
::
PhantomData
<
(
T
,
MinimumCount
,
ExpiresIn
)
>
);
impl
<
T
,
MinimumCount
,
ExpiresIn
>
CombineData
<
T
::
OracleKey
,
TimestampedValueOf
<
T
>>
for
DefaultCombineData
<
T
,
MinimumCount
,
ExpiresIn
>
where
T
:
Trait
,
MinimumCount
:
Get
<
u32
>
,
ExpiresIn
:
Get
<
MomentOf
<
T
>>
,
{
fn
combine_data
(
_key
:
&
T
::
OracleKey
,
values
:
Vec
<
TimestampedValueOf
<
T
>>
,
prev_value
:
Option
<
TimestampedValueOf
<
T
>>
,
)
->
Option
<
TimestampedValueOf
<
T
>>
{
let
expires_in
:
MomentOf
<
T
>
=
<
Self
as
Parameters
<
MomentOf
<
T
>>>
::
e
xpires
_i
n
::
get
()
.into
()
;
let
expires_in
=
E
xpires
I
n
::
get
();
let
now
=
T
::
Time
::
now
();
let
mut
valid_values
=
values
.into_iter
()
...
...
@@ -29,8 +32,8 @@ impl<T: Trait> CombineData<T::OracleKey, TimestampedValueOf<T>> for DefaultCombi
})
.collect
::
<
Vec
<
TimestampedValueOf
<
T
>>>
();
let
count
=
valid_values
.len
();
let
minimum_count
=
<
Self
as
Parameters
<
MomentOf
<
T
>>>
::
m
inimum
_c
ount
::
get
();
let
count
=
valid_values
.len
()
as
u32
;
let
minimum_count
=
M
inimum
C
ount
::
get
();
if
count
<
minimum_count
{
return
prev_value
;
}
...
...
@@ -38,21 +41,6 @@ impl<T: Trait> CombineData<T::OracleKey, TimestampedValueOf<T>> for DefaultCombi
valid_values
.sort_by
(|
a
,
b
|
a
.value
.cmp
(
&
b
.value
));
let
median_index
=
count
/
2
;
return
Some
(
valid_values
[
median_index
]
.clone
());
return
Some
(
valid_values
[
median_index
as
usize
]
.clone
());
}
}
parameter_types!
{
pub
const
MinimumCount
:
usize
=
3
;
pub
const
ExpiresIn
:
u32
=
600
;
}
trait
Parameters
<
Moment
>
{
type
minimum_count
:
Get
<
usize
>
;
type
expires_in
:
Get
<
Moment
>
;
}
impl
<
T
:
Trait
>
Parameters
<
MomentOf
<
T
>>
for
DefaultCombineData
<
T
>
{
type
minimum_count
=
MinimumCount
;
type
expires_in
=
ExpiresIn
;
}
This diff is collapsed.
Click to expand it.
oracle/src/mock.rs
+
6
−
1
View file @
4c914772
...
...
@@ -71,11 +71,16 @@ impl OperatorProvider<AccountId> for MockOperatorProvider {
}
}
parameter_types!
{
pub
const
MinimumCount
:
u32
=
3
;
pub
const
ExpiresIn
:
u32
=
600
;
}
impl
Trait
for
Test
{
type
Event
=
();
type
OnNewData
=
();
type
OperatorProvider
=
MockOperatorProvider
;
type
CombineData
=
DefaultCombineData
<
Self
>
;
type
CombineData
=
DefaultCombineData
<
Self
,
MinimumCount
,
ExpiresIn
>
;
type
Time
=
pallet_timestamp
::
Module
<
Self
>
;
type
OracleKey
=
Key
;
type
OracleValue
=
Value
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment