Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BeaconChain this in processBlock fns #4377

Merged
merged 4 commits into from Aug 7, 2022

Conversation

dapplion
Copy link
Contributor

@dapplion dapplion commented Aug 6, 2022

Motivation

Process block functions have a huge list of dependencies, since it's a complex flow that requires interactions of many dependencies of the class BeaconChain. Seeing that huge list of dependencies I think that those functions should really be a private member of the BeaconChain class.

this.blockProcessor = new BlockProcessor(
{
clock,
bls,
regen,
executionEngine,
eth1,
db,
forkChoice,
lightClientServer,
stateCache,
checkpointStateCache,
seenAggregatedAttestations: this.seenAggregatedAttestations,
seenBlockAttesters: this.seenBlockAttesters,
beaconProposerCache: this.beaconProposerCache,
checkpointBalancesCache: this.checkpointBalancesCache,
reprocessController: this.reprocessController,
emitter,
config,
logger,
metrics,
persistInvalidSszValue: this.persistInvalidSszValue.bind(this),
persistInvalidSszView: this.persistInvalidSszView.bind(this),
},
opts,
signal
);

At the very least they should be "external binded protected" properties to modularize the code.

Description

Replace dependency injection "chain" variable with actual this BeaconChain class.

  • If we want to keep this functions in separate classes we can just use this approach
  • If we don't want to have to pass around a this object let's just move this code to BeaconChain class body. Importing a block is probably the most important task that the class does, so it makes sense to be there in its body

@dapplion dapplion requested a review from a team as a code owner August 6, 2022 10:33
@github-actions
Copy link
Contributor

github-actions bot commented Aug 6, 2022

Performance Report

✔️ no performance regression detected

