Skip to content

Commit

Permalink
Add debuggers
Browse files Browse the repository at this point in the history
  • Loading branch information
t0anhh committed Mar 21, 2024
1 parent 4444c35 commit c2474fa
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 30 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ So we can attach it to anywhere like this:

```diff
Input {
output_reference: OutputReference(TransactionId(tx_0), 0),
output_reference: OutputReference(TransactionId('tx_0'), 0),
output: Output {
address: Address {
payment_credential: ScriptCredential('script_hash')
Expand All @@ -105,17 +105,21 @@ Input {
+ |> log(stringify.input)
```

> You can use defined loggers inside the `debug` module
## Supported serializers

- [x] input
- [x] output
- [x] credential
- [x] address
- [x] value
- [x] minted_value
- [x] data
- [x] out_ref
- [x] tx
- [x] redeemers
- [ ] any

## License

Expand Down
153 changes: 153 additions & 0 deletions lib/debug.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
use aiken/builtin
use aiken/dict.{Dict}
use aiken/string
use aiken/transaction.{
Datum, Input, Output, OutputReference, Redeemer, ScriptPurpose, Transaction,
}
use aiken/transaction/credential.{Address, Credential}
use aiken/transaction/value.{MintedValue, Value}
use stringify

fn debug(self: a, name: String, serializer: fn(a) -> String) -> a {
builtin.debug(string.concat(name, serializer(self)), self)
}

pub fn diagnostic(self: a, name: String, d: String) -> a {
debug(
self,
string.concat(
name,
@"\n",
),
fn(_) { stringify.format_cbor(d) |> stringify.indent },
)
}

pub fn data(self: Data, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
stringify.data,
)
}

pub fn tx(self: Transaction) {
debug(
self,
@"\n",
stringify.tx,
)
}

pub fn input(self: Input, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
stringify.input,
)
}

pub fn output(self: Output, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
stringify.output,
)
}

pub fn value(self: Value, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
stringify.value,
)
}

pub fn minted_value(self: MintedValue, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
stringify.minted_value,
)
}

pub fn int(self: Int, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
string.from_int,
)
}

pub fn redeemers(self: Dict<ScriptPurpose, Redeemer>, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
stringify.redeemers,
)
}

pub fn credential(self: Credential, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
stringify.credential,
)
}

pub fn out_ref(self: OutputReference, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
stringify.out_ref,
)
}

pub fn address(self: Address, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
stringify.address,
)
}

pub fn datum(self: Datum, name: String) {
debug(
self,
string.concat(
name,
@"\n",
),
stringify.datum,
)
}
28 changes: 12 additions & 16 deletions lib/stringify.ak
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn int(val) -> String {
string.from_int(val)
}

fn credential(cred: Credential) -> String {
pub fn credential(cred: Credential) -> String {
when cred is {
VerificationKeyCredential(hash) ->
string.concat(
Expand Down Expand Up @@ -246,10 +246,6 @@ pub fn indent(str: String) {
do_indent(string.to_bytearray(str), 0) |> bytearray.to_string
}

pub fn log(self: a, msg: String, serializer: fn(a) -> String) -> a {
builtin.debug(string.concat(msg, serializer(self)), self)
}

fn do_indent(bytes: ByteArray, lvl: Int) {
let next_newline_cursor = bytearray.index_of(bytes, "\n")
when next_newline_cursor is {
Expand Down Expand Up @@ -329,30 +325,30 @@ fn as_block(builder: String) {
|> string.join(builder)
}

pub fn first_of_quad(quad: (a, b, c, d)) {
fn first_of_quad(quad: (a, b, c, d)) {
quad.1st
}

pub fn first_of_triple(tuple: (a, b, c)) {
fn first_of_triple(tuple: (a, b, c)) {
tuple.1st
}

pub fn pop_back(self: ByteArray, qty: Int) {
fn pop_back(self: ByteArray, qty: Int) {
bytearray.take(self, builtin.length_of_bytearray(self) - qty)
}

pub fn last_char(self: ByteArray) {
fn last_char(self: ByteArray) {
bytearray.drop(self, builtin.length_of_bytearray(self) - 1)
}

pub fn is_open(chr: ByteArray) {
fn is_open(chr: ByteArray) {
or {
chr == "[",
chr == "{",
}
}

pub fn is_close(chr: ByteArray) {
fn is_close(chr: ByteArray) {
or {
chr == "]",
chr == "}",
Expand All @@ -363,7 +359,7 @@ pub fn newline(self: ByteArray) {
self |> bytearray.concat("\n")
}

pub fn to_decimal(byte: Int) {
fn to_decimal(byte: Int) {
if byte >= 97 && byte <= 102 {
byte - 97 + 10
} else if byte >= 65 && byte <= 70 {
Expand All @@ -375,7 +371,7 @@ pub fn to_decimal(byte: Int) {
}
}

pub fn hex_to_string(hex: ByteArray) {
fn hex_to_string(hex: ByteArray) {
bytearray.foldl(
// pushing a dummy byte to flush the last character
hex |> bytearray.concat("0"),
Expand All @@ -398,18 +394,18 @@ pub fn hex_to_string(hex: ByteArray) {
|> first_of_triple
}

fn format_cbor(cbr: String) {
pub fn format_cbor(cbr: String) {
cbr
|> bytearray.from_string
|> do_format_cbor
|> bytearray.to_string
}

pub fn to_bytearray(byte: Int) {
fn to_bytearray(byte: Int) {
#"" |> bytearray.push(byte)
}

pub fn do_format_cbor(bytes) {
fn do_format_cbor(bytes) {
bytes
|> bytearray.foldl(
("", 0, False, ""),
Expand Down
27 changes: 14 additions & 13 deletions lib/stringify_test.ak
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ use aiken/transaction/credential.{
Address, Inline, ScriptCredential, VerificationKeyCredential,
}
use aiken/transaction/value
use stringify

fn log(self: a, serializer: fn(a) -> String) {
trace serializer(self)
self
}
use debug

test log_int() {
( 1 |> log(stringify.int) ) == 1
( 1 |> debug.int(@"int") ) == 1
}

test log_value() {
Expand All @@ -27,7 +22,7 @@ test log_value() {
|> value.add("pid", "name", 1)
(
my_value
|> log(stringify.value)
|> debug.value(@"value")
) == my_value
}

Expand All @@ -45,7 +40,10 @@ test log_input() {
reference_script: None,
},
}
( my_input |> log(stringify.input) ) == my_input
(
my_input
|> debug.input(@"input")
) == my_input
}

test log_output() {
Expand All @@ -59,7 +57,7 @@ test log_output() {
datum: NoDatum,
reference_script: None,
}
( my_output |> log(stringify.output) ) == my_output
( my_output |> debug.output(@"output") ) == my_output
}

type MyDatum {
Expand Down Expand Up @@ -89,7 +87,7 @@ test log_output_2() {
),
reference_script: None,
}
( my_output |> log(stringify.output) ) == my_output
( my_output |> debug.output(@"output with datum") ) == my_output
}

test log_data() {
Expand All @@ -101,7 +99,10 @@ test log_data() {
int_list: [1, 2, 3, 4],
bytes_list: ["1", "2", "3"],
}
( struct |> log(fn(d) { stringify.data(d) }) ) == struct
(
struct
|> debug.diagnostic(@"data", cbor.diagnostic(struct))
) == struct
}

fn script_purpose_compare(a: ScriptPurpose, b: ScriptPurpose) {
Expand Down Expand Up @@ -184,5 +185,5 @@ test log_tx() {
},
],
}
( tx |> log(stringify.tx) ) == tx
( tx |> debug.tx ) == tx
}

0 comments on commit c2474fa

Please sign in to comment.