Skip to content

Commit 4b32928

Browse files
authored
Merge pull request #302 from pyth-network/feat/publisher-caps
feat: add publisher caps to the validator
2 parents d873725 + 2f5eeb2 commit 4b32928

File tree

6 files changed

+529
-172
lines changed

6 files changed

+529
-172
lines changed

Cargo.lock

+71-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtime/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ num-traits = { version = "0.2" }
3535
num_cpus = "1.13.1"
3636
once_cell = "1.12.0"
3737
ouroboros = "0.15.0"
38-
pyth-oracle = { git = "https://github.com/pyth-network/pyth-client", tag = "oracle-v2.32.1", features = ["library"] }
39-
pythnet-sdk = { git = "https://github.com/pyth-network/pyth-crosschain", version = "1.13.6", rev = "e670f57f89b05398ca352e4accb1e32724a8e1b4" }
38+
pyth-oracle = { git = "https://github.com/pyth-network/pyth-client", tag = "oracle-v2.33.0", features = ["library"] }
39+
pythnet-sdk = { git = "https://github.com/pyth-network/pyth-crosschain", version = "1.13.6", rev = "33f901aa45f4f0005aa5a84a1479b78ca9033074" }
4040
rand = "0.7.0"
4141
rayon = "1.5.3"
4242
regex = "1.5.6"

runtime/src/bank/pyth_accumulator.rs

+57-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use {
77
pythnet_sdk::{
88
accumulators::{merkle::MerkleAccumulator, Accumulator},
99
hashers::keccak256_160::Keccak160,
10+
publisher_stake_caps::StakeCapParameters,
1011
wormhole::{AccumulatorSequenceTracker, MessageData, PostedMessageUnreliableData},
1112
},
1213
solana_measure::measure::Measure,
@@ -44,6 +45,12 @@ lazy_static! {
4445
.parse()
4546
.unwrap(),
4647
);
48+
pub static ref STAKE_CAPS_PARAMETERS_ADDR: Pubkey = env_pubkey_or(
49+
"STAKE_CAPS_PARAMETERS_ADDR",
50+
"879ZVNagiWaAKsWDjGVf8pLq1wUBeBz7sREjUh3hrU36"
51+
.parse()
52+
.unwrap(),
53+
);
4754
}
4855

4956
/// Accumulator specific error type. It would be nice to use `transaction::Error` but it does
@@ -121,6 +128,10 @@ pub fn get_accumulator_keys() -> Vec<(
121128
("ACCUMULATOR_SEQUENCE_ADDR", Ok(*ACCUMULATOR_SEQUENCE_ADDR)),
122129
("WORMHOLE_PID", Ok(*WORMHOLE_PID)),
123130
("ORACLE_PID", Ok(*ORACLE_PID)),
131+
(
132+
"STAKE_CAPS_PARAMETERS_ADDR",
133+
Ok(*STAKE_CAPS_PARAMETERS_ADDR),
134+
),
124135
]
125136
}
126137

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

411-
let mut measure = Measure::start("update_v2_aggregate_price");
412-
413422
let mut any_v1_aggregations = false;
414423
let mut v2_messages = Vec::new();
415424

425+
if let Some(publisher_stake_caps_message) = compute_publisher_stake_caps(bank, &accounts) {
426+
info!("PublisherStakeCaps: Adding publisher stake caps to the accumulator");
427+
v2_messages.push(publisher_stake_caps_message);
428+
}
429+
430+
let mut measure = Measure::start("update_v2_aggregate_price");
431+
416432
for (pubkey, mut account) in accounts {
417433
let mut price_account_data = account.data().to_owned();
418434

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

447463
update_v1(bank, &v2_messages, any_v1_aggregations)
448464
}
465+
466+
pub fn compute_publisher_stake_caps(
467+
bank: &Bank,
468+
accounts: &[(Pubkey, AccountSharedData)],
469+
) -> Option<Vec<u8>> {
470+
let mut measure = Measure::start("compute_publisher_stake_caps");
471+
472+
let parameters: StakeCapParameters = {
473+
let data = bank
474+
.get_account_with_fixed_root(&STAKE_CAPS_PARAMETERS_ADDR)
475+
.unwrap_or_default();
476+
let data = data.data();
477+
solana_sdk::borsh::try_from_slice_unchecked(data).unwrap_or_default()
478+
};
479+
480+
let message = pyth_oracle::validator::compute_publisher_stake_caps(
481+
accounts.iter().map(|(_, account)| account.data()),
482+
bank.clock().unix_timestamp,
483+
parameters.m,
484+
parameters.z,
485+
);
486+
487+
measure.stop();
488+
debug!(
489+
"PublisherStakeCaps: Computed publisher stake caps with m : {} and z : {} in {} us",
490+
parameters.m,
491+
parameters.z,
492+
measure.as_us()
493+
);
494+
495+
if bank
496+
.feature_set
497+
.is_active(&feature_set::add_publisher_stake_caps_to_the_accumulator::id())
498+
{
499+
Some(message)
500+
} else {
501+
None
502+
}
503+
}

0 commit comments

Comments
 (0)