Re-format and re-run all acceptance tests.

This commit is contained in:
KtorZ
2024-01-20 10:43:17 +01:00
parent 9ee2d58ba3
commit b50e4ab63a
77 changed files with 449 additions and 830 deletions

View File

@@ -13,4 +13,4 @@ requirements = []
source = "github"
[etags]
"aiken-lang/stdlib@main" = [{ secs_since_epoch = 1705181066, nanos_since_epoch = 607926000 }, "cf946239d3dd481ed41f20e56bf24910b5229ea35aa171a708edc2a47fc20a7b"]
"aiken-lang/stdlib@main" = [{ secs_since_epoch = 1705743643, nanos_since_epoch = 283477000 }, "cf946239d3dd481ed41f20e56bf24910b5229ea35aa171a708edc2a47fc20a7b"]

View File

@@ -37,25 +37,19 @@ pub fn to_list(self: MerkleTree<a>) -> List<a> {
[]
Leaf { value, .. } ->
[value]
Node { left, right, .. } ->
list.concat(to_list(left), to_list(right))
Node { left, right, .. } -> list.concat(to_list(left), to_list(right))
}
}
test to_list_1() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let items =
[dog, cat, mouse]
let hash_fn =
create_string_item_hash_fn()
let hash_fn = create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let mt = from_list(items, hash_fn)
items == to_list(mt)
}
@@ -63,54 +57,39 @@ test to_list_1() {
// Function returning a hash of a given Merkle Tree element
pub fn root_hash(self: MerkleTree<a>) -> Hash<Sha2_256, ByteArray> {
when self is {
Empty ->
""
Leaf { hash, .. } ->
hash
Node { hash, .. } ->
hash
Empty -> ""
Leaf { hash, .. } -> hash
Node { hash, .. } -> hash
}
}
test root_hash_1() {
let dog =
"dog"
let dog = "dog"
let items =
[dog]
let hash_fn =
create_string_item_hash_fn()
let hash_fn = create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let mt = from_list(items, hash_fn)
let node_hash =
hash_fn(dog)
let node_hash = hash_fn(dog)
root_hash(mt) == node_hash
}
test root_hash_3() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let items =
[dog, cat, mouse]
let hash_fn =
create_string_item_hash_fn()
let hash_fn = create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let mt = from_list(items, hash_fn)
let node_hash =
sha2_256(bytearray.concat(hash_fn(cat), hash_fn(mouse)))
let rh =
sha2_256(bytearray.concat(hash_fn(dog), node_hash))
let node_hash = sha2_256(bytearray.concat(hash_fn(cat), hash_fn(mouse)))
let rh = sha2_256(bytearray.concat(hash_fn(dog), node_hash))
expect Node { hash: root_hash, .. } =
mt
expect Node { hash: root_hash, .. } = mt
rh == root_hash
}
@@ -118,13 +97,10 @@ test root_hash_3() {
test root_hash_2() {
let items =
[]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let node_hash =
#""
let node_hash = #""
root_hash(mt) == node_hash
}
@@ -137,78 +113,55 @@ pub fn is_equal(left: MerkleTree<a>, right: MerkleTree<a>) -> Bool {
/// Function returns a total numbers of leaves in the tree.
pub fn size(self: MerkleTree<a>) -> Int {
when self is {
Empty ->
0
Leaf { .. } ->
1
Node { left, right, .. } ->
size(left) + size(right)
Empty -> 0
Leaf { .. } -> 1
Node { left, right, .. } -> size(left) + size(right)
}
}
test size_1() {
let items =
[]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
size(mt) == 0
}
test size_2() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let hash_fn =
create_string_item_hash_fn()
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let hash_fn = create_string_item_hash_fn()
let mt =
from_list([dog, cat, mouse], hash_fn)
let mt = from_list([dog, cat, mouse], hash_fn)
size(mt) == 3
}
test size_3() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let items =
[dog, cat, mouse]
let hash_fn =
create_string_item_hash_fn()
let hash_fn = create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let mt = from_list(items, hash_fn)
size(mt) == 3
}
test size_4() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let horse =
"horse"
let pig =
"pig"
let bull =
"bull"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let horse = "horse"
let pig = "pig"
let bull = "bull"
let hash_fn =
create_string_item_hash_fn()
let hash_fn = create_string_item_hash_fn()
let items =
[dog, cat, mouse, horse, pig, bull]
let mt =
from_list(items, hash_fn)
let mt = from_list(items, hash_fn)
size(mt) == 6
}
@@ -222,28 +175,22 @@ fn combine_hash(
/// Function that returns whether merkle tree has any elements
pub fn is_empty(self: MerkleTree<a>) -> Bool {
when self is {
Empty ->
True
_ ->
False
Empty -> True
_ -> False
}
}
test is_empty_1() {
let mt =
Empty
let mt = Empty
is_empty(mt)
}
test is_empty_2() {
let dog =
"dog"
let hash =
create_string_item_hash_fn()
let dog = "dog"
let hash = create_string_item_hash_fn()
let mt =
Leaf { value: dog, hash: hash(dog) }
let mt = Leaf { value: dog, hash: hash(dog) }
is_empty(mt) == False
}
@@ -255,8 +202,7 @@ fn do_proof(
hash_fn: fn(a) -> Hash<Sha2_256, a>,
) -> Option<Proof> {
when self is {
Empty ->
None
Empty -> None
Leaf { hash, .. } ->
if hash == item_hash {
Some(proof)
@@ -264,10 +210,8 @@ fn do_proof(
None
}
Node { left, right, .. } -> {
let rh =
root_hash(right)
let lh =
root_hash(left)
let rh = root_hash(right)
let lh = root_hash(left)
let go_left: Option<Proof> =
do_proof(left, item_hash, list.push(proof, Right { hash: rh }), hash_fn)
let go_right: Option<Proof> =
@@ -292,130 +236,91 @@ pub fn get_proof(
}
test get_proof_1() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let horse =
"horse"
let pig =
"pig"
let bull =
"bull"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let horse = "horse"
let pig = "pig"
let bull = "bull"
let items =
[dog, cat, mouse, horse, pig, bull]
let hash_fn =
create_string_item_hash_fn()
let hash_fn = create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn =
create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let maybe_proof: Option<Proof> =
get_proof(mt, "parrot", hash_fn)
let maybe_proof: Option<Proof> = get_proof(mt, "parrot", hash_fn)
is_none(maybe_proof)
}
test get_proof_2() {
let dog =
"dog"
let dog = "dog"
let items =
[dog]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let maybe_proof: Option<Proof> =
get_proof(mt, dog, hash_fn)
let maybe_proof: Option<Proof> = get_proof(mt, dog, hash_fn)
expect Some(proof) =
maybe_proof
expect Some(proof) = maybe_proof
// when proof is empty list it actually means that root of the tree is in fact element
proof == []
}
test get_proof_3() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let items =
[dog, cat, mouse]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let node_hash =
sha2_256(bytearray.concat(hash_fn(cat), hash_fn(mouse)))
let node_hash = sha2_256(bytearray.concat(hash_fn(cat), hash_fn(mouse)))
let maybe_proof: Option<Proof> =
get_proof(mt, dog, hash_fn)
let maybe_proof: Option<Proof> = get_proof(mt, dog, hash_fn)
expect Some(proof) =
maybe_proof
expect Some(proof) = maybe_proof
let size_match =
list.length(proof) == 1
let size_match = list.length(proof) == 1
expect Some(p1) =
list.at(proof, 0)
expect Some(p1) = list.at(proof, 0)
let h1: ByteArray =
get_proof_item_value(p1)
let h1: ByteArray = get_proof_item_value(p1)
size_match && h1 == node_hash
}
test get_proof_4() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let horse =
"horse"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let horse = "horse"
let items =
[dog, cat, mouse, horse]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let right_node_hash =
sha2_256(bytearray.concat(hash_fn(mouse), hash_fn(horse)))
let maybe_proof: Option<Proof> =
get_proof(mt, dog, hash_fn)
let maybe_proof: Option<Proof> = get_proof(mt, dog, hash_fn)
expect Some(proof) =
maybe_proof
expect Some(proof) = maybe_proof
let size_match =
list.length(proof) == 2
let size_match = list.length(proof) == 2
expect Some(p1) =
list.at(proof, 0)
expect Some(p2) =
list.at(proof, 1)
expect Some(p1) = list.at(proof, 0)
expect Some(p2) = list.at(proof, 1)
let h1: ByteArray =
get_proof_item_value(p1)
let h2: ByteArray =
get_proof_item_value(p2)
let h1: ByteArray = get_proof_item_value(p1)
let h2: ByteArray = get_proof_item_value(p2)
size_match && h1 == hash_fn(cat) && h2 == right_node_hash
}
@@ -426,16 +331,13 @@ fn do_from_list(
hash_fn: fn(a) -> Hash<Sha2_256, a>,
) -> MerkleTree<a> {
when items is {
[] ->
Empty
[] -> Empty
[item] -> {
let hashed_item =
hash_fn(item)
let hashed_item = hash_fn(item)
Leaf { value: item, hash: hashed_item }
}
all -> {
let cutoff: Int =
len / 2
let cutoff: Int = len / 2
let left =
all
|> list.take(cutoff)
@@ -444,8 +346,7 @@ fn do_from_list(
all
|> list.drop(cutoff)
|> do_from_list(len - cutoff, hash_fn)
let hash =
combine_hash(root_hash(left), root_hash(right))
let hash = combine_hash(root_hash(left), root_hash(right))
Node { hash, left, right }
}
}
@@ -462,46 +363,35 @@ pub fn from_list(
}
test from_1() {
let _a =
-1
let _a = -1
let items =
[]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
Empty == mt
}
test from_2() {
let dog =
"dog"
let dog = "dog"
let items =
[dog]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
Leaf { value: dog, hash: hash_fn(dog) } == mt
}
test from_3() {
let dog =
"dog"
let cat =
"cat"
let dog = "dog"
let cat = "cat"
let items =
[dog, cat]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let root_hash =
sha2_256(bytearray.concat(hash_fn(dog), hash_fn(cat)))
let root_hash = sha2_256(bytearray.concat(hash_fn(dog), hash_fn(cat)))
Node {
hash: root_hash,
@@ -511,25 +401,18 @@ test from_3() {
}
test from_4() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let items =
[dog, cat, mouse]
let hash_fn =
create_string_item_hash_fn()
let hash_fn = create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let mt = from_list(items, hash_fn)
let node_hash =
sha2_256(bytearray.concat(hash_fn(cat), hash_fn(mouse)))
let root_hash =
sha2_256(bytearray.concat(hash_fn(dog), node_hash))
let node_hash = sha2_256(bytearray.concat(hash_fn(cat), hash_fn(mouse)))
let root_hash = sha2_256(bytearray.concat(hash_fn(dog), node_hash))
Node {
hash: root_hash,
@@ -543,30 +426,22 @@ test from_4() {
}
test from_5() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let horse =
"horse"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let horse = "horse"
let hash_fn =
create_string_item_hash_fn()
let hash_fn = create_string_item_hash_fn()
let items =
[dog, cat, mouse, horse]
let mt =
from_list(items, hash_fn)
let mt = from_list(items, hash_fn)
let left_node_hash =
sha2_256(bytearray.concat(hash_fn(dog), hash_fn(cat)))
let left_node_hash = sha2_256(bytearray.concat(hash_fn(dog), hash_fn(cat)))
let right_node_hash =
sha2_256(bytearray.concat(hash_fn(mouse), hash_fn(horse)))
let root_hash =
sha2_256(bytearray.concat(left_node_hash, right_node_hash))
let root_hash = sha2_256(bytearray.concat(left_node_hash, right_node_hash))
Node {
hash: root_hash,
@@ -594,8 +469,7 @@ pub fn member_from_hash(
hash_fn: fn(a) -> Hash<Sha2_256, a>,
) -> Bool {
when proof is {
[] ->
root_hash == item_hash
[] -> root_hash == item_hash
[head, ..tail] ->
when head is {
Left { hash: l } ->
@@ -614,173 +488,119 @@ pub fn member(
proof: Proof,
hash_fn: fn(a) -> Hash<Sha2_256, a>,
) -> Bool {
let item_hash =
hash_fn(item)
let item_hash = hash_fn(item)
member_from_hash(item_hash, root_hash, proof, hash_fn)
}
test member_1() {
let dog =
"dog"
let dog = "dog"
let items =
[dog]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let item =
dog
let rh =
root_hash(mt)
let item = dog
let rh = root_hash(mt)
expect Some(proof) =
get_proof(mt, item, hash_fn)
expect Some(proof) = get_proof(mt, item, hash_fn)
member(item: item, root_hash: rh, proof: proof, hash_fn: hash_fn)
}
test member_2() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let horse =
"horse"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let horse = "horse"
let items =
[dog, cat, mouse, horse]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let item =
cat
let rh =
root_hash(mt)
let item = cat
let rh = root_hash(mt)
expect Some(proof) =
get_proof(mt, item, hash_fn)
expect Some(proof) = get_proof(mt, item, hash_fn)
member(item: item, root_hash: rh, proof: proof, hash_fn: hash_fn)
}
test member_3() {
let dog =
"dog"
let cat =
"cat"
let dog = "dog"
let cat = "cat"
let items =
[dog, cat]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let item =
cat
let rh =
root_hash(mt)
let item = cat
let rh = root_hash(mt)
expect Some(proof) =
get_proof(mt, item, hash_fn)
expect Some(proof) = get_proof(mt, item, hash_fn)
member(item: item, root_hash: rh, proof: proof, hash_fn: hash_fn)
}
test member_4() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let items =
[dog, cat, mouse]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let item =
mouse
let rh =
root_hash(mt)
let item = mouse
let rh = root_hash(mt)
expect Some(proof) =
get_proof(mt, item, hash_fn)
expect Some(proof) = get_proof(mt, item, hash_fn)
member(item: item, root_hash: rh, proof: proof, hash_fn: hash_fn)
}
test member_5() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let horse =
"horse"
let pig =
"pig"
let bull =
"bull"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let horse = "horse"
let pig = "pig"
let bull = "bull"
let items =
[dog, cat, mouse, horse, pig, bull]
let hash_fn =
create_string_item_hash_fn()
let mt =
from_list(items, hash_fn)
let hash_fn = create_string_item_hash_fn()
let mt = from_list(items, hash_fn)
let item =
pig
let rh =
root_hash(mt)
let item = pig
let rh = root_hash(mt)
expect Some(proof) =
get_proof(mt, item, hash_fn)
expect Some(proof) = get_proof(mt, item, hash_fn)
member(item: item, root_hash: rh, proof: proof, hash_fn: hash_fn)
}
test member_6() {
let dog =
"dog"
let cat =
"cat"
let mouse =
"mouse"
let horse =
"horse"
let pig =
"pig"
let bull =
"bull"
let dog = "dog"
let cat = "cat"
let mouse = "mouse"
let horse = "horse"
let pig = "pig"
let bull = "bull"
let hash_fn =
create_string_item_hash_fn()
let hash_fn = create_string_item_hash_fn()
let items =
[dog, cat, mouse, horse, pig, bull]
let mt =
from_list(items, hash_fn)
let mt = from_list(items, hash_fn)
let item =
"parrot"
let item = "parrot"
let proof =
get_proof(mt, item, hash_fn)
let proof = get_proof(mt, item, hash_fn)
proof == None
}
fn get_proof_item_value(proof_item: ProofItem) -> Hash<Sha2_256, ByteArray> {
when proof_item is {
Left(x) ->
x
Right(y) ->
y
Left(x) -> x
Right(y) -> y
}
}