feat: encode plutus data

This commit is contained in:
rvcas 2022-08-04 11:59:40 -04:00 committed by Lucas
parent d14920265e
commit 61b70e7e3d
4 changed files with 134 additions and 20 deletions

117
Cargo.lock generated
View File

@ -40,6 +40,18 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "base58"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6107fe1be6682a68940da878d9e9f5e90ca5745b3dec9fd1bb393c8777d4f581"
[[package]]
name = "bech32"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5738be7561b0eeb501ef1d5c5db3f24e01ceb55fededd9b00039aada34966ad"
[[package]] [[package]]
name = "bit-set" name = "bit-set"
version = "0.5.2" version = "0.5.2"
@ -112,6 +124,12 @@ dependencies = [
"os_str_bytes", "os_str_bytes",
] ]
[[package]]
name = "cryptoxide"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "129eabb7b0b78644a3a7e7cf220714aba47463bb281f69fa7a71ca5d12564cca"
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "1.7.0" version = "1.7.0"
@ -146,6 +164,12 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "half"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.11.2" version = "0.11.2"
@ -192,6 +216,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "itoa"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -213,13 +243,34 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "minicbor"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5e575910763b21a0db7df5e142907fe944bff84d1dfc78e2ba92e7f3bdfd36b"
dependencies = [
"half",
"minicbor-derive 0.11.0",
]
[[package]] [[package]]
name = "minicbor" name = "minicbor"
version = "0.18.0" version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a20020e8e2d1881d8736f64011bb5ff99f1db9947ce3089706945c8915695cb" checksum = "2a20020e8e2d1881d8736f64011bb5ff99f1db9947ce3089706945c8915695cb"
dependencies = [ dependencies = [
"minicbor-derive", "minicbor-derive 0.12.0",
]
[[package]]
name = "minicbor-derive"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a86c5f04def8fb7735ae918bb589af82f985526f4c62e0249544b668b2f456"
dependencies = [
"proc-macro2",
"quote",
"syn",
] ]
[[package]] [[package]]
@ -248,6 +299,44 @@ version = "6.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "029d8d0b2f198229de29dca79676f2738ff952edf3fde542eb8bf94d8c21b435" checksum = "029d8d0b2f198229de29dca79676f2738ff952edf3fde542eb8bf94d8c21b435"
[[package]]
name = "pallas-codec"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dce0ea17341c1a0e43e2bb4a637740198dcb09826879ce3ac5ae1c6f4398a5d"
dependencies = [
"minicbor 0.17.1",
]
[[package]]
name = "pallas-crypto"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "051226367cd851895c73e3115d378b58495ade1ee60c3a154c0d0c30554565fa"
dependencies = [
"cryptoxide",
"hex",
"pallas-codec",
"rand_core",
"thiserror",
]
[[package]]
name = "pallas-primitives"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97a0fcc7d5a7120bc2b2e203ec5e7f8088107c500c0eb665569d0e77a910d3c0"
dependencies = [
"base58",
"bech32",
"hex",
"log",
"pallas-codec",
"pallas-crypto",
"serde",
"serde_json",
]
[[package]] [[package]]
name = "peg" name = "peg"
version = "0.8.0" version = "0.8.0"
@ -442,6 +531,29 @@ dependencies = [
"wait-timeout", "wait-timeout",
] ]
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "serde"
version = "1.0.142"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e590c437916fb6b221e1d00df6e3294f3fccd70ca7e92541c475d6ed6ef5fee2"
[[package]]
name = "serde_json"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38dd04e3c8279e75b31ef29dbdceebfe5ad89f4d0937213c53f7d49d01b3d5a7"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"
@ -532,7 +644,8 @@ version = "0.0.6"
dependencies = [ dependencies = [
"flat-rs", "flat-rs",
"hex", "hex",
"minicbor", "minicbor 0.18.0",
"pallas-primitives",
"peg", "peg",
"pretty", "pretty",
"proptest", "proptest",

View File

@ -16,6 +16,7 @@ exclude = ["test_data/*"]
flat-rs = { path = "../flat", version = "0.0.6" } flat-rs = { path = "../flat", version = "0.0.6" }
hex = "0.4.3" hex = "0.4.3"
minicbor = { version = "0.18.0", features = ["std"] } minicbor = { version = "0.18.0", features = ["std"] }
pallas-primitives = "0.12.0"
peg = "0.8.0" peg = "0.8.0"
pretty = "0.11.3" pretty = "0.11.3"
thiserror = "1.0.31" thiserror = "1.0.31"

View File

@ -1,5 +1,7 @@
use std::fmt::Display; use std::fmt::Display;
use pallas_primitives::alonzo::PlutusData;
use crate::{ use crate::{
builtins::DefaultFunction, builtins::DefaultFunction,
debruijn::{self, Converter}, debruijn::{self, Converter},
@ -109,7 +111,7 @@ pub enum Constant {
// tag: 7 // tag: 7
// Apply(Box<Constant>, Type), // Apply(Box<Constant>, Type),
// tag: 8 // tag: 8
Data(Data), Data(PlutusData),
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -139,15 +141,6 @@ impl Display for Type {
} }
} }
#[derive(Debug, Clone, PartialEq)]
pub enum Data {
Constr(isize, Vec<Data>),
Map(Vec<(Data, Data)>),
List(Vec<Data>),
I(isize),
B(Vec<u8>),
}
/// A Name containing it's parsed textual representation /// A Name containing it's parsed textual representation
/// and a unique id from string interning. The Name's text is /// and a unique id from string interning. The Name's text is
/// interned during parsing. /// interned during parsing.

View File

@ -5,6 +5,7 @@ use flat_rs::{
en::{self, Encode, Encoder}, en::{self, Encode, Encoder},
Flat, Flat,
}; };
use pallas_primitives::Fragment;
use crate::{ use crate::{
ast::{ ast::{
@ -78,6 +79,7 @@ where
fn decode(d: &mut Decoder) -> Result<Self, de::Error> { fn decode(d: &mut Decoder) -> Result<Self, de::Error> {
let version = (usize::decode(d)?, usize::decode(d)?, usize::decode(d)?); let version = (usize::decode(d)?, usize::decode(d)?, usize::decode(d)?);
let term = Term::decode(d)?; let term = Term::decode(d)?;
Ok(Program { version, term }) Ok(Program { version, term })
} }
} }
@ -163,15 +165,15 @@ where
} }
} }
/// Integers are typically smaller so we save space
/// by encoding them in 7 bits and this allows it to be byte alignment agnostic.
/// Strings and bytestrings span multiple bytes so using bytestring is
/// the most effective encoding.
/// i.e. A 17 or greater length byte array loses efficiency being encoded as
/// a unsigned integer instead of a byte array
impl Encode for Constant { impl Encode for Constant {
fn encode(&self, e: &mut Encoder) -> Result<(), en::Error> { fn encode(&self, e: &mut Encoder) -> Result<(), en::Error> {
match self { match self {
// Integers are typically smaller so we save space
// by encoding them in 7 bits and this allows it to be byte alignment agnostic.
// Strings and bytestrings span multiple bytes so using bytestring is
// the most effective encoding.
// i.e. A 17 or greater length byte array loses efficiency being encoded as
// a unsigned integer instead of a byte array
Constant::Integer(i) => { Constant::Integer(i) => {
encode_constant(&[0], e)?; encode_constant(&[0], e)?;
i.encode(e)?; i.encode(e)?;
@ -210,9 +212,14 @@ impl Encode for Constant {
encode_constant_value(a, e)?; encode_constant_value(a, e)?;
encode_constant_value(b, e)?; encode_constant_value(b, e)?;
} }
Constant::Data(_) => { Constant::Data(data) => {
encode_constant(&[8], e)?; encode_constant(&[8], e)?;
todo!()
let cbor = data
.encode_fragment()
.map_err(|err| en::Error::Message(err.to_string()))?;
cbor.encode(e)?;
} }
} }