-
Notifications
You must be signed in to change notification settings - Fork 2
Add mock collector #24
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
Open
cmeisl
wants to merge
4
commits into
blocknative:main
Choose a base branch
from
cmeisl:add-mock-collector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| [workspace] | ||
| members = [ | ||
| ".", | ||
| "crates/mock-collector" | ||
| ] | ||
|
|
||
| [package] | ||
| name = "gas-agent" | ||
| version = "0.1.1" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [package] | ||
| name = "mock-collector" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| gas-agent = { path = "../.." } | ||
| tokio = { version = "1.44", features = ["full"] } | ||
| axum = "0.7" | ||
| serde = { version = "1.0", features = ["derive"] } | ||
| serde_json = "1.0" | ||
| tracing = "0.1" | ||
| tracing-subscriber = { version = "0.3", features = ["env-filter"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| use axum::{ | ||
| extract::rejection::JsonRejection, http::StatusCode, response::IntoResponse, routing::post, | ||
| Router, | ||
| }; | ||
| use gas_agent::AgentPayload; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::net::SocketAddr; | ||
| use tracing::{info, warn}; | ||
|
|
||
| /// The payload structure sent by the gas agent | ||
| #[derive(Debug, Deserialize, Serialize)] | ||
| struct AgentSubmission { | ||
| payload: AgentPayload, | ||
| signature: String, | ||
| network_signature: String, | ||
| } | ||
|
|
||
| async fn handle_agent_publish( | ||
| payload: Result<axum::Json<AgentSubmission>, JsonRejection>, | ||
| ) -> impl IntoResponse { | ||
| match payload { | ||
| Ok(axum::Json(submission)) => { | ||
| info!("═══════════════════════════════════════════════════════════════"); | ||
| info!("RECEIVED AGENT SUBMISSION"); | ||
| info!("═══════════════════════════════════════════════════════════════"); | ||
| info!("System: {:?}", submission.payload.system); | ||
| info!("Network: {:?}", submission.payload.network); | ||
| info!("From Block: {}", submission.payload.from_block); | ||
| info!("Settlement: {:?}", submission.payload.settlement); | ||
| info!( | ||
| "Price: {} {:?}", | ||
| submission.payload.price, submission.payload.unit | ||
| ); | ||
| info!("Timestamp: {}", submission.payload.timestamp); | ||
| info!("Schema Version: {}", submission.payload.schema_version); | ||
| info!("───────────────────────────────────────────────────────────────"); | ||
| info!( | ||
| "Signature: {}...", | ||
| &submission.signature[..20.min(submission.signature.len())] | ||
| ); | ||
| info!( | ||
| "Network Signature: {}...", | ||
| &submission.network_signature[..20.min(submission.network_signature.len())] | ||
| ); | ||
| info!("═══════════════════════════════════════════════════════════════"); | ||
| (StatusCode::OK, "OK".to_string()) | ||
| } | ||
| Err(rejection) => { | ||
| warn!("═══════════════════════════════════════════════════════════════"); | ||
| warn!("INVALID PAYLOAD RECEIVED"); | ||
| warn!("═══════════════════════════════════════════════════════════════"); | ||
| warn!("Error: {}", rejection); | ||
| if let JsonRejection::JsonDataError(ref err) = rejection { | ||
| warn!("Details: {}", err.body_text()); | ||
| } else if let JsonRejection::JsonSyntaxError(ref err) = rejection { | ||
| warn!("Details: {}", err.body_text()); | ||
| } | ||
| warn!("═══════════════════════════════════════════════════════════════"); | ||
| ( | ||
| StatusCode::BAD_REQUEST, | ||
| format!("Invalid payload: {}", rejection), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| // Initialize tracing | ||
| tracing_subscriber::fmt() | ||
| .with_env_filter( | ||
| tracing_subscriber::EnvFilter::from_default_env() | ||
| .add_directive(tracing::Level::INFO.into()), | ||
| ) | ||
| .init(); | ||
|
|
||
| let app = Router::new() | ||
| .route("/v0/agents", post(handle_agent_publish)) | ||
| .fallback(|req: axum::http::Request<axum::body::Body>| async move { | ||
| warn!("Unhandled request: {} {}", req.method(), req.uri()); | ||
| (StatusCode::NOT_FOUND, "Not Found") | ||
| }); | ||
|
|
||
| let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); | ||
| info!("Mock Collector listening on http://{}", addr); | ||
| info!("Expecting POST requests at http://{}/v0/agents", addr); | ||
|
|
||
| let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); | ||
| axum::serve(listener, app).await.unwrap(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| //! Gas Agent library - shared types for the gas agent ecosystem. | ||
|
|
||
| mod chain; | ||
| mod types; | ||
|
|
||
| pub use types::AgentPayload; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The library exports only
AgentPayload, but this struct has public fields of typesSettlement,System,Network, andPriceUnit. These types must also be exported from the library for consumers (like the mock-collector) to deserializeAgentPayloadinstances. Without these exports, the mock-collector won't be able to compile and use theAgentPayloadtype properly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lnbc1QWFyb24 The collector seems to work without these since it only imports
AgentPayload. Is the collector able to use those elements of the payload because it's just treating them as strings written to console?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this co-pilot recommendation does not make a lot of sense in this context. If you needed to create an
AgentPayloadthen you would need those enums, but since you are just logging public fields on the struct it is fine as it is.