Skip to content

Commit b596abc

Browse files
committed
feat: initial utxo definition and code generation via cddl-codegen
This is very incomplete, the UTxO CDDL definition is not complete and there are some issues we've encountered with cddl-codegen for generating types around generics, for example. We've discussed a few potential solutions to these problems in Slack. Though it's not complete, we're getting everything merged in a state where at least it's clear what needs to be worked on when we get back to it, due to priority shifts at IO. What is left todo in this specific commit: - Complete the UTxO definition - Once the CDDL is complete that defines the output from the Haskell node, write a simple tool that converts from that representation to the canonical representation. - Provide some initial test data to use (just testing SPO module to start)
1 parent 7039a19 commit b596abc

20 files changed

+5322
-215
lines changed

ledger-state/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
# Ignore the codegen-cddl output for now
2-
codegen-cddl/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "canonical-ledger-state"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[lib]
7+
crate-type = ["cdylib", "rlib"]
8+
9+
[dependencies]
10+
cbor_event = "2.4.0"
11+
hex = "0.4.3"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This file was code-generated using an experimental CDDL to rust tool:
2+
// https://github.com/dcSpark/cddl-codegen
3+
4+
use crate::serialization::{LenEncoding, StringEncoding};
5+
use std::collections::BTreeMap;
6+
7+
#[derive(Clone, Debug, Default)]
8+
pub struct AssetQuantityU64Encoding {
9+
pub len_encoding: LenEncoding,
10+
pub orig_deser_order: Vec<usize>,
11+
pub asset_id_encoding: StringEncoding,
12+
pub asset_id_key_encoding: StringEncoding,
13+
pub quantity_encoding: Option<cbor_event::Sz>,
14+
pub quantity_key_encoding: StringEncoding,
15+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file was code-generated using an experimental CDDL to rust tool:
2+
// https://github.com/dcSpark/cddl-codegen
3+
4+
use crate::serialization::{LenEncoding, StringEncoding};
5+
use std::collections::BTreeMap;
6+
7+
#[derive(Clone, Debug, Default)]
8+
pub struct DenominatorEncoding {
9+
pub inner_encoding: Option<cbor_event::Sz>,
10+
}
11+
12+
#[derive(Clone, Debug, Default)]
13+
pub struct GovActionIdEncoding {
14+
pub len_encoding: LenEncoding,
15+
pub uint_encoding: Option<cbor_event::Sz>,
16+
}
17+
18+
#[derive(Clone, Debug, Default)]
19+
pub struct Hash28Encoding {
20+
pub inner_encoding: StringEncoding,
21+
}
22+
23+
#[derive(Clone, Debug, Default)]
24+
pub struct Hash32Encoding {
25+
pub inner_encoding: StringEncoding,
26+
}
27+
28+
#[derive(Clone, Debug, Default)]
29+
pub struct UnitIntervalEncoding {
30+
pub len_encoding: LenEncoding,
31+
pub tag_encoding: Option<cbor_event::Sz>,
32+
pub index_0_encoding: Option<cbor_event::Sz>,
33+
pub index_1_encoding: Option<cbor_event::Sz>,
34+
}
35+
36+
#[derive(Clone, Debug, Default)]
37+
pub struct UrlEncoding {
38+
pub inner_encoding: StringEncoding,
39+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// This file was code-generated using an experimental CDDL to rust tool:
2+
// https://github.com/dcSpark/cddl-codegen
3+
4+
pub mod serialization;
5+
6+
use crate::error::*;
7+
use std::collections::BTreeMap;
8+
use std::convert::TryFrom;
9+
10+
pub type Address = Vec<u8>;
11+
12+
pub type Coin = u64;
13+
14+
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
15+
pub enum Credential {
16+
I0,
17+
I1,
18+
}
19+
20+
impl Credential {
21+
pub fn new_i0() -> Self {
22+
Self::I0
23+
}
24+
25+
pub fn new_i1() -> Self {
26+
Self::I1
27+
}
28+
}
29+
30+
#[derive(Clone, Debug, Copy)]
31+
pub struct Denominator(u64);
32+
33+
impl Denominator {
34+
pub fn new(inner: u64) -> Result<Self, DeserializeError> {
35+
if inner < 1 {
36+
return Err(DeserializeError::new(
37+
"Denominator",
38+
DeserializeFailure::RangeCheck {
39+
found: inner as isize,
40+
min: Some(1),
41+
max: None,
42+
},
43+
));
44+
}
45+
Ok(Self(inner))
46+
}
47+
}
48+
49+
impl TryFrom<u64> for Denominator {
50+
type Error = DeserializeError;
51+
52+
fn try_from(inner: u64) -> Result<Self, Self::Error> {
53+
Denominator::new(inner)
54+
}
55+
}
56+
57+
pub type Epoch = u64;
58+
59+
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
60+
pub struct GovActionId {
61+
pub hash_32: Hash32,
62+
pub uint: u16,
63+
}
64+
65+
impl GovActionId {
66+
pub fn new(hash_32: Hash32, uint: u16) -> Self {
67+
Self { hash_32, uint }
68+
}
69+
}
70+
71+
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
72+
pub struct Hash28(Vec<u8>);
73+
74+
impl Hash28 {
75+
pub fn new(inner: Vec<u8>) -> Result<Self, DeserializeError> {
76+
if inner.len() != 28 {
77+
return Err(DeserializeError::new(
78+
"Hash28",
79+
DeserializeFailure::RangeCheck {
80+
found: inner.len() as isize,
81+
min: Some(28),
82+
max: Some(28),
83+
},
84+
));
85+
}
86+
Ok(Self(inner))
87+
}
88+
}
89+
90+
impl TryFrom<Vec<u8>> for Hash28 {
91+
type Error = DeserializeError;
92+
93+
fn try_from(inner: Vec<u8>) -> Result<Self, Self::Error> {
94+
Hash28::new(inner)
95+
}
96+
}
97+
98+
impl From<Hash28> for Vec<u8> {
99+
fn from(wrapper: Hash28) -> Self {
100+
wrapper.0
101+
}
102+
}
103+
104+
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
105+
pub struct Hash32(Vec<u8>);
106+
107+
impl Hash32 {
108+
pub fn new(inner: Vec<u8>) -> Result<Self, DeserializeError> {
109+
if inner.len() != 32 {
110+
return Err(DeserializeError::new(
111+
"Hash32",
112+
DeserializeFailure::RangeCheck {
113+
found: inner.len() as isize,
114+
min: Some(32),
115+
max: Some(32),
116+
},
117+
));
118+
}
119+
Ok(Self(inner))
120+
}
121+
}
122+
123+
impl TryFrom<Vec<u8>> for Hash32 {
124+
type Error = DeserializeError;
125+
126+
fn try_from(inner: Vec<u8>) -> Result<Self, Self::Error> {
127+
Hash32::new(inner)
128+
}
129+
}
130+
131+
impl From<Hash32> for Vec<u8> {
132+
fn from(wrapper: Hash32) -> Self {
133+
wrapper.0
134+
}
135+
}
136+
137+
pub type Keyhash = Hash28;
138+
139+
pub type PositiveCoin = u64;
140+
141+
pub type RewardAccount = Vec<u8>;
142+
143+
impl From<Url> for String {
144+
fn from(wrapper: Url) -> Self {
145+
wrapper.0
146+
}
147+
}
148+
149+
#[derive(Clone, Debug)]
150+
pub struct UnitInterval {
151+
pub index_0: u64,
152+
pub index_1: u64,
153+
}
154+
155+
impl UnitInterval {
156+
pub fn new(index_0: u64, index_1: u64) -> Self {
157+
Self { index_0, index_1 }
158+
}
159+
}
160+
161+
#[derive(Clone, Debug)]
162+
pub struct Url(String);
163+
164+
impl Url {
165+
pub fn new(inner: String) -> Result<Self, DeserializeError> {
166+
if inner.len() > 128 {
167+
return Err(DeserializeError::new(
168+
"Url",
169+
DeserializeFailure::RangeCheck {
170+
found: inner.len() as isize,
171+
min: Some(0),
172+
max: Some(128),
173+
},
174+
));
175+
}
176+
Ok(Self(inner))
177+
}
178+
}
179+
180+
impl TryFrom<String> for Url {
181+
type Error = DeserializeError;
182+
183+
fn try_from(inner: String) -> Result<Self, Self::Error> {
184+
Url::new(inner)
185+
}
186+
}
187+
188+
impl From<Denominator> for u64 {
189+
fn from(wrapper: Denominator) -> Self {
190+
wrapper.0
191+
}
192+
}

0 commit comments

Comments
 (0)