def createSummary(self, blocks):
if all(isinstance(block, Block) for block in blocks):
utxos = []
for blk in blocks:
for transaction in blk.transactions:
utxos += transaction.utxos
outgoing = [(utxo.sender, utxo.value) for utxo in utxos]
incoming = [(utxo.receiver, utxo.value) for utxo in utxos]
senders = set([utxo.sender for utxo in utxos])
receivers = set([utxo.receiver for utxo in utxos])
for wallet in list(senders) + list(receivers):
self.changes[wallet] = 0
for sender in senders:
self.changes[sender] = -sum(map(lambda v: v[1], filter(lambda u: u[0] == sender, outgoing)))
for receiver in receivers:
self.changes[receiver] += sum(map(lambda v: v[1], filter(lambda u: u[0] == receiver, incoming)))
elif all(isinstance(block, SummaryBlock) for block in blocks):
all_keys = reduce(operator.add,[list(block.changes.keys()) for block in blocks])
for key in all_keys:
self.changes[key] = 0
for block in blocks:
for key, value in block.changes.items():
self.changes[key] += value
else:
raise TypeError('Invalid typing of blocks')
评论列表
文章目录