From 442c739db1605463b3c6f53d6e68cac41452cd26 Mon Sep 17 00:00:00 2001 From: YaroShkvorets Date: Mon, 9 Sep 2024 22:07:08 -0400 Subject: [PATCH] add substreams timestamp type ref #5637 --- chain/substreams/proto/codec.proto | 4 ++-- chain/substreams/src/mapper.rs | 15 +++++++++++++-- .../src/protobuf/substreams.entity.v1.rs | 5 ++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/chain/substreams/proto/codec.proto b/chain/substreams/proto/codec.proto index 5529cd774af..bd75e7f95c8 100644 --- a/chain/substreams/proto/codec.proto +++ b/chain/substreams/proto/codec.proto @@ -28,8 +28,8 @@ message Value { string string = 4; bytes bytes = 5; bool bool = 6; - - //reserved 7 to 9; // For future types + int64 timestamp = 7; + //reserved 8 to 9; // For future types Array array = 10; } diff --git a/chain/substreams/src/mapper.rs b/chain/substreams/src/mapper.rs index 1d3c7ea23db..bd7a30053c1 100644 --- a/chain/substreams/src/mapper.rs +++ b/chain/substreams/src/mapper.rs @@ -8,7 +8,7 @@ use graph::blockchain::block_stream::{ SubstreamsError, }; use graph::blockchain::BlockTime; -use graph::data::store::scalar::Bytes; +use graph::data::store::scalar::{Bytes, Timestamp}; use graph::data::store::IdType; use graph::data::value::Word; use graph::data_source::CausalityRegion; @@ -264,6 +264,10 @@ fn decode_value(value: &crate::codec::value::Typed) -> anyhow::Result { Typed::Bool(new_value) => Ok(Value::Bool(*new_value)), + Typed::Timestamp(new_value) => Timestamp::from_microseconds_since_epoch(*new_value) + .map(Value::Timestamp) + .map_err(|err| anyhow::Error::from(err)), + Typed::Array(arr) => arr .value .iter() @@ -282,7 +286,7 @@ mod test { use crate::codec::{Array, Value}; use base64::prelude::*; use graph::{ - data::store::scalar::Bytes, + data::store::scalar::{Bytes, Timestamp}, prelude::{BigDecimal, BigInt, Value as GraphValue}, }; @@ -374,6 +378,13 @@ mod test { }, expected_value: GraphValue::Bool(true), }, + Case { + name: "timestamp value".to_string(), + value: Value { + typed: Some(Typed::Timestamp(1234565789)), + }, + expected_value: GraphValue::Timestamp(Timestamp::from_microseconds_since_epoch(1234565789).unwrap()), + }, Case { name: "string array".to_string(), value: Value { diff --git a/chain/substreams/src/protobuf/substreams.entity.v1.rs b/chain/substreams/src/protobuf/substreams.entity.v1.rs index 174a30baff8..372748908d7 100644 --- a/chain/substreams/src/protobuf/substreams.entity.v1.rs +++ b/chain/substreams/src/protobuf/substreams.entity.v1.rs @@ -68,7 +68,7 @@ pub mod entity_change { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct Value { - #[prost(oneof = "value::Typed", tags = "1, 2, 3, 4, 5, 6, 10")] + #[prost(oneof = "value::Typed", tags = "1, 2, 3, 4, 5, 6, 7, 10")] pub typed: ::core::option::Option, } /// Nested message and enum types in `Value`. @@ -88,6 +88,9 @@ pub mod value { Bytes(::prost::alloc::vec::Vec), #[prost(bool, tag = "6")] Bool(bool), + /// reserved 8 to 9; // For future types + #[prost(int64, tag = "7")] + Timestamp(i64), #[prost(message, tag = "10")] Array(super::Array), }