accounts/abi: reorganizing package with small fixes#14610
Conversation
|
Thank you for your contribution! Your commits seem to not adhere to the repository coding standards
Please check the contribution guidelines for more details. This message was auto-generated by https://gitcop.com |
4657848 to
1bb38aa
Compare
…ion of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io>
1bb38aa to
a52e1b4
Compare
| @@ -0,0 +1,220 @@ | |||
| // Copyright 2015 The go-ethereum Authors | |||
There was a problem hiding this comment.
Please call this file unpack.go.
There was a problem hiding this comment.
Ah I see you made it like this because of packing.go. Please change that too to pack.go.
| } | ||
| } | ||
|
|
||
| // todo: this is inefficient for a bool, just look at the last cell, save yourself 32 iterations |
There was a problem hiding this comment.
Then I guess the proper fix would be a fix instead of a comment ;) Looking at the abi package it seems allZero is only ever used to check if a boolean from a contract is true or false. Perhaps we could get rid of this method altogether?
Not sure of the implications though. Would be nice to have a test suite to validate such a change and whether it breaks anything. What happens if I call a transaction with a bool parameter but set higher order bytes?
There was a problem hiding this comment.
If you call a transaction with a bool parameter but set higher order bytes you've broken the ABI encoding :p
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
|
@karalabe So I managed to "fix" the boolean function as well. After thinking about what you said, I decided that it is best to iterate through the entire thing if only to make sure that there are no higher order bits being set (and if there is, throw an error). I think the way it's set up now is better. If we want to get REALLY anal about it we can put a switch case for uints to equal 1, 0 or fail. |
Signed-off-by: RJ Catalano <rj@monax.io>
|
on second thought, better to be anal than regretful later. |
| case 1: | ||
| return true, nil | ||
| default: | ||
| return false, fmt.Errorf("abi: Improperly encoded boolean value.") |
There was a problem hiding this comment.
Please don't capitalize error messages and don't add a period at the end. Errors in Go tend to get sometimes concatenated (e.g. if err != nil { return fmt.Errorf("something went wrong: %v", err) }, where capitalization and trailing periods make it weird.
There was a problem hiding this comment.
Interesting. Will change.
| return false, fmt.Errorf("abi: Improperly encoded boolean value.") | ||
| } | ||
| } | ||
| switch uint(b[31]) { |
There was a problem hiding this comment.
There's no reason to convert this to uint
There was a problem hiding this comment.
Yea you're right. Forgot that its a uint8 at the end of the day.
| } | ||
|
|
||
| func getBoolValue(b []byte) (bool, error) { | ||
| for i, byte := range b { |
There was a problem hiding this comment.
Please don't use byte as a variable, it's a type name. It makes reading code a lot more confusing. Perhaps name the original input b as word and then use b as the iterator.
|
|
||
| func getBoolValue(b []byte) (bool, error) { | ||
| for i, byte := range b { | ||
| if byte != 0 && i != 31 { |
There was a problem hiding this comment.
Are you sure the input will contain exactly 32 bytes? This code is quite dangerous because the code does strange logic with the 32. byte, but nowhere is it checked to ensure the 32. byte even exists or whether a 33s exists too. For such logic you'd also need to have a check at the top to ensure the input is 32 bytes long.
There was a problem hiding this comment.
With the direction i intend to take this in i can tell you i am splitting the entire set of words into a map of ints to 32 byte words which would then ensure the length and which would nestle this in nicely. But what to do in the interim...
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
…e for integers Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
…d files. Signed-off-by: RJ Catalano <rj@monax.io>
| input interface{} | ||
| output []byte | ||
| }{ | ||
| {"uint16", uint16(2), pad([]byte{2}, 32, true)}, |
There was a problem hiding this comment.
Please use hex strings for input and output in all tests. IMHO pad(...) etc. are hard to read.
There was a problem hiding this comment.
The problem with hex strings is counting the length and im always unsure whether im putting in the proper length of characters.
There was a problem hiding this comment.
I have a better idea, we should use a better function that takes in a hex string of varying length, converts it to bytes, pads it to a word and returns the byte slice.
Also. We need a better way of doing the sliceFormatting...it's currently ridiculously confusing to read.
There was a problem hiding this comment.
Or better yet, why not just simplify pad() down to two variables, that being input for the bytes input and left for whether its left padded or not ... because currently its a pain to write out the entire hex string and do it correctly for each of the types.
There was a problem hiding this comment.
perhaps even throw in a function type in there to append multiple pad statements...hrmmmm....
| improperEncoding := "abi: improperly encoded boolean value" | ||
| for i, b := range word { | ||
| if b != 0 && i != 31 { | ||
| return false, fmt.Errorf(improperEncoding) |
There was a problem hiding this comment.
Please define errBadBool at package level and use it instead of creating a new error each time.
There was a problem hiding this comment.
I don't understand how this benefits the reader of the code nor how it hampers performance? I read this article recently and it changed how I think about package state variables. https://dave.cheney.net/2017/06/11/go-without-package-scoped-variables
| packed []byte | ||
| }{ | ||
| // Protocol limits | ||
| {reflect.ValueOf(0), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, |
There was a problem hiding this comment.
Please use hex strings here too.
There was a problem hiding this comment.
I actually think this should stay as is. Whats the reasoning for turning this into a hex string?
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
| ) | ||
|
|
||
| var ( | ||
| errBadBool error = errors.New("abi: improperly encoded boolean value") |
| } | ||
|
|
||
| if err == nil { | ||
| // bit of an ugly hack for hash type but I don't feel like finding a proper solution |
Signed-off-by: RJ Catalano <rj@monax.io>
Contains moving of certain functions to their own file for unpacking and correction of a type as well as a note about how to handle bool types in the near future.
Signed-off-by: RJ Catalano rj@monax.io