Repository navigation
Expand file tree
/
Copy pathLynxChainTest.kt
More file actions
232 lines (201 loc) · 7.29 KB
/
Copy pathLynxChainTest.kt
File metadata and controls
232 lines (201 loc) · 7.29 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.willwinder.lynx
import com.willwinder.lynx.LynxChain.Companion.REWARD
import com.willwinder.lynx.Transaction.Companion.basicCoinbase
import com.willwinder.lynx.Transaction.Companion.buildTransactionList
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import java.security.PublicKey
internal class LynxChainTest {
companion object {
val key1 = generateKeyPair()
val key2 = generateKeyPair()
val genesisBlock = LynxBlock("0",
listOf(
Transaction(
listOf(),
listOf(Output(0, REWARD, key1.public)))))
}
@Test
fun validGenesisBlock() {
val chain = LynxChain(genesisBlock)
Assertions.assertThat(chain.height).isEqualTo(1)
}
/**
* Test a valid chain of transactions.
*/
@Test
fun chainOfTransactions() {
val chain = generateChainWithRewardsFor(key1.public, 1)
val transaction1 = buildTransactionList(
chain,
listOf(Pair(1L, key1.private)),
listOf(Pair(50.0, key1.public), Pair(50.0, key1.public)),
key1.public)
chain.add(transaction1)
val transaction2 = buildTransactionList(
chain,
listOf(Pair(chain.lastId, key1.private)),
listOf(
Pair(10.0, key1.public),
Pair(10.0, key1.public),
Pair(10.0, key1.public),
Pair(10.0, key1.public),
Pair(10.0, key1.public)),
key1.public)
chain.add(transaction2)
val transaction3 = buildTransactionList(
chain,
listOf(Pair(chain.lastId, key1.private)),
listOf(
Pair(5.0, key1.public),
Pair(5.0, key1.public)),
key1.public)
chain.add(transaction3)
Assertions.assertThat(chain.height).isEqualTo(5)
}
/**
* Error when a coinbase larger than REWARD is specified.
*/
@Test
fun largeCoinbaseError() {
val badTransaction = Transaction(
listOf(),
listOf(Output(0, REWARD + 1, key1.public)))
Assertions.assertThatThrownBy { LynxChain(LynxBlock("0", listOf(badTransaction))) }
.isExactlyInstanceOf(CoinbaseRewardTooLarge::class.java)
.hasFieldOrPropertyWithValue("rewardSize", 101.0)
}
/**
* Generate some transactions, then use them with some invalid transaction IDs.
* Make sure only the invalid ones are reported.
*/
@Test
fun nonIncreasingTxId() {
val chain = generateChainWithRewardsFor(key1.public, 5)
Assertions.assertThatThrownBy {
chain.add(LynxBlock(chain.last.getHash(), listOf(
Transaction(listOf(Input(1, chain.getSignature(1, key1.private))), listOf(Output(3, REWARD, key1.public))),
Transaction(listOf(Input(2, chain.getSignature(2, key1.private))), listOf(Output(4, REWARD, key1.public))),
Transaction(listOf(Input(3, chain.getSignature(3, key1.private))), listOf(Output(5, REWARD, key1.public))),
Transaction(listOf(Input(4, chain.getSignature(4, key1.private))), listOf(Output(6, REWARD, key1.public)))
))) }
.isExactlyInstanceOf(InvalidTransactionIds::class.java)
.hasFieldOrPropertyWithValue("transactionIds", listOf(3L, 4L, 5L))
}
/**
* Simple valid transaction with one input and one output.
*/
@Test
fun validOneToOneTransactionNoChange() {
val chain = generateChainWithRewardsFor(key1.public, 1)
chain.add(LynxBlock(chain.last.getHash(), listOf(
Transaction(listOf(Input(1, chain.getSignature(1, key1.private))), listOf(Output(6, REWARD, key1.public)))
)))
Assertions.assertThat(chain.height).isEqualTo(3)
}
/**
* Error when the same id is used in multiple transactions in the same block.
*/
@Test
fun doubleSpendTransaction() {
val chain = generateChainWithRewardsFor(key1.public, 1)
val coinbase = basicCoinbase(key1.public, 2)
val transaction = Transaction(
listOf(Input(1, chain.getSignature(1, key1.private))),
listOf(Output(3, REWARD, key1.public)))
Assertions.assertThatThrownBy { chain.add(listOf(coinbase, transaction, transaction)) }
.isExactlyInstanceOf(TransactionDoubleSpend::class.java)
.hasFieldOrPropertyWithValue("transactionIds", listOf(1L))
}
/**
* Error when attempting to spend a previously spent id.
*/
@Test
fun previouslySpentTransaction() {
val chain = generateChainWithRewardsFor(key1.public, 1)
val transactions = buildTransactionList(
chain,
listOf(Pair(1L, key1.private)),
listOf(Pair(REWARD, key1.public)),
key1.public)
// Spend id 1.
chain.add(transactions)
// Attempt to spend it again.
Assertions.assertThatThrownBy { chain.add(transactions) }
.isExactlyInstanceOf(PreviouslySpentTransactions::class.java)
.hasFieldOrPropertyWithValue("transactionIds", listOf(1L))
}
/**
* Error when the sum of inputs is less than the sum of outputs.
*/
@Test
fun inputLessThanOutputError() {
val chain = generateChainWithRewardsFor(key1.public, 5)
val transactions = buildTransactionList(
chain,
listOf(Pair(1L, key1.private)),
listOf(Pair(REWARD + 1, key1.public)),
key1.public)
Assertions.assertThatThrownBy { chain.add(transactions) }
.isExactlyInstanceOf(InsufficientInputValue::class.java)
.hasFieldOrProperty("transactions")
}
/**
* Try some different combinations of multiple inputs/outputs.
* Using 2x REWARD transactions and 2x values.
* Note: Leftover money in the ouputs is not an error.
*/
@ParameterizedTest
@CsvSource(
"1.0,1.0,true",
"199.0,1.0,true",
"199.9,0.1,true",
"200.0,1.0,false",
"1.0,200.0,false"
)
fun multipleInputsAndOutputs(d1: Double, d2: Double, isGood: Boolean) {
val chain = generateChainWithRewardsFor(key1.public, 5)
val transactions = buildTransactionList(
chain,
listOf(Pair(1L, key1.private), Pair(2L, key1.private)),
listOf(Pair(d1, key1.public), Pair(d2, key1.public)),
key1.public)
when(isGood) {
true -> chain.add(transactions)
false -> {
Assertions.assertThatThrownBy { chain.add(transactions) }
.isExactlyInstanceOf(InsufficientInputValue::class.java)
.hasFieldOrProperty("transactions")
}
}
}
@Test
fun badSignature() {
val chain = generateChainWithRewardsFor(key1.public, 3)
val coinbase = Transaction.basicCoinbase(key1.public,4)
val badSrc1 = Input(1, "Bad Signature")
val badSrc2 = Input(3, "Bad Signature")
val transaction = Transaction(
listOf(
badSrc1,
Input(2, chain.getSignature(2, key1.private)),
badSrc2),
listOf(Output(5, REWARD, key1.public)))
Assertions.assertThatThrownBy { chain.add(listOf(coinbase, transaction)) }
.isExactlyInstanceOf(BadSignatures::class.java)
.hasFieldOrPropertyWithValue("inputs", listOf(badSrc1, badSrc2))
}
/**
* Generate some transactions to add currency to key
*/
private fun generateChainWithRewardsFor(key: PublicKey, count: Long = 5, firstTx: Long = 1) : LynxChain {
val chain = LynxChain(genesisBlock)
for (i in 0 until count) {
chain.add(
LynxBlock(chain.last.getHash(), listOf(basicCoinbase(key, firstTx + i))))
}
return chain
}
}