-
Notifications
You must be signed in to change notification settings - Fork 446
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
Conversation
There was a problem hiding this 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
validator/server_api/json.go
Outdated
|
||
"github.com/ethereum/go-ethereum/common" | ||
|
||
"github.com/offchainlabs/nitro/validator" | ||
) | ||
|
||
type PreimagesMapJson struct { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
validator/server_api/json.go
Outdated
encoding := base64.StdEncoding | ||
for key, value := range m.Map { | ||
size += 5 // "":"" | ||
size += encoding.EncodedLen(len(key)) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
validator/server_api/json.go
Outdated
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]) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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
I'm not sure this is a good idea but it definitely reduces allocations.