Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Use Option for CallContract member 'data' #1076

Merged
merged 7 commits into from Jul 18, 2019
Merged

Conversation

tcharding
Copy link
Collaborator

Currently we use bytes.default() to generate an empty value for byte array data member of the CallContract structure. This results in the value 0x being used (I do not know the exact reason why or where this 0x is found, please see note below on testing). Rust has options for such a case.

This needs testing, I have no idea what will now be expressed by the None in place of the original 0x.

Fixes: #999

@thomaseizinger
Copy link
Contributor

CallContract gets converted to ActionResponseBody::EthereumCallContract.

What do you think @D4nte and @luckysori in terms of the API?
I think we want a test that the data field just does not exist if we serialize ActionResponseBody::EthereumCallContract to JSON and the Option is None.

@D4nte
Copy link
Contributor

D4nte commented Jul 11, 2019

CallContract gets converted to ActionResponseBody::EthereumCallContract.

What do you think @D4nte and @luckysori in terms of the API?
I think we want a test that the data field just does not exist if we serialize ActionResponseBody::EthereumCallContract to JSON and the Option is None.

Yes a serialisation test when data is None to confirm it's not present in the JSON would be great please.

@thomaseizinger
Copy link
Contributor

CallContract gets converted to ActionResponseBody::EthereumCallContract.
What do you think @D4nte and @luckysori in terms of the API?
I think we want a test that the data field just does not exist if we serialize ActionResponseBody::EthereumCallContract to JSON and the Option is None.

Yes a serialisation test when data is None to confirm it's not present in the JSON would be great please.

@tcharding Go for that then :)

@tcharding tcharding force-pushed the 999-eth-refund-action branch 2 times, most recently from 7f87858 to 601aa41 Compare July 12, 2019 04:13
@tcharding tcharding marked this pull request as ready for review July 12, 2019 04:15
@tcharding
Copy link
Collaborator Author

The enum variant ActionResponseBody::EthereumCallContract uses a struct that has fields that mirror the ethereum::CallContract struct. This is duplication of knowledge, we can just use the ethereum::CallContract directly in the enum variant instead of a struct. This allows us to rely on the serialization testing done for CallContract. The bitcoin variants are probably able to be done the same way but its Friday afternoon so this is all for this PR :) Can be merged without doing the bitcoin variants IMO.

Copy link
Contributor

@D4nte D4nte left a comment

Choose a reason for hiding this comment

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

Great insight. Well done.

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

The enum variant ActionResponseBody::EthereumCallContract uses a struct that has fields that mirror the ethereum::CallContract struct.

Sorry but this is actually very much on purpose!

  1. IMO the CallContract and DeployContract structs are part of the COMIT core (we should soon start extracting that into its own crate to make that one clear). Hence, they shouldn't implement Serialize/Deserialize in order to not leak their inner structure. (Even if you were to implement Serialize, you should probably derive it in order to not having to write the implementation manually)
  2. For Bitcoin, the data is actually different because there is a signing step that happens between emitting the action from the state and serializing it to the user.

For consistency reasons, I'd therefore not re-use those structs there. IMO that causes more confusion than it adds value in terms of de-duplication. It will look like a job half done which cannot be finished because what was done for Ethereum cannot be done for Bitcoin.

I'd say this applies in general. Separate data structures for any external facing communication is key if you want to avoid accidental breaking API changes through actions like renamed fields.

In order to fully embrace that (it is not enforced currently), we probably should do something like this:

EthereumCallContract {
    contract_address: Http<ethereum_support::Address>,
    data: Http<ethereum_support::Bytes>,
    gas_limit: Http<ethereum_support::U256>,
    network: Http<ethereum_support::Network>,
    min_block_timestamp: Option<Http<Timestamp>>,
},

(Note that all types are wrapped in Http). This would allow us to actually pin down the API and would increase awareness about breaking API changes since you have to actively change the impl Serialize for Http<ethereum_support::U256> for example.

@D4nte
Copy link
Contributor

D4nte commented Jul 15, 2019

Taking over, I will just remove commit 601aa41

comit_node/src/swap_protocols/actions.rs Outdated Show resolved Hide resolved
comit_node/src/swap_protocols/actions.rs Outdated Show resolved Hide resolved
Copy link
Member

@da-kami da-kami left a comment

Choose a reason for hiding this comment

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

looks legit :)

Copy link
Member

@bonomat bonomat left a comment

Choose a reason for hiding this comment

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

I'm just blocking this as we have two approvals and we should not accidentally get the flaky test merged into master

@CLAassistant
Copy link

CLAassistant commented Jul 18, 2019

CLA assistant check
All committers have signed the CLA.

@D4nte

This comment has been minimized.

@D4nte D4nte changed the title Use Option for CallContract member 'data' [Do Not Merge] Use Option for CallContract member 'data' Jul 18, 2019
Tobin C. Harding and others added 7 commits July 18, 2019 10:39
Currently we use bytes.default() to generate an empty value for bytes array
`data` in the `CallContract` structure.  This results in the value `0x` being
used (I do not know the exact reason).  Rust has options for such a case.

This needs testing, I have no idea what will now be expressed by the `None` in
place of the original `0x` (FTR I do not know where the `Ox` is either :)

Fixes: #999
`CallContract` has some `Option<T>` fields.  When `CallContract` is serialized
we would like field members that have the value `None` to be _not_ in the JSON
string.

Add failing unit test for serialization of `CallContract` with `None` valued
fields.
`CallContract` has some `Option<T>` fields.  When `CallContract` is serialized
we would like field members that have the value `None` to be _not_ in the JSON
string.

Make failing unit test for serialization of `CallContract` with `None` valued
fields pass.
@D4nte D4nte changed the title [Do Not Merge] Use Option for CallContract member 'data' Use Option for CallContract member 'data' Jul 18, 2019
@D4nte D4nte requested review from bonomat, da-kami and D4nte July 18, 2019 00:48
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Good work Franck! 🎉

@@ -147,7 +147,7 @@ export class EthereumWallet {

public async sendEthTransactionTo(
to: string,
data: string = "0x0",
Copy link
Contributor

Choose a reason for hiding this comment

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

This stupid EthereumWallet has bitten us so many times now, on bobtimus as well as here in the tests.
I think we should extract what we have here into an NPM package and publish it. Basically an idiot-proof EthereumWallet that cannot be used wrongly :D

Copy link
Contributor

Choose a reason for hiding this comment

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

hahahahaha, yes I think it's worth considering and to give back to the community.

Copy link
Contributor

Choose a reason for hiding this comment

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

Created ticket here: coblox/bobtimus#78

Copy link
Member

@bonomat bonomat left a comment

Choose a reason for hiding this comment

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

Good job. That definitely was an easy-first-ticket :D

@D4nte D4nte merged commit 81a1fa4 into master Jul 18, 2019
@D4nte D4nte deleted the 999-eth-refund-action branch July 18, 2019 01:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set ethereum refund action data field to None
7 participants