Skip to content

Commit 491da0d

Browse files
pslaskibsuieric
authored andcommitted
ETCM-674: added headers consistency with ready blocks validation to fetcher state
1 parent 07bc1c8 commit 491da0d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/main/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherState.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ case class BlockFetcherState(
113113
}
114114

115115
/**
116-
* Validates received headers consistency and their compatibilty with the state
116+
* Validates received headers consistency and their compatibility with the state
117117
* TODO ETCM-370: This needs to be more fine-grained and detailed so blacklisting can be re-enabled
118118
*/
119119
private def validatedHeaders(headers: Seq[BlockHeader]): Either[String, Seq[BlockHeader]] =
@@ -123,11 +123,18 @@ case class BlockFetcherState(
123123
headers
124124
.asRight[String]
125125
.ensure("Given headers should form a sequence without gaps")(HeadersSeq.areChain)
126+
.ensure("Given headers should form a sequence with ready blocks")(checkConsistencyWithReadyBlocks)
126127
.ensure("Given headers do not form a chain with already stored ones")(headers =>
127128
(waitingHeaders.lastOption, headers.headOption).mapN(_ isParentOf _).getOrElse(true)
128129
)
129130
}
130131

132+
private def checkConsistencyWithReadyBlocks(headers: Seq[BlockHeader]): Boolean = {
133+
if (waitingHeaders.isEmpty) {
134+
(readyBlocks.lastOption.map(_.header), headers.headOption).mapN(_ isParentOf _).getOrElse(true)
135+
} else true
136+
}
137+
131138
def validateNewBlockHashes(hashes: Seq[BlockHash]): Either[String, Seq[BlockHash]] =
132139
hashes
133140
.asRight[String]

src/test/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherStateSpec.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ class BlockFetcherStateSpec
6060
}
6161
assert(result.map(_.knownTop) === Right(blocks.last.number))
6262
}
63+
64+
"enqueue requested blocks fails when ready blocks is not forming a sequence with given headers" in {
65+
66+
val result = BlockFetcherState
67+
.initial(importer, validators.blockValidator, 0)
68+
.copy(readyBlocks = Queue(blocks.head))
69+
.appendHeaders(blocks.map(_.header))
70+
.map(_.handleRequestedBlocks(blocks, peer))
71+
72+
assert(result.map(_.waitingHeaders) === Left("Given headers should form a sequence with ready blocks"))
73+
}
6374
}
6475

6576
"trying to insert block into the queues" should {

0 commit comments

Comments
 (0)