Skip to content

Commit e1d95ff

Browse files
committed
ETCM-389 Move checkpoint creation to the checkpoint service
1 parent c5fbc60 commit e1d95ff

File tree

14 files changed

+99
-102
lines changed

14 files changed

+99
-102
lines changed

src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ class BlockImporterItSpec
9898
ommersPoolProbe.ref,
9999
broadcasterProbe.ref,
100100
pendingTransactionsManagerProbe.ref,
101-
checkpointBlockGenerator,
102101
supervisor.ref
103102
)
104103
)
@@ -143,7 +142,6 @@ class BlockImporterItSpec
143142
ommersPoolProbe.ref,
144143
broadcasterProbe.ref,
145144
pendingTransactionsManagerProbe.ref,
146-
checkpointBlockGenerator,
147145
supervisor.ref
148146
)
149147
)
@@ -234,9 +232,8 @@ class BlockImporterItSpec
234232
Seq(crypto.generateKeyPair(secureRandom)),
235233
newBlock5.hash
236234
)
237-
blockImporter ! NewCheckpoint(newBlock5.hash, signatures)
238-
239235
val checkpointBlock = checkpointBlockGenerator.generate(newBlock5, Checkpoint(signatures))
236+
blockImporter ! NewCheckpoint(checkpointBlock)
240237

241238
eventually { Thread.sleep(1000); blockchain.getBestBlock().get shouldEqual checkpointBlock }
242239
eventually {

src/it/scala/io/iohk/ethereum/sync/util/RegularSyncItSpecUtils.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ object RegularSyncItSpecUtils {
9595
ommersPool,
9696
broadcasterRef,
9797
pendingTransactionsManager,
98-
checkpointBlockGenerator,
9998
regularSync
10099
))
101100

@@ -110,7 +109,6 @@ object RegularSyncItSpecUtils {
110109
testSyncConfig,
111110
ommersPool,
112111
pendingTransactionsManager,
113-
checkpointBlockGenerator,
114112
system.scheduler
115113
)
116114
)
@@ -170,7 +168,8 @@ object RegularSyncItSpecUtils {
170168
Seq(crypto.generateKeyPair(secureRandom)),
171169
parent.hash
172170
)
173-
regularSync ! NewCheckpoint(parent.header.hash, signatures)
171+
val checkpoint = checkpointBlockGenerator.generate(parent, Checkpoint(signatures))
172+
regularSync ! NewCheckpoint(checkpoint)
174173
}
175174

