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

Manually implement Debug on Witness #1913

Merged
merged 1 commit into from Jul 13, 2023

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Jun 20, 2023

The current derived debug implementation on Witness prints the content field as an array of integers. We can do better than this by manually implementing Debug.

With this applied Witness is printed as follows: (first line is {:?} and the next is {:#?}):

Using {:?}:

Witness: { indices: 3, indices_start: 8, witnesses: [[0x00], [0x02, 0x03], [0x04, 0x05]] }

Using {:#?}:

Witness: {
    indices: 3,
    indices_start: 8,
    witnesses: [
        [0x00],
        [0x02, 0x03],
        [0x04, 0x05],
     ],
}

apoelstra
apoelstra previously approved these changes Jun 21, 2023
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK a44857a

Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

Agree with changing the impl but I think it'd be much better to print it as individual elements using debug_list

@tcharding
Copy link
Member Author

Can do, thanks!

@tcharding
Copy link
Member Author

tcharding commented Jun 23, 2023

Ok, I responded too quickly. Now I've looked at it, unless I'm missing something, using debug_list does nothing better than the derived version. The point of this PR was that

Witness { content: [71, 48, 68, 2, 32, 60, 181, 14, 251, 92, 74, 154, 167, 253, 54, 154, 182, 244, 178, 38, 219, 153, 244, 79, 156, 97, 11, 91, 80, 188, 66, 243, 67, 166, 170, 64, 19, 2, 32, 26, 247, 145, 84, 46, 238, 108, 27, 17, 112, 94, 136, 149, 204, 90, 220, 54, 69, 137, 16, 220, 145, 170, 220, 175, 183, 106, 100, 120, 162, 155, 159, 1, 33, 2, 66, 232, 17, 230, 111, 209, 126, 154, 110, 78, 247, 114, 118, 108, 102, 141, 110, 5, 149, 202, 29, 127, 5, 131, 20, 139, 196, 96, 181, 117, 251, 253, 0, 0, 0, 0, 72, 0, 0, 0], witness_elements: 2, indices_start: 106 }

Is a bad debugging experience and my proposition was that hex is better than an array of decimal values.

@Kixunil
Copy link
Collaborator

Kixunil commented Jun 23, 2023

@tcharding did you map the values to display as hex? I think list of hex values would be the best, especially using alternate formatting which gets pretty-printed.

@apoelstra
Copy link
Member

I also think we should split up the witness fields using indices (though we can also print indices). So the total display would look something like

{
    witnesses: [ [0xab, 0xcd, 0xde], [0x00] ],
    indices: [3],
    indices_start: 1,
}

We should also do a Display impl which does just the witnesses as hex strings, like [abcdde, 00].

@tcharding
Copy link
Member Author

Nice, I can do all that. Thanks

@tcharding
Copy link
Member Author

tcharding commented Jun 26, 2023

With some outrageously ugly code I have it looking like this, please ack/nack the layout and I'll clean up the code. I had to manually implement the whole thing because the fmt helpers take mut self so cannot be used togther (debug_struct, and debug_list).

Top one is debug, bottom one is pretty debug.

Witness: { indices: 3, indices_start: 8, witnesses: [ [0x00], [0x02, 0x03], [0x04, 0x05] ]}
Witness: { 
    indices: 3,     
    indices_start: 8,     
    witnesses: [     
        [
            0x00,
        ],
        [
            0x02,
            0x03,
        ],
        [
            0x04,
            0x05,
        ],
     ],
}

@apoelstra
Copy link
Member

apoelstra commented Jun 28, 2023

Looks good to me!

Actually, I think I'd prefer if the pretty one still put all the witness bytes for each witness in one line.

@tcharding
Copy link
Member Author

Actually, I think I'd prefer if the pretty one still put all the witness bytes for each witness in one line.

I thought that myself, so if you've thought it too lets do it.

@tcharding
Copy link
Member Author

tcharding commented Jun 28, 2023

Using this format:

Witness: { indices: 3, indices_start: 8, witnesses: [[0x00], [0x02, 0x03], [0x04, 0x05]] }
Witness: { 
    indices: 3,     
    indices_start: 8,     
    witnesses: [     
        [0x00],
        [0x02, 0x03],
        [0x04, 0x05],
     ],
}

Note no space between the square brackets in witnesses on first line, ok with that ?

@tcharding tcharding force-pushed the 06-20-witness-debug branch 2 times, most recently from e95b116 to 7e00b59 Compare June 29, 2023 00:03
@tcharding
Copy link
Member Author

The code is pretty ugly still.

@tcharding
Copy link
Member Author

To verify output you can use:

    #[test]
    fn debug_witness() {
        let witness = Witness {
            witness_elements: 3,
            content: append_u32_vec(vec![1u8, 0, 2, 2, 3, 2, 4, 5], &[0, 2, 5]),
            indices_start: 8,
        };

        println!("Print in a line {:?}", witness);
        println!("Print in a line {:#?}", witness);
    }

@apoelstra
Copy link
Member

Using this format:

Witness: { indices: 3, indices_start: 8, witnesses: [[0x00], [0x02, 0x03], [0x04, 0x05]] }
Witness: { 
    indices: 3,     
    indices_start: 8,     
    witnesses: [     
        [0x00],
        [0x02, 0x03],
        [0x04, 0x05],
     ],
}

Note no space between the square brackets in witnesses on first line, ok with that ?

I'm happy with this. Would also be happy with any variations on the spacing.

The current derived debug implementation on `Witness` prints the content
field as an array of integers. We can do better than this by manually
implementing `Debug`.

With this applied `Witness` is printed as follows: (first line is `{:?}`
and the next is `{:#?}`):

Using `{:?}`:
```
Witness: { indices: 3, indices_start: 8, witnesses: [[0x00], [0x02, 0x03], [0x04, 0x05]] }
```

Using `{:#?}`:
```
Witness: {
    indices: 3,
    indices_start: 8,
    witnesses: [
        [0x00],
        [0x02, 0x03],
        [0x04, 0x05],
     ],
}
```
@tcharding
Copy link
Member Author

Rebased on master (to pick up #1927). No other changes.

@tcharding
Copy link
Member Author

Kix's seems out-of-action at the moment, @sanket1729 do you have time to take a look at this one please?

@sanket1729
Copy link
Member

On my list for tomorrow

@tcharding
Copy link
Member Author

Legend!

Copy link
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

tested ACK d45dbef. This would be helpful for debugging downstream.

@apoelstra
Copy link
Member

In the interest of getting this in, I'll ACK this. But the allocations are really not necessary and we should file an issue to get rid of them.

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK d45dbef

@apoelstra apoelstra merged commit a7fe0f5 into rust-bitcoin:master Jul 13, 2023
29 checks passed
@tcharding tcharding deleted the 06-20-witness-debug branch July 18, 2023 00:08
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.

None yet

4 participants