ee5b55fab6
* Feature/ephemera compile (#3437) * Include ephemera node code in repo * Upgrade deps * Bump minor version of cosmwasm-std * Include ephemera in nym-api dep and downgrade rusqlite * Fix clippy and ephemera docs code * More clippy on ephemera --------- Co-authored-by: Andrus Salumets <andrus@nymtech.net> * Start ephemera components in nym-api (#3475) * Start ephemera components in nym-api * Pass nyxd client and use common metric structures * Swap url endpoint with contract for sending rewarding messages * Fix build after rebase * Perform ephemera rewards computation before normal nym-api ones * Remove contract mock from ephemera * Take raw rewards from network monitor * Remove ephemera old reward version * Use nym shutdown procedure in ephemera * Temporary fix for some warnings * Umock contract membership of ephemera (#3574) * Pass nyxd client to members provider * Basic ephemera contract * Add register peer tx * Add query all peers * Nyxd ephemera client * Add registration of ephemera peer * Replace epoch http api with actual contract * Merge ephemera config into nym-api config * Load cluster from contract * Guard nym-outfox out of cosmwasm builds (#3650) * Feature/fixes while testing (#3668) * Commit local peer before querying contract * Default to anyonline * Remove string from template * Fix avg computing * Use updated qa env * Fix clippy * Add unit tests for ephemera contract * Upload ephemera contract in CI * Add group check for peer signup * Peer registration unit test * Start ephemera only on monitoring * Remove old MixnodeToReward struct * Move all ephemera config to its file * Skip with serde ephemera config * Fix default value in args * Feature/add ephemera flag (#3727) * Replace unwrap with error handling * Add ephemera enable flag * Fix template * Add json schema to ephemera contract (#3735) * Update lock files * Update changelog --------- Co-authored-by: Andrus Salumets <andrus@nymtech.net>
103 lines
2.6 KiB
Rust
103 lines
2.6 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use thiserror::Error;
|
|
|
|
pub(crate) mod varint_async;
|
|
pub(crate) mod varint_bytes;
|
|
|
|
pub(crate) type Codec = SerdeCodec;
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum DecodingError {
|
|
#[error("Decoding error: {0}")]
|
|
DecodingError(#[from] serde_json::Error),
|
|
}
|
|
|
|
#[allow(clippy::module_name_repetitions)]
|
|
#[derive(Debug, Error)]
|
|
pub enum EncodingError {
|
|
#[error("Encoding error: {0}")]
|
|
EncodingError(#[from] serde_json::Error),
|
|
}
|
|
|
|
/// Simple trait for encoding
|
|
pub(crate) trait EphemeraCodec {
|
|
/// Encodes a message into a vector of bytes
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `data` - data to encode
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// * `Result<Vec<u8>, EncodingError>` - encoded data or error
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// * `EncodingError` - if encoding fails
|
|
fn encode<M: Serialize>(data: &M) -> Result<Vec<u8>, EncodingError>;
|
|
|
|
/// Decodes a message from a vector of bytes
|
|
fn decode<M: for<'de> serde::Deserialize<'de>>(bytes: &[u8]) -> Result<M, DecodingError>;
|
|
}
|
|
|
|
pub(crate) struct SerdeCodec;
|
|
|
|
impl EphemeraCodec for SerdeCodec {
|
|
fn encode<M: Serialize>(data: &M) -> Result<Vec<u8>, EncodingError> {
|
|
let bytes = serde_json::to_vec(data)?;
|
|
Ok(bytes)
|
|
}
|
|
|
|
fn decode<M: for<'de> Deserialize<'de>>(bytes: &[u8]) -> Result<M, DecodingError> {
|
|
let decoded = serde_json::from_slice(bytes)?;
|
|
Ok(decoded)
|
|
}
|
|
}
|
|
|
|
/// Trait which types can implement to provide their own encoding
|
|
pub trait Encode {
|
|
/// Encodes itself into a vector of bytes
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// * `Result<Vec<u8>, EncodingError>` - encoded data or error
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// * `EncodingError` - if encoding fails
|
|
fn encode(&self) -> Result<Vec<u8>, EncodingError>;
|
|
}
|
|
|
|
/// Trait which types can implement to provide their own decoding
|
|
pub trait Decode {
|
|
type Output: for<'de> serde::Deserialize<'de>;
|
|
|
|
/// Decodes itself from a vector of bytes
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `bytes` - bytes to decode
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// * `Result<Self::Output, DecodingError>` - decoded data or error
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// * `DecodingError` - if decoding fails
|
|
fn decode(bytes: &[u8]) -> Result<Self::Output, DecodingError>;
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use crate::utilities::codec::EphemeraCodec;
|
|
|
|
#[test]
|
|
fn test_encode_decode() {
|
|
let data = vec![1, 2, 3, 4, 5];
|
|
let encoded = super::SerdeCodec::encode(&data).unwrap();
|
|
let decoded = super::SerdeCodec::decode::<Vec<u8>>(&encoded).unwrap();
|
|
assert_eq!(data, decoded);
|
|
}
|
|
}
|