Skip to content

Commit

Permalink
Improved support to remove GenericData dependency and use Vec<u8> i…
Browse files Browse the repository at this point in the history
…nstead
  • Loading branch information
FloppyDisck committed Oct 15, 2024
1 parent 04287ad commit 87d3810
Show file tree
Hide file tree
Showing 32 changed files with 397 additions and 672 deletions.
106 changes: 16 additions & 90 deletions packages/proto/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,7 @@ use prost::bytes::{Buf, BufMut};
use prost::{Message, Name};
use serde::{ser, Deserialize, Deserializer, Serialize, Serializer};

/// Works as a wrapper for Vec<u8> when working with structures that have an undefined/unknown type.
#[derive(:: serde :: Serialize, :: serde :: Deserialize, Clone, PartialEq, Debug, Default)]
pub struct GenericData(pub Vec<u8>);

impl Message for GenericData {
fn encode_raw<B>(&self, buf: &mut B)
where
B: BufMut,
{
buf.put_slice(self.0.as_slice());
}

fn merge_field<B>(
&mut self,
tag: u32,
_wire_type: prost::encoding::WireType,
buf: &mut B,
_ctx: prost::encoding::DecodeContext,
) -> Result<(), prost::DecodeError>
where
B: Buf,
{
if tag == 1 {
self.0.push(10u8);
while buf.has_remaining() {
self.0.push(buf.get_u8());
}
Ok(())
} else {
Err(prost::DecodeError::new("invalid tag"))
}
}

fn encoded_len(&self) -> usize {
self.0.len()
}

fn clear(&mut self) {
self.0.clear();
}
}

impl Name for GenericData {
const NAME: &'static str = "";
const PACKAGE: &'static str = "";

fn full_name() -> String {
format!("{}{}", Self::PACKAGE, Self::NAME)
}
}
pub type GenericData = Vec<u8>;

