Parcourir la source

perf: use solana hash precompile (#1400)

guibescos il y a 1 an
Parent
commit
0e6484daca

+ 2 - 0
pythnet/pythnet_sdk/Cargo.toml

@@ -13,6 +13,7 @@ name = "pythnet_sdk"
 
 [features]
 test-utils = ["dep:wormhole-vaas-serde", "dep:serde_wormhole", "dep:libsecp256k1", "dep:rand"]
+solana-program = ["dep:solana-program"]
 
 [dependencies]
 bincode = "1.3.1"
@@ -31,6 +32,7 @@ serde_wormhole = {version ="0.1.0", optional = true}
 wormhole-vaas-serde = {version = "0.1.0", optional = true}
 libsecp256k1 = {version ="0.7.1", optional = true}
 rand = {version = "0.8.5", optional = true}
+solana-program = {version = ">=1.13.6", optional = true}
 
 [dev-dependencies]
 base64 = "0.21.0"

+ 17 - 4
pythnet/pythnet_sdk/src/hashers/keccak256_160.rs

@@ -1,18 +1,31 @@
+#[cfg(not(feature = "solana-program"))]
+use sha3::{
+    Digest,
+    Keccak256,
+};
+#[cfg(feature = "solana-program")]
+use solana_program::keccak::hashv;
 use {
     crate::hashers::Hasher,
     serde::Serialize,
-    sha3::{
-        Digest,
-        Keccak256,
-    },
 };
 
+
 #[derive(Clone, Default, Debug, Eq, Hash, PartialEq, Serialize)]
 pub struct Keccak160 {}
 
 impl Hasher for Keccak160 {
     type Hash = [u8; 20];
 
+    #[cfg(feature = "solana-program")]
+    fn hashv(data: &[impl AsRef<[u8]>]) -> Self::Hash {
+        let bytes = hashv(&data.iter().map(|x| x.as_ref()).collect::<Vec<&[u8]>>());
+        let mut hash = [0u8; 20];
+        hash.copy_from_slice(&bytes.as_ref()[0..20]);
+        hash
+    }
+
+    #[cfg(not(feature = "solana-program"))]
     fn hashv(data: &[impl AsRef<[u8]>]) -> [u8; 20] {
         let mut hasher = Keccak256::new();
         data.iter().for_each(|d| hasher.update(d));

+ 1 - 0
target_chains/solana/Cargo.lock

@@ -3095,6 +3095,7 @@ dependencies = [
  "serde_wormhole",
  "sha3 0.10.6",
  "slow_primes",
+ "solana-program",
  "thiserror",
  "wormhole-vaas-serde",
 ]

+ 1 - 1
target_chains/solana/programs/pyth-solana-receiver/Cargo.toml

@@ -17,7 +17,7 @@ test-bpf = []
 
 [dependencies]
 anchor-lang = { workspace = true }
-pythnet-sdk = { path = "../../../../pythnet/pythnet_sdk" }
+pythnet-sdk = { path = "../../../../pythnet/pythnet_sdk",  features = ["solana-program"] }
 solana-program = { workspace = true }
 byteorder = "1.4.3"
 wormhole-core-bridge-solana = {workspace = true}

+ 3 - 3
target_chains/solana/sdk/js/pyth_solana_receiver/src/compute_budget.ts

@@ -1,12 +1,12 @@
 /**
  * A hard-coded budget for the compute units required for the `verifyEncodedVaa` instruction in the Wormhole program.
  */
-export const VERIFY_ENCODED_VAA_COMPUTE_BUDGET = 400000;
+export const VERIFY_ENCODED_VAA_COMPUTE_BUDGET = 350000;
 /**
  * A hard-coded budget for the compute units required for the `postUpdateAtomic` instruction in the Pyth Solana Receiver program.
  */
-export const POST_UPDATE_ATOMIC_COMPUTE_BUDGET = 400000;
+export const POST_UPDATE_ATOMIC_COMPUTE_BUDGET = 170000;
 /**
  * A hard-coded budget for the compute units required for the `postUpdate` instruction in the Pyth Solana Receiver program.
  */
-export const POST_UPDATE_COMPUTE_BUDGET = 200000;
+export const POST_UPDATE_COMPUTE_BUDGET = 35000;