Full benchmark results
Benchmark suite Current: 3b6e322 Previous: a601e81 Ratio
getPubkeys - index2pubkey - req 1000 vs - 250000 vc 2.3770 ms/op 2.0191 ms/op 1.18
getPubkeys - validatorsArr - req 1000 vs - 250000 vc 79.060 us/op 76.830 us/op 1.03
BLS verify - blst-native 1.8561 ms/op 1.9653 ms/op 0.94
BLS verifyMultipleSignatures 3 - blst-native 3.8037 ms/op 4.1170 ms/op 0.92
BLS verifyMultipleSignatures 8 - blst-native 8.1812 ms/op 8.4039 ms/op 0.97
BLS verifyMultipleSignatures 32 - blst-native 29.663 ms/op 30.209 ms/op 0.98
BLS aggregatePubkeys 32 - blst-native 39.100 us/op 42.092 us/op 0.93
BLS aggregatePubkeys 128 - blst-native 152.85 us/op 156.91 us/op 0.97
getAttestationsForBlock 174.35 ms/op 175.86 ms/op 0.99
isKnown best case - 1 super set check 451.00 ns/op 442.00 ns/op 1.02
isKnown normal case - 2 super set checks 444.00 ns/op 436.00 ns/op 1.02
isKnown worse case - 16 super set checks 441.00 ns/op 444.00 ns/op 0.99
CheckpointStateCache - add get delete 9.0470 us/op 9.3170 us/op 0.97
validate gossip signedAggregateAndProof - struct 4.2607 ms/op 4.6836 ms/op 0.91
validate gossip attestation - struct 2.0308 ms/op 2.1834 ms/op 0.93
altair verifyImport mainnet_s3766816:31 8.9423 s/op 9.0239 s/op 0.99
pickEth1Vote - no votes 2.1589 ms/op 2.0925 ms/op 1.03
pickEth1Vote - max votes 24.937 ms/op 22.759 ms/op 1.10
pickEth1Vote - Eth1Data hashTreeRoot value x2048 11.479 ms/op 11.555 ms/op 0.99
pickEth1Vote - Eth1Data hashTreeRoot tree x2048 21.893 ms/op 21.096 ms/op 1.04
pickEth1Vote - Eth1Data fastSerialize value x2048 1.6111 ms/op 1.6271 ms/op 0.99
pickEth1Vote - Eth1Data fastSerialize tree x2048 17.869 ms/op 16.289 ms/op 1.10
bytes32 toHexString 1.1970 us/op 1.1130 us/op 1.08
bytes32 Buffer.toString(hex) 734.00 ns/op 708.00 ns/op 1.04
bytes32 Buffer.toString(hex) from Uint8Array 1.0240 us/op 928.00 ns/op 1.10
bytes32 Buffer.toString(hex) + 0x 736.00 ns/op 749.00 ns/op 0.98
Object access 1 prop 0.42900 ns/op 0.37400 ns/op 1.15
Map access 1 prop 0.29500 ns/op 0.31100 ns/op 0.95
Object get x1000 17.858 ns/op 18.499 ns/op 0.97
Map get x1000 0.98200 ns/op 1.0350 ns/op 0.95
Object set x1000 134.41 ns/op 126.23 ns/op 1.06
Map set x1000 81.525 ns/op 73.665 ns/op 1.11
Return object 10000 times 0.36670 ns/op 0.39180 ns/op 0.94
Throw Error 10000 times 6.0794 us/op 6.2638 us/op 0.97
enrSubnets - fastDeserialize 64 bits 2.9190 us/op 2.7740 us/op 1.05
enrSubnets - ssz BitVector 64 bits 788.00 ns/op 782.00 ns/op 1.01
enrSubnets - fastDeserialize 4 bits 430.00 ns/op 412.00 ns/op 1.04
enrSubnets - ssz BitVector 4 bits 841.00 ns/op 786.00 ns/op 1.07
prioritizePeers score -10:0 att 32-0.1 sync 2-0 106.78 us/op 99.047 us/op 1.08
prioritizePeers score 0:0 att 32-0.25 sync 2-0.25 123.95 us/op 128.51 us/op 0.96
prioritizePeers score 0:0 att 32-0.5 sync 2-0.5 242.01 us/op 214.58 us/op 1.13
prioritizePeers score 0:0 att 64-0.75 sync 4-0.75 500.57 us/op 472.68 us/op 1.06
prioritizePeers score 0:0 att 64-1 sync 4-1 462.51 us/op 502.76 us/op 0.92
RateTracker 1000000 limit, 1 obj count per request 201.91 ns/op 200.76 ns/op 1.01
RateTracker 1000000 limit, 2 obj count per request 152.47 ns/op 156.33 ns/op 0.98
RateTracker 1000000 limit, 4 obj count per request 127.01 ns/op 135.79 ns/op 0.94
RateTracker 1000000 limit, 8 obj count per request 112.86 ns/op 128.83 ns/op 0.88
RateTracker with prune 5.3350 us/op 5.1320 us/op 1.04
array of 16000 items push then shift 3.0886 us/op 3.4304 us/op 0.90
LinkedList of 16000 items push then shift 30.488 ns/op 24.102 ns/op 1.26
array of 16000 items push then pop 253.19 ns/op 285.19 ns/op 0.89
LinkedList of 16000 items push then pop 24.890 ns/op 20.836 ns/op 1.19
array of 24000 items push then shift 4.5627 us/op 4.7506 us/op 0.96
LinkedList of 24000 items push then shift 33.120 ns/op 26.069 ns/op 1.27
array of 24000 items push then pop 212.66 ns/op 214.89 ns/op 0.99
LinkedList of 24000 items push then pop 25.240 ns/op 21.903 ns/op 1.15
intersect bitArray bitLen 8 11.775 ns/op 12.133 ns/op 0.97
intersect array and set length 8 180.08 ns/op 174.40 ns/op 1.03
intersect bitArray bitLen 128 72.316 ns/op 75.986 ns/op 0.95
intersect array and set length 128 2.4268 us/op 2.3033 us/op 1.05
Buffer.concat 32 items 1.9740 ns/op 2.0440 ns/op 0.97
pass gossip attestations to forkchoice per slot 3.2229 ms/op 3.7278 ms/op 0.86
computeDeltas 4.0820 ms/op 3.6564 ms/op 1.12
computeProposerBoostScoreFromBalances 907.61 us/op 951.98 us/op 0.95
altair processAttestation - 250000 vs - 7PWei normalcase 4.5566 ms/op 4.1588 ms/op 1.10
altair processAttestation - 250000 vs - 7PWei worstcase 6.3148 ms/op 5.8995 ms/op 1.07
altair processAttestation - setStatus - 1/6 committees join 208.04 us/op 228.93 us/op 0.91
altair processAttestation - setStatus - 1/3 committees join 401.46 us/op 420.60 us/op 0.95
altair processAttestation - setStatus - 1/2 committees join 557.90 us/op 592.98 us/op 0.94
altair processAttestation - setStatus - 2/3 committees join 709.07 us/op 786.14 us/op 0.90
altair processAttestation - setStatus - 4/5 committees join 995.43 us/op 1.1284 ms/op 0.88
altair processAttestation - setStatus - 100% committees join 1.1815 ms/op 1.3334 ms/op 0.89
altair processBlock - 250000 vs - 7PWei normalcase 28.954 ms/op 29.408 ms/op 0.98
altair processBlock - 250000 vs - 7PWei normalcase hashState 45.955 ms/op 49.700 ms/op 0.92
altair processBlock - 250000 vs - 7PWei worstcase 83.426 ms/op 85.528 ms/op 0.98
altair processBlock - 250000 vs - 7PWei worstcase hashState 112.92 ms/op 107.99 ms/op 1.05
phase0 processBlock - 250000 vs - 7PWei normalcase 4.6200 ms/op 5.4976 ms/op 0.84
phase0 processBlock - 250000 vs - 7PWei worstcase 49.107 ms/op 49.527 ms/op 0.99
altair processEth1Data - 250000 vs - 7PWei normalcase 875.12 us/op 831.21 us/op 1.05
Tree 40 250000 create 964.94 ms/op 840.22 ms/op 1.15
Tree 40 250000 get(125000) 299.17 ns/op 331.51 ns/op 0.90
Tree 40 250000 set(125000) 3.0675 us/op 3.0119 us/op 1.02
Tree 40 250000 toArray() 34.936 ms/op 34.505 ms/op 1.01
Tree 40 250000 iterate all - toArray() + loop 35.154 ms/op 33.181 ms/op 1.06
Tree 40 250000 iterate all - get(i) 117.73 ms/op 120.26 ms/op 0.98
MutableVector 250000 create 19.201 ms/op 17.749 ms/op 1.08
MutableVector 250000 get(125000) 13.086 ns/op 13.740 ns/op 0.95
MutableVector 250000 set(125000) 750.54 ns/op 657.67 ns/op 1.14
MutableVector 250000 toArray() 8.2320 ms/op 7.6226 ms/op 1.08
MutableVector 250000 iterate all - toArray() + loop 8.3970 ms/op 7.9338 ms/op 1.06
MutableVector 250000 iterate all - get(i) 3.4411 ms/op 3.6825 ms/op 0.93
Array 250000 create 7.7450 ms/op 7.3292 ms/op 1.06
Array 250000 clone - spread 4.5677 ms/op 4.1585 ms/op 1.10
Array 250000 get(125000) 1.8390 ns/op 1.7370 ns/op 1.06
Array 250000 set(125000) 1.8240 ns/op 1.7520 ns/op 1.04
Array 250000 iterate all - loop 167.90 us/op 176.09 us/op 0.95
effectiveBalanceIncrements clone Uint8Array 300000 109.58 us/op 86.154 us/op 1.27
effectiveBalanceIncrements clone MutableVector 300000 1.3940 us/op 1.3820 us/op 1.01
effectiveBalanceIncrements rw all Uint8Array 300000 252.46 us/op 267.10 us/op 0.95
effectiveBalanceIncrements rw all MutableVector 300000 259.05 ms/op 217.11 ms/op 1.19
phase0 afterProcessEpoch - 250000 vs - 7PWei 185.38 ms/op 222.10 ms/op 0.83
phase0 beforeProcessEpoch - 250000 vs - 7PWei 79.600 ms/op 77.572 ms/op 1.03
altair processEpoch - mainnet_e81889 596.43 ms/op 621.05 ms/op 0.96
mainnet_e81889 - altair beforeProcessEpoch 159.65 ms/op 143.69 ms/op 1.11
mainnet_e81889 - altair processJustificationAndFinalization 21.167 us/op 41.184 us/op 0.51
mainnet_e81889 - altair processInactivityUpdates 12.076 ms/op 11.406 ms/op 1.06
mainnet_e81889 - altair processRewardsAndPenalties 98.866 ms/op 101.02 ms/op 0.98
mainnet_e81889 - altair processRegistryUpdates 3.4700 us/op 8.8340 us/op 0.39
mainnet_e81889 - altair processSlashings 836.00 ns/op 2.6620 us/op 0.31
mainnet_e81889 - altair processEth1DataReset 821.00 ns/op 2.0760 us/op 0.40
mainnet_e81889 - altair processEffectiveBalanceUpdates 2.3427 ms/op 2.5474 ms/op 0.92
mainnet_e81889 - altair processSlashingsReset 6.1970 us/op 13.359 us/op 0.46
mainnet_e81889 - altair processRandaoMixesReset 5.2360 us/op 10.676 us/op 0.49
mainnet_e81889 - altair processHistoricalRootsUpdate 865.00 ns/op 2.4080 us/op 0.36
mainnet_e81889 - altair processParticipationFlagUpdates 2.9300 us/op 7.2530 us/op 0.40
mainnet_e81889 - altair processSyncCommitteeUpdates 728.00 ns/op 1.6340 us/op 0.45
mainnet_e81889 - altair afterProcessEpoch 193.14 ms/op 210.52 ms/op 0.92
phase0 processEpoch - mainnet_e58758 551.42 ms/op 564.52 ms/op 0.98
mainnet_e58758 - phase0 beforeProcessEpoch 251.73 ms/op 245.00 ms/op 1.03
mainnet_e58758 - phase0 processJustificationAndFinalization 20.412 us/op 37.101 us/op 0.55
mainnet_e58758 - phase0 processRewardsAndPenalties 148.08 ms/op 139.87 ms/op 1.06
mainnet_e58758 - phase0 processRegistryUpdates 9.7030 us/op 23.541 us/op 0.41
mainnet_e58758 - phase0 processSlashings 797.00 ns/op 2.7310 us/op 0.29
mainnet_e58758 - phase0 processEth1DataReset 859.00 ns/op 2.5190 us/op 0.34
mainnet_e58758 - phase0 processEffectiveBalanceUpdates 2.1143 ms/op 2.3062 ms/op 0.92
mainnet_e58758 - phase0 processSlashingsReset 3.9970 us/op 10.872 us/op 0.37
mainnet_e58758 - phase0 processRandaoMixesReset 5.3190 us/op 14.502 us/op 0.37
mainnet_e58758 - phase0 processHistoricalRootsUpdate 896.00 ns/op 2.3980 us/op 0.37
mainnet_e58758 - phase0 processParticipationRecordUpdates 4.6610 us/op 12.452 us/op 0.37
mainnet_e58758 - phase0 afterProcessEpoch 157.84 ms/op 174.81 ms/op 0.90
phase0 processEffectiveBalanceUpdates - 250000 normalcase 2.6295 ms/op 2.9582 ms/op 0.89
phase0 processEffectiveBalanceUpdates - 250000 worstcase 0.5 3.0886 ms/op 3.2213 ms/op 0.96
altair processInactivityUpdates - 250000 normalcase 43.541 ms/op 44.091 ms/op 0.99
altair processInactivityUpdates - 250000 worstcase 42.602 ms/op 54.998 ms/op 0.77
phase0 processRegistryUpdates - 250000 normalcase 7.8580 us/op 17.132 us/op 0.46
phase0 processRegistryUpdates - 250000 badcase_full_deposits 421.76 us/op 557.96 us/op 0.76
phase0 processRegistryUpdates - 250000 worstcase 0.5 220.00 ms/op 241.42 ms/op 0.91
altair processRewardsAndPenalties - 250000 normalcase 94.690 ms/op 129.72 ms/op 0.73
altair processRewardsAndPenalties - 250000 worstcase 142.23 ms/op 94.675 ms/op 1.50
phase0 getAttestationDeltas - 250000 normalcase 12.864 ms/op 14.095 ms/op 0.91
phase0 getAttestationDeltas - 250000 worstcase 13.337 ms/op 13.985 ms/op 0.95
phase0 processSlashings - 250000 worstcase 5.4406 ms/op 5.8671 ms/op 0.93
altair processSyncCommitteeUpdates - 250000 287.73 ms/op 284.81 ms/op 1.01
BeaconState.hashTreeRoot - No change 503.00 ns/op 567.00 ns/op 0.89
BeaconState.hashTreeRoot - 1 full validator 59.960 us/op 71.370 us/op 0.84
BeaconState.hashTreeRoot - 32 full validator 637.72 us/op 673.68 us/op 0.95
BeaconState.hashTreeRoot - 512 full validator 6.5837 ms/op 8.2896 ms/op 0.79
BeaconState.hashTreeRoot - 1 validator.effectiveBalance 81.922 us/op 82.887 us/op 0.99
BeaconState.hashTreeRoot - 32 validator.effectiveBalance 2.1616 ms/op 1.2152 ms/op 1.78
BeaconState.hashTreeRoot - 512 validator.effectiveBalance 15.251 ms/op 16.013 ms/op 0.95
BeaconState.hashTreeRoot - 1 balances 58.438 us/op 62.259 us/op 0.94
BeaconState.hashTreeRoot - 32 balances 535.01 us/op 595.79 us/op 0.90
BeaconState.hashTreeRoot - 512 balances 5.9705 ms/op 6.0101 ms/op 0.99
BeaconState.hashTreeRoot - 250000 balances 98.906 ms/op 99.910 ms/op 0.99
aggregationBits - 2048 els - zipIndexesInBitList 43.354 us/op 28.859 us/op 1.50
regular array get 100000 times 67.496 us/op 70.825 us/op 0.95
wrappedArray get 100000 times 67.439 us/op 70.465 us/op 0.96
arrayWithProxy get 100000 times 34.268 ms/op 30.824 ms/op 1.11
ssz.Root.equals 561.00 ns/op 510.00 ns/op 1.10
byteArrayEquals 564.00 ns/op 514.00 ns/op 1.10
shuffle list - 16384 els 11.047 ms/op 12.516 ms/op 0.88
shuffle list - 250000 els 163.35 ms/op 176.28 ms/op 0.93
processSlot - 1 slots 14.013 us/op 12.892 us/op 1.09
processSlot - 32 slots 1.8748 ms/op 1.8493 ms/op 1.01
getEffectiveBalanceIncrementsZeroInactive - 250000 vs - 7PWei 420.84 us/op 439.56 us/op 0.96
getCommitteeAssignments - req 1 vs - 250000 vc 5.2859 ms/op 5.5893 ms/op 0.95
getCommitteeAssignments - req 100 vs - 250000 vc 7.3104 ms/op 7.9521 ms/op 0.92
getCommitteeAssignments - req 1000 vs - 250000 vc 7.7800 ms/op 7.8803 ms/op 0.99
RootCache.getBlockRootAtSlot - 250000 vs - 7PWei 11.160 ns/op 11.010 ns/op 1.01
state getBlockRootAtSlot - 250000 vs - 7PWei 1.1303 us/op 1.1056 us/op 1.02
computeProposers - vc 250000 17.163 ms/op 16.977 ms/op 1.01
computeEpochShuffling - vc 250000 166.56 ms/op 175.38 ms/op 0.95
getNextSyncCommittee - vc 250000 283.93 ms/op 275.84 ms/op 1.03

by benchmarkbot/action

Copy link
Member

@wemeetagain wemeetagain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, good change

@wemeetagain wemeetagain merged commit e9c1f05 into unstable Aug 7, 2022
@wemeetagain wemeetagain deleted the dapplion/import-block-this branch August 7, 2022 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants