From cec63e7808f13f64e66b0b60698c53be1aa2e35e Mon Sep 17 00:00:00 2001 From: Denis Carriere Date: Thu, 2 Feb 2023 10:03:23 -0500 Subject: [PATCH] Update eos mechanics --- eosmechanics/README.md | 22 +++++--- eosmechanics/proto/v1/eosmechanics.proto | 6 +-- eosmechanics/proto/v1/sink.prom.proto | 8 +++ eosmechanics/src/lib.rs | 5 ++ eosmechanics/src/maps.rs | 51 +++++++++++------- eosmechanics/src/pb/eosmechanics.v1.rs | 63 ++++++++-------------- eosmechanics/src/pb/pinax.sinks.prom.v1.rs | 24 +++++++++ eosmechanics/substreams.yaml | 14 ++++- 8 files changed, 119 insertions(+), 74 deletions(-) create mode 100644 eosmechanics/proto/v1/sink.prom.proto create mode 100644 eosmechanics/src/pb/pinax.sinks.prom.v1.rs diff --git a/eosmechanics/README.md b/eosmechanics/README.md index 81c7f60..48742eb 100644 --- a/eosmechanics/README.md +++ b/eosmechanics/README.md @@ -5,15 +5,17 @@ ### Quickstart ``` -$ substreams run -e eos.firehose.eosnation.io:9001 map_block_stats -t +10 +$ substreams run -e eos.firehose.eosnation.io:9001 map_producer_usage -s 292180468 -t +1 ``` ### Mermaid graph ```mermaid graph TD; - map_block_stats[map: map_block_stats] - sf.antelope.type.v2.Block[source: sf.antelope.type.v2.Block] --> map_block_stats + map_producer_usage[map: map_producer_usage] + sf.antelope.type.v2.Block[source: sf.antelope.type.v2.Block] --> map_producer_usage + map_prom_out[map: map_prom_out] + map_producer_usage --> map_prom_out ``` image @@ -27,10 +29,16 @@ Package name: eosmechanics Version: v0.1.0 Doc: Block Producer Benchmarks Modules: - ---- -Name: map_block_stats +---- +Name: map_producer_usage Initial block: 0 Kind: map -Output Type: proto:eosmechanics.v1.BlockStats -Hash: e71870fcf747d120a130501211e1ad8770cc44b8 +Output Type: proto:eosmechanics.v1.ProducerUsage +Hash: 7c6074773130dc85c3022d6b56fca8f21f06d8cf + +Name: map_prom_out +Initial block: 0 +Kind: map +Output Type: proto:pinax.substreams.sinks.prom.v1.PrometheusMetrics +Hash: a27326168962800fae976825c6173a6539cd8702 ``` \ No newline at end of file diff --git a/eosmechanics/proto/v1/eosmechanics.proto b/eosmechanics/proto/v1/eosmechanics.proto index 91afaf1..1a900e5 100644 --- a/eosmechanics/proto/v1/eosmechanics.proto +++ b/eosmechanics/proto/v1/eosmechanics.proto @@ -2,11 +2,7 @@ syntax = "proto3"; package eosmechanics.v1; -message BlockResults { - repeated ProducerStats producer_stats = 1; -} - -message ProducerStats { +message ProducerUsage { string producer = 1; // the name of the block producer int64 cpu_usage = 2; // cpu usage of the eosmechanics transaction } \ No newline at end of file diff --git a/eosmechanics/proto/v1/sink.prom.proto b/eosmechanics/proto/v1/sink.prom.proto new file mode 100644 index 0000000..961146b --- /dev/null +++ b/eosmechanics/proto/v1/sink.prom.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +package pinax.sinks.prom.v1; + +message PrometheusMetrics { + // TO-DO + // Reference https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#Gauge +} \ No newline at end of file diff --git a/eosmechanics/src/lib.rs b/eosmechanics/src/lib.rs index 46622c7..63aaa8c 100644 --- a/eosmechanics/src/lib.rs +++ b/eosmechanics/src/lib.rs @@ -3,4 +3,9 @@ pub mod eosmechanics; pub use self::eosmechanics::*; +#[path = "pb/pinax.sinks.prom.v1.rs"] +#[allow(dead_code)] +pub mod sinks; +pub use self::sinks::*; + mod maps; diff --git a/eosmechanics/src/maps.rs b/eosmechanics/src/maps.rs index 2cbf42b..a1ba749 100644 --- a/eosmechanics/src/maps.rs +++ b/eosmechanics/src/maps.rs @@ -1,30 +1,41 @@ use substreams::errors::Error; -use substreams::{log, prelude::*}; +use substreams::log; use substreams_antelope::Block; -use crate::eosmechanics::{BlockResults, ProducerStats}; +use crate::eosmechanics::ProducerUsage; +use crate::sinks::PrometheusMetrics; #[substreams::handlers::map] -pub fn map_block_stats(block: Block) -> Result { +pub fn map_producer_usage(block: Block) -> Result { - // ToDo parse the transaction traces of the block - // if it contains an action on account "eosmechanics" with the name "cpu" then take the - // cpu time of the transaction and the name of the producer and return it. + // Producer is found in the block header + let producer = block.clone().header.unwrap().producer; + + for trx in block.clone().all_transaction_traces() { + // CPU usage is found in the transaction receipt + let cpu_usage = trx.clone().receipt.unwrap().cpu_usage_micro_seconds as i64; - // the producer can be found on the block header using: - // block.header.unwrap().producer + // Only return a value if the transaction trace contains `eosmechanics:cpu` action + for trace in trx.clone().action_traces { + let action_trace = trace.action.as_ref().unwrap().clone(); + if action_trace.account != "eosmechanics" { continue; } + if action_trace.name != "cpu" { continue; } + return Ok(ProducerUsage{ + producer, + cpu_usage, + }) + } + } - // the cpu usage can be found in the transaction receipt - - Ok(BlockResults{ - producer_stats: vec![] - }) + // If no transaction trace contains `eosmechanics:cpu` action, return default value + Ok(Default::default()) } -// #[substreams::handlers::map] -// pub fn prom_out(block_results: BlockResults) -> Result { -// -// ToDo push generic prometheus changes here -// this requires that we define a prometheus changes protobuf first. I (Fred) will help you on that task -// -// } +#[substreams::handlers::map] +pub fn prom_out(producer_usage: ProducerUsage) -> Result { + log::debug!("producer_usage={:?}", producer_usage); + + // TO-DO push generic prometheus changes here + // this requires that we define a prometheus changes protobuf first. I (Fred) will help you on that task + return Ok(PrometheusMetrics::default()); +} diff --git a/eosmechanics/src/pb/eosmechanics.v1.rs b/eosmechanics/src/pb/eosmechanics.v1.rs index d90e27b..678c4f9 100644 --- a/eosmechanics/src/pb/eosmechanics.v1.rs +++ b/eosmechanics/src/pb/eosmechanics.v1.rs @@ -1,11 +1,6 @@ // @generated #[derive(Clone, PartialEq, ::prost::Message)] -pub struct BlockResults { - #[prost(message, repeated, tag="1")] - pub producer_stats: ::prost::alloc::vec::Vec, -} -#[derive(Clone, PartialEq, ::prost::Message)] -pub struct ProducerStats { +pub struct ProducerUsage { /// the name of the block producer #[prost(string, tag="1")] pub producer: ::prost::alloc::string::String, @@ -15,40 +10,28 @@ pub struct ProducerStats { } /// Encoded file descriptor set for the `eosmechanics.v1` package pub const FILE_DESCRIPTOR_SET: &[u8] = &[ - 0x0a, 0xa0, 0x04, 0x0a, 0x12, 0x65, 0x6f, 0x73, 0x6d, 0x65, 0x63, 0x68, 0x61, 0x6e, 0x69, 0x63, + 0x0a, 0xec, 0x02, 0x0a, 0x12, 0x65, 0x6f, 0x73, 0x6d, 0x65, 0x63, 0x68, 0x61, 0x6e, 0x69, 0x63, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0f, 0x65, 0x6f, 0x73, 0x6d, 0x65, 0x63, 0x68, - 0x61, 0x6e, 0x69, 0x63, 0x73, 0x2e, 0x76, 0x31, 0x22, 0x55, 0x0a, 0x0c, 0x42, 0x6c, 0x6f, 0x63, - 0x6b, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x12, 0x45, 0x0a, 0x0e, 0x70, 0x72, 0x6f, 0x64, - 0x75, 0x63, 0x65, 0x72, 0x5f, 0x73, 0x74, 0x61, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, - 0x32, 0x1e, 0x2e, 0x65, 0x6f, 0x73, 0x6d, 0x65, 0x63, 0x68, 0x61, 0x6e, 0x69, 0x63, 0x73, 0x2e, - 0x76, 0x31, 0x2e, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x73, - 0x52, 0x0d, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x73, 0x22, - 0x48, 0x0a, 0x0d, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x73, - 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x08, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x12, 0x1b, 0x0a, 0x09, - 0x63, 0x70, 0x75, 0x5f, 0x75, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, - 0x08, 0x63, 0x70, 0x75, 0x55, 0x73, 0x61, 0x67, 0x65, 0x4a, 0xcf, 0x02, 0x0a, 0x06, 0x12, 0x04, - 0x00, 0x00, 0x0b, 0x01, 0x0a, 0x08, 0x0a, 0x01, 0x0c, 0x12, 0x03, 0x00, 0x00, 0x12, 0x0a, 0x08, - 0x0a, 0x01, 0x02, 0x12, 0x03, 0x02, 0x00, 0x18, 0x0a, 0x0a, 0x0a, 0x02, 0x04, 0x00, 0x12, 0x04, - 0x04, 0x00, 0x06, 0x01, 0x0a, 0x0a, 0x0a, 0x03, 0x04, 0x00, 0x01, 0x12, 0x03, 0x04, 0x08, 0x14, - 0x0a, 0x0b, 0x0a, 0x04, 0x04, 0x00, 0x02, 0x00, 0x12, 0x03, 0x05, 0x04, 0x2e, 0x0a, 0x0c, 0x0a, - 0x05, 0x04, 0x00, 0x02, 0x00, 0x04, 0x12, 0x03, 0x05, 0x04, 0x0c, 0x0a, 0x0c, 0x0a, 0x05, 0x04, - 0x00, 0x02, 0x00, 0x06, 0x12, 0x03, 0x05, 0x0d, 0x1a, 0x0a, 0x0c, 0x0a, 0x05, 0x04, 0x00, 0x02, - 0x00, 0x01, 0x12, 0x03, 0x05, 0x1b, 0x29, 0x0a, 0x0c, 0x0a, 0x05, 0x04, 0x00, 0x02, 0x00, 0x03, - 0x12, 0x03, 0x05, 0x2c, 0x2d, 0x0a, 0x0a, 0x0a, 0x02, 0x04, 0x01, 0x12, 0x04, 0x08, 0x00, 0x0b, - 0x01, 0x0a, 0x0a, 0x0a, 0x03, 0x04, 0x01, 0x01, 0x12, 0x03, 0x08, 0x08, 0x15, 0x0a, 0x2d, 0x0a, - 0x04, 0x04, 0x01, 0x02, 0x00, 0x12, 0x03, 0x09, 0x04, 0x18, 0x22, 0x20, 0x20, 0x74, 0x68, 0x65, - 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6c, 0x6f, - 0x63, 0x6b, 0x20, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x0a, 0x0a, 0x0c, 0x0a, 0x05, - 0x04, 0x01, 0x02, 0x00, 0x05, 0x12, 0x03, 0x09, 0x04, 0x0a, 0x0a, 0x0c, 0x0a, 0x05, 0x04, 0x01, - 0x02, 0x00, 0x01, 0x12, 0x03, 0x09, 0x0b, 0x13, 0x0a, 0x0c, 0x0a, 0x05, 0x04, 0x01, 0x02, 0x00, - 0x03, 0x12, 0x03, 0x09, 0x16, 0x17, 0x0a, 0x39, 0x0a, 0x04, 0x04, 0x01, 0x02, 0x01, 0x12, 0x03, - 0x0a, 0x04, 0x18, 0x22, 0x2c, 0x20, 0x63, 0x70, 0x75, 0x20, 0x75, 0x73, 0x61, 0x67, 0x65, 0x20, - 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x65, 0x6f, 0x73, 0x6d, 0x65, 0x63, 0x68, 0x61, 0x6e, - 0x69, 0x63, 0x73, 0x20, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, - 0x0a, 0x0a, 0x0c, 0x0a, 0x05, 0x04, 0x01, 0x02, 0x01, 0x05, 0x12, 0x03, 0x0a, 0x04, 0x09, 0x0a, - 0x0c, 0x0a, 0x05, 0x04, 0x01, 0x02, 0x01, 0x01, 0x12, 0x03, 0x0a, 0x0a, 0x13, 0x0a, 0x0c, 0x0a, - 0x05, 0x04, 0x01, 0x02, 0x01, 0x03, 0x12, 0x03, 0x0a, 0x16, 0x17, 0x62, 0x06, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x33, + 0x61, 0x6e, 0x69, 0x63, 0x73, 0x2e, 0x76, 0x31, 0x22, 0x48, 0x0a, 0x0d, 0x50, 0x72, 0x6f, 0x64, + 0x75, 0x63, 0x65, 0x72, 0x55, 0x73, 0x61, 0x67, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x72, 0x6f, + 0x64, 0x75, 0x63, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x72, 0x6f, + 0x64, 0x75, 0x63, 0x65, 0x72, 0x12, 0x1b, 0x0a, 0x09, 0x63, 0x70, 0x75, 0x5f, 0x75, 0x73, 0x61, + 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x63, 0x70, 0x75, 0x55, 0x73, 0x61, + 0x67, 0x65, 0x4a, 0xf2, 0x01, 0x0a, 0x06, 0x12, 0x04, 0x00, 0x00, 0x07, 0x01, 0x0a, 0x08, 0x0a, + 0x01, 0x0c, 0x12, 0x03, 0x00, 0x00, 0x12, 0x0a, 0x08, 0x0a, 0x01, 0x02, 0x12, 0x03, 0x02, 0x00, + 0x18, 0x0a, 0x0a, 0x0a, 0x02, 0x04, 0x00, 0x12, 0x04, 0x04, 0x00, 0x07, 0x01, 0x0a, 0x0a, 0x0a, + 0x03, 0x04, 0x00, 0x01, 0x12, 0x03, 0x04, 0x08, 0x15, 0x0a, 0x2d, 0x0a, 0x04, 0x04, 0x00, 0x02, + 0x00, 0x12, 0x03, 0x05, 0x04, 0x18, 0x22, 0x20, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6e, 0x61, 0x6d, + 0x65, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x20, 0x70, + 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x0a, 0x0a, 0x0c, 0x0a, 0x05, 0x04, 0x00, 0x02, 0x00, + 0x05, 0x12, 0x03, 0x05, 0x04, 0x0a, 0x0a, 0x0c, 0x0a, 0x05, 0x04, 0x00, 0x02, 0x00, 0x01, 0x12, + 0x03, 0x05, 0x0b, 0x13, 0x0a, 0x0c, 0x0a, 0x05, 0x04, 0x00, 0x02, 0x00, 0x03, 0x12, 0x03, 0x05, + 0x16, 0x17, 0x0a, 0x39, 0x0a, 0x04, 0x04, 0x00, 0x02, 0x01, 0x12, 0x03, 0x06, 0x04, 0x18, 0x22, + 0x2c, 0x20, 0x63, 0x70, 0x75, 0x20, 0x75, 0x73, 0x61, 0x67, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x74, + 0x68, 0x65, 0x20, 0x65, 0x6f, 0x73, 0x6d, 0x65, 0x63, 0x68, 0x61, 0x6e, 0x69, 0x63, 0x73, 0x20, + 0x74, 0x72, 0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x0a, 0x0a, 0x0c, 0x0a, + 0x05, 0x04, 0x00, 0x02, 0x01, 0x05, 0x12, 0x03, 0x06, 0x04, 0x09, 0x0a, 0x0c, 0x0a, 0x05, 0x04, + 0x00, 0x02, 0x01, 0x01, 0x12, 0x03, 0x06, 0x0a, 0x13, 0x0a, 0x0c, 0x0a, 0x05, 0x04, 0x00, 0x02, + 0x01, 0x03, 0x12, 0x03, 0x06, 0x16, 0x17, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, ]; // @@protoc_insertion_point(module) \ No newline at end of file diff --git a/eosmechanics/src/pb/pinax.sinks.prom.v1.rs b/eosmechanics/src/pb/pinax.sinks.prom.v1.rs new file mode 100644 index 0000000..5c5af4e --- /dev/null +++ b/eosmechanics/src/pb/pinax.sinks.prom.v1.rs @@ -0,0 +1,24 @@ +// @generated +/// TO-DO +/// Reference +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct PrometheusMetrics { +} +/// Encoded file descriptor set for the `pinax.sinks.prom.v1` package +pub const FILE_DESCRIPTOR_SET: &[u8] = &[ + 0x0a, 0xd6, 0x01, 0x0a, 0x0f, 0x73, 0x69, 0x6e, 0x6b, 0x2e, 0x70, 0x72, 0x6f, 0x6d, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x13, 0x70, 0x69, 0x6e, 0x61, 0x78, 0x2e, 0x73, 0x69, 0x6e, 0x6b, + 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x6d, 0x2e, 0x76, 0x31, 0x22, 0x13, 0x0a, 0x11, 0x50, 0x72, 0x6f, + 0x6d, 0x65, 0x74, 0x68, 0x65, 0x75, 0x73, 0x4d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x4a, 0x90, + 0x01, 0x0a, 0x06, 0x12, 0x04, 0x02, 0x00, 0x09, 0x01, 0x0a, 0x08, 0x0a, 0x01, 0x0c, 0x12, 0x03, + 0x02, 0x00, 0x12, 0x0a, 0x08, 0x0a, 0x01, 0x02, 0x12, 0x03, 0x04, 0x00, 0x1c, 0x0a, 0x66, 0x0a, + 0x02, 0x04, 0x00, 0x12, 0x04, 0x06, 0x00, 0x09, 0x01, 0x22, 0x5a, 0x20, 0x54, 0x4f, 0x2d, 0x44, + 0x4f, 0x0a, 0x20, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65, 0x20, 0x68, 0x74, 0x74, + 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x70, 0x6b, 0x67, 0x2e, 0x67, 0x6f, 0x2e, 0x64, 0x65, 0x76, 0x2f, + 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x72, 0x6f, 0x6d, 0x65, + 0x74, 0x68, 0x65, 0x75, 0x73, 0x2f, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x67, 0x6f, 0x6c, + 0x61, 0x6e, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x6d, 0x65, 0x74, 0x68, 0x65, 0x75, 0x73, 0x23, 0x47, + 0x61, 0x75, 0x67, 0x65, 0x0a, 0x0a, 0x0a, 0x0a, 0x03, 0x04, 0x00, 0x01, 0x12, 0x03, 0x06, 0x08, + 0x19, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +]; +// @@protoc_insertion_point(module) \ No newline at end of file diff --git a/eosmechanics/substreams.yaml b/eosmechanics/substreams.yaml index 8dd13a3..4e51344 100644 --- a/eosmechanics/substreams.yaml +++ b/eosmechanics/substreams.yaml @@ -13,13 +13,23 @@ binaries: protobuf: files: - eosmechanics.proto + - sink.prom.proto importPaths: - proto/v1 modules: - - name: map_block_stats + - name: map_producer_usage kind: map inputs: - source: sf.antelope.type.v2.Block output: - type: proto:eosmechanics.v1.BlockStats + type: proto:eosmechanics.v1.ProducerUsage + + - name: map_prom_out + kind: map + inputs: + - map: map_producer_usage + output: + type: proto:pinax.substreams.sink.prom.v1.PrometheusMetrics + +