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

View File

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

View File

@@ -1,5 +1,7 @@
use std::fmt::Display;
use pallas_primitives::alonzo::PlutusData;
use crate::{
builtins::DefaultFunction,
debruijn::{self, Converter},
@@ -109,7 +111,7 @@ pub enum Constant {
// tag: 7
// Apply(Box<Constant>, Type),
// tag: 8
Data(Data),
Data(PlutusData),
}
#[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
/// and a unique id from string interning. The Name's text is
/// interned during parsing.

View File

@@ -5,6 +5,7 @@ use flat_rs::{
en::{self, Encode, Encoder},
Flat,
};
use pallas_primitives::Fragment;
use crate::{
ast::{
@@ -78,6 +79,7 @@ where
fn decode(d: &mut Decoder) -> Result<Self, de::Error> {
let version = (usize::decode(d)?, usize::decode(d)?, usize::decode(d)?);
let term = Term::decode(d)?;
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 {
fn encode(&self, e: &mut Encoder) -> Result<(), en::Error> {
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) => {
encode_constant(&[0], e)?;
i.encode(e)?;
@@ -210,9 +212,14 @@ impl Encode for Constant {
encode_constant_value(a, e)?;
encode_constant_value(b, e)?;
}
Constant::Data(_) => {
Constant::Data(data) => {
encode_constant(&[8], e)?;
todo!()
let cbor = data
.encode_fragment()
.map_err(|err| en::Error::Message(err.to_string()))?;
cbor.encode(e)?;
}
}