Skip to content

Hex encode graffiti in transit when calling produce block APIs - #5109

Merged
wemeetagain merged 5 commits into
ChainSafe:unstablefrom
nflaig:hex-encode-graffiti-in-transit
Feb 16, 2023
Merged

Hex encode graffiti in transit when calling produce block APIs#5109
wemeetagain merged 5 commits into
ChainSafe:unstablefrom
nflaig:hex-encode-graffiti-in-transit

Conversation

@nflaig

@nflaig nflaig commented Feb 6, 2023

Copy link
Copy Markdown
Member

Motivation

According to beacon node API specification the graffiti should be a hex encoded string, see produceBlockV2 and have a length of 66 characters.

Issue highlighted by @dB2510 in discord.

Description

Hex encodes graffiti when writing produceBlock request and decodes it when parsing it on the beacon node. The functions for converting a uft8 string to hex and vice-versa are browser compatible.

If the graffiti is too long, it removes characters from the end and if it is too short, the hex value will be right-padded with zeros.

Could consider to explicitly fail validator on start if configured graffiti is too long instead of implicitly trimming it but should be discussed first if we want this behavior.

@nflaig
nflaig requested a review from a team as a code owner February 6, 2023 12:45
@nflaig
nflaig force-pushed the hex-encode-graffiti-in-transit branch 3 times, most recently from b186523 to 4c67741 Compare February 6, 2023 16:08
@nflaig
nflaig force-pushed the hex-encode-graffiti-in-transit branch from 4c67741 to 2623842 Compare February 6, 2023 17:34
const ZERO_HASH_HEX = "0x" + ZERO_HASH.toString("hex");
const randaoReveal = Buffer.alloc(96, 1);
const graffiti = "0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2";
const graffiti = "a".repeat(32);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why is this changed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2 this value did not make too much sense in the first place, it is from the produceBlockV2 which makes sense there becasue it is is the value which should be passed to the beacon API but in our case it should be the actual validator graffiti value which is a UFT-8 string and not hex encoded.

The runGenericServerTest function currently uses the args as input to the api client and it uses that same value to check if it was received by the mock api. This means at the moment we generally assume we sent some value do some kind of encoding in writeReq and then decode in parseReq without modyfing the value.

The problem is that toGraffitiHex will trim or pad the value depending on its length, meaning the graffiti needs to be exactly 32 bytes for the test to pass.

Comment thread packages/api/src/utils/serdes.ts Outdated
Comment thread packages/api/src/utils/serdes.ts
Comment thread packages/api/src/utils/serdes.ts
@nflaig
nflaig requested a review from wemeetagain February 11, 2023 14:27
wemeetagain
wemeetagain previously approved these changes Feb 12, 2023
Comment thread packages/api/src/utils/serdes.ts Outdated
g11tech
g11tech previously approved these changes Feb 12, 2023
@philknows philknows added the meta-breaking-change Introduces breaking changes to DB, Validator, Beacon Node, or CLI interfaces. Handle with care! label Feb 12, 2023
@philknows philknows added this to the v1.5.0 milestone Feb 12, 2023
Providing an invalid graffiti hex string to the beacon node API would throw an error.
To prevent errors when proposing a block we are replacing the malformed graffiti
with an empty one.
@nflaig
nflaig dismissed stale reviews from g11tech and wemeetagain via 4eadd1b February 14, 2023 07:09
@nflaig
nflaig requested review from g11tech and wemeetagain and removed request for g11tech and wemeetagain February 14, 2023 07:20
@nflaig
nflaig force-pushed the hex-encode-graffiti-in-transit branch from 55e6799 to 2243338 Compare February 14, 2023 08:41
wemeetagain
wemeetagain previously approved these changes Feb 14, 2023

@wemeetagain wemeetagain left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, will wait for another ack for this one before merging

@g11tech g11tech left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we add unit tests for to/from graffitiHex for normal and special character strings?

@g11tech

g11tech commented Feb 16, 2023

Copy link
Copy Markdown
Contributor

also check if we require similar change in keymanager api / external signer api

@nflaig

nflaig commented Feb 16, 2023

Copy link
Copy Markdown
Member Author

can we add unit tests for to/from graffitiHex for normal and special character strings?

added some unit test, those should cover all edge cases but let me know if I missed something

@nflaig

nflaig commented Feb 16, 2023

Copy link
Copy Markdown
Member Author

also check if we require similar change in keymanager api / external signer api

did not find any references to the graffiti in the keymanager api spec or any other place in the code where we should apply graffiti encoding/decoding

@wemeetagain
wemeetagain merged commit 92ab83d into ChainSafe:unstable Feb 16, 2023
@nflaig nflaig modified the milestones: v1.5.0, v1.6.0 Mar 4, 2023
@wemeetagain

Copy link
Copy Markdown
Member

馃帀 This PR is included in v1.6.0 馃帀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

meta-breaking-change Introduces breaking changes to DB, Validator, Beacon Node, or CLI interfaces. Handle with care!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants