Module pallet_bags_list::example
source · Expand description
In this example, assuming each node has an equal id and score (eg. node 21 has a score of 21),
the node 22 can be moved from bag 1 to bag 0 with the rebag
operation.
Once the whole list is iterated, assuming the above above rebag happens, the order of iteration
would be: 25, 21, 22, 12, 22, 5, 7, 3
.
Moreover, in bag2, node 7 can be moved to the front of node 5 with the put_in_front_of
, as it
has a higher score.
graph LR
Bag0 --> Bag1 --> Bag2
subgraph Bag0[Bag 0: 21-30 DOT]
direction LR
25 --> 21 --> 22X[22]
end
subgraph Bag1[Bag 1: 11-20 DOT]
direction LR
12 --> 22
end
subgraph Bag2[Bag 2: 0-10 DOT]
direction LR
5 --> 7 --> 3
end
style 22X stroke-dasharray: 5 5,opacity:50%
The equivalent of this in code would be:
ⓘ
#[test]
fn examples_work() {
ExtBuilder::default()
.skip_genesis_ids()
// initially set the score of 11 for 22 to push it next to 12
.add_ids(vec![(25, 25), (21, 21), (12, 12), (22, 11), (5, 5), (7, 7), (3, 3)])
.build_and_execute(|| {
// initial bags
assert_eq!(
List::<Runtime>::get_bags(),
vec![
// bag 0 -> 10
(10, vec![5, 7, 3]),
// bag 10 -> 20
(20, vec![12, 22]),
// bag 20 -> 30
(30, vec![25, 21])
]
);
// set score of 22 to 22
StakingMock::set_score_of(&22, 22);
// now we rebag 22 to the first bag
assert_ok!(BagsList::rebag(RuntimeOrigin::signed(42), 22));
assert_eq!(
List::<Runtime>::get_bags(),
vec![
// bag 0 -> 10
(10, vec![5, 7, 3]),
// bag 10 -> 20
(20, vec![12]),
// bag 20 -> 30
(30, vec![25, 21, 22])
]
);
// now we put 7 at the front of bag 0
assert_ok!(BagsList::put_in_front_of(RuntimeOrigin::signed(7), 5));
assert_eq!(
List::<Runtime>::get_bags(),
vec![
// bag 0 -> 10
(10, vec![7, 5, 3]),
// bag 10 -> 20
(20, vec![12]),
// bag 20 -> 30
(30, vec![25, 21, 22])
]
);
})
}