Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add publisher stake caps to validator #1778

Merged
merged 16 commits into from
Jul 31, 2024
59 changes: 52 additions & 7 deletions pythnet/pythnet_sdk/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use borsh::{
#[cfg(feature = "quickcheck")]
use quickcheck::Arbitrary;
use {
crate::wire::PrefixedVec,
borsh::BorshSchema,
serde::{
Deserialize,
Expand All @@ -30,7 +31,7 @@ use {
/// some of the methods for PriceFeedMessage and TwapMessage are not used by the oracle
/// for the same reason. Rust compiler doesn't include the unused methods in the contract.
/// Once we start using the unused structs and methods, the contract size will increase.
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
guibescos marked this conversation as resolved.
Show resolved Hide resolved
#[cfg_attr(
feature = "strum",
derive(strum::EnumDiscriminants),
Expand All @@ -50,20 +51,23 @@ use {
pub enum Message {
PriceFeedMessage(PriceFeedMessage),
TwapMessage(TwapMessage),
PublisherCapsMessage(PublisherCapsMessage),
}

impl Message {
pub fn publish_time(&self) -> i64 {
match self {
Self::PriceFeedMessage(msg) => msg.publish_time,
Self::TwapMessage(msg) => msg.publish_time,
Self::PublisherCapsMessage(msg) => msg.publish_time,
}
}

pub fn feed_id(&self) -> FeedId {
match self {
Self::PriceFeedMessage(msg) => msg.feed_id,
Self::TwapMessage(msg) => msg.feed_id,
Self::PublisherCapsMessage(_) => [0u8; 32],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new type of message breaks this

}
}
}
Expand Down Expand Up @@ -116,15 +120,15 @@ pub struct PriceFeedMessage {
#[cfg(feature = "quickcheck")]
impl Arbitrary for PriceFeedMessage {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
let mut id = [0u8; 32];
for item in &mut id {
let mut feed_id = [0u8; 32];
for item in &mut feed_id {
*item = u8::arbitrary(g);
}

let publish_time = i64::arbitrary(g);

PriceFeedMessage {
id,
feed_id,
price: i64::arbitrary(g),
conf: u64::arbitrary(g),
exponent: i32::arbitrary(g),
Expand Down Expand Up @@ -153,15 +157,15 @@ pub struct TwapMessage {
#[cfg(feature = "quickcheck")]
impl Arbitrary for TwapMessage {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
let mut id = [0u8; 32];
for item in &mut id {
let mut feed_id = [0u8; 32];
for item in &mut feed_id {
*item = u8::arbitrary(g);
}

let publish_time = i64::arbitrary(g);

TwapMessage {
id,
feed_id,
cumulative_price: i128::arbitrary(g),
cumulative_conf: u128::arbitrary(g),
num_down_slots: u64::arbitrary(g),
Expand All @@ -173,6 +177,47 @@ impl Arbitrary for TwapMessage {
}
}

#[repr(C)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PublisherCapsMessage {
pub publish_time: i64,
pub caps: PrefixedVec<u16, PublisherCap>,
}

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub struct PublisherCap {
pub publisher: [u8; 32],
pub cap: u64,
}

#[cfg(feature = "quickcheck")]
impl Arbitrary for PublisherCapsMessage {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
let caps = Vec::arbitrary(g);
PublisherCapsMessage {
publish_time: i64::arbitrary(g),
caps: caps.into(),
}
}
}

#[cfg(feature = "quickcheck")]
impl Arbitrary for PublisherCap {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
PublisherCap {
publisher: {
let mut publisher = [0u8; 32];
for item in &mut publisher {
*item = u8::arbitrary(g);
}
publisher
},
cap: u64::arbitrary(g),
}
}
}

#[cfg(test)]
mod tests {

Expand Down
Loading