@@ -1064,18 +1064,33 @@ template getOrDefault[T, E](r: Result[T, E]): T =
10641064 type TT = T
10651065 get (r, default (TT ))
10661066
1067- proc init * (T: type Eth1Chain , cfg: RuntimeConfig , db: BeaconChainDB ): T =
1067+ proc init * (T: type Eth1Chain ,
1068+ cfg: RuntimeConfig ,
1069+ db: BeaconChainDB ,
1070+ depositContractBlockNumber: uint64 ,
1071+ depositContractBlockHash: Eth2Digest ): T =
10681072 let
1069- finalizedDeposits =
1073+ (finalizedBlockHash, depositContractState) =
10701074 if db != nil :
1071- db.getDepositTreeSnapshot ().getOrDefault ()
1075+ let treeSnapshot = db.getDepositTreeSnapshot ()
1076+ if treeSnapshot.isSome:
1077+ (treeSnapshot.get.eth1Block, treeSnapshot.get.depositContractState)
1078+ else :
1079+ let oldSnapshot = db.getUpgradableDepositSnapshot ()
1080+ if oldSnapshot.isSome:
1081+ (oldSnapshot.get.eth1Block, oldSnapshot.get.depositContractState)
1082+ else :
1083+ db.putDepositTreeSnapshot DepositTreeSnapshot (
1084+ eth1Block: depositContractBlockHash,
1085+ blockHeight: depositContractBlockNumber)
1086+ (depositContractBlockHash, default (DepositContractState ))
10721087 else :
1073- default (DepositTreeSnapshot )
1074- m = DepositsMerkleizer .init (finalizedDeposits. depositContractState)
1088+ (depositContractBlockHash, default (DepositContractState ) )
1089+ m = DepositsMerkleizer .init (depositContractState)
10751090
10761091 T (db: db,
10771092 cfg: cfg,
1078- finalizedBlockHash: finalizedDeposits.eth1Block ,
1093+ finalizedBlockHash: finalizedBlockHash ,
10791094 finalizedDepositsMerkleizer: m,
10801095 headMerkleizer: copy m)
10811096
@@ -1095,7 +1110,8 @@ proc currentEpoch(m: Eth1Monitor): Epoch =
10951110
10961111proc init * (T: type Eth1Monitor ,
10971112 cfg: RuntimeConfig ,
1098- depositContractDeployedAt: BlockHashOrNumber ,
1113+ depositContractBlockNumber: uint64 ,
1114+ depositContractBlockHash: Eth2Digest ,
10991115 db: BeaconChainDB ,
11001116 getBeaconTime: GetBeaconTimeFn ,
11011117 web3Urls: seq [string ],
@@ -1108,10 +1124,15 @@ proc init*(T: type Eth1Monitor,
11081124 for url in mitems (web3Urls):
11091125 fixupWeb3Urls url
11101126
1127+ let eth1Chain = Eth1Chain .init (
1128+ cfg, db, depositContractBlockNumber, depositContractBlockHash)
1129+
11111130 T (state: Initialized ,
1112- depositsChain: Eth1Chain . init (cfg, db) ,
1131+ depositsChain: eth1Chain ,
11131132 depositContractAddress: cfg.DEPOSIT_CONTRACT_ADDRESS ,
1114- depositContractDeployedAt: depositContractDeployedAt,
1133+ depositContractDeployedAt: BlockHashOrNumber (
1134+ isHash: true ,
1135+ hash: depositContractBlockHash),
11151136 getBeaconTime: getBeaconTime,
11161137 web3Urls: web3Urls,
11171138 eth1Network: eth1Network,
@@ -1121,37 +1142,6 @@ proc init*(T: type Eth1Monitor,
11211142 blocksPerLogsRequest: targetBlocksPerLogsRequest,
11221143 ttdReachedField: ttdReached)
11231144
1124- proc runDbMigrations * (m: Eth1Monitor ) {.async .} =
1125- template db : auto = m.depositsChain.db
1126-
1127- if db.hasDepositTreeSnapshot ():
1128- return
1129-
1130- # There might be an old deposit snapshot in the database that needs upgrade.
1131- let oldSnapshot = db.getUpgradableDepositSnapshot ()
1132- if oldSnapshot.isSome:
1133- let
1134- hash = oldSnapshot.get.eth1Block.asBlockHash ()
1135- blk = awaitWithRetries m.dataProvider.getBlockByHash (hash)
1136- blockNumber = uint64 (blk.number)
1137-
1138- db.putDepositTreeSnapshot oldSnapshot.get.toDepositTreeSnapshot (blockNumber)
1139- elif not m.depositContractAddress.isZeroMemory:
1140- # If there is no DCS record at all, create one pointing to the deployment block
1141- # of the deposit contract and insert it as a starting point.
1142- let blk = try :
1143- awaitWithRetries m.dataProvider.getBlock (m.depositContractDeployedAt)
1144- except CatchableError as e:
1145- fatal " Failed to fetch deployment block" ,
1146- depositContract = m.depositContractAddress,
1147- deploymentBlock = $ m.depositContractDeployedAt,
1148- err = e.msg
1149- quit 1
1150- doAssert blk != nil , " getBlock should not return nil"
1151- db.putDepositTreeSnapshot DepositTreeSnapshot (
1152- eth1Block: blk.hash.asEth2Digest,
1153- blockHeight: uint64 blk.number)
1154-
11551145proc safeCancel (fut: var Future [void ]) =
11561146 if not fut.isNil and not fut.finished:
11571147 fut.cancel ()
@@ -1483,8 +1473,6 @@ proc startEth1Syncing(m: Eth1Monitor, delayBeforeStart: Duration) {.async.} =
14831473 await m.ensureDataProvider ()
14841474 doAssert m.dataProvider != nil , " close not called concurrently"
14851475
1486- await m.runDbMigrations ()
1487-
14881476 # We might need to reset the chain if the new provider disagrees
14891477 # with the previous one regarding the history of the chain or if
14901478 # we have detected a conensus violation - our view disagreeing with
0 commit comments