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

Fix isSigned returning true for unsigned txs #1042

Merged
merged 3 commits into from Jan 13, 2021
Merged

Conversation

ryanio
Copy link
Contributor

@ryanio ryanio commented Jan 12, 2021

This PR fixes tx.isSigned() as reported in #1041. It ensures that v,r,s aren't initialized in the constructor if undefined.

In writing some extra test cases, I also found the same bug in fromValuesArray that would initialize v,r,s to BN(0) if missing, so that is also fixed.

@codecov
Copy link

codecov bot commented Jan 12, 2021

Codecov Report

Merging #1042 (7a804e8) into master (5953e09) will increase coverage by 0.17%.
The diff coverage is 87.50%.

Impacted file tree graph

Flag Coverage Δ
block 77.65% <ø> (ø)
blockchain 77.92% <ø> (ø)
client 88.36% <ø> (-0.04%) ⬇️
common 92.11% <ø> (+0.24%) ⬆️
devp2p 82.60% <ø> (+0.26%) ⬆️
ethash 82.08% <ø> (ø)
tx 90.00% <87.50%> (+3.74%) ⬆️
vm 83.09% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

LGTM

data: data ?? emptyBuffer,
v: !v?.equals(emptyBuffer) ? new BN(v) : undefined,
r: !r?.equals(emptyBuffer) ? new BN(r) : undefined,
s: !s?.equals(emptyBuffer) ? new BN(s) : undefined,
Copy link
Member

Choose a reason for hiding this comment

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

I find these lines somewhat hard to read, replicated on REPL to trust them. No blocker though.

Copy link
Member

Choose a reason for hiding this comment

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

Is the question mark necessary? What does it do? The ! implies that it is defined, so I don't see what this ? should do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that this is not the easiest to read, but I found it the best option for this particular use. I am happy to change if there is a better suggestion.

Is the question mark necessary? What does it do? The ! implies that it is defined, so I don't see what this ? should do?

The ! negates the equals expression to ensure that it doesn't match emptyBuffer. It would imply that it is defined if it was in the position of the ?. The ? is optional chaining so that if it is undefined, it will revert to the false clause of the ternary and output undefined.

If ternary expressions could accept && clauses, then It could be written as: v && !v.equals(emptyBuffer) ? new BN(v) : undefined

Copy link
Member

Choose a reason for hiding this comment

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

Hi, the bug is actually not fixed (I should have noticed this earlier especially after your comment) - indeed, if v does not exist, the ? operator makes it false, but it is then negated - so it is true. There's always a BN created.

      let r = undefined
      console.log(!r?.equals(emptyBuffer) ? new BN(r) : undefined)

Prints <BN 0>

Copy link
Member

Choose a reason for hiding this comment

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

CC #1048

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmmm oh man I am just testing this again, I have to check why the new tests I added in this PR are passing okay then...

I am also wondering why in the comment above I was confused because ternary expression do accept && clauses (must have been confused with something else), so this example code I wrote might be more reasonable to read:

v && !v.equals(emptyBuffer) ? new BN(v) : undefined

I saw you updated in your PR to:

r !== undefined && !r?.equals(emptyBuffer) ? new BN(r) : undefined,

but in that case the optional chaining ? is not needed anymore (since it's already being checked for undefined) so this is a lot more understandable:

r !== undefined && !r.equals(emptyBuffer) ? new BN(r) : undefined,

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes good point! I still find it very hard to read though, so if there's a better alternative that'd be great :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opened a PR to fix this: #1077

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants