Calculate transaction and output values

This commit is contained in:
Mark Spanbroek 2021-08-04 14:23:46 +02:00
parent 7db55248fa
commit 94b4cfb825
2 changed files with 19 additions and 0 deletions

View File

@ -88,3 +88,12 @@ func hasValidSignature*(transaction: Transaction): bool =
let signature = transaction.signature let signature = transaction.signature
let signee = aggregate(signees) let signee = aggregate(signees)
signee.verify(message, signature) signee.verify(message, signature)
func value*(transaction: Transaction): UInt256 =
for (_, value) in transaction.outputs:
result += value
func outputValue*(transaction: Transaction, owner: PublicKey): UInt256 =
for (outputOwner, value) in transaction.outputs:
if outputOwner == owner:
result += value

View File

@ -27,6 +27,16 @@ suite "Transactions":
check transaction.inputs.len == 1 check transaction.inputs.len == 1
check transaction.outputs.len == 2 check transaction.outputs.len == 2
test "transaction value is the sum of its output values":
let genesis = !Transaction.init({alice: 32.u256, bob: 10.u256}, victor)
check genesis.value == 42.u256
test "output value is the value of the output for given owner":
let genesis = !Transaction.init({alice: 32.u256, bob: 10.u256}, victor)
check genesis.outputValue(alice) == 32.u256
check genesis.outputValue(bob) == 10.u256
check genesis.outputValue(victor) == 0.u256
test "a transaction can be converted to bytes": test "a transaction can be converted to bytes":
let genesis = !Transaction.init({alice: 32.u256, bob: 10.u256}, victor) let genesis = !Transaction.init({alice: 32.u256, bob: 10.u256}, victor)
let transaction = !Transaction.init( let transaction = !Transaction.init(