Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Duniter v2S
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
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
nodes
rust
Duniter v2S
Commits
68087d9c
Commit
68087d9c
authored
1 year ago
by
Cédric Moreau
Browse files
Options
Downloads
Patches
Plain Diff
feat(smith-members): events
parent
ce5f317f
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
pallets/smith-members/src/lib.rs
+30
-7
30 additions, 7 deletions
pallets/smith-members/src/lib.rs
pallets/smith-members/src/mock.rs
+12
-0
12 additions, 0 deletions
pallets/smith-members/src/mock.rs
pallets/smith-members/src/tests.rs
+34
-1
34 additions, 1 deletion
pallets/smith-members/src/tests.rs
with
76 additions
and
8 deletions
pallets/smith-members/src/lib.rs
+
30
−
7
View file @
68087d9c
...
@@ -116,8 +116,22 @@ pub mod pallet {
...
@@ -116,8 +116,22 @@ pub mod pallet {
#[pallet::event]
#[pallet::event]
#[pallet::generate_deposit(pub(super)
fn
deposit_event)]
#[pallet::generate_deposit(pub(super)
fn
deposit_event)]
pub
enum
Event
<
T
:
Config
>
{
pub
enum
Event
<
T
:
Config
>
{
/// An identity is being inivited to become a smith
InvitationSent
{
idty_index
:
T
::
IdtyIndex
,
invited_by
:
T
::
IdtyIndex
,
},
/// The invitation has been accepted
InvitationAccepted
{
idty_index
:
T
::
IdtyIndex
},
/// Certification received
CertificationReceived
{
idty_index
:
T
::
IdtyIndex
,
issued_by
:
T
::
IdtyIndex
,
},
/// A smith gathered enough certifications to become an authority (can call `go_online()`).
/// A smith gathered enough certifications to become an authority (can call `go_online()`).
SmithBecameAuthority
{
idty_index
:
T
::
IdtyIndex
},
PromotedToSmith
{
idty_index
:
T
::
IdtyIndex
},
/// A smith has been removed from the smiths set
SmithExcluded
{
idty_index
:
T
::
IdtyIndex
},
}
}
#[pallet::genesis_config]
#[pallet::genesis_config]
...
@@ -248,7 +262,7 @@ pub mod pallet {
...
@@ -248,7 +262,7 @@ pub mod pallet {
let
who
=
ensure_signed
(
origin
.clone
())
?
;
let
who
=
ensure_signed
(
origin
.clone
())
?
;
let
issuer
=
T
::
IdtyIdOf
::
convert
(
who
.clone
())
.ok_or
(
Error
::
<
T
>
::
UnknownIssuer
)
?
;
let
issuer
=
T
::
IdtyIdOf
::
convert
(
who
.clone
())
.ok_or
(
Error
::
<
T
>
::
UnknownIssuer
)
?
;
Self
::
check_invite_smith
(
issuer
,
receiver
)
?
;
Self
::
check_invite_smith
(
issuer
,
receiver
)
?
;
Self
::
do_invite_smith
(
receiver
);
Self
::
do_invite_smith
(
issuer
,
receiver
);
Ok
(()
.into
())
Ok
(()
.into
())
}
}
...
@@ -299,7 +313,7 @@ impl<T: Config> Pallet<T> {
...
@@ -299,7 +313,7 @@ impl<T: Config> Pallet<T> {
Ok
(()
.into
())
Ok
(()
.into
())
}
}
fn
do_invite_smith
(
receiver
:
T
::
IdtyIndex
)
{
fn
do_invite_smith
(
issuer
:
T
::
IdtyIndex
,
receiver
:
T
::
IdtyIndex
)
{
let
new_expires_on
=
CurrentSession
::
<
T
>
::
get
()
+
T
::
InactivityMaxDuration
::
get
();
let
new_expires_on
=
CurrentSession
::
<
T
>
::
get
()
+
T
::
InactivityMaxDuration
::
get
();
// TODO: another way to write this?
// TODO: another way to write this?
if
Smiths
::
<
T
>
::
get
(
receiver
)
.is_some
()
{
if
Smiths
::
<
T
>
::
get
(
receiver
)
.is_some
()
{
...
@@ -321,6 +335,10 @@ impl<T: Config> Pallet<T> {
...
@@ -321,6 +335,10 @@ impl<T: Config> Pallet<T> {
);
);
}
}
ExpiresOn
::
<
T
>
::
append
(
new_expires_on
,
receiver
);
ExpiresOn
::
<
T
>
::
append
(
new_expires_on
,
receiver
);
Self
::
deposit_event
(
Event
::
<
T
>
::
InvitationSent
{
idty_index
:
receiver
,
invited_by
:
issuer
,
});
}
}
fn
check_accept_invitation
(
receiver
:
T
::
IdtyIndex
)
->
DispatchResultWithPostInfo
{
fn
check_accept_invitation
(
receiver
:
T
::
IdtyIndex
)
->
DispatchResultWithPostInfo
{
...
@@ -339,6 +357,9 @@ impl<T: Config> Pallet<T> {
...
@@ -339,6 +357,9 @@ impl<T: Config> Pallet<T> {
let
maybe_smith_meta
=
maybe_smith_meta
.as_mut
()
.expect
(
"status checked earlier"
);
let
maybe_smith_meta
=
maybe_smith_meta
.as_mut
()
.expect
(
"status checked earlier"
);
maybe_smith_meta
.status
=
SmithStatus
::
Pending
;
maybe_smith_meta
.status
=
SmithStatus
::
Pending
;
});
});
Self
::
deposit_event
(
Event
::
<
T
>
::
InvitationAccepted
{
idty_index
:
receiver
,
});
Ok
(()
.into
())
Ok
(()
.into
())
}
}
...
@@ -388,8 +409,12 @@ impl<T: Config> Pallet<T> {
...
@@ -388,8 +409,12 @@ impl<T: Config> Pallet<T> {
// expiry postponed
// expiry postponed
let
new_expires_on
=
CurrentSession
::
<
T
>
::
get
()
+
T
::
InactivityMaxDuration
::
get
();
let
new_expires_on
=
CurrentSession
::
<
T
>
::
get
()
+
T
::
InactivityMaxDuration
::
get
();
maybe_smith_meta
.expires_on
=
Some
(
new_expires_on
);
maybe_smith_meta
.expires_on
=
Some
(
new_expires_on
);
Self
::
deposit_event
(
Event
::
<
T
>
::
CertificationReceived
{
idty_index
:
receiver
,
issued_by
:
issuer
,
});
if
maybe_smith_meta
.status
==
SmithStatus
::
Smith
{
if
maybe_smith_meta
.status
==
SmithStatus
::
Smith
{
Self
::
deposit_event
(
Event
::
<
T
>
::
SmithBecameAuthority
{
Self
::
deposit_event
(
Event
::
<
T
>
::
PromotedToSmith
{
idty_index
:
receiver
,
idty_index
:
receiver
,
});
});
}
}
...
@@ -433,7 +458,7 @@ impl<T: Config> Pallet<T> {
...
@@ -433,7 +458,7 @@ impl<T: Config> Pallet<T> {
smith
,
smith
,
SmithRemovalReason
::
OfflineTooLong
,
SmithRemovalReason
::
OfflineTooLong
,
);
);
// TODO: add event? (+ add others too)
Self
::
deposit_event
(
Event
::
<
T
>
::
SmithExcluded
{
idty_index
:
smith
});
}
}
}
}
}
}
...
@@ -479,9 +504,7 @@ impl<T: Config> Pallet<T> {
...
@@ -479,9 +504,7 @@ impl<T: Config> Pallet<T> {
// T::OnSmithDelete::on_smith_delete(idty_index, SmithRemovalReason::Blacklisted);
// T::OnSmithDelete::on_smith_delete(idty_index, SmithRemovalReason::Blacklisted);
// }
// }
}
}
}
impl
<
T
:
Config
>
Pallet
<
T
>
{
fn
provide_is_member
(
idty_id
:
&
T
::
IdtyIndex
)
->
bool
{
fn
provide_is_member
(
idty_id
:
&
T
::
IdtyIndex
)
->
bool
{
let
Some
(
smith
)
=
Smiths
::
<
T
>
::
get
(
idty_id
)
else
{
let
Some
(
smith
)
=
Smiths
::
<
T
>
::
get
(
idty_id
)
else
{
return
false
;
return
false
;
...
...
This diff is collapsed.
Click to expand it.
pallets/smith-members/src/mock.rs
+
12
−
0
View file @
68087d9c
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
#![cfg(test)]
#![cfg(test)]
use
crate
::{
self
as
pallet_smith_members
};
use
crate
::{
self
as
pallet_smith_members
};
use
frame_support
::
pallet_prelude
::
Hooks
;
use
frame_support
::{
use
frame_support
::{
parameter_types
,
parameter_types
,
traits
::{
ConstU32
,
ConstU64
},
traits
::{
ConstU32
,
ConstU64
},
...
@@ -107,3 +108,14 @@ pub fn new_test_ext(
...
@@ -107,3 +108,14 @@ pub fn new_test_ext(
.unwrap
()
.unwrap
()
.into
()
.into
()
}
}
pub
fn
run_to_block
(
n
:
u64
)
{
while
System
::
block_number
()
<
n
{
Smith
::
on_finalize
(
System
::
block_number
());
System
::
on_finalize
(
System
::
block_number
());
System
::
reset_events
();
System
::
set_block_number
(
System
::
block_number
()
+
1
);
System
::
on_initialize
(
System
::
block_number
());
Smith
::
on_initialize
(
System
::
block_number
());
}
}
This diff is collapsed.
Click to expand it.
pallets/smith-members/src/tests.rs
+
34
−
1
View file @
68087d9c
...
@@ -17,7 +17,7 @@
...
@@ -17,7 +17,7 @@
#![cfg(test)]
#![cfg(test)]
use
super
::
*
;
use
super
::
*
;
use
crate
::
mock
::{
new_test_ext
,
Runtime
,
Runtime
Origin
};
use
crate
::
mock
::{
new_test_ext
,
run_to_block
,
Runtime
,
Runtime
Event
,
RuntimeOrigin
,
System
};
use
frame_support
::{
assert_err
,
assert_ok
};
use
frame_support
::{
assert_err
,
assert_ok
};
#[cfg(test)]
#[cfg(test)]
...
@@ -35,14 +35,23 @@ fn process_to_become_a_smith_and_lose_it() {
...
@@ -35,14 +35,23 @@ fn process_to_become_a_smith_and_lose_it() {
],
],
})
})
.execute_with
(||
{
.execute_with
(||
{
// Events cannot be recorded on genesis
run_to_block
(
1
);
// State before
// State before
assert_eq!
(
Smiths
::
<
Runtime
>
::
get
(
5
),
None
);
assert_eq!
(
Smiths
::
<
Runtime
>
::
get
(
5
),
None
);
// Try to invite
// Try to invite
assert_ok!
(
Pallet
::
<
Runtime
>
::
invite_smith
(
RuntimeOrigin
::
signed
(
1
),
5
));
assert_ok!
(
Pallet
::
<
Runtime
>
::
invite_smith
(
RuntimeOrigin
::
signed
(
1
),
5
));
System
::
assert_has_event
(
RuntimeEvent
::
Smith
(
Event
::
<
Runtime
>
::
InvitationSent
{
idty_index
:
5
,
invited_by
:
1
,
}));
// Accept invitation
// Accept invitation
assert_ok!
(
Pallet
::
<
Runtime
>
::
accept_invitation
(
RuntimeOrigin
::
signed
(
assert_ok!
(
Pallet
::
<
Runtime
>
::
accept_invitation
(
RuntimeOrigin
::
signed
(
5
5
)));
)));
System
::
assert_has_event
(
RuntimeEvent
::
Smith
(
Event
::
<
Runtime
>
::
InvitationAccepted
{
idty_index
:
5
,
}));
// State after
// State after
assert_eq!
(
assert_eq!
(
Smiths
::
<
Runtime
>
::
get
(
5
)
.unwrap
(),
Smiths
::
<
Runtime
>
::
get
(
5
)
.unwrap
(),
...
@@ -58,6 +67,12 @@ fn process_to_become_a_smith_and_lose_it() {
...
@@ -58,6 +67,12 @@ fn process_to_become_a_smith_and_lose_it() {
RuntimeOrigin
::
signed
(
1
),
RuntimeOrigin
::
signed
(
1
),
5
5
));
));
System
::
assert_has_event
(
RuntimeEvent
::
Smith
(
Event
::
<
Runtime
>
::
CertificationReceived
{
idty_index
:
5
,
issued_by
:
1
,
},
));
assert_eq!
(
assert_eq!
(
Smiths
::
<
Runtime
>
::
get
(
5
)
.unwrap
(),
Smiths
::
<
Runtime
>
::
get
(
5
)
.unwrap
(),
SmithMeta
{
SmithMeta
{
...
@@ -72,6 +87,15 @@ fn process_to_become_a_smith_and_lose_it() {
...
@@ -72,6 +87,15 @@ fn process_to_become_a_smith_and_lose_it() {
RuntimeOrigin
::
signed
(
2
),
RuntimeOrigin
::
signed
(
2
),
5
5
));
));
System
::
assert_has_event
(
RuntimeEvent
::
Smith
(
Event
::
<
Runtime
>
::
CertificationReceived
{
idty_index
:
5
,
issued_by
:
1
,
},
));
System
::
assert_has_event
(
RuntimeEvent
::
Smith
(
Event
::
<
Runtime
>
::
PromotedToSmith
{
idty_index
:
5
,
}));
assert_eq!
(
assert_eq!
(
Smiths
::
<
Runtime
>
::
get
(
5
)
.unwrap
(),
Smiths
::
<
Runtime
>
::
get
(
5
)
.unwrap
(),
SmithMeta
{
SmithMeta
{
...
@@ -89,6 +113,15 @@ fn process_to_become_a_smith_and_lose_it() {
...
@@ -89,6 +113,15 @@ fn process_to_become_a_smith_and_lose_it() {
assert!
(
Smiths
::
<
Runtime
>
::
get
(
5
)
.is_some
());
assert!
(
Smiths
::
<
Runtime
>
::
get
(
5
)
.is_some
());
// On session 5 no more smiths because of lack of activity
// On session 5 no more smiths because of lack of activity
Pallet
::
<
Runtime
>
::
on_new_session
(
5
);
Pallet
::
<
Runtime
>
::
on_new_session
(
5
);
System
::
assert_has_event
(
RuntimeEvent
::
Smith
(
Event
::
<
Runtime
>
::
SmithExcluded
{
idty_index
:
1
,
}));
System
::
assert_has_event
(
RuntimeEvent
::
Smith
(
Event
::
<
Runtime
>
::
SmithExcluded
{
idty_index
:
2
,
}));
System
::
assert_has_event
(
RuntimeEvent
::
Smith
(
Event
::
<
Runtime
>
::
SmithExcluded
{
idty_index
:
5
,
}));
assert_eq!
(
assert_eq!
(
Smiths
::
<
Runtime
>
::
get
(
1
),
Smiths
::
<
Runtime
>
::
get
(
1
),
Some
(
SmithMeta
{
Some
(
SmithMeta
{
...
...
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