Skip to content

Commit

Permalink
Insert events and contract as main_chain = false
Browse files Browse the repository at this point in the history
The same way we do for other entities, the `main_chain = true`
is updated later.
  • Loading branch information
tdroxler committed Jul 4, 2024
1 parent ca8c6ff commit 4572cd3
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ object ContractEntity {
destructionEventOrder = None,
category = None,
interfaceId = None,
mainChain = true
mainChain = false
)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ object EventEntity {
eventIndex,
fields,
order,
mainChain = true
mainChain = false
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ object ContractQueries {
SELECT *
FROM contracts
WHERE contract = $contract
AND main_chain = true
""".asASE[ContractEntity](contractEntityGetResult)
}

Expand All @@ -143,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,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
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 @@ -72,7 +72,7 @@ class ContractEntitySpec extends AlephiumSpec {
None,
None,
None,
true
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

0 comments on commit 4572cd3

Please sign in to comment.