Skip to content

Commit

Permalink
Jdbc for transaction repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Beksultan committed Oct 30, 2023
1 parent decdb67 commit 0fb5166
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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
)

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ 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

@Service
@Transactional(readOnly = true)
class DefaultUDelegateTransactionService(
uRepository: UDelegateTransactionRepository
) : DefaultUTransactionService<UnconfirmedDelegateTransaction>(uRepository), UDelegateTransactionService
uRepository: UDelegateTransactionRepository,
jdbcRepository: UTransactionsJdbcRepository
) : DefaultUTransactionService<UnconfirmedDelegateTransaction>(uRepository, jdbcRepository), UDelegateTransactionService
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +26,8 @@ import org.springframework.transaction.annotation.Transactional

@Transactional(readOnly = true)
abstract class DefaultUTransactionService<uT : UnconfirmedTransaction>(
private val uRepository: UTransactionRepository<uT>
private val uRepository: UTransactionRepository<uT>,
private val jdbcRepository: UTransactionsJdbcRepository
) : UTransactionService<uT> {

@Autowired private lateinit var networkService: NetworkApiService
Expand Down Expand Up @@ -93,9 +95,19 @@ abstract class DefaultUTransactionService<uT : UnconfirmedTransaction>(
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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ 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

@Service
@Transactional(readOnly = true)
class DefaultUTransferTransactionService(
uRepository: UTransferTransactionRepository
) : DefaultUTransactionService<UnconfirmedTransferTransaction>(uRepository), UTransferTransactionService
uRepository: UTransferTransactionRepository,
jdbcRepository: UTransactionsJdbcRepository
) : DefaultUTransactionService<UnconfirmedTransferTransaction>(uRepository, jdbcRepository), UTransferTransactionService
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,8 +12,9 @@ import org.springframework.transaction.annotation.Transactional
@Service
@Transactional(readOnly = true)
class DefaultUVoteTransactionService(
private val uRepository: UVoteTransactionRepository
) : DefaultUTransactionService<UnconfirmedVoteTransaction>(uRepository), UVoteTransactionService {
private val uRepository: UVoteTransactionRepository,
jdbcRepository: UTransactionsJdbcRepository
) : DefaultUTransactionService<UnconfirmedVoteTransaction>(uRepository, jdbcRepository), UVoteTransactionService {

override fun getBySenderAgainstDelegate(senderAddress: String, delegateKey: String): UnconfirmedVoteTransaction? {
BlockchainLock.readLock.lock()
Expand Down

0 comments on commit 0fb5166

Please sign in to comment.