Skip to content

Commit

Permalink
add cuprate_types::json
Browse files Browse the repository at this point in the history
  • Loading branch information
hinto-janai committed Sep 25, 2024
1 parent 88605b0 commit 243d367
Show file tree
Hide file tree
Showing 9 changed files with 545 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ This allows all workspace crates to share, and aids compile times.
If a 3rd party's crate/functions/types are small enough, it could be moved here to trim dependencies and allow easy modifications.

## Features
Code can be selectively used/compiled with cargo's `--feature` or `features = ["..."]`.
Modules can be selectively used/compiled with cargo's `--feature` or `features = ["..."]`.

All features on by default.
All features are off by default.

See [`Cargo.toml`](Cargo.toml)'s `[features]` table to see what features there are and what they enable.

Special non-module related features:
- `serde`: Enables serde implementations on applicable types
- `std`: Enables usage of `std`

## `#[no_std]`
Each modules documents whether it requires `std` or not.

Expand Down
6 changes: 5 additions & 1 deletion types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/types"
keywords = ["cuprate", "types"]

[features]
default = ["blockchain", "epee", "serde"]
default = ["blockchain", "epee", "serde", "json"]
blockchain = []
epee = ["dep:cuprate-epee-encoding"]
serde = ["dep:serde"]
proptest = ["dep:proptest", "dep:proptest-derive"]
json = []

[dependencies]
cuprate-epee-encoding = { path = "../net/epee-encoding", optional = true }
cuprate-fixed-bytes = { path = "../net/fixed-bytes" }

cfg-if = { workspace = true }
bytes = { workspace = true }
curve25519-dalek = { workspace = true }
monero-serai = { workspace = true }
Expand All @@ -29,6 +31,8 @@ proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }

[dev-dependencies]
pretty_assertions = { workspace = true }
serde_json = { workspace = true, features = ["std"] }

[lints]
workspace = true
321 changes: 321 additions & 0 deletions types/src/json/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
cfg_if::cfg_if! {
if #[cfg(feature = "serde")] {
use serde::{Serialize, Deserialize};
// use monero_serai::{block::Block, transaction::Transaction};
}
}

/// TODO
///
/// `/get_block`
///
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Block {
pub major_version: u64,
pub minor_version: u64,
pub timestamp: u64,
pub prev_id: String,
pub nonce: u32,
pub miner_tx: MinerTransaction,
pub tx_hashes: Vec<String>,
}

/// TODO
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MinerTransaction {
pub version: u64,
pub unlock_time: u64,
pub vin: Vec<Input>,
pub vout: Vec<Output>,
pub extra: Vec<u8>,

/// Should be [`None`] if [`Self::rct_signatures`] is [`Some`]
#[serde(skip_serializing_if = "Option::is_none")]
pub signatures: Option<MinerTransactionSignature>,

/// Should be [`None`] if [`Self::signatures`] is [`Some`]
#[serde(skip_serializing_if = "Option::is_none")]
pub rct_signatures: Option<MinerTransactionRctSignature>,
}

/// TODO
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[serde(transparent)]
#[repr(transparent)]
pub struct MinerTransactionSignature(Vec<()>);

impl MinerTransactionSignature {
pub const fn new() -> Self {
Self(Vec::new())
}
}

/// TODO
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MinerTransactionRctSignature {
pub r#type: u8,
}

/// TODO
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Input {
pub r#gen: Gen,
}

/// TODO
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Gen {
pub height: u64,
}

/// TODO
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Output {
pub amount: u64,
pub target: Target,
}

/// TODO
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Target {
/// Should be [`None`] if [`Self::tagged_key`] is [`Some`]
#[serde(skip_serializing_if = "Option::is_none")]
pub key: Option<String>,

/// Should be [`None`] if [`Self::key`] is [`Some`]
#[serde(skip_serializing_if = "Option::is_none")]
pub tagged_key: Option<TaggedKey>,
}

/// TODO
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TaggedKey {
pub key: String,
pub view_tag: String,
}

#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;

use super::*;

