Skip to content

Commit

Permalink
feat(hermes): add tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Reisen authored and ali-bahjati committed Sep 4, 2023
1 parent 0559b45 commit 938abd1
Show file tree
Hide file tree
Showing 13 changed files with 524 additions and 132 deletions.
75 changes: 75 additions & 0 deletions contract_manager/scripts/bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { DefaultStore } from "../src";
import { PriceFeed, PriceServiceConnection } from "@pythnetwork/price-service-client";
import { ethers } from "ethers";
import { PrivateKey, toPrivateKey } from "../lib";
import {sleep} from "pyth_relay/lib/helpers";

const parser = yargs(hideBin(process.argv))
.scriptName("update_pricefeed.ts")
.usage(
"Usage: $0"
)
.options({});

async function main() {
const mainnetFeedIds = [
"e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43",
"ff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace",
"63f341689d98a12ef60a5cff1d7f85c70a9e17bf1575f0e7c0b2512d48b1c8b3",
"7677dd124dee46cfcd46ff03cf405fb0ed94b1f49efbea3444aadbda939a7ad3",
"14eb6f846b84f37c841ce7a52a38706e54966df84b3a09cc40499b164af05672",
"fa2d39b681f3cef5fa87432a8dbd05113917fffb1b6829a37395c88396522a4e",
"7a5bc1d2b56ad029048cd63964b3ad2776eadf812edc1a43a31406cb54bff592",
"abe4f2b264560a397f38eec024369356e5c1ea4f7aab94729369f144b3d97779",
"5fcf71143bb70d41af4fa9aa1287e2efd3c5911cee59f909f915c9f61baacb1e",
"b82449fd728133488d2d41131cffe763f9c1693b73c544d9ef6aaa371060dd25",
];

let feedIds: string[] = [];
feedIds = [mainnetFeedIds[0]];
const defaultEndpoint = "https://xc-mainnet.pyth.network";
const hermesEndpoint = "http://127.0.0.1:7575";

const priceService = new PriceServiceConnection(defaultEndpoint, {verbose: true});
const hermes = new PriceServiceConnection(hermesEndpoint, {verbose: true});

const origUpdates: PriceFeed[] = [];
const hermesUpdates: PriceFeed[] = [];

let priceServiceSum = 0;
let priceServiceCount = 0;

priceService.subscribePriceFeedUpdates(feedIds, (feed) => {
const publishTime = feed.getPriceUnchecked().publishTime;
// const now = (new Date()).getTime();
// const diff = now / 1000 - publishTime;
const receivedTime = feed.getMetadata()?.priceServiceReceiveTime!;
const diff = receivedTime - publishTime;

priceServiceSum += diff;
priceServiceCount += 1;

console.log(`p: ${priceServiceSum/priceServiceCount} ${diff} ${feed.getPriceUnchecked().price} ${JSON.stringify(feed.getMetadata())}`)
origUpdates.push(feed);
})

let hermesSum = 0;
let hermesCount = 0;

hermes.subscribePriceFeedUpdates(feedIds, (feed) => {
const publishTime = feed.getPriceUnchecked().publishTime;
// const now = (new Date()).getTime();
// const diff = now / 1000 - publishTime;
const receivedTime = feed.getMetadata()?.priceServiceReceiveTime!;
const diff = receivedTime - publishTime;

hermesSum += diff;
hermesCount += 1;
console.log(`h: ${hermesSum/hermesCount} ${diff} ${feed.getPriceUnchecked().price} ${JSON.stringify(feed.getMetadata())}`)
hermesUpdates.push(feed);
})
}

main();
76 changes: 73 additions & 3 deletions hermes/Cargo.lock

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

71 changes: 37 additions & 34 deletions hermes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,43 @@ version = "0.1.13"
edition = "2021"

[dependencies]
anyhow = { version = "1.0.69" }
axum = { version = "0.6.20", features = ["json", "ws", "macros"] }
axum-macros = { version = "0.3.8" }
base64 = { version = "0.21.0" }
borsh = { version = "0.10.3" }
byteorder = { version = "1.4.3" }
dashmap = { version = "5.4.0" }
derive_more = { version = "0.99.17" }
env_logger = { version = "0.10.0" }
futures = { version = "0.3.28" }
hex = { version = "0.4.3" }
humantime = { version = "2.1.0" }
lazy_static = { version = "1.4.0" }
libc = { version = "0.2.140" }
log = { version = "0.4.17" }
mock_instant = { version = "0.3.1", features = ["sync"] }
prometheus-client = { version = "0.21.1" }
pyth-sdk = { version = "0.8.0" }
pythnet-sdk = { path = "../pythnet/pythnet_sdk/", version = "2.0.0", features = ["strum"] }
rand = { version = "0.8.5" }
reqwest = { version = "0.11.14", features = ["blocking", "json"] }
secp256k1 = { version = "0.27.0", features = ["rand", "recovery", "serde"] }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0.93" }
serde_qs = { version = "0.12.0", features = ["axum"] }
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", tag = "v2.17.1" }
sha3 = { version = "0.10.4" }
structopt = { version = "0.3.26" }
strum = { version = "0.24.1", features = ["derive"] }
tokio = { version = "1.26.0", features = ["full"] }
tower-http = { version = "0.4.0", features = ["cors"] }
utoipa = { version = "3.4.0", features = ["axum_extras"] }
utoipa-swagger-ui = { version = "3.1.4", features = ["axum"] }
wormhole-sdk = { git = "https://github.com/wormhole-foundation/wormhole", tag = "v2.17.1" }
anyhow = { version = "1.0.69" }
axum = { version = "0.6.20", features = ["json", "ws", "macros"] }
axum-macros = { version = "0.3.8" }
base64 = { version = "0.21.0" }
borsh = { version = "0.10.3" }
byteorder = { version = "1.4.3" }
chrono = { version = "0.4.28" }
dashmap = { version = "5.4.0" }
derive_more = { version = "0.99.17" }
env_logger = { version = "0.10.0" }
futures = { version = "0.3.28" }
hex = { version = "0.4.3" }
humantime = { version = "2.1.0" }
lazy_static = { version = "1.4.0" }
libc = { version = "0.2.140" }
log = { version = "0.4.17" }
mock_instant = { version = "0.3.1", features = ["sync"] }
prometheus-client = { version = "0.21.1" }
pyth-sdk = { version = "0.8.0" }
pythnet-sdk = { path = "../pythnet/pythnet_sdk/", version = "2.0.0", features = ["strum"] }
rand = { version = "0.8.5" }
reqwest = { version = "0.11.14", features = ["blocking", "json"] }
secp256k1 = { version = "0.27.0", features = ["rand", "recovery", "serde"] }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0.93" }
serde_qs = { version = "0.12.0", features = ["axum"] }
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", tag = "v2.17.1" }
sha3 = { version = "0.10.4" }
structopt = { version = "0.3.26" }
strum = { version = "0.24.1", features = ["derive"] }
tokio = { version = "1.26.0", features = ["full"] }
tower-http = { version = "0.4.0", features = ["cors"] }
tracing = { version = "0.1.37", features = ["log"] }
tracing-subscriber = { version = "0.3.17" }
utoipa = { version = "3.4.0", features = ["axum_extras"] }
utoipa-swagger-ui = { version = "3.1.4", features = ["axum"] }
wormhole-sdk = { git = "https://github.com/wormhole-foundation/wormhole", tag = "v2.17.1" }

# Setup LibP2P. Unfortunately the dependencies required by libp2p are shared
# with the dependencies required by many Solana components. This means that we
Expand Down
7 changes: 4 additions & 3 deletions hermes/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ impl State {
///
/// Currently this is based on Axum due to the simplicity and strong ecosystem support for the
/// packages they are based on (tokio & hyper).
#[tracing::instrument(skip(opts, store, update_rx))]
pub async fn run(opts: RunOptions, store: Arc<Store>, mut update_rx: Receiver<()>) -> Result<()> {
log::info!("Starting RPC server on {}", opts.api_addr);
tracing::info!(endpoint = %opts.api_addr, "Starting RPC Server.");

#[derive(OpenApi)]
#[openapi(
Expand Down Expand Up @@ -109,15 +110,15 @@ pub async fn run(opts: RunOptions, store: Arc<Store>, mut update_rx: Receiver<()
// Causes a full application shutdown if an error occurs, we can't recover from this so
// we just quit.
if update_rx.recv().await.is_none() {
log::error!("Failed to receive update from store.");
tracing::error!("Failed to receive update from store.");
crate::SHOULD_EXIT.store(true, Ordering::Release);
break;
}

notify_updates(state.ws.clone()).await;
}

log::info!("Shutting down websocket updates...")
tracing::info!("Shutting down websocket updates...")
});

// Binds the axum's server to the configured address and port. This is a blocking call and will
Expand Down
Loading

0 comments on commit 938abd1

Please sign in to comment.