Fixes to encryption storage

This commit is contained in:
adam.houston 2022-03-14 14:02:41 +00:00
parent 9f4b46e8d4
commit bba864d86d
6 changed files with 23 additions and 16 deletions

View File

@ -130,6 +130,7 @@ object VaultMigrationSchema
object VaultMigrationSchemaV1 : MappedSchema(schemaFamily = VaultMigrationSchema.javaClass, version = 1,
mappedTypes = listOf(
DBTransactionStorage.DBTransaction::class.java,
DBTransactionStorage.DBEncryptedTransaction::class.java,
PersistentIdentityService.PersistentPublicKeyHashToCertificate::class.java,
PersistentIdentityService.PersistentPublicKeyHashToParty::class.java,
BasicHSMKeyManagementService.PersistentKey::class.java,

View File

@ -1,5 +1,6 @@
package net.corda.node.services.persistence
import com.github.benmanes.caffeine.cache.Weigher
import net.corda.core.concurrent.CordaFuture
import net.corda.core.crypto.SecureHash
import net.corda.core.crypto.TransactionSignature
@ -19,6 +20,7 @@ import net.corda.core.utilities.debug
import net.corda.node.CordaClock
import net.corda.node.services.api.WritableTransactionStorage
import net.corda.node.services.statemachine.FlowStateMachineImpl
import net.corda.node.utilities.AppendOnlyPersistentMap
import net.corda.node.utilities.AppendOnlyPersistentMapBase
import net.corda.node.utilities.WeightBasedAppendOnlyPersistentMap
import net.corda.nodeapi.internal.persistence.*
@ -165,7 +167,7 @@ class DBTransactionStorage(private val database: CordaPersistence, cacheFactory:
private fun createEncryptedTransactionsMap(cacheFactory: NamedCacheFactory, clock: CordaClock)
: AppendOnlyPersistentMapBase<SecureHash, TxCacheEncryptedValue, DBEncryptedTransaction, String> {
return WeightBasedAppendOnlyPersistentMap<SecureHash, TxCacheEncryptedValue, DBEncryptedTransaction, String>(
return AppendOnlyPersistentMap<SecureHash, TxCacheEncryptedValue, DBEncryptedTransaction, String>(
cacheFactory = cacheFactory,
name = "DBTransactionStorage_encrypted_transactions",
toPersistentEntityKey = SecureHash::toString,
@ -189,12 +191,10 @@ class DBTransactionStorage(private val database: CordaPersistence, cacheFactory:
timestamp = clock.instant()
)
},
persistentEntityClass = DBEncryptedTransaction::class.java,
weighingFunc = { hash, _ -> hash.size } // TODO: should their be weighing?
persistentEntityClass = DBEncryptedTransaction::class.java
)
}
// TODO: weight of transactions will be wrong at this stage for encrypted transactions
private fun weighTx(tx: AppendOnlyPersistentMapBase.Transactional<TxCacheValue>): Int {
val actTx = tx.peekableValue ?: return 0
return actTx.sigs.sumBy { it.size + transactionSignatureOverheadEstimate } + actTx.txBits.size

View File

@ -41,6 +41,7 @@ class NodeSchemaService(private val extraSchemas: Set<MappedSchema> = emptySet()
DBCheckpointStorage.DBFlowMetadata::class.java,
DBTransactionStorage.DBTransaction::class.java,
DBTransactionStorage.DBEncryptedTransaction::class.java,
BasicHSMKeyManagementService.PersistentKey::class.java,
NodeSchedulerService.PersistentScheduledState::class.java,
NodeAttachmentService.DBAttachment::class.java,

View File

@ -42,6 +42,7 @@ open class DefaultNamedCacheFactory protected constructor(private val metricRegi
name == "SerializationScheme_attachmentClassloader" -> caffeine
name == "HibernateConfiguration_sessionFactories" -> caffeine.maximumSize(database.mappedSchemaCacheSize)
name == "DBTransactionStorage_transactions" -> caffeine.maximumWeight(transactionCacheSizeBytes)
name == "DBTransactionStorage_encrypted_transactions" -> caffeine.maximumWeight(transactionCacheSizeBytes)
name == "NodeAttachmentService_attachmentContent" -> caffeine.maximumWeight(attachmentContentCacheSizeBytes)
name == "NodeAttachmentService_contractAttachmentVersions" -> caffeine.maximumSize(defaultCacheSize)
name == "PersistentIdentityService_keyToPartyAndCert" -> caffeine.maximumSize(defaultCacheSize)

View File

@ -21,7 +21,6 @@ import net.corda.core.toFuture
import net.corda.core.transactions.EncryptedTransaction
import net.corda.core.transactions.SignedTransaction
import net.corda.core.transactions.TransactionBuilder
import net.corda.core.transactions.VerifiedEncryptedTransaction
import net.corda.core.transactions.WireTransaction
import net.corda.core.utilities.days
import net.corda.core.utilities.getOrThrow
@ -790,18 +789,12 @@ class TwoPartyTradeFlowTests(private val anonymous: Boolean) {
}
// TODO: these Encrypted transactions may need an overhaul is probably indicative that overloading the current storage was a bad idea
override fun addVerifiedEncryptedTransaction(encryptedTransaction: VerifiedEncryptedTransaction): Boolean {
override fun addVerifiedEncryptedTransaction(encryptedTransaction: EncryptedTransaction): Boolean {
return database.transaction {
delegate.addVerifiedEncryptedTransaction(encryptedTransaction)
}
}
override fun getVerifiedEncryptedTransaction(id: SecureHash): VerifiedEncryptedTransaction? {
return database.transaction {
delegate.getVerifiedEncryptedTransaction(id)
}
}
override fun addUnverifiedEncryptedTransaction(encryptedTransaction: EncryptedTransaction) {
return database.transaction {
delegate.addUnverifiedEncryptedTransaction(encryptedTransaction)

View File

@ -187,6 +187,17 @@ class DBTransactionStorageTests {
return fromDb[0].timestamp
}
private fun readEncryptedTransactionTimestampFromDB(id: SecureHash): Instant {
val fromDb = database.transaction {
session.createQuery(
"from ${DBTransactionStorage.DBEncryptedTransaction::class.java.name} where tx_id = :transactionId",
DBTransactionStorage.DBEncryptedTransaction::class.java
).setParameter("transactionId", id.toString()).resultList.map { it }
}
assertEquals(1, fromDb.size)
return fromDb[0].timestamp
}
@Test(timeout = 300_000)
fun `empty store`() {
assertThat(transactionStorage.getTransaction(newTransaction().id)).isNull()
@ -419,9 +430,9 @@ class DBTransactionStorageTests {
encryptionCipher.init(Cipher.ENCRYPT_MODE, key, iv)
val encryptedTxBytes = encryptionCipher.doFinal(transaction.serialize(context = contextToUse().withEncoding(CordaSerializationEncoding.SNAPPY)).bytes)
val encryptedTx = EncryptedTransaction(transaction.id, encryptedTxBytes)
val encryptedTx = EncryptedTransaction(transaction.id, encryptedTxBytes, emptySet(), emptyList())
transactionStorage.addVerifiedEncryptedTransaction(encryptedTx.toVerified(byteArrayOf()))
transactionStorage.addVerifiedEncryptedTransaction(encryptedTx)
val storedTx = transactionStorage.getEncryptedTransaction(transaction.id)
@ -430,11 +441,11 @@ class DBTransactionStorageTests {
assertNotNull(storedTx, "Could not find stored encrypted message")
val decryptedTx = decryptionCipher.doFinal(storedTx!!.bytes).deserialize<SignedTransaction>(context = contextToUse())
val decryptedTx = decryptionCipher.doFinal(storedTx!!.encryptedBytes).deserialize<SignedTransaction>(context = contextToUse())
assertEquals(decryptedTx, transaction)
assertEquals(now, readTransactionTimestampFromDB(transaction.id))
assertEquals(now, readEncryptedTransactionTimestampFromDB(transaction.id))
}
fun generateIv(): IvParameterSpec? {