const BLOCK_300000_JSON: &str = r#"{
"major_version": 1,
"minor_version": 0,
"timestamp": 1415690591,
"prev_id": "e97a0ab6307de9b9f9a9872263ef3e957976fb227eb9422c6854e989e5d5d34c",
"nonce": 2147484616,
"miner_tx": {
"version": 1,
"unlock_time": 300060,
"vin": [
{
"gen": {
"height": 300000
}
}
],
"vout": [
{
"amount": 47019296802,
"target": {
"key": "3c1dcbf5b485987ecef4596bb700e32cbc7bd05964e3888ffc05f8a46bf5fc33"
}
},
{
"amount": 200000000000,
"target": {
"key": "5810afc7a1b01a1c913eb6aab15d4a851cbc4a8cf0adf90bb80ac1a7ca9928aa"
}
},
{
"amount": 3000000000000,
"target": {
"key": "520f49c5f2ce8456dc1a565f35ed3a5ccfff3a1210b340870a57d2749a81a2df"
}
},
{
"amount": 10000000000000,
"target": {
"key": "44d7705e62c76c2e349a474df6724aa1d9932092002b03a94f9c19d9d12b9427"
}
}
],
"extra": [
1,
251,
8,
189,
254,
12,
213,
173,
108,
61,
156,
198,
144,
151,
31,
130,
141,
211,
120,
55,
81,
98,
32,
247,
111,
127,
254,
170,
170,
240,
124,
190,
223,
2,
8,
0,
0,
0,
64,
184,
115,
46,
246
],
"signatures": []
},
"tx_hashes": []
}"#;

const BLOCK_3245409_JSON: &str = r#"{
"major_version": 16,
"minor_version": 16,
"timestamp": 1727293028,
"prev_id": "41b56c273d69def3294e56179de71c61808042d54c1e085078d21dbe99e81b6f",
"nonce": 311,
"miner_tx": {
"version": 2,
"unlock_time": 3245469,
"vin": [
{
"gen": {
"height": 3245409
}
}
],
"vout": [
{
"amount": 601012280000,
"target": {
"tagged_key": {
"key": "8c0b16c6df02b9944b49f375d96a958a0fc5431c048879bb5bf25f64a1163b9e",
"view_tag": "88"
}
}
}
],
"extra": [
1,
39,
23,
182,
203,
58,
48,
15,
217,
9,
13,
147,
104,
133,
206,
176,
185,
56,
237,
179,
136,
72,
84,
129,
113,
98,
206,
4,
18,
50,
130,
162,
94,
2,
17,
73,
18,
21,
33,
32,
112,
5,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"rct_signatures": {
"type": 0
}
},
"tx_hashes": [
"eab76986a0cbcae690d8499f0f616f783fd2c89c6f611417f18011950dbdab2e",
"57b19aa8c2cdbb6836cf13dd1e321a67860965c12e4418f3c30f58c8899a851e",
"5340185432ab6b74fb21379f7e8d8f0e37f0882b2a7121fd7c08736f079e2edc",
"01dc6d31db56d68116f5294c1b4f80b33b048b5cdfefcd904f23e6c0de3daff5",
"c9fb6a2730678203948fef2a49fa155b63f35a3649f3d32ed405a6806f3bbd56",
"af965cdd2a2315baf1d4a3d242f44fe07b1fd606d5f4853c9ff546ca6c12a5af",
"97bc9e047d25fae8c14ce6ec882224e7b722f5e79b62a2602a6bacebdac8547b",
"28c46992eaf10dc0cceb313c30572d023432b7bd26e85e679bc8fe419533a7bf",
"c32e3acde2ff2885c9cc87253b40d6827d167dfcc3022c72f27084fd98788062",
"19e66a47f075c7cccde8a7b52803119e089e33e3a4847cace0bd1d17b0d22bab",
"8e8ac560e77a1ee72e82a5eb6887adbe5979a10cd29cb2c2a3720ce87db43a70",
"b7ff5141524b5cca24de6780a5dbfdf71e7de1e062fd85f557fb3b43b8e285dc",
"f09df0f113763ef9b9a2752ac293b478102f7cab03ef803a3d9db7585aea8912"
]
}"#;

fn test(block_json: &'static str) {
let json = serde_json::from_str::<Block>(block_json).unwrap();
let string = serde_json::to_string_pretty(&json).unwrap();
assert_eq!(block_json, &string);
}

#[test]
fn block_300000() {
test(BLOCK_300000_JSON);
}

#[test]
fn block_3245409() {
test(BLOCK_3245409_JSON);
}
}
6 changes: 6 additions & 0 deletions types/src/json/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod block;
mod tx;
pub mod tx_in_pool;

pub use block::Block;
pub use tx::Transaction;
Loading

0 comments on commit 243d367

Please sign in to comment.