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

Handroll validation input preimages JSON marshal/unmarshal #1734

Merged
merged 6 commits into from
Jul 10, 2023

Conversation

PlasmaPower
Copy link
Collaborator

I'm not sure this is a good idea but it definitely reduces allocations.

@PlasmaPower PlasmaPower requested a review from tsahee July 1, 2023 23:09
@cla-bot cla-bot bot added the s Automatically added by the CLA bot if the creator of a PR is registered as having signed the CLA. label Jul 1, 2023
tsahee
tsahee previously approved these changes Jul 7, 2023
Copy link
Contributor

@tsahee tsahee left a comment

Choose a reason for hiding this comment

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

seems good, some optional comments


"github.com/ethereum/go-ethereum/common"

"github.com/offchainlabs/nitro/validator"
)

type PreimagesMapJson struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

the same will also be used in execution-api. Let's put it somewhere else, like under /util/, to be used by both.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've moved validator/server_api/json.go to util/jsonapi/validation.go. Let me know if I should do the split differently.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think only PreimagesMapJson should be there. This will be used by multiple APIs, but the rest of this file seems validation-api specific.

encoding := base64.StdEncoding
for key, value := range m.Map {
size += 5 // "":""
size += encoding.EncodedLen(len(key))
Copy link
Contributor

Choose a reason for hiding this comment

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

can have a var/const that has the encodelen of a common.Hash and use it instead of calling EncodeLen over and over.
Also - since len of comas is calculated outside this loop - adding 5 + encLen(32) per item outside this loop makes sense

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've moved the calculation out of the loop, but I don't think we need to pull encoding.EncodedLen(32) out into a global because it's just (n + 2) / 3 * 4

if maxKeyLen > len(buf) {
return fmt.Errorf("preimage key base64 possible length %v is greater than buffer size of %v", maxKeyLen, len(buf))
}
keyLen, err := encoding.Decode(buf, data[:strLen])
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to decode into buf.
You can error-out if maxKeyLen > len(key) and decode directly into key[:] avoiding the copy.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unfortunately maxKeyLen will necessarily be greater than len(key) because of base64 padding. encoding.DecodedLen will always return a multiple of 3, for keys that'll be 33 bytes which is unfortunately too long to safely decode directly into key with.

tsahee
tsahee previously approved these changes Jul 10, 2023
Copy link
Contributor

@tsahee tsahee left a comment

Choose a reason for hiding this comment

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

LGTM, though I would only keep PreimagesMapJson in jsonapi and would keep the rest in validation

@tsahee tsahee merged commit b389154 into master Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s Automatically added by the CLA bot if the creator of a PR is registered as having signed the CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants