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
0526dd25
Commit
0526dd25
authored
5 years ago
by
Ermal Kaleci
Committed by
Xiliang Chen
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
update ended auctions cleanup (#67)
* update ended auctions cleanup * update * update
parent
18ffc4b9
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
auction/src/lib.rs
+15
-18
15 additions, 18 deletions
auction/src/lib.rs
auction/src/tests.rs
+17
-0
17 additions, 0 deletions
auction/src/tests.rs
with
32 additions
and
18 deletions
auction/src/lib.rs
+
15
−
18
View file @
0526dd25
...
@@ -4,7 +4,7 @@ use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, P
...
@@ -4,7 +4,7 @@ use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, P
use
frame_system
::{
self
as
system
,
ensure_signed
};
use
frame_system
::{
self
as
system
,
ensure_signed
};
use
orml_utilities
::{
LinkedItem
,
LinkedList
};
use
orml_utilities
::{
LinkedItem
,
LinkedList
};
use
sp_runtime
::{
use
sp_runtime
::{
traits
::{
MaybeSerializeDeserialize
,
Member
,
SimpleArithmetic
,
Zero
},
traits
::{
MaybeSerializeDeserialize
,
Member
,
One
,
Saturating
,
SimpleArithmetic
,
Zero
},
DispatchResult
,
DispatchResult
,
};
};
...
@@ -87,22 +87,7 @@ decl_module! {
...
@@ -87,22 +87,7 @@ decl_module! {
}
}
fn
on_finalize
(
now
:
T
::
BlockNumber
)
{
fn
on_finalize
(
now
:
T
::
BlockNumber
)
{
let
head_key
:
Option
<
T
::
AuctionId
>
=
None
;
Self
::
_on_finalize
(
now
);
if
let
Some
(
mut
head_item
)
=
<
AuctionEndTime
<
T
>>
::
get
((
now
,
head_key
))
{
while
let
Some
(
auction_id
)
=
head_item
.next
{
if
let
Some
(
auction
)
=
Self
::
auctions
(
auction_id
)
{
T
::
Handler
::
on_auction_ended
(
auction_id
,
auction
.bid
);
<
Auctions
<
T
>>
::
remove
(
auction_id
);
}
head_item
=
<
AuctionEndTime
<
T
>>
::
get
((
now
,
Some
(
auction_id
)))
.unwrap_or_else
(||
LinkedItem
{
prev
:
None
,
next
:
None
,
});
<
AuctionEndTime
<
T
>>
::
remove
((
now
,
Some
(
auction_id
)));
}
<
AuctionEndTime
<
T
>>
::
remove
((
now
,
head_key
));
}
}
}
}
}
}
}
...
@@ -117,7 +102,19 @@ decl_error! {
...
@@ -117,7 +102,19 @@ decl_error! {
}
}
}
}
impl
<
T
:
Trait
>
Module
<
T
>
{}
impl
<
T
:
Trait
>
Module
<
T
>
{
fn
_on_finalize
(
now
:
T
::
BlockNumber
)
{
let
ended_auctions
=
<
AuctionEndTimeList
<
T
>>
::
take_all
(
&
now
);
let
mut
count
=
Self
::
auctions_count
();
ended_auctions
.for_each
(|
auction_id
|
{
if
let
Some
(
auction
)
=
<
Auctions
<
T
>>
::
take
(
&
auction_id
)
{
T
::
Handler
::
on_auction_ended
(
auction_id
,
auction
.bid
.clone
());
count
=
count
.saturating_sub
(
T
::
AuctionId
::
one
());
}
});
<
AuctionsCount
<
T
>>
::
put
(
count
);
}
}
impl
<
T
:
Trait
>
Auction
<
T
::
AccountId
,
T
::
BlockNumber
>
for
Module
<
T
>
{
impl
<
T
:
Trait
>
Auction
<
T
::
AccountId
,
T
::
BlockNumber
>
for
Module
<
T
>
{
type
AuctionId
=
T
::
AuctionId
;
type
AuctionId
=
T
::
AuctionId
;
...
...
This diff is collapsed.
Click to expand it.
auction/src/tests.rs
+
17
−
0
View file @
0526dd25
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
use
super
::
*
;
use
super
::
*
;
use
frame_support
::
assert_ok
;
use
frame_support
::
assert_ok
;
use
mock
::{
AuctionModule
,
ExtBuilder
,
Runtime
,
ALICE
};
use
mock
::{
AuctionModule
,
ExtBuilder
,
Runtime
,
ALICE
};
use
sp_runtime
::
traits
::
OnFinalize
;
#[test]
#[test]
fn
new_auction_should_work
()
{
fn
new_auction_should_work
()
{
...
@@ -69,3 +70,19 @@ fn bid_should_fail() {
...
@@ -69,3 +70,19 @@ fn bid_should_fail() {
);
);
});
});
}
}
#[test]
fn
cleanup_auction_should_work
()
{
ExtBuilder
::
default
()
.build
()
.execute_with
(||
{
assert_eq!
(
AuctionModule
::
new_auction
(
10
,
Some
(
100
)),
0
);
assert_eq!
(
AuctionModule
::
auctions_count
(),
1
);
assert_eq!
(
AuctionModule
::
new_auction
(
10
,
Some
(
50
)),
1
);
assert_eq!
(
AuctionModule
::
auctions_count
(),
2
);
AuctionModule
::
on_finalize
(
1
);
assert_eq!
(
AuctionModule
::
auctions_count
(),
2
);
AuctionModule
::
on_finalize
(
50
);
assert_eq!
(
AuctionModule
::
auctions_count
(),
1
);
AuctionModule
::
on_finalize
(
100
);
assert_eq!
(
AuctionModule
::
auctions_count
(),
0
);
});
}
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