feat: output build assets

This commit is contained in:
rvcas 2022-11-08 22:08:19 -05:00 committed by Lucas
parent 4db0c93061
commit 7e0767ef74
14 changed files with 248 additions and 115 deletions

47
Cargo.lock generated
View File

@ -85,8 +85,11 @@ name = "aiken-project"
version = "0.0.24"
dependencies = [
"aiken-lang",
"hex",
"ignore",
"miette",
"pallas",
"pallas-traverse",
"petgraph",
"regex",
"serde",
@ -601,6 +604,21 @@ version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]]
name = "pallas"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a9a54e891592befe6d7da8e43dc61606b86375d7a094dd78cc4cdf32e26d779"
dependencies = [
"pallas-addresses",
"pallas-codec",
"pallas-crypto",
"pallas-miniprotocols",
"pallas-multiplexer",
"pallas-primitives",
"pallas-traverse",
]
[[package]]
name = "pallas-addresses"
version = "0.14.0"
@ -640,6 +658,34 @@ dependencies = [
"thiserror",
]
[[package]]
name = "pallas-miniprotocols"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "665d8bba23eaa69ce5715249d69c0b8c49c6034fc3f5ea4f010f9437d6fc7167"
dependencies = [
"hex",
"itertools",
"log",
"pallas-codec",
"pallas-multiplexer",
"thiserror",
]
[[package]]
name = "pallas-multiplexer"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fe342b801c5954cfd31159bc68a38cb597e18d7a4ae89fc9397f5abcb5c5696"
dependencies = [
"byteorder",
"hex",
"log",
"pallas-codec",
"rand",
"thiserror",
]
[[package]]
name = "pallas-primitives"
version = "0.14.0"
@ -956,6 +1002,7 @@ version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",

View File

