Skip to content

Commit

Permalink
Merge pull request #302 from pyth-network/feat/publisher-caps
Browse files Browse the repository at this point in the history
feat: add publisher caps to the validator
  • Loading branch information
guibescos authored Jul 31, 2024
2 parents d873725 + 2f5eeb2 commit 4b32928
Show file tree
Hide file tree
Showing 6 changed files with 529 additions and 172 deletions.
96 changes: 71 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ num-traits = { version = "0.2" }
num_cpus = "1.13.1"
once_cell = "1.12.0"
ouroboros = "0.15.0"
pyth-oracle = { git = "https://github.com/pyth-network/pyth-client", tag = "oracle-v2.32.1", features = ["library"] }
pythnet-sdk = { git = "https://github.com/pyth-network/pyth-crosschain", version = "1.13.6", rev = "e670f57f89b05398ca352e4accb1e32724a8e1b4" }
pyth-oracle = { git = "https://github.com/pyth-network/pyth-client", tag = "oracle-v2.33.0", features = ["library"] }
pythnet-sdk = { git = "https://github.com/pyth-network/pyth-crosschain", version = "1.13.6", rev = "33f901aa45f4f0005aa5a84a1479b78ca9033074" }
rand = "0.7.0"
rayon = "1.5.3"
regex = "1.5.6"
Expand Down
59 changes: 57 additions & 2 deletions runtime/src/bank/pyth_accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
pythnet_sdk::{
accumulators::{merkle::MerkleAccumulator, Accumulator},
hashers::keccak256_160::Keccak160,
publisher_stake_caps::StakeCapParameters,
wormhole::{AccumulatorSequenceTracker, MessageData, PostedMessageUnreliableData},
},
solana_measure::measure::Measure,
Expand Down Expand Up @@ -44,6 +45,12 @@ lazy_static! {
.parse()
.unwrap(),
);
pub static ref STAKE_CAPS_PARAMETERS_ADDR: Pubkey = env_pubkey_or(
"STAKE_CAPS_PARAMETERS_ADDR",
"879ZVNagiWaAKsWDjGVf8pLq1wUBeBz7sREjUh3hrU36"
.parse()
.unwrap(),
);
}

/// Accumulator specific error type. It would be nice to use `transaction::Error` but it does
Expand Down Expand Up @@ -121,6 +128,10 @@ pub fn get_accumulator_keys() -> Vec<(
("ACCUMULATOR_SEQUENCE_ADDR", Ok(*ACCUMULATOR_SEQUENCE_ADDR)),
("WORMHOLE_PID", Ok(*WORMHOLE_PID)),
("ORACLE_PID", Ok(*ORACLE_PID)),
(
"STAKE_CAPS_PARAMETERS_ADDR",
Ok(*STAKE_CAPS_PARAMETERS_ADDR),
),
]
}

Expand Down Expand Up @@ -408,11 +419,16 @@ pub fn update_v2(bank: &Bank) -> std::result::Result<(), AccumulatorUpdateErrorV
measure.as_us()
);

let mut measure = Measure::start("update_v2_aggregate_price");

let mut any_v1_aggregations = false;
let mut v2_messages = Vec::new();

if let Some(publisher_stake_caps_message) = compute_publisher_stake_caps(bank, &accounts) {
info!("PublisherStakeCaps: Adding publisher stake caps to the accumulator");
v2_messages.push(publisher_stake_caps_message);
}

let mut measure = Measure::start("update_v2_aggregate_price");

for (pubkey, mut account) in accounts {
let mut price_account_data = account.data().to_owned();

Expand Down Expand Up @@ -446,3 +462,42 @@ pub fn update_v2(bank: &Bank) -> std::result::Result<(), AccumulatorUpdateErrorV

update_v1(bank, &v2_messages, any_v1_aggregations)
}

pub fn compute_publisher_stake_caps(
bank: &Bank,
accounts: &[(Pubkey, AccountSharedData)],
) -> Option<Vec<u8>> {
let mut measure = Measure::start("compute_publisher_stake_caps");

let parameters: StakeCapParameters = {
let data = bank
.get_account_with_fixed_root(&STAKE_CAPS_PARAMETERS_ADDR)
.unwrap_or_default();
let data = data.data();
solana_sdk::borsh::try_from_slice_unchecked(data).unwrap_or_default()
};

let message = pyth_oracle::validator::compute_publisher_stake_caps(
accounts.iter().map(|(_, account)| account.data()),
bank.clock().unix_timestamp,
parameters.m,
parameters.z,
);

measure.stop();
debug!(
"PublisherStakeCaps: Computed publisher stake caps with m : {} and z : {} in {} us",
parameters.m,
parameters.z,
measure.as_us()
);

if bank
.feature_set
.is_active(&feature_set::add_publisher_stake_caps_to_the_accumulator::id())
{
Some(message)
} else {
None
}
}
Loading

0 comments on commit 4b32928

Please sign in to comment.