Skip to content

Commit 8b6eeca

Browse files
author
Beksultan
committed
SA-93 Add empty block validation
1 parent f7e511b commit 8b6eeca

File tree

9 files changed

+25
-14
lines changed

9 files changed

+25
-14
lines changed

src/main/kotlin/io/openfuture/chain/core/model/entity/block/payload/MainBlockPayload.kt

+4
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@ class MainBlockPayload(
6161
override fun getBytes(): ByteArray =
6262
transactionMerkleHash.toByteArray() + stateMerkleHash.toByteArray() + receiptMerkleHash.toByteArray()
6363

64+
fun hasTransferTransactions(): Boolean = transferTransactions.isNotEmpty()
65+
66+
fun hasVoteTransactions(): Boolean = voteTransactions.isNotEmpty()
67+
6468
}

src/main/kotlin/io/openfuture/chain/core/service/block/validation/MainBlockValidator.kt

+12-6
Original file line numberDiff line numberDiff line change
@@ -125,37 +125,43 @@ class MainBlockValidator(
125125

126126
fun checkReceiptsAndStates(): BlockValidateHandler = { block, _, _, new ->
127127
block as MainBlock
128-
val blockStates = block.getPayload().delegateStates + block.getPayload().accountStates
128+
val payload = block.getPayload()
129+
val blockStates = payload.delegateStates + payload.accountStates
129130

130131
blockStates.forEach {
131132
if (!stateManager.verify(it)) {
132133
throw ValidationException("Invalid block states in block: height #${block.height}, hash ${block.hash}")
133134
}
134135
}
135136

136-
block.getPayload().receipts.forEach {
137+
payload.receipts.forEach {
137138
if (!receiptService.verify(it)) {
138139
throw ValidationException("Invalid block receipts in block: height #${block.height}, hash ${block.hash}")
139140
}
140141
}
141142

142143
if (new) {
143144
val delegateWallet = stateManager.getByAddress<DelegateState>(block.publicKey).walletAddress
144-
val transactions = block.getPayload().delegateTransactions + block.getPayload().transferTransactions +
145-
block.getPayload().voteTransactions + block.getPayload().rewardTransactions
145+
val transactions = payload.delegateTransactions + payload.transferTransactions +
146+
payload.voteTransactions + payload.rewardTransactions
146147
val receipts = transactionManager.processTransactions(transactions, delegateWallet)
147148
val states = statePool.getStates()
148149

149-
if (block.getPayload().receipts.size != receipts.size) {
150+
if (payload.receipts.size != receipts.size) {
150151
throw ValidationException("Invalid count block receipts in block: height #${block.height}, hash ${block.hash}")
151152
}
152153

154+
if (!consensusProperties.emptyBlockProductionEnabled!! && !payload.hasTransferTransactions() && !payload.hasVoteTransactions()) {
155+
throw ValidationException("Block won't be created due to allow to create empty blocks: ${consensusProperties.emptyBlockProductionEnabled}" +
156+
" has transfer transactions: ${payload.hasTransferTransactions()} has vote transactions: ${payload.hasVoteTransactions()}")
157+
}
158+
153159
if (blockStates.size != states.size) {
154160
throw ValidationException("Invalid count block states in block: height #${block.height}, hash ${block.hash}")
155161
}
156162

157163
receipts.forEach { r ->
158-
block.getPayload().receipts.firstOrNull { it.hash == r.hash }
164+
payload.receipts.firstOrNull { it.hash == r.hash }
159165
?: throw ValidationException("Invalid block receipts in block: height #${block.height}, hash ${block.hash}")
160166
}
161167

src/main/kotlin/io/openfuture/chain/network/entity/NodeInfo.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ data class NodeInfo(
1414

1515
override fun read(buf: ByteBuf) {
1616
uid = buf.readString()
17-
address = NetworkAddress::class.java.newInstance()
17+
address = NetworkAddress::class.java.getDeclaredConstructor().newInstance()
1818
address.read(buf)
1919
}
2020

src/main/kotlin/io/openfuture/chain/network/extension/ByteBufExtension.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ inline fun <reified T : Message> ByteBuf.readList(): MutableList<T> {
3939
val size = this.readInt()
4040
val list = mutableListOf<T>()
4141
for (index in 1..size) {
42-
val instance = T::class.java.newInstance()
42+
val instance = T::class.java.getDeclaredConstructor().newInstance()
4343
instance.read(this)
4444
list.add(instance)
4545
}
@@ -55,7 +55,7 @@ inline fun <reified T : Message> ByteBuf.readSet(): MutableSet<T> {
5555
val size = this.readInt()
5656
val set = mutableSetOf<T>()
5757
for (index in 1..size) {
58-
val instance = T::class.java.newInstance()
58+
val instance = T::class.java.getDeclaredConstructor().newInstance()
5959
instance.read(this)
6060
set.add(instance)
6161
}

src/main/kotlin/io/openfuture/chain/network/handler/network/initializer/ClientChannelInitializer.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ class ClientChannelInitializer(
6565
epochResponseHandler,
6666
mainBlockHandler,
6767
genesisBlockHandler,
68+
69+
blockAvailabilityRequestHandler,
6870
blockAvailabilityResponseHandler,
6971
syncStatusHandler,
70-
blockAvailabilityRequestHandler,
7172
// core
7273
transferTransactionHandler,
7374
delegateTransactionHandler,

src/main/kotlin/io/openfuture/chain/network/handler/network/initializer/ServerChannelInitializer.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ class ServerChannelInitializer(
6565
epochResponseHandler,
6666
mainBlockHandler,
6767
genesisBlockHandler,
68+
blockAvailabilityRequestHandler,
6869
blockAvailabilityResponseHandler,
6970
syncStatusHandler,
70-
blockAvailabilityRequestHandler,
7171
// core
7272
transferTransactionHandler,
7373
delegateTransactionHandler,

src/main/kotlin/io/openfuture/chain/network/message/consensus/BlockAvailabilityResponse.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class BlockAvailabilityResponse(
1818
hash = buf.readString()
1919
height = buf.readLong()
2020
if (-1L != height) {
21-
val block = GenesisBlockMessage::class.java.newInstance()
21+
val block = GenesisBlockMessage::class.java.getDeclaredConstructor().newInstance()
2222
block.read(buf)
2323
genesisBlock = block
2424
}

src/main/kotlin/io/openfuture/chain/network/message/network/NewClient.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ data class NewClient(
1111
) : Message {
1212

1313
override fun read(buf: ByteBuf) {
14-
nodeInfo = NodeInfo::class.java.newInstance()
14+
nodeInfo = NodeInfo::class.java.getDeclaredConstructor().newInstance()
1515
nodeInfo.read(buf)
1616
}
1717

src/main/kotlin/io/openfuture/chain/smartcontract/component/SmartContractInjector.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object SmartContractInjector {
1010

1111

1212
fun initSmartContract(clazz: Class<*>, owner: String, address: String): SmartContract {
13-
val instance = clazz.newInstance() as SmartContract
13+
val instance = clazz.getDeclaredConstructor().newInstance() as SmartContract
1414

1515
injectField(instance, OWNER_FIELD, owner)
1616
injectField(instance, ADDRESS_FIELD, address)

0 commit comments

Comments
 (0)