176175
def getCheckpointFromPeer(checkpoint: Block, peerId: PeerId): Task[Unit] = Task {

src/main/scala/io/iohk/ethereum/blockchain/sync/SyncController.scala

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package io.iohk.ethereum.blockchain.sync
33
import akka.actor.{Actor, ActorLogging, ActorRef, PoisonPill, Props, Scheduler}
44
import io.iohk.ethereum.blockchain.sync.fast.FastSync
55
import io.iohk.ethereum.blockchain.sync.regular.RegularSync
6-
import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator
76
import io.iohk.ethereum.consensus.validators.Validators
87
import io.iohk.ethereum.db.storage.{AppStateStorage, FastSyncStateStorage}
98
import io.iohk.ethereum.domain.Blockchain
@@ -18,7 +17,6 @@ class SyncController(
1817
validators: Validators,
1918
peerEventBus: ActorRef,
2019
pendingTransactionsManager: ActorRef,
21-
checkpointBlockGenerator: CheckpointBlockGenerator,
2220
ommersPool: ActorRef,
2321
etcPeerManager: ActorRef,
2422
blacklist: Blacklist,
@@ -106,7 +104,6 @@ class SyncController(
106104
syncConfig,
107105
ommersPool,
108106
pendingTransactionsManager,
109-
checkpointBlockGenerator,
110107
scheduler
111108
),
112109
"regular-sync"
@@ -127,7 +124,6 @@ object SyncController {
127124
validators: Validators,
128125
peerEventBus: ActorRef,
129126
pendingTransactionsManager: ActorRef,
130-
checkpointBlockGenerator: CheckpointBlockGenerator,
131127
ommersPool: ActorRef,
132128
etcPeerManager: ActorRef,
133129
blacklist: Blacklist,
@@ -142,7 +138,6 @@ object SyncController {
142138
validators,
143139
peerEventBus,
144140
pendingTransactionsManager,
145-
checkpointBlockGenerator,
146141
ommersPool,
147142
etcPeerManager,
148143
blacklist,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ class BlockFetcher(
268268
fetchBlocks(newState)
269269
case Left(_) if block.number <= state.knownTop =>
270270
log.debug(
271-
s"Checkpoint block ${ByteStringUtils.hash2string(blockHash)} not fit into queues - clearing the queues and setting possible new top"
271+
s"Checkpoint block ${ByteStringUtils.hash2string(blockHash)} not fit into queues - clearing the queues and setting new top"
272272
)
273273
val newState = state
274274
.clearQueues()

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

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@ package io.iohk.ethereum.blockchain.sync.regular
22

33
import akka.actor.Actor.Receive
44
import akka.actor.{Actor, ActorLogging, ActorRef, NotInfluenceReceiveTimeout, Props, ReceiveTimeout}
5-
import akka.util.ByteString
65
import cats.data.NonEmptyList
76
import cats.implicits._
87
import io.iohk.ethereum.blockchain.sync.regular.BlockBroadcast.BlockToBroadcast
98
import io.iohk.ethereum.blockchain.sync.regular.BlockBroadcasterActor.BroadcastBlocks
109
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.ProgressProtocol
11-
import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator
12-
import io.iohk.ethereum.crypto.{ECDSASignature, kec256}
10+
import io.iohk.ethereum.crypto.kec256
1311
import io.iohk.ethereum.domain._
1412
import io.iohk.ethereum.ledger._
1513
import io.iohk.ethereum.mpt.MerklePatriciaTrie.MissingNodeException
1614
import io.iohk.ethereum.network.PeerId
1715
import io.iohk.ethereum.ommers.OmmersPool.AddOmmers
1816
import io.iohk.ethereum.transactions.PendingTransactionsManager
1917
import io.iohk.ethereum.transactions.PendingTransactionsManager.{AddUncheckedTransactions, RemoveTransactions}
20-
import io.iohk.ethereum.utils.ByteStringUtils
2118
import io.iohk.ethereum.utils.Config.SyncConfig
2219
import io.iohk.ethereum.utils.FunctorOps._
2320
import monix.eval.Task
@@ -34,7 +31,6 @@ class BlockImporter(
3431
ommersPool: ActorRef,
3532
broadcaster: ActorRef,
3633
pendingTransactionsManager: ActorRef,
37-
checkpointBlockGenerator: CheckpointBlockGenerator,
3834
supervisor: ActorRef
3935
) extends Actor
4036
with ActorLogging {
@@ -69,39 +65,44 @@ class BlockImporter(
6965
SignedTransaction.retrieveSendersInBackGround(blocks.toList.map(_.body))
7066
importBlocks(blocks, DefaultBlockImport)(state)
7167

72-
//TODO ETCM-389: Handle mined, checkpoint and new blocks uniformly
73-
case MinedBlock(block) =>
74-
if (!state.importing) {
75-
importMinedBlock(block, state)
76-
}
68+
case MinedBlock(block) if !state.importing =>
69+
importBlock(
70+
block,
71+
new MinedBlockImportMessages(block),
72+
MinedBlockImport,
73+
false,
74+
true)(state)
7775

78-
//TODO ETCM-389: Handle mined, checkpoint and new blocks uniformly
79-
case nc @ NewCheckpoint(parentHash, signatures) =>
76+
case nc @ NewCheckpoint(block) =>
8077
if (state.importing) {
8178
//We don't want to lose a checkpoint
8279
context.system.scheduler.scheduleOnce(1.second, self, nc)
8380
} else {
84-
ledger.getBlockByHash(parentHash) match {
85-
case Some(parent) =>
86-
val checkpointBlock = checkpointBlockGenerator.generate(parent, Checkpoint(signatures))
87-
importCheckpointBlock(checkpointBlock, state)
88-
89-
case None =>
90-
log.error(s"Could not find parent (${ByteStringUtils.hash2string(parentHash)}) for new checkpoint block")
91-
}
81+
importBlock(
82+
block,
83+
new CheckpointBlockImportMessages(block),
84+
CheckpointBlockImport,
85+
false,
86+
true)(state)
9287
}
9388

94-
case ImportNewBlock(block, peerId) if state.isOnTop && !state.importing => importNewBlock(block, peerId, state)
89+
case ImportNewBlock(block, peerId) if state.isOnTop && !state.importing =>
90+
importBlock(
91+
block,
92+
new NewBlockImportMessages(block, peerId),
93+
NewBlockImport,
94+
informFetcherOnFail = true,
95+
internally = false
96+
)(state)
9597

9698
case ImportDone(newBehavior, importType) =>
9799
val newState = state.notImportingBlocks().branchResolved()
98100
val behavior: Behavior = getBehavior(newBehavior, importType)
99-
100101
if (newBehavior == Running) {
101102
self ! PickBlocks
102103
}
103-
104104
context become behavior(newState)
105+
105106
case PickBlocks if !state.importing => pickBlocks(state)
106107
}
107108

@@ -214,33 +215,6 @@ class BlockImporter(
214215
}
215216
}
216217

217-
private def importMinedBlock(block: Block, state: ImporterState): Unit =
218-
importBlock(
219-
block,
220-
new MinedBlockImportMessages(block),
221-
MinedBlockImport,
222-
informFetcherOnFail = false,
223-
internally = true
224-
)(state)
225-
226-
private def importCheckpointBlock(block: Block, state: ImporterState): Unit =
227-
importBlock(
228-
block,
229-
new CheckpointBlockImportMessages(block),
230-
CheckpointBlockImport,
231-
informFetcherOnFail = false,
232-
internally = true
233-
)(state)
234-
235-
private def importNewBlock(block: Block, peerId: PeerId, state: ImporterState): Unit =
236-
importBlock(
237-
block,
238-
new NewBlockImportMessages(block, peerId),
239-
NewBlockImport,
240-
informFetcherOnFail = true,
241-
internally = false
242-
)(state)
243-
244218
private def importBlock(
245219
block: Block,
246220
importMessages: ImportMessages,
@@ -363,7 +337,6 @@ object BlockImporter {
363337
ommersPool: ActorRef,
364338
broadcaster: ActorRef,
365339
pendingTransactionsManager: ActorRef,
366-
checkpointBlockGenerator: CheckpointBlockGenerator,
367340
supervisor: ActorRef
368341
): Props =
369342
Props(
@@ -375,7 +348,6 @@ object BlockImporter {
375348
ommersPool,
376349
broadcaster,
377350
pendingTransactionsManager,
378-
checkpointBlockGenerator,
379351
supervisor
380352
)
381353
)
@@ -388,7 +360,7 @@ object BlockImporter {
388360
case object OnTop extends ImporterMsg
389361
case object NotOnTop extends ImporterMsg
390362
case class MinedBlock(block: Block) extends ImporterMsg
391-
case class NewCheckpoint(parentHash: ByteString, signatures: Seq[ECDSASignature]) extends ImporterMsg
363+
case class NewCheckpoint(block: Block) extends ImporterMsg
392364
case class ImportNewBlock(block: Block, peerId: PeerId) extends ImporterMsg
393365
case class ImportDone(newBehavior: NewBehavior, blockImportType: BlockImportType) extends ImporterMsg
394366
case object PickBlocks extends ImporterMsg

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package io.iohk.ethereum.blockchain.sync.regular
22

33
import akka.actor.{Actor, ActorLogging, ActorRef, AllForOneStrategy, Cancellable, Props, Scheduler, SupervisorStrategy}
4-
import akka.util.ByteString
54
import io.iohk.ethereum.blockchain.sync.SyncProtocol
65
import io.iohk.ethereum.blockchain.sync.SyncProtocol.Status
76
import io.iohk.ethereum.blockchain.sync.SyncProtocol.Status.Progress
87
import io.iohk.ethereum.blockchain.sync.regular.BlockFetcher.{InternalCheckpointImport, InternalLastBlockImport}
98
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.{NewCheckpoint, ProgressProtocol, ProgressState}
10-
import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator
119
import io.iohk.ethereum.consensus.validators.BlockValidator
12-
import io.iohk.ethereum.crypto.ECDSASignature
13-
import io.iohk.ethereum.domain.Blockchain
10+
import io.iohk.ethereum.domain.{Block, Blockchain}
1411
import io.iohk.ethereum.ledger.Ledger
1512
import io.iohk.ethereum.utils.ByteStringUtils
1613
import io.iohk.ethereum.utils.Config.SyncConfig
@@ -25,7 +22,6 @@ class RegularSync(
2522
syncConfig: SyncConfig,
2623
ommersPool: ActorRef,
2724
pendingTransactionsManager: ActorRef,
28-
checkpointBlockGenerator: CheckpointBlockGenerator,
2925
scheduler: Scheduler
3026
) extends Actor
3127
with ActorLogging {
@@ -50,7 +46,6 @@ class RegularSync(
5046
ommersPool,
5147
broadcaster,
5248
pendingTransactionsManager,
53-
checkpointBlockGenerator,
5449
self
5550
),
5651
"block-importer"
@@ -83,9 +78,9 @@ class RegularSync(
8378
log.info(s"Block mined [number = {}, hash = {}]", block.number, block.header.hashAsHexString)
8479
importer ! BlockImporter.MinedBlock(block)
8580

86-
case NewCheckpoint(parentHash, signatures) =>
87-
log.info(s"Received new checkpoint for block ${ByteStringUtils.hash2string(parentHash)}")
88-
importer ! BlockImporter.NewCheckpoint(parentHash, signatures)
81+
case NewCheckpoint(block) =>
82+
log.info(s"Received new checkpoint for block ${ByteStringUtils.hash2string(block.header.parentHash)}")
83+
importer ! BlockImporter.NewCheckpoint(block)
8984

9085
case SyncProtocol.GetStatus =>
9186
sender() ! progressState.toStatus
@@ -131,7 +126,6 @@ object RegularSync {
131126
syncConfig: SyncConfig,
132127
ommersPool: ActorRef,
133128
pendingTransactionsManager: ActorRef,
134-
checkpointBlockGenerator: CheckpointBlockGenerator,
135129
scheduler: Scheduler
136130
): Props =
137131
Props(
@@ -145,12 +139,11 @@ object RegularSync {
145139
syncConfig,
146140
ommersPool,
147141
pendingTransactionsManager,
148-
checkpointBlockGenerator,
149142
scheduler
150143
)
151144
)
152145

153-
case class NewCheckpoint(parentHash: ByteString, signatures: Seq[ECDSASignature])
146+
case class NewCheckpoint(block: Block)
154147

155148
case class ProgressState(
156149
startedFetching: Boolean,

src/main/scala/io/iohk/ethereum/jsonrpc/CheckpointingService.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ package io.iohk.ethereum.jsonrpc
33
import akka.actor.ActorRef
44
import akka.util.ByteString
55
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.NewCheckpoint
6+
import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator
67
import io.iohk.ethereum.crypto.ECDSASignature
7-
import io.iohk.ethereum.domain.Blockchain
8-
import io.iohk.ethereum.utils.Logger
8+
import io.iohk.ethereum.domain.{Block, Blockchain, Checkpoint}
9+
import io.iohk.ethereum.ledger.Ledger
10+
import io.iohk.ethereum.utils.{ByteStringUtils, Logger}
911
import monix.eval.Task
1012

1113
class CheckpointingService(
1214
blockchain: Blockchain,
15+
ledger: Ledger,
16+
checkpointBlockGenerator: CheckpointBlockGenerator,
1317
syncController: ActorRef
1418
) extends Logger {
1519

@@ -40,7 +44,16 @@ class CheckpointingService(
4044
}
4145

4246
def pushCheckpoint(req: PushCheckpointRequest): ServiceResponse[PushCheckpointResponse] = Task {
43-
syncController ! NewCheckpoint(req.hash, req.signatures)
47+
val parentHash = req.hash
48+
49+
ledger.getBlockByHash(parentHash) match {
50+
case Some(parent) =>
51+
val checkpointBlock: Block = checkpointBlockGenerator.generate(parent, Checkpoint(req.signatures))
52+
syncController ! NewCheckpoint(checkpointBlock)
53+
54+
case None =>
55+
log.error(s"Could not find parent (${ByteStringUtils.hash2string(parentHash)}) for new checkpoint block")
56+
}
4457
Right(PushCheckpointResponse())
4558
}
4659
}

src/main/scala/io/iohk/ethereum/jsonrpc/QAService.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import cats.implicits._
66
import enumeratum._
77
import io.iohk.ethereum.blockchain.sync.regular.RegularSync.NewCheckpoint
88
import io.iohk.ethereum.consensus._
9+
import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator
910
import io.iohk.ethereum.consensus.ethash.MinerResponses._
1011
import io.iohk.ethereum.consensus.ethash.MockedMinerProtocol.MineBlocks
1112
import io.iohk.ethereum.consensus.ethash.{MinerResponse, MinerResponses}
1213
import io.iohk.ethereum.crypto
1314
import io.iohk.ethereum.crypto.ECDSASignature
14-
import io.iohk.ethereum.domain.{Blockchain, Checkpoint}
15+
import io.iohk.ethereum.domain.{Block, Blockchain, Checkpoint}
1516
import io.iohk.ethereum.jsonrpc.QAService.MineBlocksResponse.MinerResponseType
1617
import io.iohk.ethereum.jsonrpc.QAService._
1718
import io.iohk.ethereum.utils.{BlockchainConfig, Logger}
@@ -21,6 +22,7 @@ import mouse.all._
2122
class QAService(
2223
consensus: Consensus,
2324
blockchain: Blockchain,
25+
checkpointBlockGenerator: CheckpointBlockGenerator,
2426
blockchainConfig: BlockchainConfig,
2527
syncController: ActorRef
2628
) extends Logger {
@@ -48,8 +50,10 @@ class QAService(
4850
hash match {
4951
case Some(hashValue) =>
5052
Task {
53+
val parent = blockchain.getBlockByHash(hashValue).orElse(blockchain.getBestBlock()).getOrElse(blockchain.genesisBlock)
5154
val checkpoint = generateCheckpoint(hashValue, req.privateKeys)
52-
syncController ! NewCheckpoint(hashValue, checkpoint.signatures)
55+
val checkpointBlock: Block = checkpointBlockGenerator.generate(parent, checkpoint)
56+
syncController ! NewCheckpoint(checkpointBlock)
5357
Right(GenerateCheckpointResponse(checkpoint))
5458
}
5559
case None => Task.now(Left(JsonRpcError.BlockNotFound))

0 commit comments

Comments
 (0)