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
8f0e16b6
Commit
8f0e16b6
authored
1 year ago
by
Cédric Moreau
Browse files
Options
Downloads
Patches
Plain Diff
feat(smith-members): refact: check_* and do_* functions
parent
a16039d3
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pallets/smith-members/src/lib.rs
+105
-76
105 additions, 76 deletions
pallets/smith-members/src/lib.rs
with
105 additions
and
76 deletions
pallets/smith-members/src/lib.rs
+
105
−
76
View file @
8f0e16b6
...
...
@@ -27,10 +27,12 @@ mod traits;
mod
types
;
use
codec
::{
Codec
,
Decode
,
Encode
};
use
frame_support
::
dispatch
::
TypeInfo
;
use
frame_support
::
dispatch
::
{
DispatchResultWithPostInfo
,
TypeInfo
}
;
use
frame_support
::
pallet_prelude
::
Get
;
use
frame_support
::
RuntimeDebug
;
use
sp_runtime
::
traits
::
AtLeast32BitUnsigned
;
use
frame_support
::{
ensure
,
RuntimeDebug
};
use
frame_system
::
ensure_signed
;
use
frame_system
::
pallet_prelude
::
OriginFor
;
use
sp_runtime
::
traits
::{
AtLeast32BitUnsigned
,
Convert
};
use
sp_std
::
fmt
::
Debug
;
use
sp_std
::
prelude
::
*
;
...
...
@@ -238,8 +240,6 @@ pub mod pallet {
NotAMember
,
}
// TODO: refactor with check_* and do_* functions
#[pallet::call]
impl
<
T
:
Config
>
Pallet
<
T
>
{
#[pallet::call_index(
0
)]
...
...
@@ -250,6 +250,41 @@ pub mod pallet {
)
->
DispatchResultWithPostInfo
{
let
who
=
ensure_signed
(
origin
.clone
())
?
;
let
issuer
=
T
::
IdtyIdOf
::
convert
(
who
.clone
())
.ok_or
(
Error
::
<
T
>
::
UnknownIssuer
)
?
;
Self
::
check_invite_smith
(
issuer
,
receiver
)
?
;
Self
::
do_invite_smith
(
receiver
);
Ok
(()
.into
())
}
#[pallet::call_index(
1
)]
#[pallet::weight(
1_000_000_000
)]
pub
fn
accept_invitation
(
origin
:
OriginFor
<
T
>
)
->
DispatchResultWithPostInfo
{
let
who
=
ensure_signed
(
origin
.clone
())
?
;
let
receiver
=
T
::
IdtyIdOf
::
convert
(
who
.clone
())
.ok_or
(
Error
::
<
T
>
::
UnknownIssuer
)
?
;
Self
::
check_accept_invitation
(
receiver
)
?
;
Self
::
do_accept_invitation
(
receiver
)
?
;
Ok
(()
.into
())
}
#[pallet::call_index(
2
)]
#[pallet::weight(
1_000_000_000
)]
pub
fn
certify_smith
(
origin
:
OriginFor
<
T
>
,
receiver
:
T
::
IdtyIndex
,
)
->
DispatchResultWithPostInfo
{
let
who
=
ensure_signed
(
origin
)
?
;
let
issuer
=
T
::
IdtyIdOf
::
convert
(
who
.clone
())
.ok_or
(
Error
::
<
T
>
::
UnknownIssuer
)
?
;
Self
::
check_certify_smith
(
issuer
,
receiver
)
?
;
Self
::
do_certify_smith
(
receiver
,
issuer
);
Ok
(()
.into
())
}
}
}
impl
<
T
:
Config
>
Pallet
<
T
>
{
fn
check_invite_smith
(
issuer
:
T
::
IdtyIndex
,
receiver
:
T
::
IdtyIndex
,
)
->
DispatchResultWithPostInfo
{
let
issuer_status
=
Smiths
::
<
T
>
::
get
(
issuer
)
.ok_or
(
Error
::
<
T
>
::
IssuerHasNoSmithStatus
)
?
.status
;
...
...
@@ -262,6 +297,10 @@ pub mod pallet {
Error
::
<
T
>
::
ReceveirAlreadyHasSmithStatus
);
Ok
(()
.into
())
}
fn
do_invite_smith
(
receiver
:
T
::
IdtyIndex
)
{
let
new_expires_on
=
CurrentSession
::
<
T
>
::
get
()
+
T
::
InactivityMaxDuration
::
get
();
Smiths
::
<
T
>
::
insert
(
receiver
,
...
...
@@ -272,39 +311,31 @@ pub mod pallet {
},
);
ExpiresOn
::
<
T
>
::
append
(
new_expires_on
,
receiver
);
Ok
(()
.into
())
}
#[pallet::call_index(
1
)]
#[pallet::weight(
1_000_000_000
)]
pub
fn
accept_invitation
(
origin
:
OriginFor
<
T
>
)
->
DispatchResultWithPostInfo
{
let
who
=
ensure_signed
(
origin
.clone
())
?
;
let
pretender
=
T
::
IdtyIdOf
::
convert
(
who
.clone
())
.ok_or
(
Error
::
<
T
>
::
UnknownIssuer
)
?
;
let
pretender_status
=
Smiths
::
<
T
>
::
get
(
pretender
)
fn
check_accept_invitation
(
receiver
:
T
::
IdtyIndex
)
->
DispatchResultWithPostInfo
{
let
pretender_status
=
Smiths
::
<
T
>
::
get
(
receiver
)
.ok_or
(
Error
::
<
T
>
::
NotInvited
)
?
.status
;
ensure!
(
pretender_status
==
SmithStatus
::
Invited
,
Error
::
<
T
>
::
AlreadyAcceptedInvitation
);
Ok
(()
.into
())
}
Smiths
::
<
T
>
::
mutate
(
pretender
,
|
maybe_smith_meta
|
{
fn
do_accept_invitation
(
receiver
:
T
::
IdtyIndex
)
->
DispatchResultWithPostInfo
{
Smiths
::
<
T
>
::
mutate
(
receiver
,
|
maybe_smith_meta
|
{
let
maybe_smith_meta
=
maybe_smith_meta
.as_mut
()
.expect
(
"status checked earlier"
);
maybe_smith_meta
.status
=
SmithStatus
::
Pending
;
});
Ok
(()
.into
())
}
#[pallet::call_index(
2
)]
#[pallet::weight(
1_000_000_000
)]
pub
fn
certify_smith
(
origin
:
OriginFor
<
T
>
,
fn
check_certify_smith
(
issuer
:
T
::
IdtyIndex
,
receiver
:
T
::
IdtyIndex
,
)
->
DispatchResultWithPostInfo
{
let
who
=
ensure_signed
(
origin
)
?
;
let
issuer
=
T
::
IdtyIdOf
::
convert
(
who
.clone
())
.ok_or
(
Error
::
<
T
>
::
UnknownIssuer
)
?
;
ensure!
(
issuer
!=
receiver
,
Error
::
<
T
>
::
CannotCertifySelf
);
let
issuer_status
=
Smiths
::
<
T
>
::
get
(
issuer
)
.ok_or
(
Error
::
<
T
>
::
IssuerHasNoSmithStatus
)
?
...
...
@@ -321,14 +352,17 @@ pub mod pallet {
Error
::
<
T
>
::
ReceveirMustAcceptInvitation
);
Ok
(()
.into
())
}
fn
do_certify_smith
(
receiver
:
T
::
IdtyIndex
,
issuer
:
T
::
IdtyIndex
)
{
Smiths
::
<
T
>
::
mutate
(
receiver
,
|
maybe_smith_meta
|
{
let
maybe_smith_meta
=
maybe_smith_meta
.as_mut
()
.expect
(
"status checked earlier"
);
maybe_smith_meta
.received_certs
.push
(
issuer
);
maybe_smith_meta
.received_certs
.sort
();
// TODO: "as u32" allowed?
maybe_smith_meta
.status
=
if
maybe_smith_meta
.received_certs
.len
()
as
u32
>=
T
::
MinCertForMembership
::
get
()
{
maybe_smith_meta
.status
=
if
maybe_smith_meta
.received_certs
.len
()
as
u32
>=
T
::
MinCertForMembership
::
get
()
{
SmithStatus
::
Smith
}
else
{
SmithStatus
::
Pending
...
...
@@ -338,13 +372,8 @@ pub mod pallet {
maybe_smith_meta
.expires_on
=
Some
(
new_expires_on
);
// TODO: unschedule old expiry
});
Ok
(()
.into
())
}
}
}
impl
<
T
:
Config
>
Pallet
<
T
>
{
// TODO: return what?
fn
remove_expired_smiths
(
at
:
SessionIndex
)
{
if
let
Some
(
smiths_to_remove
)
=
ExpiresOn
::
<
T
>
::
get
(
at
)
{
...
...
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