Skip to content

Commit 44762a8

Browse files
author
Aurélien Richez
committed
move isInChain into BlockchainBranch
1 parent 985804f commit 44762a8

File tree

8 files changed

+21
-18
lines changed

8 files changed

+21
-18
lines changed

src/it/scala/io/iohk/ethereum/txExecTest/util/DumpChainApp.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ class BlockchainMock(genesisHash: ByteString) extends Blockchain {
208208

209209
override def getLatestCheckpointBlockNumber(): BigInt = ???
210210

211-
override def isInChain(hash: NodeHash): Boolean = ???
212-
213211
override def getBackingMptStorage(blockNumber: BigInt): MptStorage = ???
214212

215213
override def getReadOnlyMptStorage(): MptStorage = ???

src/main/scala/io/iohk/ethereum/domain/Blockchain.scala

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ trait Blockchain {
7878

7979
def saveBestKnownBlocks(bestBlockNumber: BigInt, latestCheckpointNumber: Option[BigInt] = None): Unit
8080

81-
/** Strict check if given block hash is in chain
82-
* Using any of getXXXByHash is not always accurate - after restart the best block is often lower than before restart
83-
* The result of that is returning data of blocks which we don't consider as a part of the chain anymore
84-
* @param hash block hash
85-
*/
86-
def isInChain(hash: ByteString): Boolean
8781
}
8882

8983
class BlockchainImpl(
@@ -100,12 +94,6 @@ class BlockchainImpl(
10094
) extends Blockchain
10195
with Logger {
10296

103-
override def isInChain(hash: ByteString): Boolean =
104-
(for {
105-
header <- blockchainReader.getBlockHeaderByHash(hash) if header.number <= blockchainReader.getBestBlockNumber()
106-
hash <- blockchainReader.getBestBranch().getHashByBlockNumber(header.number)
107-
} yield header.hash == hash).getOrElse(false)
108-
10997
override def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight] = chainWeightStorage.get(blockhash)
11098

11199
override def getLatestCheckpointBlockNumber(): BigInt =

src/main/scala/io/iohk/ethereum/domain/branch/BestBlockchainBranch.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ class BestBlockchainBranch(
3232
if (tipBlockHeader.number >= number && number >= 0) {
3333
bestChainBlockNumberMappingStorage.get(number)
3434
} else None
35+
36+
override def isInChain(hash: ByteString): Boolean =
37+
(for {
38+
header <- blockchainReader.getBlockHeaderByHash(hash) if header.number <= tipBlockHeader.number
39+
hash <- getHashByBlockNumber(header.number)
40+
} yield header.hash == hash).getOrElse(false)
3541
}

src/main/scala/io/iohk/ethereum/domain/branch/BlockchainBranch.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ trait BlockchainBranch {
1212
/** Returns a block hash for the block at the given height if any */
1313
def getHashByBlockNumber(number: BigInt): Option[ByteString]
1414

15+
/** Checks if given block hash is in this chain. (i.e. is an ancestor of the tip block) */
16+
def isInChain(hash: ByteString): Boolean
1517
}

src/main/scala/io/iohk/ethereum/domain/branch/EmptyBlockchainBranch.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ object EmptyBlockchainBranch extends BlockchainBranch {
88
override def getBlockByNumber(number: BigInt): Option[Block] = None
99

1010
override def getHashByBlockNumber(number: BigInt): Option[ByteString] = None
11+
12+
override def isInChain(hash: ByteString): Boolean = false
1113
}

src/main/scala/io/iohk/ethereum/ledger/BranchResolution.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class BranchResolution(blockchain: Blockchain, blockchainReader: BlockchainReade
1515
if (!doHeadersFormChain(headers)) {
1616
InvalidBranch
1717
} else {
18-
val knownParentOrGenesis = blockchain
18+
val knownParentOrGenesis = blockchainReader
19+
.getBestBranch()
1920
.isInChain(headers.head.parentHash) || headers.head.hash == blockchainReader.genesisHeader.hash
2021

2122
if (!knownParentOrGenesis)

src/test/scala/io/iohk/ethereum/domain/BlockchainSpec.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
5252

5353
it should "be able to do strict check of block existence in the chain" in new EphemBlockchainTestSetup {
5454
val validBlock = Fixtures.Blocks.ValidBlock.block
55+
blockchainWriter.save(
56+
validBlock.copy(header = validBlock.header.copy(number = validBlock.number - 1)),
57+
Seq.empty,
58+
ChainWeight(100, 100),
59+
saveAsBestBlock = true
60+
)
5561
blockchainWriter.save(validBlock, Seq.empty, ChainWeight(100, 100), saveAsBestBlock = true)
56-
blockchain.isInChain(validBlock.hash) === false
62+
blockchainReader.getBestBranch().isInChain(validBlock.hash) should ===(true)
5763
// simulation of node restart
5864
blockchain.saveBestKnownBlocks(validBlock.header.number - 1)
59-
blockchain.isInChain(validBlock.hash) should ===(false)
65+
blockchainReader.getBestBranch().isInChain(validBlock.hash) should ===(false)
6066
}
6167

6268
it should "be able to query a stored blockHeader by it's number" in new EphemBlockchainTestSetup {

src/test/scala/io/iohk/ethereum/ledger/LedgerTestSetup.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ trait MockBlockchain extends MockFactory { self: TestSetupWithVmAndValidators =>
456456
.once()
457457

458458
def setHeaderInChain(hash: ByteString, result: Boolean = true): CallHandler1[ByteString, Boolean] =
459-
(blockchain.isInChain _).expects(hash).returning(result)
459+
(bestChain.isInChain _).expects(hash).returning(result)
460460

461461
def setBlockByNumber(number: BigInt, block: Option[Block]): CallHandler1[BigInt, Option[Block]] =
462462
(bestChain.getBlockByNumber _).expects(number).returning(block)

0 commit comments

Comments
 (0)