mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-22 14:33:06 +01:00
Drop AutoInc col for Broadcastable Transaction Table (#1630)
This commit is contained in:
parent
02453db411
commit
f7037c00a2
6 changed files with 49 additions and 29 deletions
|
@ -69,6 +69,6 @@ class DbManagementTest extends BitcoinSAsyncTest with EmbeddedPg {
|
|||
dbConfig(ProjectType.Node))
|
||||
val nodeDbManagement = createNodeDbManagement(nodeAppConfig)
|
||||
val result = nodeDbManagement.migrate()
|
||||
assert(result == 1)
|
||||
assert(result == 2)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package org.bitcoins.node.models
|
||||
|
||||
import org.bitcoins.server.BitcoinSAppConfig
|
||||
import org.bitcoins.testkit.{BitcoinSTestAppConfig, EmbeddedPg}
|
||||
import org.bitcoins.testkit.Implicits._
|
||||
import org.bitcoins.testkit.core.gen.TransactionGenerators
|
||||
import org.bitcoins.testkit.fixtures.NodeDAOFixture
|
||||
import org.bitcoins.testkit.{BitcoinSTestAppConfig, EmbeddedPg}
|
||||
|
||||
class BroadcastAbleTransactionDAOTest extends NodeDAOFixture with EmbeddedPg {
|
||||
|
||||
|
@ -20,7 +20,7 @@ class BroadcastAbleTransactionDAOTest extends NodeDAOFixture with EmbeddedPg {
|
|||
|
||||
for {
|
||||
created <- txDAO.create(BroadcastAbleTransaction(tx))
|
||||
read <- txDAO.read(created.id.get)
|
||||
read <- txDAO.read(created.transaction.txIdBE)
|
||||
} yield assert(read.contains(created))
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE broadcast_elements DROP COLUMN id;
|
||||
ALTER TABLE broadcast_elements ADD CONSTRAINT pk_broadcast_tx_id PRIMARY KEY (txid);
|
|
@ -0,0 +1,7 @@
|
|||
-- This block drops the "id" column
|
||||
CREATE TEMPORARY TABLE "broadcast_elements_backup" ("txid" VARCHAR(254) NOT NULL UNIQUE,"tx_bytes" VARCHAR(254) NOT NULL);
|
||||
INSERT INTO "broadcast_elements_backup" SELECT "txid", "tx_bytes" FROM "broadcast_elements";
|
||||
DROP TABLE "broadcast_elements";
|
||||
CREATE TABLE "broadcast_elements" ("txid" VARCHAR(254) PRIMARY KEY,"tx_bytes" VARCHAR(254) NOT NULL);
|
||||
INSERT INTO "broadcast_elements" SELECT "txid", "tx_bytes" FROM "broadcast_elements";
|
||||
DROP TABLE "broadcast_elements_backup";
|
|
@ -1,12 +1,6 @@
|
|||
package org.bitcoins.node.models
|
||||
|
||||
import org.bitcoins.core.protocol.transaction.Transaction
|
||||
import org.bitcoins.db.DbRowAutoInc
|
||||
|
||||
/** TXs we can broadcast over the P2P network */
|
||||
final case class BroadcastAbleTransaction(
|
||||
transaction: Transaction,
|
||||
id: Option[Long] = None)
|
||||
extends DbRowAutoInc[BroadcastAbleTransaction] {
|
||||
def copyWithId(id: Long): BroadcastAbleTransaction = copy(id = Some(id))
|
||||
}
|
||||
final case class BroadcastAbleTransaction(transaction: Transaction)
|
||||
|
|
|
@ -1,20 +1,19 @@
|
|||
package org.bitcoins.node.models
|
||||
|
||||
import org.bitcoins.db.CRUDAutoInc
|
||||
import org.bitcoins.node.config.NodeAppConfig
|
||||
|
||||
import scala.concurrent.ExecutionContext
|
||||
import slick.lifted.ProvenShape
|
||||
|
||||
import scala.concurrent.Future
|
||||
import org.bitcoins.core.protocol.transaction.Transaction
|
||||
import org.bitcoins.crypto.{DoubleSha256Digest, DoubleSha256DigestBE}
|
||||
import org.bitcoins.db.{CRUD, SlickUtil}
|
||||
import org.bitcoins.node.config.NodeAppConfig
|
||||
import scodec.bits.ByteVector
|
||||
import slick.lifted.ProvenShape
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
|
||||
final case class BroadcastAbleTransactionDAO()(implicit
|
||||
override val appConfig: NodeAppConfig,
|
||||
val ec: ExecutionContext)
|
||||
extends CRUDAutoInc[BroadcastAbleTransaction] {
|
||||
extends CRUD[BroadcastAbleTransaction, DoubleSha256DigestBE]
|
||||
with SlickUtil[BroadcastAbleTransaction, DoubleSha256DigestBE] {
|
||||
|
||||
import profile.api._
|
||||
val mappers = new org.bitcoins.db.DbCommonsColumnMappers(profile)
|
||||
|
@ -23,6 +22,27 @@ final case class BroadcastAbleTransactionDAO()(implicit
|
|||
override val table: profile.api.TableQuery[BroadcastAbleTransactionTable] =
|
||||
profile.api.TableQuery[BroadcastAbleTransactionTable]
|
||||
|
||||
override def createAll(ts: Vector[BroadcastAbleTransaction]): Future[
|
||||
Vector[BroadcastAbleTransaction]] = createAllNoAutoInc(ts, safeDatabase)
|
||||
|
||||
/** Finds the rows that correlate to the given primary keys */
|
||||
override protected def findByPrimaryKeys(
|
||||
txIds: Vector[DoubleSha256DigestBE]): Query[
|
||||
BroadcastAbleTransactionTable,
|
||||
BroadcastAbleTransaction,
|
||||
Seq] = {
|
||||
table.filter(_.txid.inSet(txIds))
|
||||
}
|
||||
|
||||
override protected def findAll(ts: Vector[BroadcastAbleTransaction]): Query[
|
||||
BroadcastAbleTransactionTable,
|
||||
BroadcastAbleTransaction,
|
||||
Seq] = findByPrimaryKeys(ts.map(_.transaction.txIdBE))
|
||||
|
||||
def findByHash(
|
||||
hash: DoubleSha256DigestBE): Future[Option[BroadcastAbleTransaction]] =
|
||||
findByHash(hash.flip)
|
||||
|
||||
/** Searches for a TX by its TXID */
|
||||
def findByHash(
|
||||
hash: DoubleSha256Digest): Future[Option[BroadcastAbleTransaction]] = {
|
||||
|
@ -35,26 +55,23 @@ final case class BroadcastAbleTransactionDAO()(implicit
|
|||
|
||||
/** Table over TXs we can broadcast over the P2P network */
|
||||
class BroadcastAbleTransactionTable(tag: Tag)
|
||||
extends TableAutoInc[BroadcastAbleTransaction](tag,
|
||||
"broadcast_elements") {
|
||||
private type Tuple = (DoubleSha256DigestBE, ByteVector, Option[Long])
|
||||
extends Table[BroadcastAbleTransaction](tag, "broadcast_elements") {
|
||||
private type Tuple = (DoubleSha256DigestBE, ByteVector)
|
||||
|
||||
private val fromTuple: (Tuple => BroadcastAbleTransaction) = {
|
||||
case (txid, bytes, id) =>
|
||||
private val fromTuple: Tuple => BroadcastAbleTransaction = {
|
||||
case (txid, bytes) =>
|
||||
val tx = Transaction.fromBytes(bytes)
|
||||
require(tx.txId == txid.flip)
|
||||
BroadcastAbleTransaction(tx, id)
|
||||
BroadcastAbleTransaction(tx)
|
||||
}
|
||||
|
||||
private val toTuple: BroadcastAbleTransaction => Option[Tuple] = tx =>
|
||||
Some(tx.transaction.txId.flip, tx.transaction.bytes, tx.id)
|
||||
Some(tx.transaction.txId.flip, tx.transaction.bytes)
|
||||
|
||||
def txid: Rep[DoubleSha256DigestBE] = column("txid", O.Unique)
|
||||
def txid: Rep[DoubleSha256DigestBE] = column("txid", O.PrimaryKey)
|
||||
def bytes: Rep[ByteVector] = column("tx_bytes")
|
||||
|
||||
def * : ProvenShape[BroadcastAbleTransaction] =
|
||||
(txid, bytes, id.?) <>
|
||||
(fromTuple, toTuple)
|
||||
(txid, bytes) <> (fromTuple, toTuple)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue