Add cached Merkle tree structure to wire transaction and root hash as transaction id.

This commit is contained in:
Katarzyna Streich 2016-10-18 11:18:55 +01:00
parent 6af7573955
commit e7cb47ecd0

View File

@ -38,16 +38,15 @@ class WireTransaction(
// Cache the serialised form of the transaction and its hash to give us fast access to it. // Cache the serialised form of the transaction and its hash to give us fast access to it.
@Volatile @Transient private var cachedBits: SerializedBytes<WireTransaction>? = null @Volatile @Transient private var cachedBits: SerializedBytes<WireTransaction>? = null
val serialized: SerializedBytes<WireTransaction> get() = cachedBits ?: serialize().apply { cachedBits = this } val serialized: SerializedBytes<WireTransaction> get() = cachedBits ?: serialize().apply { cachedBits = this }
// override val id: SecureHash get() = serialized.hash //todo remove
//We need cashed leaves hashes and whole tree for an id and Partial Merkle Tree calculation.
//We need cashed leaves hashes for id and Partial Merkle Tree calculation.
@Volatile @Transient private var cachedLeavesHashes: List<SecureHash>? = null @Volatile @Transient private var cachedLeavesHashes: List<SecureHash>? = null
val allLeavesHashes: List<SecureHash> get() = cachedLeavesHashes ?: calculateLeavesHashes().apply { cachedLeavesHashes = this } val allLeavesHashes: List<SecureHash> get() = cachedLeavesHashes ?: calculateLeavesHashes().apply { cachedLeavesHashes = this }
//TODO There is a problem with that it's failing 4 tests. Also in few places in code, there was reference to tx.serialized.hash @Volatile @Transient var cachedTree: MerkleTree? = null
// instead of tx.id. val merkleTree: MerkleTree get() = cachedTree ?: MerkleTree.getMerkleTree(allLeavesHashes).apply { cachedTree = this }
override val id: SecureHash get() = getMerkleRoot(allLeavesHashes) //TODO There is a problem with that, it's failing 4 tests.
override val id: SecureHash get() = merkleTree.hash
companion object { companion object {
fun deserialize(bits: SerializedBytes<WireTransaction>, kryo: Kryo = THREAD_LOCAL_KRYO.get()): WireTransaction { fun deserialize(bits: SerializedBytes<WireTransaction>, kryo: Kryo = THREAD_LOCAL_KRYO.get()): WireTransaction {