Skip to content
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

feat: distribution messages #34

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "seda-common"
version = "0.2.0"
version = "0.4.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to start matching versions to the devnet version?
If so, this could be something like 0.5.0-dev1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I don't think matching versions makes sense. Since they could be in very different development cycles, but that's my opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna merge for now, and if we decide to do this I can do it in a future PR

edition = "2021"

[features]
Expand Down
69 changes: 62 additions & 7 deletions crates/common/src/msgs/data_requests/sudo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,79 @@
use crate::types::{Bytes, U128};

pub mod expire_data_requests;
pub mod remove_requests;

#[cfg_attr(feature = "cosmwasm", cosmwasm_schema::cw_serde)]
#[cfg_attr(not(feature = "cosmwasm"), derive(serde::Serialize, Debug, PartialEq))]
#[cfg_attr(not(feature = "cosmwasm"), serde(rename_all = "snake_case"))]
pub struct RemoveDataRequest {
pub dr_id: String,
pub struct DistributionExecutorReward {
/// The amount to burn
pub amount: U128,
/// The identity to reward.
pub identity: String,
}

impl From<RemoveDataRequest> for crate::msgs::SudoMsg {
fn from(value: RemoveDataRequest) -> Self {
SudoMsg::RemoveDataRequest(value).into()
}
#[cfg_attr(feature = "cosmwasm", cosmwasm_schema::cw_serde)]
#[cfg_attr(not(feature = "cosmwasm"), derive(serde::Serialize, Debug, PartialEq))]
#[cfg_attr(not(feature = "cosmwasm"), serde(rename_all = "snake_case"))]
pub struct DistributionBurn {
/// The amount to burn
pub amount: U128,
}

#[cfg_attr(feature = "cosmwasm", cosmwasm_schema::cw_serde)]
#[cfg_attr(not(feature = "cosmwasm"), derive(serde::Serialize, Debug, PartialEq))]
#[cfg_attr(not(feature = "cosmwasm"), serde(rename_all = "snake_case"))]
pub struct DistributionSend {
/// The address to send the funds to.
pub to: Bytes,
/// The amount to send to the address.
pub amount: U128,
}

#[cfg_attr(feature = "cosmwasm", cosmwasm_schema::cw_serde)]
#[cfg_attr(not(feature = "cosmwasm"), derive(serde::Serialize, Debug, PartialEq))]
#[cfg_attr(not(feature = "cosmwasm"), serde(rename_all = "snake_case"))]
pub enum DistributionKind {
/// For burning funds
Burn(DistributionBurn),
/// For rewarding an executor
ExecutorReward(DistributionExecutorReward),
/// For sending the funds to someone
Send(DistributionSend),
}

#[cfg_attr(feature = "cosmwasm", cosmwasm_schema::cw_serde)]
#[cfg_attr(not(feature = "cosmwasm"), derive(serde::Serialize, Debug, PartialEq))]
#[cfg_attr(not(feature = "cosmwasm"), serde(rename_all = "snake_case"))]
pub enum DistributionType {
TallyReward,
ExecutorReward,
TimedOut,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't decided what happens with the funds when a timeout happens, nice one!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this repo, but we discussed that we could have a tally parameter for a fixed gas amount for timeout DRs. That would mean that timeout DRs will have a small cost and the rest can be refunded to the requestor.

NoConsensus,
RemainderRefund,
}
#[cfg_attr(feature = "cosmwasm", cosmwasm_schema::cw_serde)]
#[cfg_attr(not(feature = "cosmwasm"), derive(serde::Serialize, Debug, PartialEq))]
#[cfg_attr(not(feature = "cosmwasm"), serde(rename_all = "snake_case"))]
pub struct DistributionMessage {
pub kind: DistributionKind,
#[serde(rename = "type")]
pub type_: DistributionType,
}

#[cfg_attr(feature = "cosmwasm", cosmwasm_schema::cw_serde)]
#[cfg_attr(not(feature = "cosmwasm"), derive(serde::Serialize, Debug, PartialEq))]
#[cfg_attr(not(feature = "cosmwasm"), serde(rename_all = "snake_case"))]
pub struct DistributionMessages {
pub messages: Vec<DistributionMessage>,
pub refund_type: DistributionType,
}

#[cfg_attr(feature = "cosmwasm", cosmwasm_schema::cw_serde)]
#[cfg_attr(not(feature = "cosmwasm"), derive(serde::Serialize, Debug, PartialEq))]
#[cfg_attr(not(feature = "cosmwasm"), serde(rename_all = "snake_case"))]
pub enum SudoMsg {
RemoveDataRequest(RemoveDataRequest),
RemoveDataRequests(remove_requests::Sudo),
ExpireDataRequests(expire_data_requests::Sudo),
}
Expand Down
4 changes: 3 additions & 1 deletion crates/common/src/msgs/data_requests/sudo/remove_requests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::collections::HashMap;

use super::*;

#[cfg_attr(feature = "cosmwasm", cosmwasm_schema::cw_serde)]
#[cfg_attr(not(feature = "cosmwasm"), derive(serde::Serialize, Debug, PartialEq))]
#[cfg_attr(not(feature = "cosmwasm"), serde(rename_all = "snake_case"))]
pub struct Sudo {
pub requests: Vec<RemoveDataRequest>,
pub requests: HashMap<String, DistributionMessages>,
}

impl From<Sudo> for crate::msgs::SudoMsg {
Expand Down
79 changes: 71 additions & 8 deletions crates/common/src/msgs/data_requests/sudo_tests.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,86 @@
use std::collections::HashMap;

use serde_json::json;

use super::sudo::*;
use crate::msgs;
#[cfg(feature = "cosmwasm")]
use crate::msgs::assert_json_deser;
#[cfg(not(feature = "cosmwasm"))]
use crate::msgs::assert_json_ser;
use crate::{msgs, types::Bytes};

#[test]
fn json_remove_request() {
fn json_remove_requests() {
#[cfg(not(feature = "cosmwasm"))]
let to: Bytes = "to".to_string();
#[cfg(feature = "cosmwasm")]
let to: Bytes = "to".as_bytes().into();

let expected_json = json!({
"remove_data_request": {
"dr_id": "dr_id",
"remove_data_requests": {
"requests": {
"dr_id1": {
"messages": [
{
"kind": {
"burn": {
"amount": "100"
}
},
"type": "executor_reward"
},
{
"kind": {
"executor_reward": {
"amount": "100",
"identity": "identity"
}
},
"type": "executor_reward"
},
{
"kind": {
"send": {
"amount": "100",
"to": to
}
},
"type": "executor_reward"
}
],
"refund_type": "remainder_refund"
},
}
}
});
let msg: msgs::SudoMsg = RemoveDataRequest {
dr_id: "dr_id".to_string(),
}
.into();
let mut requests = HashMap::new();
requests.insert(
"dr_id1".to_string(),
DistributionMessages {
messages: vec![
DistributionMessage {
kind: DistributionKind::Burn(DistributionBurn { amount: 100u128.into() }),
type_: DistributionType::ExecutorReward,
},
DistributionMessage {
kind: DistributionKind::ExecutorReward(DistributionExecutorReward {
amount: 100u128.into(),
identity: "identity".to_string(),
}),
type_: DistributionType::ExecutorReward,
},
DistributionMessage {
kind: DistributionKind::Send(DistributionSend {
amount: 100u128.into(),
to,
}),
type_: DistributionType::ExecutorReward,
},
],
refund_type: DistributionType::RemainderRefund,
},
);
let msg: msgs::SudoMsg = remove_requests::Sudo { requests }.into();
#[cfg(not(feature = "cosmwasm"))]
assert_json_ser(msg, expected_json);
#[cfg(feature = "cosmwasm")]
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/types/hash_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl HashSelf for Vec<u8> {
}
}

impl<'a, T> HashSelf for &'a [T]
impl<T> HashSelf for &[T]
where
T: HashSelf,
{
Expand Down
Loading