// An improved any type that allows you to implement typing directly into it
#[derive(Clone, PartialEq, Serialize, Deserialize, Message)]
Expand All @@ -73,7 +24,7 @@ impl<T: Message + Name + PartialEq + Default> Any<T> {
pub fn generic(value: T) -> Any<GenericData> {
Any {
type_url: T::full_name(),
value: GenericData(value.encode_to_vec()),
value: value.encode_to_vec(),
}
}
}
Expand Down Expand Up @@ -211,9 +162,9 @@ mod test {
use serde::{Deserialize, Serialize};
use serde_test::{assert_tokens, Token};

#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[derive(::serde::Serialize, ::serde::Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
#[derive(Clone, PartialEq, ::prost::Message)]
struct ExternalStructTest<
A: Default + Message + Name + Send + Sync + Serialize + DeserializeOwned + PartialEq + Clone,
> {
Expand All @@ -226,16 +177,16 @@ mod test {
}

impl<
A: Default
+ Message
+ Name
+ Send
+ Sync
+ Serialize
+ DeserializeOwned
+ PartialEq
+ Clone,
> Name for ExternalStructTest<A>
A: Default
+ Message
+ Name
+ Send
+ Sync
+ Serialize
+ DeserializeOwned
+ PartialEq
+ Clone,
> Name for ExternalStructTest<A>
{
const NAME: &'static str = "AnyValue";
const PACKAGE: &'static str = "test";
Expand All @@ -245,9 +196,9 @@ mod test {
}
}

#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[derive(::serde::Serialize, ::serde::Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
#[derive(Clone, PartialEq, ::prost::Message)]
struct BuildTest<
A: Default + Message + Name + Send + Sync + Serialize + DeserializeOwned + PartialEq + Clone,
C: Default + Message + Name + Send + Sync + Serialize + DeserializeOwned + PartialEq + Clone,
Expand Down Expand Up @@ -299,31 +250,6 @@ mod test {
}
}

#[test]
fn generic_equal_any() {
let val = AnyValue {
value: "testinnnng".to_string(),
number: 33,
};

dbg!(val.encode_to_vec());
dbg!(GenericData(val.encode_to_vec()).encode_to_vec());

let real_any = Any::new(val.clone());
let bytes_any = Any::generic(val);

let mut real_encoded = real_any.encode_to_vec();
let mut bytes_encoded = bytes_any.encode_to_vec();

assert_eq!(real_encoded, bytes_encoded);

let real_decoded = Any::<AnyValue>::decode(bytes_encoded.as_slice()).unwrap();
let bytes_decoded = Any::<GenericData>::decode(real_encoded.as_slice()).unwrap();

assert_eq!(real_decoded.type_url, bytes_decoded.type_url);
assert_eq!(real_decoded.value.encode_to_vec(), bytes_decoded.value.0);
}

#[test]
fn test_ser_de() {
let test = Test {
Expand Down
6 changes: 2 additions & 4 deletions packages/proto/src/gen/archway.cwica.v1.abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl ::prost::Name for MsgRegisterInterchainAccountResponse {
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct MsgSendTx<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> {
pub struct MsgSendTx<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[doc = " contract_address is the address of the who wants to submit a transaction to"]
#[doc = " the counterparty chain"]
#[prost(string, tag = "1")]
Expand All @@ -234,9 +234,7 @@ pub struct MsgSendTx<A: Clone + PartialEq + Default + Send + Sync + prost::Messa
#[prost(uint64, tag = "5")]
pub timeout: u64,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
for MsgSendTx<A>
{
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name for MsgSendTx<A> {
const NAME: &'static str = "MsgSendTx";
const PACKAGE: &'static str = "archway.cwica.v1";
fn full_name() -> ::prost::alloc::string::String {
Expand Down
5 changes: 2 additions & 3 deletions packages/proto/src/gen/archway.genmsg.v1.abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct GenesisState<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name>
{
pub struct GenesisState<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[prost(message, repeated, tag = "1")]
pub messages: ::prost::alloc::vec::Vec<crate::any::Any<A>>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for GenesisState<A>
{
const NAME: &'static str = "GenesisState";
Expand Down
17 changes: 6 additions & 11 deletions packages/proto/src/gen/cosmos.app.v1alpha1.abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl ::prost::Name for MigrateFromInfo {
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct Config<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> {
pub struct Config<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[doc = " modules are the module configurations for the app."]
#[prost(message, repeated, tag = "1")]
pub modules: ::prost::alloc::vec::Vec<ModuleConfig<A>>,
Expand All @@ -122,9 +122,7 @@ pub struct Config<A: Clone + PartialEq + Default + Send + Sync + prost::Message
#[prost(message, repeated, tag = "2")]
pub golang_bindings: ::prost::alloc::vec::Vec<GolangBinding>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
for Config<A>
{
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name for Config<A> {
const NAME: &'static str = "Config";
const PACKAGE: &'static str = "cosmos.app.v1alpha1";
fn full_name() -> ::prost::alloc::string::String {
Expand All @@ -135,8 +133,7 @@ impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct ModuleConfig<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name>
{
pub struct ModuleConfig<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[doc = " name is the unique name of the module within the app. It should be a name"]
#[doc = " that persists between different versions of a module so that modules"]
#[doc = " can be smoothly upgraded to new versions."]
Expand All @@ -159,7 +156,7 @@ pub struct ModuleConfig<A: Clone + PartialEq + Default + Send + Sync + prost::Me
#[prost(message, repeated, tag = "3")]
pub golang_bindings: ::prost::alloc::vec::Vec<GolangBinding>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for ModuleConfig<A>
{
const NAME: &'static str = "ModuleConfig";
Expand Down Expand Up @@ -203,14 +200,12 @@ impl ::prost::Name for QueryConfigRequest {
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct QueryConfigResponse<
A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name,
> {
pub struct QueryConfigResponse<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[doc = " config is the current app config."]
#[prost(message, optional, tag = "1")]
pub config: ::core::option::Option<Config<A>>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for QueryConfigResponse<A>
{
const NAME: &'static str = "QueryConfigResponse";
Expand Down
42 changes: 16 additions & 26 deletions packages/proto/src/gen/cosmos.auth.v1beta1.abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct BaseAccount<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name>
{
pub struct BaseAccount<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[prost(string, tag = "1")]
pub address: ::prost::alloc::string::String,
#[prost(message, optional, tag = "2")]
Expand All @@ -15,7 +14,7 @@ pub struct BaseAccount<A: Clone + PartialEq + Default + Send + Sync + prost::Mes
#[prost(uint64, tag = "4")]
pub sequence: u64,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for BaseAccount<A>
{
const NAME: &'static str = "BaseAccount";
Expand All @@ -28,17 +27,15 @@ impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct ModuleAccount<
A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name,
> {
pub struct ModuleAccount<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[prost(message, optional, tag = "1")]
pub base_account: ::core::option::Option<BaseAccount<A>>,
#[prost(string, tag = "2")]
pub name: ::prost::alloc::string::String,
#[prost(string, repeated, tag = "3")]
pub permissions: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for ModuleAccount<A>
{
const NAME: &'static str = "ModuleAccount";
Expand Down Expand Up @@ -96,16 +93,15 @@ impl ::prost::Name for Params {
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct GenesisState<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name>
{
pub struct GenesisState<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[doc = " params defines all the parameters of the module."]
#[prost(message, optional, tag = "1")]
pub params: ::core::option::Option<Params>,
#[doc = " accounts are the accounts present at genesis."]
#[prost(message, repeated, tag = "2")]
pub accounts: ::prost::alloc::vec::Vec<crate::any::Any<A>>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for GenesisState<A>
{
const NAME: &'static str = "GenesisState";
Expand Down Expand Up @@ -138,17 +134,15 @@ impl ::prost::Name for QueryAccountsRequest {
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct QueryAccountsResponse<
A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name,
> {
pub struct QueryAccountsResponse<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[doc = " accounts are the existing accounts"]
#[prost(message, repeated, tag = "1")]
pub accounts: ::prost::alloc::vec::Vec<crate::any::Any<A>>,
#[doc = " pagination defines the pagination in the response."]
#[prost(message, optional, tag = "2")]
pub pagination: ::core::option::Option<super::super::base::query::v1beta1::PageResponse>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for QueryAccountsResponse<A>
{
const NAME: &'static str = "QueryAccountsResponse";
Expand Down Expand Up @@ -177,14 +171,12 @@ impl ::prost::Name for QueryAccountRequest {
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct QueryAccountResponse<
A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name,
> {
pub struct QueryAccountResponse<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[doc = " account defines the account of the corresponding address."]
#[prost(message, optional, tag = "1")]
pub account: ::core::option::Option<crate::any::Any<A>>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for QueryAccountResponse<A>
{
const NAME: &'static str = "QueryAccountResponse";
Expand Down Expand Up @@ -242,12 +234,12 @@ impl ::prost::Name for QueryModuleAccountsRequest {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct QueryModuleAccountsResponse<
A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name,
A: Clone + PartialEq + Default + Send + Sync + prost::Message,
> {
#[prost(message, repeated, tag = "1")]
pub accounts: ::prost::alloc::vec::Vec<crate::any::Any<A>>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for QueryModuleAccountsResponse<A>
{
const NAME: &'static str = "QueryModuleAccountsResponse";
Expand Down Expand Up @@ -276,12 +268,12 @@ impl ::prost::Name for QueryModuleAccountByNameRequest {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct QueryModuleAccountByNameResponse<
A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name,
A: Clone + PartialEq + Default + Send + Sync + prost::Message,
> {
#[prost(message, optional, tag = "1")]
pub account: ::core::option::Option<crate::any::Any<A>>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for QueryModuleAccountByNameResponse<A>
{
const NAME: &'static str = "QueryModuleAccountByNameResponse";
Expand Down Expand Up @@ -458,14 +450,12 @@ impl ::prost::Name for QueryAccountInfoRequest {
#[derive(:: serde :: Serialize, :: serde :: Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, :: prost :: Message)]
pub struct QueryAccountInfoResponse<
A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name,
> {
pub struct QueryAccountInfoResponse<A: Clone + PartialEq + Default + Send + Sync + prost::Message> {
#[doc = " info is the account info which is represented by BaseAccount."]
#[prost(message, optional, tag = "1")]
pub info: ::core::option::Option<BaseAccount<A>>,
}
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message + prost::Name> ::prost::Name
impl<A: Clone + PartialEq + Default + Send + Sync + prost::Message> ::prost::Name
for QueryAccountInfoResponse<A>
{
const NAME: &'static str = "QueryAccountInfoResponse";
Expand Down
Loading

0 comments on commit 87d3810

Please sign in to comment.