-
Notifications
You must be signed in to change notification settings - Fork 20.3k
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
rlp: support for uint256 #26898
Merged
Merged
rlp: support for uint256 #26898
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4c5c9b5
rlp: support for uint256
holiman eb8c057
rlp: return error for too large uint256
fjl 56c659e
rlp: fix decoding of non-pointer uint256
fjl 0d92715
rlp: add uint256 tests
fjl 08d450e
rlp: handle encoding u256 both pointer and non-pointer
holiman 5084e85
rlp: implement encoderlp for uint256.Int
holiman 4712879
rlp: benchmark tests for u256
holiman 6f9fcc4
rlp: add EncoderBuffer method for Uint256
fjl c5306d3
rlp: update comment
fjl 87b7fdb
Update rlp/encbuffer.go
holiman b774ecc
rlp: rename to ReadUint256
fjl 6e9d483
rlp/rlpgen: add support for uint256
fjl 0a2fdfb
rlp: add failing test for array encoding
fjl 89298f7
rlp: fix array test
fjl 8a6cbf8
rlp: check for uint256 more strictly
fjl aa376f8
rlp: check for uint256 more strictly in decoder
fjl 4ad1faa
rlp/rlpgen: update uint256 output after renaming of method
holiman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"reflect" | ||
|
||
"github.com/ethereum/go-ethereum/rlp/internal/rlpstruct" | ||
"github.com/holiman/uint256" | ||
) | ||
|
||
var ( | ||
|
@@ -144,6 +145,10 @@ func makeWriter(typ reflect.Type, ts rlpstruct.Tags) (writer, error) { | |
return writeBigIntPtr, nil | ||
case typ.AssignableTo(bigInt): | ||
return writeBigIntNoPtr, nil | ||
case typ.AssignableTo(reflect.PtrTo(u256Int)): | ||
return writeU256IntPtr, nil | ||
case typ.AssignableTo(u256Int): | ||
return writeU256IntNoPtr, nil | ||
case kind == reflect.Ptr: | ||
return makePtrWriter(typ, ts) | ||
case reflect.PtrTo(typ).Implements(encoderInterface): | ||
|
@@ -206,6 +211,22 @@ func writeBigIntNoPtr(val reflect.Value, w *encBuffer) error { | |
return nil | ||
} | ||
|
||
func writeU256IntPtr(val reflect.Value, w *encBuffer) error { | ||
ptr := val.Interface().(*uint256.Int) | ||
if ptr == nil { | ||
w.str = append(w.str, 0x80) | ||
return nil | ||
} | ||
ptr.EncodeRLP(w) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is weird. We shouldn't rely on the method in uint256 here. Instead, we can just put the encoding logic in this package. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, fixed, please check |
||
return nil | ||
} | ||
|
||
func writeU256IntNoPtr(val reflect.Value, w *encBuffer) error { | ||
i := val.Interface().(uint256.Int) | ||
i.EncodeRLP(w) | ||
return nil | ||
} | ||
|
||
func writeBytes(val reflect.Value, w *encBuffer) error { | ||
w.writeBytes(val.Bytes()) | ||
return nil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 there is a problem with this check. As written, this will also trigger for values of type
[4]uint64
, because those values are also 'assignable' to uint256.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.
Hm. Ouch
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.
We check for big.Int in the same way, but it's less of a problem because (1) big.Int is a struct with private fields, and (2) it is usually handled as a pointer, and named types can't be pointers (
type X *big.Int
is not allowed by compiler).