Skip to content

Commit

Permalink
improve deposit processing performance (#4082)
Browse files Browse the repository at this point in the history
When there are a lot of deposits, we decompress the public key into a
crypto cache. To avoid having those caches grow unreasonably big,
make sure to operate on the decompressed pubkey instead.
  • Loading branch information
etan-status authored Sep 7, 2022
1 parent bf3a014 commit 0191225
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 2 additions & 4 deletions beacon_chain/spec/datatypes/base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,9 @@ const
DOMAIN_CONTRIBUTION_AND_PROOF* = DomainType([byte 0x09, 0x00, 0x00, 0x00])

func getImmutableValidatorData*(validator: Validator): ImmutableValidatorData2 =
let cookedKey = validator.pubkey.load() # Loading the pubkey is slow!
doAssert cookedKey.isSome,
"Cannot parse validator key: " & toHex(validator.pubkey)
let cookedKey = validator.pubkey.loadValid() # `Validator` has valid key
ImmutableValidatorData2(
pubkey: cookedKey.get(),
pubkey: cookedKey,
withdrawal_credentials: validator.withdrawal_credentials)

template makeLimitedUInt*(T: untyped, limit: SomeUnsignedInt) =
Expand Down
15 changes: 13 additions & 2 deletions beacon_chain/spec/signatures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,24 @@ func get_deposit_signature*(message: DepositMessage, version: Version,
blsSign(privkey, signing_root.data)

proc verify_deposit_signature*(preset: RuntimeConfig,
deposit: DepositData): bool =
deposit: DepositData,
pubkey: CookedPubKey): bool =
let
deposit_message = deposit.getDepositMessage()
signing_root = compute_deposit_signing_root(
preset.GENESIS_FORK_VERSION, deposit_message)

blsVerify(deposit.pubkey, signing_root.data, deposit.signature)
blsVerify(pubkey, signing_root.data, deposit.signature)

proc verify_deposit_signature*(preset: RuntimeConfig,
deposit: DepositData): bool =
# Deposits come with compressed public keys; uncompressing them is expensive.
# `blsVerify` fills an internal cache when using `ValidatorPubKey`.
# To avoid filling this cache unnecessarily, uncompress explicitly here.
let pubkey = deposit.pubkey.load() # Loading the pubkey is slow!
if pubkey.isNone:
return false
verify_deposit_signature(preset, deposit, pubkey.get)

func compute_voluntary_exit_signing_root*(
fork: Fork, genesis_validators_root: Eth2Digest,
Expand Down

0 comments on commit 0191225

Please sign in to comment.