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

Consider state slot in getDomain #4430

Merged
merged 5 commits into from
Aug 20, 2022
Merged

Consider state slot in getDomain #4430

merged 5 commits into from
Aug 20, 2022

Conversation

wemeetagain
Copy link
Member

@wemeetagain wemeetagain commented Aug 17, 2022

Resolves #4428

Add a stateSlot: Slot parameter to getDomain. This matches the ergonomics of the consensus function.
Note: getDomain still relies on the configured fork schedule rather than the on-chain fork schedule (state.fork)

@wemeetagain wemeetagain requested a review from a team as a code owner August 17, 2022 19:09
@github-actions
Copy link
Contributor

github-actions bot commented Aug 17, 2022

Performance Report

✔️ no performance regression detected

Full benchmark results
Benchmark suite Current: 52982cf Previous: 2a6929b Ratio
getPubkeys - index2pubkey - req 1000 vs - 250000 vc 1.8016 ms/op 1.5245 ms/op 1.18
getPubkeys - validatorsArr - req 1000 vs - 250000 vc 63.227 us/op 63.193 us/op 1.00
BLS verify - blst-native 2.1659 ms/op 2.1686 ms/op 1.00
BLS verifyMultipleSignatures 3 - blst-native 4.4710 ms/op 4.4735 ms/op 1.00
BLS verifyMultipleSignatures 8 - blst-native 9.6689 ms/op 9.6766 ms/op 1.00
BLS verifyMultipleSignatures 32 - blst-native 35.135 ms/op 35.135 ms/op 1.00
BLS aggregatePubkeys 32 - blst-native 46.745 us/op 46.519 us/op 1.00
BLS aggregatePubkeys 128 - blst-native 182.47 us/op 182.54 us/op 1.00
getAttestationsForBlock 145.22 ms/op 140.45 ms/op 1.03
isKnown best case - 1 super set check 481.00 ns/op 492.00 ns/op 0.98
isKnown normal case - 2 super set checks 467.00 ns/op 481.00 ns/op 0.97
isKnown worse case - 16 super set checks 468.00 ns/op 479.00 ns/op 0.98
CheckpointStateCache - add get delete 8.7650 us/op 8.6120 us/op 1.02
validate gossip signedAggregateAndProof - struct 5.0400 ms/op 5.0221 ms/op 1.00
validate gossip attestation - struct 2.3708 ms/op 2.3740 ms/op 1.00
altair verifyImport mainnet_s3766816:31 4.7504 s/op 4.7507 s/op 1.00
pickEth1Vote - no votes 2.0609 ms/op 2.0178 ms/op 1.02
pickEth1Vote - max votes 19.362 ms/op 19.049 ms/op 1.02
pickEth1Vote - Eth1Data hashTreeRoot value x2048 12.904 ms/op 12.169 ms/op 1.06
pickEth1Vote - Eth1Data hashTreeRoot tree x2048 20.607 ms/op 21.305 ms/op 0.97
pickEth1Vote - Eth1Data fastSerialize value x2048 1.5100 ms/op 1.3853 ms/op 1.09
pickEth1Vote - Eth1Data fastSerialize tree x2048 16.207 ms/op 11.272 ms/op 1.44
bytes32 toHexString 1.0320 us/op 900.00 ns/op 1.15
bytes32 Buffer.toString(hex) 802.00 ns/op 713.00 ns/op 1.12
bytes32 Buffer.toString(hex) from Uint8Array 1.0560 us/op 946.00 ns/op 1.12
bytes32 Buffer.toString(hex) + 0x 793.00 ns/op 710.00 ns/op 1.12
Object access 1 prop 0.37500 ns/op 0.32900 ns/op 1.14
Map access 1 prop 0.32200 ns/op 0.35400 ns/op 0.91
Object get x1000 11.129 ns/op 11.151 ns/op 1.00
Map get x1000 0.94400 ns/op 0.99500 ns/op 0.95
Object set x1000 76.054 ns/op 63.735 ns/op 1.19
Map set x1000 49.627 ns/op 43.051 ns/op 1.15
Return object 10000 times 0.43190 ns/op 0.43530 ns/op 0.99
Throw Error 10000 times 6.0805 us/op 5.9415 us/op 1.02
enrSubnets - fastDeserialize 64 bits 2.8510 us/op 2.4520 us/op 1.16
enrSubnets - ssz BitVector 64 bits 811.00 ns/op 733.00 ns/op 1.11
enrSubnets - fastDeserialize 4 bits 418.00 ns/op 346.00 ns/op 1.21
enrSubnets - ssz BitVector 4 bits 830.00 ns/op 739.00 ns/op 1.12
prioritizePeers score -10:0 att 32-0.1 sync 2-0 88.549 us/op 77.994 us/op 1.14
prioritizePeers score 0:0 att 32-0.25 sync 2-0.25 115.31 us/op 120.51 us/op 0.96
prioritizePeers score 0:0 att 32-0.5 sync 2-0.5 206.38 us/op 192.27 us/op 1.07
prioritizePeers score 0:0 att 64-0.75 sync 4-0.75 418.14 us/op 333.48 us/op 1.25
prioritizePeers score 0:0 att 64-1 sync 4-1 411.57 us/op 406.37 us/op 1.01
RateTracker 1000000 limit, 1 obj count per request 193.19 ns/op 175.90 ns/op 1.10
RateTracker 1000000 limit, 2 obj count per request 142.57 ns/op 128.47 ns/op 1.11
RateTracker 1000000 limit, 4 obj count per request 113.76 ns/op 105.73 ns/op 1.08
RateTracker 1000000 limit, 8 obj count per request 101.23 ns/op 93.291 ns/op 1.09
RateTracker with prune 4.2300 us/op 3.6090 us/op 1.17
array of 16000 items push then shift 51.603 us/op 51.571 us/op 1.00
LinkedList of 16000 items push then shift 16.083 ns/op 14.883 ns/op 1.08
array of 16000 items push then pop 213.27 ns/op 186.90 ns/op 1.14
LinkedList of 16000 items push then pop 14.059 ns/op 13.921 ns/op 1.01
array of 24000 items push then shift 77.343 us/op 77.362 us/op 1.00
LinkedList of 24000 items push then shift 20.027 ns/op 17.843 ns/op 1.12
array of 24000 items push then pop 196.88 ns/op 197.39 ns/op 1.00
LinkedList of 24000 items push then pop 15.948 ns/op 14.711 ns/op 1.08
intersect bitArray bitLen 8 10.720 ns/op 10.817 ns/op 0.99
intersect array and set length 8 146.13 ns/op 124.32 ns/op 1.18
intersect bitArray bitLen 128 57.869 ns/op 57.013 ns/op 1.02
intersect array and set length 128 1.8543 us/op 1.6941 us/op 1.09
Buffer.concat 32 items 2.1280 ns/op 1.8360 ns/op 1.16
pass gossip attestations to forkchoice per slot 2.8512 ms/op 5.3038 ms/op 0.54
computeDeltas 3.6641 ms/op 3.9928 ms/op 0.92
computeProposerBoostScoreFromBalances 803.93 us/op 804.15 us/op 1.00
altair processAttestation - 250000 vs - 7PWei normalcase 3.4830 ms/op 3.3781 ms/op 1.03
altair processAttestation - 250000 vs - 7PWei worstcase 5.1389 ms/op 6.0948 ms/op 0.84
altair processAttestation - setStatus - 1/6 committees join 176.07 us/op 174.97 us/op 1.01
altair processAttestation - setStatus - 1/3 committees join 341.92 us/op 341.51 us/op 1.00
altair processAttestation - setStatus - 1/2 committees join 483.88 us/op 487.10 us/op 0.99
altair processAttestation - setStatus - 2/3 committees join 628.57 us/op 636.35 us/op 0.99
altair processAttestation - setStatus - 4/5 committees join 887.75 us/op 890.66 us/op 1.00
altair processAttestation - setStatus - 100% committees join 1.0688 ms/op 1.0829 ms/op 0.99
altair processBlock - 250000 vs - 7PWei normalcase 25.759 ms/op 24.967 ms/op 1.03
altair processBlock - 250000 vs - 7PWei normalcase hashState 36.906 ms/op 36.522 ms/op 1.01
altair processBlock - 250000 vs - 7PWei worstcase 88.469 ms/op 93.761 ms/op 0.94
altair processBlock - 250000 vs - 7PWei worstcase hashState 112.26 ms/op 97.962 ms/op 1.15
phase0 processBlock - 250000 vs - 7PWei normalcase 4.5442 ms/op 3.6040 ms/op 1.26
phase0 processBlock - 250000 vs - 7PWei worstcase 52.842 ms/op 51.028 ms/op 1.04
altair processEth1Data - 250000 vs - 7PWei normalcase 1.1067 ms/op 656.39 us/op 1.69
Tree 40 250000 create 681.77 ms/op 696.43 ms/op 0.98
Tree 40 250000 get(125000) 227.81 ns/op 225.58 ns/op 1.01
Tree 40 250000 set(125000) 1.9419 us/op 1.9027 us/op 1.02
Tree 40 250000 toArray() 28.408 ms/op 25.869 ms/op 1.10
Tree 40 250000 iterate all - toArray() + loop 30.857 ms/op 28.067 ms/op 1.10
Tree 40 250000 iterate all - get(i) 110.67 ms/op 105.04 ms/op 1.05
MutableVector 250000 create 19.729 ms/op 18.050 ms/op 1.09
MutableVector 250000 get(125000) 10.991 ns/op 12.017 ns/op 0.91
MutableVector 250000 set(125000) 437.02 ns/op 416.58 ns/op 1.05
MutableVector 250000 toArray() 5.5226 ms/op 5.4315 ms/op 1.02
MutableVector 250000 iterate all - toArray() + loop 5.6719 ms/op 5.5249 ms/op 1.03
MutableVector 250000 iterate all - get(i) 2.5995 ms/op 2.7011 ms/op 0.96
Array 250000 create 5.4132 ms/op 5.2677 ms/op 1.03
Array 250000 clone - spread 2.3910 ms/op 2.3400 ms/op 1.02
Array 250000 get(125000) 1.2120 ns/op 1.2050 ns/op 1.01
Array 250000 set(125000) 1.1840 ns/op 1.1940 ns/op 0.99
Array 250000 iterate all - loop 150.91 us/op 152.20 us/op 0.99
effectiveBalanceIncrements clone Uint8Array 300000 65.955 us/op 34.513 us/op 1.91
effectiveBalanceIncrements clone MutableVector 300000 656.00 ns/op 705.00 ns/op 0.93
effectiveBalanceIncrements rw all Uint8Array 300000 247.45 us/op 247.38 us/op 1.00
effectiveBalanceIncrements rw all MutableVector 300000 131.26 ms/op 130.24 ms/op 1.01
phase0 afterProcessEpoch - 250000 vs - 7PWei 186.70 ms/op 206.59 ms/op 0.90
phase0 beforeProcessEpoch - 250000 vs - 7PWei 112.77 ms/op 112.40 ms/op 1.00
altair processEpoch - mainnet_e81889 644.67 ms/op 646.23 ms/op 1.00
mainnet_e81889 - altair beforeProcessEpoch 171.02 ms/op 168.02 ms/op 1.02
mainnet_e81889 - altair processJustificationAndFinalization 16.063 us/op 24.668 us/op 0.65
mainnet_e81889 - altair processInactivityUpdates 9.0414 ms/op 8.5621 ms/op 1.06
mainnet_e81889 - altair processRewardsAndPenalties 160.86 ms/op 155.88 ms/op 1.03
mainnet_e81889 - altair processRegistryUpdates 2.5910 us/op 5.4330 us/op 0.48
mainnet_e81889 - altair processSlashings 626.00 ns/op 1.1170 us/op 0.56
mainnet_e81889 - altair processEth1DataReset 630.00 ns/op 1.2600 us/op 0.50
mainnet_e81889 - altair processEffectiveBalanceUpdates 2.1897 ms/op 2.3195 ms/op 0.94
mainnet_e81889 - altair processSlashingsReset 4.6600 us/op 9.0370 us/op 0.52
mainnet_e81889 - altair processRandaoMixesReset 4.5280 us/op 6.9000 us/op 0.66
mainnet_e81889 - altair processHistoricalRootsUpdate 755.00 ns/op 1.1110 us/op 0.68
mainnet_e81889 - altair processParticipationFlagUpdates 2.7080 us/op 4.0640 us/op 0.67
mainnet_e81889 - altair processSyncCommitteeUpdates 572.00 ns/op 931.00 ns/op 0.61
mainnet_e81889 - altair afterProcessEpoch 195.89 ms/op 199.10 ms/op 0.98
phase0 processEpoch - mainnet_e58758 614.96 ms/op 614.40 ms/op 1.00
mainnet_e58758 - phase0 beforeProcessEpoch 229.93 ms/op 234.00 ms/op 0.98
mainnet_e58758 - phase0 processJustificationAndFinalization 17.001 us/op 28.788 us/op 0.59
mainnet_e58758 - phase0 processRewardsAndPenalties 125.59 ms/op 129.24 ms/op 0.97
mainnet_e58758 - phase0 processRegistryUpdates 7.6550 us/op 14.226 us/op 0.54
mainnet_e58758 - phase0 processSlashings 585.00 ns/op 1.0860 us/op 0.54
mainnet_e58758 - phase0 processEth1DataReset 630.00 ns/op 1.3150 us/op 0.48
mainnet_e58758 - phase0 processEffectiveBalanceUpdates 1.7395 ms/op 1.9476 ms/op 0.89
mainnet_e58758 - phase0 processSlashingsReset 4.0700 us/op 5.0500 us/op 0.81
mainnet_e58758 - phase0 processRandaoMixesReset 3.9640 us/op 8.0330 us/op 0.49
mainnet_e58758 - phase0 processHistoricalRootsUpdate 636.00 ns/op 1.0580 us/op 0.60
mainnet_e58758 - phase0 processParticipationRecordUpdates 3.5640 us/op 6.6870 us/op 0.53
mainnet_e58758 - phase0 afterProcessEpoch 162.53 ms/op 164.26 ms/op 0.99
phase0 processEffectiveBalanceUpdates - 250000 normalcase 1.9579 ms/op 2.0689 ms/op 0.95
phase0 processEffectiveBalanceUpdates - 250000 worstcase 0.5 2.2815 ms/op 2.3892 ms/op 0.95
altair processInactivityUpdates - 250000 normalcase 58.268 ms/op 52.379 ms/op 1.11
altair processInactivityUpdates - 250000 worstcase 54.294 ms/op 57.565 ms/op 0.94
phase0 processRegistryUpdates - 250000 normalcase 5.8520 us/op 11.379 us/op 0.51
phase0 processRegistryUpdates - 250000 badcase_full_deposits 370.51 us/op 542.41 us/op 0.68
phase0 processRegistryUpdates - 250000 worstcase 0.5 221.28 ms/op 225.12 ms/op 0.98
altair processRewardsAndPenalties - 250000 normalcase 128.68 ms/op 132.85 ms/op 0.97
altair processRewardsAndPenalties - 250000 worstcase 139.24 ms/op 140.94 ms/op 0.99
phase0 getAttestationDeltas - 250000 normalcase 11.611 ms/op 11.619 ms/op 1.00
phase0 getAttestationDeltas - 250000 worstcase 11.933 ms/op 11.667 ms/op 1.02
phase0 processSlashings - 250000 worstcase 5.1927 ms/op 5.3269 ms/op 0.97
altair processSyncCommitteeUpdates - 250000 284.76 ms/op 293.35 ms/op 0.97
BeaconState.hashTreeRoot - No change 508.00 ns/op 515.00 ns/op 0.99
BeaconState.hashTreeRoot - 1 full validator 71.399 us/op 70.562 us/op 1.01
BeaconState.hashTreeRoot - 32 full validator 731.52 us/op 817.38 us/op 0.89
BeaconState.hashTreeRoot - 512 full validator 8.5081 ms/op 6.9101 ms/op 1.23
BeaconState.hashTreeRoot - 1 validator.effectiveBalance 103.18 us/op 98.847 us/op 1.04
BeaconState.hashTreeRoot - 32 validator.effectiveBalance 1.2730 ms/op 1.2697 ms/op 1.00
BeaconState.hashTreeRoot - 512 validator.effectiveBalance 18.665 ms/op 16.788 ms/op 1.11
BeaconState.hashTreeRoot - 1 balances 69.898 us/op 68.629 us/op 1.02
BeaconState.hashTreeRoot - 32 balances 1.1548 ms/op 730.24 us/op 1.58
BeaconState.hashTreeRoot - 512 balances 5.8948 ms/op 6.1734 ms/op 0.95
BeaconState.hashTreeRoot - 250000 balances 113.16 ms/op 101.55 ms/op 1.11
aggregationBits - 2048 els - zipIndexesInBitList 24.680 us/op 24.645 us/op 1.00
regular array get 100000 times 60.634 us/op 65.225 us/op 0.93
wrappedArray get 100000 times 60.549 us/op 60.576 us/op 1.00
arrayWithProxy get 100000 times 32.727 ms/op 32.241 ms/op 1.02
ssz.Root.equals 545.00 ns/op 543.00 ns/op 1.00
byteArrayEquals 511.00 ns/op 455.00 ns/op 1.12
shuffle list - 16384 els 11.287 ms/op 11.536 ms/op 0.98
shuffle list - 250000 els 165.90 ms/op 169.06 ms/op 0.98
processSlot - 1 slots 12.987 us/op 12.574 us/op 1.03
processSlot - 32 slots 1.9623 ms/op 1.8759 ms/op 1.05
getEffectiveBalanceIncrementsZeroInactive - 250000 vs - 7PWei 410.35 us/op 309.63 us/op 1.33
getCommitteeAssignments - req 1 vs - 250000 vc 5.4482 ms/op 5.4665 ms/op 1.00
getCommitteeAssignments - req 100 vs - 250000 vc 7.9954 ms/op 8.0717 ms/op 0.99
getCommitteeAssignments - req 1000 vs - 250000 vc 8.5654 ms/op 8.7036 ms/op 0.98
RootCache.getBlockRootAtSlot - 250000 vs - 7PWei 7.4200 ns/op 7.9300 ns/op 0.94
state getBlockRootAtSlot - 250000 vs - 7PWei 965.62 ns/op 972.61 ns/op 0.99
computeProposers - vc 250000 17.121 ms/op 17.347 ms/op 0.99
computeEpochShuffling - vc 250000 170.67 ms/op 173.41 ms/op 0.98
getNextSyncCommittee - vc 250000 284.59 ms/op 287.50 ms/op 0.99

