nim-abc/tests/testTransactions.nim

43 lines
1.3 KiB
Nim
Raw Normal View History

2021-06-28 13:04:50 +00:00
import std/unittest
import abc/transactions
import ./examples
suite "Transactions":
let alice, bob = PublicKey.example
test "a genesis transaction can be made":
let genesis = Transaction.init({alice: 32.u256, bob: 10.u256})
test "a transaction has a hash":
let transaction1, transaction2 = Transaction.example
check transaction1.hash == transaction1.hash
check transaction2.hash == transaction2.hash
check transaction1.hash != transaction2.hash
test "a transaction references outputs from other transactions":
let genesis = Transaction.init({alice: 32.u256, bob: 10.u256})
let transaction = Transaction.init(
{genesis.hash: alice},
{alice: 2.u256, bob: 30.u256}
)
check transaction.inputs.len == 1
check transaction.outputs.len == 2
test "a transaction can be converted to bytes":
let genesis = Transaction.init({alice: 32.u256, bob: 10.u256})
let transaction = Transaction.init(
{genesis.hash: alice},
{alice: 2.u256, bob: 30.u256}
)
var expected: seq[byte]
expected.add(1) # amount of inputs
expected.add(genesis.hash.toBytes)
expected.add(alice.toBytes)
expected.add(2) # amount of outputs
expected.add(alice.toBytes)
expected.add(2.u256.toBytes)
expected.add(bob.toBytes)
expected.add(30.u256.toBytes)
check transaction.toBytes == expected