-
Notifications
You must be signed in to change notification settings - Fork 15
/
InmemoryUnderlyingLedgerServiceSpec.scala
179 lines (176 loc) · 7.23 KB
/
InmemoryUnderlyingLedgerServiceSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package io.iohk.atala.iris.core.service
import com.google.protobuf.ByteString
import io.iohk.atala.iris.core.model.ledger.TransactionStatus.{InLedger, InMempool}
import io.iohk.atala.iris.core.model.ledger.{Funds, Ledger, TransactionDetails}
import io.iohk.atala.iris.core.service.InmemoryUnderlyingLedgerService.{CardanoBlock, CardanoTransaction}
import io.iohk.atala.iris.core.testutils.RandomUtils.*
import io.iohk.atala.iris.core.testutils.PublishThenAdjust.*
import io.iohk.atala.iris.core.testutils.PublishThenAdjust
import io.iohk.atala.iris.proto.did_operations.{CreateDid, DocumentDefinition, UpdateDid}
import io.iohk.atala.iris.proto.dlt as proto
import zio.*
import zio.test.*
import zio.test.TestAspect.ignore
import zio.test.Assertion.*
object InmemoryUnderlyingLedgerServiceSpec extends ZIOSpecDefault {
val defaultConfig = InmemoryUnderlyingLedgerService.Config(10.seconds, Funds(1000), Funds(1), Ledger.Mainnet)
val inmemoryLedger = InmemoryUnderlyingLedgerService.layer(defaultConfig)
def spec = suite("InmemoryUnderlyingLedgerServiceSpec")(
suite("Background worker")(
test("All the operations in the one block within 4 different transactions") {
val testCase =
for {
op <- ZIO.replicateZIO(4)(genOperation()).map(_.toList)
srvc <- ZIO.service[InmemoryUnderlyingLedgerService]
scenario = List(
Seq(op(0)) >> 1.seconds,
Seq(op(1)) >> 1.seconds,
Seq(op(2)) >> 0.seconds,
Seq(op(3)) >> 20.seconds
)
_ <- PublishThenAdjust.foreachZIO(srvc)(scenario)
mempool <- srvc.getMempool
blocks <- srvc.getBlocks
} yield assertTrue(mempool == List.empty) &&
assertTrue(
blocks.map(_.txs) == List(
List(),
List(
CardanoTransaction(Seq(op(0))),
CardanoTransaction(Seq(op(1))),
CardanoTransaction(Seq(op(2))),
CardanoTransaction(Seq(op(3)))
),
List()
)
)
testCase.provideLayer(inmemoryLedger)
} @@ TestAspect.ignore,
test("Operations distributed between 2 blocks") {
val testCase =
for {
op <- ZIO.replicateZIO(4)(genOperation()).map(_.toList)
srvc <- ZIO.service[InmemoryUnderlyingLedgerService]
scenario = List(
Seq(op(0)) >> 1.seconds,
Seq(op(1)) >> 10.seconds,
Seq(op(2)) >> 0.seconds,
Seq(op(3)) >> 10.seconds
)
_ <- PublishThenAdjust.foreachZIO(srvc)(scenario)
mempool <- srvc.getMempool
blocks <- srvc.getBlocks
} yield assertTrue(mempool == List.empty) &&
assertTrue(
blocks.map(_.txs) == List(
List(),
List(
CardanoTransaction(Seq(op(0))),
CardanoTransaction(Seq(op(1))),
),
List(
CardanoTransaction(Seq(op(2))),
CardanoTransaction(Seq(op(3))),
)
)
)
testCase.provideLayer(inmemoryLedger)
} @@ TestAspect.ignore
),
suite("getTransactionDetails")(
test("Find unconfirmed transaction") {
val testCase =
for {
op <- ZIO.replicateZIO(5)(genOperation()).map(_.toList)
srvc <- ZIO.service[InmemoryUnderlyingLedgerService]
scenario = List(
Seq(op(0)) >> 1.seconds,
Seq(op(1)) >> 10.seconds,
Seq(op(2), op(3)) >> 0.seconds,
Seq(op(4)) >> 2.seconds
)
_ <- PublishThenAdjust.foreachZIO(srvc)(scenario)
targetTx = CardanoTransaction(Seq(op(2), op(3)))
txDetails <- srvc.getTransactionDetails(targetTx.transactionId)
} yield assertTrue(txDetails == TransactionDetails(targetTx.transactionId, InMempool))
testCase.provideLayer(inmemoryLedger)
},
test("Find confirmed transaction") {
val testCase =
for {
op <- ZIO.replicateZIO(5)(genOperation()).map(_.toList)
srvc <- ZIO.service[InmemoryUnderlyingLedgerService]
scenario = List(
Seq(op(0)) >> 11.seconds,
Seq(op(1)) >> 11.seconds,
Seq(op(2), op(3)) >> 0.seconds,
Seq(op(4)) >> 12.seconds
)
_ <- PublishThenAdjust.foreachZIO(srvc)(scenario)
targetTx = CardanoTransaction(Seq(op(2), op(3)))
txDetails <- srvc.getTransactionDetails(targetTx.transactionId)
} yield assertTrue(txDetails == TransactionDetails(targetTx.transactionId, InLedger))
testCase.provideLayer(inmemoryLedger)
},
test("Find unknown transaction") {
val testCase =
for {
op <- ZIO.replicateZIO(5)(genOperation()).map(_.toList)
srvc <- ZIO.service[InmemoryUnderlyingLedgerService]
scenario = List(
Seq(op(0)) >> 11.seconds,
Seq(op(1)) >> 11.seconds,
Seq(op(2), op(3)) >> 0.seconds,
Seq(op(4)) >> 12.seconds
)
_ <- PublishThenAdjust.foreachZIO(srvc)(scenario)
targetTx = CardanoTransaction(Seq(op(1), op(2)))
testResult <- assertZIO(srvc.getTransactionDetails(targetTx.transactionId).exit) {
fails(equalTo(LedgerError(s"Couldn't find tx ${targetTx.transactionId}")))
}
} yield testResult
testCase.provideLayer(inmemoryLedger)
}
),
suite("deleteTransaction")(
test("Delete transaction from mempool") {
val testCase =
for {
op <- ZIO.replicateZIO(5)(genOperation()).map(_.toList)
srvc <- ZIO.service[InmemoryUnderlyingLedgerService]
scenario = List(
Seq(op(0)) >> 1.seconds,
Seq(op(1)) >> 10.seconds,
Seq(op(2), op(3)) >> 0.seconds,
Seq(op(4)) >> 2.seconds
)
_ <- PublishThenAdjust.foreachZIO(srvc)(scenario)
targetTx = CardanoTransaction(Seq(op(2), op(3)))
_ <- srvc.deleteTransaction(targetTx.transactionId)
mempool <- srvc.getMempool
} yield assertTrue(mempool == List(CardanoTransaction(Seq(op(4)))))
testCase.provideLayer(inmemoryLedger)
},
test("Delete confirmed transaction") {
val testCase =
for {
op <- ZIO.replicateZIO(5)(genOperation()).map(_.toList)
srvc <- ZIO.service[InmemoryUnderlyingLedgerService]
scenario = List(
Seq(op(0)) >> 1.seconds,
Seq(op(1)) >> 10.seconds,
Seq(op(2), op(3)) >> 0.seconds,
Seq(op(4)) >> 2.seconds
)
_ <- PublishThenAdjust.foreachZIO(srvc)(scenario)
targetTx = CardanoTransaction(Seq(op(1)))
testResult <-
assertZIO(srvc.deleteTransaction(targetTx.transactionId).exit) {
fails(equalTo(LedgerError(s"Transaction ${targetTx.transactionId} not found in the mempool")))
}
} yield testResult
testCase.provideLayer(inmemoryLedger)
}
)
)
}