by benchmarkbot/action

Copy link
Contributor

@dapplion dapplion left a comment

Choose a reason for hiding this comment

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

I'm not sure this solution is correct. What about doing this?

Pushed the solution only on config package here https://github.com/ChainSafe/lodestar/compare/cayman/getDomain-alternative?expand=1

  return {
    getDomain(domainType: DomainType, slot: Slot, state?: {slot: Slot}): Uint8Array {
      // ```py
      // def get_domain(state: BeaconState, domain_type: DomainType, epoch: Epoch=None) -> Domain:
      //   """
      //   Return the signature domain (fork version concatenated with domain type) of a message.
      //   """
      //   epoch = get_current_epoch(state) if epoch is None else epoch
      //   fork_version = state.fork.previous_version if epoch < state.fork.epoch else state.fork.current_version
      //   return compute_domain(domain_type, fork_version, state.genesis_validators_root)
      // ```

      // If not state argument is passed, assume requested slot is the state slot
      const stateSlot = state?.slot ?? slot;
      const epoch = Math.floor(slot / SLOTS_PER_EPOCH);
      // Get pre-computed fork schedule, which _should_ match the one in the state
      const forkInfo = chainForkConfig.getForkInfo(stateSlot);
      // Only allow to select either current or previous fork respective of the fork schedule at stateSlot
      const forkName = epoch < forkInfo.epoch ? forkInfo.prevForkName : forkInfo.name;

      let domainByType = domainCache.get(forkName);
      if (!domainByType) {
        domainByType = new Map<DomainType, Uint8Array>();
        domainCache.set(forkInfo.name, domainByType);
      }
      let domain = domainByType.get(domainType);
      if (!domain) {
        domain = computeDomain(domainType, forkInfo.version, genesisValidatorsRoot);
        domainByType.set(domainType, domain);
      }
      return domain;
    },

@wemeetagain wemeetagain changed the title Consider state fork in getDomain Consider state slot in getDomain Aug 17, 2022
@wemeetagain
Copy link
Member Author

refactored similarly to your solution, please rereview

g11tech
g11tech previously approved these changes Aug 19, 2022
Copy link
Contributor

@g11tech g11tech left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -182,7 +182,7 @@ export class ValidatorStore {
// Duties are filtered before-hard by doppelganger-safe, this assert should never throw
this.assertDoppelgangerSafe(pubkey);

const proposerDomain = this.config.getDomain(DOMAIN_BEACON_PROPOSER, blindedOrFull.slot);
const proposerDomain = this.config.getDomain(currentSlot, DOMAIN_BEACON_PROPOSER, blindedOrFull.slot);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this really be the currentSlot? Without checking the specs, sounds it should be the state slot, so block's slot

Copy link
Contributor

Choose a reason for hiding this comment

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

currentslot should be same as blindedorfull slot same as state slot for signing block here

Copy link
Contributor

Choose a reason for hiding this comment

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

should is not enough, this can lead to bad signatures on the fork slot if there are clock issues. There's no solid reason to not use blindedOrFull.slot

Copy link
Contributor

Choose a reason for hiding this comment

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

updated 👍

@g11tech g11tech enabled auto-merge (squash) August 20, 2022 11:11
@g11tech g11tech disabled auto-merge August 20, 2022 11:27
@g11tech g11tech merged commit 104eb54 into unstable Aug 20, 2022
@g11tech g11tech deleted the cayman/getDomain branch August 20, 2022 11:27
@twoeths twoeths mentioned this pull request Aug 21, 2022
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.

Only include voluntaryExit in block for curr or prev forks
3 participants