Skip to content

Commit 4572cd3

Browse files
committed
Insert events and contract as main_chain = false
The same way we do for other entities, the `main_chain = true` is updated later.
1 parent ca8c6ff commit 4572cd3

File tree

8 files changed

+41
-22
lines changed

8 files changed

+41
-22
lines changed

app/src/main/scala/org/alephium/explorer/persistence/model/ContractEntity.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ object ContractEntity {
110110
destructionEventOrder = None,
111111
category = None,
112112
interfaceId = None,
113-
mainChain = true
113+
mainChain = false
114114
)
115115
}
116116
} else {

app/src/main/scala/org/alephium/explorer/persistence/model/EventEntity.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ object EventEntity {
6767
eventIndex,
6868
fields,
6969
order,
70-
mainChain = true
70+
mainChain = false
7171
)
7272
}
7373
}

app/src/main/scala/org/alephium/explorer/persistence/queries/ContractQueries.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ object ContractQueries {
133133
SELECT *
134134
FROM contracts
135135
WHERE contract = $contract
136+
AND main_chain = true
136137
""".asASE[ContractEntity](contractEntityGetResult)
137138
}
138139

@@ -143,6 +144,7 @@ object ContractQueries {
143144
SELECT parent
144145
FROM contracts
145146
WHERE contract = $contract
147+
AND main_chain = true
146148
LIMIT 1
147149
""".asASE[Option[Address]](optionAddressGetResult).headOrNone.map(_.flatten)
148150
}

app/src/main/scala/org/alephium/explorer/persistence/schema/CustomGetResult.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ object CustomGetResult {
377377
destructionTimestamp = result.<<?,
378378
destructionEventOrder = result.<<?,
379379
category = result.<<?,
380-
interfaceId = result.<<?
380+
interfaceId = result.<<?,
381+
mainChain = result.<<
381382
)
382383

383384
implicit val migrationVersionGetResult: GetResult[AppState.MigrationVersion] =

app/src/test/scala/org/alephium/explorer/GenDBModel.scala

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ object GenDBModel {
239239
token = token
240240
)
241241

242-
def eventEntityGen()(implicit groupSetting: GroupSetting): Gen[EventEntity] =
242+
def eventEntityGen(
243+
mainChain: Boolean = false
244+
)(implicit groupSetting: GroupSetting): Gen[EventEntity] =
243245
for {
244246
blockHash <- blockHashGen
245247
hash <- transactionHashGen
@@ -249,16 +251,18 @@ object GenDBModel {
249251
eventIndex <- Gen.posNum[Int]
250252
fields <- Gen.listOf(valGen())
251253

252-
} yield EventEntity.from(
253-
blockHash,
254-
hash,
255-
contractAddress,
256-
inputAddress,
257-
timestamp,
258-
eventIndex,
259-
fields,
260-
0
261-
)
254+
} yield EventEntity
255+
.from(
256+
blockHash,
257+
hash,
258+
contractAddress,
259+
inputAddress,
260+
timestamp,
261+
eventIndex,
262+
fields,
263+
0
264+
)
265+
.copy(mainChain = mainChain)
262266

263267
def tokenOutputEntityGen(
264268
addressGen: Gen[Address] = addressGen,

app/src/test/scala/org/alephium/explorer/persistence/model/ContractEntitySpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ContractEntitySpec extends AlephiumSpec {
7272
None,
7373
None,
7474
None,
75-
true
75+
false
7676
)
7777
)
7878
}

app/src/test/scala/org/alephium/explorer/persistence/queries/ContractQueriesSpec.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class ContractQueriesSpec
5555
run(ContractSchema.table.result).futureValue.sortBy(_.creationTimestamp) is events
5656
.flatMap(ContractEntity.creationFromEventEntity(_, groupIndex))
5757
.sortBy(_.creationTimestamp)
58+
events.foreach(event =>
59+
run(BlockQueries.updateMainChainStatusQuery(event.blockHash, true)).futureValue
60+
)
5861

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

7682
events.flatMap(ContractEntity.creationFromEventEntity(_, groupIndex)).foreach { event =>
7783
run(ContractQueries.getContractEntity(event.contract)).futureValue is
78-
ArraySeq(event)
84+
ArraySeq(event.copy(mainChain = true))
7985
}
8086

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

9099
events.flatMap(ContractEntity.creationFromEventEntity(_, groupIndex)).foreach { event =>
91100
run(ContractQueries.getParentAddressQuery(event.contract)).futureValue is
@@ -107,6 +116,9 @@ class ContractQueriesSpec
107116
run(ContractSchema.table.delete).futureValue
108117
run(ContractQueries.insertContractCreation(events, groupIndex)).futureValue
109118
run(ContractQueries.insertContractCreation(otherEvents, otherGroup)).futureValue
119+
(events ++ otherEvents).foreach(event =>
120+
run(BlockQueries.updateMainChainStatusQuery(event.blockHash, true)).futureValue
121+
)
110122

111123
run(ContractQueries.getSubContractsQuery(parent, pagination)).futureValue is events
112124
.sortBy(_.timestamp)

app/src/test/scala/org/alephium/explorer/persistence/queries/EventQueriesSpec.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class EventQueriesSpec
4343

4444
"Event Queries" should {
4545
"get event by tx hash" in {
46-
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
46+
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
4747
insert(events)
4848

4949
events.map { event =>
@@ -55,7 +55,7 @@ class EventQueriesSpec
5555
}
5656

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

7575
"get event by contract address" in {
76-
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
76+
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
7777
insert(events)
7878

7979
events.map { event =>
@@ -88,7 +88,7 @@ class EventQueriesSpec
8888
}
8989

9090
"get all events with same contractAddress" in {
91-
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
91+
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
9292
val contractAddress = addressGen.sample.get
9393
val uniqueContractAddressEvents = events.map { event =>
9494
event.copy(contractAddress = contractAddress)
@@ -121,7 +121,7 @@ class EventQueriesSpec
121121
}
122122

123123
"get event by contract address and input address" in {
124-
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
124+
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
125125
insert(events)
126126

127127
events.map { event =>
@@ -153,7 +153,7 @@ class EventQueriesSpec
153153
}
154154

155155
"get all events with same contractAddress and input address" in {
156-
forAll(Gen.nonEmptyListOf(eventEntityGen())) { events =>
156+
forAll(Gen.nonEmptyListOf(eventEntityGen(true))) { events =>
157157
val contractAddress = addressGen.sample.get
158158
val inputAddress = addressGen.sample.get
159159
val uniqueContractAddressEvents = events.map { event =>

0 commit comments

Comments
 (0)