mirror of
https://github.com/corda/corda.git
synced 2025-03-11 15:04:14 +00:00
Add simple filtering and ordering of command leaves.
This commit is contained in:
parent
7f6f1807b1
commit
2ce21842f8
@ -29,25 +29,44 @@ class MerkleTransaction(
|
|||||||
val merkleRoot = merkleTree.last()
|
val merkleRoot = merkleTree.last()
|
||||||
|
|
||||||
val allLeavesHashes: MutableList<SecureHash> = ArrayList()
|
val allLeavesHashes: MutableList<SecureHash> = ArrayList()
|
||||||
|
|
||||||
|
//todo naive version with inputs, outputs, attachemets each as one block
|
||||||
getTransactionBlocks(wtx).mapTo(allLeavesHashes, { it.sha256() })
|
getTransactionBlocks(wtx).mapTo(allLeavesHashes, { it.sha256() })
|
||||||
val filteredCommands: MutableList<Command> = ArrayList()
|
val filteredCommands: MutableList<Command> = ArrayList()
|
||||||
val includeLeaves: MutableList<Boolean> = ArrayList()
|
val includeLeaves: MutableList<Boolean> = ArrayList()
|
||||||
wtx.commands.forEach {
|
|
||||||
val include = filterFunction(it)
|
filterLeaves(filterFunction, wtx, includeLeaves, filteredCommands)
|
||||||
if(include) filteredCommands.add(it)
|
|
||||||
includeLeaves.add(include)
|
|
||||||
}
|
|
||||||
|
|
||||||
val pmt = PartialMerkleTree.build(includeLeaves, allLeavesHashes)
|
val pmt = PartialMerkleTree.build(includeLeaves, allLeavesHashes)
|
||||||
return MerkleTransaction(merkleRoot, filteredCommands, pmt)
|
return MerkleTransaction(merkleRoot, filteredCommands, pmt)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function that splits the transaction into serialized blocks.
|
//todo type -> only on in, out, att, cmd
|
||||||
Blocks: inputs, outputs, attachments, commands. */
|
private fun filterLeaves(filterFunction: (Command) -> Boolean,
|
||||||
|
wtx: WireTransaction,
|
||||||
|
includeLeaves: MutableList<Boolean>,
|
||||||
|
filteredCommands: MutableList<Command> ){
|
||||||
|
//todo glued together for now
|
||||||
|
val tmpArr = arrayListOf(false, false, false)
|
||||||
|
includeLeaves.addAll(tmpArr)
|
||||||
|
val orderedCmds = wtx.commands.sortedBy { it.toString() }
|
||||||
|
|
||||||
|
orderedCmds.forEach { //todo should go on all in/outputs etc.
|
||||||
|
val include = filterFunction(it)
|
||||||
|
if(include) filteredCommands.add(it)
|
||||||
|
includeLeaves.add(include)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Function that splits the transaction into serialized blocks.
|
||||||
|
* Blocks: inputs, outputs, attachments, commands.
|
||||||
|
*/
|
||||||
private fun getTransactionBlocks(wtx: WireTransaction) : MutableList<ByteArray> {
|
private fun getTransactionBlocks(wtx: WireTransaction) : MutableList<ByteArray> {
|
||||||
val blocks: MutableList<ByteArray> = ArrayList()
|
val blocks: MutableList<ByteArray> = ArrayList()
|
||||||
val toBlockList = listOf(wtx.inputs, wtx.outputs, wtx.attachments, wtx.commands) //todo ordering
|
val toBlockList = listOf(wtx.inputs, wtx.outputs, wtx.attachments)
|
||||||
toBlockList.flatMapTo(blocks, { listOf(it.serialize().bits) } )
|
val orderedCmds = wtx.commands.sortedBy { it.toString() }
|
||||||
|
toBlockList.mapTo(blocks, { it.serialize().bits } )
|
||||||
|
blocks.addAll(orderedCmds.map { it.serialize().bits })
|
||||||
return blocks
|
return blocks
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,6 +110,8 @@ class MerkleTransaction(
|
|||||||
//todo exception
|
//todo exception
|
||||||
fun verify():Boolean{
|
fun verify():Boolean{
|
||||||
val hashes: List<SecureHash> = filteredCommands.map { it.serialize().sha256() }
|
val hashes: List<SecureHash> = filteredCommands.map { it.serialize().sha256() }
|
||||||
|
if(hashes.size == 0)
|
||||||
|
throw MerkleTreeException("Transaction without included leaves.")
|
||||||
return partialMerkleTree.verify(hashes, merkleRoot)
|
return partialMerkleTree.verify(hashes, merkleRoot)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user