Skip to content

Commit 32289d4

Browse files
committed
fix(data_structures): fix stakes serialization problems
1 parent d17b992 commit 32289d4

File tree

2 files changed

+111
-51
lines changed

2 files changed

+111
-51
lines changed

data_structures/src/staking/helpers.rs

Lines changed: 83 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ pub type Power = u64;
2727
pub type StakesResult<T, Address, Coins, Epoch> = Result<T, StakesError<Address, Coins, Epoch>>;
2828

2929
/// Pairs a stake key and the stake data it refers.
30-
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
30+
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
3131
pub struct StakeEntry<const UNIT: u8, Address, Coins, Epoch, Power>
3232
where
33-
Address: Clone + Default,
34-
Coins: Clone,
35-
Epoch: Clone + Default,
36-
Power: Clone,
33+
Address: Clone + Default + Serialize,
34+
Coins: Clone + Serialize,
35+
Epoch: Clone + Default + Serialize,
36+
Power: Clone + Serialize,
3737
{
3838
/// The key to which this stake entry belongs to.
3939
pub key: StakeKey<Address>,
@@ -53,10 +53,10 @@ impl<const UNIT: u8, Address, Coins, Epoch, Power>
5353
From<StakeEntry<UNIT, Address, Coins, Epoch, Power>>
5454
for Stake<UNIT, Address, Coins, Epoch, Power>
5555
where
56-
Address: Clone + Default,
57-
Coins: Clone,
58-
Epoch: Clone + Default,
59-
Power: Clone,
56+
Address: Clone + Default + Serialize,
57+
Coins: Clone + Serialize,
58+
Epoch: Clone + Default + Serialize,
59+
Power: Clone + Serialize,
6060
{
6161
fn from(entry: StakeEntry<UNIT, Address, Coins, Epoch, Power>) -> Self {
6262
entry.value
@@ -84,10 +84,10 @@ where
8484
impl<const UNIT: u8, Address, Coins, Epoch, Power>
8585
SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>
8686
where
87-
Address: Clone + Default,
88-
Coins: Clone,
89-
Epoch: Clone + Default,
90-
Power: Clone,
87+
Address: Clone + Default + Serialize,
88+
Coins: Clone + Serialize,
89+
Epoch: Clone + Default + Serialize,
90+
Power: Clone + Serialize,
9191
{
9292
/// Locks and reads both the stake key and the stake data referred by the stake entry.
9393
pub fn read_entry(&self) -> StakeEntry<UNIT, Address, Coins, Epoch, Power> {
@@ -114,10 +114,10 @@ impl<const UNIT: u8, Address, Coins, Epoch, Power>
114114
From<StakeEntry<UNIT, Address, Coins, Epoch, Power>>
115115
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>
116116
where
117-
Address: Clone + Default,
118-
Coins: Clone,
119-
Epoch: Clone + Default,
120-
Power: Clone,
117+
Address: Clone + Default + Serialize,
118+
Coins: Clone + Serialize,
119+
Epoch: Clone + Default + Serialize,
120+
Power: Clone + Serialize,
121121
{
122122
fn from(entry: StakeEntry<UNIT, Address, Coins, Epoch, Power>) -> Self {
123123
SyncStakeEntry {
@@ -127,6 +127,23 @@ where
127127
}
128128
}
129129

130+
impl<const UNIT: u8, Address, Coins, Epoch, Power>
131+
From<&SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>>
132+
for StakeEntry<UNIT, Address, Coins, Epoch, Power>
133+
where
134+
Address: Clone + Default + Serialize,
135+
Coins: Clone + Serialize,
136+
Epoch: Clone + Default + Serialize,
137+
Power: Clone + Serialize,
138+
{
139+
fn from(sync: &SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>) -> Self {
140+
StakeEntry {
141+
key: sync.read_key(),
142+
value: sync.read_value(),
143+
}
144+
}
145+
}
146+
130147
impl<const UNIT: u8, Address, Coins, Epoch, Power> PartialEq
131148
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>
132149
where
@@ -146,10 +163,10 @@ where
146163
impl<'de, const UNIT: u8, Address, Coins, Epoch, Power> Deserialize<'de>
147164
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>
148165
where
149-
Address: Clone + Default,
150-
Coins: Clone,
151-
Epoch: Clone + Default,
152-
Power: Clone,
166+
Address: Clone + Default + Serialize,
167+
Coins: Clone + Serialize,
168+
Epoch: Clone + Default + Serialize,
169+
Power: Clone + Serialize,
153170
StakeEntry<UNIT, Address, Coins, Epoch, Power>: Deserialize<'de>,
154171
{
155172
#[inline]
@@ -165,17 +182,17 @@ where
165182
impl<const UNIT: u8, Address, Coins, Epoch, Power> Serialize
166183
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>
167184
where
168-
Address: Clone + Default,
169-
Coins: Clone,
170-
Epoch: Clone + Default,
171-
Power: Clone,
185+
Address: Clone + Default + Serialize,
186+
Coins: Clone + Serialize,
187+
Epoch: Clone + Default + Serialize,
188+
Power: Clone + Serialize,
172189
Stake<UNIT, Address, Coins, Epoch, Power>: Serialize,
173190
{
174191
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
175192
where
176193
S: Serializer,
177194
{
178-
self.value.read().unwrap().serialize(serializer)
195+
StakeEntry::from(self).serialize(serializer)
179196
}
180197
}
181198

@@ -296,7 +313,16 @@ where
296313
impl<'de, const UNIT: u8, Address, Coins, Epoch, Power> Deserialize<'de>
297314
for Stakes<UNIT, Address, Coins, Epoch, Power>
298315
where
299-
Address: Clone + Debug + Default + DeserializeOwned + Display + Ord + Send + Sync + 'static,
316+
Address: Clone
317+
+ Debug
318+
+ Default
319+
+ DeserializeOwned
320+
+ Display
321+
+ Ord
322+
+ Send
323+
+ Serialize
324+
+ Sync
325+
+ 'static,
300326
Coins: Copy
301327
+ Debug
302328
+ Default
@@ -308,6 +334,7 @@ where
308334
+ Ord
309335
+ PrecisionLoss
310336
+ Send
337+
+ Serialize
311338
+ Sub<Output = Coins>
312339
+ Sum
313340
+ Sync
@@ -322,12 +349,19 @@ where
322349
+ From<u32>
323350
+ Saturating
324351
+ Send
352+
+ Serialize
325353
+ Sub<Output = Epoch>
326354
+ Sync
327355
+ Add<Output = Epoch>
328356
+ Div<Output = Epoch>,
329-
Power:
330-
Add<Output = Power> + Copy + Default + DeserializeOwned + Div<Output = Power> + Ord + Sum,
357+
Power: Add<Output = Power>
358+
+ Copy
359+
+ Default
360+
+ DeserializeOwned
361+
+ Div<Output = Power>
362+
+ Ord
363+
+ Serialize
364+
+ Sum,
331365
u64: From<Coins> + From<Power>,
332366
{
333367
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -349,7 +383,16 @@ struct StakesVisitor<const UNIT: u8, Address, Coins, Epoch, Power> {
349383
impl<'de, const UNIT: u8, Address, Coins, Epoch, Power> Visitor<'de>
350384
for StakesVisitor<UNIT, Address, Coins, Epoch, Power>
351385
where
352-
Address: Clone + Debug + Default + Deserialize<'de> + Display + Ord + Send + Sync + 'static,
386+
Address: Clone
387+
+ Debug
388+
+ Default
389+
+ Deserialize<'de>
390+
+ Display
391+
+ Ord
392+
+ Send
393+
+ Serialize
394+
+ Sync
395+
+ 'static,
353396
Coins: Copy
354397
+ Debug
355398
+ Default
@@ -361,6 +404,7 @@ where
361404
+ Ord
362405
+ PrecisionLoss
363406
+ Send
407+
+ Serialize
364408
+ Sub<Output = Coins>
365409
+ Sum
366410
+ Sync
@@ -374,13 +418,20 @@ where
374418
+ Display
375419
+ From<u32>
376420
+ Send
421+
+ Serialize
377422
+ Saturating
378423
+ Sub<Output = Epoch>
379424
+ Sync
380425
+ Add<Output = Epoch>
381426
+ Div<Output = Epoch>,
382-
Power:
383-
Add<Output = Power> + Copy + Default + Deserialize<'de> + Div<Output = Power> + Ord + Sum,
427+
Power: Add<Output = Power>
428+
+ Copy
429+
+ Default
430+
+ Deserialize<'de>
431+
+ Div<Output = Power>
432+
+ Ord
433+
+ Serialize
434+
+ Sum,
384435
u64: From<Coins> + From<Power>,
385436
{
386437
type Value = Stakes<UNIT, Address, Coins, Epoch, Power>;

0 commit comments

Comments
 (0)