Skip to content

accounts/abi: reorganizing package with small fixes#14610

Merged
karalabe merged 19 commits into
ethereum:masterfrom
VoR0220:smallFixesAndReorganizationOfABI
Jun 27, 2017
Merged

accounts/abi: reorganizing package with small fixes#14610
karalabe merged 19 commits into
ethereum:masterfrom
VoR0220:smallFixesAndReorganizationOfABI

Conversation

@VoR0220
Copy link
Copy Markdown
Member

@VoR0220 VoR0220 commented Jun 12, 2017

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

@VoR0220 VoR0220 requested a review from karalabe June 12, 2017 15:13
@GitCop
Copy link
Copy Markdown

GitCop commented Jun 12, 2017

Thank you for your contribution! Your commits seem to not adhere to the repository coding standards

  • Commit: 46578485bc904b6c5612c1963c49c7fa3ce59908
  • Commit subjects should be kept under 100 characters

Please check the contribution guidelines for more details.


This message was auto-generated by https://gitcop.com

@VoR0220 VoR0220 force-pushed the smallFixesAndReorganizationOfABI branch from 4657848 to 1bb38aa Compare June 12, 2017 15:16
…ion of name.

Signed-off-by: RJ Catalano <rj@monax.io>

get rid of some imports

Signed-off-by: RJ Catalano <rj@monax.io>
@VoR0220 VoR0220 force-pushed the smallFixesAndReorganizationOfABI branch from 1bb38aa to a52e1b4 Compare June 12, 2017 16:28
Comment thread accounts/abi/unpacking.go
@@ -0,0 +1,220 @@
// Copyright 2015 The go-ethereum Authors
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.

Please call this file unpack.go.

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.

Ah I see you made it like this because of packing.go. Please change that too to pack.go.

Comment thread accounts/abi/unpacking.go Outdated
}
}

// todo: this is inefficient for a bool, just look at the last cell, save yourself 32 iterations
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.

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?

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.

If you call a transaction with a bool parameter but set higher order bytes you've broken the ABI encoding :p

RJ Catalano added 3 commits June 13, 2017 17:58
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
Signed-off-by: RJ Catalano <rj@monax.io>
@VoR0220
Copy link
Copy Markdown
Member Author

VoR0220 commented Jun 13, 2017

@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>
@VoR0220
Copy link
Copy Markdown
Member Author

VoR0220 commented Jun 13, 2017

on second thought, better to be anal than regretful later.

Comment thread accounts/abi/unpack.go Outdated
case 1:
return true, nil
default:
return false, fmt.Errorf("abi: Improperly encoded boolean value.")
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.

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.

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.

Interesting. Will change.

Comment thread accounts/abi/unpack.go Outdated
return false, fmt.Errorf("abi: Improperly encoded boolean value.")
}
}
switch uint(b[31]) {
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.

There's no reason to convert this to uint

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.

Yea you're right. Forgot that its a uint8 at the end of the day.

Comment thread accounts/abi/unpack.go Outdated
}

func getBoolValue(b []byte) (bool, error) {
for i, byte := range b {
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.

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.

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.

Good idea

Comment thread accounts/abi/unpack.go Outdated

func getBoolValue(b []byte) (bool, error) {
for i, byte := range b {
if byte != 0 && i != 31 {
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.

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.

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.

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...

RJ Catalano added 9 commits June 14, 2017 08:34
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>
Comment thread accounts/abi/pack_test.go Outdated
input interface{}
output []byte
}{
{"uint16", uint16(2), pad([]byte{2}, 32, true)},
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.

Please use hex strings for input and output in all tests. IMHO pad(...) etc. are hard to read.

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.

The problem with hex strings is counting the length and im always unsure whether im putting in the proper length of characters.

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.

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.

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.

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.

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.

perhaps even throw in a function type in there to append multiple pad statements...hrmmmm....

Comment thread accounts/abi/unpack.go Outdated
improperEncoding := "abi: improperly encoded boolean value"
for i, b := range word {
if b != 0 && i != 31 {
return false, fmt.Errorf(improperEncoding)
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.

Please define errBadBool at package level and use it instead of creating a new error each time.

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.

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

Comment thread accounts/abi/pack_test.go Outdated
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}},
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.

Please use hex strings here too.

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.

I actually think this should stay as is. Whats the reasoning for turning this into a hex string?

RJ Catalano added 4 commits June 21, 2017 18:21
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>
Comment thread accounts/abi/error.go Outdated
)

var (
errBadBool error = errors.New("abi: improperly encoded boolean value")
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.

error type is not needed here

Comment thread accounts/abi/unpack_test.go Outdated
}

if err == nil {
// bit of an ugly hack for hash type but I don't feel like finding a proper solution
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.

erm? :P

Signed-off-by: RJ Catalano <rj@monax.io>
@karalabe karalabe merged commit 5421a08 into ethereum:master Jun 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants