-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sbpf build don't like bitflags!. so implement the flags by ourself.
- Loading branch information
HaoranYi
committed
Jun 13, 2023
1 parent
e8556ce
commit 8633b5a
Showing
7 changed files
with
102 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
pub mod config; | ||
pub mod instruction; | ||
pub mod stake_flags; | ||
pub mod state; | ||
pub mod tools; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; | ||
|
||
/// Additional flags for stake state. | ||
#[derive( | ||
Serialize, | ||
Deserialize, | ||
AbiExample, | ||
BorshDeserialize, | ||
BorshSchema, | ||
BorshSerialize, | ||
Copy, | ||
PartialEq, | ||
Eq, | ||
Clone, | ||
PartialOrd, | ||
Ord, | ||
Hash, | ||
Debug, | ||
)] | ||
pub struct StakeFlags { | ||
bits: u8, | ||
} | ||
|
||
/// Currently, only bit 1 is used. The other 7 bits are reserved for future usage. | ||
impl StakeFlags { | ||
/// Stake must be fully activated before deactivation is allowed (bit 1). | ||
pub const MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED: Self = | ||
Self { bits: 0b0000_0001 }; | ||
|
||
pub const fn empty() -> Self { | ||
Self { bits: 0 } | ||
} | ||
|
||
pub const fn contains(&self, other: Self) -> bool { | ||
(self.bits & other.bits) == other.bits | ||
} | ||
|
||
pub fn remove(&mut self, other: Self) { | ||
self.bits &= !other.bits; | ||
} | ||
|
||
pub fn set(&mut self, other: Self) { | ||
self.bits |= other.bits; | ||
} | ||
|
||
pub const fn union(self, other: Self) -> Self { | ||
Self { | ||
bits: self.bits | other.bits, | ||
} | ||
} | ||
} | ||
|
||
impl Default for StakeFlags { | ||
fn default() -> Self { | ||
StakeFlags::empty() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_stake_flags() { | ||
let mut f = StakeFlags::empty(); | ||
assert!(!f.contains(StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED)); | ||
|
||
f.set(StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED); | ||
assert!(f.contains(StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED)); | ||
|
||
f.remove(StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED); | ||
assert!(!f.contains(StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED)); | ||
|
||
let f1 = StakeFlags::empty(); | ||
let f2 = StakeFlags::empty(); | ||
let f3 = f1.union(f2); | ||
assert!(!f3.contains(StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED)); | ||
|
||
let f1 = StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED; | ||
let f2 = StakeFlags::empty(); | ||
let f3 = f1.union(f2); | ||
assert!(f3.contains(StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED)); | ||
|
||
let f1 = StakeFlags::empty(); | ||
let f2 = StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED; | ||
let f3 = f1.union(f2); | ||
assert!(f3.contains(StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED)); | ||
|
||
let f1 = StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED; | ||
let f2 = StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED; | ||
let f3 = f1.union(f2); | ||
assert!(f3.contains(StakeFlags::MUST_FULLY_ACTIVATE_BEFORE_DEACTIVATION_IS_PERMITTED)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters