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

Add the Rust Debug format as an output format when decoding XDR #357

Merged
merged 3 commits into from
Apr 9, 2024

Conversation

leighmcculloch
Copy link
Member

@leighmcculloch leighmcculloch commented Apr 5, 2024

What

Add the Rust Debug format as an output format when decoding XDR.

Why

At times it's helpful to decode XDR into a format that ignores all formatting logic and just displays what's there in memory in the raw types. This is particularly useful because the JSON format the tool otherwise outputs makes several optimisations to the output so that it is human readable, but that makes it not possible to know exactly how in memory the data is represented when decoded.

For example, when decoding AssetCodes the JSON format hides what types of asset codes they are, favouring the human readable representations that for most developers do not need the distinction between AssetCode4 and AssetCode12:

$ echo -n 'AAAAAkFCQ0RFAAAAAAAAAA==' | stellar-xdr d --type AssetCode
"ABCDE"

$ echo -n 'AAAAAUFCQ0Q=' | stellar-xdr d --type AssetCode
"ABCD"

Another example, when decoding source accounts the JSON format hides the significant structural differences between G and M accounts that again is not important to most developers:

$ echo -n 'AAAAANR1x2Z+IeB2/ud7aM/+sg+KeGaMwEM2uE3cBBarA/1f' | stellar-xdr d --type MuxedAccount
"GDKHLR3GPYQ6A5X6455WRT76WIHYU6DGRTAEGNVYJXOAIFVLAP6V6QIN"

$ echo -n 'AAABAAAAAM/bJtviN2dUl3INi2G89zHrWvur875dUPT4dcJfsLozlf7zYu4=' | stellar-xdr d --type MuxedAccount
"MA3WOVEXOIGYWYN464Y6WWX3VPZ34XKQ6T4HLQS7WC5DHFP66NRO4AAAADH5WJW34KJTO"

But when debugging the exact data stored in the XDR, it is helpful to be able to look at these exact representations:

$ echo -n 'AAAAAkFCQ0RFAAAAAAAAAA==' | stellar-xdr d --type AssetCode --output rust-debug-formatted
AssetCode(
    CreditAlphanum12(
        AssetCode12(414243444500000000000000),
    ),
)

$ echo -n 'AAAAAUFCQ0Q=' | stellar-xdr d --type AssetCode --output rust-debug-formatted
AssetCode(
    CreditAlphanum4(
        AssetCode4(41424344),
    ),
)

And also helpful with source accounts:

$ echo -n 'AAAAANR1x2Z+IeB2/ud7aM/+sg+KeGaMwEM2uE3cBBarA/1f' | stellar-xdr d --type MuxedAccount --output rust-debug-formatted
MuxedAccount(
    Ed25519(
        Uint256(d475c7667e21e076fee77b68cffeb20f8a78668cc04336b84ddc0416ab03fd5f),
    ),
)

$ echo -n 'AAABAAAAAM/bJtviN2dUl3INi2G89zHrWvur875dUPT4dcJfsLozlf7zYu4=' | stellar-xdr d --type MuxedAccount --output rust-debug-formatted
MuxedAccount(
    MuxedEd25519(
        MuxedAccountMed25519 {
            id: 892734987234,
            ed25519: Uint256(37675497720d8b61bcf731eb5afbabf3be5d50f4f875c25fb0ba3395fef362ee),
        },
    ),
)

I needed to view this type of output for some XDR today while looking at fixing a bug in the XDR lib, and so I coded this to solve that problem and it seems reasonably useful to keep around.

@leighmcculloch leighmcculloch marked this pull request as ready for review April 5, 2024 02:00
@leighmcculloch leighmcculloch enabled auto-merge April 9, 2024 10:16
@leighmcculloch leighmcculloch added this pull request to the merge queue Apr 9, 2024
Merged via the queue into main with commit a80c899 Apr 9, 2024
10 checks passed
@leighmcculloch leighmcculloch deleted the rust-debug branch April 9, 2024 16:24
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.

2 participants