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

Remove assert, use typeforce typing #434

Merged
merged 14 commits into from
Aug 17, 2015
Merged

Remove assert, use typeforce typing #434

merged 14 commits into from
Aug 17, 2015

Conversation

dcousens
Copy link
Contributor

Will wait on ACK before merge.

ping @weilu, @jprichardson

Curious on your opinions about this...

// external dependent types
function BigInt (value) { return !typeforce.Null(value) && value.constructor === bigi }
function ECCurve (value) { return !typeforce.Null(value) && value.constructor === ecurve.Curve }
function ECPoint (value) { return !typeforce.Null(value) && value.constructor === ecurve.Point }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@weilu this does revert our previous duck typing change.
I've never had the issue of de-duping where it wasn't symptomatic of something potentially disastrous anyway, so I'm not sure why we allowed it in the first place.

ECPair and ecdsa are the only interfaces that accept bigi / ECPoint's, and we intend to remove their individual use entirely for 2.0.0, so this shouldn't even be an issue.

If you feel otherwise, I'll change these back to typeforce.quacksLike which is the identical behaviour to what was there before.

Copy link
Contributor

Choose a reason for hiding this comment

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

I remember an important reason why we went with duck typing is to deal with having multiple versions of the same library in the same app. I still think that's relevant so my preference is still duck typing.

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 remember an important reason why we went with duck typing is to deal with having multiple versions of the same library in the same app.

Right.

I've never had the issue of de-duping where it wasn't symptomatic of something potentially disastrous anyway, so I'm not sure why we allowed it in the first place.

I feel like such a situation is dangerous enough that we shouldn't let it occur though?
Especially since this duck typing is only really enforced on types that, if calculated wrong, directly impact where a user might send or receive funds.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the other breaking change, but, that has already been pointed out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This specific change was reverted in #450 as it was still an issue. quacksLike behaviour was restored.

@dcousens dcousens added this to the 2.0.0 milestone Aug 12, 2015
@dcousens
Copy link
Contributor Author

I'm happy to break out the typeforce typing changes if theres any issues, as the move away from assert is a much simpler decision and it does have highly tangible benefits.

@jprichardson
Copy link
Member

as the move away from assert is a much simpler decision and it does have highly tangible benefits.

I think for posterity, these benefits should be explicitly stated.

@weilu
Copy link
Contributor

weilu commented Aug 12, 2015

I don't get why you are getting rid of asserts. IMHO in most of the places, assert is still more direct and semantically straightforward. It's not like we are throwing any custom Error types for better error handling.

@dcousens
Copy link
Contributor Author

I think for posterity, these benefits should be explicitly stated.

  • In a production application, with over 30 high level dependencies, bitcoinjs-lib, bs58, bip39, bs58check, are the only dependencies using assert for error handling. All other conclusions aside, this is a specific pattern we have adopted only in our ecosystem.
  • assert explicitly makes code coverage difficult, because the branch is deeper down in the application. typeforce changes do not help this, however, the majority of typeforce changes were type checking. The majority of logic based changes that now exist as throw conditions are now visible in code coverage.
  • assert only accepts value arguments (like any function), therefore error messages are evaluated/built on every call. This has caused major performance problems for us in the past where JSON.stringify or toString has been more complex then expected and excessively slowed down applications.
  • assert.equal is more often used over assert.strictEqual, most likely because it is shorter to write. We actually have our linter fail our tests if we wrote this code normally, and yet, we have no qualms with this. While that could just be fixed by enforcing strictEqual, it is more verbose and often longer code than just writing out the explicit branch anyway.

It's not like we are throwing any custom Error types for better error handling.

The errors thrown are mostly the same, as out lined by the tests. The only places it did change was in the use of typeforce which has a consistent error format of Expected TYPE, got VALUETYPE VALUE.

@dcousens
Copy link
Contributor Author

Rebased on master. Added 94e16fb, 6002d9a.

@dcousens
Copy link
Contributor Author

@jprichardson @weilu, shall I merge or?

To clarify, in the end, I'd like to use typeforce less, but, for now, this is how it is.

@weilu
Copy link
Contributor

weilu commented Aug 17, 2015

@dcousens Given that the types of the errors and in some cases the messages are different, I wonder if we need to bump major for this?

@dcousens
Copy link
Contributor Author

@weilu I wasn't going to bother back porting this to 1.5.x, the change is worth it, but it doesn't fix/add anything such that it would be worth back porting.

@@ -780,7 +780,7 @@
},
{
"description": "Wrong key pair for multisig redeemScript",
"exception": "key pair cannot sign for this input",
"exception": "Key pair cannot sign for this input",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Breaking change - but it already is different to 1.5.x: 'privateKey cannot sign for this input'

@dcousens
Copy link
Contributor Author

I'd say there is only a few breaking changes, outlined above.

Even then:

  • ecdsa is no longer exposed.
  • TransactionBuilder.sign has changed input types, and error message already. Therefore, not an issue.

The only error message that has therefore changed in terms of the 1.5.x API, in this PR, is only Transaction.addInput, which has already had its API changed anyway.

The other errors should all be consistent as types.tuple should behave the same as the previous code.


if (hash.length !== 20) throw new TypeError('Invalid hash length')
if (version & ~0xff) throw new TypeError('Invalid version byte')
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This API is new, so its not a breaking change.

@weilu
Copy link
Contributor

weilu commented Aug 17, 2015

I'm fine with not back-porting this to 1.5.x but let's add these breaking changes into our 2.0 release note.

@weilu
Copy link
Contributor

weilu commented Aug 17, 2015

Good to merge

dcousens added a commit that referenced this pull request Aug 17, 2015
Remove assert, use typeforce typing
@dcousens dcousens merged commit 21980b6 into master Aug 17, 2015
@dcousens dcousens deleted the noassert branch August 17, 2015 12:03
@dcousens
Copy link
Contributor Author

Thanks @weilu, I added the breaking change tag so we can easily build that release note by reviewing all issues under it later.

@dcousens
Copy link
Contributor Author

Follow up 5d2abb5 for HDNode.

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.

3 participants