diff --git a/src/main/kotlin/io/openfuture/chain/core/repository/jdbc/UTransactionsJdbcRepository.kt b/src/main/kotlin/io/openfuture/chain/core/repository/jdbc/UTransactionsJdbcRepository.kt new file mode 100644 index 000000000..63e637c18 --- /dev/null +++ b/src/main/kotlin/io/openfuture/chain/core/repository/jdbc/UTransactionsJdbcRepository.kt @@ -0,0 +1,61 @@ +package io.openfuture.chain.core.repository.jdbc + +import io.openfuture.chain.core.model.entity.transaction.payload.TransferTransactionPayload +import io.openfuture.chain.core.model.entity.transaction.unconfirmed.UnconfirmedTransferTransaction +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations +import org.springframework.jdbc.support.GeneratedKeyHolder +import org.springframework.stereotype.Repository + + +interface UTransactionsJdbcRepository { + fun save(utransaction: UnconfirmedTransferTransaction): UnconfirmedTransferTransaction +} + +@Repository +class UTransactionsJdbcRepositoryImpl( + private val namedParameterJdbcOperations: NamedParameterJdbcOperations +) : UTransactionsJdbcRepository { + + override fun save(utransaction: UnconfirmedTransferTransaction): UnconfirmedTransferTransaction { + val generatedKeyHolder = GeneratedKeyHolder() + val sqlParameterSource = MapSqlParameterSource() + + sqlParameterSource.addValue("timestamp", utransaction.timestamp) + sqlParameterSource.addValue("fee", utransaction.fee) + sqlParameterSource.addValue("senderAddress", utransaction.senderAddress) + sqlParameterSource.addValue("hash", utransaction.hash) + sqlParameterSource.addValue("signature", utransaction.signature) + sqlParameterSource.addValue("publicKey", utransaction.publicKey) + + namedParameterJdbcOperations.update( + "INSERT INTO U_TRANSACTIONS(TIMESTAMP, FEE, SENDER_ADDRESS, HASH, SIGNATURE, SENDER_KEY) " + + "VALUES (:timestamp, :fee, :senderAddress, :hash, :signature, :publicKey)", + sqlParameterSource, generatedKeyHolder + ) + + val id = generatedKeyHolder.key.toLong() + + saveUTransferTransaction(utransaction.getPayload(), id) + + utransaction.id = id + return utransaction + } + + private fun saveUTransferTransaction(t: TransferTransactionPayload, id: Long) { + val sqlParameterSource = MapSqlParameterSource() + + sqlParameterSource.addValue("id", id) + sqlParameterSource.addValue("amount", t.amount) + sqlParameterSource.addValue("address", t.recipientAddress) + sqlParameterSource.addValue("data", t.data) + + namedParameterJdbcOperations.update( + "insert into U_TRANSACTIONS(ID, AMOUNT, RECIPIENT_ADDRESS, DATA) values (:id, amount, address, data)", + sqlParameterSource + ) + + } + + +} \ No newline at end of file diff --git a/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUDelegateTransactionService.kt b/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUDelegateTransactionService.kt index 6d94f2804..43d84096d 100644 --- a/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUDelegateTransactionService.kt +++ b/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUDelegateTransactionService.kt @@ -2,6 +2,7 @@ package io.openfuture.chain.core.service.transaction.unconfirmed import io.openfuture.chain.core.model.entity.transaction.unconfirmed.UnconfirmedDelegateTransaction import io.openfuture.chain.core.repository.UDelegateTransactionRepository +import io.openfuture.chain.core.repository.jdbc.UTransactionsJdbcRepository import io.openfuture.chain.core.service.UDelegateTransactionService import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -9,5 +10,6 @@ import org.springframework.transaction.annotation.Transactional @Service @Transactional(readOnly = true) class DefaultUDelegateTransactionService( - uRepository: UDelegateTransactionRepository -) : DefaultUTransactionService(uRepository), UDelegateTransactionService \ No newline at end of file + uRepository: UDelegateTransactionRepository, + jdbcRepository: UTransactionsJdbcRepository +) : DefaultUTransactionService(uRepository, jdbcRepository), UDelegateTransactionService \ No newline at end of file diff --git a/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUTransactionService.kt b/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUTransactionService.kt index a1927b635..c493c34e5 100644 --- a/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUTransactionService.kt +++ b/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUTransactionService.kt @@ -12,6 +12,7 @@ import io.openfuture.chain.core.model.entity.transaction.unconfirmed.Unconfirmed import io.openfuture.chain.core.model.entity.transaction.unconfirmed.UnconfirmedVoteTransaction import io.openfuture.chain.core.repository.TransactionRepository import io.openfuture.chain.core.repository.UTransactionRepository +import io.openfuture.chain.core.repository.jdbc.UTransactionsJdbcRepository import io.openfuture.chain.core.service.UTransactionService import io.openfuture.chain.core.service.transaction.validation.DelegateTransactionValidator import io.openfuture.chain.core.service.transaction.validation.TransferTransactionValidator @@ -25,7 +26,8 @@ import org.springframework.transaction.annotation.Transactional @Transactional(readOnly = true) abstract class DefaultUTransactionService( - private val uRepository: UTransactionRepository + private val uRepository: UTransactionRepository, + private val jdbcRepository: UTransactionsJdbcRepository ) : UTransactionService { @Autowired private lateinit var networkService: NetworkApiService @@ -93,9 +95,19 @@ abstract class DefaultUTransactionService( else -> throw IllegalStateException("Wrong type") } - val savedUtx = uRepository.saveAndFlush(uTx) - networkService.broadcast(savedUtx.toMessage()) - return savedUtx + // + + return if (uTx is UnconfirmedTransferTransaction) { + val savedUtx = jdbcRepository.save(uTx) + networkService.broadcast(savedUtx.toMessage()) + uTx.id = savedUtx.id + return uTx + } else { + val savedUtx = uRepository.saveAndFlush(uTx) + networkService.broadcast(savedUtx.toMessage()) + savedUtx + } + } finally { BlockchainLock.writeLock.unlock() } diff --git a/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUTransferTransactionService.kt b/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUTransferTransactionService.kt index 21ff5f705..ef0675771 100644 --- a/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUTransferTransactionService.kt +++ b/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUTransferTransactionService.kt @@ -2,6 +2,7 @@ package io.openfuture.chain.core.service.transaction.unconfirmed import io.openfuture.chain.core.model.entity.transaction.unconfirmed.UnconfirmedTransferTransaction import io.openfuture.chain.core.repository.UTransferTransactionRepository +import io.openfuture.chain.core.repository.jdbc.UTransactionsJdbcRepository import io.openfuture.chain.core.service.UTransferTransactionService import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -9,5 +10,6 @@ import org.springframework.transaction.annotation.Transactional @Service @Transactional(readOnly = true) class DefaultUTransferTransactionService( - uRepository: UTransferTransactionRepository -) : DefaultUTransactionService(uRepository), UTransferTransactionService \ No newline at end of file + uRepository: UTransferTransactionRepository, + jdbcRepository: UTransactionsJdbcRepository +) : DefaultUTransactionService(uRepository, jdbcRepository), UTransferTransactionService \ No newline at end of file diff --git a/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUVoteTransactionService.kt b/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUVoteTransactionService.kt index af3743b54..523a3c8a4 100644 --- a/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUVoteTransactionService.kt +++ b/src/main/kotlin/io/openfuture/chain/core/service/transaction/unconfirmed/DefaultUVoteTransactionService.kt @@ -3,6 +3,7 @@ package io.openfuture.chain.core.service.transaction.unconfirmed import io.openfuture.chain.core.model.entity.dictionary.VoteType.AGAINST import io.openfuture.chain.core.model.entity.transaction.unconfirmed.UnconfirmedVoteTransaction import io.openfuture.chain.core.repository.UVoteTransactionRepository +import io.openfuture.chain.core.repository.jdbc.UTransactionsJdbcRepository import io.openfuture.chain.core.service.UVoteTransactionService import io.openfuture.chain.core.sync.BlockchainLock import org.springframework.stereotype.Service @@ -11,8 +12,9 @@ import org.springframework.transaction.annotation.Transactional @Service @Transactional(readOnly = true) class DefaultUVoteTransactionService( - private val uRepository: UVoteTransactionRepository -) : DefaultUTransactionService(uRepository), UVoteTransactionService { + private val uRepository: UVoteTransactionRepository, + jdbcRepository: UTransactionsJdbcRepository +) : DefaultUTransactionService(uRepository, jdbcRepository), UVoteTransactionService { override fun getBySenderAgainstDelegate(senderAddress: String, delegateKey: String): UnconfirmedVoteTransaction? { BlockchainLock.readLock.lock()