Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add main_chain to contracts and events #529

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ final case class ContractEntity(
destructionTimestamp: Option[TimeStamp],
destructionEventOrder: Option[Int],
category: Option[String],
interfaceId: Option[InterfaceIdEntity]
interfaceId: Option[InterfaceIdEntity],
mainChain: Boolean
) {
def destroyInfo(): Option[ContractEntity.DestroyInfo] =
for {
Expand Down Expand Up @@ -108,7 +109,8 @@ object ContractEntity {
destructionTimestamp = None,
destructionEventOrder = None,
category = None,
interfaceId = None
interfaceId = None,
mainChain = false
)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ final case class EventEntity(
timestamp: TimeStamp,
eventIndex: Int,
fields: ArraySeq[Val],
eventOrder: Int
eventOrder: Int,
mainChain: Boolean
) {
def toApi: Event = Event(
blockHash,
Expand Down Expand Up @@ -65,7 +66,8 @@ object EventEntity {
timestamp,
eventIndex,
fields,
order
order,
mainChain = false
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ object BlockQueries extends StrictLogging {
def whereClause(columnName: String): String =
Array.fill(blocks.size)(s"$columnName = ?").mkString(" OR ")

// format: off
val query =
s"""
BEGIN;
Expand All @@ -250,12 +251,14 @@ object BlockQueries extends StrictLogging {
UPDATE transaction_per_token SET main_chain = ? WHERE ${whereClause("block_hash")};
UPDATE token_tx_per_addresses SET main_chain = ? WHERE ${whereClause("block_hash")};
UPDATE token_outputs SET main_chain = ? WHERE ${whereClause("block_hash")};
UPDATE events SET main_chain = ? WHERE ${whereClause("block_hash")};
UPDATE contracts SET main_chain = ? WHERE ${whereClause("creation_block_hash")};
COMMIT;
"""

val parameters: SetParameter[Unit] =
(_: Unit, params: PositionedParameters) =>
(1 to 8) foreach { _ =>
(1 to 10) foreach { _ =>
params >> mainChain
blocks foreach (params >> _)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,24 @@ object ContractQueries {
}

def insertContractCreationEventEntities(events: Iterable[ContractEntity]): DBActionW[Int] = {
QuerySplitter.splitUpdates(rows = events, columnsPerRow = 7) { (events, placeholder) =>
QuerySplitter.splitUpdates(rows = events, columnsPerRow = 8) { (events, placeholder) =>
val query =
s"""
INSERT INTO contracts
("contract", "parent", "std_interface_id_guessed", "creation_block_hash", "creation_tx_hash","creation_timestamp","creation_event_order")
INSERT INTO contracts (
"contract",
"parent",
"std_interface_id_guessed",
"creation_block_hash",
"creation_tx_hash",
"creation_timestamp",
"creation_event_order",
"main_chain"
)
VALUES $placeholder
ON CONFLICT
ON CONSTRAINT contracts_pk
DO NOTHING
"""
""".stripMargin

val parameters: SetParameter[Unit] =
(_: Unit, params: PositionedParameters) =>
Expand All @@ -76,6 +84,7 @@ object ContractQueries {
params >> event.creationTxHash
params >> event.creationTimestamp
params >> event.creationEventOrder
params >> event.mainChain
}

SQLActionBuilder(
Expand Down Expand Up @@ -124,6 +133,7 @@ object ContractQueries {
SELECT *
FROM contracts
WHERE contract = $contract
AND main_chain = true
""".asASE[ContractEntity](contractEntityGetResult)
}

Expand All @@ -134,6 +144,7 @@ object ContractQueries {
SELECT parent
FROM contracts
WHERE contract = $contract
AND main_chain = true
LIMIT 1
""".asASE[Option[Address]](optionAddressGetResult).headOrNone.map(_.flatten)
}
Expand All @@ -143,6 +154,7 @@ object ContractQueries {
SELECT contract
FROM contracts
WHERE parent = $parent
AND main_chain = true
ORDER BY creation_timestamp DESC, creation_event_order
"""
.paginate(pagination)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,25 @@ import org.alephium.protocol.model.{Address, TransactionId}
object EventQueries {

def insertEventsQuery(events: Iterable[EventEntity]): DBActionW[Int] = {
QuerySplitter.splitUpdates(rows = events, columnsPerRow = 8) { (events, placeholder) =>
QuerySplitter.splitUpdates(rows = events, columnsPerRow = 9) { (events, placeholder) =>
val query =
s"""
INSERT INTO events ("block_hash", "tx_hash", "contract_address", "input_address","block_timestamp", "event_index", "fields", "event_order_in_block")
INSERT INTO events (
"block_hash",
"tx_hash",
"contract_address",
"input_address",
"block_timestamp",
"event_index",
"fields",
"event_order_in_block",
"main_chain"
)
VALUES $placeholder
ON CONFLICT
ON CONSTRAINT events_pk
DO NOTHING
"""
""".stripMargin

val parameters: SetParameter[Unit] =
(_: Unit, params: PositionedParameters) =>
Expand All @@ -51,6 +61,7 @@ object EventQueries {
params >> event.eventIndex
params >> event.fields
params >> event.eventOrder
params >> event.mainChain
}

// Return builder generated by Slick's string interpolation
Expand All @@ -66,6 +77,7 @@ object EventQueries {
SELECT *
FROM events
WHERE tx_hash = $txId
AND main_chain = true
ORDER BY event_order_in_block
""".asASE[EventEntity](eventGetResult)

Expand All @@ -77,6 +89,7 @@ object EventQueries {
SELECT *
FROM events
WHERE contract_address = $address
AND main_chain = true
ORDER BY block_timestamp DESC, event_order_in_block
"""
.paginate(pagination)
Expand All @@ -93,6 +106,7 @@ object EventQueries {
FROM events
WHERE contract_address = $contract
AND input_address = $input
AND main_chain = true
ORDER BY block_timestamp DESC
"""
.paginate(pagination)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ object ContractSchema extends SchemaMainChain[ContractEntity]("contracts") {
def category: Rep[Option[String]] = column[Option[String]]("category")
def interfaceId: Rep[Option[InterfaceIdEntity]] =
column[Option[InterfaceIdEntity]]("interface_id")
def mainChain: Rep[Boolean] = column[Boolean]("main_chain")

def * : ProvenShape[ContractEntity] =
(
Expand All @@ -63,7 +64,8 @@ object ContractSchema extends SchemaMainChain[ContractEntity]("contracts") {
destructionTimestamp,
destructionEventOrder,
category,
interfaceId
interfaceId,
mainChain
)
.<>((ContractEntity.apply _).tupled, ContractEntity.unapply)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ object CustomGetResult {
timestamp = result.<<,
eventIndex = result.<<,
fields = result.<<,
eventOrder = result.<<
eventOrder = result.<<,
mainChain = result.<<
)

val fungibleTokenMetadataGetResult: GetResult[FungibleTokenMetadata] =
Expand Down Expand Up @@ -376,7 +377,8 @@ object CustomGetResult {
destructionTimestamp = result.<<?,
destructionEventOrder = result.<<?,
category = result.<<?,
interfaceId = result.<<?
interfaceId = result.<<?,
mainChain = result.<<
)

implicit val migrationVersionGetResult: GetResult[AppState.MigrationVersion] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,20 @@ object EventSchema extends SchemaMainChain[EventEntity]("events") {
def eventIndex: Rep[Int] = column[Int]("event_index")
def fields: Rep[ArraySeq[Val]] = column[ArraySeq[Val]]("fields")
def eventOrder: Rep[Int] = column[Int]("event_order_in_block")
def mainChain: Rep[Boolean] = column[Boolean]("main_chain")

def * : ProvenShape[EventEntity] =
(blockHash, txHash, contractAddress, inputAddress, timestamp, eventIndex, fields, eventOrder)
(
blockHash,
txHash,
contractAddress,
inputAddress,
timestamp,
eventIndex,
fields,
eventOrder,
mainChain
)
.<>((EventEntity.apply _).tupled, EventEntity.unapply)

def pk: PrimaryKey = primaryKey("events_pk", (blockHash, eventOrder))
Expand Down
26 changes: 15 additions & 11 deletions app/src/test/scala/org/alephium/explorer/GenDBModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ object GenDBModel {
token = token
)

def eventEntityGen()(implicit groupSetting: GroupSetting): Gen[EventEntity] =
def eventEntityGen(
mainChain: Boolean = false
)(implicit groupSetting: GroupSetting): Gen[EventEntity] =
for {
blockHash <- blockHashGen
hash <- transactionHashGen
Expand All @@ -249,16 +251,18 @@ object GenDBModel {
eventIndex <- Gen.posNum[Int]
fields <- Gen.listOf(valGen())

} yield EventEntity.from(
blockHash,
hash,
contractAddress,
inputAddress,
timestamp,
eventIndex,
fields,
0
)
} yield EventEntity
.from(
blockHash,
hash,
contractAddress,
inputAddress,
timestamp,
eventIndex,
fields,
0
)
.copy(mainChain = mainChain)

def tokenOutputEntityGen(
addressGen: Gen[Address] = addressGen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class ContractEntitySpec extends AlephiumSpec {
None,
None,
None,
None
None,
false
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class ContractQueriesSpec
run(ContractSchema.table.result).futureValue.sortBy(_.creationTimestamp) is events
.flatMap(ContractEntity.creationFromEventEntity(_, groupIndex))
.sortBy(_.creationTimestamp)
events.foreach(event =>
run(BlockQueries.updateMainChainStatusQuery(event.blockHash, true)).futureValue
)

// Destruction
val destroyEvents = events.map(e => destroyEventGen(contractAddressFromEvent(e)).sample.get)
Expand All @@ -72,10 +75,13 @@ class ContractQueriesSpec
forAll(createEventsGen()) { case (groupIndex, events) =>
run(ContractSchema.table.delete).futureValue
run(ContractQueries.insertContractCreation(events, groupIndex)).futureValue
events.foreach(event =>
run(BlockQueries.updateMainChainStatusQuery(event.blockHash, true)).futureValue
)

events.flatMap(ContractEntity.creationFromEventEntity(_, groupIndex)).foreach { event =>
run(ContractQueries.getContractEntity(event.contract)).futureValue is
ArraySeq(event)
ArraySeq(event.copy(mainChain = true))
}

run(ContractQueries.getContractEntity(addressGen.sample.get)).futureValue is ArraySeq.empty
Expand All @@ -86,6 +92,9 @@ class ContractQueriesSpec
forAll(createEventsGen()) { case (groupIndex, events) =>
run(ContractSchema.table.delete).futureValue
run(ContractQueries.insertContractCreation(events, groupIndex)).futureValue
events.foreach(event =>
run(BlockQueries.updateMainChainStatusQuery(event.blockHash, true)).futureValue
)

events.flatMap(ContractEntity.creationFromEventEntity(_, groupIndex)).foreach { event =>
run(ContractQueries.getParentAddressQuery(event.contract)).futureValue is
Expand All @@ -107,6 +116,9 @@ class ContractQueriesSpec
run(ContractSchema.table.delete).futureValue
run(ContractQueries.insertContractCreation(events, groupIndex)).futureValue
run(ContractQueries.insertContractCreation(otherEvents, otherGroup)).futureValue
(events ++ otherEvents).foreach(event =>
run(BlockQueries.updateMainChainStatusQuery(event.blockHash, true)).futureValue
)

run(ContractQueries.getSubContractsQuery(parent, pagination)).futureValue is events
.sortBy(_.timestamp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class EventQueriesSpec

"Event Queries" should {
"get event by tx hash" in {
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
insert(events)

events.map { event =>
Expand All @@ -55,7 +55,7 @@ class EventQueriesSpec
}

"get all events with same tx hash" in {
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
val txHash = transactionHashGen.sample.get
val uniqueTxHashEvents = events.zipWithIndex.map { case (event, order) =>
event.copy(txHash = txHash, eventOrder = order)
Expand All @@ -73,7 +73,7 @@ class EventQueriesSpec
}

"get event by contract address" in {
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
insert(events)

events.map { event =>
Expand All @@ -88,7 +88,7 @@ class EventQueriesSpec
}

"get all events with same contractAddress" in {
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
val contractAddress = addressGen.sample.get
val uniqueContractAddressEvents = events.map { event =>
event.copy(contractAddress = contractAddress)
Expand Down Expand Up @@ -121,7 +121,7 @@ class EventQueriesSpec
}

"get event by contract address and input address" in {
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
insert(events)

events.map { event =>
Expand Down Expand Up @@ -153,7 +153,7 @@ class EventQueriesSpec
}

"get all events with same contractAddress and input address" in {
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
val contractAddress = addressGen.sample.get
val inputAddress = addressGen.sample.get
val uniqueContractAddressEvents = events.map { event =>
Expand Down
Loading