Skip to content

Commit a089ad1

Browse files
committed
feat(stakes): add nonce field to Stake
1 parent 552fd5a commit a089ad1

File tree

5 files changed

+284
-102
lines changed

5 files changed

+284
-102
lines changed

data_structures/src/chain/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3867,7 +3867,7 @@ pub struct ChainState {
38673867
pub tapi_engine: TapiEngine,
38683868
/// Tracks stakes for every validator in the network
38693869
#[serde(default)]
3870-
pub stakes: Stakes<WIT_DECIMAL_PLACES, PublicKeyHash, Wit, Epoch, u64>,
3870+
pub stakes: Stakes<WIT_DECIMAL_PLACES, PublicKeyHash, Wit, Epoch, u64, u64>,
38713871
/// Unspent Outputs Pool
38723872
#[serde(skip)]
38733873
pub unspent_outputs_pool: UnspentOutputsPool,

data_structures/src/staking/helpers.rs

Lines changed: 88 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
fmt::{Debug, Display, Formatter},
44
iter::Sum,
55
marker::PhantomData,
6-
ops::{Add, Div, Mul, Rem, Sub},
6+
ops::{Add, AddAssign, Div, Mul, Rem, Sub},
77
rc::Rc,
88
str::FromStr,
99
sync::RwLock,
@@ -28,37 +28,39 @@ pub type StakesResult<T, Address, Coins, Epoch> = Result<T, StakesError<Address,
2828

2929
/// Pairs a stake key and the stake data it refers.
3030
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
31-
pub struct StakeEntry<const UNIT: u8, Address, Coins, Epoch, Power>
31+
pub struct StakeEntry<const UNIT: u8, Address, Coins, Epoch, Nonce, Power>
3232
where
3333
Address: Clone + Default + Serialize,
3434
Coins: Clone + Serialize,
3535
Epoch: Clone + Default + Serialize,
36+
Nonce: Clone + Default + Serialize,
3637
Power: Clone + Serialize,
3738
{
3839
/// The key to which this stake entry belongs to.
3940
pub key: StakeKey<Address>,
4041
/// The stake data itself.
41-
pub value: Stake<UNIT, Address, Coins, Epoch, Power>,
42+
pub value: Stake<UNIT, Address, Coins, Epoch, Nonce, Power>,
4243
}
4344

4445
/// The resulting type for functions in this module that return a single stake entry.
45-
pub type StakeEntryResult<const UNIT: u8, Address, Coins, Epoch, Power> =
46-
StakesResult<StakeEntry<UNIT, Address, Coins, Epoch, Power>, Address, Coins, Epoch>;
46+
pub type StakeEntryResult<const UNIT: u8, Address, Coins, Epoch, Nonce, Power> =
47+
StakesResult<StakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>, Address, Coins, Epoch>;
4748

4849
/// The resulting type for functions in this module that return a vector of stake entries.
49-
pub type StakeEntryVecResult<const UNIT: u8, Address, Coins, Epoch, Power> =
50-
StakesResult<Vec<StakeEntry<UNIT, Address, Coins, Epoch, Power>>, Address, Coins, Epoch>;
50+
pub type StakeEntryVecResult<const UNIT: u8, Address, Coins, Epoch, Nonce, Power> =
51+
StakesResult<Vec<StakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>>, Address, Coins, Epoch>;
5152

52-
impl<const UNIT: u8, Address, Coins, Epoch, Power>
53-
From<StakeEntry<UNIT, Address, Coins, Epoch, Power>>
54-
for Stake<UNIT, Address, Coins, Epoch, Power>
53+
impl<const UNIT: u8, Address, Coins, Epoch, Nonce, Power>
54+
From<StakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>>
55+
for Stake<UNIT, Address, Coins, Epoch, Nonce, Power>
5556
where
5657
Address: Clone + Default + Serialize,
5758
Coins: Clone + Serialize,
5859
Epoch: Clone + Default + Serialize,
60+
Nonce: Clone + Default + Serialize,
5961
Power: Clone + Serialize,
6062
{
61-
fn from(entry: StakeEntry<UNIT, Address, Coins, Epoch, Power>) -> Self {
63+
fn from(entry: StakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>) -> Self {
6264
entry.value
6365
}
6466
}
@@ -68,29 +70,31 @@ where
6870
/// This is needed for implementing `PartialEq` manually on the locked data, which cannot be done directly
6971
/// because those are externally owned types.
7072
#[derive(Clone, Debug, Default)]
71-
pub struct SyncStakeEntry<const UNIT: u8, Address, Coins, Epoch, Power>
73+
pub struct SyncStakeEntry<const UNIT: u8, Address, Coins, Epoch, Nonce, Power>
7274
where
7375
Address: Clone + Default,
7476
Coins: Clone,
7577
Epoch: Clone + Default,
78+
Nonce: Clone + Default,
7679
Power: Clone,
7780
{
7881
/// A smart lock referring the key to which this stake entry belongs to.
7982
pub key: Rc<RwLock<StakeKey<Address>>>,
8083
/// A smart lock referring the stake data itself.
81-
pub value: Rc<RwLock<Stake<UNIT, Address, Coins, Epoch, Power>>>,
84+
pub value: Rc<RwLock<Stake<UNIT, Address, Coins, Epoch, Nonce, Power>>>,
8285
}
8386

84-
impl<const UNIT: u8, Address, Coins, Epoch, Power>
85-
SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>
87+
impl<const UNIT: u8, Address, Coins, Epoch, Nonce, Power>
88+
SyncStakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>
8689
where
8790
Address: Clone + Default + Serialize,
8891
Coins: Clone + Serialize,
8992
Epoch: Clone + Default + Serialize,
93+
Nonce: Clone + Default + Serialize,
9094
Power: Clone + Serialize,
9195
{
9296
/// Locks and reads both the stake key and the stake data referred by the stake entry.
93-
pub fn read_entry(&self) -> StakeEntry<UNIT, Address, Coins, Epoch, Power> {
97+
pub fn read_entry(&self) -> StakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power> {
9498
let key = self.read_key();
9599
let value = self.read_value();
96100

@@ -105,88 +109,95 @@ where
105109

106110
/// Locks and reads the stake data referred by the stake entry.
107111
#[inline]
108-
pub fn read_value(&self) -> Stake<UNIT, Address, Coins, Epoch, Power> {
112+
pub fn read_value(&self) -> Stake<UNIT, Address, Coins, Epoch, Nonce, Power> {
109113
self.value.read().unwrap().clone()
110114
}
111115
}
112116

113-
impl<const UNIT: u8, Address, Coins, Epoch, Power>
114-
From<StakeEntry<UNIT, Address, Coins, Epoch, Power>>
115-
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>
117+
impl<const UNIT: u8, Address, Coins, Epoch, Nonce, Power>
118+
From<StakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>>
119+
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>
116120
where
117121
Address: Clone + Default + Serialize,
118122
Coins: Clone + Serialize,
119123
Epoch: Clone + Default + Serialize,
124+
Nonce: Clone + Default + Serialize,
120125
Power: Clone + Serialize,
121126
{
122-
fn from(entry: StakeEntry<UNIT, Address, Coins, Epoch, Power>) -> Self {
127+
fn from(entry: StakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>) -> Self {
123128
SyncStakeEntry {
124129
key: Rc::new(RwLock::new(entry.key)),
125130
value: Rc::new(RwLock::new(entry.value)),
126131
}
127132
}
128133
}
129134

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>
135+
impl<const UNIT: u8, Address, Coins, Epoch, Nonce, Power>
136+
From<&SyncStakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>>
137+
for StakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>
133138
where
134139
Address: Clone + Default + Serialize,
135140
Coins: Clone + Serialize,
136141
Epoch: Clone + Default + Serialize,
142+
Nonce: Clone + Default + Serialize,
137143
Power: Clone + Serialize,
138144
{
139-
fn from(sync: &SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>) -> Self {
145+
fn from(sync: &SyncStakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>) -> Self {
140146
StakeEntry {
141147
key: sync.read_key(),
142148
value: sync.read_value(),
143149
}
144150
}
145151
}
146152

147-
impl<const UNIT: u8, Address, Coins, Epoch, Power> PartialEq
148-
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>
153+
impl<const UNIT: u8, Address, Coins, Epoch, Nonce, Power> PartialEq
154+
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>
149155
where
150156
Address: Clone + Default,
151-
Epoch: Clone + Default + PartialEq,
152157
Coins: Clone + PartialEq,
158+
Epoch: Clone + Default + PartialEq,
159+
Nonce: Clone + Default + PartialEq,
153160
Power: Clone,
154161
{
155162
fn eq(&self, other: &Self) -> bool {
156163
let self_stake = self.value.read().unwrap();
157164
let other_stake = other.value.read().unwrap();
158165

159-
self_stake.coins.eq(&other_stake.coins) && other_stake.epochs.eq(&other_stake.epochs)
166+
self_stake.coins.eq(&other_stake.coins)
167+
&& other_stake.epochs.eq(&other_stake.epochs)
168+
&& other_stake.nonce.eq(&other_stake.nonce)
160169
}
161170
}
162171

163-
impl<'de, const UNIT: u8, Address, Coins, Epoch, Power> Deserialize<'de>
164-
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>
172+
impl<'de, const UNIT: u8, Address, Coins, Epoch, Nonce, Power> Deserialize<'de>
173+
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>
165174
where
166175
Address: Clone + Default + Serialize,
167176
Coins: Clone + Serialize,
168177
Epoch: Clone + Default + Serialize,
178+
Nonce: Clone + Default + Serialize,
169179
Power: Clone + Serialize,
170-
StakeEntry<UNIT, Address, Coins, Epoch, Power>: Deserialize<'de>,
180+
StakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>: Deserialize<'de>,
171181
{
172182
#[inline]
173183
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
174184
where
175185
D: Deserializer<'de>,
176186
{
177-
<StakeEntry<UNIT, Address, Coins, Epoch, Power>>::deserialize(deserializer)
187+
<StakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>>::deserialize(deserializer)
178188
.map(SyncStakeEntry::from)
179189
}
180190
}
181191

182-
impl<const UNIT: u8, Address, Coins, Epoch, Power> Serialize
183-
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>
192+
impl<const UNIT: u8, Address, Coins, Epoch, Nonce, Power> Serialize
193+
for SyncStakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>
184194
where
185195
Address: Clone + Default + Serialize,
186196
Coins: Clone + Serialize,
187197
Epoch: Clone + Default + Serialize,
198+
Nonce: Clone + Default + Serialize,
188199
Power: Clone + Serialize,
189-
Stake<UNIT, Address, Coins, Epoch, Power>: Serialize,
200+
Stake<UNIT, Address, Coins, Epoch, Nonce, Power>: Serialize,
190201
{
191202
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
192203
where
@@ -292,15 +303,16 @@ pub enum CensusStrategy {
292303
Evenly(usize) = 3,
293304
}
294305

295-
impl<const UNIT: u8, Address, Coins, Epoch, Power> Serialize
296-
for Stakes<UNIT, Address, Coins, Epoch, Power>
306+
impl<const UNIT: u8, Address, Coins, Epoch, Nonce, Power> Serialize
307+
for Stakes<UNIT, Address, Coins, Epoch, Nonce, Power>
297308
where
298309
Address: Clone + Default + Ord,
299310
Coins: Clone + Ord,
300311
Epoch: Clone + Default,
312+
Nonce: Clone + Default,
301313
Power: Clone,
302314
StakeKey<Address>: Serialize,
303-
SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>: Serialize,
315+
SyncStakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>: Serialize,
304316
{
305317
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
306318
where
@@ -310,8 +322,8 @@ where
310322
}
311323
}
312324

313-
impl<'de, const UNIT: u8, Address, Coins, Epoch, Power> Deserialize<'de>
314-
for Stakes<UNIT, Address, Coins, Epoch, Power>
325+
impl<'de, const UNIT: u8, Address, Coins, Epoch, Nonce, Power> Deserialize<'de>
326+
for Stakes<UNIT, Address, Coins, Epoch, Nonce, Power>
315327
where
316328
Address: Clone
317329
+ Debug
@@ -354,6 +366,17 @@ where
354366
+ Sync
355367
+ Add<Output = Epoch>
356368
+ Div<Output = Epoch>,
369+
Nonce: AddAssign
370+
+ Copy
371+
+ Debug
372+
+ Default
373+
+ DeserializeOwned
374+
+ Display
375+
+ From<u32>
376+
+ Saturating
377+
+ Send
378+
+ Serialize
379+
+ Sync,
357380
Power: Add<Output = Power>
358381
+ Copy
359382
+ Default
@@ -368,20 +391,22 @@ where
368391
where
369392
D: Deserializer<'de>,
370393
{
371-
deserializer.deserialize_map(StakesVisitor::<UNIT, Address, Coins, Epoch, Power>::default())
394+
deserializer
395+
.deserialize_map(StakesVisitor::<UNIT, Address, Coins, Epoch, Nonce, Power>::default())
372396
}
373397
}
374398

375399
#[derive(Default)]
376-
struct StakesVisitor<const UNIT: u8, Address, Coins, Epoch, Power> {
400+
struct StakesVisitor<const UNIT: u8, Address, Coins, Epoch, Nonce, Power> {
377401
phantom_address: PhantomData<Address>,
378402
phantom_coins: PhantomData<Coins>,
379403
phantom_epoch: PhantomData<Epoch>,
404+
phantom_action: PhantomData<Nonce>,
380405
phantom_power: PhantomData<Power>,
381406
}
382407

383-
impl<'de, const UNIT: u8, Address, Coins, Epoch, Power> Visitor<'de>
384-
for StakesVisitor<UNIT, Address, Coins, Epoch, Power>
408+
impl<'de, const UNIT: u8, Address, Coins, Epoch, Nonce, Power> Visitor<'de>
409+
for StakesVisitor<UNIT, Address, Coins, Epoch, Nonce, Power>
385410
where
386411
Address: Clone
387412
+ Debug
@@ -424,6 +449,17 @@ where
424449
+ Sync
425450
+ Add<Output = Epoch>
426451
+ Div<Output = Epoch>,
452+
Nonce: AddAssign
453+
+ Copy
454+
+ Debug
455+
+ Default
456+
+ DeserializeOwned
457+
+ Display
458+
+ From<u32>
459+
+ Saturating
460+
+ Send
461+
+ Serialize
462+
+ Sync,
427463
Power: Add<Output = Power>
428464
+ Copy
429465
+ Default
@@ -434,7 +470,7 @@ where
434470
+ Sum,
435471
u64: From<Coins> + From<Power>,
436472
{
437-
type Value = Stakes<UNIT, Address, Coins, Epoch, Power>;
473+
type Value = Stakes<UNIT, Address, Coins, Epoch, Nonce, Power>;
438474

439475
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
440476
formatter.write_str("Stakes<Address, Coins, Epoch, Power>")
@@ -444,9 +480,10 @@ where
444480
where
445481
A: MapAccess<'de>,
446482
{
447-
let mut entries =
448-
<BTreeMap<StakeKey<Address>, SyncStakeEntry<UNIT, Address, Coins, Epoch, Power>>>::new(
449-
);
483+
let mut entries = <BTreeMap<
484+
StakeKey<Address>,
485+
SyncStakeEntry<UNIT, Address, Coins, Epoch, Nonce, Power>,
486+
>>::new();
450487

451488
while let Some((key, value)) = map.next_entry()? {
452489
entries.insert(key, value);
@@ -464,9 +501,9 @@ mod test {
464501

465502
#[test]
466503
fn test_cloning_assumptions() {
467-
let a = SyncStakeEntry::<0, String, u64, u64, u64>::from(StakeEntry {
504+
let a = SyncStakeEntry::<0, String, u64, u64, u64, u64>::from(StakeEntry {
468505
key: Default::default(),
469-
value: Stake::from_parts(123, Default::default()),
506+
value: Stake::from_parts(123, Default::default(), Default::default()),
470507
});
471508
let b = a.clone();
472509

0 commit comments

Comments
 (0)