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

msgIdToStrFn: support Uint8Array #5240

Merged
merged 1 commit into from
Mar 7, 2023
Merged

Conversation

twoeths
Copy link
Contributor

@twoeths twoeths commented Mar 7, 2023

Motivation

  • Nimbus sent responded IWANT messages, however Lodestar missed some of them
  • That's due to this untracked gossipsub rpc error:
Mar-06 11:50:37.088[network]         ^[[34mdebug^[[39m: 2023-03-06T11:50:37.088Z libp2p:gossipsub TypeError: buf.hexSlice is not a function
    at Object.slice (node:buffer:675:37)
    at Uint8Array.toString (node:buffer:818:14)
    at Eth2Gossipsub.msgIdToStrFn (file:///usr/src/lodestar/packages/beacon-node/src/network/gossip/encoding.ts:31:36)
    at file:///usr/src/lodestar/node_modules/@chainsafe/libp2p-gossipsub/src/index.ts:1354:31
    at Array.forEach (<anonymous>)
    at file:///usr/src/lodestar/node_modules/@chainsafe/libp2p-gossipsub/src/index.ts:1353:18
    at Array.forEach (<anonymous>)
    at Eth2Gossipsub.handleIHave (file:///usr/src/lodestar/node_modules/@chainsafe/libp2p-gossipsub/src/index.ts:1346:11)
    at Eth2Gossipsub.handleControlMessage (file:///usr/src/lodestar/node_modules/@chainsafe/libp2p-gossipsub/src/index.ts:1264:43)
    at Eth2Gossipsub.handleReceivedRpc (file:///usr/src/lodestar/node_modules/@chainsafe/libp2p-gossipsub/src/index.ts:1045:18)

Screen Shot 2023-03-07 at 13 08 18

Description

  • Right now we assume message id is Buffer but sometimes it may be Uint8Array and it causes the above error
  • Use our toHex() utility which support both Buffer and Uint8Array

part of #5074

part of ChainSafe/js-libp2p-gossipsub#411

@twoeths twoeths requested a review from a team as a code owner March 7, 2023 06:10
@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2023

Performance Report

✔️ no performance regression detected

Full benchmark results
Benchmark suite Current: 744fef4 Previous: 0006d86 Ratio
getPubkeys - index2pubkey - req 1000 vs - 250000 vc 746.63 us/op 977.24 us/op 0.76
getPubkeys - validatorsArr - req 1000 vs - 250000 vc 45.754 us/op 48.576 us/op 0.94
BLS verify - blst-native 1.2301 ms/op 1.2222 ms/op 1.01
BLS verifyMultipleSignatures 3 - blst-native 2.4987 ms/op 2.5631 ms/op 0.97
BLS verifyMultipleSignatures 8 - blst-native 5.4395 ms/op 5.4137 ms/op 1.00
BLS verifyMultipleSignatures 32 - blst-native 19.407 ms/op 19.301 ms/op 1.01
BLS aggregatePubkeys 32 - blst-native 26.174 us/op 26.628 us/op 0.98
BLS aggregatePubkeys 128 - blst-native 101.57 us/op 102.86 us/op 0.99
getAttestationsForBlock 60.422 ms/op 55.975 ms/op 1.08
isKnown best case - 1 super set check 262.00 ns/op 277.00 ns/op 0.95
isKnown normal case - 2 super set checks 250.00 ns/op 263.00 ns/op 0.95
isKnown worse case - 16 super set checks 244.00 ns/op 261.00 ns/op 0.93
CheckpointStateCache - add get delete 5.0080 us/op 5.5290 us/op 0.91
validate gossip signedAggregateAndProof - struct 2.7641 ms/op 2.7986 ms/op 0.99
validate gossip attestation - struct 1.3289 ms/op 1.3283 ms/op 1.00
pickEth1Vote - no votes 1.3163 ms/op 1.3558 ms/op 0.97
pickEth1Vote - max votes 9.7535 ms/op 9.9731 ms/op 0.98
pickEth1Vote - Eth1Data hashTreeRoot value x2048 8.5371 ms/op 9.2057 ms/op 0.93
pickEth1Vote - Eth1Data hashTreeRoot tree x2048 13.716 ms/op 15.419 ms/op 0.89
pickEth1Vote - Eth1Data fastSerialize value x2048 701.25 us/op 706.13 us/op 0.99
pickEth1Vote - Eth1Data fastSerialize tree x2048 5.7849 ms/op 7.0876 ms/op 0.82
bytes32 toHexString 677.00 ns/op 578.00 ns/op 1.17
bytes32 Buffer.toString(hex) 420.00 ns/op 414.00 ns/op 1.01
bytes32 Buffer.toString(hex) from Uint8Array 641.00 ns/op 641.00 ns/op 1.00
bytes32 Buffer.toString(hex) + 0x 364.00 ns/op 415.00 ns/op 0.88
Object access 1 prop 0.17500 ns/op 0.19200 ns/op 0.91
Map access 1 prop 0.16500 ns/op 0.16900 ns/op 0.98
Object get x1000 6.5460 ns/op 6.7450 ns/op 0.97
Map get x1000 0.63100 ns/op 0.66000 ns/op 0.96
Object set x1000 53.294 ns/op 71.650 ns/op 0.74
Map set x1000 46.500 ns/op 57.524 ns/op 0.81
Return object 10000 times 0.23750 ns/op 0.26850 ns/op 0.88
Throw Error 10000 times 4.1998 us/op 4.5115 us/op 0.93
fastMsgIdFn sha256 / 200 bytes 3.4270 us/op 3.5780 us/op 0.96
fastMsgIdFn h32 xxhash / 200 bytes 277.00 ns/op 328.00 ns/op 0.84
fastMsgIdFn h64 xxhash / 200 bytes 400.00 ns/op 492.00 ns/op 0.81
fastMsgIdFn sha256 / 1000 bytes 11.680 us/op 11.973 us/op 0.98
fastMsgIdFn h32 xxhash / 1000 bytes 407.00 ns/op 465.00 ns/op 0.88
fastMsgIdFn h64 xxhash / 1000 bytes 471.00 ns/op 555.00 ns/op 0.85
fastMsgIdFn sha256 / 10000 bytes 104.35 us/op 105.85 us/op 0.99
fastMsgIdFn h32 xxhash / 10000 bytes 1.9480 us/op 2.0250 us/op 0.96
fastMsgIdFn h64 xxhash / 10000 bytes 1.4460 us/op 1.3940 us/op 1.04
enrSubnets - fastDeserialize 64 bits 1.3800 us/op 1.7800 us/op 0.78
enrSubnets - ssz BitVector 64 bits 515.00 ns/op 608.00 ns/op 0.85
enrSubnets - fastDeserialize 4 bits 183.00 ns/op 218.00 ns/op 0.84
enrSubnets - ssz BitVector 4 bits 502.00 ns/op 631.00 ns/op 0.80
prioritizePeers score -10:0 att 32-0.1 sync 2-0 103.11 us/op 128.02 us/op 0.81
prioritizePeers score 0:0 att 32-0.25 sync 2-0.25 132.99 us/op 163.37 us/op 0.81
prioritizePeers score 0:0 att 32-0.5 sync 2-0.5 182.43 us/op 201.53 us/op 0.91
prioritizePeers score 0:0 att 64-0.75 sync 4-0.75 312.58 us/op 377.51 us/op 0.83
prioritizePeers score 0:0 att 64-1 sync 4-1 387.59 us/op 441.80 us/op 0.88
array of 16000 items push then shift 1.5706 us/op 1.6949 us/op 0.93
LinkedList of 16000 items push then shift 8.3800 ns/op 9.4130 ns/op 0.89
array of 16000 items push then pop 89.148 ns/op 117.21 ns/op 0.76
LinkedList of 16000 items push then pop 8.7280 ns/op 9.2910 ns/op 0.94
array of 24000 items push then shift 2.3798 us/op 2.4132 us/op 0.99
LinkedList of 24000 items push then shift 8.7210 ns/op 9.2620 ns/op 0.94
array of 24000 items push then pop 74.183 ns/op 87.759 ns/op 0.85
LinkedList of 24000 items push then pop 8.5310 ns/op 9.1510 ns/op 0.93
intersect bitArray bitLen 8 13.291 ns/op 13.484 ns/op 0.99
intersect array and set length 8 79.105 ns/op 87.301 ns/op 0.91
intersect bitArray bitLen 128 43.921 ns/op 45.157 ns/op 0.97
intersect array and set length 128 1.0605 us/op 1.3000 us/op 0.82
Buffer.concat 32 items 2.6580 us/op 2.7830 us/op 0.96
Uint8Array.set 32 items 2.9100 us/op 2.4930 us/op 1.17
pass gossip attestations to forkchoice per slot 2.3649 ms/op 3.7523 ms/op 0.63
computeDeltas 3.4860 ms/op 2.9915 ms/op 1.17
computeProposerBoostScoreFromBalances 1.7910 ms/op 1.7865 ms/op 1.00
altair processAttestation - 250000 vs - 7PWei normalcase 2.1448 ms/op 2.3342 ms/op 0.92
altair processAttestation - 250000 vs - 7PWei worstcase 3.2649 ms/op 3.5442 ms/op 0.92
altair processAttestation - setStatus - 1/6 committees join 137.66 us/op 142.23 us/op 0.97
altair processAttestation - setStatus - 1/3 committees join 265.18 us/op 275.01 us/op 0.96
altair processAttestation - setStatus - 1/2 committees join 354.96 us/op 364.40 us/op 0.97
altair processAttestation - setStatus - 2/3 committees join 462.54 us/op 459.51 us/op 1.01
altair processAttestation - setStatus - 4/5 committees join 637.74 us/op 656.17 us/op 0.97
altair processAttestation - setStatus - 100% committees join 720.09 us/op 782.18 us/op 0.92
altair processBlock - 250000 vs - 7PWei normalcase 19.360 ms/op 18.451 ms/op 1.05
altair processBlock - 250000 vs - 7PWei normalcase hashState 26.426 ms/op 28.528 ms/op 0.93
altair processBlock - 250000 vs - 7PWei worstcase 51.735 ms/op 48.393 ms/op 1.07
altair processBlock - 250000 vs - 7PWei worstcase hashState 67.788 ms/op 67.603 ms/op 1.00
phase0 processBlock - 250000 vs - 7PWei normalcase 2.0119 ms/op 2.1219 ms/op 0.95
phase0 processBlock - 250000 vs - 7PWei worstcase 27.303 ms/op 29.903 ms/op 0.91
altair processEth1Data - 250000 vs - 7PWei normalcase 467.29 us/op 490.05 us/op 0.95
vc - 250000 eb 1 eth1 1 we 0 wn 0 - smpl 15 7.2430 us/op 9.0500 us/op 0.80
vc - 250000 eb 0.95 eth1 0.1 we 0.05 wn 0 - smpl 219 22.707 us/op 28.768 us/op 0.79
vc - 250000 eb 0.95 eth1 0.3 we 0.05 wn 0 - smpl 42 8.5440 us/op 11.078 us/op 0.77
vc - 250000 eb 0.95 eth1 0.7 we 0.05 wn 0 - smpl 18 6.3950 us/op 8.4970 us/op 0.75
vc - 250000 eb 0.1 eth1 0.1 we 0 wn 0 - smpl 1020 85.023 us/op 109.89 us/op 0.77
vc - 250000 eb 0.03 eth1 0.03 we 0 wn 0 - smpl 11777 655.14 us/op 665.89 us/op 0.98
vc - 250000 eb 0.01 eth1 0.01 we 0 wn 0 - smpl 16384 903.64 us/op 942.32 us/op 0.96
vc - 250000 eb 0 eth1 0 we 0 wn 0 - smpl 16384 878.03 us/op 970.49 us/op 0.90
vc - 250000 eb 0 eth1 0 we 0 wn 0 nocache - smpl 16384 2.3150 ms/op 2.6445 ms/op 0.88
vc - 250000 eb 0 eth1 1 we 0 wn 0 - smpl 16384 1.5326 ms/op 1.5308 ms/op 1.00
vc - 250000 eb 0 eth1 1 we 0 wn 0 nocache - smpl 16384 3.8499 ms/op 4.2452 ms/op 0.91
Tree 40 250000 create 297.20 ms/op 343.44 ms/op 0.87
Tree 40 250000 get(125000) 183.97 ns/op 195.21 ns/op 0.94
Tree 40 250000 set(125000) 949.47 ns/op 1.0777 us/op 0.88
Tree 40 250000 toArray() 16.977 ms/op 22.473 ms/op 0.76
Tree 40 250000 iterate all - toArray() + loop 17.425 ms/op 24.743 ms/op 0.70
Tree 40 250000 iterate all - get(i) 66.921 ms/op 74.992 ms/op 0.89
MutableVector 250000 create 9.5553 ms/op 11.726 ms/op 0.81
MutableVector 250000 get(125000) 6.2600 ns/op 6.9530 ns/op 0.90
MutableVector 250000 set(125000) 249.71 ns/op 321.35 ns/op 0.78
MutableVector 250000 toArray() 2.6390 ms/op 3.6745 ms/op 0.72
MutableVector 250000 iterate all - toArray() + loop 2.7525 ms/op 3.5484 ms/op 0.78
MutableVector 250000 iterate all - get(i) 1.5016 ms/op 1.5349 ms/op 0.98
Array 250000 create 2.4385 ms/op 2.7636 ms/op 0.88
Array 250000 clone - spread 1.1620 ms/op 1.3095 ms/op 0.89
Array 250000 get(125000) 0.56800 ns/op 0.67600 ns/op 0.84
Array 250000 set(125000) 0.65500 ns/op 0.72900 ns/op 0.90
Array 250000 iterate all - loop 82.013 us/op 106.52 us/op 0.77
effectiveBalanceIncrements clone Uint8Array 300000 25.188 us/op 41.787 us/op 0.60
effectiveBalanceIncrements clone MutableVector 300000 378.00 ns/op 447.00 ns/op 0.85
effectiveBalanceIncrements rw all Uint8Array 300000 169.78 us/op 170.98 us/op 0.99
effectiveBalanceIncrements rw all MutableVector 300000 82.188 ms/op 100.96 ms/op 0.81
phase0 afterProcessEpoch - 250000 vs - 7PWei 113.00 ms/op 117.96 ms/op 0.96
phase0 beforeProcessEpoch - 250000 vs - 7PWei 35.299 ms/op 41.089 ms/op 0.86
altair processEpoch - mainnet_e81889 320.56 ms/op 339.58 ms/op 0.94
mainnet_e81889 - altair beforeProcessEpoch 60.378 ms/op 68.500 ms/op 0.88
mainnet_e81889 - altair processJustificationAndFinalization 16.257 us/op 18.170 us/op 0.89
mainnet_e81889 - altair processInactivityUpdates 5.6349 ms/op 6.1876 ms/op 0.91
mainnet_e81889 - altair processRewardsAndPenalties 51.613 ms/op 71.157 ms/op 0.73
mainnet_e81889 - altair processRegistryUpdates 2.8770 us/op 4.0750 us/op 0.71
mainnet_e81889 - altair processSlashings 553.00 ns/op 554.00 ns/op 1.00
mainnet_e81889 - altair processEth1DataReset 547.00 ns/op 981.00 ns/op 0.56
mainnet_e81889 - altair processEffectiveBalanceUpdates 1.2063 ms/op 1.2539 ms/op 0.96
mainnet_e81889 - altair processSlashingsReset 4.5200 us/op 4.9190 us/op 0.92
mainnet_e81889 - altair processRandaoMixesReset 4.7500 us/op 4.3750 us/op 1.09
mainnet_e81889 - altair processHistoricalRootsUpdate 955.00 ns/op 618.00 ns/op 1.55
mainnet_e81889 - altair processParticipationFlagUpdates 2.1600 us/op 2.5290 us/op 0.85
mainnet_e81889 - altair processSyncCommitteeUpdates 477.00 ns/op 1.1520 us/op 0.41
mainnet_e81889 - altair afterProcessEpoch 127.87 ms/op 130.13 ms/op 0.98
phase0 processEpoch - mainnet_e58758 356.57 ms/op 369.78 ms/op 0.96
mainnet_e58758 - phase0 beforeProcessEpoch 132.96 ms/op 130.27 ms/op 1.02
mainnet_e58758 - phase0 processJustificationAndFinalization 27.378 us/op 19.665 us/op 1.39
mainnet_e58758 - phase0 processRewardsAndPenalties 63.557 ms/op 63.483 ms/op 1.00
mainnet_e58758 - phase0 processRegistryUpdates 7.5940 us/op 7.8840 us/op 0.96
mainnet_e58758 - phase0 processSlashings 519.00 ns/op 539.00 ns/op 0.96
mainnet_e58758 - phase0 processEth1DataReset 524.00 ns/op 514.00 ns/op 1.02
mainnet_e58758 - phase0 processEffectiveBalanceUpdates 1.0140 ms/op 996.57 us/op 1.02
mainnet_e58758 - phase0 processSlashingsReset 3.9670 us/op 3.4340 us/op 1.16
mainnet_e58758 - phase0 processRandaoMixesReset 4.3140 us/op 4.1750 us/op 1.03
mainnet_e58758 - phase0 processHistoricalRootsUpdate 624.00 ns/op 581.00 ns/op 1.07
mainnet_e58758 - phase0 processParticipationRecordUpdates 3.8470 us/op 4.1920 us/op 0.92
mainnet_e58758 - phase0 afterProcessEpoch 98.280 ms/op 96.457 ms/op 1.02
phase0 processEffectiveBalanceUpdates - 250000 normalcase 1.2338 ms/op 1.2226 ms/op 1.01
phase0 processEffectiveBalanceUpdates - 250000 worstcase 0.5 1.5171 ms/op 1.4551 ms/op 1.04
altair processInactivityUpdates - 250000 normalcase 23.835 ms/op 20.577 ms/op 1.16
altair processInactivityUpdates - 250000 worstcase 26.252 ms/op 26.158 ms/op 1.00
phase0 processRegistryUpdates - 250000 normalcase 6.8800 us/op 6.7450 us/op 1.02
phase0 processRegistryUpdates - 250000 badcase_full_deposits 253.81 us/op 235.52 us/op 1.08
phase0 processRegistryUpdates - 250000 worstcase 0.5 107.16 ms/op 127.66 ms/op 0.84
altair processRewardsAndPenalties - 250000 normalcase 63.684 ms/op 70.009 ms/op 0.91
altair processRewardsAndPenalties - 250000 worstcase 67.534 ms/op 70.226 ms/op 0.96
phase0 getAttestationDeltas - 250000 normalcase 6.2312 ms/op 6.5250 ms/op 0.95
phase0 getAttestationDeltas - 250000 worstcase 6.4776 ms/op 6.5553 ms/op 0.99
phase0 processSlashings - 250000 worstcase 3.1964 ms/op 3.3040 ms/op 0.97
altair processSyncCommitteeUpdates - 250000 169.73 ms/op 177.18 ms/op 0.96
BeaconState.hashTreeRoot - No change 262.00 ns/op 275.00 ns/op 0.95
BeaconState.hashTreeRoot - 1 full validator 51.324 us/op 58.420 us/op 0.88
BeaconState.hashTreeRoot - 32 full validator 495.62 us/op 517.82 us/op 0.96
BeaconState.hashTreeRoot - 512 full validator 5.2972 ms/op 4.9662 ms/op 1.07
BeaconState.hashTreeRoot - 1 validator.effectiveBalance 58.785 us/op 63.203 us/op 0.93
BeaconState.hashTreeRoot - 32 validator.effectiveBalance 932.58 us/op 877.15 us/op 1.06
BeaconState.hashTreeRoot - 512 validator.effectiveBalance 10.973 ms/op 10.866 ms/op 1.01
BeaconState.hashTreeRoot - 1 balances 46.230 us/op 48.091 us/op 0.96
BeaconState.hashTreeRoot - 32 balances 428.60 us/op 480.51 us/op 0.89
BeaconState.hashTreeRoot - 512 balances 4.2627 ms/op 4.4279 ms/op 0.96
BeaconState.hashTreeRoot - 250000 balances 74.956 ms/op 72.324 ms/op 1.04
aggregationBits - 2048 els - zipIndexesInBitList 14.977 us/op 16.452 us/op 0.91
regular array get 100000 times 32.339 us/op 40.821 us/op 0.79
wrappedArray get 100000 times 32.358 us/op 44.126 us/op 0.73
arrayWithProxy get 100000 times 15.828 ms/op 15.684 ms/op 1.01
ssz.Root.equals 528.00 ns/op 536.00 ns/op 0.99
byteArrayEquals 523.00 ns/op 535.00 ns/op 0.98
shuffle list - 16384 els 6.7424 ms/op 6.8388 ms/op 0.99
shuffle list - 250000 els 99.023 ms/op 100.83 ms/op 0.98
processSlot - 1 slots 8.4940 us/op 9.1690 us/op 0.93
processSlot - 32 slots 1.2839 ms/op 1.3175 ms/op 0.97
getEffectiveBalanceIncrementsZeroInactive - 250000 vs - 7PWei 184.22 us/op 189.62 us/op 0.97
getCommitteeAssignments - req 1 vs - 250000 vc 2.7925 ms/op 2.8053 ms/op 1.00
getCommitteeAssignments - req 100 vs - 250000 vc 4.0806 ms/op 3.9907 ms/op 1.02
getCommitteeAssignments - req 1000 vs - 250000 vc 4.3956 ms/op 4.3849 ms/op 1.00
RootCache.getBlockRootAtSlot - 250000 vs - 7PWei 4.6200 ns/op 4.7500 ns/op 0.97
state getBlockRootAtSlot - 250000 vs - 7PWei 951.50 ns/op 937.51 ns/op 1.01
computeProposers - vc 250000 10.451 ms/op 10.233 ms/op 1.02
computeEpochShuffling - vc 250000 101.89 ms/op 102.70 ms/op 0.99
getNextSyncCommittee - vc 250000 169.31 ms/op 174.76 ms/op 0.97

by benchmarkbot/action

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.

👍

@@ -27,8 +27,7 @@ export function fastMsgIdFn(rpcMsg: RPC.IMessage): string {
}

export function msgIdToStrFn(msgId: Uint8Array): string {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
return Buffer.prototype.toString.call(msgId, "base64");
return toHex(msgId);
Copy link
Member

Choose a reason for hiding this comment

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

are there any potential side-effects or considerations changing the return value from base64 to hex?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

previously we choosed base64 probably because of the memory efficiency, but it's an issue with Uint8Array anyway. Switching to hex helps us:

  • fix the issue with Uint8Array by using toHex function
  • have same message id format to other clients which make it easier to debug, for example Nimbus

I think the memory difference is no much

Copy link
Member

Choose a reason for hiding this comment

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

sounds good to me, thanks for explaining 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

toHex util uses Buffer toString which is memory efficient too. Prefixing with "'0x" adds non-trivial unnecessary memory, but it's not that bad, and all hex strings are assumed to be prefixed so

@wemeetagain wemeetagain merged commit a88c5b2 into unstable Mar 7, 2023
@wemeetagain wemeetagain deleted the tuyen/fix_msgIdToStrFn branch March 7, 2023 14:04
@wemeetagain
Copy link
Member

🎉 This PR is included in v1.7.0 🎉

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.

5 participants