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 #566: serialize commit to hex #574

Merged
merged 2 commits into from
Jan 29, 2018
Merged

Conversation

mslipper
Copy link
Contributor

@mslipper mslipper commented Jan 4, 2018

No description provided.

@antiochp
Copy link
Member

antiochp commented Jan 4, 2018

We have the following representations of an Output in the api code -

  • Output
  • OutputPrintable
  • OutputSwitch

Seems like we may be able to consolidate these into just a single representation (this is likely what you are doing by using OutputPrintable and not Output).

As these are all http/json serialization (via the api) - it seems like everything should be "printable".
I don't think there is much need for non-printable versions of an output (whatever that actually means).

@mslipper
Copy link
Contributor Author

mslipper commented Jan 4, 2018

I can give that a shot. Full disclosure, I'm still a Rust noob so it'll take a little time for me to refactor all the places that these output types are used and to test them thoroughly.

@ignopeverell
Copy link
Contributor

Any interest in picking this up?

@antiochp
Copy link
Member

antiochp commented Jan 7, 2018

I'm actually deep in the output serialization code due to us picking #215 back up.
We are getting rid of the "output_by_commit" index - so we have very limited info available via the api when retrieving outputs by their commitments.
The only way to get heights, output_types etc. now will be to go via the byheight api endpoint and look outputs up via their containing block.

So I'd recommend holding off on changing any of the output serialization until #215 is merged (assuming we don't find any blockers with this proposed approach).

[Bringing this up because I'm expecting api codebase to change a fair bit in the next couple of days.]

@mslipper
Copy link
Contributor Author

mslipper commented Jan 8, 2018

Ok great. I'll hold off then until #215 is merged.

@antiochp
Copy link
Member

Ok - #215 ended up not getting merged.
But - we merged #615 instead.

So now we have the following "models" in api -

  • OutputPrintable (everything is basically a string here)
  • Utxo (just a wrapper for a commitment, replaces the old OutputSwitch model)

A Utxo currently looks like this -

curl "127.0.0.1:13413/v1/chain/utxos/byids?id=0960dd49bc81aa33fe094d98126ad3f759eb0ff5ee36c75ae82acd2486253df18f"
[{"commit":[9,96,221,73,188,129,170,51,254,9,77,152,18,106,211,247,89,235,15,245,238,54,199,90,232,42,205,36,134,37,61,241,143]}]

And OutputPrintables look like this (nested in a BlockPrintable here) -

curl "127.0.0.1:13413/v1/chain/utxos/byheight?startheight=1&endheight=1"
[
  {
    "header":{
      "hash":"7272a182aed9268920d4949929490511d2006864ee5bdd976d3a4595cf1c8f94",
      "height":1,
      "previous":"b83053b4b2e29376d6850de4fdaa60366300520bc6957cc69bec7089fcb1bfd8"
    },
    "outputs":[
      {
        "output_type":"Coinbase",
        "commit":"083eafae5d61a85ab07b12e1a51b3918d8e6de11fc6cde641d54af53608aa77b9f",
        "switch_commit_hash":"85daaf11011dc11e52af84ebe78e2f2d19cbdc76",
        "spent":false,
        "proof":null,
        "proof_hash":"ed6ba96009b86173bade6a9227ed60422916593fa32dd6d78b25b7a4eeef4946"
      }
    ]
  }
]

So -

  1. I think we should serialize the commit in the Utxo as a hex string (in the json).
  2. I do think we are currently approaching the serialization problem the wrong way in the api code - OutputPrintable (and other api models) should maintain its internal state using Commitment, Hash etc. And we should serialize these to hex strings when necessary (rather than maintaining state internally in strings etc.)

@mslipper
Copy link
Contributor Author

Agreed on your points. I can pick this back up and look into using Rust's serde library to automatically convert between hex internal representations of these objects.

@antiochp
Copy link
Member

antiochp commented Jan 18, 2018

Sounds like a good plan.
We're doing similar stuff in the wallet code (to serialize outputs out to wallet.dat).

For example -

grin/wallet/src/types.rs

Lines 246 to 253 in f1bbf53

impl serde::ser::Serialize for BlockIdentifier {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serializer.serialize_str(&self.0.to_hex())
}
}

Edit: One thing to point out - we wrapped a Hash up in a BlockIdentifier basically so we could add this custom serialization/deserialization to it.
It was not possible to add it directly to Hash itself because we do not want to assume we will always serialize/deserialize a Hash in this way (and for many uses we do not).

@mslipper
Copy link
Contributor Author

Great, this is really helpful!

@mslipper
Copy link
Contributor Author

@antiochp FYI will have this ready by EOD today.

api/src/types.rs Outdated
let hex_output = "{\
\"output_type\":\"Coinbase\",\
\"commit\":\"083eafae5d61a85ab07b12e1a51b3918d8e6de11fc6cde641d54af53608aa77b9f\",\
\"switch_commit_hash\":\"85daaf11011dc11e52af84ebe78e2f2d19cbdc76000000000000000000000000\",\

This comment was marked as spam.

@@ -153,29 +167,72 @@ pub enum OutputType {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Utxo {
/// The output commitment representing the amount
pub commit: pedersen::Commitment,
pub commit: PrintableCommitment,

This comment was marked as spam.

This comment was marked as spam.

Copy link
Member

@antiochp antiochp left a comment

Choose a reason for hiding this comment

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

👍

@@ -153,29 +167,72 @@ pub enum OutputType {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Utxo {
/// The output commitment representing the amount
pub commit: pedersen::Commitment,
pub commit: PrintableCommitment,

This comment was marked as spam.

spent: spent,
proof: proof,
output_type,
commit: output.commit,

This comment was marked as spam.

}
}

impl<'de> serde::de::Deserialize<'de> for OutputPrintable {

This comment was marked as spam.

@antiochp antiochp merged commit d754b8d into mimblewimble:master Jan 29, 2018
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.

3 participants