@ -69,6 +69,8 @@ impl UntypedModule {
pub type TypedDefinition = Definition<Arc<Type>, TypedExpr, String, String>;
pub type UntypedDefinition = Definition<(), UntypedExpr, (), ()>;
pub type TypedFunction = Function<Arc<Type>, TypedExpr>;
#[derive(Debug, Clone, PartialEq)]
pub struct Function<T, Expr> {
pub arguments: Vec<Arg<T>>,

View File

@ -185,7 +185,7 @@ impl<'a> CodeGenerator<'a> {
}
pub(crate) fn recurse_scope_level(&mut self, body: &TypedExpr, scope_level: ScopeLevels) {
match dbg!(body) {
match body {
TypedExpr::Int { .. } => {}
TypedExpr::String { .. } => {}
TypedExpr::ByteArray { .. } => {}
@ -393,7 +393,7 @@ impl<'a> CodeGenerator<'a> {
value: &TypedExpr,
scope_level: ScopeLevels,
) {
match dbg!(pattern) {
match pattern {
Pattern::Int { .. } | Pattern::String { .. } | Pattern::Var { .. } => {
self.recurse_scope_level(value, scope_level);
}
@ -403,11 +403,11 @@ impl<'a> CodeGenerator<'a> {
Pattern::Discard { .. } => todo!(),
Pattern::List { .. } => todo!(),
Pattern::Constructor {
name: constructor_name,
// name: constructor_name,
tipo,
arguments,
constructor,
module,
// arguments,
// constructor,
// module,
..
} => {
self.recurse_scope_level(value, scope_level.scope_increment_sequence(1));
@ -428,17 +428,17 @@ impl<'a> CodeGenerator<'a> {
}
}
Type::Fn { .. } => {
let mapping_index = match constructor {
tipo::PatternConstructor::Record { name, field_map } => {
if let Some(fields_mapping) = field_map {
fields_mapping.fields.clone()
} else {
HashMap::new()
}
}
};
let mut args = arguments.clone();
let local_var_name = "";
// let mapping_index = match constructor {
// tipo::PatternConstructor::Record { name, field_map } => {
// if let Some(fields_mapping) = field_map {
// fields_mapping.fields.clone()
// } else {
// HashMap::new()
// }
// }
// };
// let mut args = arguments.clone();
// let local_var_name = "";
// arguments.iter().map(|x| {
// let name = match &x.value {
// Pattern::Var { location, name } => {
@ -480,7 +480,7 @@ impl<'a> CodeGenerator<'a> {
}
fn recurse_code_gen(&mut self, body: &TypedExpr, scope_level: ScopeLevels) -> Term<Name> {
match dbg!(body) {
match body {
TypedExpr::Int { value, .. } => {
Term::Constant(Constant::Integer(value.parse::<i128>().unwrap()))
}
@ -1090,6 +1090,7 @@ impl<'a> CodeGenerator<'a> {
}
// Pull out all uplc data holder and data usage, filter by Scope Level, Sort By Scope Depth, Then Apply
#[allow(clippy::type_complexity)]
let mut data_holder: Vec<((String, String, String), (bool, ScopeLevels, u64))> = self
.uplc_data_usage_holder_lookup
.iter()

View File

@ -16,7 +16,10 @@ miette = { version = "5.3.0", features = ["fancy"] }
petgraph = "0.6.2"
regex = "1.6.0"
serde = { version = "1.0.144", features = ["derive"] }
serde_json = "1.0.85"
serde_json = { version = "1.0.85", features = ["preserve_order"] }
thiserror = "1.0.37"
toml = "0.5.9"
walkdir = "2.3.2"
hex = "0.4.3"
pallas = "0.14.0"
pallas-traverse = "0.14.0"

View File

@ -8,15 +8,22 @@ pub mod config;
pub mod error;
pub mod format;
pub mod module;
pub mod script;
use aiken_lang::{
ast::{Definition, Function, ModuleKind},
ast::{Definition, Function, ModuleKind, TypedFunction},
builtins,
tipo::TypeInfo,
uplc::CodeGenerator,
IdGenerator,
};
use uplc::ast::{NamedDeBruijn, Program};
use pallas::{
codec::minicbor,
ledger::{addresses::Address, primitives::babbage},
};
use pallas_traverse::ComputeHash;
use script::Script;
use serde_json::json;
use crate::{
config::Config,
@ -88,7 +95,9 @@ impl Project {
let scripts = self.validate_scripts(&mut checked_modules)?;
if uplc_gen {
self.code_gen(scripts, &checked_modules)?;
let programs = self.code_gen(scripts, &checked_modules)?;
self.write_build_outputs(programs)?;
}
Ok(())
@ -238,27 +247,19 @@ impl Project {
fn validate_scripts(
&self,
checked_modules: &mut CheckedModules,
) -> Result<Vec<CheckedModule>, Error> {
) -> Result<Vec<(String, TypedFunction)>, Error> {
let mut errors = Vec::new();
let mut scripts = Vec::new();
let mut indices_to_remove = Vec::new();
for module in checked_modules.scripts() {
scripts.push(module.clone());
for def in module.ast.definitions() {
if let Definition::Fn(Function {
arguments,
location,
name,
return_type,
..
}) = def
{
if VALIDATOR_NAMES.contains(&name.as_str()) {
for (index, def) in module.ast.definitions().enumerate() {
if let Definition::Fn(func_def) = def {
if VALIDATOR_NAMES.contains(&func_def.name.as_str()) {
// validators must return a Bool
if !return_type.is_bool() {
if !func_def.return_type.is_bool() {
errors.push(Error::ValidatorMustReturnBool {
location: *location,
location: func_def.location,
src: module.code.clone(),
path: module.input_path.clone(),
})
@ -266,35 +267,40 @@ impl Project {
// depending on name, validate the minimum number of arguments
// if too low, push a new error on to errors
if [MINT, CERT, WITHDRAWL].contains(&name.as_str()) && arguments.len() < 2 {
if [MINT, CERT, WITHDRAWL].contains(&func_def.name.as_str())
&& func_def.arguments.len() < 2
{
errors.push(Error::WrongValidatorArity {
location: *location,
location: func_def.location,
src: module.code.clone(),
path: module.input_path.clone(),
name: name.clone(),
name: func_def.name.clone(),
at_least: 2,
})
}
if SPEND == name && arguments.len() < 3 {
if SPEND == func_def.name && func_def.arguments.len() < 3 {
errors.push(Error::WrongValidatorArity {
location: *location,
location: func_def.location,
src: module.code.clone(),
path: module.input_path.clone(),
name: name.clone(),
name: func_def.name.clone(),
at_least: 3,
})
}
scripts.push((module.name.clone(), func_def.clone()));
indices_to_remove.push(index);
}
}
}
for index in indices_to_remove.drain(0..) {
module.ast.definitions.remove(index);
}
}
if errors.is_empty() {
for script in &scripts {
checked_modules.remove(&script.name);
}
Ok(scripts)
} else {
Err(Error::List(errors))
@ -303,9 +309,9 @@ impl Project {
fn code_gen(
&mut self,
scripts: Vec<CheckedModule>,
scripts: Vec<(String, TypedFunction)>,
checked_modules: &CheckedModules,
) -> Result<Vec<Program<NamedDeBruijn>>, Error> {
) -> Result<Vec<Script>, Error> {
let mut programs = Vec::new();
let mut functions = HashMap::new();
let mut type_aliases = HashMap::new();
@ -335,35 +341,106 @@ impl Project {
}
}
for script in scripts {
for def in script.ast.into_definitions() {
if let Definition::Fn(Function {
arguments,
name,
body,
..
}) = def
{
if VALIDATOR_NAMES.contains(&name.as_str()) {
let mut generator = CodeGenerator::new(
&functions,
// &type_aliases,
&data_types,
// &imports,
// &constants,
);
for (module_name, func_def) in scripts {
let Function {
arguments,
name,
body,
..
} = func_def;
let program = generator.generate(body, arguments);
let mut generator = CodeGenerator::new(
&functions,
// &type_aliases,
&data_types,
// &imports,
// &constants,
);
programs.push(program.try_into().unwrap());
}
}
}
let program = generator.generate(body, arguments);
let script = Script::new(module_name, name, program.try_into().unwrap());
programs.push(script);
}
Ok(programs)
}
fn write_build_outputs(&self, programs: Vec<Script>) -> Result<(), Error> {
let assets = self.root.join("assets");
for script in programs {
let script_output_dir = assets.join(script.module).join(script.name);
fs::create_dir_all(&script_output_dir)?;
let cbor = script.program.to_cbor().unwrap();
// Create file containing just the script cbor hex
let script_path = script_output_dir.join("script.txt");
let cbor_hex = hex::encode(&cbor);
fs::write(script_path, &cbor_hex)?;
// Create the payment script JSON file
let payment_script_path = script_output_dir.join("payment_script.json");
let mut bytes = Vec::new();
let mut encoder = minicbor::Encoder::new(&mut bytes);
encoder.bytes(&cbor).unwrap();
let prefixed_cbor_hex = hex::encode(&bytes);
let payment_script = json!({
"type": "PlutusScriptV2",
"description": "Generated by Aiken",
"cborHex": prefixed_cbor_hex
});
fs::write(
payment_script_path,
serde_json::to_string_pretty(&payment_script).unwrap(),
)?;
// Create mainnet and testnet addresses
let plutus_script = babbage::PlutusV2Script(cbor.into());
let hash = plutus_script.compute_hash();
// mainnet
let mainnet_path = script_output_dir.join("mainnet.txt");
let mut mainnet_bytes: Vec<u8> = vec![0b01110001];
mainnet_bytes.extend(hash.iter());
let mainnet_addr = Address::from_bytes(&mainnet_bytes)
.unwrap()
.to_bech32()
.unwrap();
fs::write(mainnet_path, mainnet_addr)?;
// testnet
let testnet_path = script_output_dir.join("testnet.txt");
let mut testnet_bytes: Vec<u8> = vec![0b01110000];
testnet_bytes.extend(hash.iter());
let testnet_addr = Address::from_bytes(&testnet_bytes)
.unwrap()
.to_bech32()
.unwrap();
fs::write(testnet_path, testnet_addr)?;
}
Ok(())
}
fn aiken_files(&mut self, dir: &Path, kind: ModuleKind) -> Result<(), Error> {
let paths = walkdir::WalkDir::new(dir)
.follow_links(true)

View File

@ -181,8 +181,8 @@ impl From<CheckedModules> for HashMap<String, CheckedModule> {
}
impl CheckedModules {
pub fn scripts(&self) -> impl Iterator<Item = &CheckedModule> {
self.0.values().filter(|module| module.kind.is_script())
pub fn scripts(&mut self) -> impl Iterator<Item = &mut CheckedModule> {
self.0.values_mut().filter(|module| module.kind.is_script())
}
pub fn into_scripts(self) -> impl Iterator<Item = CheckedModule> {

View File

@ -0,0 +1,18 @@
use uplc::ast::{NamedDeBruijn, Program};
#[derive(Debug)]
pub struct Script {
pub module: String,
pub name: String,
pub program: Program<NamedDeBruijn>,
}
impl Script {
pub fn new(module: String, name: String, program: Program<NamedDeBruijn>) -> Script {
Script {
module,
name,
program,
}
}
}

View File

@ -0,0 +1 @@
addr1w8eyldeh4n25jfcaa7uya7cutcs0p0ra20lm8yvn5adapwcxnmsex

View File

@ -0,0 +1,5 @@
{
"type": "PlutusScriptV2",
"description": "Generated by Aiken",
"cborHex": "5905355905320100002105646174756d00210472646d7200210363747800320114636f6e7374725f6669656c645f6765745f61726700320115636f6e7374725f6669656c64735f6578706f73657200320101790032010c646174756d5f6669656c64730032010e646174756d5f6669656c645f736300320115646174756d5f6669656c645f73635f6669656c64730032011b646174756d5f6669656c645f73635f6669656c645f7369676e6572003201017800320122646174756d5f6669656c645f73635f6669656c645f7369676e65725f6669656c647300320126646174756d5f6669656c645f73635f6669656c645f7369676e65725f6669656c645f68617368003201016100320110646174756d5f6669656c645f72646d7200320101620032011b63686f6f73655f73616d706c655f52656465656d5f636f6e737472003330011b63686f6f73655f73616d706c655f52656465656d5f636f6e7374720001010162000213370e900124004266e1d200a48008810b636f6e7374725f646174610032010c636f6e7374725f696e64657800210f636f6e7374725f305f726573756c740021176c6173745f636f6e7374727563746f725f726573756c74005333573466e1d20000010c636f6e7374725f696e6465780003010f636f6e7374725f305f726573756c74000201176c6173745f636f6e7374727563746f725f726573756c740001373a6ea8010b636f6e7374725f6461746100010110646174756d5f6669656c645f72646d720001330114636f6e7374725f6669656c645f6765745f617267000b010c646174756d5f6669656c6473000848010126646174756d5f6669656c645f73635f6669656c645f7369676e65725f6669656c645f686173680001330114636f6e7374725f6669656c645f6765745f61726700090122646174756d5f6669656c645f73635f6669656c645f7369676e65725f6669656c6473000148000c0115636f6e7374725f6669656c64735f6578706f7365720007011b646174756d5f6669656c645f73635f6669656c645f7369676e65720002011b646174756d5f6669656c645f73635f6669656c645f7369676e65720001330114636f6e7374725f6669656c645f6765745f61726700060115646174756d5f6669656c645f73635f6669656c6473000148000c0115636f6e7374725f6669656c64735f6578706f7365720004010e646174756d5f6669656c645f73630001330114636f6e7374725f6669656c645f6765745f6172670004010c646174756d5f6669656c6473000148008c0115636f6e7374725f6669656c64735f6578706f73657200020105646174756d00064801090a636f6e7374725f7661720037586e78dd50010a636f6e7374725f7661720001210b636f6e7374725f6c69737400210a6172675f6e756d6265720032010772656375727365003330010772656375727365000101077265637572736500014800010b636f6e7374725f6c6973740003210d73656c665f7265637572736f7200211263757272656e745f6172675f6e756d6265720021136c6973745f6f665f636f6e7374725f61726773003333573466e1c10a6172675f6e756d6265720004011263757272656e745f6172675f6e756d62657200025742411b63757272656e745f6c6973745f6f665f636f6e7374725f61726773003330010d73656c665f7265637572736f720004010d73656c665f7265637572736f7200043370011263757272656e745f6172675f6e756d626572000348008d5d10011b63757272656e745f6c6973745f6f665f636f6e7374725f61726773000101136c6973745f6f665f636f6e7374725f61726773000101"
}

View File

@ -0,0 +1 @@
5905355905320100002105646174756d00210472646d7200210363747800320114636f6e7374725f6669656c645f6765745f61726700320115636f6e7374725f6669656c64735f6578706f73657200320101790032010c646174756d5f6669656c64730032010e646174756d5f6669656c645f736300320115646174756d5f6669656c645f73635f6669656c64730032011b646174756d5f6669656c645f73635f6669656c645f7369676e6572003201017800320122646174756d5f6669656c645f73635f6669656c645f7369676e65725f6669656c647300320126646174756d5f6669656c645f73635f6669656c645f7369676e65725f6669656c645f68617368003201016100320110646174756d5f6669656c645f72646d7200320101620032011b63686f6f73655f73616d706c655f52656465656d5f636f6e737472003330011b63686f6f73655f73616d706c655f52656465656d5f636f6e7374720001010162000213370e900124004266e1d200a48008810b636f6e7374725f646174610032010c636f6e7374725f696e64657800210f636f6e7374725f305f726573756c740021176c6173745f636f6e7374727563746f725f726573756c74005333573466e1d20000010c636f6e7374725f696e6465780003010f636f6e7374725f305f726573756c74000201176c6173745f636f6e7374727563746f725f726573756c740001373a6ea8010b636f6e7374725f6461746100010110646174756d5f6669656c645f72646d720001330114636f6e7374725f6669656c645f6765745f617267000b010c646174756d5f6669656c6473000848010126646174756d5f6669656c645f73635f6669656c645f7369676e65725f6669656c645f686173680001330114636f6e7374725f6669656c645f6765745f61726700090122646174756d5f6669656c645f73635f6669656c645f7369676e65725f6669656c6473000148000c0115636f6e7374725f6669656c64735f6578706f7365720007011b646174756d5f6669656c645f73635f6669656c645f7369676e65720002011b646174756d5f6669656c645f73635f6669656c645f7369676e65720001330114636f6e7374725f6669656c645f6765745f61726700060115646174756d5f6669656c645f73635f6669656c6473000148000c0115636f6e7374725f6669656c64735f6578706f7365720004010e646174756d5f6669656c645f73630001330114636f6e7374725f6669656c645f6765745f6172670004010c646174756d5f6669656c6473000148008c0115636f6e7374725f6669656c64735f6578706f73657200020105646174756d00064801090a636f6e7374725f7661720037586e78dd50010a636f6e7374725f7661720001210b636f6e7374725f6c69737400210a6172675f6e756d6265720032010772656375727365003330010772656375727365000101077265637572736500014800010b636f6e7374725f6c6973740003210d73656c665f7265637572736f7200211263757272656e745f6172675f6e756d6265720021136c6973745f6f665f636f6e7374725f61726773003333573466e1c10a6172675f6e756d6265720004011263757272656e745f6172675f6e756d62657200025742411b63757272656e745f6c6973745f6f665f636f6e7374725f61726773003330010d73656c665f7265637572736f720004010d73656c665f7265637572736f7200043370011263757272656e745f6172675f6e756d626572000348008d5d10011b63757272656e745f6c6973745f6f665f636f6e7374725f61726773000101136c6973745f6f665f636f6e7374725f61726773000101

View File

@ -0,0 +1 @@
addr_test1wreyldeh4n25jfcaa7uya7cutcs0p0ra20lm8yvn5adapwcam0vkr

View File

@ -1,38 +1,36 @@
pub type Signer {
hash: Int
hash: Int,
}
pub type ScriptContext {
signer: Signer
signer: Signer,
}
pub type Redeem {
Buy
Sell
Buy
Sell
}
pub type Datum {
fin: Int,
sc: ScriptContext,
rdmr: Redeem,
fin: Int,
sc: ScriptContext,
rdmr: Redeem,
}
pub fn eqInt(a: Int, b: Int) {
a == b
a == b
}
pub fn eqString(a: ByteArray, b: ByteArray) {
a == b
a == b
}
pub type Thing {
Some
None
Some
None
}
pub type Other {
Wow
Yes
Wow
Yes
}

View File

@ -7,16 +7,17 @@ pub type Redeemer {
Sell(Int)
}
pub fn spend(datum: sample.Datum, rdmr: Redeemer, ctx: spend.ScriptContext) -> Bool {
pub fn spend(
datum: sample.Datum,
rdmr: Redeemer,
ctx: spend.ScriptContext,
) -> Bool {
let y = 2
let x = datum.sc.signer
let a = datum.sc.signer.hash
let b = datum.rdmr
when b is {
sample.Buy -> 1 == 1
sample.Sell -> 5 == 1
sample.Sell -> 5 == 1
}
}

View File

@ -1,22 +0,0 @@
(program
1.0.0
(lam
datum
(lam
rdmr
(lam
ctx
[
(lam
a
[
(lam b [ [ (lam sample_thing sample_thing) (lam a a) ] a ])
(con bool False)
]
)
(con bool True)
]
)
)
)
)