2018-11-26 13:33:06 +00:00
|
|
|
import
|
2020-07-27 16:04:44 +00:00
|
|
|
std/[deques, tables, hashes, options, strformat],
|
2020-07-02 15:14:11 +00:00
|
|
|
chronos, web3, web3/ethtypes as web3Types, json, chronicles,
|
|
|
|
eth/common/eth_types, eth/async_utils,
|
2020-07-27 16:04:44 +00:00
|
|
|
spec/[datatypes, digest, crypto, beaconstate, helpers, validator],
|
2020-07-09 22:08:54 +00:00
|
|
|
network_metadata, merkle_minimal
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
from times import epochTime
|
|
|
|
|
2020-06-19 17:42:28 +00:00
|
|
|
export
|
2020-07-02 15:14:11 +00:00
|
|
|
web3Types
|
2020-06-19 17:42:28 +00:00
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
contract(DepositContract):
|
|
|
|
proc deposit(pubkey: Bytes48,
|
|
|
|
withdrawalCredentials: Bytes32,
|
|
|
|
signature: Bytes96,
|
|
|
|
deposit_data_root: FixedBytes[32])
|
|
|
|
|
|
|
|
proc get_deposit_root(): FixedBytes[32]
|
|
|
|
proc get_deposit_count(): Bytes8
|
|
|
|
|
|
|
|
proc DepositEvent(pubkey: Bytes48,
|
|
|
|
withdrawalCredentials: Bytes32,
|
|
|
|
amount: Bytes8,
|
|
|
|
signature: Bytes96,
|
|
|
|
index: Bytes8) {.event.}
|
|
|
|
# TODO
|
|
|
|
# The raises list of this module are still not usable due to general
|
|
|
|
# Exceptions being reported from Chronos's asyncfutures2.
|
|
|
|
|
2018-11-26 13:33:06 +00:00
|
|
|
type
|
2020-03-24 11:13:07 +00:00
|
|
|
Eth1BlockNumber* = uint64
|
|
|
|
Eth1BlockTimestamp* = uint64
|
2020-07-02 15:14:11 +00:00
|
|
|
Eth1BlockHeader = web3Types.BlockHeader
|
2020-03-24 11:13:07 +00:00
|
|
|
|
|
|
|
Eth1Block* = ref object
|
|
|
|
number*: Eth1BlockNumber
|
|
|
|
timestamp*: Eth1BlockTimestamp
|
|
|
|
deposits*: seq[Deposit]
|
|
|
|
voteData*: Eth1Data
|
2020-06-27 12:01:19 +00:00
|
|
|
knownGoodDepositsCount*: Option[uint64]
|
2020-06-25 23:33:06 +00:00
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
Eth1Chain* = object
|
2020-06-27 12:01:19 +00:00
|
|
|
knownStart: Eth1Data
|
|
|
|
knownStartBlockNum: Option[Eth1BlockNumber]
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
blocks: Deque[Eth1Block]
|
|
|
|
blocksByHash: Table[BlockHash, Eth1Block]
|
2020-06-27 12:01:19 +00:00
|
|
|
allDeposits*: seq[Deposit]
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2019-09-09 15:59:02 +00:00
|
|
|
MainchainMonitor* = ref object
|
2020-07-07 23:02:14 +00:00
|
|
|
preset: RuntimePreset
|
2019-09-09 15:59:02 +00:00
|
|
|
depositContractAddress: Address
|
2020-03-24 11:13:07 +00:00
|
|
|
dataProviderFactory*: DataProviderFactory
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2020-04-23 18:58:54 +00:00
|
|
|
genesisState: NilableBeaconStateRef
|
2019-09-09 15:59:02 +00:00
|
|
|
genesisStateFut: Future[void]
|
2020-06-27 12:01:19 +00:00
|
|
|
genesisMonitoringFut: Future[void]
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
eth1Chain: Eth1Chain
|
2019-09-09 15:59:02 +00:00
|
|
|
|
2020-07-02 15:14:11 +00:00
|
|
|
depositQueue: AsyncQueue[Eth1BlockHeader]
|
2019-11-22 13:16:07 +00:00
|
|
|
runFut: Future[void]
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
DataProvider* = object of RootObj
|
|
|
|
DataProviderRef* = ref DataProvider
|
|
|
|
|
|
|
|
DataProviderFactory* = object
|
|
|
|
desc: string
|
|
|
|
new: proc(depositContractAddress: Address): Future[DataProviderRef] {.
|
|
|
|
gcsafe
|
|
|
|
# raises: [Defect]
|
|
|
|
.}
|
|
|
|
|
|
|
|
Web3DataProvider* = object of DataProvider
|
|
|
|
url: string
|
|
|
|
web3: Web3
|
|
|
|
ns: Sender[DepositContract]
|
2020-06-27 12:01:19 +00:00
|
|
|
blockHeadersSubscription: Subscription
|
2020-03-24 11:13:07 +00:00
|
|
|
|
|
|
|
Web3DataProviderRef* = ref Web3DataProvider
|
|
|
|
|
|
|
|
ReorgDepthLimitExceeded = object of CatchableError
|
|
|
|
CorruptDataProvider = object of CatchableError
|
|
|
|
|
|
|
|
DisconnectHandler* = proc () {.gcsafe, raises: [Defect].}
|
|
|
|
|
|
|
|
DepositEventHandler* = proc (
|
|
|
|
pubkey: Bytes48,
|
|
|
|
withdrawalCredentials: Bytes32,
|
|
|
|
amount: Bytes8,
|
2020-05-08 14:24:47 +00:00
|
|
|
signature: Bytes96, merkleTreeIndex: Bytes8, j: JsonNode) {.raises: [Defect], gcsafe.}
|
2020-03-24 11:13:07 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
reorgDepthLimit = 1000
|
2020-06-17 16:39:16 +00:00
|
|
|
web3Timeouts = 5.seconds
|
2020-06-27 12:01:19 +00:00
|
|
|
followDistanceInSeconds = uint64(SECONDS_PER_ETH1_BLOCK * ETH1_FOLLOW_DISTANCE)
|
2020-07-07 23:02:14 +00:00
|
|
|
|
|
|
|
# TODO: Add preset validation
|
|
|
|
# MIN_GENESIS_ACTIVE_VALIDATOR_COUNT should be larger than SLOTS_PER_EPOCH
|
|
|
|
# doAssert SECONDS_PER_ETH1_BLOCK * ETH1_FOLLOW_DISTANCE < GENESIS_DELAY,
|
|
|
|
# "Invalid configuration: GENESIS_DELAY is set too low"
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-04-22 23:35:55 +00:00
|
|
|
# TODO Nim's analysis on the lock level of the methods in this
|
|
|
|
# module seems broken. Investigate and file this as an issue.
|
|
|
|
{.push warning[LockLevel]: off.}
|
|
|
|
|
2020-07-29 12:47:03 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/validator.md#get_eth1_data
|
2020-03-24 11:13:07 +00:00
|
|
|
func compute_time_at_slot(state: BeaconState, slot: Slot): uint64 =
|
2020-06-15 09:38:05 +00:00
|
|
|
state.genesis_time + slot * SECONDS_PER_SLOT
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-07-30 09:47:57 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/validator.md#get_eth1_data
|
2020-03-24 11:13:07 +00:00
|
|
|
func voting_period_start_time*(state: BeaconState): uint64 =
|
2020-06-15 09:38:05 +00:00
|
|
|
let eth1_voting_period_start_slot =
|
|
|
|
state.slot - state.slot mod SLOTS_PER_ETH1_VOTING_PERIOD.uint64
|
|
|
|
compute_time_at_slot(state, eth1_voting_period_start_slot)
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-07-29 12:47:03 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/validator.md#get_eth1_data
|
2020-03-24 11:13:07 +00:00
|
|
|
func is_candidate_block(blk: Eth1Block, period_start: uint64): bool =
|
2020-04-27 19:03:03 +00:00
|
|
|
(blk.timestamp + SECONDS_PER_ETH1_BLOCK.uint64 * ETH1_FOLLOW_DISTANCE.uint64 <= period_start) and
|
|
|
|
(blk.timestamp + SECONDS_PER_ETH1_BLOCK.uint64 * ETH1_FOLLOW_DISTANCE.uint64 * 2 >= period_start)
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-19 17:42:28 +00:00
|
|
|
func asEth2Digest*(x: BlockHash): Eth2Digest =
|
2020-03-24 11:13:07 +00:00
|
|
|
Eth2Digest(data: array[32, byte](x))
|
|
|
|
|
|
|
|
template asBlockHash(x: Eth2Digest): BlockHash =
|
|
|
|
BlockHash(x.data)
|
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
func shortLog(b: Eth1Block): string =
|
|
|
|
&"{b.number}:{shortLog b.voteData.block_hash}"
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
func getDepositsInRange(eth1Chain: Eth1Chain,
|
|
|
|
sinceBlock, latestBlock: Eth1BlockNumber): seq[Deposit] =
|
|
|
|
## Returns all deposits that happened AFTER the block `sinceBlock` (not inclusive).
|
|
|
|
## The deposits in `latestBlock` will be included.
|
|
|
|
if latestBlock <= sinceBlock: return
|
|
|
|
|
|
|
|
let firstBlockInCache = eth1Chain.blocks[0].number
|
|
|
|
|
|
|
|
# This function should be used with indices obtained with `eth1Chain.findBlock`.
|
|
|
|
# This guarantess that both of these indices will be valid:
|
|
|
|
doAssert sinceBlock >= firstBlockInCache and
|
2020-07-26 18:55:48 +00:00
|
|
|
(latestBlock - firstBlockInCache) < eth1Chain.blocks.lenu64
|
2020-03-24 11:13:07 +00:00
|
|
|
let
|
|
|
|
sinceBlockIdx = sinceBlock - firstBlockInCache
|
|
|
|
latestBlockIdx = latestBlock - firstBlockInCache
|
|
|
|
|
|
|
|
for i in (sinceBlockIdx + 1) ..< latestBlockIdx:
|
|
|
|
result.add eth1Chain.blocks[i].deposits
|
|
|
|
|
|
|
|
template findBlock*(eth1Chain: Eth1Chain, hash: BlockHash): Eth1Block =
|
|
|
|
eth1Chain.blocksByHash.getOrDefault(hash, nil)
|
|
|
|
|
|
|
|
template findBlock*(eth1Chain: Eth1Chain, eth1Data: Eth1Data): Eth1Block =
|
|
|
|
getOrDefault(eth1Chain.blocksByHash, asBlockHash(eth1Data.block_hash), nil)
|
|
|
|
|
|
|
|
proc findParent*(eth1Chain: Eth1Chain, blk: BlockObject): Eth1Block =
|
|
|
|
result = eth1Chain.findBlock(blk.parentHash)
|
|
|
|
# a distinct type is stipped here:
|
|
|
|
let blockNumber = Eth1BlockNumber(blk.number)
|
|
|
|
if result != nil and result.number != blockNumber - 1:
|
|
|
|
debug "Found inconsistent numbering of Eth1 blocks. Ignoring block.",
|
|
|
|
blockHash = blk.hash.toHex, blockNumber,
|
|
|
|
parentHash = blk.parentHash.toHex, parentNumber = result.number
|
|
|
|
result = nil
|
|
|
|
|
|
|
|
func latestCandidateBlock(eth1Chain: Eth1Chain, periodStart: uint64): Eth1Block =
|
|
|
|
for i in countdown(eth1Chain.blocks.len - 1, 0):
|
|
|
|
let blk = eth1Chain.blocks[i]
|
|
|
|
if is_candidate_block(blk, periodStart):
|
|
|
|
return blk
|
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
func popBlock(eth1Chain: var Eth1Chain) =
|
|
|
|
let removed = eth1Chain.blocks.popLast
|
|
|
|
eth1Chain.blocksByHash.del removed.voteData.block_hash.asBlockHash
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
func trimHeight(eth1Chain: var Eth1Chain, blockNumber: Eth1BlockNumber) =
|
|
|
|
## Removes all blocks above certain `blockNumber`
|
2020-06-27 12:01:19 +00:00
|
|
|
while eth1Chain.blocks.len > 0:
|
|
|
|
if eth1Chain.blocks.peekLast.number > blockNumber:
|
|
|
|
eth1Chain.popBlock()
|
|
|
|
else:
|
|
|
|
break
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
if eth1Chain.blocks.len > 0:
|
|
|
|
eth1Chain.allDeposits.setLen(eth1Chain.blocks[^1].voteData.deposit_count)
|
|
|
|
else:
|
|
|
|
eth1Chain.allDeposits.setLen(0)
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-24 13:48:57 +00:00
|
|
|
func isSuccessorBlock(eth1Chain: Eth1Chain, newBlock: Eth1Block): bool =
|
2020-06-27 12:01:19 +00:00
|
|
|
let currentDepositCount = if eth1Chain.blocks.len == 0:
|
|
|
|
eth1Chain.knownStart.deposit_count
|
|
|
|
else:
|
|
|
|
let lastBlock = eth1Chain.blocks.peekLast
|
|
|
|
if lastBlock.number >= newBlock.number: return false
|
|
|
|
lastBlock.voteData.deposit_count
|
2020-06-24 13:48:57 +00:00
|
|
|
|
2020-07-26 18:55:48 +00:00
|
|
|
(currentDepositCount + newBlock.deposits.lenu64) == newBlock.voteData.deposit_count
|
2020-06-24 13:48:57 +00:00
|
|
|
|
|
|
|
func addSuccessorBlock*(eth1Chain: var Eth1Chain, newBlock: Eth1Block): bool =
|
|
|
|
result = isSuccessorBlock(eth1Chain, newBlock)
|
|
|
|
if result:
|
2020-06-27 12:01:19 +00:00
|
|
|
eth1Chain.allDeposits.add newBlock.deposits
|
|
|
|
reset newBlock.deposits
|
2020-06-24 13:48:57 +00:00
|
|
|
eth1Chain.blocks.addLast newBlock
|
|
|
|
eth1Chain.blocksByHash[newBlock.voteData.block_hash.asBlockHash] = newBlock
|
2020-03-24 11:13:07 +00:00
|
|
|
|
|
|
|
func totalDeposits*(eth1Chain: Eth1Chain): int =
|
|
|
|
for blk in eth1Chain.blocks:
|
|
|
|
result += blk.deposits.len
|
|
|
|
|
|
|
|
func allDeposits*(eth1Chain: Eth1Chain): seq[Deposit] =
|
|
|
|
for blk in eth1Chain.blocks:
|
|
|
|
result.add blk.deposits
|
|
|
|
|
2020-06-19 17:42:28 +00:00
|
|
|
func clear*(eth1Chain: var Eth1Chain) =
|
|
|
|
eth1Chain = default(Eth1Chain)
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
template hash*(x: Eth1Block): Hash =
|
|
|
|
hash(x.voteData.block_hash.data)
|
|
|
|
|
|
|
|
template notImplemented =
|
|
|
|
doAssert false, "Method not implemented"
|
|
|
|
|
|
|
|
method getBlockByHash*(p: DataProviderRef, hash: BlockHash): Future[BlockObject] {.
|
|
|
|
base
|
|
|
|
gcsafe
|
|
|
|
locks: 0
|
|
|
|
# raises: [Defect]
|
|
|
|
.} =
|
2020-06-19 17:42:28 +00:00
|
|
|
notImplemented
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
method getBlockByNumber*(p: DataProviderRef, hash: Eth1BlockNumber): Future[BlockObject] {.
|
|
|
|
base
|
|
|
|
gcsafe
|
|
|
|
locks: 0
|
|
|
|
# raises: [Defect]
|
|
|
|
.} =
|
|
|
|
notImplemented
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
method onDisconnect*(p: DataProviderRef, handler: DisconnectHandler) {.
|
|
|
|
base
|
|
|
|
gcsafe
|
|
|
|
locks: 0
|
|
|
|
# raises: []
|
|
|
|
.} =
|
|
|
|
notImplemented
|
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
method onBlockHeaders*(p: DataProviderRef,
|
|
|
|
blockHeaderHandler: BlockHeaderHandler,
|
|
|
|
errorHandler: SubscriptionErrorHandler): Future[void] {.
|
2020-03-24 11:13:07 +00:00
|
|
|
base
|
|
|
|
gcsafe
|
|
|
|
locks: 0
|
|
|
|
# raises: []
|
|
|
|
.} =
|
|
|
|
notImplemented
|
|
|
|
|
|
|
|
method close*(p: DataProviderRef): Future[void] {.
|
|
|
|
base
|
|
|
|
gcsafe
|
|
|
|
locks: 0
|
|
|
|
# raises: [Defect]
|
|
|
|
.} =
|
|
|
|
notImplemented
|
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
method fetchDepositData*(p: DataProviderRef,
|
|
|
|
fromBlock, toBlock: Eth1BlockNumber): Future[seq[Eth1Block]] {.
|
2020-06-19 17:42:28 +00:00
|
|
|
base
|
|
|
|
gcsafe
|
|
|
|
locks: 0
|
|
|
|
# raises: [Defect, CatchableError]
|
|
|
|
.} =
|
|
|
|
notImplemented
|
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
method fetchBlockDetails(p: DataProviderRef, blk: Eth1Block): Future[void] {.
|
2020-03-24 11:13:07 +00:00
|
|
|
base
|
|
|
|
gcsafe
|
|
|
|
locks: 0
|
|
|
|
# raises: [Defect, CatchableError]
|
|
|
|
.} =
|
|
|
|
notImplemented
|
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
proc new*(T: type Web3DataProvider,
|
|
|
|
web3Url: string,
|
|
|
|
depositContractAddress: Address): Future[ref Web3DataProvider] {.
|
|
|
|
async
|
|
|
|
# raises: [Defect]
|
|
|
|
.} =
|
|
|
|
try:
|
|
|
|
type R = ref T
|
|
|
|
let
|
|
|
|
web3 = await newWeb3(web3Url)
|
|
|
|
ns = web3.contractSender(DepositContract, depositContractAddress)
|
|
|
|
return R(url: web3Url, web3: web3, ns: ns)
|
|
|
|
except CatchableError:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
func web3Provider*(web3Url: string): DataProviderFactory =
|
|
|
|
proc factory(depositContractAddress: Address): Future[DataProviderRef] {.async.} =
|
|
|
|
result = await Web3DataProvider.new(web3Url, depositContractAddress)
|
|
|
|
|
|
|
|
DataProviderFactory(desc: "web3(" & web3Url & ")", new: factory)
|
|
|
|
|
|
|
|
method close*(p: Web3DataProviderRef): Future[void] {.async, locks: 0.} =
|
|
|
|
if p.blockHeadersSubscription != nil:
|
|
|
|
await p.blockHeadersSubscription.unsubscribe()
|
|
|
|
|
|
|
|
await p.web3.close()
|
|
|
|
|
|
|
|
method getBlockByHash*(p: Web3DataProviderRef, hash: BlockHash): Future[BlockObject] =
|
|
|
|
return p.web3.provider.eth_getBlockByHash(hash, false)
|
|
|
|
|
|
|
|
method getBlockByNumber*(p: Web3DataProviderRef, number: Eth1BlockNumber): Future[BlockObject] =
|
|
|
|
return p.web3.provider.eth_getBlockByNumber(&"0x{number:X}", false)
|
|
|
|
|
|
|
|
proc getBlockNumber(p: DataProviderRef, hash: BlockHash): Future[Eth1BlockNumber] {.async.} =
|
|
|
|
try:
|
|
|
|
let blk = awaitWithTimeout(p.getBlockByHash(hash), web3Timeouts):
|
|
|
|
return 0
|
|
|
|
return Eth1BlockNumber(blk.number)
|
|
|
|
except CatchableError as exc:
|
|
|
|
notice "Failed to get Eth1 block number from hash",
|
|
|
|
hash = $hash, err = exc.msg
|
|
|
|
raise
|
|
|
|
|
|
|
|
template readJsonField(j: JsonNode,
|
|
|
|
fieldName: string,
|
|
|
|
ValueType: type): untyped =
|
|
|
|
var res: ValueType
|
|
|
|
fromJson(j[fieldName], fieldName, res)
|
|
|
|
res
|
|
|
|
|
|
|
|
proc readJsonDeposits(depositsList: JsonNode): seq[Eth1Block] =
|
|
|
|
if depositsList.kind != JArray:
|
|
|
|
raise newException(CatchableError,
|
|
|
|
"Web3 provider didn't return a list of deposit events")
|
|
|
|
|
|
|
|
var lastEth1Block: Eth1Block
|
|
|
|
|
|
|
|
for logEvent in depositsList:
|
|
|
|
let
|
|
|
|
blockNumber = Eth1BlockNumber readJsonField(logEvent, "blockNumber", Quantity)
|
|
|
|
blockHash = readJsonField(logEvent, "blockHash", BlockHash)
|
|
|
|
logData = strip0xPrefix(logEvent["data"].getStr)
|
|
|
|
|
|
|
|
if lastEth1Block == nil or lastEth1Block.number != blockNumber:
|
|
|
|
lastEth1Block = Eth1Block(
|
|
|
|
number: blockNumber,
|
|
|
|
voteData: Eth1Data(block_hash: blockHash.asEth2Digest))
|
|
|
|
|
|
|
|
result.add lastEth1Block
|
|
|
|
|
|
|
|
var
|
|
|
|
pubkey: Bytes48
|
|
|
|
withdrawalCredentials: Bytes32
|
|
|
|
amount: Bytes8
|
|
|
|
signature: Bytes96
|
|
|
|
index: Bytes8
|
|
|
|
|
|
|
|
var offset = 0
|
|
|
|
offset += decode(logData, offset, pubkey)
|
|
|
|
offset += decode(logData, offset, withdrawalCredentials)
|
|
|
|
offset += decode(logData, offset, amount)
|
|
|
|
offset += decode(logData, offset, signature)
|
|
|
|
offset += decode(logData, offset, index)
|
|
|
|
|
|
|
|
lastEth1Block.deposits.add Deposit(
|
|
|
|
data: DepositData(
|
|
|
|
pubkey: ValidatorPubKey.init(array[48, byte](pubkey)),
|
|
|
|
withdrawal_credentials: Eth2Digest(data: array[32, byte](withdrawalCredentials)),
|
2020-07-27 10:59:57 +00:00
|
|
|
amount: bytes_to_uint64(array[8, byte](amount)),
|
2020-06-27 12:01:19 +00:00
|
|
|
signature: ValidatorSig.init(array[96, byte](signature))))
|
|
|
|
|
|
|
|
method fetchDepositData*(p: Web3DataProviderRef,
|
|
|
|
fromBlock, toBlock: Eth1BlockNumber): Future[seq[Eth1Block]]
|
|
|
|
{.async, locks: 0.} =
|
2020-06-29 13:00:16 +00:00
|
|
|
info "Obtaining deposit log events", fromBlock, toBlock
|
2020-06-27 12:01:19 +00:00
|
|
|
return readJsonDeposits(await p.ns.getJsonLogs(DepositEvent,
|
|
|
|
fromBlock = some blockId(fromBlock),
|
|
|
|
toBlock = some blockId(toBlock)))
|
|
|
|
|
|
|
|
method fetchBlockDetails(p: Web3DataProviderRef, blk: Eth1Block) {.async.} =
|
|
|
|
let
|
|
|
|
web3Block = p.getBlockByNumber(blk.number)
|
|
|
|
depositRoot = p.ns.get_deposit_root.call(blockNumber = blk.number)
|
|
|
|
rawCount = p.ns.get_deposit_count.call(blockNumber = blk.number)
|
|
|
|
|
|
|
|
discard await web3Block
|
|
|
|
discard await depositRoot
|
|
|
|
discard await rawCount
|
|
|
|
|
2020-07-27 10:59:57 +00:00
|
|
|
let depositCount = bytes_to_uint64(array[8, byte](rawCount.read))
|
2020-06-27 12:01:19 +00:00
|
|
|
|
|
|
|
blk.timestamp = Eth1BlockTimestamp(web3Block.read.timestamp)
|
|
|
|
blk.voteData.deposit_count = depositCount
|
|
|
|
blk.voteData.deposit_root = depositRoot.read.asEth2Digest
|
|
|
|
|
|
|
|
method onDisconnect*(p: Web3DataProviderRef, handler: DisconnectHandler) {.
|
|
|
|
gcsafe
|
|
|
|
locks: 0
|
|
|
|
# raises: []
|
|
|
|
.} =
|
|
|
|
p.web3.onDisconnect = handler
|
|
|
|
|
|
|
|
method onBlockHeaders*(p: Web3DataProviderRef,
|
|
|
|
blockHeaderHandler: BlockHeaderHandler,
|
|
|
|
errorHandler: SubscriptionErrorHandler): Future[void] {.
|
|
|
|
async
|
|
|
|
gcsafe
|
|
|
|
locks: 0
|
|
|
|
# raises: []
|
|
|
|
.} =
|
|
|
|
if p.blockHeadersSubscription != nil:
|
|
|
|
await p.blockHeadersSubscription.unsubscribe()
|
|
|
|
|
2020-06-28 20:22:23 +00:00
|
|
|
info "Waiting for new Eth1 block headers"
|
2020-06-27 12:01:19 +00:00
|
|
|
|
|
|
|
let options = newJObject()
|
|
|
|
p.blockHeadersSubscription = await p.web3.subscribeForBlockHeaders(
|
|
|
|
options, blockHeaderHandler, errorHandler)
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/validator.md#get_eth1_data
|
|
|
|
func getBlockProposalData*(eth1Chain: Eth1Chain,
|
|
|
|
state: BeaconState): (Eth1Data, seq[Deposit]) =
|
|
|
|
template voteForNoChange() =
|
|
|
|
return (state.eth1_data, newSeq[Deposit]())
|
|
|
|
|
|
|
|
let prevBlock = eth1Chain.findBlock(state.eth1_data)
|
|
|
|
if prevBlock == nil:
|
|
|
|
# The Eth1 block currently referenced in the BeaconState is unknown to us.
|
|
|
|
# This situation is not specifically covered in the honest validator spec,
|
|
|
|
# but there is a similar condition where none of the eth1_data_votes is
|
|
|
|
# present in our worldview. The suggestion there is to vote for "no change"
|
|
|
|
# and we'll do the same here:
|
|
|
|
voteForNoChange()
|
|
|
|
|
|
|
|
let periodStart = voting_period_start_time(state)
|
|
|
|
|
|
|
|
var otherVotesCountTable = initCountTable[Eth1Block]()
|
|
|
|
for vote in state.eth1_data_votes:
|
|
|
|
let eth1Block = eth1Chain.findBlock(vote)
|
|
|
|
if eth1Block != nil and is_candidate_block(eth1Block, periodStart):
|
|
|
|
otherVotesCountTable.inc eth1Block
|
|
|
|
|
|
|
|
var ourVote: Eth1Block
|
|
|
|
if otherVotesCountTable.len > 0:
|
|
|
|
ourVote = otherVotesCountTable.largest.key
|
|
|
|
else:
|
|
|
|
ourVote = eth1Chain.latestCandidateBlock(periodStart)
|
|
|
|
if ourVote == nil:
|
|
|
|
voteForNoChange()
|
|
|
|
|
|
|
|
(ourVote.voteData, eth1Chain.getDepositsInRange(prevBlock.number, ourVote.number))
|
|
|
|
|
|
|
|
template getBlockProposalData*(m: MainchainMonitor, state: BeaconState): untyped =
|
|
|
|
getBlockProposalData(m.eth1Chain, state)
|
|
|
|
|
|
|
|
proc init*(T: type MainchainMonitor,
|
2020-07-07 23:02:14 +00:00
|
|
|
preset: RuntimePreset,
|
2020-03-24 11:13:07 +00:00
|
|
|
dataProviderFactory: DataProviderFactory,
|
2020-07-02 15:14:11 +00:00
|
|
|
depositContractAddress: Eth1Address,
|
2020-06-27 12:01:19 +00:00
|
|
|
startPosition: Eth1Data): T =
|
2020-07-07 23:02:14 +00:00
|
|
|
T(preset: preset,
|
|
|
|
depositQueue: newAsyncQueue[Eth1BlockHeader](),
|
2020-06-19 17:42:28 +00:00
|
|
|
dataProviderFactory: dataProviderFactory,
|
2020-07-02 15:14:11 +00:00
|
|
|
depositContractAddress: Address depositContractAddress,
|
2020-06-27 12:01:19 +00:00
|
|
|
eth1Chain: Eth1Chain(knownStart: startPosition))
|
2019-09-09 15:59:02 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
proc isCandidateForGenesis(preset: RuntimePreset,
|
|
|
|
timeNow: float,
|
|
|
|
blk: Eth1Block): bool =
|
2020-06-27 12:01:19 +00:00
|
|
|
if float(blk.timestamp + followDistanceInSeconds) > timeNow:
|
|
|
|
return false
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
if genesis_time_from_eth1_timestamp(preset, blk.timestamp) < preset.MIN_GENESIS_TIME:
|
2020-06-27 12:01:19 +00:00
|
|
|
return false
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
if blk.knownGoodDepositsCount.isSome:
|
2020-07-07 23:02:14 +00:00
|
|
|
blk.knownGoodDepositsCount.get >= preset.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
|
2020-06-27 12:01:19 +00:00
|
|
|
else:
|
2020-07-07 23:02:14 +00:00
|
|
|
blk.voteData.deposit_count >= preset.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
proc minGenesisCandidateBlockIdx(m: MainchainMonitor): Option[int]
|
2020-06-27 12:01:19 +00:00
|
|
|
{.raises: [Defect].} =
|
2020-07-07 23:02:14 +00:00
|
|
|
if m.eth1Chain.blocks.len == 0:
|
2020-06-27 12:01:19 +00:00
|
|
|
return
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
let now = epochTime()
|
2020-07-07 23:02:14 +00:00
|
|
|
if not isCandidateForGenesis(m.preset, now, m.eth1Chain.blocks.peekLast):
|
2020-03-24 11:13:07 +00:00
|
|
|
return
|
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
var candidatePos = m.eth1Chain.blocks.len - 1
|
2020-06-27 12:01:19 +00:00
|
|
|
while candidatePos > 1:
|
2020-07-07 23:02:14 +00:00
|
|
|
if not isCandidateForGenesis(m.preset, now, m.eth1Chain.blocks[candidatePos - 1]):
|
2020-06-27 12:01:19 +00:00
|
|
|
break
|
|
|
|
dec candidatePos
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
return some(candidatePos)
|
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
proc createBeaconStateAux(preset: RuntimePreset,
|
|
|
|
eth1Block: Eth1Block,
|
2020-06-28 20:17:47 +00:00
|
|
|
deposits: var openarray[Deposit]): BeaconStateRef =
|
2020-06-27 12:01:19 +00:00
|
|
|
attachMerkleProofs deposits
|
2020-07-07 23:02:14 +00:00
|
|
|
result = initialize_beacon_state_from_eth1(preset,
|
|
|
|
eth1Block.voteData.block_hash,
|
2020-06-28 20:17:47 +00:00
|
|
|
eth1Block.timestamp.uint64,
|
|
|
|
deposits, {})
|
2020-07-27 16:04:44 +00:00
|
|
|
var cache = StateCache()
|
|
|
|
let activeValidators = count_active_validators(result[], GENESIS_EPOCH, cache)
|
2020-07-23 17:01:07 +00:00
|
|
|
eth1Block.knownGoodDepositsCount = some activeValidators
|
2020-06-28 20:17:47 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
proc createBeaconState(m: MainchainMonitor, eth1Block: Eth1Block): BeaconStateRef =
|
2020-06-28 20:17:47 +00:00
|
|
|
createBeaconStateAux(
|
2020-07-07 23:02:14 +00:00
|
|
|
m.preset,
|
2020-06-28 20:17:47 +00:00
|
|
|
eth1Block,
|
2020-07-07 23:02:14 +00:00
|
|
|
m.eth1Chain.allDeposits.toOpenArray(0, int(eth1Block.voteData.deposit_count - 1)))
|
2020-06-28 20:17:47 +00:00
|
|
|
|
|
|
|
proc signalGenesis(m: MainchainMonitor, genesisState: BeaconStateRef) =
|
|
|
|
m.genesisState = genesisState
|
|
|
|
|
|
|
|
if not m.genesisStateFut.isNil:
|
|
|
|
m.genesisStateFut.complete()
|
|
|
|
m.genesisStateFut = nil
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2020-06-28 20:17:47 +00:00
|
|
|
proc findGenesisBlockInRange(m: MainchainMonitor,
|
|
|
|
startBlock, endBlock: Eth1Block): Future[Eth1Block]
|
|
|
|
{.async.} =
|
|
|
|
let dataProvider = await m.dataProviderFactory.new(m.depositContractAddress)
|
|
|
|
if dataProvider == nil:
|
|
|
|
error "Failed to initialize Eth1 data provider",
|
|
|
|
provider = m.dataProviderFactory.desc
|
|
|
|
raise newException(CatchableError, "Failed to initialize Eth1 data provider")
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2020-06-28 20:17:47 +00:00
|
|
|
var
|
|
|
|
startBlock = startBlock
|
|
|
|
endBlock = endBlock
|
|
|
|
depositData = startBlock.voteData
|
|
|
|
|
|
|
|
while startBlock.number + 1 < endBlock.number:
|
|
|
|
let
|
2020-07-07 23:02:14 +00:00
|
|
|
MIN_GENESIS_TIME = m.preset.MIN_GENESIS_TIME
|
|
|
|
startBlockTime = genesis_time_from_eth1_timestamp(m.preset, startBlock.timestamp)
|
2020-06-28 20:17:47 +00:00
|
|
|
secondsPerBlock = float(endBlock.timestamp - startBlock.timestamp) /
|
|
|
|
float(endBlock.number - startBlock.number)
|
|
|
|
blocksToJump = max(float(MIN_GENESIS_TIME - startBlockTime) / secondsPerBlock, 1.0)
|
|
|
|
candidateNumber = min(endBlock.number - 1, startBlock.number + blocksToJump.uint64)
|
|
|
|
candidateBlock = await dataProvider.getBlockByNumber(candidateNumber)
|
|
|
|
|
|
|
|
var candidateAsEth1Block = Eth1Block(number: candidateBlock.number.uint64,
|
|
|
|
timestamp: candidateBlock.timestamp.uint64,
|
|
|
|
voteData: depositData)
|
|
|
|
candidateAsEth1Block.voteData.block_hash = candidateBlock.hash.asEth2Digest
|
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
let candidateGenesisTime = genesis_time_from_eth1_timestamp(
|
|
|
|
m.preset, candidateBlock.timestamp.uint64)
|
|
|
|
|
2020-06-28 20:17:47 +00:00
|
|
|
info "Probing possible genesis block",
|
|
|
|
`block` = candidateBlock.number.uint64,
|
2020-07-07 23:02:14 +00:00
|
|
|
candidateGenesisTime
|
2020-06-28 20:17:47 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
if candidateGenesisTime < MIN_GENESIS_TIME:
|
2020-06-28 20:17:47 +00:00
|
|
|
startBlock = candidateAsEth1Block
|
|
|
|
else:
|
|
|
|
endBlock = candidateAsEth1Block
|
|
|
|
|
|
|
|
return endBlock
|
2020-06-27 12:01:19 +00:00
|
|
|
|
|
|
|
proc checkForGenesisLoop(m: MainchainMonitor) {.async.} =
|
2019-09-09 15:59:02 +00:00
|
|
|
while true:
|
2020-06-27 12:01:19 +00:00
|
|
|
if not m.genesisState.isNil:
|
|
|
|
return
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
try:
|
2020-07-07 23:02:14 +00:00
|
|
|
let genesisCandidateIdx = m.minGenesisCandidateBlockIdx
|
2020-06-27 12:01:19 +00:00
|
|
|
if genesisCandidateIdx.isSome:
|
|
|
|
let
|
2020-06-28 20:17:47 +00:00
|
|
|
genesisCandidateIdx = genesisCandidateIdx.get
|
|
|
|
genesisCandidate = m.eth1Chain.blocks[genesisCandidateIdx]
|
2020-07-07 23:02:14 +00:00
|
|
|
candidateState = m.createBeaconState(genesisCandidate)
|
2020-06-28 20:17:47 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
if genesisCandidate.knownGoodDepositsCount.get >= m.preset.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT:
|
2020-06-28 20:17:47 +00:00
|
|
|
# We have a candidate state on our hands, but our current Eth1Chain
|
|
|
|
# may consist only of blocks that have deposits attached to them
|
|
|
|
# while the real genesis may have happened in a block without any
|
|
|
|
# deposits (triggered by MIN_GENESIS_TIME).
|
|
|
|
#
|
|
|
|
# This can happen when the beacon node is launched after the genesis
|
|
|
|
# event. We take a short cut when constructing the initial Eth1Chain
|
|
|
|
# by downloading only deposit log entries. Thus, we'll see all the
|
|
|
|
# blocks with deposits, but not the regular blocks in between.
|
|
|
|
#
|
|
|
|
# We'll handle this special case below by examing whether we are in
|
|
|
|
# this potential scenario and we'll use a fast guessing algorith to
|
|
|
|
# discover the ETh1 block with minimal valid genesis time.
|
|
|
|
if genesisCandidateIdx > 0:
|
|
|
|
let preceedingEth1Block = m.eth1Chain.blocks[genesisCandidateIdx - 1]
|
|
|
|
if preceedingEth1Block.voteData.deposit_root == genesisCandidate.voteData.deposit_root:
|
|
|
|
preceedingEth1Block.knownGoodDepositsCount = genesisCandidate.knownGoodDepositsCount
|
|
|
|
else:
|
2020-07-07 23:02:14 +00:00
|
|
|
discard m.createBeaconState(preceedingEth1Block)
|
2020-06-28 20:17:47 +00:00
|
|
|
|
2020-07-07 23:02:14 +00:00
|
|
|
if preceedingEth1Block.knownGoodDepositsCount.get >= m.preset.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT and
|
2020-06-28 20:17:47 +00:00
|
|
|
genesisCandidate.number - preceedingEth1Block.number > 1:
|
|
|
|
let genesisBlock = await m.findGenesisBlockInRange(preceedingEth1Block, genesisCandidate)
|
|
|
|
if genesisBlock.number != genesisCandidate.number:
|
2020-07-07 23:02:14 +00:00
|
|
|
m.signalGenesis m.createBeaconState(genesisBlock)
|
2020-06-28 20:17:47 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
m.signalGenesis candidateState
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
info "Eth2 genesis candidate block rejected",
|
|
|
|
`block` = shortLog(genesisCandidate),
|
|
|
|
validDeposits = genesisCandidate.knownGoodDepositsCount.get,
|
2020-07-07 23:02:14 +00:00
|
|
|
needed = m.preset.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
|
2020-06-27 12:01:19 +00:00
|
|
|
else:
|
|
|
|
# TODO: check for a stale monitor
|
|
|
|
discard
|
|
|
|
except CatchableError as err:
|
|
|
|
debug "Unexpected error in checkForGenesisLoop", err = err.msg
|
2019-09-09 15:59:02 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
await sleepAsync(1.seconds)
|
2019-09-09 15:59:02 +00:00
|
|
|
|
2020-06-28 20:17:47 +00:00
|
|
|
proc waitGenesis*(m: MainchainMonitor): Future[BeaconStateRef] {.async.} =
|
2019-09-09 15:59:02 +00:00
|
|
|
if m.genesisState.isNil:
|
|
|
|
if m.genesisStateFut.isNil:
|
2020-06-28 20:17:47 +00:00
|
|
|
m.genesisStateFut = newFuture[void]("waitGenesis")
|
2020-06-27 12:01:19 +00:00
|
|
|
|
|
|
|
m.genesisMonitoringFut = m.checkForGenesisLoop()
|
2019-09-09 15:59:02 +00:00
|
|
|
await m.genesisStateFut
|
|
|
|
m.genesisStateFut = nil
|
|
|
|
|
2020-04-23 18:58:54 +00:00
|
|
|
if m.genesisState != nil:
|
2020-04-22 23:35:55 +00:00
|
|
|
return m.genesisState
|
2020-04-23 18:58:54 +00:00
|
|
|
else:
|
2020-05-19 18:57:35 +00:00
|
|
|
result = new BeaconStateRef # make the compiler happy
|
2020-04-23 18:58:54 +00:00
|
|
|
raiseAssert "Unreachable code"
|
2019-09-09 15:59:02 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
func totalNonFinalizedBlocks(eth1Chain: Eth1Chain): Natural =
|
|
|
|
# TODO: implement this precisely
|
|
|
|
eth1Chain.blocks.len
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
func latestEth1Data(eth1Chain: Eth1Chain): Eth1Data =
|
|
|
|
if eth1Chain.blocks.len > 0:
|
|
|
|
eth1Chain.blocks[^1].voteData
|
|
|
|
else:
|
|
|
|
eth1Chain.knownStart
|
2020-06-25 23:33:06 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
func knownInvalidDepositsCount(eth1Chain: Eth1Chain): uint64 =
|
|
|
|
for i in countdown(eth1Chain.blocks.len - 1, 0):
|
|
|
|
let blk = eth1Chain.blocks[i]
|
|
|
|
if blk.knownGoodDepositsCount.isSome:
|
|
|
|
return blk.voteData.deposit_count - blk.knownGoodDepositsCount.get
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
return 0
|
2020-02-07 07:13:38 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
func maxValidDeposits(eth1Chain: Eth1Chain): uint64 =
|
|
|
|
if eth1Chain.blocks.len > 0:
|
|
|
|
let lastBlock = eth1Chain.blocks[^1]
|
|
|
|
lastBlock.knownGoodDepositsCount.get(
|
|
|
|
lastBlock.voteData.deposit_count - eth1Chain.knownInvalidDepositsCount)
|
|
|
|
else:
|
|
|
|
0
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
proc processDeposits(m: MainchainMonitor,
|
|
|
|
dataProvider: DataProviderRef) {.async.} =
|
|
|
|
# ATTENTION!
|
|
|
|
# Please note that this code is using a queue to guarantee the
|
|
|
|
# strict serial order of processing of deposits. If we had the
|
|
|
|
# same code embedded in the deposit contracts events handler,
|
|
|
|
# it could easily re-order the steps due to the intruptable
|
|
|
|
# interleaved execution of async code.
|
|
|
|
while true:
|
|
|
|
let blk = await m.depositQueue.popFirst()
|
|
|
|
m.eth1Chain.trimHeight(Eth1BlockNumber(blk.number) - 1)
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
let latestKnownBlock = if m.eth1Chain.blocks.len > 0:
|
|
|
|
m.eth1Chain.blocks[^1].number
|
|
|
|
elif m.eth1Chain.knownStartBlockNum.isSome:
|
|
|
|
m.eth1Chain.knownStartBlockNum.get
|
|
|
|
else:
|
|
|
|
m.eth1Chain.knownStartBlockNum = some(
|
|
|
|
await dataProvider.getBlockNumber(m.eth1Chain.knownStart.block_hash.asBlockHash))
|
|
|
|
m.eth1Chain.knownStartBlockNum.get
|
|
|
|
|
|
|
|
let eth1Blocks = await dataProvider.fetchDepositData(latestKnownBlock + 1,
|
|
|
|
Eth1BlockNumber blk.number)
|
|
|
|
if eth1Blocks.len == 0:
|
2020-07-07 23:02:14 +00:00
|
|
|
if m.eth1Chain.maxValidDeposits > m.preset.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT and
|
2020-06-28 20:47:04 +00:00
|
|
|
m.eth1Chain.knownStart.deposit_count == 0:
|
2020-06-27 12:01:19 +00:00
|
|
|
let latestEth1Data = m.eth1Chain.latestEth1Data
|
|
|
|
|
|
|
|
for missingBlockNum in latestKnownBlock + 1 ..< Eth1BlockNumber(blk.number):
|
|
|
|
let missingBlock = await dataProvider.getBlockByNumber(missingBlockNum)
|
|
|
|
doAssert m.eth1Chain.addSuccessorBlock Eth1Block(
|
|
|
|
number: Eth1BlockNumber(missingBlock.number),
|
|
|
|
timestamp: Eth1BlockTimestamp(missingBlock.timestamp),
|
|
|
|
voteData: latestEth1Data)
|
|
|
|
|
|
|
|
doAssert m.eth1Chain.addSuccessorBlock Eth1Block(
|
|
|
|
number: Eth1BlockNumber(blk.number),
|
|
|
|
timestamp: Eth1BlockTimestamp(blk.timestamp),
|
|
|
|
voteData: latestEth1Data)
|
|
|
|
else:
|
|
|
|
template logBlockProcessed(blk) =
|
|
|
|
info "Eth1 block processed",
|
|
|
|
`block` = shortLog(blk), totalDeposits = blk.voteData.deposit_count
|
|
|
|
|
|
|
|
await dataProvider.fetchBlockDetails(eth1Blocks[0])
|
|
|
|
if m.eth1Chain.addSuccessorBlock(eth1Blocks[0]):
|
|
|
|
logBlockProcessed eth1Blocks[0]
|
|
|
|
|
|
|
|
for i in 1 ..< eth1Blocks.len:
|
|
|
|
await dataProvider.fetchBlockDetails(eth1Blocks[i])
|
|
|
|
if m.eth1Chain.addSuccessorBlock(eth1Blocks[i]):
|
|
|
|
logBlockProcessed eth1Blocks[i]
|
|
|
|
else:
|
|
|
|
raise newException(CorruptDataProvider,
|
|
|
|
"A non-successor Eth1 block reported")
|
|
|
|
else:
|
|
|
|
# A non-continuous chain detected.
|
|
|
|
# This could be the result of a deeper fork that was not reported
|
|
|
|
# properly by the web3 provider. Since this should be an extremely
|
|
|
|
# rare event we can afford to handle it in a relatively inefficient
|
|
|
|
# manner. Let's delete half of our non-finalized chain and try again.
|
2020-07-01 18:54:20 +00:00
|
|
|
var blocksToPop = 0
|
|
|
|
if m.eth1Chain.blocks.len > 0:
|
|
|
|
blocksToPop = max(1, m.eth1Chain.totalNonFinalizedBlocks div 2)
|
|
|
|
for i in 0 ..< blocksToPop:
|
|
|
|
m.eth1Chain.popBlock()
|
|
|
|
warn "Web3 provider responded with a non-continous chain of deposits",
|
|
|
|
backtrackedDeposits = blocksToPop
|
2020-06-27 12:01:19 +00:00
|
|
|
m.depositQueue.addFirstNoWait blk
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
proc isRunning*(m: MainchainMonitor): bool =
|
|
|
|
not m.runFut.isNil
|
2019-11-22 13:16:07 +00:00
|
|
|
|
2020-06-24 13:48:57 +00:00
|
|
|
func `===`(json: JsonNode, boolean: bool): bool =
|
|
|
|
json.kind == JBool and json.bval == boolean
|
|
|
|
|
2019-11-22 13:16:07 +00:00
|
|
|
proc run(m: MainchainMonitor, delayBeforeStart: Duration) {.async.} =
|
|
|
|
if delayBeforeStart != ZeroDuration:
|
|
|
|
await sleepAsync(delayBeforeStart)
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
let dataProvider = await m.dataProviderFactory.new(m.depositContractAddress)
|
|
|
|
if dataProvider == nil:
|
|
|
|
error "Failed to initialize Eth1 data provider",
|
|
|
|
provider = m.dataProviderFactory.desc
|
|
|
|
raise newException(CatchableError, "Failed to initialize Eth1 data provider")
|
2019-11-22 13:16:07 +00:00
|
|
|
|
2020-06-16 18:38:51 +00:00
|
|
|
try:
|
2020-06-28 20:17:47 +00:00
|
|
|
info "Starting Eth1 deposit contract monitoring",
|
2020-06-16 18:38:51 +00:00
|
|
|
contract = $m.depositContractAddress,
|
|
|
|
url = m.dataProviderFactory.desc
|
|
|
|
|
2020-07-02 15:14:11 +00:00
|
|
|
await dataProvider.onBlockHeaders do (blk: Eth1BlockHeader)
|
2020-06-27 12:01:19 +00:00
|
|
|
{.raises: [Defect], gcsafe}:
|
2020-06-16 18:38:51 +00:00
|
|
|
try:
|
2020-06-27 12:01:19 +00:00
|
|
|
m.depositQueue.addLastNoWait(blk)
|
|
|
|
except AsyncQueueFullError:
|
|
|
|
raiseAssert "The depositQueue has no size limit"
|
|
|
|
except Exception:
|
|
|
|
# TODO Investigate why this exception is being raised
|
|
|
|
raiseAssert "queue.addLastNoWait should not raise exceptions"
|
|
|
|
do (err: CatchableError):
|
|
|
|
debug "Error while processing Eth1 block headers subscription", err = err.msg
|
|
|
|
|
|
|
|
await m.processDeposits(dataProvider)
|
2020-06-19 17:42:28 +00:00
|
|
|
|
2020-06-16 18:38:51 +00:00
|
|
|
finally:
|
2020-06-19 17:42:28 +00:00
|
|
|
await close(dataProvider)
|
2019-09-09 15:59:02 +00:00
|
|
|
|
2020-06-27 12:01:19 +00:00
|
|
|
proc safeCancel(fut: var Future[void]) =
|
2020-06-28 20:47:04 +00:00
|
|
|
if not fut.isNil and not fut.finished:
|
2020-06-27 12:01:19 +00:00
|
|
|
fut.cancel()
|
|
|
|
fut = nil
|
|
|
|
|
|
|
|
proc stop*(m: MainchainMonitor) =
|
|
|
|
safeCancel m.runFut
|
|
|
|
safeCancel m.genesisMonitoringFut
|
|
|
|
|
2019-11-22 13:16:07 +00:00
|
|
|
proc start(m: MainchainMonitor, delayBeforeStart: Duration) =
|
|
|
|
if m.runFut.isNil:
|
|
|
|
let runFut = m.run(delayBeforeStart)
|
|
|
|
m.runFut = runFut
|
2020-03-24 11:13:07 +00:00
|
|
|
runFut.addCallback do (p: pointer):
|
|
|
|
if runFut.failed:
|
|
|
|
if runFut.error[] of CatchableError:
|
|
|
|
if runFut == m.runFut:
|
|
|
|
error "Mainchain monitor failure, restarting", err = runFut.error.msg
|
2020-06-27 12:01:19 +00:00
|
|
|
m.stop()
|
2020-03-24 11:13:07 +00:00
|
|
|
m.start(5.seconds)
|
|
|
|
else:
|
|
|
|
fatal "Fatal exception reached", err = runFut.error.msg
|
|
|
|
quit 1
|
2019-11-22 13:16:07 +00:00
|
|
|
|
|
|
|
proc start*(m: MainchainMonitor) {.inline.} =
|
|
|
|
m.start(0.seconds)
|
|
|
|
|
2020-07-28 13:36:11 +00:00
|
|
|
proc getEth1BlockHash*(url: string, blockId: RtBlockIdentifier): Future[BlockHash] {.async.} =
|
2019-10-25 14:53:31 +00:00
|
|
|
let web3 = await newWeb3(url)
|
2020-04-22 23:35:55 +00:00
|
|
|
try:
|
2020-07-28 13:36:11 +00:00
|
|
|
let blk = await web3.provider.eth_getBlockByNumber(blockId, false)
|
|
|
|
return blk.hash
|
2020-04-22 23:35:55 +00:00
|
|
|
finally:
|
|
|
|
await web3.close()
|
|
|
|
|
|
|
|
{.pop.}
|
2020-03-24 11:13:07 +00:00
|
|
|
|